Skip to content

Commit

Permalink
Deprecate old MQTT topics
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmay committed Mar 31, 2024
1 parent 3e05f68 commit 4ced27c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 104 deletions.
32 changes: 18 additions & 14 deletions lib/teslamate/mqtt/pubsub/vehicle_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
@impl true
def handle_info(%Summary{} = summary, state) do
values =
summary
|> Map.from_struct()
|> Map.drop([:car])
%{}
|> add_simple_values(summary)
|> add_car_latitude_longitude(summary)
|> add_geofence(summary)
|> add_active_route(summary)
Expand Down Expand Up @@ -80,6 +79,22 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
end)
end

@simple_values ~w(
display_name state since healthy latitude longitude heading battery_level charging_state usable_battery_level
ideal_battery_range_km est_battery_range_km rated_battery_range_km charge_energy_added
speed outside_temp inside_temp is_climate_on is_preconditioning locked sentry_mode
plugged_in scheduled_charging_start_time charge_limit_soc charger_power windows_open doors_open
odometer shift_state charge_port_door_open time_to_full_charge charger_phases
charger_actual_current charger_voltage version update_available update_version is_user_present
model trim_badging exterior_color wheel_type spoiler_type trunk_open frunk_open elevation power
charge_current_request charge_current_request_max tpms_pressure_fl tpms_pressure_fr tpms_pressure_rl tpms_pressure_rr
tpms_soft_warning_fl tpms_soft_warning_fr tpms_soft_warning_rl tpms_soft_warning_rr climate_keeper_mode
)a

defp add_simple_values(map, %Summary{} = summary) do
Map.merge(map, Map.take(summary, @simple_values))
end

defp add_car_latitude_longitude(map, %Summary{} = summary) do
lat_lng =
case {summary.latitude, summary.longitude} do
Expand All @@ -106,7 +121,6 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
end

defp add_geofence(map, %Summary{} = summary) do
# This overwrites the existing geofence value in map.
case summary.geofence do
nil ->
Map.put(map, :geofence, Application.get_env(:teslamate, :default_geofence))
Expand All @@ -117,7 +131,6 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
end

defp add_active_route(map, %Summary{active_route_destination: nil}) do
# This overwrites the existing values in map.
error =
%{
error: "No active route available"
Expand All @@ -130,18 +143,13 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
active_route_destination: "nil",
active_route_latitude: "nil",
active_route_longitude: "nil",
active_route_energy_at_arrival: "nil",
active_route_miles_to_arrival: "nil",
active_route_minutes_to_arrival: "nil",
active_route_traffic_minutes_delay: "nil",
active_route_location: error,
active_route: error
}
)
end

defp add_active_route(map, %Summary{} = summary) do
# This overwrites the existing values in map.
location =
%{
latitude: summary.active_route_latitude,
Expand All @@ -168,10 +176,6 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriber do
active_route_destination: summary.active_route_destination,
active_route_latitude: summary.active_route_latitude,
active_route_longitude: summary.active_route_longitude,
active_route_energy_at_arrival: summary.active_route_energy_at_arrival,
active_route_miles_to_arrival: summary.active_route_miles_to_arrival,
active_route_minutes_to_arrival: summary.active_route_minutes_to_arrival,
active_route_traffic_minutes_delay: summary.active_route_traffic_minutes_delay,
active_route_location: location,
active_route: active_route
})
Expand Down
18 changes: 3 additions & 15 deletions test/teslamate/mqtt/pubsub/vehicle_subscriber_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do
for key <- [
:active_route_destination,
:active_route_longitude,
:active_route_latitude,
:active_route_energy_at_arrival,
:active_route_miles_to_arrival,
:active_route_minutes_to_arrival,
:active_route_traffic_minutes_delay
:active_route_latitude
] do
topic = "teslamate/cars/0/#{key}"
assert_receive {MqttPublisherMock, {:publish, ^topic, "nil", [retain: true, qos: 1]}}
Expand Down Expand Up @@ -183,11 +179,7 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do
for key <- [
:active_route_destination,
:active_route_longitude,
:active_route_latitude,
:active_route_energy_at_arrival,
:active_route_miles_to_arrival,
:active_route_minutes_to_arrival,
:active_route_traffic_minutes_delay
:active_route_latitude
] do
topic = "teslamate/cars/0/#{key}"
assert_receive {MqttPublisherMock, {:publish, ^topic, "nil", [retain: true, qos: 1]}}
Expand Down Expand Up @@ -271,11 +263,7 @@ defmodule TeslaMate.Mqtt.PubSub.VehicleSubscriberTest do
for key <- [
:active_route_destination,
:active_route_longitude,
:active_route_latitude,
:active_route_energy_at_arrival,
:active_route_miles_to_arrival,
:active_route_minutes_to_arrival,
:active_route_traffic_minutes_delay
:active_route_latitude
] do
topic = "teslamate/account_0/cars/0/#{key}"
assert_receive {MqttPublisherMock, {:publish, ^topic, "nil", [retain: true, qos: 1]}}
Expand Down
6 changes: 1 addition & 5 deletions test/teslamate/vehicles/vehicle_sync_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ defmodule TeslaMate.Vehicles.VehicleSyncTest do
for key <- [
:active_route_destination,
:active_route_longitude,
:active_route_latitude,
:active_route_energy_at_arrival,
:active_route_miles_to_arrival,
:active_route_minutes_to_arrival,
:active_route_traffic_minutes_delay
:active_route_latitude
] do
topic = "teslamate/cars/#{car.id}/#{key}"
assert_receive {MqttPublisherMock, {:publish, ^topic, "nil", [retain: true, qos: 1]}}
Expand Down
Loading

0 comments on commit 4ced27c

Please sign in to comment.