Disable updating ZHA coordinator path from discovery info (#112415)

* Never update the device path from config flows

* Bring coverage up to 100%

* Update tests/components/zha/test_config_flow.py

Co-authored-by: TheJulianJES <TheJulianJES@users.noreply.github.com>

---------

Co-authored-by: TheJulianJES <TheJulianJES@users.noreply.github.com>
pull/113250/head
puddly 2024-03-08 14:33:33 -05:00 committed by Franck Nijhof
parent c60f203aab
commit 095aab5f9d
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 20 additions and 55 deletions

View File

@ -491,23 +491,27 @@ class ZhaConfigFlowHandler(BaseZhaFlow, config_entries.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
@ -575,7 +579,7 @@ class ZhaConfigFlowHandler(BaseZhaFlow, config_entries.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,
)
@ -625,7 +629,7 @@ class ZhaConfigFlowHandler(BaseZhaFlow, config_entries.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,
)
@ -650,7 +654,7 @@ class ZhaConfigFlowHandler(BaseZhaFlow, config_entries.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,
)

View File

@ -293,45 +293,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:
@ -547,8 +508,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,
@ -579,7 +540,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,
}