Smarthome Functionen
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

__init__.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. devices (DEVICES)
  6. =================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. This Module supports smarthome devices
  11. **Submodules:**
  12. * :mod:`shelly`
  13. * :mod:`silvercrest_powerplug`
  14. **Unittest:**
  15. See also the :download:`unittest <devices/_testresults_/unittest.pdf>` documentation.
  16. **Module Documentation:**
  17. """
  18. try:
  19. from config import APP_NAME as ROOT_LOGGER_NAME
  20. except ImportError:
  21. ROOT_LOGGER_NAME = 'root'
  22. from devices.shelly import shelly as shelly_sw1
  23. from devices.tradfri import tradfri_light as tradfri_sw
  24. from devices.tradfri import tradfri_light as tradfri_sw_br
  25. from devices.tradfri import tradfri_light as tradfri_sw_br_ct
  26. from devices.tradfri import tradfri_button as tradfri_button
  27. from devices.tradfri import tradfri_light as livarno_sw_br_ct
  28. from devices.brennenstuhl import brennenstuhl_heatingvalve
  29. from devices.silvercrest import silvercrest_powerplug
  30. from devices.silvercrest import silvercrest_motion_sensor
  31. from devices.mydevices import powerplug as my_powerplug
  32. from devices.mydevices import audio_status
  33. from devices.mydevices import remote
  34. class group(object):
  35. def __init__(self, *args):
  36. super().__init__()
  37. self._members = args
  38. self._iter_counter = 0
  39. #
  40. self.methods = []
  41. self.variables = []
  42. for name in [m for m in args[0].__class__.__dict__.keys()]:
  43. if not name.startswith('_') and callable(getattr(args[0], name)): # add all public callable attributes to the list
  44. self.methods.append(name)
  45. if not name.startswith('_') and not callable(getattr(args[0], name)): # add all public callable attributes to the list
  46. self.variables.append(name)
  47. #
  48. for member in self:
  49. methods = [m for m in member.__class__.__dict__.keys() if not m.startswith(
  50. '_') if not m.startswith('_') and callable(getattr(args[0], m))]
  51. if self.methods != methods:
  52. raise ValueError("All given instances needs to have same methods:", self.methods, methods)
  53. #
  54. variables = [v for v in member.__class__.__dict__.keys() if not v.startswith(
  55. '_') if not v.startswith('_') and not callable(getattr(args[0], v))]
  56. if self.variables != variables:
  57. raise ValueError("All given instances needs to have same variables:", self.variables, variables)
  58. def __iter__(self):
  59. return self
  60. def __next__(self):
  61. if self._iter_counter < len(self):
  62. self._iter_counter += 1
  63. return self._members[self._iter_counter - 1]
  64. self._iter_counter = 0
  65. raise StopIteration
  66. def __getitem__(self, i):
  67. return self._members[i]
  68. def __len__(self):
  69. return len(self._members)
  70. def __getattribute__(self, name):
  71. def group_execution(*args, **kwargs):
  72. for member in self[:]:
  73. m = getattr(member, name)
  74. m(*args, **kwargs)
  75. try:
  76. rv = super().__getattribute__(name)
  77. except AttributeError:
  78. if callable(getattr(self[0], name)):
  79. return group_execution
  80. else:
  81. return getattr(self[0], name)
  82. else:
  83. return rv