From da355438aa17f4e0dfe0d9ba8e5f3d25d099574b Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 28 Jan 2022 17:33:31 +0100 Subject: [PATCH] Reconnect client service tried to connect even if device didn't exist (#65082) --- homeassistant/components/unifi/services.py | 3 ++ tests/components/unifi/test_services.py | 38 +++++++++------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/unifi/services.py b/homeassistant/components/unifi/services.py index c0498395a10..fcde48528b3 100644 --- a/homeassistant/components/unifi/services.py +++ b/homeassistant/components/unifi/services.py @@ -57,6 +57,9 @@ async def async_reconnect_client(hass, data) -> None: device_registry = dr.async_get(hass) device_entry = device_registry.async_get(data[ATTR_DEVICE_ID]) + if device_entry is None: + return + mac = "" for connection in device_entry.connections: if connection[0] == CONNECTION_NETWORK_MAC: diff --git a/tests/components/unifi/test_services.py b/tests/components/unifi/test_services.py index b483e789f96..27e4ddea930 100644 --- a/tests/components/unifi/test_services.py +++ b/tests/components/unifi/test_services.py @@ -77,15 +77,26 @@ async def test_reconnect_client(hass, aioclient_mock): assert aioclient_mock.call_count == 1 +async def test_reconnect_non_existant_device(hass, aioclient_mock): + """Verify no call is made if device does not exist.""" + await setup_unifi_integration(hass, aioclient_mock) + + aioclient_mock.clear_requests() + + await hass.services.async_call( + UNIFI_DOMAIN, + SERVICE_RECONNECT_CLIENT, + service_data={ATTR_DEVICE_ID: "device_entry.id"}, + blocking=True, + ) + assert aioclient_mock.call_count == 0 + + async def test_reconnect_device_without_mac(hass, aioclient_mock): """Verify no call is made if device does not have a known mac.""" config_entry = await setup_unifi_integration(hass, aioclient_mock) - controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id] aioclient_mock.clear_requests() - aioclient_mock.post( - f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr", - ) device_registry = await hass.helpers.device_registry.async_get_registry() device_entry = device_registry.async_get_or_create( @@ -139,12 +150,8 @@ async def test_reconnect_client_controller_unavailable(hass, aioclient_mock): async def test_reconnect_client_unknown_mac(hass, aioclient_mock): """Verify no call is made if trying to reconnect a mac unknown to controller.""" config_entry = await setup_unifi_integration(hass, aioclient_mock) - controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id] aioclient_mock.clear_requests() - aioclient_mock.post( - f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr", - ) device_registry = await hass.helpers.device_registry.async_get_registry() device_entry = device_registry.async_get_or_create( @@ -172,12 +179,8 @@ async def test_reconnect_wired_client(hass, aioclient_mock): config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_response=clients ) - controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id] aioclient_mock.clear_requests() - aioclient_mock.post( - f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr", - ) device_registry = await hass.helpers.device_registry.async_get_registry() device_entry = device_registry.async_get_or_create( @@ -264,9 +267,6 @@ async def test_remove_clients_controller_unavailable(hass, aioclient_mock): controller.available = False aioclient_mock.clear_requests() - aioclient_mock.post( - f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr", - ) await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True) assert aioclient_mock.call_count == 0 @@ -281,15 +281,9 @@ async def test_remove_clients_no_call_on_empty_list(hass, aioclient_mock): "mac": "00:00:00:00:00:01", } ] - config_entry = await setup_unifi_integration( - hass, aioclient_mock, clients_all_response=clients - ) - controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id] + await setup_unifi_integration(hass, aioclient_mock, clients_all_response=clients) aioclient_mock.clear_requests() - aioclient_mock.post( - f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr", - ) await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True) assert aioclient_mock.call_count == 0