2019-02-14 15:01:46 +00:00
|
|
|
"""Support for device tracking of Huawei LTE routers."""
|
2018-09-13 08:01:28 +00:00
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
|
|
import attr
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.device_tracker import PLATFORM_SCHEMA, DeviceScanner
|
2018-09-13 08:01:28 +00:00
|
|
|
from homeassistant.const import CONF_URL
|
2019-05-02 17:11:37 +00:00
|
|
|
from . import DATA_KEY, RouterData
|
2018-09-13 08:01:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Optional(CONF_URL): cv.url})
|
2018-09-13 08:01:28 +00:00
|
|
|
|
2018-12-14 13:51:13 +00:00
|
|
|
HOSTS_PATH = "wlan_host_list.Hosts"
|
|
|
|
|
2018-09-13 08:01:28 +00:00
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
|
|
|
"""Get a Huawei LTE router scanner."""
|
|
|
|
data = hass.data[DATA_KEY].get_data(config)
|
2018-12-14 13:51:13 +00:00
|
|
|
data.subscribe(HOSTS_PATH)
|
2018-09-13 08:01:28 +00:00
|
|
|
return HuaweiLteScanner(data)
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class HuaweiLteScanner(DeviceScanner):
|
|
|
|
"""Huawei LTE router scanner."""
|
|
|
|
|
|
|
|
data = attr.ib(type=RouterData)
|
|
|
|
|
|
|
|
_hosts = attr.ib(init=False, factory=dict)
|
|
|
|
|
|
|
|
def scan_devices(self) -> List[str]:
|
|
|
|
"""Scan for devices."""
|
|
|
|
self.data.update()
|
|
|
|
self._hosts = {
|
|
|
|
x["MacAddress"]: x
|
2018-12-14 13:51:13 +00:00
|
|
|
for x in self.data[HOSTS_PATH + ".Host"]
|
2018-09-13 08:01:28 +00:00
|
|
|
if x.get("MacAddress")
|
|
|
|
}
|
|
|
|
return list(self._hosts)
|
|
|
|
|
|
|
|
def get_device_name(self, device: str) -> Optional[str]:
|
|
|
|
"""Get name for a device."""
|
|
|
|
host = self._hosts.get(device)
|
|
|
|
return host.get("HostName") or None if host else None
|
|
|
|
|
|
|
|
def get_extra_attributes(self, device: str) -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Get extra attributes of a device.
|
|
|
|
|
|
|
|
Some known extra attributes that may be returned in the dict
|
|
|
|
include MacAddress (MAC address), ID (client ID), IpAddress
|
|
|
|
(IP address), AssociatedSsid (associated SSID), AssociatedTime
|
|
|
|
(associated time in seconds), and HostName (host name).
|
|
|
|
"""
|
|
|
|
return self._hosts.get(device) or {}
|