From ba5c8d22c07fba34e70d84df5c9c6fcc3d77c6cd Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 12 Jan 2022 20:44:21 +0100 Subject: [PATCH 1/3] Only calculate total time if first and last seen are ints, else remove client --- homeassistant/components/unifi/services.py | 5 ++++- tests/components/unifi/test_services.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/unifi/services.py b/homeassistant/components/unifi/services.py index c1a6bdd3a53bb2..a0d5fb4bce8be8 100644 --- a/homeassistant/components/unifi/services.py +++ b/homeassistant/components/unifi/services.py @@ -93,7 +93,10 @@ async def async_remove_clients(hass, data) -> None: for client in controller.api.clients_all.values(): - if client.last_seen - client.first_seen > 900: + if ( + all({client.last_seen, client.first_seen}) + and client.last_seen - client.first_seen > 900 + ): continue if any({client.fixed_ip, client.hostname, client.name}): diff --git a/tests/components/unifi/test_services.py b/tests/components/unifi/test_services.py index 8fe41d7a856f4e..b483e789f96e85 100644 --- a/tests/components/unifi/test_services.py +++ b/tests/components/unifi/test_services.py @@ -197,6 +197,9 @@ async def test_reconnect_wired_client(hass, aioclient_mock): async def test_remove_clients(hass, aioclient_mock): """Verify removing different variations of clients work.""" clients = [ + { + "mac": "00:00:00:00:00:00", + }, { "first_seen": 100, "last_seen": 500, @@ -239,7 +242,7 @@ async def test_remove_clients(hass, aioclient_mock): await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True) assert aioclient_mock.mock_calls[0][2] == { "cmd": "forget-sta", - "macs": ["00:00:00:00:00:01"], + "macs": ["00:00:00:00:00:00", "00:00:00:00:00:01"], } assert await hass.config_entries.async_unload(config_entry.entry_id) From 2004e65bfee3441fe3a42ec4f5b190742091c0e9 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 13 Jan 2022 07:46:19 +0100 Subject: [PATCH 2/3] Update homeassistant/components/unifi/services.py Co-authored-by: Paulus Schoutsen --- homeassistant/components/unifi/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/unifi/services.py b/homeassistant/components/unifi/services.py index a0d5fb4bce8be8..3eeae1d7a5e200 100644 --- a/homeassistant/components/unifi/services.py +++ b/homeassistant/components/unifi/services.py @@ -94,7 +94,7 @@ async def async_remove_clients(hass, data) -> None: for client in controller.api.clients_all.values(): if ( - all({client.last_seen, client.first_seen}) + client.last_seen and client.first_seen and client.last_seen - client.first_seen > 900 ): continue From 5384fb650def0e6f76e5d878fc710a81c7e83510 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 13 Jan 2022 07:59:16 +0100 Subject: [PATCH 3/3] Update homeassistant/components/unifi/services.py --- homeassistant/components/unifi/services.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/unifi/services.py b/homeassistant/components/unifi/services.py index 3eeae1d7a5e200..ecf7c33c7bac4f 100644 --- a/homeassistant/components/unifi/services.py +++ b/homeassistant/components/unifi/services.py @@ -94,7 +94,8 @@ async def async_remove_clients(hass, data) -> None: for client in controller.api.clients_all.values(): if ( - client.last_seen and client.first_seen + client.last_seen + and client.first_seen and client.last_seen - client.first_seen > 900 ): continue