Smarthome Functionen
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rooms.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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__ == 'devices':
  21. obj.all_off()
  22. except AttributeError:
  23. pass # not a module or has no method all_off
  24. class room_collection(object):
  25. ALLOWED_CLASSES = ("room", "room_collection")
  26. def __init__(self, mqtt_client, pd, vd):
  27. self.mqtt_client = mqtt_client
  28. self.pd = pd
  29. self.vd = vd
  30. def all_off(self, device=None, key=None, data=None):
  31. logger.info("Switching all off \"%s\"", type(self).__name__)
  32. for sub_name in dir(self):
  33. # attribute name is not private
  34. if not sub_name.startswith("__"):
  35. sub = getattr(self, sub_name)
  36. if sub.__class__.__bases__[0].__name__ in self.ALLOWED_CLASSES:
  37. sub.all_off()
  38. def all_devices(self, object_to_analyse=None, depth=0):
  39. target = object_to_analyse or self
  40. #
  41. devices = []
  42. for name, obj in inspect.getmembers(target):
  43. if not callable(obj): # sort out methods
  44. try:
  45. if obj.__module__.startswith('function.') and not obj.__module__.endswith('.videv'):
  46. devices.extend(self.all_devices(obj, depth+1)) # rekurse in function instances
  47. elif obj.__module__ == "devices":
  48. devices.append(obj)
  49. except AttributeError:
  50. pass # sort out non modules
  51. return devices