Smarthome Functionen
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.

__init__.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.shelly import shelly_rpc as shelly_pro3
  24. from devices.tradfri import tradfri_light as tradfri_sw
  25. from devices.tradfri import tradfri_light as tradfri_sw_br
  26. from devices.tradfri import tradfri_light as tradfri_sw_br_ct
  27. from devices.tradfri import tradfri_button as tradfri_button
  28. from devices.tradfri import tradfri_light as livarno_sw_br_ct
  29. from devices.brennenstuhl import brennenstuhl_heatingvalve
  30. from devices.silvercrest import silvercrest_powerplug
  31. from devices.silvercrest import silvercrest_motion_sensor
  32. from devices.mydevices import powerplug as my_powerplug
  33. from devices.mydevices import audio_status
  34. from devices.mydevices import remote
  35. my_ambient = None
  36. class group(object):
  37. def __init__(self, *args):
  38. super().__init__()
  39. self._members = args
  40. self._iter_counter = 0
  41. #
  42. self.methods = []
  43. self.variables = []
  44. for name in [m for m in args[0].__class__.__dict__.keys()]:
  45. if not name.startswith('_') and callable(getattr(args[0], name)): # add all public callable attributes to the list
  46. self.methods.append(name)
  47. if not name.startswith('_') and not callable(getattr(args[0], name)): # add all public callable attributes to the list
  48. self.variables.append(name)
  49. #
  50. for member in self:
  51. methods = [m for m in member.__class__.__dict__.keys() if not m.startswith(
  52. '_') if not m.startswith('_') and callable(getattr(args[0], m))]
  53. if self.methods != methods:
  54. raise ValueError("All given instances needs to have same methods:", self.methods, methods)
  55. #
  56. variables = [v for v in member.__class__.__dict__.keys() if not v.startswith(
  57. '_') if not v.startswith('_') and not callable(getattr(args[0], v))]
  58. if self.variables != variables:
  59. raise ValueError("All given instances needs to have same variables:", self.variables, variables)
  60. def __iter__(self):
  61. return self
  62. def __next__(self):
  63. if self._iter_counter < len(self):
  64. self._iter_counter += 1
  65. return self._members[self._iter_counter - 1]
  66. self._iter_counter = 0
  67. raise StopIteration
  68. def __getitem__(self, i):
  69. return self._members[i]
  70. def __len__(self):
  71. return len(self._members)
  72. def __getattribute__(self, name):
  73. def group_execution(*args, **kwargs):
  74. for member in self[:]:
  75. m = getattr(member, name)
  76. m(*args, **kwargs)
  77. try:
  78. rv = super().__getattribute__(name)
  79. except AttributeError:
  80. if callable(getattr(self[0], name)):
  81. return group_execution
  82. else:
  83. return getattr(self[0], name)
  84. else:
  85. return rv