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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from devices.base import base, base_output
  5. import logging
  6. class tradfri_light(base_output):
  7. """ Communication (MQTT)
  8. tradfri_light {
  9. | "state": ["ON" / "OFF" / "TOGGLE"]
  10. | "linkquality": [0...255] lqi
  11. | "brightness": [0...254]
  12. | "color_mode": ["color_temp"]
  13. | "color_temp": ["coolest", "cool", "neutral", "warm", "warmest", 250...454]
  14. | "color_temp_startup": ["coolest", "cool", "neutral", "warm", "warmest", "previous", 250...454]
  15. | "update": []
  16. | }
  17. +- get {
  18. | "state": ""
  19. | }
  20. +- set {
  21. "state": ["ON" / "OFF"]
  22. "brightness": [0...256]
  23. "color_temp": [250...454]
  24. "transition": [0...] seconds
  25. "brightness_move": [-X...0...X] X/s
  26. "brightness_step": [-X...0...X]
  27. "color_temp_move": [-X...0...X] X/s
  28. "color_temp_step": [-X...0...X]
  29. }
  30. """
  31. KEY_LINKQUALITY = "linkquality"
  32. KEY_OUTPUT_0 = "state"
  33. KEY_BRIGHTNESS = "brightness"
  34. KEY_COLOR_TEMP = "color_temp"
  35. KEY_BRIGHTNESS_FADE = "brightness_move"
  36. #
  37. TX_TYPE = base.TX_DICT
  38. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_BRIGHTNESS_FADE]
  39. #
  40. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  41. RX_IGNORE_KEYS = ['update', 'color_mode', 'color_temp_startup']
  42. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  43. def __state_logging__(self, inst, key, data):
  44. if key in [self.KEY_OUTPUT_0, self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP, self.KEY_BRIGHTNESS_FADE]:
  45. self.logger.info("State change of '%s' to '%s'", key, repr(data))
  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.set_output_0(data)
  87. def toggle_output_0_mcb(self, device, key, data):
  88. self.set_output_0(not self.output_0)
  89. def set_brightness(self, brightness):
  90. """brightness: [0, ..., 100]"""
  91. self.send_command(self.KEY_BRIGHTNESS, brightness)
  92. def set_brightness_mcb(self, device, key, data):
  93. self.set_brightness(data)
  94. def default_inc(self, speed=40):
  95. self.send_command(self.KEY_BRIGHTNESS_FADE, speed)
  96. def default_dec(self, speed=-40):
  97. self.default_inc(speed)
  98. def default_stop(self):
  99. self.default_inc(0)
  100. def set_color_temp(self, color_temp):
  101. """color_temp: [0, ..., 10]"""
  102. self.send_command(self.KEY_COLOR_TEMP, color_temp)
  103. def set_color_temp_mcb(self, device, key, data):
  104. self.set_color_temp(data)
  105. def __all_off__(self):
  106. if self.output_0:
  107. self.set_output_0(False)
  108. class tradfri_button(base):
  109. """ Communication (MQTT)
  110. tradfri_button {
  111. "action": [
  112. "arrow_left_click",
  113. "arrow_left_hold",
  114. "arrow_left_release",
  115. "arrow_right_click",
  116. "arrow_right_hold",
  117. "arrow_right_release",
  118. "brightness_down_click",
  119. "brightness_down_hold",
  120. "brightness_down_release",
  121. "brightness_up_click",
  122. "brightness_up_hold",
  123. "brightness_up_release",
  124. "toggle"
  125. ]
  126. "action_duration": [0...] s
  127. "battery": [0...100] %
  128. "linkquality": [0...255] lqi
  129. "update": []
  130. }
  131. """
  132. ACTION_TOGGLE = "toggle"
  133. ACTION_BRIGHTNESS_UP = "brightness_up_click"
  134. ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
  135. ACTION_RIGHT = "arrow_right_click"
  136. ACTION_LEFT = "arrow_left_click"
  137. ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
  138. ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
  139. ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
  140. ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
  141. ACTION_RIGHT_LONG = "arrow_right_hold"
  142. ACTION_RIGHT_RELEASE = "arrow_right_release"
  143. ACTION_LEFT_LONG = "arrow_left_hold"
  144. ACTION_LEFT_RELEASE = "arrow_left_release"
  145. #
  146. KEY_LINKQUALITY = "linkquality"
  147. KEY_BATTERY = "battery"
  148. KEY_ACTION = "action"
  149. KEY_ACTION_DURATION = "action_duration"
  150. #
  151. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_ACTION]
  152. RX_IGNORE_KEYS = ['update', KEY_ACTION_DURATION]
  153. def __init__(self, mqtt_client, topic):
  154. super().__init__(mqtt_client, topic)
  155. def __state_logging__(self, inst, key, data):
  156. if key in [self.KEY_ACTION]:
  157. self.logger.info("Input '%s' with '%s'", key, repr(data))
  158. #
  159. # RX
  160. #
  161. @property
  162. def action(self):
  163. """rv: action_txt"""
  164. return self.get(self.KEY_ACTION)