Smarthome Functionen
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

__init__.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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
  84. class warning(dict):
  85. TYPE_BATTERY_LOW = 1
  86. TYPE_OVERTEMPERATURE = 2
  87. #
  88. KEY_ID = 'id'
  89. KEY_TYPE = 'type'
  90. KEY_TEXT = 'text'
  91. KEY_TM = 'tm'
  92. def __init__(self, identification, type, text, args):
  93. super().__init__({
  94. self.KEY_ID: identification,
  95. self.KEY_TYPE: type,
  96. self.KEY_TEXT: text % args,
  97. self.KEY_TM: time.localtime(),
  98. })
  99. def __str__(self):
  100. return time.asctime(self.get(self.KEY_TM)) + ": " + self[self.KEY_TEXT] + " - " + self[self.KEY_ID]