diff --git a/homeassistant/components/bosch_shc/select.py b/homeassistant/components/bosch_shc/select.py index b56a496f41ca..38dcdceb440c 100644 --- a/homeassistant/components/bosch_shc/select.py +++ b/homeassistant/components/bosch_shc/select.py @@ -8,11 +8,15 @@ from boschshcpy import ( OutdoorSirenService, SHCMotionDetector2, SHCOutdoorSiren, + SHCRoomThermostat2, SHCShutterContact2Plus, SHCSmokeDetector, + SHCThermostatGen2, ) from boschshcpy.device import SHCDevice from boschshcpy.services_impl import ( + DisplayDirection, + DisplayedTemperatureConfiguration, PirSensorConfigurationService, SmokeSensitivityService, VibrationSensorService, @@ -115,6 +119,50 @@ SMOKE_SENSITIVITY_DESCRIPTION = SHCSelectEntityDescription[SHCSmokeDetector]( ) +def _display_direction_select_option( + device: SHCThermostatGen2 | SHCRoomThermostat2, option: str +) -> None: + """Write the thermostat's display direction.""" + device.display_direction = DisplayDirection.Direction[option.upper()] + + +DISPLAY_DIRECTION_DESCRIPTION = SHCSelectEntityDescription[ + SHCThermostatGen2 | SHCRoomThermostat2 +]( + key="display_direction", + translation_key="display_direction", + entity_category=EntityCategory.CONFIG, + options=["normal", "reversed"], + current_option_fn=lambda device, options: ( + direction.name.lower() if (direction := device.display_direction) else None + ), + select_option_fn=_display_direction_select_option, +) + + +def _displayed_temperature_select_option( + device: SHCThermostatGen2 | SHCRoomThermostat2, option: str +) -> None: + """Write which temperature the thermostat display shows.""" + device.displayed_temperature = ( + DisplayedTemperatureConfiguration.DisplayedTemperature[option.upper()] + ) + + +DISPLAYED_TEMPERATURE_DESCRIPTION = SHCSelectEntityDescription[ + SHCThermostatGen2 | SHCRoomThermostat2 +]( + key="displayed_temperature", + translation_key="displayed_temperature", + entity_category=EntityCategory.CONFIG, + options=["setpoint", "measured"], + current_option_fn=lambda device, options: ( + shown.name.lower() if (shown := device.displayed_temperature) else None + ), + select_option_fn=_displayed_temperature_select_option, +) + + async def async_setup_entry( hass: HomeAssistant, config_entry: BoschConfigEntry, @@ -126,7 +174,8 @@ async def async_setup_entry( if TYPE_CHECKING: assert shc_info is not None and shc_info.unique_id is not None - async_add_entities( + entities: list[SHCSelect] = [] + entities.extend( SHCSelect( hass=hass, device=siren, @@ -146,7 +195,7 @@ async def async_setup_entry( continue motion_detectors.append(detector) - async_add_entities( + entities.extend( SHCSelect( hass=hass, device=detector, @@ -157,7 +206,7 @@ async def async_setup_entry( for detector in motion_detectors ) - async_add_entities( + entities.extend( SHCSelect( hass=hass, device=contact, @@ -169,7 +218,7 @@ async def async_setup_entry( if isinstance(contact, SHCShutterContact2Plus) ) - async_add_entities( + entities.extend( SHCSelect( hass=hass, device=smoke_detector, @@ -181,6 +230,39 @@ async def async_setup_entry( if smoke_detector.smoke_sensitivity is not None ) + thermostats = [ + thermostat + for thermostat in ( + *session.device_helper.thermostats, + *session.device_helper.roomthermostats, + ) + if isinstance(thermostat, (SHCThermostatGen2, SHCRoomThermostat2)) + ] + entities.extend( + SHCSelect( + hass=hass, + device=thermostat, + parent_id=shc_info.unique_id, + entry_id=config_entry.entry_id, + description=DISPLAY_DIRECTION_DESCRIPTION, + ) + for thermostat in thermostats + if thermostat.supports_display_direction + ) + entities.extend( + SHCSelect( + hass=hass, + device=thermostat, + parent_id=shc_info.unique_id, + entry_id=config_entry.entry_id, + description=DISPLAYED_TEMPERATURE_DESCRIPTION, + ) + for thermostat in thermostats + if thermostat.supports_displayed_temperature + ) + + async_add_entities(entities) + class SHCSelect[_DeviceT: SHCDevice](SHCEntity, SelectEntity): """Generic SHC select entity, driven by a SHCSelectEntityDescription.""" diff --git a/homeassistant/components/bosch_shc/strings.json b/homeassistant/components/bosch_shc/strings.json index be72fc462926..e0a3a4dc5729 100644 --- a/homeassistant/components/bosch_shc/strings.json +++ b/homeassistant/components/bosch_shc/strings.json @@ -87,6 +87,20 @@ } }, "select": { + "display_direction": { + "name": "Display orientation", + "state": { + "normal": "[%key:common::state::normal%]", + "reversed": "Rotated by 180°" + } + }, + "displayed_temperature": { + "name": "Displayed temperature", + "state": { + "measured": "Measured", + "setpoint": "Set temperature" + } + }, "motion_sensitivity": { "name": "Motion sensitivity", "state": { diff --git a/tests/components/bosch_shc/conftest.py b/tests/components/bosch_shc/conftest.py index 325d23f1348e..d90d1ed4c169 100644 --- a/tests/components/bosch_shc/conftest.py +++ b/tests/components/bosch_shc/conftest.py @@ -31,6 +31,7 @@ from boschshcpy import ( SHCMotionDetector2, SHCOutdoorSiren, SHCPresenceSimulationSystem, + SHCRoomThermostat2, SHCShutterContact, SHCShutterContact2, SHCShutterContact2Plus, @@ -47,6 +48,8 @@ from boschshcpy import ( ThermostatService, ) from boschshcpy.services_impl import ( + DisplayDirection, + DisplayedTemperatureConfiguration, OutdoorSirenService, PirSensorConfigurationService, PresenceSimulationConfigurationService, @@ -448,9 +451,15 @@ def thermostat_gen2_device( name: str = "Thermostat Gen2", supports_display_configuration: bool = False, humidity_warning_enabled: bool = False, -) -> SHCThermostatGen2: + display_direction: DisplayDirection.Direction | None = None, + displayed_temperature: DisplayedTemperatureConfiguration.DisplayedTemperature + | None = None, + supports_display_direction: bool | None = None, + supports_displayed_temperature: bool | None = None, + spec: type[SHCThermostatGen2 | SHCRoomThermostat2] = SHCThermostatGen2, +) -> SHCThermostatGen2 | SHCRoomThermostat2: """Build a minimal device double for the thermostats/roomthermostats buckets (Gen2).""" - device = create_autospec(SHCThermostatGen2, instance=True, spec_set=True) + device = create_autospec(spec, instance=True, spec_set=True) device.name = name device.id = device_id device.root_device_id = "test-mac" @@ -462,6 +471,18 @@ def thermostat_gen2_device( device.status = "AVAILABLE" device.supports_display_configuration = supports_display_configuration device.humidity_warning_enabled = humidity_warning_enabled + device.supports_display_direction = ( + display_direction is not None + if supports_display_direction is None + else supports_display_direction + ) + device.display_direction = display_direction + device.supports_displayed_temperature = ( + displayed_temperature is not None + if supports_displayed_temperature is None + else supports_displayed_temperature + ) + device.displayed_temperature = displayed_temperature return device diff --git a/tests/components/bosch_shc/test_select.py b/tests/components/bosch_shc/test_select.py index d4ee20f0ebac..74a488911eca 100644 --- a/tests/components/bosch_shc/test_select.py +++ b/tests/components/bosch_shc/test_select.py @@ -2,7 +2,10 @@ from unittest.mock import MagicMock +from boschshcpy import SHCRoomThermostat2 from boschshcpy.services_impl import ( + DisplayDirection, + DisplayedTemperatureConfiguration, OutdoorSirenService, PirSensorConfigurationService, SmokeSensitivityService, @@ -11,7 +14,7 @@ from boschshcpy.services_impl import ( import pytest from homeassistant.components.select import DOMAIN as SELECT_DOMAIN -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_SELECT_OPTION +from homeassistant.const import ATTR_ENTITY_ID, SERVICE_SELECT_OPTION, STATE_UNKNOWN from homeassistant.core import HomeAssistant from .conftest import ( @@ -21,12 +24,15 @@ from .conftest import ( shutter_contact2_device, shutter_contact2_plus_device, smoke_detector_device, + thermostat_gen2_device, ) from tests.common import MockConfigEntry SOUND_LEVEL_ENTITY_ID = "select.outdoor_siren_siren_volume" MOTION_SENSITIVITY_ENTITY_ID = "select.motion_detector_motion_sensitivity" +DISPLAY_DIRECTION_ENTITY_ID = "select.thermostat_gen2_display_orientation" +DISPLAYED_TEMPERATURE_ENTITY_ID = "select.thermostat_gen2_displayed_temperature" SMOKE_SENSITIVITY_ENTITY_ID = "select.smoke_detector_smoke_sensitivity" VIBRATION_SENSITIVITY_ENTITY_ID = "select.shutter_contact_vibration_sensitivity" @@ -308,3 +314,160 @@ async def test_smoke_sensitivity_unsupported( await setup_integration(hass, mock_config_entry) assert hass.states.get(SMOKE_SENSITIVITY_ENTITY_ID) is None + + +@pytest.mark.parametrize( + "device_buckets", + [ + { + "thermostats": [ + thermostat_gen2_device( + display_direction=DisplayDirection.Direction.REVERSED, + displayed_temperature=DisplayedTemperatureConfiguration.DisplayedTemperature.MEASURED, + ) + ] + } + ], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_thermostat_display_current_options( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """The thermostat's display direction and shown temperature are reported.""" + await setup_integration(hass, mock_config_entry) + + state = hass.states.get(DISPLAY_DIRECTION_ENTITY_ID) + assert state is not None + assert state.state == "reversed" + state = hass.states.get(DISPLAYED_TEMPERATURE_ENTITY_ID) + assert state is not None + assert state.state == "measured" + + +@pytest.mark.parametrize( + "device_buckets", + [ + { + "thermostats": [ + thermostat_gen2_device( + display_direction=DisplayDirection.Direction.NORMAL, + displayed_temperature=DisplayedTemperatureConfiguration.DisplayedTemperature.MEASURED, + ) + ] + } + ], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_thermostat_display_select_options( + hass: HomeAssistant, + mock_session: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Selecting options writes them to the thermostat.""" + await setup_integration(hass, mock_config_entry) + device = mock_session.device_helper.thermostats[0] + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: DISPLAY_DIRECTION_ENTITY_ID, "option": "reversed"}, + blocking=True, + ) + assert device.display_direction == DisplayDirection.Direction.REVERSED + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: DISPLAYED_TEMPERATURE_ENTITY_ID, "option": "setpoint"}, + blocking=True, + ) + assert ( + device.displayed_temperature + == DisplayedTemperatureConfiguration.DisplayedTemperature.SETPOINT + ) + + +@pytest.mark.parametrize( + "device_buckets", + [{"thermostats": [thermostat_gen2_device()]}], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_thermostat_display_unsupported( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """No selects are created without display support.""" + await setup_integration(hass, mock_config_entry) + + assert hass.states.get(DISPLAY_DIRECTION_ENTITY_ID) is None + assert hass.states.get(DISPLAYED_TEMPERATURE_ENTITY_ID) is None + + +@pytest.mark.parametrize( + "device_buckets", + [ + { + "thermostats": [ + thermostat_gen2_device( + supports_display_direction=True, + supports_displayed_temperature=True, + ) + ] + } + ], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_thermostat_display_unknown_initial_values( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """Supported display selects are created even without an initial value.""" + await setup_integration(hass, mock_config_entry) + + assert hass.states.get(DISPLAY_DIRECTION_ENTITY_ID).state == STATE_UNKNOWN + assert hass.states.get(DISPLAYED_TEMPERATURE_ENTITY_ID).state == STATE_UNKNOWN + + +@pytest.mark.parametrize( + "device_buckets", + [ + { + "roomthermostats": [ + thermostat_gen2_device( + spec=SHCRoomThermostat2, + display_direction=DisplayDirection.Direction.NORMAL, + displayed_temperature=DisplayedTemperatureConfiguration.DisplayedTemperature.SETPOINT, + ) + ] + } + ], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_room_thermostat_2_display_selects( + hass: HomeAssistant, + mock_session: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Room Thermostat II gets the display selects and writes to the device.""" + await setup_integration(hass, mock_config_entry) + device = mock_session.device_helper.roomthermostats[0] + entity_id = "select.thermostat_gen2_display_orientation" + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "normal" + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: entity_id, "option": "reversed"}, + blocking=True, + ) + assert device.display_direction == DisplayDirection.Direction.REVERSED + assert hass.states.get(DISPLAYED_TEMPERATURE_ENTITY_ID) is not None