From 609118d3ac10101f531314ba6515bc1f95636901 Mon Sep 17 00:00:00 2001 From: Robert Svensson <Kane610@users.noreply.github.com> Date: Tue, 6 Aug 2019 23:55:36 +0200 Subject: [PATCH] Fix last seen not available on certain devices (#25735) --- homeassistant/components/unifi/device_tracker.py | 12 +++++++----- tests/components/unifi/test_device_tracker.py | 13 ++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index d0c2684ff53..89d3fce515e 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -301,11 +301,10 @@ class UniFiDeviceTracker(ScannerEntity): CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME ) - if ( - self.device.last_seen - and dt_util.utcnow() - - dt_util.utc_from_timestamp(float(self.device.last_seen)) - ) < detection_time: + if self.device.last_seen and ( + dt_util.utcnow() - dt_util.utc_from_timestamp(float(self.device.last_seen)) + < detection_time + ): return True return False @@ -347,6 +346,9 @@ class UniFiDeviceTracker(ScannerEntity): @property def device_state_attributes(self): """Return the device state attributes.""" + if not self.device.last_seen: + return {} + attributes = {} attributes["upgradable"] = self.device.upgradable diff --git a/tests/components/unifi/test_device_tracker.py b/tests/components/unifi/test_device_tracker.py index 9fca9d21a5b..0d8d631d8ff 100644 --- a/tests/components/unifi/test_device_tracker.py +++ b/tests/components/unifi/test_device_tracker.py @@ -73,6 +73,17 @@ DEVICE_1 = { "upgradable": False, "version": "4.0.42.10433", } +DEVICE_2 = { + "board_rev": 3, + "device_id": "mock-id", + "has_fan": True, + "ip": "10.0.1.1", + "mac": "00:00:00:00:01:01", + "model": "US16P150", + "name": "device_1", + "type": "usw", + "version": "4.0.42.10433", +} CONTROLLER_DATA = { CONF_HOST: "mock-host", @@ -167,7 +178,7 @@ async def test_no_clients(hass, mock_controller): async def test_tracked_devices(hass, mock_controller): """Test the update_items function with some clients.""" mock_controller.mock_client_responses.append([CLIENT_1, CLIENT_2, CLIENT_3]) - mock_controller.mock_device_responses.append([DEVICE_1]) + mock_controller.mock_device_responses.append([DEVICE_1, DEVICE_2]) mock_controller.unifi_config = {unifi_dt.CONF_SSID_FILTER: ["ssid"]} await setup_controller(hass, mock_controller)