MQTT Home Emulation
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

brennenstuhl.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """ Communication (MQTT)
  5. brennenstuhl_heatingvalve {
  6. | "away_mode": ["ON", "OFF"]
  7. | "battery": [0...100] %
  8. | "child_lock": ["LOCK", "UNLOCK"]
  9. | "current_heating_setpoint": [5...30] °C
  10. | "linkquality": [0...255] lqi
  11. | "local_temperature": [numeric] °C
  12. | "preset": ["manual", ...]
  13. | "system_mode": ["heat", ...]
  14. | "valve_detection": ["ON", "OFF"]
  15. | "window_detection": ["ON", "OFF"]
  16. | }
  17. +- set {
  18. "away_mode": ["ON", "OFF", "TOGGLE"]
  19. "child_lock": ["LOCK", "UNLOCK"]
  20. "current_heating_setpoint": [5...30] °C
  21. "preset": ["manual", ...]
  22. "system_mode": ["heat", ...]
  23. "valve_detection": ["ON", "OFF", "TOGGLE"]
  24. "window_detection": ["ON", "OFF", "TOGGLE"]
  25. }
  26. """
  27. from devices.base import base
  28. import json
  29. import task
  30. import time
  31. """ ANSWER of a device:
  32. {
  33. "away_mode":"OFF",
  34. "battery":5,
  35. "child_lock":"UNLOCK",
  36. "current_heating_setpoint":21,
  37. "linkquality":196,
  38. "local_temperature":21.2,
  39. "preset":"manual",
  40. "system_mode":"heat",
  41. "valve_detection":"ON",
  42. "window_detection":"ON"
  43. }
  44. """
  45. class vlv(base):
  46. """A tradfri device with switching functionality
  47. Args:
  48. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  49. topic (str): the base topic for this device
  50. """
  51. PROPERTIES = [
  52. "away_mode",
  53. "battery",
  54. "child_lock",
  55. "current_heating_setpoint",
  56. "linkquality",
  57. "local_temperature",
  58. "preset",
  59. "system_mode",
  60. "valve_detection",
  61. "window_detection"
  62. ]
  63. def __init__(self, mqtt_client, topic, **kwargs):
  64. super().__init__(mqtt_client, topic, **kwargs)
  65. self["away_mode"] = "OFF"
  66. self["battery"] = 87
  67. self["child_lock"] = "UNLOCK"
  68. self["current_heating_setpoint"] = 21
  69. self["linkquality"] = 196
  70. self["local_temperature"] = 21.2
  71. self["preset"] = "manual"
  72. self["system_mode"] = "heat"
  73. self["valve_detection"] = "ON"
  74. self["window_detection"] = "ON"
  75. #
  76. self.mqtt_client.add_callback(self.topic + '/set', self.__rx_set__)
  77. #
  78. self.tq = task.threaded_queue()
  79. self.tq.run()
  80. #
  81. cmd_base = self.topic.replace('/', '.') + '.'
  82. self.user_cmds = {
  83. cmd_base + 'setpoint': self.__ui_setpoint__,
  84. }
  85. def set_state(self, value):
  86. self.__set__("state", "on" if value else "off")
  87. self.send_device_status()
  88. def __rx_set__(self, client, userdata, message):
  89. data = json.loads(message.payload)
  90. self.logger.info("Received set data: %s", repr(data))
  91. for key in data:
  92. self.__set__(key, data[key])
  93. self.tq.enqueue(1, self.send_device_status, data)
  94. def send_device_status(self, rt, data):
  95. for i in range(0, 75):
  96. time.sleep(0.01)
  97. if self.tq.qsize() >= 3:
  98. return
  99. for key in self:
  100. if key not in data:
  101. data[key] = self[key]
  102. self.logger.info("Sending status: %s", repr(data))
  103. self.mqtt_client.send(self.topic, json.dumps(data))
  104. def __ui_setpoint__(self, *args):
  105. try:
  106. value = float(args[0].replace(',', '.'))
  107. except (ValueError, IndexError):
  108. print("You need to give a numeric value as first argument.")
  109. else:
  110. self.__set__("current_heating_setpoint", value)
  111. self.tq.enqueue(1, self.send_device_status, self)