Skip to content

Commit 6bfa0d1

Browse files
committed
feat(mqtt): make discovery entity IDs match the manual mqtt_sensors.yaml
Prefix the discovery object_id with tesla_ so Home Assistant generates the same entity IDs as the documented manual configuration (e.g. sensor.tesla_speed instead of sensor.speed), avoiding broken dashboards and automations on migration. Drop the _km suffix from the battery range sensors to match the manual unique_ids. Document that the manual mqtt_sensors.yaml must be removed before enabling discovery to avoid duplicate unique_id errors. Addresses review comment on #5543.
1 parent 1db061e commit 6bfa0d1

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

lib/teslamate/mqtt/pubsub/home_assistant.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistant do
4747
config
4848
|> resolve_topics(car_id, namespace)
4949
|> Map.put(:unique_id, "teslamate_#{car_id}_#{object_id}")
50-
|> Map.put(:object_id, object_id)
50+
|> Map.put(:object_id, "tesla_#{object_id}")
5151
|> Map.put(:device, device)
5252
|> Jason.encode!()
5353

@@ -219,23 +219,23 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistant do
219219
unit_of_measurement: "km",
220220
icon: "mdi:counter"
221221
}},
222-
{"sensor", "est_battery_range_km",
222+
{"sensor", "est_battery_range",
223223
%{
224224
state_topic_key: :est_battery_range_km,
225225
name: "Est Battery Range",
226226
device_class: "distance",
227227
unit_of_measurement: "km",
228228
icon: "mdi:gauge"
229229
}},
230-
{"sensor", "rated_battery_range_km",
230+
{"sensor", "rated_battery_range",
231231
%{
232232
state_topic_key: :rated_battery_range_km,
233233
name: "Rated Battery Range",
234234
device_class: "distance",
235235
unit_of_measurement: "km",
236236
icon: "mdi:gauge"
237237
}},
238-
{"sensor", "ideal_battery_range_km",
238+
{"sensor", "ideal_battery_range",
239239
%{
240240
state_topic_key: :ideal_battery_range_km,
241241
name: "Ideal Battery Range",

test/teslamate/mqtt/pubsub/home_assistant_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,27 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistantTest do
134134
end)
135135
end
136136

137+
test "entity ids match the manual mqtt_sensors.yaml naming", %{test: name} do
138+
publisher_name = start_publisher(name)
139+
140+
:ok = HomeAssistant.publish(@summary, [car_id: 0], {MqttPublisherMock, publisher_name})
141+
142+
find_config("homeassistant/sensor/teslamate_0/speed/config", fn decoded ->
143+
assert decoded["object_id"] == "tesla_speed"
144+
assert decoded["unique_id"] == "teslamate_0_speed"
145+
end)
146+
147+
find_config("homeassistant/sensor/teslamate_0/est_battery_range/config", fn decoded ->
148+
assert decoded["object_id"] == "tesla_est_battery_range"
149+
assert decoded["unique_id"] == "teslamate_0_est_battery_range"
150+
end)
151+
152+
find_config("homeassistant/binary_sensor/teslamate_0/healthy/config", fn decoded ->
153+
assert decoded["object_id"] == "tesla_healthy"
154+
assert decoded["unique_id"] == "teslamate_0_healthy"
155+
end)
156+
end
157+
137158
test "clear publishes empty payloads per entity", %{test: name} do
138159
publisher_name = start_publisher(name)
139160

website/docs/integrations/home_assistant.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ vehicle summary, where `<node>` is `teslamate`. The device is grouped under the
4141
`teslamate_car_<car_id>` identifier, and the entity IDs match those produced by
4242
the manual `mqtt_sensors.yaml` below.
4343

44+
:::note
45+
46+
The `unique_id`s of the discovered entities match those of the manual
47+
`mqtt_sensors.yaml`, so enabling discovery while the manual configuration is
48+
still active will produce duplicate-ID errors in Home Assistant. Remove the
49+
manual `mqtt: !include mqtt_sensors.yaml` section from `configuration.yaml`
50+
(and the entities it created) before enabling discovery.
51+
52+
:::
53+
4454
On startup, discovery configs are also cleared for cars that are no longer
4555
tracked, e.g. because they were removed from the Tesla account or because
4656
logging was disabled, so their entities are removed from Home Assistant.

0 commit comments

Comments
 (0)