diff --git a/homeassistant/components/zha/config_flow.py b/homeassistant/components/zha/config_flow.py index 7ecf3357334..42febb3b36d 100644 --- a/homeassistant/components/zha/config_flow.py +++ b/homeassistant/components/zha/config_flow.py @@ -501,23 +501,27 @@ class ZhaConfigFlowHandler(BaseZhaFlow, ConfigFlow, domain=DOMAIN): VERSION = 4 - async def _set_unique_id_or_update_path( + async def _set_unique_id_and_update_ignored_flow( self, unique_id: str, device_path: str ) -> None: - """Set the flow's unique ID and update the device path if it isn't unique.""" + """Set the flow's unique ID and update the device path in an ignored flow.""" current_entry = await self.async_set_unique_id(unique_id) if not current_entry: return - self._abort_if_unique_id_configured( - updates={ - CONF_DEVICE: { - **current_entry.data.get(CONF_DEVICE, {}), - CONF_DEVICE_PATH: device_path, - }, - } - ) + if current_entry.source != SOURCE_IGNORE: + self._abort_if_unique_id_configured() + else: + # Only update the current entry if it is an ignored discovery + self._abort_if_unique_id_configured( + updates={ + CONF_DEVICE: { + **current_entry.data.get(CONF_DEVICE, {}), + CONF_DEVICE_PATH: device_path, + }, + } + ) @staticmethod @callback @@ -587,7 +591,7 @@ class ZhaConfigFlowHandler(BaseZhaFlow, ConfigFlow, domain=DOMAIN): description = discovery_info.description dev_path = discovery_info.device - await self._set_unique_id_or_update_path( + await self._set_unique_id_and_update_ignored_flow( unique_id=f"{vid}:{pid}_{serial_number}_{manufacturer}_{description}", device_path=dev_path, ) @@ -637,7 +641,7 @@ class ZhaConfigFlowHandler(BaseZhaFlow, ConfigFlow, domain=DOMAIN): node_name = local_name.removesuffix(".local") device_path = f"socket://{discovery_info.host}:{port}" - await self._set_unique_id_or_update_path( + await self._set_unique_id_and_update_ignored_flow( unique_id=node_name, device_path=device_path, ) @@ -662,7 +666,7 @@ class ZhaConfigFlowHandler(BaseZhaFlow, ConfigFlow, domain=DOMAIN): device_settings = discovery_data["port"] device_path = device_settings[CONF_DEVICE_PATH] - await self._set_unique_id_or_update_path( + await self._set_unique_id_and_update_ignored_flow( unique_id=f"{name}_{radio_type.name}_{device_path}", device_path=device_path, ) diff --git a/tests/components/zha/test_config_flow.py b/tests/components/zha/test_config_flow.py index 57833f0e67e..9ec3cc9f497 100644 --- a/tests/components/zha/test_config_flow.py +++ b/tests/components/zha/test_config_flow.py @@ -294,45 +294,6 @@ async def test_efr32_via_zeroconf(hass: HomeAssistant) -> None: } -@patch("homeassistant.components.zha.async_setup_entry", AsyncMock(return_value=True)) -@patch(f"zigpy_znp.{PROBE_FUNCTION_PATH}", AsyncMock(return_value=True)) -async def test_discovery_via_zeroconf_ip_change(hass: HomeAssistant) -> None: - """Test zeroconf flow -- radio detected.""" - entry = MockConfigEntry( - domain=DOMAIN, - unique_id="tube_zb_gw_cc2652p2_poe", - data={ - CONF_DEVICE: { - CONF_DEVICE_PATH: "socket://192.168.1.5:6638", - CONF_BAUDRATE: 115200, - CONF_FLOW_CONTROL: None, - } - }, - ) - entry.add_to_hass(hass) - - service_info = zeroconf.ZeroconfServiceInfo( - ip_address=ip_address("192.168.1.22"), - ip_addresses=[ip_address("192.168.1.22")], - hostname="tube_zb_gw_cc2652p2_poe.local.", - name="mock_name", - port=6053, - properties={"address": "tube_zb_gw_cc2652p2_poe.local"}, - type="mock_type", - ) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_ZEROCONF}, data=service_info - ) - - assert result["type"] == FlowResultType.ABORT - assert result["reason"] == "already_configured" - assert entry.data[CONF_DEVICE] == { - CONF_DEVICE_PATH: "socket://192.168.1.22:6638", - CONF_BAUDRATE: 115200, - CONF_FLOW_CONTROL: None, - } - - @patch("homeassistant.components.zha.async_setup_entry", AsyncMock(return_value=True)) @patch(f"zigpy_znp.{PROBE_FUNCTION_PATH}", AsyncMock(return_value=True)) async def test_discovery_via_zeroconf_ip_change_ignored(hass: HomeAssistant) -> None: @@ -548,8 +509,8 @@ async def test_discovery_via_usb_already_setup(hass: HomeAssistant) -> None: @patch("homeassistant.components.zha.async_setup_entry", AsyncMock(return_value=True)) -async def test_discovery_via_usb_path_changes(hass: HomeAssistant) -> None: - """Test usb flow already setup and the path changes.""" +async def test_discovery_via_usb_path_does_not_change(hass: HomeAssistant) -> None: + """Test usb flow already set up and the path does not change.""" entry = MockConfigEntry( domain=DOMAIN, @@ -580,7 +541,7 @@ async def test_discovery_via_usb_path_changes(hass: HomeAssistant) -> None: assert result["type"] == FlowResultType.ABORT assert result["reason"] == "already_configured" assert entry.data[CONF_DEVICE] == { - CONF_DEVICE_PATH: "/dev/ttyZIGBEE", + CONF_DEVICE_PATH: "/dev/ttyUSB1", CONF_BAUDRATE: 115200, CONF_FLOW_CONTROL: None, }