MQTT Home Emulation
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import logging
  2. try:
  3. from config import APP_NAME as ROOT_LOGGER_NAME
  4. except ImportError:
  5. ROOT_LOGGER_NAME = 'root'
  6. class base(dict):
  7. """A base device for all devicetypes
  8. Args:
  9. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  10. topic (str): the base topic for this device
  11. """
  12. PROPERTIES = []
  13. def __init__(self, mqtt_client, topic):
  14. super().__init__()
  15. self.mqtt_client = mqtt_client
  16. self.topic = topic
  17. #
  18. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild("devices")
  19. for entry in self.topic.split('/'):
  20. self.logger = self.logger.getChild(entry)
  21. #
  22. self.__power_on_inst__ = {}
  23. def __set__(self, key, data):
  24. if key in self.PROPERTIES:
  25. self.logger.debug("Setting new property %s to %s", key, repr(data))
  26. self[key] = data
  27. else:
  28. self.logger.warning("Ignoring unsupported property %s", key)
  29. def power_on(self, key):
  30. for i in self.__power_on_inst__.get(key, []):
  31. self.logger.info("Power on action for %s will be executed.", i.topic)
  32. i.power_on_action()
  33. def register_power_on_instance(self, inst, key):
  34. if key not in self.__power_on_inst__:
  35. self.__power_on_inst__[key] = []
  36. if inst not in self.__power_on_inst__[key]:
  37. self.__power_on_inst__[key].append(inst)
  38. def power_on_action(self):
  39. pass