From b7bcdbef0adf948fd91216bf87588abe99649b7e Mon Sep 17 00:00:00 2001 From: Brian May Date: Mon, 11 Mar 2024 13:44:46 +1100 Subject: [PATCH 1/2] Add location topic Fixes #3660. --- .../mqtt/pubsub/vehicle_subscriber.ex | 30 +++++++++++++++++++ .../mqtt/pubsub/vehicle_subscriber_test.exs | 10 ++++++- test/teslamate/vehicles/vehicle_sync_test.exs | 8 +++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex b/lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex index a8e3081a58..c770e714c9 100644 --- a/lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex +++ b/lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex @@ -74,6 +74,36 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do nil end) + if state.last_summary == nil or + state.last_summary.latitude != summary.latitude or + state.last_summary.longitude != summary.longitude do + lat_lng = + case {summary.latitude, summary.longitude} do + {nil, _} -> nil + {_, nil} -> nil + {%Decimal{} = lat, %Decimal{} = lon} -> {Decimal.to_float(lat), Decimal.to_float(lon)} + {lat, lon} -> {lat, lon} + end + + case lat_lng do + nil -> + nil + + {lat, lon} -> + location = + %{ + latitude: lat, + longitude: lon + } + |> Jason.encode!() + + case publish({"location", location}, state) do + :ok -> nil + {:error, reason} -> Logger.warning("Failed to publish location: #{inspect(reason)}") + end + end + end + {:noreply, %State{state | last_summary: summary}} end diff --git a/test/teslamate/mqtt/pubsub/vehicle_subscriber_test.exs b/test/teslamate/mqtt/pubsub/vehicle_subscriber_test.exs index 9c6c720baa..93199a7a01 100644 --- a/test/teslamate/mqtt/pubsub/vehicle_subscriber_test.exs +++ b/test/teslamate/mqtt/pubsub/vehicle_subscriber_test.exs @@ -95,6 +95,14 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do assert_receive {MqttPublisherMock, {:publish, ^topic, "", [retain: true, qos: 1]}} end + assert_receive {MqttPublisherMock, + {:publish, "teslamate/cars/0/location", data, [retain: true, qos: 1]}} + + assert Jason.decode!(data) == %{ + "latitude" => 37.889602, + "longitude" => 41.129182 + } + refute_receive _ end @@ -158,7 +166,7 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do geofence = %GeoFence{id: 0, name: "Home", latitude: 0.0, longitude: 0.0, radius: 20} other_geofence = %GeoFence{id: 0, name: "Work", latitude: 0.0, longitude: 0.0, radius: 20} - # Send geofence + # Send geofence send(pid, %Summary{geofence: geofence, version: "1"}) assert_receive {MqttPublisherMock, {:publish, "teslamate/cars/0/geofence", "Home", _}} assert_receive {MqttPublisherMock, {:publish, "teslamate/cars/0/version", "1", _}} diff --git a/test/teslamate/vehicles/vehicle_sync_test.exs b/test/teslamate/vehicles/vehicle_sync_test.exs index dd27138681..4550f6656e 100644 --- a/test/teslamate/vehicles/vehicle_sync_test.exs +++ b/test/teslamate/vehicles/vehicle_sync_test.exs @@ -149,6 +149,14 @@ defmodule TeslaMate.Vehicles.VehicleSyncTest do assert_receive {MqttPublisherMock, {:publish, ^topic, ^data, [retain: true, qos: 1]}} end + topic = "teslamate/cars/#{car.id}/location" + assert_receive {MqttPublisherMock, {:publish, ^topic, data, [retain: true, qos: 1]}} + + assert Jason.decode!(data) == %{ + "latitude" => 37.889544, + "longitude" => 41.128817 + } + refute_receive _ end end From 00f36205444d80c5bb10c730337840b8c6fe79e1 Mon Sep 17 00:00:00 2001 From: Jakob Lichterfeld Date: Mon, 18 Mar 2024 08:32:54 +0100 Subject: [PATCH 2/2] doc: update mqtt topics with new location topic --- website/docs/integrations/mqtt.md | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/integrations/mqtt.md b/website/docs/integrations/mqtt.md index 613fb4f9a1..07d643fa7a 100644 --- a/website/docs/integrations/mqtt.md +++ b/website/docs/integrations/mqtt.md @@ -29,6 +29,7 @@ Vehicle data will be published to the following topics: | | | | | `teslamate/cars/$car_id/latitude` | 35.278131 | Last reported car latitude | | `teslamate/cars/$car_id/longitude` | 29.744801 | Last reported car longitude | +| `teslamate/cars/$car_id/location` | "latitude": 37.889544, "longitude: 41.128817 | Last reported car location | | `teslamate/cars/$car_id/shift_state` | D | Current/Last Shift State (D/N/R/P) | | `teslamate/cars/$car_id/power` | -9 | Current battery power in watts. Positive value on discharge, negative value on charge | | `teslamate/cars/$car_id/speed` | 12 | Current Speed in km/h |