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

shelly.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from devices.base import base
  5. from devices.base import warning
  6. import logging
  7. import task
  8. class shelly(base):
  9. """ Communication (MQTT)
  10. shelly
  11. +- relay
  12. | +- 0 ["on" / "off"] <- status
  13. | | +- command ["on"/ "off"] <- command
  14. | | +- energy [numeric] <- status
  15. | +- 1 ["on" / "off"] <- status
  16. | +- command ["on"/ "off"] <- command
  17. | +- energy [numeric] <- status
  18. +- input
  19. | +- 0 [0 / 1] <- status
  20. | +- 1 [0 / 1] <- status
  21. +- input_event
  22. | +- 0 <- status
  23. | +- 1 <- status
  24. +- logpush
  25. | +- 0 [0 / 1] <- status
  26. | +- 1 [0 / 1] <- status
  27. +- temperature [numeric] °C <- status
  28. +- temperature_f [numeric] F <- status
  29. +- overtemperature [0 / 1] <- status
  30. +- id <- status
  31. +- model <- status
  32. +- mac <- status
  33. +- ip <- status
  34. +- new_fw <- status
  35. +- fw_ver <- status
  36. """
  37. KEY_OUTPUT_0 = "relay/0"
  38. KEY_OUTPUT_1 = "relay/1"
  39. KEY_INPUT_0 = "input/0"
  40. KEY_INPUT_1 = "input/1"
  41. KEY_LONGPUSH_0 = "longpush/0"
  42. KEY_LONGPUSH_1 = "longpush/1"
  43. KEY_TEMPERATURE = "temperature"
  44. KEY_OVERTEMPERATURE = "overtemperature"
  45. KEY_ID = "id"
  46. KEY_MODEL = "model"
  47. KEY_MAC = "mac"
  48. KEY_IP = "ip"
  49. KEY_NEW_FIRMWARE = "new_fw"
  50. KEY_FIRMWARE_VERSION = "fw_ver"
  51. #
  52. TX_TOPIC = "command"
  53. TX_TYPE = base.TX_VALUE
  54. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1]
  55. #
  56. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_INPUT_0, KEY_INPUT_1, KEY_LONGPUSH_0, KEY_LONGPUSH_1, KEY_OVERTEMPERATURE, KEY_TEMPERATURE,
  57. KEY_ID, KEY_MODEL, KEY_MAC, KEY_IP, KEY_NEW_FIRMWARE, KEY_FIRMWARE_VERSION]
  58. RX_IGNORE_TOPICS = [KEY_OUTPUT_0 + '/' + "energy", KEY_OUTPUT_1 + '/' + "energy", 'input_event/0', 'input_event/1']
  59. RX_IGNORE_KEYS = ['temperature_f']
  60. RX_FILTER_DATA_KEYS = [KEY_INPUT_0, KEY_INPUT_1, KEY_LONGPUSH_0, KEY_LONGPUSH_1, KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OVERTEMPERATURE]
  61. def __init__(self, mqtt_client, topic):
  62. super().__init__(mqtt_client, topic)
  63. #
  64. self.output_key_delayed = None
  65. self.delayed_flash_task = task.delayed(0.3, self.flash_task)
  66. self.delayed_off_task = task.delayed(0.3, self.off_task)
  67. #
  68. self.add_callback(self.KEY_OVERTEMPERATURE, True, self.__warning__, True)
  69. #
  70. self.all_off_requested = False
  71. def __state_logging__(self, inst, key, data):
  72. if key in [self.KEY_OUTPUT_0, self.KEY_OUTPUT_1]:
  73. self.logger.info("State change of '%s' to '%s'", key, repr(data))
  74. elif key in [self.KEY_INPUT_0, self.KEY_INPUT_1, self.KEY_LONGPUSH_0, self.KEY_LONGPUSH_1]:
  75. self.logger.info("Input action '%s' with '%s'", key, repr(data))
  76. def flash_task(self, *args):
  77. if self.flash_active:
  78. self.send_command(self.output_key_delayed, not self.get(self.output_key_delayed))
  79. self.output_key_delayed = None
  80. if self.all_off_requested:
  81. self.delayed_off_task.run()
  82. def off_task(self, *args):
  83. self.all_off()
  84. @property
  85. def flash_active(self):
  86. return self.output_key_delayed is not None
  87. #
  88. # WARNING CALL
  89. #
  90. def __warning__(self, client, key, data):
  91. w = warning(self.topic, warning.TYPE_OVERTEMPERATURE, "Temperature to high (%.1f°C)", self.get(self.KEY_TEMPERATURE) or math.nan)
  92. self.logger.warning(w)
  93. self.set(self.KEY_WARNING, w)
  94. #
  95. # RX
  96. #
  97. @property
  98. def output_0(self):
  99. """rv: [True, False]"""
  100. return self.get(self.KEY_OUTPUT_0)
  101. @property
  102. def output_1(self):
  103. """rv: [True, False]"""
  104. return self.get(self.KEY_OUTPUT_1)
  105. @property
  106. def input_0(self):
  107. """rv: [True, False]"""
  108. return self.get(self.KEY_INPUT_0)
  109. @property
  110. def input_1(self):
  111. """rv: [True, False]"""
  112. return self.get(self.KEY_INPUT_1)
  113. @property
  114. def longpush_0(self):
  115. """rv: [True, False]"""
  116. return self.get(self.KEY_LONGPUSH_0)
  117. @property
  118. def longpush_1(self):
  119. """rv: [True, False]"""
  120. return self.get(self.KEY_LONGPUSH_1)
  121. @property
  122. def temperature(self):
  123. """rv: numeric value"""
  124. return self.get(self.KEY_TEMPERATURE)
  125. #
  126. # TX
  127. #
  128. def set_output_0(self, state):
  129. """state: [True, False]"""
  130. self.send_command(self.KEY_OUTPUT_0, state)
  131. def set_output_0_mcb(self, device, key, data):
  132. self.set_output_0(data)
  133. def toggle_output_0_mcb(self, device, key, data):
  134. self.set_output_0(not self.output_0)
  135. def set_output_1(self, state):
  136. """state: [True, False]"""
  137. self.send_command(self.KEY_OUTPUT_1, state)
  138. def set_output_1_mcb(self, device, key, data):
  139. self.set_output_1(data)
  140. def toggle_output_1_mcb(self, device, key, data):
  141. self.set_output_1(not self.output_1)
  142. def flash_0_mcb(self, device, key, data):
  143. self.output_key_delayed = self.KEY_OUTPUT_0
  144. self.toggle_output_0_mcb(device, key, data)
  145. self.delayed_flash_task.run()
  146. def flash_1_mcb(self, device, key, data):
  147. self.output_key_delayed = self.KEY_OUTPUT_1
  148. self.toggle_output_1_mcb(device, key, data)
  149. self.delayed_flash_task.run()
  150. def all_off(self):
  151. if self.flash_active:
  152. self.all_off_requested = True
  153. else:
  154. if self.output_0:
  155. self.set_output_0(False)
  156. if self.output_1:
  157. self.set_output_1(False)