Add binary sensor platform to Fumis integration (#169032)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Franck Nijhof
2026-04-27 11:53:25 +02:00
committed by GitHub
co-authored by Joost Lekkerkerker
parent e9fc6b3e74
commit 64c9a76fc8
6 changed files with 189 additions and 11 deletions
+6 -1
View File
@@ -7,7 +7,12 @@ from homeassistant.core import HomeAssistant
from .coordinator import FumisConfigEntry, FumisDataUpdateCoordinator
PLATFORMS = [Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR]
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.CLIMATE,
Platform.SENSOR,
]
async def async_setup_entry(hass: HomeAssistant, entry: FumisConfigEntry) -> bool:
@@ -0,0 +1,76 @@
"""Support for Fumis binary sensor entities."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from fumis import FumisInfo
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 FumisConfigEntry, FumisDataUpdateCoordinator
from .entity import FumisEntity
PARALLEL_UPDATES = 0
@dataclass(frozen=True, kw_only=True)
class FumisBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes a Fumis binary sensor entity."""
has_fn: Callable[[FumisInfo], bool] = lambda _: True
is_on_fn: Callable[[FumisInfo], bool | None]
BINARY_SENSORS: tuple[FumisBinarySensorEntityDescription, ...] = (
FumisBinarySensorEntityDescription(
key="door",
device_class=BinarySensorDeviceClass.DOOR,
entity_category=EntityCategory.DIAGNOSTIC,
has_fn=lambda data: data.controller.door_open is not None,
is_on_fn=lambda data: data.controller.door_open,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: FumisConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Fumis binary sensor entities based on a config entry."""
coordinator = entry.runtime_data
async_add_entities(
FumisBinarySensorEntity(coordinator=coordinator, description=description)
for description in BINARY_SENSORS
if description.has_fn(coordinator.data)
)
class FumisBinarySensorEntity(FumisEntity, BinarySensorEntity):
"""Defines a Fumis binary sensor entity."""
entity_description: FumisBinarySensorEntityDescription
def __init__(
self,
coordinator: FumisDataUpdateCoordinator,
description: FumisBinarySensorEntityDescription,
) -> None:
"""Initialize the Fumis binary sensor entity."""
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{coordinator.config_entry.unique_id}_{description.key}"
@property
def is_on(self) -> bool | None:
"""Return the state of the binary sensor."""
return self.entity_description.is_on_fn(self.coordinator.data)
+3
View File
@@ -0,0 +1,3 @@
"""Constants for the Fumis integration tests."""
UNIQUE_ID = "aa:bb:cc:dd:ee:ff"
@@ -0,0 +1,52 @@
# serializer version: 1
# name: test_binary_sensors[binary_sensor][binary_sensor.clou_duo_door-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.clou_duo_door',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Door',
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.DOOR: 'door'>,
'original_icon': None,
'original_name': 'Door',
'platform': 'fumis',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'aa:bb:cc:dd:ee:ff_door',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor][binary_sensor.clou_duo_door-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'door',
'friendly_name': 'Clou Duo Door',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.clou_duo_door',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
@@ -0,0 +1,42 @@
"""Tests for the Fumis binary sensor entities."""
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 .const import UNIQUE_ID
from tests.common import MockConfigEntry, snapshot_platform
pytestmark = pytest.mark.parametrize(
"init_integration", [Platform.BINARY_SENSOR], indirect=True
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_binary_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test the Fumis binary sensor entities."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.parametrize("device_fixture", ["info_minimal"])
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_binary_sensors_conditional_creation(
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test door binary sensor is not created when data is missing."""
entity_entries = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
unique_ids = {entry.unique_id for entry in entity_entries}
assert f"{UNIQUE_ID}_door" not in unique_ids
+10 -10
View File
@@ -7,9 +7,9 @@ from homeassistant.const import STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
from .const import UNIQUE_ID
UNIQUE_ID_PREFIX = "aa:bb:cc:dd:ee:ff"
from tests.common import MockConfigEntry, snapshot_platform
pytestmark = pytest.mark.parametrize(
"init_integration", [Platform.SENSOR], indirect=True
@@ -31,11 +31,11 @@ async def test_sensors(
@pytest.mark.parametrize(
"unique_id",
[
f"{UNIQUE_ID_PREFIX}_fan_1_speed",
f"{UNIQUE_ID_PREFIX}_fan_2_speed",
f"{UNIQUE_ID_PREFIX}_module_temperature",
f"{UNIQUE_ID_PREFIX}_pressure",
f"{UNIQUE_ID_PREFIX}_wifi_rssi",
f"{UNIQUE_ID}_fan_1_speed",
f"{UNIQUE_ID}_fan_2_speed",
f"{UNIQUE_ID}_module_temperature",
f"{UNIQUE_ID}_pressure",
f"{UNIQUE_ID}_wifi_rssi",
],
)
@pytest.mark.usefixtures("init_integration")
@@ -59,7 +59,7 @@ async def test_sensors_unknown_status(
"""Test sensor returns unknown when stove status is unmapped."""
for key in ("stove_status", "detailed_stove_status"):
entry = entity_registry.async_get_entity_id(
"sensor", "fumis", f"{UNIQUE_ID_PREFIX}_{key}"
"sensor", "fumis", f"{UNIQUE_ID}_{key}"
)
assert entry is not None
assert (state := hass.states.get(entry))
@@ -89,7 +89,7 @@ async def test_sensors_conditional_creation(
"temperature",
"time_to_service",
):
assert f"{UNIQUE_ID_PREFIX}_{key}" not in unique_ids, key
assert f"{UNIQUE_ID}_{key}" not in unique_ids, key
# These should still exist
for key in (
@@ -99,4 +99,4 @@ async def test_sensors_conditional_creation(
"wifi_rssi",
"wifi_signal_strength",
):
assert f"{UNIQUE_ID_PREFIX}_{key}" in unique_ids, key
assert f"{UNIQUE_ID}_{key}" in unique_ids, key