Smarthome Functionen
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ground_floor_west.py 14KB

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