diff --git a/homeassistant/components/bosch_shc/select.py b/homeassistant/components/bosch_shc/select.py index 744c20565fb8..a86c53fed258 100644 --- a/homeassistant/components/bosch_shc/select.py +++ b/homeassistant/components/bosch_shc/select.py @@ -303,6 +303,29 @@ ACTUATOR_TYPE_DESCRIPTION = SHCSelectEntityDescription[SHCMicromoduleRelay]( ) +def _output_mode_select_option(device: SHCMicromoduleRelay, option: str) -> None: + """Write the relay's output mode.""" + device.output_mode = SwitchConfiguration.OutputMode[option.upper()] + + +OUTPUT_MODE_DESCRIPTION = SHCSelectEntityDescription[SHCMicromoduleRelay]( + key="output_mode", + translation_key="output_mode", + entity_category=EntityCategory.CONFIG, + options=[ + "attached", + "detached", + "detached_short_press", + "detached_long_press", + "unsupported", + ], + current_option_fn=lambda device, options: ( + value.name.lower() if (value := device.output_mode) else None + ), + select_option_fn=_output_mode_select_option, +) + + async def async_setup_entry( hass: HomeAssistant, config_entry: BoschConfigEntry, @@ -486,6 +509,17 @@ async def async_setup_entry( for relay in session.device_helper.micromodule_relays if relay.supports_switch_configuration and relay.actuator_type is not None ) + entities.extend( + SHCSelect( + hass=hass, + device=relay, + parent_id=shc_info.unique_id, + entry_id=config_entry.entry_id, + description=OUTPUT_MODE_DESCRIPTION, + ) + for relay in session.device_helper.micromodule_relays + if relay.supports_switch_configuration and relay.output_mode is not None + ) async_add_entities(entities) diff --git a/homeassistant/components/bosch_shc/strings.json b/homeassistant/components/bosch_shc/strings.json index 065842c9656f..9f686b757447 100644 --- a/homeassistant/components/bosch_shc/strings.json +++ b/homeassistant/components/bosch_shc/strings.json @@ -131,6 +131,16 @@ "middle": "[%key:common::state::medium%]" } }, + "output_mode": { + "name": "Output mode", + "state": { + "attached": "Coupled", + "detached": "Disconnected on any key press", + "detached_long_press": "Disconnected on long press", + "detached_short_press": "Disconnected on short press", + "unsupported": "Unsupported" + } + }, "siren_sound_level": { "name": "Siren volume", "state": { diff --git a/tests/components/bosch_shc/conftest.py b/tests/components/bosch_shc/conftest.py index fb0fddc742e8..6cf91ad58251 100644 --- a/tests/components/bosch_shc/conftest.py +++ b/tests/components/bosch_shc/conftest.py @@ -598,6 +598,7 @@ def micromodule_relay_device( impulse_length_raises_key_error: bool = False, switch_type: SwitchConfiguration.SwitchType | None = None, actuator_type: SwitchConfiguration.ActuatorType | None = None, + output_mode: SwitchConfiguration.OutputMode | None = None, ) -> SHCMicromoduleRelay: """Build a minimal device double for the micromodule_relays bucket.""" device = create_autospec(SHCMicromoduleRelay, instance=True, spec_set=True) @@ -617,6 +618,7 @@ def micromodule_relay_device( device.swap_outputs = swap_outputs device.switch_type = switch_type device.actuator_type = actuator_type + device.output_mode = output_mode if impulse_length_raises_key_error: type(device).impulse_length = PropertyMock( side_effect=KeyError("impulseLength") diff --git a/tests/components/bosch_shc/test_select.py b/tests/components/bosch_shc/test_select.py index 1e5f42ac79be..1b0ed67df125 100644 --- a/tests/components/bosch_shc/test_select.py +++ b/tests/components/bosch_shc/test_select.py @@ -790,3 +790,57 @@ async def test_relay_actuator_type_unsupported( await setup_integration(hass, mock_config_entry) assert hass.states.get(ACTUATOR_TYPE_ENTITY_ID) is None + + +OUTPUT_MODE_ENTITY_ID = "select.relay_output_mode" + + +@pytest.mark.parametrize( + "device_buckets", + [ + { + "micromodule_relays": [ + micromodule_relay_device( + supports_switch_configuration=True, + output_mode=SwitchConfiguration.OutputMode.ATTACHED, + ) + ] + } + ], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_relay_output_mode( + hass: HomeAssistant, + mock_session: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """The relay output mode is reported and written to the device.""" + await setup_integration(hass, mock_config_entry) + device = mock_session.device_helper.micromodule_relays[0] + + assert hass.states.get(OUTPUT_MODE_ENTITY_ID).state == "attached" + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: OUTPUT_MODE_ENTITY_ID, "option": "detached"}, + blocking=True, + ) + assert device.output_mode == SwitchConfiguration.OutputMode.DETACHED + + +@pytest.mark.parametrize( + "device_buckets", + [{"micromodule_relays": [micromodule_relay_device()]}], + indirect=True, +) +@pytest.mark.usefixtures("mock_session") +async def test_relay_output_mode_unsupported( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """No output mode select is created without switch configuration support.""" + await setup_integration(hass, mock_config_entry) + + assert hass.states.get(OUTPUT_MODE_ENTITY_ID) is None