From d4c5411d47f33a1b2a6f31a81c26b651af164fe5 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Thu, 26 Jan 2023 10:40:56 +0100 Subject: [PATCH] all off improved --- function/__init__.py | 26 ++++---------------------- function/rooms.py | 17 +++++++---------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/function/__init__.py b/function/__init__.py index 8814348..d5fb3b0 100644 --- a/function/__init__.py +++ b/function/__init__.py @@ -98,11 +98,6 @@ class all_functions(object): rv.append(obj) return rv - def common_off(self, device=None, key=None, data=None): - logger.info("Switching \"common\" off.") - for common in self.getmembers('common'): - common.all_off() - def gfw_off(self, device=None, key=None, data=None): logger.info("Switching \"ground floor west\" off.") for gfw in self.getmembers('gfw'): @@ -124,20 +119,7 @@ class all_functions(object): stw.all_off() def all_off(self, device=None, key=None, data=None): - self.common_off(device, key, data) - self.gfw_off(device, key, data) - self.ffw_off(device, key, data) - self.ffe_off(device, key, data) - self.stw_off(device, key, data) - - def devicelist(self): - if self.__devices__ is None: - self.__devices__ = [] - for name, obj in inspect.getmembers(self): - if obj.__class__.__module__ == "devices": - self.__devices__.append(obj) - elif obj.__class__.__module__.split('.')[0] == 'function': - for devicename, device in inspect.getmembers(obj): - if device.__class__.__module__ == "devices": - self.__devices__.append(device) - return self.__devices__ + for name, obj in inspect.getmembers(self): + parent_name = obj.__class__.__base__.__name__ + if parent_name == "room": + obj.all_off() diff --git a/function/rooms.py b/function/rooms.py index b265900..5fcb4f1 100644 --- a/function/rooms.py +++ b/function/rooms.py @@ -3,7 +3,7 @@ # import logging -import task +import inspect try: from config import APP_NAME as ROOT_LOGGER_NAME @@ -11,11 +11,6 @@ except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) -# TODO: all_off: getattr and identify switchable devices switch those off (default method of device) -# - all devices are as attributes in the room class -# - implement a all_off blacklist to be initialised while __init__ -# TODO: implement all off and user feedback method (all off save) - class room(object): def __init__(self, mqtt_client): @@ -23,7 +18,9 @@ class room(object): def all_off(self, device=None, key=None, data=None): logger.info("Switching all off \"%s\"", type(self).__name__) - try: - self.main_light_shelly.all_off() - except AttributeError: - logger.exception("Device self.main_light does not exist!") + for name, obj in inspect.getmembers(self): + try: + if obj.__module__ == 'devices': + obj.all_off() + except AttributeError: + pass # not a module or has no method all_off