From e81ca64aa4923c102f181b489ccc002eefcc8863 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Feb 2022 09:37:19 -1000 Subject: [PATCH] Prevent the wrong WiZ device from being used when the IP is a different device (#67250) --- homeassistant/components/wiz/__init__.py | 9 +++++++++ tests/components/wiz/test_init.py | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/homeassistant/components/wiz/__init__.py b/homeassistant/components/wiz/__init__.py index 7bea86d323c..d739c571c8b 100644 --- a/homeassistant/components/wiz/__init__.py +++ b/homeassistant/components/wiz/__init__.py @@ -60,6 +60,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await bulb.async_close() raise ConfigEntryNotReady(f"{ip_address}: {err}") from err + if bulb.mac != entry.unique_id: + # The ip address of the bulb has changed and its likely offline + # and another WiZ device has taken the IP. Avoid setting up + # since its the wrong device. As soon as the device comes back + # online the ip will get updated and setup will proceed. + raise ConfigEntryNotReady( + "Found bulb {bulb.mac} at {ip_address}, expected {entry.unique_id}" + ) + async def _async_update() -> None: """Update the WiZ device.""" try: diff --git a/tests/components/wiz/test_init.py b/tests/components/wiz/test_init.py index fb21e930efd..58afb5c944a 100644 --- a/tests/components/wiz/test_init.py +++ b/tests/components/wiz/test_init.py @@ -50,3 +50,11 @@ async def test_cleanup_on_failed_first_update(hass: HomeAssistant) -> None: _, entry = await async_setup_integration(hass, wizlight=bulb) assert entry.state == config_entries.ConfigEntryState.SETUP_RETRY bulb.async_close.assert_called_once() + + +async def test_wrong_device_now_has_our_ip(hass: HomeAssistant) -> None: + """Test setup is retried when the wrong device is found.""" + bulb = _mocked_wizlight(None, None, FAKE_SOCKET) + bulb.mac = "dddddddddddd" + _, entry = await async_setup_integration(hass, wizlight=bulb) + assert entry.state == config_entries.ConfigEntryState.SETUP_RETRY