From 70faad15d57d0a12a4e52c46eed73f9ff8af5f28 Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Wed, 11 Mar 2026 16:21:16 +0100 Subject: [PATCH] Add binary_sensor to eheimdigital (#165035) Co-authored-by: Joost Lekkerkerker Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../components/eheimdigital/__init__.py | 1 + .../components/eheimdigital/binary_sensor.py | 101 ++++++++++++++++ .../components/eheimdigital/icons.json | 14 +++ .../components/eheimdigital/strings.json | 11 ++ .../snapshots/test_binary_sensor.ambr | 101 ++++++++++++++++ .../eheimdigital/test_binary_sensor.py | 109 ++++++++++++++++++ 6 files changed, 337 insertions(+) create mode 100644 homeassistant/components/eheimdigital/binary_sensor.py create mode 100644 tests/components/eheimdigital/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/eheimdigital/test_binary_sensor.py diff --git a/homeassistant/components/eheimdigital/__init__.py b/homeassistant/components/eheimdigital/__init__.py index bc8bbded186..dbb672dcb4b 100644 --- a/homeassistant/components/eheimdigital/__init__.py +++ b/homeassistant/components/eheimdigital/__init__.py @@ -10,6 +10,7 @@ from .const import DOMAIN from .coordinator import EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator PLATFORMS = [ + Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.LIGHT, Platform.NUMBER, diff --git a/homeassistant/components/eheimdigital/binary_sensor.py b/homeassistant/components/eheimdigital/binary_sensor.py new file mode 100644 index 00000000000..82ce8c3f9fc --- /dev/null +++ b/homeassistant/components/eheimdigital/binary_sensor.py @@ -0,0 +1,101 @@ +"""EHEIM Digital binary sensors.""" + +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any + +from eheimdigital.device import EheimDigitalDevice +from eheimdigital.reeflex import EheimDigitalReeflexUV + +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 EheimDigitalConfigEntry, EheimDigitalUpdateCoordinator +from .entity import EheimDigitalEntity + +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class EheimDigitalBinarySensorDescription[_DeviceT: EheimDigitalDevice]( + BinarySensorEntityDescription +): + """Class describing EHEIM Digital binary sensor entities.""" + + value_fn: Callable[[_DeviceT], bool | None] + + +REEFLEX_DESCRIPTIONS: tuple[ + EheimDigitalBinarySensorDescription[EheimDigitalReeflexUV], ... +] = ( + EheimDigitalBinarySensorDescription[EheimDigitalReeflexUV]( + key="is_lighting", + translation_key="is_lighting", + value_fn=lambda device: device.is_lighting, + device_class=BinarySensorDeviceClass.LIGHT, + ), + EheimDigitalBinarySensorDescription[EheimDigitalReeflexUV]( + key="is_uvc_connected", + translation_key="is_uvc_connected", + entity_category=EntityCategory.DIAGNOSTIC, + value_fn=lambda device: device.is_uvc_connected, + device_class=BinarySensorDeviceClass.CONNECTIVITY, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: EheimDigitalConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the callbacks for the coordinator so binary sensors can be added as devices are found.""" + coordinator = entry.runtime_data + + def async_setup_device_entities( + device_address: dict[str, EheimDigitalDevice], + ) -> None: + """Set up the binary sensor entities for one or multiple devices.""" + entities: list[EheimDigitalBinarySensor[Any]] = [] + for device in device_address.values(): + if isinstance(device, EheimDigitalReeflexUV): + entities += [ + EheimDigitalBinarySensor[EheimDigitalReeflexUV]( + coordinator, device, description + ) + for description in REEFLEX_DESCRIPTIONS + ] + + async_add_entities(entities) + + coordinator.add_platform_callback(async_setup_device_entities) + async_setup_device_entities(coordinator.hub.devices) + + +class EheimDigitalBinarySensor[_DeviceT: EheimDigitalDevice]( + EheimDigitalEntity[_DeviceT], BinarySensorEntity +): + """Represent an EHEIM Digital binary sensor entity.""" + + entity_description: EheimDigitalBinarySensorDescription[_DeviceT] + + def __init__( + self, + coordinator: EheimDigitalUpdateCoordinator, + device: _DeviceT, + description: EheimDigitalBinarySensorDescription[_DeviceT], + ) -> None: + """Initialize an EHEIM Digital binary sensor entity.""" + super().__init__(coordinator, device) + self.entity_description = description + self._attr_unique_id = f"{self._device_address}_{description.key}" + + def _async_update_attrs(self) -> None: + self._attr_is_on = self.entity_description.value_fn(self._device) diff --git a/homeassistant/components/eheimdigital/icons.json b/homeassistant/components/eheimdigital/icons.json index 23eca2051dd..13ae0b75814 100644 --- a/homeassistant/components/eheimdigital/icons.json +++ b/homeassistant/components/eheimdigital/icons.json @@ -1,5 +1,19 @@ { "entity": { + "binary_sensor": { + "is_lighting": { + "default": "mdi:lightbulb-outline", + "state": { + "on": "mdi:lightbulb-on" + } + }, + "is_uvc_connected": { + "default": "mdi:lightbulb-off", + "state": { + "on": "mdi:lightbulb-outline" + } + } + }, "number": { "day_speed": { "default": "mdi:weather-sunny" diff --git a/homeassistant/components/eheimdigital/strings.json b/homeassistant/components/eheimdigital/strings.json index 68e02b559ae..f02f33763d8 100644 --- a/homeassistant/components/eheimdigital/strings.json +++ b/homeassistant/components/eheimdigital/strings.json @@ -33,6 +33,17 @@ } }, "entity": { + "binary_sensor": { + "is_lighting": { + "state": { + "off": "[%key:common::state::off%]", + "on": "[%key:common::state::on%]" + } + }, + "is_uvc_connected": { + "name": "UVC lamp connected" + } + }, "climate": { "heater": { "state_attributes": { diff --git a/tests/components/eheimdigital/snapshots/test_binary_sensor.ambr b/tests/components/eheimdigital/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..1b71ace7cb2 --- /dev/null +++ b/tests/components/eheimdigital/snapshots/test_binary_sensor.ambr @@ -0,0 +1,101 @@ +# serializer version: 1 +# name: test_setup[binary_sensor.mock_reeflex_light-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.mock_reeflex_light', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Light', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Light', + 'platform': 'eheimdigital', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'is_lighting', + 'unique_id': '00:00:00:00:00:05_is_lighting', + 'unit_of_measurement': None, + }) +# --- +# name: test_setup[binary_sensor.mock_reeflex_light-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'light', + 'friendly_name': 'Mock reeflex Light', + }), + 'context': , + 'entity_id': 'binary_sensor.mock_reeflex_light', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_setup[binary_sensor.mock_reeflex_uvc_lamp_connected-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + '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.mock_reeflex_uvc_lamp_connected', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'UVC lamp connected', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'UVC lamp connected', + 'platform': 'eheimdigital', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'is_uvc_connected', + 'unique_id': '00:00:00:00:00:05_is_uvc_connected', + 'unit_of_measurement': None, + }) +# --- +# name: test_setup[binary_sensor.mock_reeflex_uvc_lamp_connected-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'Mock reeflex UVC lamp connected', + }), + 'context': , + 'entity_id': 'binary_sensor.mock_reeflex_uvc_lamp_connected', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/eheimdigital/test_binary_sensor.py b/tests/components/eheimdigital/test_binary_sensor.py new file mode 100644 index 00000000000..8dfbbbce5f8 --- /dev/null +++ b/tests/components/eheimdigital/test_binary_sensor.py @@ -0,0 +1,109 @@ +"""Tests for the binary sensor module.""" + +from unittest.mock import AsyncMock, MagicMock, 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 .conftest import init_integration + +from tests.common import MockConfigEntry, get_sensor_display_state, snapshot_platform + + +async def test_setup( + hass: HomeAssistant, + eheimdigital_hub_mock: MagicMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test binary sensor platform setup.""" + mock_config_entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.eheimdigital.PLATFORMS", [Platform.BINARY_SENSOR] + ), + patch( + "homeassistant.components.eheimdigital.coordinator.asyncio.Event", + new=AsyncMock, + ), + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + for device in eheimdigital_hub_mock.return_value.devices: + await eheimdigital_hub_mock.call_args.kwargs["device_found_callback"]( + device, eheimdigital_hub_mock.return_value.devices[device].device_type + ) + await eheimdigital_hub_mock.call_args.kwargs["receive_callback"]() + + await hass.async_block_till_done() + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +@pytest.mark.parametrize( + ("device_name", "entity_list"), + [ + ( + "reeflex_mock", + [ + ( + "binary_sensor.mock_reeflex_light", + "reeflex_data", + "isLighting", + True, + "on", + ), + ( + "binary_sensor.mock_reeflex_light", + "reeflex_data", + "isLighting", + False, + "off", + ), + ( + "binary_sensor.mock_reeflex_uvc_lamp_connected", + "reeflex_data", + "isUVCConnected", + True, + "on", + ), + ( + "binary_sensor.mock_reeflex_uvc_lamp_connected", + "reeflex_data", + "isUVCConnected", + False, + "off", + ), + ], + ), + ], +) +async def test_state_update( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + eheimdigital_hub_mock: MagicMock, + mock_config_entry: MockConfigEntry, + device_name: str, + entity_list: list[tuple[str, str, str, bool | int, str]], + request: pytest.FixtureRequest, +) -> None: + """Test the binary sensor state update.""" + device: MagicMock = request.getfixturevalue(device_name) + await init_integration(hass, mock_config_entry) + + await eheimdigital_hub_mock.call_args.kwargs["device_found_callback"]( + device.mac_address, device.device_type + ) + + await hass.async_block_till_done() + + for item in entity_list: + getattr(device, item[1])[item[2]] = item[3] + await eheimdigital_hub_mock.call_args.kwargs["receive_callback"]() + assert get_sensor_display_state(hass, entity_registry, item[0]) == str(item[4])