From 1745cd8ca46bb1c8f1a2b59776dbf11cf9173b9a Mon Sep 17 00:00:00 2001 From: Thomas <10558666+mosandlt@users.noreply.github.com> Date: Tue, 20 Oct 2026 15:30:38 +0200 Subject: [PATCH] bosch_shc: add heating circuit climate entity (#186001) Co-authored-by: Claude Sonnet 5.5 --- homeassistant/components/bosch_shc/climate.py | 85 ++++++++- .../components/bosch_shc/strings.json | 2 +- tests/components/bosch_shc/conftest.py | 27 +++ tests/components/bosch_shc/test_climate.py | 163 +++++++++++++++++- 4 files changed, 273 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/bosch_shc/climate.py b/homeassistant/components/bosch_shc/climate.py index e1678f9e5c75..a8bb2b0f8521 100644 --- a/homeassistant/components/bosch_shc/climate.py +++ b/homeassistant/components/bosch_shc/climate.py @@ -2,7 +2,12 @@ from typing import TYPE_CHECKING, Any, override -from boschshcpy import RoomClimateControlService, SHCClimateControl +from boschshcpy import ( + HeatingCircuitService, + RoomClimateControlService, + SHCClimateControl, + SHCHeatingCircuit, +) from homeassistant.components.climate import ( ATTR_HVAC_MODE, @@ -48,6 +53,15 @@ async def async_setup_entry( ) for device in session.device_helper.climate_controls ) + async_add_entities( + HeatingCircuit( + hass=hass, + device=device, + parent_id=shc_info.unique_id, + entry_id=config_entry.entry_id, + ) + for device in session.device_helper.heating_circuits + ) class ClimateControl(SHCEntity, ClimateEntity): @@ -211,3 +225,72 @@ class ClimateControl(SHCEntity, ClimateEntity): def turn_off(self) -> None: """Turn the climate device off.""" self.set_hvac_mode(HVACMode.OFF) + + +class HeatingCircuit(SHCEntity, ClimateEntity): + """Representation of a SHC heating circuit.""" + + _attr_name = None + _attr_target_temperature_step = 0.5 + _attr_native_temperature_unit = UnitOfTemperature.CELSIUS + _attr_hvac_modes = [HVACMode.AUTO, HVACMode.HEAT] + _attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE + _device: SHCHeatingCircuit + + @property + @override + def min_temp(self) -> float: + """Return the minimum settable temperature.""" + if (temperature_range := self._device.setpoint_temperature_range) is None: + return 5.0 + return temperature_range[0] + + @property + @override + def max_temp(self) -> float: + """Return the maximum settable temperature.""" + if (temperature_range := self._device.setpoint_temperature_range) is None: + return 30.0 + return temperature_range[1] + + @property + @override + def native_target_temperature(self) -> float | None: + """Return the target temperature.""" + return self._device.setpoint_temperature + + @property + @override + def hvac_mode(self) -> HVACMode: + """Return the current HVAC mode.""" + if self._device.operation_mode is HeatingCircuitService.OperationMode.AUTOMATIC: + return HVACMode.AUTO + return HVACMode.HEAT + + @property + @override + def hvac_action(self) -> HVACAction: + """Return the current HVAC action.""" + return HVACAction.HEATING if self._device.on else HVACAction.IDLE + + @override + def set_temperature(self, **kwargs: Any) -> None: + """Set the target temperature.""" + if (hvac_mode := kwargs.get(ATTR_HVAC_MODE)) is not None: + if hvac_mode not in self.hvac_modes: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="unsupported_hvac_mode", + translation_placeholders={"hvac_mode": hvac_mode}, + ) + self.set_hvac_mode(hvac_mode) + self._device.setpoint_temperature = kwargs[ATTR_TEMPERATURE] + + @override + def set_hvac_mode(self, hvac_mode: HVACMode) -> None: + """Set the HVAC mode.""" + self._device.operation_mode = ( + HeatingCircuitService.OperationMode.AUTOMATIC + if hvac_mode == HVACMode.AUTO + else HeatingCircuitService.OperationMode.MANUAL + ) diff --git a/homeassistant/components/bosch_shc/strings.json b/homeassistant/components/bosch_shc/strings.json index e0a3a4dc5729..03a065a47e42 100644 --- a/homeassistant/components/bosch_shc/strings.json +++ b/homeassistant/components/bosch_shc/strings.json @@ -251,7 +251,7 @@ }, "exceptions": { "unsupported_hvac_mode": { - "message": "This room does not support the HVAC mode {hvac_mode}." + "message": "This entity does not support the HVAC mode {hvac_mode}." } } } diff --git a/tests/components/bosch_shc/conftest.py b/tests/components/bosch_shc/conftest.py index 6c3322c5a98b..9774c83d039e 100644 --- a/tests/components/bosch_shc/conftest.py +++ b/tests/components/bosch_shc/conftest.py @@ -13,6 +13,7 @@ from boschshcpy import ( CameraFrontLightService, CameraLightService, CameraNotificationService, + HeatingCircuitService, PowerSwitchService, PrivacyModeService, RoomClimateControlService, @@ -22,6 +23,7 @@ from boschshcpy import ( SHCCameraEyes, SHCCameraOutdoorGen2, SHCClimateControl, + SHCHeatingCircuit, SHCIntrusionSystem, SHCLightSwitchBSM, SHCMicromoduleBlinds, @@ -97,6 +99,7 @@ _EMPTY_DEVICE_BUCKETS: dict[str, Any] = { "camera_eyes", "camera_outdoor_gen2", "climate_controls", + "heating_circuits", "light_switches_bsm", "micromodule_blinds", "micromodule_dimmers", @@ -417,6 +420,30 @@ def climate_control_device( return device +def heating_circuit_device( + device_id: str = "hdm:HomeMaticIP:hc1", + operation_mode: HeatingCircuitService.OperationMode = HeatingCircuitService.OperationMode.AUTOMATIC, + on: bool = False, + setpoint_temperature_range: tuple[float, float] | None = (5.0, 30.0), +) -> SHCHeatingCircuit: + """Build a minimal device double for the heating_circuits bucket.""" + device = create_autospec(SHCHeatingCircuit, instance=True, spec_set=True) + device.name = "Floor Heating" + device.id = device_id + device.root_device_id = "test-mac" + device.serial = f"serial-{device_id}" + device.manufacturer = "Bosch" + device.device_model = "HeatingCircuit" + device.device_services = [] + device.deleted = False + device.status = "AVAILABLE" + device.setpoint_temperature = 22.0 + device.operation_mode = operation_mode + device.on = on + device.setpoint_temperature_range = setpoint_temperature_range + return device + + def thermostat_device( device_id: str = "hdm:ZigBee:thermostat1", name: str = "Thermostat", diff --git a/tests/components/bosch_shc/test_climate.py b/tests/components/bosch_shc/test_climate.py index 5c9081b0ab8c..a42afd3cb735 100644 --- a/tests/components/bosch_shc/test_climate.py +++ b/tests/components/bosch_shc/test_climate.py @@ -3,7 +3,7 @@ from collections.abc import Generator from unittest.mock import MagicMock, patch -from boschshcpy import RoomClimateControlService +from boschshcpy import HeatingCircuitService, RoomClimateControlService import pytest from pytest_homeassistant.common import MockConfigEntry @@ -31,7 +31,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError -from .conftest import climate_control_device, setup_integration +from .conftest import climate_control_device, heating_circuit_device, setup_integration ENTITY_ID = "climate.living_room" AUTOMATIC = RoomClimateControlService.OperationMode.AUTOMATIC @@ -386,3 +386,162 @@ async def test_turn_on_off( ) assert device.summer_mode is False assert device.operation_mode == AUTOMATIC + + +HC_ENTITY_ID = "climate.floor_heating" +HC_AUTOMATIC = HeatingCircuitService.OperationMode.AUTOMATIC +HC_MANUAL = HeatingCircuitService.OperationMode.MANUAL + + +@pytest.mark.parametrize( + ("device_kwargs", "hvac_mode", "hvac_action"), + [ + ({}, HVACMode.AUTO, HVACAction.IDLE), + ({"on": True}, HVACMode.AUTO, HVACAction.HEATING), + ({"operation_mode": HC_MANUAL}, HVACMode.HEAT, HVACAction.IDLE), + ], + ids=["auto", "heating", "manual"], +) +async def test_heating_circuit_states( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_session: MagicMock, + device_kwargs: dict, + hvac_mode: HVACMode, + hvac_action: HVACAction, +) -> None: + """The heating circuit state maps onto HVAC mode and action.""" + mock_session.device_helper.heating_circuits = [ + heating_circuit_device(**device_kwargs) + ] + await setup_integration(hass, mock_config_entry) + + state = hass.states.get(HC_ENTITY_ID) + assert state is not None + assert state.state == hvac_mode + assert state.attributes[ATTR_HVAC_ACTION] == hvac_action + assert state.attributes[ATTR_TEMPERATURE] == 22.0 + assert state.attributes["current_temperature"] is None + + +@pytest.mark.parametrize( + ("temperature_range", "min_temp", "max_temp"), + [((5.0, 30.0), 5.0, 30.0), ((15.0, 28.0), 15.0, 28.0), (None, 5.0, 30.0)], + ids=["default", "raised-minimum", "unreported"], +) +async def test_heating_circuit_temperature_range( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_session: MagicMock, + temperature_range: tuple[float, float] | None, + min_temp: float, + max_temp: float, +) -> None: + """The setpoint range follows the device and falls back to 5-30 °C.""" + mock_session.device_helper.heating_circuits = [ + heating_circuit_device(setpoint_temperature_range=temperature_range) + ] + await setup_integration(hass, mock_config_entry) + + state = hass.states.get(HC_ENTITY_ID) + assert state is not None + assert state.attributes["min_temp"] == min_temp + assert state.attributes["max_temp"] == max_temp + + +@pytest.mark.parametrize( + ("hvac_mode", "value"), + [(HVACMode.AUTO, HC_AUTOMATIC), (HVACMode.HEAT, HC_MANUAL)], +) +async def test_heating_circuit_set_hvac_mode( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_session: MagicMock, + hvac_mode: HVACMode, + value: HeatingCircuitService.OperationMode, +) -> None: + """Setting an HVAC mode writes the operation mode.""" + device = heating_circuit_device() + mock_session.device_helper.heating_circuits = [device] + await setup_integration(hass, mock_config_entry) + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: HC_ENTITY_ID, "hvac_mode": hvac_mode}, + blocking=True, + ) + + assert device.operation_mode == value + + +async def test_heating_circuit_set_temperature( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_session: MagicMock, +) -> None: + """Setting the temperature writes the setpoint.""" + device = heating_circuit_device() + mock_session.device_helper.heating_circuits = [device] + await setup_integration(hass, mock_config_entry) + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + {ATTR_ENTITY_ID: HC_ENTITY_ID, ATTR_TEMPERATURE: 24.0}, + blocking=True, + ) + + assert device.setpoint_temperature == 24.0 + + +async def test_heating_circuit_set_temperature_with_hvac_mode( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_session: MagicMock, +) -> None: + """A combined call applies the mode and the setpoint.""" + device = heating_circuit_device(operation_mode=HC_AUTOMATIC) + mock_session.device_helper.heating_circuits = [device] + await setup_integration(hass, mock_config_entry) + + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: HC_ENTITY_ID, + ATTR_TEMPERATURE: 24.0, + "hvac_mode": HVACMode.HEAT, + }, + blocking=True, + ) + + assert device.operation_mode == HC_MANUAL + assert device.setpoint_temperature == 24.0 + + +async def test_heating_circuit_set_temperature_unsupported_hvac_mode( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_session: MagicMock, +) -> None: + """An unsupported mode in a combined call changes nothing.""" + device = heating_circuit_device(operation_mode=HC_AUTOMATIC) + mock_session.device_helper.heating_circuits = [device] + await setup_integration(hass, mock_config_entry) + setpoint = device.setpoint_temperature + + with pytest.raises(ServiceValidationError): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: HC_ENTITY_ID, + ATTR_TEMPERATURE: 24.0, + "hvac_mode": HVACMode.COOL, + }, + blocking=True, + ) + + assert device.operation_mode == HC_AUTOMATIC + assert device.setpoint_temperature == setpoint