Smarthome Functionen
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ground_floor_west.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. # http://shelly1l-84CCA8AD1148
  16. def __init__(self, mqtt_client):
  17. super().__init__(mqtt_client, "shellies/gfw/floor/main_light", "gui/gfw/floor/main_light/switch",
  18. "zigbee/gfw/floor/main_light/a", "gui/gfw/floor/main_light/br_ct")
  19. #
  20. # Callback initialisation
  21. #
  22. self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee/gfw/floor/main_light/b")
  23. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.main_light_tradfri_2.set_brightness_mcb)
  24. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.main_light_tradfri_2.set_color_temp_mcb)
  25. def send_init_message_main_light(self):
  26. super().send_init_message_main_light()
  27. self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
  28. class ground_floor_west_marion(room_shelly):
  29. # http://shelly1l-E8DB84A1E067
  30. def __init__(self, mqtt_client):
  31. super().__init__(mqtt_client, "shellies/gfw/marion/main_light", "gui/gfw/marion/main_light/switch")
  32. class ground_floor_west_dirk(room_shelly_tradfri_light):
  33. STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
  34. STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
  35. STATE_ACTIVE_DEVICE_AMPLIFIER = 2
  36. STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
  37. #
  38. LED_ACTIVE_DEVICE_MAIN_LIGHT = devices.nodered_gui_leds.KEY_LED_0
  39. LED_ACTIVE_DEVICE_DESK_LIGHT = devices.nodered_gui_leds.KEY_LED_1
  40. LED_ACTIVE_DEVICE_AMPLIFIER = devices.nodered_gui_leds.KEY_LED_2
  41. #
  42. KEY_POWERPLUG_AMPLIFIER = devices.my_powerplug.KEY_OUTPUT_0
  43. KEY_POWERPLUG_CD_PLAYER = devices.my_powerplug.KEY_OUTPUT_2
  44. KEY_POWERPLUG_DESK_LIGHT = devices.my_powerplug.KEY_OUTPUT_1
  45. KEY_POWERPLUG_PC_DOCK = devices.my_powerplug.KEY_OUTPUT_3
  46. #
  47. AUDIO_SOURCE_PC = 0
  48. AUDIO_SOURCE_CD = 1
  49. AUDIO_SOURCE_RASPI = 2
  50. # http://shelly1l-3C6105E44F27
  51. def __init__(self, mqtt_client):
  52. super().__init__(mqtt_client, "shellies/gfw/dirk/main_light", "gui/gfw/dirk/main_light/switch", "zigbee/gfw/dirk/main_light", "gui/gfw/dirk/main_light/br_ct")
  53. #
  54. self.powerplug_common = devices.my_powerplug(mqtt_client, "my_apps/gfw/dirk/powerplug")
  55. self.desk_light_tradfri = devices.tradfri_light(mqtt_client, "zigbee/gfw/dirk/desk_light")
  56. self.button_tradfri = devices.tradfri_button(mqtt_client, "zigbee/gfw/dirk/input_device")
  57. #
  58. self.gui_switch_desk_light = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/desk_light/switch")
  59. self.gui_br_cr_desk_light = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/gfw/dirk/desk_light/br_ct")
  60. #
  61. self.gui_switch_amplifier = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/amplifier/switch")
  62. self.gui_switch_cd_player = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/cd_player/switch")
  63. self.gui_switch_pc_dock = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/pc_dock/switch")
  64. #
  65. self.remote_amplifier = devices.remote(mqtt_client, "my_apps/gfw/dirk/remote/RAS5")
  66. self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, "gui/gfw/dirk/active_device_state/led")
  67. #
  68. self.spotify_state = devices.audio_status(mqtt_client, "my_apps/gfw/dirk/hifi/spotify")
  69. self.mpd_state = devices.audio_status(mqtt_client, "my_apps/gfw/dirk/hifi/mpd")
  70. #
  71. self.delayed_task = task.delayed(1.0, self.send_audio_source)
  72. #
  73. self.cvi = changed_value_indicator()
  74. #
  75. # Callback initialisation
  76. #
  77. # main light
  78. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
  79. self.main_light_shelly.toggle_output_0_mcb)
  80. # desk light
  81. # switch
  82. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT,
  83. self.powerplug_common.toggle_output_1_mcb)
  84. self.gui_switch_desk_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_1_mcb)
  85. self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_switch_desk_light.set_state_mcb)
  86. # brightness and color temp
  87. self.gui_br_cr_desk_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
  88. None, self.desk_light_tradfri.set_brightness_mcb)
  89. self.gui_br_cr_desk_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
  90. None, self.desk_light_tradfri.set_color_temp_mcb)
  91. self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_br_cr_desk_light.set_enable_mcb)
  92. self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_cr_desk_light.set_brightness_mcb)
  93. self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_cr_desk_light.set_color_temp_mcb)
  94. # amplifier
  95. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT_LONG,
  96. self.powerplug_common.toggle_output_0_mcb)
  97. self.gui_switch_amplifier.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_0_mcb)
  98. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.gui_switch_amplifier.set_state_mcb)
  99. # amplifier auto on
  100. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation)
  101. self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation)
  102. self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation)
  103. # audio source
  104. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.audio_source_selector)
  105. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.audio_source_selector)
  106. self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector)
  107. self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector)
  108. self.audio_source = self.AUDIO_SOURCE_PC
  109. # cd player
  110. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG,
  111. self.powerplug_common.toggle_output_2_mcb)
  112. self.gui_switch_cd_player.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_2_mcb)
  113. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.gui_switch_cd_player.set_state_mcb)
  114. # pc dock
  115. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
  116. self.powerplug_common.toggle_output_3_mcb)
  117. self.gui_switch_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb)
  118. self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_switch_pc_dock.set_state_mcb)
  119. # brightness
  120. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.brightness_action)
  121. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.device_chooser_action)
  122. self.powerplug_common.add_callback(None, None, self.device_chooser_action)
  123. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  124. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  125. self.active_device_state = None
  126. self.update_active_device_led()
  127. def all_off(self, device=None, key=None, data=None):
  128. super().all_off(device, key, data)
  129. self.powerplug_common.set_output_all(False)
  130. def cd_amplifier_synchronisation(self, device, key, data):
  131. if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
  132. logger.info("Syncing \"%s\" amplifier with cd player: %s", type(self).__name__, data)
  133. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  134. def raspi_amplifier_synchronisation(self, device, key, data):
  135. if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
  136. logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data)
  137. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  138. def device_chooser_action(self, device, key, data):
  139. if device == self.main_light_shelly:
  140. if self.cvi.changed_here(device.topic, key, data):
  141. if data is True:
  142. self.active_device_state = self.STATE_ACTIVE_DEVICE_MAIN_LIGHT
  143. self.update_active_device_led()
  144. else:
  145. self.choose_next_device()
  146. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_DESK_LIGHT:
  147. if self.cvi.changed_here(device.topic, key, data):
  148. if data is True:
  149. self.active_device_state = self.STATE_ACTIVE_DEVICE_DESK_LIGHT
  150. self.update_active_device_led()
  151. else:
  152. self.choose_next_device()
  153. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  154. if self.cvi.changed_here(device.topic, key, data):
  155. if data is True:
  156. self.active_device_state = self.STATE_ACTIVE_DEVICE_AMPLIFIER
  157. self.update_active_device_led()
  158. else:
  159. self.choose_next_device()
  160. def get_activity_state(self, state):
  161. if state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT:
  162. return self.main_light_shelly.output_0
  163. elif state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT:
  164. return self.powerplug_common.get(self.KEY_POWERPLUG_DESK_LIGHT)
  165. elif state == self.STATE_ACTIVE_DEVICE_AMPLIFIER:
  166. return self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER)
  167. def update_active_device_led(self):
  168. self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_AMPLIFIER, self.active_device_state == self.STATE_ACTIVE_DEVICE_AMPLIFIER)
  169. self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_MAIN_LIGHT, self.active_device_state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT)
  170. self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_DESK_LIGHT, self.active_device_state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT)
  171. def choose_prev_device(self, device=None, key=None, data=None):
  172. if self.active_device_state is not None:
  173. start_value = self.active_device_state
  174. for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
  175. target_state = (start_value + i + 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1)
  176. if self.get_activity_state(target_state):
  177. self.active_device_state = target_state
  178. self.update_active_device_led()
  179. return
  180. self.active_device_state = None
  181. self.update_active_device_led()
  182. def choose_next_device(self, device=None, key=None, data=None):
  183. if self.active_device_state is not None:
  184. start_value = self.active_device_state
  185. for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
  186. target_state = (start_value - i - 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1)
  187. if self.get_activity_state(target_state):
  188. self.active_device_state = target_state
  189. self.update_active_device_led()
  190. return
  191. self.active_device_state = None
  192. self.update_active_device_led()
  193. def brightness_action(self, device, key, data):
  194. if self.active_device_state is not None:
  195. brightness_func = {
  196. self.STATE_ACTIVE_DEVICE_MAIN_LIGHT: self.main_light_tradfri,
  197. self.STATE_ACTIVE_DEVICE_DESK_LIGHT: self.desk_light_tradfri,
  198. self.STATE_ACTIVE_DEVICE_AMPLIFIER: self.remote_amplifier
  199. }
  200. target = brightness_func[self.active_device_state]
  201. if target is None:
  202. logger.error("Not yet implemented")
  203. return
  204. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  205. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  206. target.default_inc()
  207. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  208. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  209. target.default_dec()
  210. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  211. target.default_stop()
  212. def audio_source_selector(self, device, key, data):
  213. if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
  214. if self.cvi.changed_here(device.topic, key, data) and data is True:
  215. # switch on of cd player
  216. self.audio_source = self.AUDIO_SOURCE_CD
  217. elif device in [self.spotify_state, self.mpd_state]:
  218. if self.cvi.changed_here(device.topic, key, data) and data is True:
  219. # switch on raspi-source
  220. self.audio_source = self.AUDIO_SOURCE_RASPI
  221. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  222. if self.cvi.changed_here(device.topic, key, data) and data is True:
  223. # switch on of amplifier -> select source and reset stored source value
  224. self.delayed_task.run()
  225. def send_audio_source(self):
  226. if self.audio_source == self.AUDIO_SOURCE_PC:
  227. logger.info("Sending IR command to change audio source to pc")
  228. self.remote_amplifier.set_line3()
  229. elif self.audio_source == self.AUDIO_SOURCE_CD:
  230. logger.info("Sending IR command to change audio source to cd")
  231. self.remote_amplifier.set_cd()
  232. elif self.audio_source == self.AUDIO_SOURCE_RASPI:
  233. logger.info("Sending IR command to change audio source to raspi")
  234. self.remote_amplifier.set_line1()
  235. self.audio_source = self.AUDIO_SOURCE_PC