diff --git a/homeassistant/components/silvercrest_infrared/config_flow.py b/homeassistant/components/silvercrest_infrared/config_flow.py index ce20c22739b7..c7e05f034970 100644 --- a/homeassistant/components/silvercrest_infrared/config_flow.py +++ b/homeassistant/components/silvercrest_infrared/config_flow.py @@ -64,26 +64,70 @@ class SilverCrestIfConfigFlow(ConfigFlow, domain=DOMAIN): return self.async_show_form( step_id="user", - data_schema=probatio.Schema( - { - probatio.Required(CONF_DEVICE_TYPE): SelectSelector( - SelectSelectorConfig( - options=[ - device_type.value - for device_type in SilverCrestIrDeviceType - ], - translation_key=CONF_DEVICE_TYPE, - mode=SelectSelectorMode.DROPDOWN, - sort=True, + data_schema=self.add_suggested_values_to_schema( + probatio.Schema( + { + probatio.Required(CONF_DEVICE_TYPE): SelectSelector( + SelectSelectorConfig( + options=[ + device_type.value + for device_type in SilverCrestIrDeviceType + ], + translation_key=CONF_DEVICE_TYPE, + mode=SelectSelectorMode.DROPDOWN, + sort=True, + ) + ), + probatio.Optional(CONF_INFRARED_ENTITY_ID): EntitySelector( + EntitySelectorConfig( + domain=INFRARED_DOMAIN, + include_entities=emitter_entity_ids, + ) + ), + } + ), + user_input, + ), + errors=errors, + ) + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfigure flow.""" + errors: dict[str, str] = {} + + entry = self._get_reconfigure_entry() + + emitter_entity_ids = async_get_emitters(self.hass) + if not emitter_entity_ids: + return self.async_abort(reason="no_infrared_entities") + + if user_input is not None: + emitter_id = user_input.get(CONF_INFRARED_ENTITY_ID) + if emitter_id: + self._async_abort_entries_match({CONF_INFRARED_ENTITY_ID: emitter_id}) + return self.async_update_reload_and_abort( + entry, + data={CONF_DEVICE_TYPE: entry.data[CONF_DEVICE_TYPE], **user_input}, + ) + + errors["base"] = "missing_infrared_entity" + + return self.async_show_form( + step_id="reconfigure", + data_schema=self.add_suggested_values_to_schema( + probatio.Schema( + { + probatio.Optional(CONF_INFRARED_ENTITY_ID): EntitySelector( + EntitySelectorConfig( + domain=INFRARED_DOMAIN, + include_entities=emitter_entity_ids, + ) ) - ), - probatio.Optional(CONF_INFRARED_ENTITY_ID): EntitySelector( - EntitySelectorConfig( - domain=INFRARED_DOMAIN, - include_entities=emitter_entity_ids, - ) - ), - } + } + ), + entry.data, ), errors=errors, ) diff --git a/homeassistant/components/silvercrest_infrared/quality_scale.yaml b/homeassistant/components/silvercrest_infrared/quality_scale.yaml index 113c5fbed169..fe69d5860836 100644 --- a/homeassistant/components/silvercrest_infrared/quality_scale.yaml +++ b/homeassistant/components/silvercrest_infrared/quality_scale.yaml @@ -99,7 +99,7 @@ rules: comment: | This integration does not raise exceptions. icon-translations: done - reconfiguration-flow: todo + reconfiguration-flow: done repair-issues: status: exempt comment: | diff --git a/homeassistant/components/silvercrest_infrared/strings.json b/homeassistant/components/silvercrest_infrared/strings.json index 911b6ff81bd0..4aa2c00454e0 100644 --- a/homeassistant/components/silvercrest_infrared/strings.json +++ b/homeassistant/components/silvercrest_infrared/strings.json @@ -8,6 +8,15 @@ "missing_infrared_entity": "Select an infrared emitter." }, "step": { + "reconfigure": { + "data": { + "infrared_entity_id": "[%key:common::config_flow::data::infrared_entity_id%]" + }, + "data_description": { + "infrared_entity_id": "[%key:common::config_flow::data_description::infrared_entity_id%]" + }, + "title": "Reconfigure SilverCrest Infrared device" + }, "user": { "data": { "device_type": "[%key:common::generic::device_type%]", diff --git a/tests/components/silvercrest_infrared/test_config_flow.py b/tests/components/silvercrest_infrared/test_config_flow.py index 3eeee66b7a87..5057e8678de1 100644 --- a/tests/components/silvercrest_infrared/test_config_flow.py +++ b/tests/components/silvercrest_infrared/test_config_flow.py @@ -130,3 +130,105 @@ async def test_user_flow_requires_emitter( }, ) assert result["type"] is FlowResultType.CREATE_ENTRY + + +@pytest.mark.usefixtures("mock_infrared_emitter_entity") +async def test_flow_reconfigure(hass: HomeAssistant) -> None: + """Test reconfigure flow.""" + config_entry = MockConfigEntry( + domain=DOMAIN, + title="Tower fan STVD 45 A1 via Test IR emitter", + entry_id="1234567890", + data={ + CONF_DEVICE_TYPE: SilverCrestIrDeviceType.STVD45A1, + CONF_INFRARED_ENTITY_ID: None, + }, + ) + config_entry.add_to_hass(hass) + result = await config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_INFRARED_ENTITY_ID: EMITTER_ENTITY_ID}, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert config_entry.data[CONF_INFRARED_ENTITY_ID] == EMITTER_ENTITY_ID + + assert len(hass.config_entries.async_entries()) == 1 + + +@pytest.mark.usefixtures("mock_infrared_emitter_entity") +async def test_reconfigure_flow_requires_emitter( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test reconfigure flow requires an infrared emitter.""" + config_entry.add_to_hass(hass) + result = await config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "missing_infrared_entity"} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_INFRARED_ENTITY_ID: EMITTER_ENTITY_ID}, + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + + +@pytest.mark.usefixtures("mock_infrared_emitter_entity") +async def test_flow_reconfigure_already_configured( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test reconfigure flow.""" + config_entry_2 = MockConfigEntry( + domain=DOMAIN, + title="Tower fan STVD 45 A1 via Test IR emitter", + entry_id="0987654321", + data={ + CONF_DEVICE_TYPE: SilverCrestIrDeviceType.STVD45A1, + CONF_INFRARED_ENTITY_ID: None, + }, + ) + config_entry.add_to_hass(hass) + config_entry_2.add_to_hass(hass) + result = await config_entry_2.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_INFRARED_ENTITY_ID: EMITTER_ENTITY_ID}, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +@pytest.mark.usefixtures("init_infrared") +async def test_reconfigure_flow_no_emitters( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test reconfigure flow aborts when no infrared emitters exist.""" + config_entry.add_to_hass(hass) + result = await config_entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_infrared_entities"