Freebox thin client
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.

60 lines
1.7 KiB

  1. """
  2. Search Freebox through MDNS
  3. """
  4. import time
  5. import logging
  6. from zeroconf import ServiceBrowser, Zeroconf
  7. from . import api
  8. class FbxMDNS():
  9. """
  10. Search for a freebox using MDNS
  11. """
  12. class MyListener:
  13. """
  14. Callback class to get information of the available MDNS service
  15. """
  16. def __init__(self):
  17. self._info = None
  18. def add_service(self, zeroconf, stype, name):
  19. info = zeroconf.get_service_info(stype, name)
  20. self._info = info
  21. @property
  22. def svc_info(self):
  23. return self._info
  24. def __init__(self, timeout=1):
  25. self.timeout = timeout
  26. self.log = logging.getLogger("pyfbx.mdns")
  27. def search(self, svc_name=api._DISC_MDNS_NAME, timeout=1):
  28. self.log.debug("Searching with MDNS")
  29. zeroconf = Zeroconf()
  30. self._listener = FbxMDNS.MyListener()
  31. browser = ServiceBrowser(zeroconf, svc_name, self._listener)
  32. time.sleep(timeout)
  33. self.log.debug("Closing MDNS")
  34. browser.cancel() # slow but required to
  35. zeroconf.close() # stop listener thread
  36. if self.svc_info:
  37. prop = self.svc_prop
  38. base = "%sv%s" % (prop['api_base_url'], prop['api_version'][0])
  39. if int(prop['https_available']):
  40. return f"https://{prop['api_domain']}:{prop['https_port']}{base}"
  41. else:
  42. return f"http://{prop['api_domain']}{base}"
  43. @property
  44. def svc_info(self):
  45. return self._listener.svc_info
  46. @property
  47. def svc_prop(self):
  48. return {k.decode(): v.decode()
  49. for k, v in self._listener.svc_info.properties.items()}