MQTT Home Emulation
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #
  19. cmd_base = self.topic.replace('/', '.') + '.'
  20. self.user_cmds = {
  21. cmd_base + 'toggle1': self.__ui_toggle_output_0__,
  22. cmd_base + 'toggle2': self.__ui_toggle_output_1__,
  23. cmd_base + 'toggle3': self.__ui_toggle_output_2__,
  24. cmd_base + 'toggle4': self.__ui_toggle_output_3__,
  25. }
  26. def __rx_set__(self, client, userdata, message):
  27. data = message.payload.decode('utf-8')
  28. key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
  29. self.logger.info("Received set data for %s: %s", key, repr(data))
  30. self.__set__(key, data)
  31. def __set__(self, key, data):
  32. base.__set__(self, key, data)
  33. #
  34. self.send_device_status(key)
  35. if key.startswith("output/"):
  36. if data == "true":
  37. self.power_on(key)
  38. def send_device_status(self, key):
  39. data = self[key]
  40. self.logger.info("Sending status for %s: %s", key, repr(data))
  41. self.mqtt_client.send(self.topic + '/' + key, data)
  42. def __ui_toggle_output__(self, num):
  43. key = self.PROPERTIES[num]
  44. self.__set__(key, 'false' if self.get(key).lower() == 'true' else 'true')
  45. def __ui_toggle_output_0__(self, *args):
  46. self.__ui_toggle_output__(0)
  47. def __ui_toggle_output_1__(self, *args):
  48. self.__ui_toggle_output__(1)
  49. def __ui_toggle_output_2__(self, *args):
  50. self.__ui_toggle_output__(2)
  51. def __ui_toggle_output_3__(self, *args):
  52. self.__ui_toggle_output__(3)