123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- devices (DEVICES)
- =================
-
- **Author:**
-
- * Dirk Alders <sudo-dirk@mount-mockery.de>
-
- **Description:**
-
- This Module supports smarthome devices
-
- **Submodules:**
-
- * :mod:`shelly`
- * :mod:`silvercrest_powerplug`
-
- **Unittest:**
-
- See also the :download:`unittest <devices/_testresults_/unittest.pdf>` documentation.
-
- **Module Documentation:**
-
- """
-
- try:
- from config import APP_NAME as ROOT_LOGGER_NAME
- except ImportError:
- ROOT_LOGGER_NAME = 'root'
-
- from devices.shelly import shelly as shelly_sw1
- from devices.tradfri import tradfri_light as tradfri_sw
- from devices.tradfri import tradfri_light as tradfri_sw_br
- from devices.tradfri import tradfri_light as tradfri_sw_br_ct
- from devices.tradfri import tradfri_button as tradfri_button
- from devices.tradfri import tradfri_light as livarno_sw_br_ct
- from devices.brennenstuhl import brennenstuhl_heatingvalve
- from devices.silvercrest import silvercrest_powerplug
- from devices.silvercrest import silvercrest_motion_sensor
- from devices.mydevices import powerplug as my_powerplug
- from devices.mydevices import audio_status
- from devices.mydevices import remote
-
-
- class group(object):
- def __init__(self, *args):
- super().__init__()
- self._members = args
- self._iter_counter = 0
- #
- self.methods = []
- self.variables = []
- for name in [m for m in args[0].__class__.__dict__.keys()]:
- if not name.startswith('_') and callable(getattr(args[0], name)): # add all public callable attributes to the list
- self.methods.append(name)
- if not name.startswith('_') and not callable(getattr(args[0], name)): # add all public callable attributes to the list
- self.variables.append(name)
- #
- for member in self:
- methods = [m for m in member.__class__.__dict__.keys() if not m.startswith(
- '_') if not m.startswith('_') and callable(getattr(args[0], m))]
- if self.methods != methods:
- raise ValueError("All given instances needs to have same methods:", self.methods, methods)
- #
- variables = [v for v in member.__class__.__dict__.keys() if not v.startswith(
- '_') if not v.startswith('_') and not callable(getattr(args[0], v))]
- if self.variables != variables:
- raise ValueError("All given instances needs to have same variables:", self.variables, variables)
-
- def __iter__(self):
- return self
-
- def __next__(self):
- if self._iter_counter < len(self):
- self._iter_counter += 1
- return self._members[self._iter_counter - 1]
- self._iter_counter = 0
- raise StopIteration
-
- def __getitem__(self, i):
- return self._members[i]
-
- def __len__(self):
- return len(self._members)
-
- def __getattribute__(self, name):
- def group_execution(*args, **kwargs):
- for member in self[:]:
- m = getattr(member, name)
- m(*args, **kwargs)
- try:
- rv = super().__getattribute__(name)
- except AttributeError:
- if callable(getattr(self[0], name)):
- return group_execution
- else:
- return getattr(self[0], name)
- else:
- return rv
|