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.

33 lines
1.2 KiB

4 years ago
  1. """Discover Xiaomi Mi Home (aka Lumi) Gateways."""
  2. from . import MDNSDiscoverable
  3. from ..const import ATTR_MAC_ADDRESS, ATTR_PROPERTIES
  4. class Discoverable(MDNSDiscoverable):
  5. """Add support for discovering Xiaomi Gateway"""
  6. def __init__(self, nd):
  7. """Initialize the discovery."""
  8. super(Discoverable, self).__init__(nd, '_miio._udp.local.')
  9. def info_from_entry(self, entry):
  10. """Return most important info from mDNS entries."""
  11. info = super().info_from_entry(entry)
  12. # Workaround of misparsing of mDNS properties. It's unclear
  13. # whether it's bug in zeroconf module or in the Gateway, but
  14. # returned properties look like:
  15. # {b'poch': b'0:mac=286c07aaaaaa\x00'} instead of expected:
  16. # {b'epoch': b'0', b'mac': '286c07aaaaaa'}
  17. if "poch" in info[ATTR_PROPERTIES]:
  18. misparsed = info[ATTR_PROPERTIES]["poch"]
  19. misparsed = misparsed.rstrip("\0")
  20. for val in misparsed.split(":"):
  21. if val.startswith("mac="):
  22. info[ATTR_MAC_ADDRESS] = val[len("mac="):]
  23. return info
  24. def get_entries(self):
  25. """Return Xiaomi Gateway devices."""
  26. return self.find_by_device_name('lumi-gateway-')