From a341bfd8cafecedbf382432a629e7da246f91db9 Mon Sep 17 00:00:00 2001 From: TimL Date: Fri, 6 Sep 2024 16:11:50 +1000 Subject: [PATCH] Add binary_sensor platform for Smlight integration (#125284) * Support binary_sensors for SMLight integration * Add strings for binary sensors * Add tests for binary_sensor platform * Update binary sensor docstring Co-authored-by: Shay Levy * Regenerate snapshot --------- Co-authored-by: Shay Levy Co-authored-by: Tim Lunn --- homeassistant/components/smlight/__init__.py | 1 + .../components/smlight/binary_sensor.py | 80 ++++++++++++++++ homeassistant/components/smlight/strings.json | 8 ++ .../smlight/snapshots/test_binary_sensor.ambr | 95 +++++++++++++++++++ .../components/smlight/test_binary_sensor.py | 52 ++++++++++ 5 files changed, 236 insertions(+) create mode 100644 homeassistant/components/smlight/binary_sensor.py create mode 100644 tests/components/smlight/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/smlight/test_binary_sensor.py diff --git a/homeassistant/components/smlight/__init__.py b/homeassistant/components/smlight/__init__.py index 47dc943423ea1..4f0f2c0fb022e 100644 --- a/homeassistant/components/smlight/__init__.py +++ b/homeassistant/components/smlight/__init__.py @@ -9,6 +9,7 @@ from .coordinator import SmDataUpdateCoordinator PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, ] diff --git a/homeassistant/components/smlight/binary_sensor.py b/homeassistant/components/smlight/binary_sensor.py new file mode 100644 index 0000000000000..b010c3f7cbd99 --- /dev/null +++ b/homeassistant/components/smlight/binary_sensor.py @@ -0,0 +1,80 @@ +"""Support for SLZB-06 binary sensors.""" + +from __future__ import annotations + +from _collections_abc import Callable +from dataclasses import dataclass + +from pysmlight import Sensors + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .coordinator import SmDataUpdateCoordinator +from .entity import SmEntity + + +@dataclass(frozen=True, kw_only=True) +class SmBinarySensorEntityDescription(BinarySensorEntityDescription): + """Class describing SMLIGHT binary sensor entities.""" + + value_fn: Callable[[Sensors], bool] + + +SENSORS = [ + SmBinarySensorEntityDescription( + key="ethernet", + translation_key="ethernet", + value_fn=lambda x: x.ethernet, + ), + SmBinarySensorEntityDescription( + key="wifi", + translation_key="wifi", + entity_registry_enabled_default=False, + value_fn=lambda x: x.wifi_connected, + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up SMLIGHT sensor based on a config entry.""" + coordinator = entry.runtime_data + + async_add_entities( + SmBinarySensorEntity(coordinator, description) for description in SENSORS + ) + + +class SmBinarySensorEntity(SmEntity, BinarySensorEntity): + """Representation of a slzb binary sensor.""" + + entity_description: SmBinarySensorEntityDescription + _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY + _attr_entity_category = EntityCategory.DIAGNOSTIC + + def __init__( + self, + coordinator: SmDataUpdateCoordinator, + description: SmBinarySensorEntityDescription, + ) -> None: + """Initialize slzb binary sensor.""" + super().__init__(coordinator) + + self.entity_description = description + self._attr_unique_id = f"{coordinator.unique_id}_{description.key}" + + @property + def is_on(self) -> bool: + """Return the state of the sensor.""" + return self.entity_description.value_fn(self.coordinator.data.sensors) diff --git a/homeassistant/components/smlight/strings.json b/homeassistant/components/smlight/strings.json index f22966df9048f..7e17a53a38a6c 100644 --- a/homeassistant/components/smlight/strings.json +++ b/homeassistant/components/smlight/strings.json @@ -42,6 +42,14 @@ } }, "entity": { + "binary_sensor": { + "ethernet": { + "name": "Ethernet" + }, + "wifi": { + "name": "Wi-Fi" + } + }, "sensor": { "zigbee_temperature": { "name": "Zigbee chip temp" diff --git a/tests/components/smlight/snapshots/test_binary_sensor.ambr b/tests/components/smlight/snapshots/test_binary_sensor.ambr new file mode 100644 index 0000000000000..5ea936f96472f --- /dev/null +++ b/tests/components/smlight/snapshots/test_binary_sensor.ambr @@ -0,0 +1,95 @@ +# serializer version: 1 +# name: test_all_binary_sensors[binary_sensor.mock_title_ethernet-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mock_title_ethernet', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Ethernet', + 'platform': 'smlight', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'ethernet', + 'unique_id': 'aa:bb:cc:dd:ee:ff_ethernet', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensors[binary_sensor.mock_title_ethernet-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'Mock Title Ethernet', + }), + 'context': , + 'entity_id': 'binary_sensor.mock_title_ethernet', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_binary_sensors[binary_sensor.mock_title_wi_fi-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mock_title_wi_fi', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Wi-Fi', + 'platform': 'smlight', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'wifi', + 'unique_id': 'aa:bb:cc:dd:ee:ff_wifi', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensors[binary_sensor.mock_title_wi_fi-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'Mock Title Wi-Fi', + }), + 'context': , + 'entity_id': 'binary_sensor.mock_title_wi_fi', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/smlight/test_binary_sensor.py b/tests/components/smlight/test_binary_sensor.py new file mode 100644 index 0000000000000..ddf9b01bf16a2 --- /dev/null +++ b/tests/components/smlight/test_binary_sensor.py @@ -0,0 +1,52 @@ +"""Tests for the SMLIGHT binary sensor platform.""" + +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 .conftest import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + +pytestmark = [ + pytest.mark.usefixtures( + "mock_smlight_client", + ) +] + + +@pytest.fixture +def platforms() -> list[Platform]: + """Platforms, which should be loaded during the test.""" + return [Platform.BINARY_SENSOR] + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_binary_sensors( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test the SMLIGHT binary sensors.""" + entry = await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) + + +async def test_disabled_by_default_sensors( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, +) -> None: + """Test wifi sensor is disabled by default .""" + await setup_integration(hass, mock_config_entry) + + assert not hass.states.get("binary_sensor.mock_title_wi_fi") + + assert (entry := entity_registry.async_get("binary_sensor.mock_title_wi_fi")) + assert entry.disabled + assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION