Smarthome Functionen
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

rooms.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import logging
  5. import inspect
  6. try:
  7. from config import APP_NAME as ROOT_LOGGER_NAME
  8. except ImportError:
  9. ROOT_LOGGER_NAME = 'root'
  10. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  11. class room(object):
  12. def __init__(self, mqtt_client):
  13. self.mqtt_client = mqtt_client
  14. def all_off(self, device=None, key=None, data=None):
  15. logger.info("Switching all off \"%s\"", type(self).__name__)
  16. for name, obj in inspect.getmembers(self):
  17. try:
  18. if obj.__module__ == 'devices':
  19. obj.all_off()
  20. except AttributeError:
  21. pass # not a module or has no method all_off
  22. class room_collection(object):
  23. ALLOWED_CLASSES = ("room", "room_collection")
  24. def __init__(self, mqtt_client):
  25. self.mqtt_client = mqtt_client
  26. def all_off(self, device=None, key=None, data=None):
  27. logger.info("Switching all off \"%s\"", type(self).__name__)
  28. for sub_name in dir(self):
  29. # attribute name is not private
  30. if not sub_name.startswith("__"):
  31. sub = getattr(self, sub_name)
  32. if sub.__class__.__bases__[0].__name__ in self.ALLOWED_CLASSES:
  33. sub.all_off()
  34. def all_devices(self, object_to_analyse=None, depth=0):
  35. target = object_to_analyse or self
  36. #
  37. devices = []
  38. for name, obj in inspect.getmembers(target):
  39. if not callable(obj): # sort out methods
  40. try:
  41. if obj.__module__.startswith('function.') and not obj.__module__.endswith('.videv'):
  42. devices.extend(self.all_devices(obj, depth+1)) # rekurse in function instances
  43. elif obj.__module__ == "devices":
  44. devices.append(obj)
  45. except AttributeError:
  46. pass # sort out non modules
  47. return devices