diff --git a/homeassistant/components/izone/__init__.py b/homeassistant/components/izone/__init__.py index 6984c6eb0376..f33575464ef1 100644 --- a/homeassistant/components/izone/__init__.py +++ b/homeassistant/components/izone/__init__.py @@ -21,7 +21,12 @@ from .discovery import ( ) from .services import async_setup_services -PLATFORMS = [Platform.CLIMATE, Platform.NUMBER, Platform.SENSOR] +PLATFORMS = [ + Platform.BINARY_SENSOR, + Platform.CLIMATE, + Platform.NUMBER, + Platform.SENSOR, +] CONFIG_SCHEMA = probatio.Schema( { diff --git a/homeassistant/components/izone/binary_sensor.py b/homeassistant/components/izone/binary_sensor.py new file mode 100644 index 000000000000..8e24b44017ff --- /dev/null +++ b/homeassistant/components/izone/binary_sensor.py @@ -0,0 +1,96 @@ +"""Support for iZone binary sensors.""" + +from collections.abc import Callable +from dataclasses import dataclass +from operator import attrgetter +from typing import override + +from pizone import Zone + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .coordinator import IZoneConfigEntry, IZoneCoordinator +from .entity import IZoneCoordinatorEntity, zone_device_info + +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class IZoneBinarySensorEntityDescription(BinarySensorEntityDescription): + """Describes an iZone zone binary sensor.""" + + is_on_fn: Callable[[Zone], bool | None] + exists_fn: Callable[[Zone], bool] + + +ZONE_BINARY_SENSOR_DESCRIPTIONS: tuple[IZoneBinarySensorEntityDescription, ...] = ( + IZoneBinarySensorEntityDescription( + key="damper_fault", + translation_key="damper_fault", + device_class=BinarySensorDeviceClass.PROBLEM, + entity_category=EntityCategory.DIAGNOSTIC, + is_on_fn=attrgetter("damper_fault"), + exists_fn=lambda zone: zone.damper_fault is not None, + ), + IZoneBinarySensorEntityDescription( + key="sensor_fault", + translation_key="sensor_fault", + device_class=BinarySensorDeviceClass.PROBLEM, + entity_category=EntityCategory.DIAGNOSTIC, + is_on_fn=attrgetter("sensor_fault"), + exists_fn=lambda zone: ( + zone.sensor_fault is not None + and zone.sensor_type is not Zone.SensorType.NONE + ), + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: IZoneConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up iZone binary sensor entities from the entry coordinator.""" + coordinator = entry.runtime_data + async_add_entities( + IZoneZoneBinarySensor(coordinator, description, zone) + for zone in coordinator.controller.zones + for description in ZONE_BINARY_SENSOR_DESCRIPTIONS + if description.exists_fn(zone) + ) + + +class IZoneZoneBinarySensor(IZoneCoordinatorEntity, BinarySensorEntity): + """Binary sensor backed by a pizone Zone.""" + + _attr_has_entity_name = True + entity_description: IZoneBinarySensorEntityDescription + + def __init__( + self, + coordinator: IZoneCoordinator, + description: IZoneBinarySensorEntityDescription, + zone: Zone, + ) -> None: + """Initialize the binary sensor for *zone*.""" + super().__init__(coordinator) + self.entity_description = description + self._zone = zone + self._attr_unique_id = ( + f"{coordinator.controller.device_uid}_z{zone.index + 1}_{description.key}" + ) + self._attr_device_info = zone_device_info(coordinator, zone) + + @property + @override + def is_on(self) -> bool | None: + """Return True if the zone reports the fault.""" + return self.entity_description.is_on_fn(self._zone) diff --git a/homeassistant/components/izone/strings.json b/homeassistant/components/izone/strings.json index 771fd8b74923..53b52d815ee5 100644 --- a/homeassistant/components/izone/strings.json +++ b/homeassistant/components/izone/strings.json @@ -51,6 +51,14 @@ } }, "entity": { + "binary_sensor": { + "damper_fault": { + "name": "Damper fault" + }, + "sensor_fault": { + "name": "Sensor fault" + } + }, "number": { "damper_max": { "name": "Damper maximum" diff --git a/tests/components/izone/conftest.py b/tests/components/izone/conftest.py index af898290d09c..e70ccb6a2333 100644 --- a/tests/components/izone/conftest.py +++ b/tests/components/izone/conftest.py @@ -157,6 +157,8 @@ def create_mock_zone( sensor_type: Zone.SensorType | None = Zone.SensorType.WIRELESS, rf_signal: Zone.RfSignal | None = Zone.RfSignal.FULL, battery_level: Zone.BatteryLevel | None = Zone.BatteryLevel.FULL, + damper_fault: bool | None = False, + sensor_fault: bool | None = False, ) -> Mock: """Create a mock Zone with configurable parameters.""" zone = Mock(spec=Zone) @@ -172,6 +174,8 @@ def create_mock_zone( zone.sensor_type = sensor_type zone.rf_signal = rf_signal zone.battery_level = battery_level + zone.damper_fault = damper_fault + zone.sensor_fault = sensor_fault zone._zone_data = { "AirStreamDeviceUId": "000000001", "Id": 0, diff --git a/tests/components/izone/snapshots/test_binary_sensor.ambr b/tests/components/izone/snapshots/test_binary_sensor.ambr new file mode 100644 index 000000000000..ecc163dce2c5 --- /dev/null +++ b/tests/components/izone/snapshots/test_binary_sensor.ambr @@ -0,0 +1,103 @@ +# serializer version: 1 +# name: test_binary_sensor_entities[binary_sensor.living_room_damper_fault-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.living_room_damper_fault', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Damper fault', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Damper fault', + 'platform': 'izone', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'damper_fault', + 'unique_id': '000000001_z1_damper_fault', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_sensor_entities[binary_sensor.living_room_damper_fault-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'Living Room Damper fault', + }), + 'context': , + 'entity_id': 'binary_sensor.living_room_damper_fault', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_binary_sensor_entities[binary_sensor.living_room_sensor_fault-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.living_room_sensor_fault', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Sensor fault', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Sensor fault', + 'platform': 'izone', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sensor_fault', + 'unique_id': '000000001_z1_sensor_fault', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_sensor_entities[binary_sensor.living_room_sensor_fault-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'Living Room Sensor fault', + }), + 'context': , + 'entity_id': 'binary_sensor.living_room_sensor_fault', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/izone/test_binary_sensor.py b/tests/components/izone/test_binary_sensor.py new file mode 100644 index 000000000000..30a53327bb33 --- /dev/null +++ b/tests/components/izone/test_binary_sensor.py @@ -0,0 +1,101 @@ +"""Tests for iZone binary sensor platform.""" + +from unittest.mock import Mock + +from pizone import Zone +import pytest +from pytest_homeassistant.common import MockConfigEntry, snapshot_platform +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import STATE_OFF, STATE_ON, Platform +from homeassistant.core import HomeAssistant +import homeassistant.helpers.entity_registry as er + +from . import setup_integration + +LIVING_ROOM_DAMPER_FAULT_ENTITY = "binary_sensor.living_room_damper_fault" +LIVING_ROOM_SENSOR_FAULT_ENTITY = "binary_sensor.living_room_sensor_fault" + + +@pytest.fixture +def platforms() -> list[Platform]: + """Only load the binary sensor platform for these tests.""" + return [Platform.BINARY_SENSOR] + + +@pytest.mark.usefixtures("init_integration") +async def test_binary_sensor_entities( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Zone damper and sensor fault binary sensors are created.""" + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +@pytest.mark.parametrize( + ("damper_fault", "sensor_fault", "damper_state", "sensor_state"), + [ + pytest.param(True, False, STATE_ON, STATE_OFF, id="damper_fault"), + pytest.param(False, True, STATE_OFF, STATE_ON, id="sensor_fault"), + ], +) +@pytest.mark.usefixtures("mock_create_discovery") +async def test_fault_states( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_zones: list[Mock], + damper_fault: bool, + sensor_fault: bool, + damper_state: str, + sensor_state: str, +) -> None: + """Zone fault flags map to binary sensor states.""" + mock_zones[0].damper_fault = damper_fault + mock_zones[0].sensor_fault = sensor_fault + + await setup_integration(hass, mock_config_entry) + + assert hass.states.get(LIVING_ROOM_DAMPER_FAULT_ENTITY).state == damper_state + assert hass.states.get(LIVING_ROOM_SENSOR_FAULT_ENTITY).state == sensor_state + + +@pytest.mark.usefixtures("mock_create_discovery") +async def test_damper_fault_not_created_without_data( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_zones: list[Mock], + entity_registry: er.EntityRegistry, +) -> None: + """Zones that don't report a damper fault get no damper fault sensor.""" + mock_zones[0].damper_fault = None + + await setup_integration(hass, mock_config_entry) + + assert entity_registry.async_get(LIVING_ROOM_DAMPER_FAULT_ENTITY) is None + + +@pytest.mark.parametrize( + ("sensor_fault", "sensor_type"), + [ + pytest.param(None, None, id="v1"), + pytest.param(False, Zone.SensorType.NONE, id="no_sensor"), + ], +) +@pytest.mark.usefixtures("mock_create_discovery") +async def test_sensor_fault_not_created( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_zones: list[Mock], + entity_registry: er.EntityRegistry, + sensor_fault: bool | None, + sensor_type: Zone.SensorType | None, +) -> None: + """Zones without a sensor fault reading or without a sensor get no sensor fault.""" + mock_zones[0].sensor_fault = sensor_fault + mock_zones[0].sensor_type = sensor_type + + await setup_integration(hass, mock_config_entry) + + assert entity_registry.async_get(LIVING_ROOM_SENSOR_FAULT_ENTITY) is None