From 8e9f60428f9882c8b0926ac9c3c95b8b9882a346 Mon Sep 17 00:00:00 2001 From: Alessandro Manighetti <76836856+xtimmy86x@users.noreply.github.com> Date: Fri, 20 Oct 2023 17:35:51 +0200 Subject: [PATCH] fix: restore previous unique id structure in `AlertSensor` (#93) https://github.com/palazzem/ha-econnect-alarm/pull/93 --- .../econnect_metronet/binary_sensor.py | 2 +- tests/test_binary_sensors.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/custom_components/econnect_metronet/binary_sensor.py b/custom_components/econnect_metronet/binary_sensor.py index d8969c1..48c7a18 100644 --- a/custom_components/econnect_metronet/binary_sensor.py +++ b/custom_components/econnect_metronet/binary_sensor.py @@ -47,7 +47,7 @@ async def async_setup_entry( # Iterate through the alerts of the provided device and create AlertSensor objects for alert_id, name in device.alerts: - unique_id = f"{entry.entry_id}_{DOMAIN}_{q.ALERTS}_{alert_id}" + unique_id = f"{entry.entry_id}_{DOMAIN}_{name}" sensors.append(AlertSensor(unique_id, alert_id, entry, name, coordinator, device)) async_add_entities(sensors) diff --git a/tests/test_binary_sensors.py b/tests/test_binary_sensors.py index 0b16c37..9d65526 100644 --- a/tests/test_binary_sensors.py +++ b/tests/test_binary_sensors.py @@ -59,6 +59,21 @@ def ensure_unused_sectors(sensors): await async_setup_entry(hass, config_entry, ensure_unused_sectors) +@pytest.mark.asyncio +async def test_async_setup_entry_alerts_unique_id(hass, config_entry, alarm_device, coordinator): + # Regression test: changing this unique ID format is a breaking change + hass.data[DOMAIN][config_entry.entry_id] = { + "device": alarm_device, + "coordinator": coordinator, + } + + # Test + def ensure_unused_sectors(sensors): + assert sensors[30].unique_id == "test_entry_id_econnect_metronet_tamper_led" + + await async_setup_entry(hass, config_entry, ensure_unused_sectors) + + class TestAlertSensor: def test_binary_sensor_is_on(self, hass, config_entry, alarm_device): # Ensure the sensor attribute is_on has the right status True