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.
pull/21913/head
Niall Donegan 2019-03-10 18:22:28 +00:00 committed by Martin Hjelmare
parent c888e65f11
commit 5debc8828a
2 changed files with 22 additions and 9 deletions

View File

@ -43,6 +43,8 @@ AVAILABLE_ATTRS = [
'uptime', 'user_id', 'usergroup_id', 'vlan' 'uptime', 'user_id', 'usergroup_id', 'vlan'
] ]
TIMESTAMP_ATTRS = ['first_seen', 'last_seen', 'latest_assoc_time']
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_SITE_ID, default='default'): cv.string, vol.Optional(CONF_SITE_ID, default='default'): cv.string,
@ -149,6 +151,11 @@ class UnifiScanner(DeviceScanner):
attributes = {} attributes = {}
for variable in self._monitored_conditions: for variable in self._monitored_conditions:
if variable in client: if variable in client:
if variable in TIMESTAMP_ATTRS:
attributes[variable] = dt_util.utc_from_timestamp(
float(client[variable])
)
else:
attributes[variable] = client[variable] attributes[variable] = client[variable]
_LOGGER.debug("Device mac %s attributes %s", device, attributes) _LOGGER.debug("Device mac %s attributes %s", device, attributes)

View File

@ -1,13 +1,13 @@
"""The tests for the Unifi WAP device tracker platform.""" """The tests for the Unifi WAP device tracker platform."""
from unittest import mock from unittest import mock
from datetime import datetime, timedelta
from pyunifi.controller import APIError from pyunifi.controller import APIError
import homeassistant.util.dt as dt_util
from datetime import timedelta
import pytest import pytest
import voluptuous as vol import voluptuous as vol
import homeassistant.util.dt as dt_util
from homeassistant.components.device_tracker import DOMAIN, unifi as unifi from homeassistant.components.device_tracker import DOMAIN, unifi as unifi
from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD, from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
CONF_PLATFORM, CONF_VERIFY_SSL, CONF_PLATFORM, CONF_VERIFY_SSL,
@ -241,7 +241,8 @@ def test_monitored_conditions():
'hostname': 'foobar', 'hostname': 'foobar',
'essid': 'barnet', 'essid': 'barnet',
'signal': -60, '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', {'mac': '234',
'name': 'Nice Name', 'name': 'Nice Name',
'essid': 'barnet', 'essid': 'barnet',
@ -254,9 +255,14 @@ def test_monitored_conditions():
] ]
ctrl.get_clients.return_value = fake_clients ctrl.get_clients.return_value = fake_clients
scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None, scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None,
['essid', 'signal']) ['essid', 'signal', 'latest_assoc_time'])
assert scanner.get_extra_attributes('123') == {'essid': 'barnet', assert scanner.get_extra_attributes('123') == {
'signal': -60} 'essid': 'barnet',
assert scanner.get_extra_attributes('234') == {'essid': 'barnet', 'signal': -60,
'signal': -42} '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'} assert scanner.get_extra_attributes('456') == {'essid': 'barnet'}