Add extra attributes for device scanner, Nmap and Unifi (IP, SSID, etc.) (#13673)

* Start of development

* Add extra attributes from unifi scanner

* Store IP of the device in the state attributes with nmap

* Allow not defining get_extra_attributes method in derived classes
This commit is contained in:
stephanerosi 2018-04-16 08:20:58 +02:00 committed by Fabian Affolter
parent 517fb2e983
commit 36a663adeb
3 changed files with 39 additions and 1 deletions

View File

@ -605,6 +605,17 @@ class DeviceScanner(object):
"""
return self.hass.async_add_job(self.get_device_name, device)
def get_extra_attributes(self, device: str) -> dict:
"""Get the extra attributes of a device."""
raise NotImplementedError()
def async_get_extra_attributes(self, device: str) -> Any:
"""Get the extra attributes of a device.
This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(self.get_extra_attributes, device)
def load_config(path: str, hass: HomeAssistantType, consider_home: timedelta):
"""Load devices from YAML configuration file."""
@ -690,10 +701,20 @@ def async_setup_scanner_platform(hass: HomeAssistantType, config: ConfigType,
host_name = yield from scanner.async_get_device_name(mac)
seen.add(mac)
try:
extra_attributes = (yield from
scanner.async_get_extra_attributes(mac))
except NotImplementedError:
extra_attributes = dict()
kwargs = {
'mac': mac,
'host_name': host_name,
'source_type': SOURCE_TYPE_ROUTER
'source_type': SOURCE_TYPE_ROUTER,
'attributes': {
'scanner': scanner.__class__.__name__,
**extra_attributes
}
}
zone_home = hass.states.get(zone.ENTITY_ID_HOME)

View File

@ -80,6 +80,8 @@ class NmapDeviceScanner(DeviceScanner):
"""Scan for new devices and return a list with found device IDs."""
self._update_info()
_LOGGER.debug("Nmap last results %s", self.last_results)
return [device.mac for device in self.last_results]
def get_device_name(self, device):
@ -91,6 +93,15 @@ class NmapDeviceScanner(DeviceScanner):
return filter_named[0]
return None
def get_extra_attributes(self, device):
"""Return the IP pf the given device."""
filter_ip = [result.ip for result in self.last_results
if result.mac == device]
if filter_ip:
return {'ip': filter_ip[0]}
return None
def _update_info(self):
"""Scan the network for devices.

View File

@ -122,3 +122,9 @@ class UnifiScanner(DeviceScanner):
name = client.get('name') or client.get('hostname')
_LOGGER.debug("Device mac %s name %s", device, name)
return name
def get_extra_attributes(self, device):
"""Return the extra attributes of the device."""
client = self._clients.get(device, {})
_LOGGER.debug("Device mac %s attributes %s", device, client)
return client