diff --git a/homeassistant/components/lyric/__init__.py b/homeassistant/components/lyric/__init__.py index 54c843ae11f495..6a9a375f8c5d24 100644 --- a/homeassistant/components/lyric/__init__.py +++ b/homeassistant/components/lyric/__init__.py @@ -23,7 +23,7 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) -PLATFORMS = [Platform.CLIMATE, Platform.SELECT, Platform.SENSOR] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SELECT, Platform.SENSOR] async def async_setup_entry(hass: HomeAssistant, entry: LyricConfigEntry) -> bool: diff --git a/homeassistant/components/lyric/binary_sensor.py b/homeassistant/components/lyric/binary_sensor.py new file mode 100644 index 00000000000000..2e33abf8269e59 --- /dev/null +++ b/homeassistant/components/lyric/binary_sensor.py @@ -0,0 +1,88 @@ +"""Support for Honeywell Lyric binary sensor platform.""" + +from collections.abc import Callable +from dataclasses import dataclass +from typing import override + +from aiolyric.objects.device import LyricDevice +from aiolyric.objects.location import LyricLocation + +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .coordinator import LyricConfigEntry, LyricDataUpdateCoordinator +from .entity import LyricDeviceEntity + + +@dataclass(frozen=True, kw_only=True) +class LyricBinarySensorEntityDescription(BinarySensorEntityDescription): + """Class describing Honeywell Lyric binary sensor entities.""" + + value_fn: Callable[[LyricDevice], bool] + suitable_fn: Callable[[LyricDevice], bool] + + +DEVICE_BINARY_SENSORS: list[LyricBinarySensorEntityDescription] = [ + LyricBinarySensorEntityDescription( + key="device_pairing_enabled", + translation_key="device_pairing_enabled", + entity_category=EntityCategory.DIAGNOSTIC, + value_fn=lambda device: device.settings.device_pairing_enabled, + suitable_fn=lambda device: True, + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, + entry: LyricConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the Honeywell Lyric binary sensor platform based on a config entry.""" + coordinator = entry.runtime_data + + async_add_entities( + LyricBinarySensor( + coordinator, + device_binary_sensor, + location, + device, + ) + for location in coordinator.data.locations + for device in location.devices + for device_binary_sensor in DEVICE_BINARY_SENSORS + if device_binary_sensor.suitable_fn(device) + ) + + +class LyricBinarySensor(LyricDeviceEntity, BinarySensorEntity): + """Define a Honeywell Lyric binary sensor.""" + + entity_description: LyricBinarySensorEntityDescription + + def __init__( + self, + coordinator: LyricDataUpdateCoordinator, + description: LyricBinarySensorEntityDescription, + location: LyricLocation, + device: LyricDevice, + ) -> None: + """Initialize.""" + super().__init__( + coordinator, + location, + device, + f"{device.mac_id}_{description.key}", + ) + self.entity_description = description + + @property + @override + def is_on(self) -> bool: + """Return true if the condition is met.""" + return self.entity_description.value_fn(self.device) diff --git a/homeassistant/components/lyric/strings.json b/homeassistant/components/lyric/strings.json index 45fd2f06a28bcf..eba24151f2e451 100644 --- a/homeassistant/components/lyric/strings.json +++ b/homeassistant/components/lyric/strings.json @@ -37,6 +37,11 @@ } }, "entity": { + "binary_sensor": { + "device_pairing_enabled": { + "name": "Device pairing enabled" + } + }, "select": { "room_priority": { "name": "Room priority", diff --git a/tests/components/lyric/snapshots/test_binary_sensor.ambr b/tests/components/lyric/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..25051f05620e3b --- /dev/null +++ b/tests/components/lyric/snapshots/test_binary_sensor.ambr @@ -0,0 +1,51 @@ +# serializer version: 1 +# name: test_binary_sensor[binary_sensor.ocala_thermostat_device_pairing_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.ocala_thermostat_device_pairing_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Device pairing enabled', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Device pairing enabled', + 'platform': 'lyric', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'device_pairing_enabled', + 'unique_id': '5CFCE1B67035_device_pairing_enabled', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_sensor[binary_sensor.ocala_thermostat_device_pairing_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'Ocala Thermostat Device pairing enabled', + }), + 'context': , + 'entity_id': 'binary_sensor.ocala_thermostat_device_pairing_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/lyric/test_binary_sensor.py b/tests/components/lyric/test_binary_sensor.py new file mode 100644 index 00000000000000..974403b7d25568 --- /dev/null +++ b/tests/components/lyric/test_binary_sensor.py @@ -0,0 +1,28 @@ +"""Tests for the Honeywell Lyric binary sensor platform.""" + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("setup_credentials", "mock_lyric_api") +async def test_binary_sensor( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test the Lyric binary sensor platform via a real config entry setup.""" + with patch("homeassistant.components.lyric.PLATFORMS", [Platform.BINARY_SENSOR]): + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)