You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

4 years ago
  1. """Discover Axis devices."""
  2. from . import MDNSDiscoverable
  3. from ..const import (
  4. ATTR_HOST, ATTR_PORT, ATTR_HOSTNAME, ATTR_PROPERTIES)
  5. class Discoverable(MDNSDiscoverable):
  6. """Add support for discovering Axis devices."""
  7. def info_from_entry(self, entry):
  8. """Return most important info from mDNS entries."""
  9. properties = {}
  10. for key, value in entry.properties.items():
  11. if isinstance(value, bytes):
  12. value = value.decode('utf-8')
  13. properties[key.decode('utf-8')] = value
  14. return {
  15. ATTR_HOST: self.ip_from_host(entry.server),
  16. ATTR_PORT: entry.port,
  17. ATTR_HOSTNAME: entry.server,
  18. ATTR_PROPERTIES: properties,
  19. }
  20. def __init__(self, nd):
  21. """Initialize the Axis discovery."""
  22. super(Discoverable, self).__init__(nd, '_axis-video._tcp.local.')
  23. def ip_from_host(self, host):
  24. """Attempt to return the ip address from an mDNS host.
  25. Return host if failed.
  26. """
  27. ips = self.netdis.mdns.zeroconf.cache.entries_with_name(host.lower())
  28. try:
  29. return repr(ips[0]) if ips else host
  30. except TypeError:
  31. return host