mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
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:
parent
517fb2e983
commit
36a663adeb
@ -605,6 +605,17 @@ class DeviceScanner(object):
|
|||||||
"""
|
"""
|
||||||
return self.hass.async_add_job(self.get_device_name, device)
|
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):
|
def load_config(path: str, hass: HomeAssistantType, consider_home: timedelta):
|
||||||
"""Load devices from YAML configuration file."""
|
"""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)
|
host_name = yield from scanner.async_get_device_name(mac)
|
||||||
seen.add(mac)
|
seen.add(mac)
|
||||||
|
|
||||||
|
try:
|
||||||
|
extra_attributes = (yield from
|
||||||
|
scanner.async_get_extra_attributes(mac))
|
||||||
|
except NotImplementedError:
|
||||||
|
extra_attributes = dict()
|
||||||
|
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'mac': mac,
|
'mac': mac,
|
||||||
'host_name': host_name,
|
'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)
|
zone_home = hass.states.get(zone.ENTITY_ID_HOME)
|
||||||
|
@ -80,6 +80,8 @@ class NmapDeviceScanner(DeviceScanner):
|
|||||||
"""Scan for new devices and return a list with found device IDs."""
|
"""Scan for new devices and return a list with found device IDs."""
|
||||||
self._update_info()
|
self._update_info()
|
||||||
|
|
||||||
|
_LOGGER.debug("Nmap last results %s", self.last_results)
|
||||||
|
|
||||||
return [device.mac for device in self.last_results]
|
return [device.mac for device in self.last_results]
|
||||||
|
|
||||||
def get_device_name(self, device):
|
def get_device_name(self, device):
|
||||||
@ -91,6 +93,15 @@ class NmapDeviceScanner(DeviceScanner):
|
|||||||
return filter_named[0]
|
return filter_named[0]
|
||||||
return None
|
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):
|
def _update_info(self):
|
||||||
"""Scan the network for devices.
|
"""Scan the network for devices.
|
||||||
|
|
||||||
|
@ -122,3 +122,9 @@ class UnifiScanner(DeviceScanner):
|
|||||||
name = client.get('name') or client.get('hostname')
|
name = client.get('name') or client.get('hostname')
|
||||||
_LOGGER.debug("Device mac %s name %s", device, name)
|
_LOGGER.debug("Device mac %s name %s", device, name)
|
||||||
return 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user