from simulation.devices import tradfri_light from simulation.devices import silvercrest_powerplug from simulation.devices import shelly as shelly_sw1 from simulation.devices import brennenstuhl_heatingvalve from simulation.devices import my_powerplug shelly_pro3 = None tradfri_button = None hue_sw_br_ct = None silvercrest_button = None silvercrest_motion_sensor = None audio_status = None remote = None my_ambient = None class tradfri_sw(tradfri_light): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False, send_on_power_on=True) class tradfri_sw_br(tradfri_light): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic, enable_state=True, enable_brightness=True, enable_color_temp=False, send_on_power_on=True) class tradfri_sw_br_ct(tradfri_light): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic, enable_state=True, enable_brightness=True, enable_color_temp=True, send_on_power_on=True) class livarno_sw_br_ct(tradfri_light): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic, enable_state=True, enable_brightness=True, enable_color_temp=True, send_on_power_on=False) 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): rv_list = [] for member in self[:]: m = getattr(member, name) rv_list.append(m(*args, **kwargs)) rv = rv_list[0] for other in rv_list[1:]: if other != rv: return None # Hopefully None was not expected ;-) return rv # If all return values are identical 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