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.

148 lines
3.9 KiB

4 years ago
  1. """Combine all the different protocols into a simple interface."""
  2. import logging
  3. import os
  4. import importlib
  5. from .ssdp import SSDP
  6. from .mdns import MDNS
  7. from .gdm import GDM
  8. from .lms import LMS
  9. from .tellstick import Tellstick
  10. from .daikin import Daikin
  11. from .smartglass import XboxSmartGlass
  12. _LOGGER = logging.getLogger(__name__)
  13. class NetworkDiscovery:
  14. """Scan the network for devices.
  15. mDNS scans in a background thread.
  16. SSDP scans in the foreground.
  17. GDM scans in the foreground.
  18. LMS scans in the foreground.
  19. Tellstick scans in the foreground
  20. Xbox One scans in the foreground
  21. start: is ready to scan
  22. scan: scan the network
  23. discover: parse scanned data
  24. get_in
  25. """
  26. # pylint: disable=too-many-instance-attributes
  27. def __init__(self):
  28. """Initialize the discovery."""
  29. self.mdns = None
  30. self.ssdp = None
  31. self.gdm = None
  32. self.lms = None
  33. self.tellstick = None
  34. self.daikin = None
  35. self.xbox_smartglass = None
  36. self.is_discovering = False
  37. self.discoverables = None
  38. def scan(self):
  39. """Start and tells scanners to scan."""
  40. self.is_discovering = True
  41. self.mdns = MDNS()
  42. # Needs to be after MDNS init
  43. self._load_device_support()
  44. self.mdns.start()
  45. self.ssdp = SSDP()
  46. self.ssdp.scan()
  47. self.gdm = GDM()
  48. self.gdm.scan()
  49. self.lms = LMS()
  50. self.lms.scan()
  51. self.tellstick = Tellstick()
  52. self.tellstick.scan()
  53. self.daikin = Daikin()
  54. self.daikin.scan()
  55. self.xbox_smartglass = XboxSmartGlass()
  56. self.xbox_smartglass.scan()
  57. def stop(self):
  58. """Turn discovery off."""
  59. if not self.is_discovering:
  60. return
  61. self.mdns.stop()
  62. # Not removing SSDP because it tracks state
  63. self.mdns = None
  64. self.gdm = None
  65. self.lms = None
  66. self.tellstick = None
  67. self.daikin = None
  68. self.xbox_smartglass = None
  69. self.discoverables = None
  70. self.is_discovering = False
  71. def discover(self):
  72. """Return a list of discovered devices and services."""
  73. if not self.is_discovering:
  74. raise RuntimeError("Needs to be called after start, before stop")
  75. return [dis for dis, checker in self.discoverables.items()
  76. if checker.is_discovered()]
  77. def get_info(self, dis):
  78. """Get a list with the most important info about discovered type."""
  79. return self.discoverables[dis].get_info()
  80. def get_entries(self, dis):
  81. """Get a list with all info about a discovered type."""
  82. return self.discoverables[dis].get_entries()
  83. def _load_device_support(self):
  84. """Load the devices and services that can be discovered."""
  85. self.discoverables = {}
  86. discoverables_format = __name__.rsplit('.', 1)[0] + '.discoverables.{}'
  87. for module_name in os.listdir(os.path.join(os.path.dirname(__file__),
  88. 'discoverables')):
  89. if module_name[-3:] != '.py' or module_name == '__init__.py':
  90. continue
  91. module_name = module_name[:-3]
  92. module = importlib.import_module(
  93. discoverables_format.format(module_name))
  94. self.discoverables[module_name] = module.Discoverable(self)
  95. def print_raw_data(self):
  96. """Helper method to show what is discovered in your network."""
  97. from pprint import pprint
  98. print("Zeroconf")
  99. pprint(self.mdns.entries)
  100. print("")
  101. print("SSDP")
  102. pprint(self.ssdp.entries)
  103. print("")
  104. print("GDM")
  105. pprint(self.gdm.entries)
  106. print("")
  107. print("LMS")
  108. pprint(self.lms.entries)
  109. print("")
  110. print("Tellstick")
  111. pprint(self.tellstick.entries)
  112. print("")
  113. print("Xbox SmartGlass")
  114. pprint(self.xbox_smartglass.entries)