Smarthome Functionen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

tradfri.py 7.5KB

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