From bc7624b417f96b754c24f01941232208058122d9 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 13 Jan 2022 08:15:40 +0100 Subject: [PATCH] Fix UniFi remove client service if time stamps are not integers (#63998) * Only calculate total time if first and last seen are ints, else remove client * Update homeassistant/components/unifi/services.py Co-authored-by: Paulus Schoutsen * Update homeassistant/components/unifi/services.py Co-authored-by: Paulus Schoutsen --- homeassistant/components/unifi/services.py | 6 +++++- tests/components/unifi/test_services.py | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/unifi/services.py b/homeassistant/components/unifi/services.py index c1a6bdd3a53..ecf7c33c7ba 100644 --- a/homeassistant/components/unifi/services.py +++ b/homeassistant/components/unifi/services.py @@ -93,7 +93,11 @@ 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 ( + client.last_seen + and 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 8fe41d7a856..b483e789f96 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)