From a5d334bbf7b708b990a314509da4c667c6462e8c Mon Sep 17 00:00:00 2001 From: Jens Nistler Date: Thu, 27 Feb 2020 20:50:34 +0100 Subject: [PATCH] Mark clients away if they have never been seen. (#32222) --- .../components/unifi/device_tracker.py | 5 +++++ tests/components/unifi/test_device_tracker.py | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index 5dd5f0c83ae..52370fb0e3d 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -200,6 +200,11 @@ class UniFiClientTracker(UniFiClient, ScannerEntity): else: self.wired_bug = None + + # A client that has never been seen cannot be connected. + if self.client.last_seen is None: + return False + since_last_seen = dt_util.utcnow() - dt_util.utc_from_timestamp( float(self.client.last_seen) ) diff --git a/tests/components/unifi/test_device_tracker.py b/tests/components/unifi/test_device_tracker.py index 608e72b483a..cbef7c31922 100644 --- a/tests/components/unifi/test_device_tracker.py +++ b/tests/components/unifi/test_device_tracker.py @@ -54,6 +54,14 @@ CLIENT_4 = { "last_seen": 1562600145, "mac": "00:00:00:00:00:04", } +CLIENT_5 = { + "essid": "ssid", + "hostname": "client_5", + "ip": "10.0.0.5", + "is_wired": True, + "last_seen": None, + "mac": "00:00:00:00:00:05", +} DEVICE_1 = { "board_rev": 3, @@ -111,11 +119,11 @@ async def test_tracked_devices(hass): controller = await setup_unifi_integration( hass, options={CONF_SSID_FILTER: ["ssid"]}, - clients_response=[CLIENT_1, CLIENT_2, CLIENT_3, client_4_copy], + clients_response=[CLIENT_1, CLIENT_2, CLIENT_3, CLIENT_5, client_4_copy], devices_response=[DEVICE_1, DEVICE_2], known_wireless_clients=(CLIENT_4["mac"],), ) - assert len(hass.states.async_all()) == 6 + assert len(hass.states.async_all()) == 7 client_1 = hass.states.get("device_tracker.client_1") assert client_1 is not None @@ -134,6 +142,11 @@ async def test_tracked_devices(hass): assert client_4 is not None assert client_4.state == "not_home" + # A client that has never been seen should be marked away. + client_5 = hass.states.get("device_tracker.client_5") + assert client_5 is not None + assert client_5.state == "not_home" + device_1 = hass.states.get("device_tracker.device_1") assert device_1 is not None assert device_1.state == "not_home"