From 5debc8828a4ea40b8e2425368139395c83421b22 Mon Sep 17 00:00:00 2001 From: Niall Donegan Date: Sun, 10 Mar 2019 18:22:28 +0000 Subject: [PATCH] Return time based attributes as datetime in Unifi module (#21146) * Return time based data as datetime in Unifi module * Fix missing space and pylint complaints about import order. --- .../components/device_tracker/unifi.py | 9 +++++++- tests/components/device_tracker/test_unifi.py | 22 ++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/device_tracker/unifi.py b/homeassistant/components/device_tracker/unifi.py index 0c0c908d1e0..2dc5f7a4df3 100644 --- a/homeassistant/components/device_tracker/unifi.py +++ b/homeassistant/components/device_tracker/unifi.py @@ -43,6 +43,8 @@ AVAILABLE_ATTRS = [ 'uptime', 'user_id', 'usergroup_id', 'vlan' ] +TIMESTAMP_ATTRS = ['first_seen', 'last_seen', 'latest_assoc_time'] + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, vol.Optional(CONF_SITE_ID, default='default'): cv.string, @@ -149,7 +151,12 @@ class UnifiScanner(DeviceScanner): attributes = {} for variable in self._monitored_conditions: if variable in client: - attributes[variable] = client[variable] + if variable in TIMESTAMP_ATTRS: + attributes[variable] = dt_util.utc_from_timestamp( + float(client[variable]) + ) + else: + attributes[variable] = client[variable] _LOGGER.debug("Device mac %s attributes %s", device, attributes) return attributes diff --git a/tests/components/device_tracker/test_unifi.py b/tests/components/device_tracker/test_unifi.py index b3ce1e93e4c..7a791b334aa 100644 --- a/tests/components/device_tracker/test_unifi.py +++ b/tests/components/device_tracker/test_unifi.py @@ -1,13 +1,13 @@ """The tests for the Unifi WAP device tracker platform.""" from unittest import mock +from datetime import datetime, timedelta from pyunifi.controller import APIError -import homeassistant.util.dt as dt_util -from datetime import timedelta import pytest import voluptuous as vol +import homeassistant.util.dt as dt_util from homeassistant.components.device_tracker import DOMAIN, unifi as unifi from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD, CONF_PLATFORM, CONF_VERIFY_SSL, @@ -241,7 +241,8 @@ def test_monitored_conditions(): 'hostname': 'foobar', 'essid': 'barnet', 'signal': -60, - 'last_seen': dt_util.as_timestamp(dt_util.utcnow())}, + 'last_seen': dt_util.as_timestamp(dt_util.utcnow()), + 'latest_assoc_time': 946684800.0}, {'mac': '234', 'name': 'Nice Name', 'essid': 'barnet', @@ -254,9 +255,14 @@ def test_monitored_conditions(): ] ctrl.get_clients.return_value = fake_clients scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None, - ['essid', 'signal']) - assert scanner.get_extra_attributes('123') == {'essid': 'barnet', - 'signal': -60} - assert scanner.get_extra_attributes('234') == {'essid': 'barnet', - 'signal': -42} + ['essid', 'signal', 'latest_assoc_time']) + assert scanner.get_extra_attributes('123') == { + 'essid': 'barnet', + 'signal': -60, + 'latest_assoc_time': datetime(2000, 1, 1, 0, 0, tzinfo=dt_util.UTC) + } + assert scanner.get_extra_attributes('234') == { + 'essid': 'barnet', + 'signal': -42 + } assert scanner.get_extra_attributes('456') == {'essid': 'barnet'}