58 lines
2.0 KiB
Python
Raw Normal View History

2022-12-20 14:05:32 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import logging
2023-01-26 10:40:56 +01:00
import inspect
2022-12-20 14:05:32 +01:00
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
class room(object):
def __init__(self, mqtt_client):
self.mqtt_client = mqtt_client
def all_off(self, device=None, key=None, data=None):
logger.info("Switching all off \"%s\"", type(self).__name__)
2023-01-26 10:40:56 +01:00
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
2023-01-31 19:17:10 +01:00
class room_collection(object):
ALLOWED_CLASSES = ("room", "room_collection")
def __init__(self, mqtt_client):
self.mqtt_client = mqtt_client
def all_off(self, device=None, key=None, data=None):
logger.info("Switching all off \"%s\"", type(self).__name__)
for sub_name in dir(self):
# attribute name is not private
if not sub_name.startswith("__"):
sub = getattr(self, sub_name)
if sub.__class__.__bases__[0].__name__ in self.ALLOWED_CLASSES:
sub.all_off()
2023-02-10 14:34:49 +01:00
def all_devices(self, object_to_analyse=None):
target = object_to_analyse or self
#
devices = []
for name, obj in inspect.getmembers(target):
if not callable(obj): # sort out methods
try:
if obj.__module__.startswith('function.'):
devices.extend(self.all_devices(obj)) # rekurse in function instances
elif obj.__module__ == "devices":
devices.append(obj)
except AttributeError:
pass # sort out non modules
return devices