Smarthome Functionen
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.

tradfri.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from devices.base import base
  5. from devices.base import BATTERY_WARN_LEVEL
  6. from devices.base import warning
  7. import logging
  8. class tradfri_light(base):
  9. """ Communication (MQTT)
  10. tradfri_light {
  11. | "state": ["ON" / "OFF" / "TOGGLE"]
  12. | "linkquality": [0...255] lqi
  13. | "brightness": [0...254]
  14. | "color_mode": ["color_temp"]
  15. | "color_temp": ["coolest", "cool", "neutral", "warm", "warmest", 250...454]
  16. | "color_temp_startup": ["coolest", "cool", "neutral", "warm", "warmest", "previous", 250...454]
  17. | "update": []
  18. | }
  19. +- get {
  20. | "state": ""
  21. | }
  22. +- set {
  23. "state": ["ON" / "OFF"]
  24. "brightness": [0...256]
  25. "color_temp": [250...454]
  26. "transition": [0...] seconds
  27. "brightness_move": [-X...0...X] X/s
  28. "brightness_step": [-X...0...X]
  29. "color_temp_move": [-X...0...X] X/s
  30. "color_temp_step": [-X...0...X]
  31. }
  32. """
  33. KEY_LINKQUALITY = "linkquality"
  34. KEY_OUTPUT_0 = "state"
  35. KEY_BRIGHTNESS = "brightness"
  36. KEY_COLOR_TEMP = "color_temp"
  37. KEY_BRIGHTNESS_FADE = "brightness_move"
  38. #
  39. TX_TYPE = base.TX_DICT
  40. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_BRIGHTNESS_FADE]
  41. #
  42. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  43. RX_IGNORE_KEYS = ['update', 'color_mode', 'color_temp_startup']
  44. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  45. def __init__(self, mqtt_client, topic):
  46. super().__init__(mqtt_client, topic)
  47. def __device_to_instance_filter__(self, key, data):
  48. if key == self.KEY_BRIGHTNESS:
  49. return int(round((data - 1) * 100 / 253, 0))
  50. elif key == self.KEY_COLOR_TEMP:
  51. return int(round((data - 250) * 10 / 204, 0))
  52. return super().__device_to_instance_filter__(key, data)
  53. def __instance_to_device_filter__(self, key, data):
  54. if key == self.KEY_BRIGHTNESS:
  55. return int(round(data * 253 / 100 + 1, 0))
  56. elif key == self.KEY_COLOR_TEMP:
  57. return int(round(data * 204 / 10 + 250, 0))
  58. return super().__instance_to_device_filter__(key, data)
  59. #
  60. # RX
  61. #
  62. @property
  63. def output_0(self):
  64. """rv: [True, False]"""
  65. return self.get(self.KEY_OUTPUT_0, False)
  66. @property
  67. def linkquality(self):
  68. """rv: numeric value"""
  69. return self.get(self.KEY_LINKQUALITY, 0)
  70. @property
  71. def brightness(self):
  72. """rv: numeric value [0%, ..., 100%]"""
  73. return self.get(self.KEY_BRIGHTNESS, 0)
  74. @property
  75. def color_temp(self):
  76. """rv: numeric value [0, ..., 10]"""
  77. return self.get(self.KEY_COLOR_TEMP, 0)
  78. #
  79. # TX
  80. #
  81. def request_data(self, device=None, key=None, data=None):
  82. self.mqtt_client.send(self.topic + "/get", '{"%s": ""}' % self.KEY_OUTPUT_0)
  83. def set_output_0(self, state):
  84. """state: [True, False]"""
  85. self.send_command(self.KEY_OUTPUT_0, state)
  86. def set_output_0_mcb(self, device, key, data):
  87. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  88. self.set_output_0(data)
  89. def toggle_output_0_mcb(self, device, key, data):
  90. self.logger.info("Toggeling output 0")
  91. self.set_output_0(not self.output_0)
  92. def set_brightness(self, brightness):
  93. """brightness: [0, ..., 100]"""
  94. self.send_command(self.KEY_BRIGHTNESS, brightness)
  95. def set_brightness_mcb(self, device, key, data):
  96. self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "Changing brightness to %s", str(data))
  97. self.set_brightness(data)
  98. def default_inc(self, speed=40):
  99. self.send_command(self.KEY_BRIGHTNESS_FADE, speed)
  100. def default_dec(self, speed=-40):
  101. self.default_inc(speed)
  102. def default_stop(self):
  103. self.default_inc(0)
  104. def set_color_temp(self, color_temp):
  105. """color_temp: [0, ..., 10]"""
  106. self.send_command(self.KEY_COLOR_TEMP, color_temp)
  107. def set_color_temp_mcb(self, device, key, data):
  108. self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "Changing color temperature to %s", str(data))
  109. self.set_color_temp(data)
  110. def all_off(self):
  111. if self.output_0:
  112. self.set_output_0(False)
  113. class tradfri_button(base):
  114. """ Communication (MQTT)
  115. tradfri_button {
  116. "action": [
  117. "arrow_left_click",
  118. "arrow_left_hold",
  119. "arrow_left_release",
  120. "arrow_right_click",
  121. "arrow_right_hold",
  122. "arrow_right_release",
  123. "brightness_down_click",
  124. "brightness_down_hold",
  125. "brightness_down_release",
  126. "brightness_up_click",
  127. "brightness_up_hold",
  128. "brightness_up_release",
  129. "toggle"
  130. ]
  131. "action_duration": [0...] s
  132. "battery": [0...100] %
  133. "linkquality": [0...255] lqi
  134. "update": []
  135. }
  136. """
  137. ACTION_TOGGLE = "toggle"
  138. ACTION_BRIGHTNESS_UP = "brightness_up_click"
  139. ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
  140. ACTION_RIGHT = "arrow_right_click"
  141. ACTION_LEFT = "arrow_left_click"
  142. ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
  143. ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
  144. ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
  145. ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
  146. ACTION_RIGHT_LONG = "arrow_right_hold"
  147. ACTION_RIGHT_RELEASE = "arrow_right_release"
  148. ACTION_LEFT_LONG = "arrow_left_hold"
  149. ACTION_LEFT_RELEASE = "arrow_left_release"
  150. #
  151. KEY_LINKQUALITY = "linkquality"
  152. KEY_BATTERY = "battery"
  153. KEY_ACTION = "action"
  154. KEY_ACTION_DURATION = "action_duration"
  155. #
  156. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_ACTION]
  157. RX_IGNORE_KEYS = ['update', KEY_ACTION_DURATION]
  158. def __init__(self, mqtt_client, topic):
  159. super().__init__(mqtt_client, topic)
  160. #
  161. self.add_callback(self.KEY_BATTERY, None, self.__warning__, True)
  162. self.__battery_warning__ = False
  163. #
  164. # WARNING CALL
  165. #
  166. def __warning__(self, client, key, data):
  167. if data <= BATTERY_WARN_LEVEL:
  168. if not self.__battery_warning__:
  169. w = warning(self.topic, warning.TYPE_BATTERY_LOW, "Battery low (%.1f%%)", data)
  170. self.logger.warning(w)
  171. self.set(self.KEY_WARNING, w)
  172. else:
  173. self.__battery_warning__ = False
  174. #
  175. # RX
  176. #
  177. @property
  178. def action(self):
  179. """rv: action_txt"""
  180. return self.get(self.KEY_ACTION)