diff --git a/homeassistant/components/enphase_envoy/config_flow.py b/homeassistant/components/enphase_envoy/config_flow.py index 70ba3570e91..1a2186d305e 100644 --- a/homeassistant/components/enphase_envoy/config_flow.py +++ b/homeassistant/components/enphase_envoy/config_flow.py @@ -141,9 +141,13 @@ class EnphaseConfigFlow(ConfigFlow, domain=DOMAIN): and entry.data[CONF_HOST] == self.ip_address ): _LOGGER.debug( - "Zeroconf update envoy with this ip and blank serial in unique_id", + "Zeroconf update envoy with this ip and blank unique_id", ) - title = f"{ENVOY} {serial}" if entry.title == ENVOY else ENVOY + # Found an entry with blank unique_id (prior deleted) with same ip + # If the title is still default shorthand 'Envoy' then append serial + # to differentiate multiple Envoy. Don't change the title if any other + # title is still present in the old entry. + title = f"{ENVOY} {serial}" if entry.title == ENVOY else entry.title return self.async_update_reload_and_abort( entry, title=title, unique_id=serial, reason="already_configured" ) diff --git a/homeassistant/components/enphase_envoy/quality_scale.yaml b/homeassistant/components/enphase_envoy/quality_scale.yaml index d64a62d4a48..2b9350ed944 100644 --- a/homeassistant/components/enphase_envoy/quality_scale.yaml +++ b/homeassistant/components/enphase_envoy/quality_scale.yaml @@ -18,8 +18,6 @@ rules: config-flow: status: todo comment: | - - async_step_zeroconf -> a config entry title is considered userland, - so if someone renamed their entry, it will be reverted back with the code at L146. - async_step_reaut L160: I believe that the unique is already set when starting a reauth flow - The config flow is missing data descriptions for the other fields dependency-transparency: done diff --git a/tests/components/enphase_envoy/test_config_flow.py b/tests/components/enphase_envoy/test_config_flow.py index b0b139053ce..c20e73d774b 100644 --- a/tests/components/enphase_envoy/test_config_flow.py +++ b/tests/components/enphase_envoy/test_config_flow.py @@ -631,6 +631,88 @@ async def test_zero_conf_old_blank_entry( assert entry.title == "Envoy 1234" +async def test_zero_conf_old_blank_entry_standard_title( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + mock_envoy: AsyncMock, +) -> None: + """Test re-using old blank entry was Envoy as title.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "1.1.1.1", + CONF_USERNAME: "", + CONF_PASSWORD: "", + CONF_NAME: "unknown", + }, + unique_id=None, + title="Envoy", + ) + entry.add_to_hass(hass) + # test if shorthand title Envoy gets serial appended + hass.config_entries.async_update_entry(entry, title="Envoy") + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=zeroconf.ZeroconfServiceInfo( + ip_address=ip_address("1.1.1.1"), + ip_addresses=[ip_address("1.1.1.1"), ip_address("1.1.1.2")], + hostname="mock_hostname", + name="mock_name", + port=None, + properties={"serialnum": "1234", "protovers": "7.1.2"}, + type="mock_type", + ), + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + assert entry.data[CONF_HOST] == "1.1.1.1" + assert entry.unique_id == "1234" + assert entry.title == "Envoy 1234" + + +async def test_zero_conf_old_blank_entry_user_title( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + mock_envoy: AsyncMock, +) -> None: + """Test re-using old blank entry with user title.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "1.1.1.1", + CONF_USERNAME: "", + CONF_PASSWORD: "", + CONF_NAME: "unknown", + }, + unique_id=None, + title="Envoy", + ) + entry.add_to_hass(hass) + # set user title on entry + hass.config_entries.async_update_entry(entry, title="Envoy Backyard") + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=zeroconf.ZeroconfServiceInfo( + ip_address=ip_address("1.1.1.1"), + ip_addresses=[ip_address("1.1.1.1"), ip_address("1.1.1.2")], + hostname="mock_hostname", + name="mock_name", + port=None, + properties={"serialnum": "1234", "protovers": "7.1.2"}, + type="mock_type", + ), + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + assert entry.data[CONF_HOST] == "1.1.1.1" + assert entry.unique_id == "1234" + assert entry.title == "Envoy Backyard" + + async def test_reauth( hass: HomeAssistant, config_entry: MockConfigEntry,