diff --git a/homeassistant/components/novy_cooker_hood/config_flow.py b/homeassistant/components/novy_cooker_hood/config_flow.py index 7a7d8d93916..f8c316fa822 100644 --- a/homeassistant/components/novy_cooker_hood/config_flow.py +++ b/homeassistant/components/novy_cooker_hood/config_flow.py @@ -9,7 +9,11 @@ from homeassistant.components.radio_frequency import ( async_get_transmitters, async_send_command, ) -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import ( + SOURCE_RECONFIGURE, + ConfigFlow, + ConfigFlowResult, +) from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er, selector @@ -43,7 +47,26 @@ class NovyCookerHoodConfigFlow(ConfigFlow, domain=DOMAIN): async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: - """Pick a transmitter and code.""" + """Pick a transmitter and code for a new entry.""" + return await self._async_step_picker("user", user_input) + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Pick a transmitter and code to update an existing entry.""" + if user_input is None and self._transmitter_entity_id is None: + entry = self._get_reconfigure_entry() + transmitter = er.async_get(self.hass).async_get( + entry.data[CONF_TRANSMITTER] + ) + self._transmitter_entity_id = transmitter.entity_id if transmitter else None + self._code = entry.data[CONF_CODE] + return await self._async_step_picker("reconfigure", user_input) + + async def _async_step_picker( + self, step_id: str, user_input: dict[str, Any] | None + ) -> ConfigFlowResult: + """Show the transmitter+code picker shared by user and reconfigure steps.""" try: transmitters = async_get_transmitters(self.hass, FREQUENCY, MODULATION) except HomeAssistantError: @@ -57,8 +80,17 @@ class NovyCookerHoodConfigFlow(ConfigFlow, domain=DOMAIN): entity_entry = registry.async_get(user_input[CONF_TRANSMITTER]) assert entity_entry is not None code = int(user_input[CONF_CODE]) - await self.async_set_unique_id(f"{entity_entry.id}_{code}") - self._abort_if_unique_id_configured() + unique_id = f"{entity_entry.id}_{code}" + await self.async_set_unique_id(unique_id) + if self.source == SOURCE_RECONFIGURE: + existing = self.hass.config_entries.async_entry_for_domain_unique_id( + DOMAIN, unique_id + ) + reconfigure_entry = self._get_reconfigure_entry() + if existing and existing.entry_id != reconfigure_entry.entry_id: + return self.async_abort(reason="already_configured") + else: + self._abort_if_unique_id_configured() self._transmitter_entity_id = entity_entry.entity_id self._transmitter_id = entity_entry.id self._code = code @@ -80,7 +112,7 @@ class NovyCookerHoodConfigFlow(ConfigFlow, domain=DOMAIN): ), } return self.async_show_form( - step_id="user", + step_id=step_id, data_schema=vol.Schema(schema), ) @@ -117,18 +149,21 @@ class NovyCookerHoodConfigFlow(ConfigFlow, domain=DOMAIN): async def async_step_retry( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: - """Return to the code selection step.""" + """Return to the picker step matching the current source.""" + if self.source == SOURCE_RECONFIGURE: + return await self.async_step_reconfigure() return await self.async_step_user() async def async_step_finish( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: - """Create the config entry.""" + """Create or update the config entry.""" assert self._transmitter_id is not None - return self.async_create_entry( - title="Novy Cooker Hood", - data={ - CONF_TRANSMITTER: self._transmitter_id, - CONF_CODE: self._code, - }, - ) + data = {CONF_TRANSMITTER: self._transmitter_id, CONF_CODE: self._code} + if self.source == SOURCE_RECONFIGURE: + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data_updates=data, + unique_id=f"{self._transmitter_id}_{self._code}", + ) + return self.async_create_entry(title="Novy Cooker Hood", data=data) diff --git a/homeassistant/components/novy_cooker_hood/quality_scale.yaml b/homeassistant/components/novy_cooker_hood/quality_scale.yaml index 277ee4aba26..a4dcab9b588 100644 --- a/homeassistant/components/novy_cooker_hood/quality_scale.yaml +++ b/homeassistant/components/novy_cooker_hood/quality_scale.yaml @@ -90,7 +90,7 @@ rules: status: exempt comment: | The light entity uses the default icon for its state. - reconfiguration-flow: todo + reconfiguration-flow: done repair-issues: status: exempt comment: | diff --git a/homeassistant/components/novy_cooker_hood/strings.json b/homeassistant/components/novy_cooker_hood/strings.json index 1a546af6fb9..2e73787095f 100644 --- a/homeassistant/components/novy_cooker_hood/strings.json +++ b/homeassistant/components/novy_cooker_hood/strings.json @@ -3,9 +3,21 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "no_compatible_transmitters": "No radio frequency transmitter supports 433.92 MHz OOK transmissions. Please add a compatible transmitter first.", - "no_transmitters": "No radio frequency transmitters are available. Please set up a transmitter first." + "no_transmitters": "No radio frequency transmitters are available. Please set up a transmitter first.", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" }, "step": { + "reconfigure": { + "data": { + "code": "[%key:component::novy_cooker_hood::config::step::user::data::code%]", + "transmitter": "[%key:component::novy_cooker_hood::config::step::user::data::transmitter%]" + }, + "data_description": { + "code": "[%key:component::novy_cooker_hood::config::step::user::data_description::code%]", + "transmitter": "[%key:component::novy_cooker_hood::config::step::user::data_description::transmitter%]" + }, + "description": "[%key:component::novy_cooker_hood::config::step::user::description%]" + }, "test_failed": { "description": "Could not send the test command for code {code}. Check that your radio frequency transmitter is online, then press Retry.", "menu_options": { diff --git a/tests/components/novy_cooker_hood/test_config_flow.py b/tests/components/novy_cooker_hood/test_config_flow.py index f60e439eb67..da7644c1abb 100644 --- a/tests/components/novy_cooker_hood/test_config_flow.py +++ b/tests/components/novy_cooker_hood/test_config_flow.py @@ -59,7 +59,6 @@ async def test_user_flow_test_then_finish( """Submitting the user step fires the test, then Finish creates the entry.""" result = await _start_user_flow(hass, code="3") - # Test was fired automatically (toggle on, wait, toggle off). assert result["type"] is FlowResultType.MENU assert result["step_id"] == "test_light" mock_get_codes.async_load_command.assert_awaited_with(COMMAND_LIGHT) @@ -89,14 +88,12 @@ async def test_user_flow_retry_picks_different_code( result = await _start_user_flow(hass, code="1") assert result["type"] is FlowResultType.MENU - # Pick Retry → back to user step. result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={"next_step_id": "retry"} ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" - # Submit a different code → test fires again, menu shown. result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -131,6 +128,40 @@ async def test_user_flow_test_transmit_failure( assert result["step_id"] == "test_failed" +async def test_recover_after_transmit_failure( + hass: HomeAssistant, + mock_get_codes: MagicMock, + mock_rf_entity: MockRadioFrequencyEntity, +) -> None: + """The user can Retry from `test_failed` and complete the flow on a fresh attempt.""" + with patch( + "homeassistant.components.novy_cooker_hood.config_flow.async_send_command", + side_effect=HomeAssistantError("nope"), + ): + result = await _start_user_flow(hass) + + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "test_failed" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={"next_step_id": "retry"} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_TRANSMITTER: TRANSMITTER_ENTITY_ID, CONF_CODE: "1"}, + ) + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "test_light" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={"next_step_id": "finish"} + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + + async def test_unique_id_already_configured( hass: HomeAssistant, mock_config_entry: MockConfigEntry, @@ -175,6 +206,117 @@ async def test_same_transmitter_different_code_is_allowed( assert result["result"].unique_id == f"{entity_entry.id}_5" +async def test_reconfigure_updates_entry( + hass: HomeAssistant, + mock_get_codes: MagicMock, + init_novy_cooker_hood: MockConfigEntry, + mock_rf_entity: MockRadioFrequencyEntity, + entity_registry: er.EntityRegistry, +) -> None: + """Reconfigure can change the code on an existing entry.""" + result = await init_novy_cooker_hood.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_TRANSMITTER: TRANSMITTER_ENTITY_ID, + CONF_CODE: "4", + }, + ) + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "test_light" + mock_get_codes.async_load_command.assert_awaited_with(COMMAND_LIGHT) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={"next_step_id": "finish"} + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert init_novy_cooker_hood.data[CONF_CODE] == 4 + entity_entry = entity_registry.async_get(TRANSMITTER_ENTITY_ID) + assert init_novy_cooker_hood.unique_id == f"{entity_entry.id}_4" + + +async def test_reconfigure_frees_old_unique_id( + hass: HomeAssistant, + mock_get_codes: MagicMock, + init_novy_cooker_hood: MockConfigEntry, + mock_rf_entity: MockRadioFrequencyEntity, +) -> None: + """After reconfigure, the previous (transmitter, code) can be reused.""" + # Reconfigure away from code 1. + result = await init_novy_cooker_hood.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_TRANSMITTER: TRANSMITTER_ENTITY_ID, CONF_CODE: "4"}, + ) + await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={"next_step_id": "finish"} + ) + await hass.async_block_till_done() + + # Adding a new entry on the freed (transmitter, code 1) should now work. + result = await _start_user_flow(hass, code="1") + assert result["type"] is FlowResultType.MENU + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={"next_step_id": "finish"} + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_CODE] == 1 + + +async def test_reconfigure_aborts_on_collision( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_rf_entity: MockRadioFrequencyEntity, + entity_registry: er.EntityRegistry, +) -> None: + """Reconfigure aborts when the new (transmitter, code) is already used.""" + # First entry: code 1 (already in mock_config_entry). + mock_config_entry.add_to_hass(hass) + # Second entry: code 9 — the one we'll try to reconfigure to code 1. + entity_entry = entity_registry.async_get(TRANSMITTER_ENTITY_ID) + other = MockConfigEntry( + domain=DOMAIN, + title="Novy Cooker Hood", + data={CONF_TRANSMITTER: entity_entry.id, CONF_CODE: 9}, + unique_id=f"{entity_entry.id}_9", + ) + other.add_to_hass(hass) + + result = await other.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_TRANSMITTER: TRANSMITTER_ENTITY_ID, CONF_CODE: "1"}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_reconfigure_retry_returns_to_picker( + hass: HomeAssistant, + mock_get_codes: MagicMock, + init_novy_cooker_hood: MockConfigEntry, + mock_rf_entity: MockRadioFrequencyEntity, +) -> None: + """Picking Retry from the test menu during reconfigure shows the reconfigure form.""" + result = await init_novy_cooker_hood.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_TRANSMITTER: TRANSMITTER_ENTITY_ID, CONF_CODE: "2"}, + ) + assert result["type"] is FlowResultType.MENU + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={"next_step_id": "retry"} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + + async def test_no_transmitters(hass: HomeAssistant) -> None: """Test the flow aborts when no RF transmitters are registered at all.""" result = await hass.config_entries.flow.async_init( @@ -185,6 +327,27 @@ async def test_no_transmitters(hass: HomeAssistant) -> None: assert result["reason"] == "no_transmitters" +async def test_recover_after_no_transmitters( + hass: HomeAssistant, + mock_get_codes: MagicMock, +) -> None: + """User can re-init the flow after the radio_frequency integration loads.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_transmitters" + + assert await async_setup_component(hass, RF_DOMAIN, {}) + await hass.async_block_till_done() + transmitter = MockRadioFrequencyEntity("test_rf_transmitter") + await hass.data[DATA_COMPONENT].async_add_entities([transmitter]) + + result = await _start_user_flow(hass, code="1") + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "test_light" + + async def test_no_compatible_transmitters(hass: HomeAssistant) -> None: """Test aborting when transmitters exist but none support 433.92 MHz OOK.""" assert await async_setup_component(hass, RF_DOMAIN, {})