From e591291cbe3fe01deda4e7c8939b54313b6dd9ef Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Wed, 25 Feb 2026 07:20:19 -0800 Subject: [PATCH] Add platform tests for aladdin_connect cover and sensor (#164011) --- .../aladdin_connect/quality_scale.yaml | 4 +- .../aladdin_connect/snapshots/test_cover.ambr | 52 +++++++ .../snapshots/test_sensor.ambr | 55 +++++++ .../components/aladdin_connect/test_cover.py | 135 ++++++++++++++++++ .../components/aladdin_connect/test_sensor.py | 59 ++++++++ 5 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 tests/components/aladdin_connect/snapshots/test_cover.ambr create mode 100644 tests/components/aladdin_connect/snapshots/test_sensor.ambr create mode 100644 tests/components/aladdin_connect/test_cover.py create mode 100644 tests/components/aladdin_connect/test_sensor.py diff --git a/homeassistant/components/aladdin_connect/quality_scale.yaml b/homeassistant/components/aladdin_connect/quality_scale.yaml index d857f1dcdc2..61bd6fc3e42 100644 --- a/homeassistant/components/aladdin_connect/quality_scale.yaml +++ b/homeassistant/components/aladdin_connect/quality_scale.yaml @@ -37,9 +37,7 @@ rules: log-when-unavailable: todo parallel-updates: todo reauthentication-flow: done - test-coverage: - status: todo - comment: Platform tests for cover and sensor need to be implemented to reach 95% coverage. + test-coverage: done # Gold devices: done diff --git a/tests/components/aladdin_connect/snapshots/test_cover.ambr b/tests/components/aladdin_connect/snapshots/test_cover.ambr new file mode 100644 index 00000000000..d9d9ff8ace6 --- /dev/null +++ b/tests/components/aladdin_connect/snapshots/test_cover.ambr @@ -0,0 +1,52 @@ +# serializer version: 1 +# name: test_cover_entities[cover.test_door-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.test_door', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': None, + 'platform': 'aladdin_connect', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'test_device_id-1', + 'unit_of_measurement': None, + }) +# --- +# name: test_cover_entities[cover.test_door-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'garage', + 'friendly_name': 'Test Door', + 'supported_features': , + }), + 'context': , + 'entity_id': 'cover.test_door', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'closed', + }) +# --- diff --git a/tests/components/aladdin_connect/snapshots/test_sensor.ambr b/tests/components/aladdin_connect/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..7f888c1f554 --- /dev/null +++ b/tests/components/aladdin_connect/snapshots/test_sensor.ambr @@ -0,0 +1,55 @@ +# serializer version: 1 +# name: test_sensor_entities[sensor.test_door_battery-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_door_battery', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Battery', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Battery', + 'platform': 'aladdin_connect', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test_device_id-1-battery_level', + 'unit_of_measurement': '%', + }) +# --- +# name: test_sensor_entities[sensor.test_door_battery-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'Test Door Battery', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.test_door_battery', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100', + }) +# --- diff --git a/tests/components/aladdin_connect/test_cover.py b/tests/components/aladdin_connect/test_cover.py new file mode 100644 index 00000000000..173c0736371 --- /dev/null +++ b/tests/components/aladdin_connect/test_cover.py @@ -0,0 +1,135 @@ +"""Tests for the Aladdin Connect cover platform.""" + +from unittest.mock import AsyncMock, patch + +import aiohttp +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.cover import ( + DOMAIN as COVER_DOMAIN, + SERVICE_CLOSE_COVER, + SERVICE_OPEN_COVER, +) +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration + +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform + +ENTITY_ID = "cover.test_door" + + +async def _setup(hass: HomeAssistant, entry: MockConfigEntry) -> None: + """Set up integration with only the cover platform.""" + with patch("homeassistant.components.aladdin_connect.PLATFORMS", [Platform.COVER]): + await init_integration(hass, entry) + + +async def test_cover_entities( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the cover entity states and attributes.""" + await _setup(hass, mock_config_entry) + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_open_cover( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_aladdin_connect_api: AsyncMock, +) -> None: + """Test opening the cover.""" + await _setup(hass, mock_config_entry) + await hass.services.async_call( + COVER_DOMAIN, + SERVICE_OPEN_COVER, + {ATTR_ENTITY_ID: ENTITY_ID}, + blocking=True, + ) + mock_aladdin_connect_api.open_door.assert_called_once_with("test_device_id", 1) + + +async def test_close_cover( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_aladdin_connect_api: AsyncMock, +) -> None: + """Test closing the cover.""" + await _setup(hass, mock_config_entry) + await hass.services.async_call( + COVER_DOMAIN, + SERVICE_CLOSE_COVER, + {ATTR_ENTITY_ID: ENTITY_ID}, + blocking=True, + ) + mock_aladdin_connect_api.close_door.assert_called_once_with("test_device_id", 1) + + +@pytest.mark.parametrize( + ("status", "expected_closed", "expected_opening", "expected_closing"), + [ + ("closed", True, False, False), + ("open", False, False, False), + ("opening", False, True, False), + ("closing", False, False, True), + ], +) +async def test_cover_states( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_aladdin_connect_api: AsyncMock, + status: str, + expected_closed: bool, + expected_opening: bool, + expected_closing: bool, +) -> None: + """Test cover state properties.""" + mock_aladdin_connect_api.get_doors.return_value[0].status = status + await _setup(hass, mock_config_entry) + state = hass.states.get(ENTITY_ID) + assert state is not None + assert (state.state == "closed") == expected_closed + assert (state.state == "opening") == expected_opening + assert (state.state == "closing") == expected_closing + + +async def test_cover_none_status( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_aladdin_connect_api: AsyncMock, +) -> None: + """Test cover state when status is None.""" + mock_aladdin_connect_api.get_doors.return_value[0].status = None + await _setup(hass, mock_config_entry) + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state == "unknown" + + +async def test_cover_unavailable( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_aladdin_connect_api: AsyncMock, + freezer: FrozenDateTimeFactory, +) -> None: + """Test cover becomes unavailable when coordinator update fails.""" + await _setup(hass, mock_config_entry) + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state != STATE_UNAVAILABLE + + mock_aladdin_connect_api.update_door.side_effect = aiohttp.ClientError() + freezer.tick(15) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state == STATE_UNAVAILABLE diff --git a/tests/components/aladdin_connect/test_sensor.py b/tests/components/aladdin_connect/test_sensor.py new file mode 100644 index 00000000000..7f0b9a26016 --- /dev/null +++ b/tests/components/aladdin_connect/test_sensor.py @@ -0,0 +1,59 @@ +"""Tests for the Aladdin Connect sensor platform.""" + +from unittest.mock import AsyncMock, patch + +import aiohttp +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import STATE_UNAVAILABLE, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration + +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform + +ENTITY_ID = "sensor.test_door_battery" + + +async def _setup(hass: HomeAssistant, entry: MockConfigEntry) -> None: + """Set up integration with only the sensor platform.""" + with patch("homeassistant.components.aladdin_connect.PLATFORMS", [Platform.SENSOR]): + await init_integration(hass, entry) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensor_entities( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the sensor entity states and attributes.""" + await _setup(hass, mock_config_entry) + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensor_unavailable( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_aladdin_connect_api: AsyncMock, + freezer: FrozenDateTimeFactory, +) -> None: + """Test sensor becomes unavailable when coordinator update fails.""" + await _setup(hass, mock_config_entry) + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state != STATE_UNAVAILABLE + + mock_aladdin_connect_api.update_door.side_effect = aiohttp.ClientError() + freezer.tick(15) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state == STATE_UNAVAILABLE