From e8c4c566e333c84ea9bd018881206a2fe40f409e Mon Sep 17 00:00:00 2001 From: Alessandro Manighetti <76836856+xtimmy86x@users.noreply.github.com> Date: Fri, 20 Oct 2023 10:34:19 +0200 Subject: [PATCH] feat: add missing tests for binary_sensor entities (#90) https://github.com/palazzem/ha-econnect-alarm/pull/90 --- tests/conftest.py | 3 ++ tests/fixtures/responses.py | 8 ++++ tests/test_binary_sensors.py | 73 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 6ea00b1..88011a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ from custom_components.econnect_metronet import async_setup from custom_components.econnect_metronet.alarm_control_panel import EconnectAlarm +from custom_components.econnect_metronet.const import DOMAIN from custom_components.econnect_metronet.coordinator import AlarmCoordinator from custom_components.econnect_metronet.devices import AlarmDevice @@ -25,6 +26,7 @@ async def hass(hass): """ await async_setup(hass, {}) hass.data["custom_components"] = None + hass.data[DOMAIN]["test_entry_id"] = {} yield hass @@ -139,6 +141,7 @@ def __init__(self): } # Options at configuration-time + self.entry_id = "test_entry_id" self.options = {} return MockConfigEntry() diff --git a/tests/fixtures/responses.py b/tests/fixtures/responses.py index 7b7a399..4e7700f 100644 --- a/tests/fixtures/responses.py +++ b/tests/fixtures/responses.py @@ -103,6 +103,14 @@ def test_client_get_sectors_status(server): "Created": "/Date(1546004147490+0100)/", "Version": "AAAAAAAAgRs=" }, + { + "AccountId": 1, + "Class": 9, + "Index": 3, + "Description": "S4 Garage", + "Created": "/Date(1546004147491+0100)/", + "Version": "AAAAAAAAgRt=" + }, { "AccountId": 1, "Class": 10, diff --git a/tests/test_binary_sensors.py b/tests/test_binary_sensors.py index 120d579..0b16c37 100644 --- a/tests/test_binary_sensors.py +++ b/tests/test_binary_sensors.py @@ -7,7 +7,56 @@ AlertSensor, InputSensor, SectorSensor, + async_setup_entry, ) +from custom_components.econnect_metronet.const import DOMAIN + + +@pytest.mark.asyncio +async def test_async_setup_entry_in_use(hass, config_entry, alarm_device, coordinator): + # Ensure the async setup loads only sectors and sensors that are in use + hass.data[DOMAIN][config_entry.entry_id] = { + "device": alarm_device, + "coordinator": coordinator, + } + + # Test + def ensure_only_in_use(sensors): + assert len(sensors) == 31 + + await async_setup_entry(hass, config_entry, ensure_only_in_use) + + +@pytest.mark.asyncio +async def test_async_setup_entry_unused_input(hass, config_entry, alarm_device, coordinator): + # Ensure the async setup don't load sensors that are not in use + hass.data[DOMAIN][config_entry.entry_id] = { + "device": alarm_device, + "coordinator": coordinator, + } + + # Test + def ensure_unused_sensors(sensors): + for sensor in sensors: + assert sensor._name not in ["Outdoor Sensor 3"] + + await async_setup_entry(hass, config_entry, ensure_unused_sensors) + + +@pytest.mark.asyncio +async def test_async_setup_entry_unused_sector(hass, config_entry, alarm_device, coordinator): + # Ensure the async setup don't load sectors that are not in use + hass.data[DOMAIN][config_entry.entry_id] = { + "device": alarm_device, + "coordinator": coordinator, + } + + # Test + def ensure_unused_sectors(sensors): + for sensor in sensors: + assert sensor._name not in ["S4 Garage"] + + await async_setup_entry(hass, config_entry, ensure_unused_sectors) class TestAlertSensor: @@ -69,12 +118,24 @@ def test_binary_sensor_entity_id_with_system_name(self, hass, config_entry, alar entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device) assert entity.entity_id == "econnect_metronet.econnect_metronet_home_has_anomalies" + def test_binary_sensor_unique_id(self, hass, config_entry, alarm_device): + # Ensure the alert has the right unique ID + coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet") + entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device) + assert entity.unique_id == "test_id" + def test_binary_sensor_icon(self, hass, config_entry, alarm_device): # Ensure the sensor has the right icon coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet") entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device) assert entity.icon == "hass:alarm-light" + def test_binary_sensor_device_class(self, hass, config_entry, alarm_device): + # Ensure the sensor has the right device class + coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet") + entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device) + assert entity.device_class == "problem" + class TestInputSensor: def test_binary_sensor_name(self, hass, config_entry, alarm_device): @@ -103,6 +164,12 @@ def test_binary_sensor_entity_id_with_system_name(self, hass, config_entry, alar entity = InputSensor("test_id", 1, config_entry, "1 Tamper Sirena", coordinator, alarm_device) assert entity.entity_id == "econnect_metronet.econnect_metronet_home_1_tamper_sirena" + def test_binary_sensor_unique_id(self, hass, config_entry, alarm_device): + # Ensure the sensor has the right unique ID + coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet") + entity = InputSensor("test_id", 1, config_entry, "1 Tamper Sirena", coordinator, alarm_device) + assert entity.unique_id == "test_id" + def test_binary_sensor_icon(self, hass, config_entry, alarm_device): # Ensure the sensor has the right icon coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet") @@ -149,6 +216,12 @@ def test_binary_sensor_input_entity_id_with_system_name(self, hass, config_entry entity = SectorSensor("test_id", 1, config_entry, "1 S1 Living Room", coordinator, alarm_device) assert entity.entity_id == "econnect_metronet.econnect_metronet_home_1_s1_living_room" + def test_binary_sensor_input_unique_id(self, hass, config_entry, alarm_device): + # Ensure the sensor has the right unique ID + coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet") + entity = SectorSensor("test_id", 1, config_entry, "1 S1 Living Room", coordinator, alarm_device) + assert entity.unique_id == "test_id" + def test_binary_sensor_icon(self, hass, config_entry, alarm_device): # Ensure the sensor has the right icon coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")