Smarthome Functionen
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ground_floor_west.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import devices
  5. import logging
  6. from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light
  7. from function.helpers import changed_value_indicator
  8. import task
  9. try:
  10. from config import APP_NAME as ROOT_LOGGER_NAME
  11. except ImportError:
  12. ROOT_LOGGER_NAME = 'root'
  13. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  14. class ground_floor_west_floor(room_shelly_silvercrest_light):
  15. # https://shelly1l-84CCA8AD1148
  16. def __init__(self, mqtt_client):
  17. super().__init__(mqtt_client, "shellies/floor_eg_w", "gui/gfw_sw_floor",
  18. "zigbee_eg_w/light/floor_eg_w/a", "gui/gfw_br_floor", "gui/gfw_ct_floor")
  19. #
  20. # Callback initialisation
  21. #
  22. self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/floor_eg_w/b")
  23. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
  24. None, self.sync_brightness_main_light)
  25. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
  26. None, self.sync_color_temp_main_light)
  27. def send_init_message_main_light(self):
  28. return super().send_init_message_main_light()
  29. self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
  30. def sync_brightness_main_light(self, device, key, data):
  31. self.main_light_tradfri_2.set_brightness(data)
  32. def sync_color_temp_main_light(self, device, key, data):
  33. self.main_light_tradfri_2.set_color_temp(data)
  34. class ground_floor_west_marion(room_shelly):
  35. # https://shelly1l-E8DB84A1E067
  36. def __init__(self, mqtt_client):
  37. super().__init__(mqtt_client, "shellies/marion", "gui/gfw_sw_marion")
  38. class ground_floor_west_dirk(room_shelly_tradfri_light):
  39. STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
  40. STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
  41. STATE_ACTIVE_DEVICE_AMPLIFIER = 2
  42. STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
  43. #
  44. KEY_POWERPLUG_AMPLIFIER = devices.my_powerplug.KEY_OUTPUT_0
  45. KEY_POWERPLUG_CD_PLAYER = devices.my_powerplug.KEY_OUTPUT_2
  46. KEY_POWERPLUG_DESK_LIGHT = devices.my_powerplug.KEY_OUTPUT_1
  47. KEY_POWERPLUG_PC_DOCK = devices.my_powerplug.KEY_OUTPUT_3
  48. #
  49. AUDIO_SOURCE_PC = 0
  50. AUDIO_SOURCE_CD = 1
  51. AUDIO_SOURCE_RASPI = 2
  52. #
  53. KEY_SPOTIFY = "hifi/spotify/state"
  54. KEY_MPD = "hifi/mpd/state"
  55. # https://shelly1l-3C6105E44F27
  56. def __init__(self, mqtt_client):
  57. super().__init__(mqtt_client, "shellies/dirk", "gui/gfw_sw_dirk",
  58. "zigbee_eg_w/light/dirk", "gui/gfw_br_dirk", "gui/gfw_ct_dirk")
  59. #
  60. self.powerplug_common = devices.my_powerplug(mqtt_client, "powerplug/dirk")
  61. self.desk_light_tradfri = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/dirk_desk")
  62. self.button_tradfri = devices.tradfri_button(mqtt_client, "zigbee_eg_w/input_device/dirk")
  63. #
  64. self.gui_switch_desk_light = devices.nodered_gui(mqtt_client, "gui/gfw_sw_desk_light")
  65. self.gui_brightness_desk_light = devices.nodered_gui(mqtt_client, "gui/gfw_br_desk_light")
  66. self.gui_brightness_desk_light.enable(False)
  67. self.gui_brightness_desk_light.set_feedback(0)
  68. self.gui_color_temp_desk_light = devices.nodered_gui(mqtt_client, "gui/gfw_ct_desk_light")
  69. self.gui_color_temp_desk_light.enable(False)
  70. self.gui_color_temp_desk_light.set_feedback(0)
  71. #
  72. self.gui_switch_amplifier = devices.nodered_gui(mqtt_client, "gui/gfw_sw_amplifier")
  73. self.gui_switch_cd_player = devices.nodered_gui(mqtt_client, "gui/gfw_sw_cd_player")
  74. self.gui_switch_pc_dock = devices.nodered_gui(mqtt_client, "gui/gfw_sw_pc_dock")
  75. #
  76. self.remote_amplifier = devices.remote(mqtt_client, "hifi/remote/RAS5")
  77. self.active_device_state_led = devices.status(mqtt_client, "gui/gfw_active_device_state")
  78. #
  79. self.spotify_state = devices.audio_status(mqtt_client, "hifi/spotify")
  80. self.mpd_state = devices.audio_status(mqtt_client, "hifi/mpd")
  81. #
  82. self.delayed_task = task.delayed(1.0, self.send_audio_source)
  83. #
  84. self.cvi = changed_value_indicator()
  85. #
  86. self.powerplug_common.add_callback(None, None, self.powerplug_gui_feedback_actions)
  87. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation)
  88. self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation)
  89. self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation)
  90. #
  91. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  92. devices.tradfri_button.ACTION_TOGGLE, self.toggle_main_light)
  93. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  94. devices.tradfri_button.ACTION_RIGHT, self.desk_light_switch_action)
  95. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  96. devices.tradfri_button.ACTION_LEFT_LONG, self.amplifier_switch_action)
  97. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  98. devices.tradfri_button.ACTION_RIGHT_LONG, self.cd_player_switch_action)
  99. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  100. devices.tradfri_button.ACTION_LEFT, self.pc_dock_switch_action)
  101. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.brightness_action)
  102. #
  103. self.gui_switch_desk_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.desk_light_switch_action)
  104. self.gui_brightness_desk_light.add_callback(
  105. devices.nodered_gui.KEY_BRIGHTNESS, None, self.desk_light_set_action)
  106. self.gui_color_temp_desk_light.add_callback(
  107. devices.nodered_gui.KEY_COLOR_TEMP, None, self.desk_light_set_action)
  108. self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
  109. None, self.desk_light_set_gui_params_action)
  110. self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
  111. None, self.desk_light_set_gui_params_action)
  112. #
  113. self.gui_switch_amplifier.add_callback(devices.nodered_gui.KEY_STATE, None, self.amplifier_switch_action)
  114. self.gui_switch_cd_player.add_callback(devices.nodered_gui.KEY_STATE, None, self.cd_player_switch_action)
  115. self.gui_switch_pc_dock.add_callback(devices.nodered_gui.KEY_STATE, None, self.pc_dock_switch_action)
  116. #
  117. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.device_chooser_action)
  118. self.powerplug_common.add_callback(None, None, self.device_chooser_action)
  119. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  120. devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  121. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  122. devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  123. #
  124. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.audio_source_selector)
  125. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.audio_source_selector)
  126. self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector)
  127. self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector)
  128. #
  129. self.active_device_state = None
  130. #
  131. self.audio_source = self.AUDIO_SOURCE_PC
  132. def all_off(self, device=None, key=None, data=None):
  133. super().all_off(device, key, data)
  134. self.powerplug_common.set_output_all(False)
  135. def powerplug_gui_feedback_actions(self, device, key, data):
  136. if key == self.KEY_POWERPLUG_AMPLIFIER:
  137. self.gui_switch_amplifier.set_feedback(data)
  138. elif key == self.KEY_POWERPLUG_DESK_LIGHT:
  139. self.gui_switch_desk_light.set_feedback(data)
  140. self.gui_brightness_desk_light.enable(data)
  141. self.gui_color_temp_desk_light.enable(data)
  142. if not data:
  143. self.gui_brightness_desk_light.set_feedback(0)
  144. self.gui_color_temp_desk_light.set_feedback(0)
  145. else:
  146. self.gui_brightness_desk_light.set_feedback(self.desk_light_tradfri.brightness)
  147. self.gui_color_temp_desk_light.set_feedback(self.desk_light_tradfri.color_temp / 10)
  148. elif key == self.KEY_POWERPLUG_CD_PLAYER:
  149. self.gui_switch_cd_player.set_feedback(data)
  150. elif key == self.KEY_POWERPLUG_PC_DOCK:
  151. self.gui_switch_pc_dock.set_feedback(data)
  152. def cd_amplifier_synchronisation(self, device, key, data):
  153. if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
  154. logger.info("Syncing \"%s\" amplifier with cd player: %s", type(self).__name__, data)
  155. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  156. def raspi_amplifier_synchronisation(self, device, key, data):
  157. if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
  158. logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data)
  159. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  160. def desk_light_switch_action(self, device, key, data):
  161. if device == self.button_tradfri:
  162. logger.info("Toggeling \"%s\" desk light to %s", type(self).__name__,
  163. not self.powerplug_common.get(self.KEY_POWERPLUG_DESK_LIGHT))
  164. self.powerplug_common.set_output(self.KEY_POWERPLUG_DESK_LIGHT, "toggle")
  165. else:
  166. logger.info("Setting \"%s\" desk light: %s", type(self).__name__, data)
  167. self.powerplug_common.set_output(self.KEY_POWERPLUG_DESK_LIGHT, data)
  168. def desk_light_set_gui_params_action(self, device, key, data):
  169. if key == devices.nodered_gui.KEY_BRIGHTNESS:
  170. self.gui_brightness_desk_light.set_feedback(data)
  171. elif key == devices.nodered_gui.KEY_COLOR_TEMP:
  172. self.gui_color_temp_desk_light.set_feedback(data / 10)
  173. def desk_light_set_action(self, device, key, data):
  174. if key == devices.nodered_gui.KEY_BRIGHTNESS:
  175. logger.info("Setting brightness \"%s\" desk light: %s", type(self).__name__, data)
  176. self.desk_light_tradfri.set_brightness(data)
  177. elif key == devices.nodered_gui.KEY_COLOR_TEMP:
  178. logger.info("Setting color_temp \"%s\" desk light: %s", type(self).__name__, data)
  179. self.desk_light_tradfri.set_color_temp(data * 10)
  180. def amplifier_switch_action(self, device, key, data):
  181. if device == self.button_tradfri:
  182. logger.info("Toggeling \"%s\" amplifier to %s", type(self).__name__,
  183. not self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER))
  184. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, "toggle")
  185. else:
  186. logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data)
  187. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  188. def cd_player_switch_action(self, device, key, data):
  189. if device == self.button_tradfri:
  190. logger.info("Toggeling \"%s\" cd_player to %s", type(self).__name__,
  191. not self.powerplug_common.get(self.KEY_POWERPLUG_CD_PLAYER))
  192. self.powerplug_common.set_output(self.KEY_POWERPLUG_CD_PLAYER, "toggle")
  193. else:
  194. logger.info("Setting \"%s\" cd_player: %s", type(self).__name__, data)
  195. self.powerplug_common.set_output(self.KEY_POWERPLUG_CD_PLAYER, data)
  196. def pc_dock_switch_action(self, device, key, data):
  197. if device == self.button_tradfri:
  198. logger.info("Toggeling \"%s\" pc_dock to %s", type(self).__name__,
  199. not self.powerplug_common.get(self.KEY_POWERPLUG_PC_DOCK))
  200. self.powerplug_common.set_output(self.KEY_POWERPLUG_PC_DOCK, "toggle")
  201. else:
  202. logger.info("Setting \"%s\" pc_dock: %s", type(self).__name__, data)
  203. self.powerplug_common.set_output(self.KEY_POWERPLUG_PC_DOCK, data)
  204. def device_chooser_action(self, device, key, data):
  205. if device == self.main_light_shelly:
  206. if self.cvi.changed_here(device.topic, key, data):
  207. if data is True:
  208. self.active_device_state = self.STATE_ACTIVE_DEVICE_MAIN_LIGHT
  209. else:
  210. self.choose_next_device()
  211. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_DESK_LIGHT:
  212. if self.cvi.changed_here(device.topic, key, data):
  213. if data is True:
  214. self.active_device_state = self.STATE_ACTIVE_DEVICE_DESK_LIGHT
  215. else:
  216. self.choose_next_device()
  217. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  218. if self.cvi.changed_here(device.topic, key, data):
  219. if data is True:
  220. self.active_device_state = self.STATE_ACTIVE_DEVICE_AMPLIFIER
  221. else:
  222. self.choose_next_device()
  223. def get_activity_state(self, state):
  224. if state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT:
  225. return self.main_light_shelly.output_0
  226. elif state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT:
  227. return self.powerplug_common.get(self.KEY_POWERPLUG_DESK_LIGHT)
  228. elif state == self.STATE_ACTIVE_DEVICE_AMPLIFIER:
  229. return self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER)
  230. def choose_prev_device(self, device=None, key=None, data=None):
  231. if self.active_device_state is not None:
  232. start_value = self.active_device_state
  233. for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
  234. target_state = (start_value + i + 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1)
  235. if self.get_activity_state(target_state):
  236. self.active_device_state = target_state
  237. for num in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
  238. self.active_device_state_led.set_state(num, self.active_device_state == num)
  239. return
  240. self.active_device_state = None
  241. def choose_next_device(self, device=None, key=None, data=None):
  242. if self.active_device_state is not None:
  243. start_value = self.active_device_state
  244. for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
  245. target_state = (start_value - i - 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1)
  246. if self.get_activity_state(target_state):
  247. self.active_device_state = target_state
  248. for num in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
  249. self.active_device_state_led.set_state(num, self.active_device_state == num)
  250. return
  251. self.active_device_state = None
  252. def brightness_action(self, device, key, data):
  253. if self.active_device_state is not None:
  254. brightness_func = {
  255. self.STATE_ACTIVE_DEVICE_MAIN_LIGHT: self.main_light_tradfri,
  256. self.STATE_ACTIVE_DEVICE_DESK_LIGHT: self.desk_light_tradfri,
  257. self.STATE_ACTIVE_DEVICE_AMPLIFIER: self.remote_amplifier
  258. }
  259. target = brightness_func[self.active_device_state]
  260. if target is None:
  261. logger.error("Not yet implemented")
  262. return
  263. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  264. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  265. target.default_inc()
  266. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  267. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  268. target.default_dec()
  269. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  270. target.default_stop()
  271. def audio_source_selector(self, device, key, data):
  272. if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
  273. if self.cvi.changed_here(device.topic, key, data) and data is True:
  274. # switch on of cd player
  275. self.audio_source = self.AUDIO_SOURCE_CD
  276. elif device in [self.spotify_state, self.mpd_state]:
  277. if self.cvi.changed_here(device.topic, key, data) and data is True:
  278. # switch on raspi-source
  279. self.audio_source = self.AUDIO_SOURCE_RASPI
  280. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  281. if self.cvi.changed_here(device.topic, key, data) and data is True:
  282. # switch on of amplifier -> select source and reset stored source value
  283. self.delayed_task.run()
  284. def send_audio_source(self):
  285. if self.audio_source == self.AUDIO_SOURCE_PC:
  286. logger.info("Sending IR command to change audio source to pc")
  287. self.remote_amplifier.set_line3()
  288. elif self.audio_source == self.AUDIO_SOURCE_CD:
  289. logger.info("Sending IR command to change audio source to cd")
  290. self.remote_amplifier.set_cd()
  291. elif self.audio_source == self.AUDIO_SOURCE_RASPI:
  292. logger.info("Sending IR command to change audio source to raspi")
  293. self.remote_amplifier.set_line1()
  294. self.audio_source = self.AUDIO_SOURCE_PC