diff --git a/homeassistant/components/esphome/config_flow.py b/homeassistant/components/esphome/config_flow.py index ad6561ec054..1c23110644b 100644 --- a/homeassistant/components/esphome/config_flow.py +++ b/homeassistant/components/esphome/config_flow.py @@ -485,7 +485,12 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN): }, ) if self._reconfig_entry.unique_id != format_mac(self.unique_id): - if self._reconfig_entry.data[CONF_DEVICE_NAME] == self._device_name: + if ( + self._reconfig_entry.data[CONF_DEVICE_NAME] == self._device_name + and not self.hass.config_entries.async_entry_for_domain_unique_id( + self.handler, format_mac(self.unique_id) + ) + ): self._entry_with_name_conflict = self._reconfig_entry return await self.async_step_name_conflict() return self.async_abort( diff --git a/tests/components/esphome/test_config_flow.py b/tests/components/esphome/test_config_flow.py index d009fb6c0f7..01a09be8613 100644 --- a/tests/components/esphome/test_config_flow.py +++ b/tests/components/esphome/test_config_flow.py @@ -1902,6 +1902,46 @@ async def test_reconfig_attempt_to_change_mac_aborts( assert CONF_NOISE_PSK not in entry.data +@pytest.mark.usefixtures("mock_zeroconf", "mock_setup_entry") +async def test_reconfig_name_conflict_other_entry_for_mac( + hass: HomeAssistant, mock_client: APIClient +) -> None: + """Test reconfig name conflict when there is already another entry for the mac.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "127.0.0.1", + CONF_PORT: 6053, + CONF_PASSWORD: "", + CONF_DEVICE_NAME: "test", + }, + unique_id="11:22:33:44:55:aa", + ) + entry.add_to_hass(hass) + entry2 = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "127.0.0.2", + CONF_PORT: 6053, + CONF_PASSWORD: "", + CONF_DEVICE_NAME: "test4", + }, + unique_id="11:22:33:44:55:bb", + ) + entry2.add_to_hass(hass) + result = await entry.start_reconfigure_flow(hass) + + mock_client.device_info.return_value = DeviceInfo( + uses_password=False, name="test", mac_address="11:22:33:44:55:bb" + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={CONF_HOST: "127.0.0.2", CONF_PORT: 6053} + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_unique_id_changed" + + @pytest.mark.usefixtures("mock_zeroconf", "mock_setup_entry") async def test_reconfig_name_conflict_migrate( hass: HomeAssistant, mock_client: APIClient