Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, pd, vd):
  13. self.mqtt_client = mqtt_client
  14. self.pd = pd
  15. self.vd = vd
  16. def all_off(self, device=None, key=None, data=None):
  17. logger.info("Switching all off \"%s\"", type(self).__name__)
  18. for name, obj in inspect.getmembers(self):
  19. try:
  20. if obj.__module__.startswith('devices'):
  21. obj.all_off()
  22. except AttributeError:
  23. pass # not a module or has no method all_off
  24. def summer_mode(self, enable):
  25. for name, obj in inspect.getmembers(self):
  26. if obj.__class__.__name__ == 'heating_function':
  27. if obj.__module__ == 'function.modules':
  28. obj.set(obj.KEY_SUMMER_MODE, enable)
  29. class room_collection(object):
  30. ALLOWED_CLASSES = ("room", "room_collection")
  31. def __init__(self, mqtt_client, pd, vd):
  32. self.mqtt_client = mqtt_client
  33. self.pd = pd
  34. self.vd = vd
  35. def all_off(self, device=None, key=None, data=None):
  36. logger.info("Switching all off \"%s\"", type(self).__name__)
  37. for sub_name in dir(self):
  38. # attribute name is not private
  39. if not sub_name.startswith("__"):
  40. sub = getattr(self, sub_name)
  41. if sub.__class__.__bases__[0].__name__ in self.ALLOWED_CLASSES:
  42. sub.all_off()
  43. def summer_mode(self, device=None, key=None, data=None):
  44. logger.info("Changing to %s \"%s\"", "summer mode" if data else "winter_mode", type(self).__name__)
  45. for sub_name in dir(self):
  46. # attribute name is not private
  47. if not sub_name.startswith("__"):
  48. sub = getattr(self, sub_name)
  49. if sub.__class__.__bases__[0].__name__ in self.ALLOWED_CLASSES:
  50. sub.summer_mode(data)
  51. def all_devices(self, object_to_analyse=None, depth=0):
  52. target = object_to_analyse or self
  53. #
  54. devices = []
  55. for name, obj in inspect.getmembers(target):
  56. if not callable(obj): # sort out methods
  57. try:
  58. if obj.__module__.startswith('function.') and not obj.__module__.endswith('.videv'):
  59. devices.extend(self.all_devices(obj, depth+1)) # rekurse in function instances
  60. elif obj.__module__ == "devices":
  61. devices.append(obj)
  62. except AttributeError:
  63. pass # sort out non modules
  64. return devices