Add room climate binary sensors to Bosch SHC (#188791)

This commit is contained in:
Thomas
2026-11-10 19:20:32 +01:00
committed by GitHub
parent 3d535df7ec
commit 11573e56d7
4 changed files with 162 additions and 2 deletions
@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Any, override
from boschshcpy import (
BatteryLevelService,
SHCBatteryDevice,
SHCClimateControl,
SHCShutterContact,
SHCSmokeDetectionSystem,
SHCTwinguard,
@@ -41,6 +42,41 @@ class SHCShutterContactSensorEntityDescription(BinarySensorEntityDescription):
is_on_fn: Callable[[SHCShutterContact], bool]
@dataclass(frozen=True, kw_only=True)
class SHCBinarySensorEntityDescription[_DeviceT: SHCDevice](
BinarySensorEntityDescription
):
"""Describes a SHC binary sensor."""
is_on_fn: Callable[[_DeviceT], bool]
CLIMATE_CONTROL_DESCRIPTIONS: tuple[
SHCBinarySensorEntityDescription[SHCClimateControl], ...
] = (
SHCBinarySensorEntityDescription(
key="call_for_heat",
translation_key="call_for_heat",
device_class=BinarySensorDeviceClass.RUNNING,
is_on_fn=lambda device: device.has_demand and not device.cooling_mode,
),
SHCBinarySensorEntityDescription(
key="schedule_override_active",
translation_key="schedule_override_active",
is_on_fn=lambda device: device.setpoint_temperature_offset_active,
),
SHCBinarySensorEntityDescription(
key="summer_mode",
translation_key="summer_mode",
is_on_fn=lambda device: device.summer_mode,
),
SHCBinarySensorEntityDescription(
key="ventilation_mode",
translation_key="ventilation_mode",
is_on_fn=lambda device: device.ventilation_mode,
),
)
SHUTTER_CONTACT_DESCRIPTION = SHCShutterContactSensorEntityDescription(
key="shutter_contact",
is_on_fn=lambda device: device.state is ShutterContactService.State.OPEN,
@@ -108,10 +144,53 @@ async def async_setup_entry(
)
for twinguard in session.device_helper.twinguards
)
entities.extend(
SHCBinarySensor(
hass=hass,
device=device,
parent_id=shc_info.unique_id,
entry_id=config_entry.entry_id,
entity_description=description,
room_name=session.room(device.room_id).name,
)
for device in session.device_helper.climate_controls
for description in CLIMATE_CONTROL_DESCRIPTIONS
)
async_add_entities(entities)
class SHCBinarySensor[_DeviceT: SHCDevice](SHCEntity, BinarySensorEntity):
"""Generic SHC binary sensor, driven by a SHCBinarySensorEntityDescription."""
entity_description: SHCBinarySensorEntityDescription[_DeviceT]
_device: _DeviceT
def __init__(
self,
hass: HomeAssistant,
device: _DeviceT,
parent_id: str,
entry_id: str,
entity_description: SHCBinarySensorEntityDescription[_DeviceT],
room_name: str | None = None,
) -> None:
"""Initialize the binary sensor."""
self.entity_description = entity_description
super().__init__(hass, device, parent_id, entry_id)
if room_name is not None:
if TYPE_CHECKING:
assert self._attr_device_info is not None
self._attr_device_info.name = room_name
self._attr_unique_id = f"{device.serial}_{entity_description.key}"
@property
@override
def is_on(self) -> bool:
"""Return the state of the sensor."""
return self.entity_description.is_on_fn(self._device)
class ShutterContactSensor(SHCEntity, BinarySensorEntity):
"""Representation of an SHC shutter contact sensor."""
@@ -48,6 +48,20 @@
}
},
"entity": {
"binary_sensor": {
"call_for_heat": {
"name": "Call for heat"
},
"schedule_override_active": {
"name": "Schedule override active"
},
"summer_mode": {
"name": "Heating break"
},
"ventilation_mode": {
"name": "Sustainable ventilation"
}
},
"button": {
"reset_tamper": {
"name": "Reset tamper"
+4
View File
@@ -424,6 +424,8 @@ def climate_control_device(
supports_eco: bool = True,
low: bool = False,
has_demand: bool = False,
ventilation_mode: bool = False,
setpoint_temperature_offset_active: bool = False,
) -> SHCClimateControl:
"""Build a minimal device double for the climate_controls bucket."""
device = create_autospec(SHCClimateControl, instance=True, spec_set=True)
@@ -448,6 +450,8 @@ def climate_control_device(
device.supports_eco = supports_eco
device.low = low
device.has_demand = has_demand
device.ventilation_mode = ventilation_mode
device.setpoint_temperature_offset_active = setpoint_temperature_offset_active
return device
@@ -1,6 +1,7 @@
"""Tests for the Bosch SHC binary_sensor platform."""
from unittest.mock import MagicMock
from collections.abc import Generator
from unittest.mock import MagicMock, patch
from boschshcpy import ShutterContactService, SurveillanceAlarmService
from freezegun.api import FrozenDateTimeFactory
@@ -8,10 +9,12 @@ import pytest
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
from homeassistant.components.bosch_shc.binary_sensor import RETRY_DELAY
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from .conftest import (
climate_control_device,
setup_integration,
shutter_contact_device,
smoke_detection_system_device,
@@ -19,6 +22,15 @@ from .conftest import (
)
@pytest.fixture(autouse=True)
def platforms() -> Generator[None]:
"""Restrict bosch_shc setup to the binary sensor platform."""
with patch(
"homeassistant.components.bosch_shc.PLATFORMS", [Platform.BINARY_SENSOR]
):
yield
@pytest.mark.parametrize(
"device_buckets",
[
@@ -225,3 +237,54 @@ async def test_twinguard_smoke_looks_up_once(
assert system.triggered_device_ids.call_count == 1
assert hass.states.get("binary_sensor.twinguard_smoke").state == STATE_OFF
assert hass.states.get("binary_sensor.second_smoke").state == STATE_ON
@pytest.mark.parametrize(
("device_kwargs", "entity_id", "expected"),
[
({"has_demand": True}, "binary_sensor.living_room_call_for_heat", STATE_ON),
({}, "binary_sensor.living_room_call_for_heat", STATE_OFF),
(
{"has_demand": True, "supports_cooling": True, "cooling_mode": True},
"binary_sensor.living_room_call_for_heat",
STATE_OFF,
),
(
{"setpoint_temperature_offset_active": True},
"binary_sensor.living_room_schedule_override_active",
STATE_ON,
),
({"summer_mode": True}, "binary_sensor.living_room_heating_break", STATE_ON),
(
{"ventilation_mode": True},
"binary_sensor.living_room_sustainable_ventilation",
STATE_ON,
),
],
)
@pytest.mark.usefixtures("mock_session")
async def test_climate_control_binary_sensors(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
device_kwargs: dict[str, bool],
entity_id: str,
expected: str,
) -> None:
"""A room climate control exposes its demand and mode flags."""
mock_session.device_helper.climate_controls = [
climate_control_device(**device_kwargs)
]
mock_session.room.return_value.name = "Living Room"
await setup_integration(hass, mock_config_entry)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == expected
entry = entity_registry.async_get(entity_id)
assert entry is not None
device = device_registry.async_get(entry.device_id)
assert device is not None
assert device.name == "Living Room"