1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Add platform tests for aladdin_connect cover and sensor (#164011)

This commit is contained in:
Jamie Magee
2026-02-25 07:20:19 -08:00
committed by GitHub
parent cb990823cd
commit e591291cbe
5 changed files with 302 additions and 3 deletions
@@ -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
@@ -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': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.test_door',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': None,
'options': dict({
}),
'original_device_class': <CoverDeviceClass.GARAGE: 'garage'>,
'original_icon': None,
'original_name': None,
'platform': 'aladdin_connect',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <CoverEntityFeature: 3>,
'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': <CoverEntityFeature: 3>,
}),
'context': <ANY>,
'entity_id': 'cover.test_door',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'closed',
})
# ---
@@ -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': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.test_door_battery',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Battery',
'options': dict({
}),
'original_device_class': <SensorDeviceClass.BATTERY: 'battery'>,
'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': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.test_door_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '100',
})
# ---
@@ -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
@@ -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