MQTT Home Emulation
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from devices.base import base
  5. class powerplug(base):
  6. PROPERTIES = [
  7. "output/1",
  8. "output/2",
  9. "output/3",
  10. "output/4",
  11. ]
  12. def __init__(self, mqtt_client, topic):
  13. super().__init__(mqtt_client, topic)
  14. #
  15. for i in range(0, 4):
  16. self[self.PROPERTIES[i]] = False
  17. self.mqtt_client.add_callback(self.topic + '/output/%d/set' % (i + 1), self.__rx_set__)
  18. def __rx_set__(self, client, userdata, message):
  19. data = message.payload.decode('utf-8')
  20. key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
  21. self.logger.info("Received set data for %s: %s", key, repr(data))
  22. self.__set__(key, data)
  23. self.send_device_status(key)
  24. if key.startswith("output/"):
  25. if data == "true":
  26. self.power_on(key)
  27. def send_device_status(self, key):
  28. data = self[key]
  29. self.logger.info("Sending status for %s: %s", key, repr(data))
  30. self.mqtt_client.send(self.topic + '/' + key, data)