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

ground_floor_west.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. from function.modules import brightness_choose_n_action, radiator_function
  7. import logging
  8. from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light
  9. from function.videv import videv_switching, videv_switch_brightness_color_temp
  10. import task
  11. try:
  12. from config import APP_NAME as ROOT_LOGGER_NAME
  13. except ImportError:
  14. ROOT_LOGGER_NAME = 'root'
  15. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  16. class ground_floor_west_floor(room_shelly_silvercrest_light):
  17. # http://shelly1l-84CCA8AD1148
  18. def __init__(self, mqtt_client):
  19. super().__init__(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_1_ZIGBEE)
  20. #
  21. # Callback initialisation
  22. #
  23. self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_2_ZIGBEE)
  24. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.main_light_tradfri_2.set_brightness_mcb)
  25. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.main_light_tradfri_2.set_color_temp_mcb)
  26. #
  27. self.main_light_videv = videv_switch_brightness_color_temp(
  28. mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_VIDEV,
  29. self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
  30. self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
  31. self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
  32. )
  33. def send_init_message_main_light(self):
  34. super().send_init_message_main_light()
  35. self.main_light_tradfri_2.request_data()
  36. class ground_floor_west_marion(room_shelly):
  37. # http://shelly1l-E8DB84A1E067
  38. def __init__(self, mqtt_client):
  39. super().__init__(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_MARION_MAIN_LIGHT_GUI)
  40. # radiator valve
  41. self.radiator_function = radiator_function(mqtt_client, config.TOPIC_GFW_MARION_RADIATOR_VALVE_ZIGBEE,
  42. config.TOPIC_GFW_MARION_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_GFW_MARION)
  43. #
  44. self.main_light_videv = videv_switching(
  45. mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_VIDEV,
  46. self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
  47. )
  48. class ground_floor_west_dirk(room_shelly_tradfri_light):
  49. STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
  50. STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
  51. STATE_ACTIVE_DEVICE_AMPLIFIER = 2
  52. STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
  53. #
  54. LED_ACTIVE_DEVICE_MAIN_LIGHT = devices.nodered_gui_leds.KEY_LED_0
  55. LED_ACTIVE_DEVICE_DESK_LIGHT = devices.nodered_gui_leds.KEY_LED_1
  56. LED_ACTIVE_DEVICE_AMPLIFIER = devices.nodered_gui_leds.KEY_LED_2
  57. #
  58. KEY_POWERPLUG_AMPLIFIER = devices.my_powerplug.KEY_OUTPUT_0
  59. KEY_POWERPLUG_CD_PLAYER = devices.my_powerplug.KEY_OUTPUT_2
  60. KEY_POWERPLUG_DESK_LIGHT = devices.my_powerplug.KEY_OUTPUT_1
  61. KEY_POWERPLUG_PC_DOCK = devices.my_powerplug.KEY_OUTPUT_3
  62. #
  63. AUDIO_SOURCE_PC = 0
  64. AUDIO_SOURCE_CD = 1
  65. AUDIO_SOURCE_RASPI = 2
  66. # http://shelly1l-3C6105E44F27
  67. def __init__(self, mqtt_client):
  68. super().__init__(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI, config.TOPIC_GFW_DIRK_MAIN_LIGHT_ZIGBEE)
  69. #
  70. self.powerplug_common = devices.my_powerplug(mqtt_client, config.TOPIC_GFW_DIRK_POWERPLUG)
  71. self.desk_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_ZIGBEE)
  72. self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_GFW_DIRK_INPUT_DEVICE)
  73. #
  74. self.gui_desk_light = devices.nodered_gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI)
  75. #
  76. self.gui_amplifier = devices.nodered_gui_switch(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_GUI)
  77. self.gui_cd_player = devices.nodered_gui_switch(mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_GUI)
  78. self.gui_pc_dock = devices.nodered_gui_switch(mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_GUI)
  79. #
  80. self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
  81. #
  82. self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED)
  83. self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0)
  84. self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT)
  85. self.brightness_functions.add(self.remote_amplifier, self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER)
  86. #
  87. self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY)
  88. self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD)
  89. # radiator valve
  90. self.radiator_function = radiator_function(mqtt_client, config.TOPIC_GFW_DIRK_RADIATOR_VALVE_ZIGBEE,
  91. config.TOPIC_GFW_DIRK_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_GFW_DIRK)
  92. #
  93. self.delayed_task = task.delayed(1.0, self.send_audio_source)
  94. #
  95. # Callback initialisation
  96. #
  97. # main light
  98. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
  99. self.main_light_shelly.toggle_output_0_mcb)
  100. # desk light
  101. # switch
  102. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT,
  103. self.powerplug_common.toggle_output_1_mcb)
  104. self.gui_desk_light.add_callback(devices.nodered_gui_light.KEY_STATE, None, self.powerplug_common.set_output_1_mcb)
  105. self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_desk_light.set_state_mcb)
  106. # brightness and color temp
  107. self.gui_desk_light.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.desk_light_tradfri.set_brightness_mcb)
  108. self.gui_desk_light.add_callback(devices.nodered_gui_light.KEY_COLOR_TEMP, None, self.desk_light_tradfri.set_color_temp_mcb)
  109. self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_desk_light.set_enable_mcb)
  110. self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_desk_light.set_brightness_mcb)
  111. self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_desk_light.set_color_temp_mcb)
  112. # amplifier
  113. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT_LONG,
  114. self.powerplug_common.toggle_output_0_mcb)
  115. self.gui_amplifier.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_0_mcb)
  116. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.gui_amplifier.set_state_mcb)
  117. # amplifier auto on
  118. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation, True)
  119. self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation, True)
  120. self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation, True)
  121. # audio source
  122. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, True, self.audio_source_selector, True)
  123. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, True, self.audio_source_selector, True)
  124. self.spotify_state.add_callback(devices.status.KEY_STATE, True, self.audio_source_selector, True)
  125. self.mpd_state.add_callback(devices.status.KEY_STATE, True, self.audio_source_selector, True)
  126. self.audio_source = self.AUDIO_SOURCE_PC
  127. # cd player
  128. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG,
  129. self.powerplug_common.toggle_output_2_mcb)
  130. self.gui_cd_player.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_2_mcb)
  131. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.gui_cd_player.set_state_mcb)
  132. # pc dock
  133. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
  134. self.powerplug_common.toggle_output_3_mcb)
  135. self.gui_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb)
  136. self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_pc_dock.set_state_mcb)
  137. #
  138. self.main_light_videv = videv_switch_brightness_color_temp(
  139. mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_VIDEV,
  140. self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
  141. self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
  142. self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
  143. )
  144. self.desk_light_videv = videv_switch_brightness_color_temp(
  145. mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_VIDEV,
  146. self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT,
  147. self.desk_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
  148. self.desk_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
  149. )
  150. self.amplifier_videv = videv_switching(
  151. mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_VIDEV,
  152. self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER
  153. )
  154. self.cd_player_videv = videv_switching(
  155. mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_VIDEV,
  156. self.powerplug_common, self.KEY_POWERPLUG_CD_PLAYER
  157. )
  158. self.pc_dock_videv = videv_switching(
  159. mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_VIDEV,
  160. self.powerplug_common, self.KEY_POWERPLUG_PC_DOCK
  161. )
  162. def all_off(self, device=None, key=None, data=None):
  163. super().all_off(device, key, data)
  164. self.powerplug_common.set_output_all(False)
  165. def cd_amplifier_synchronisation(self, device, key, data):
  166. logger.info("Syncing \"%s\" amplifier with cd player: %s", type(self).__name__, data)
  167. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  168. def raspi_amplifier_synchronisation(self, device, key, data):
  169. logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data)
  170. self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
  171. def audio_source_selector(self, device, key, data):
  172. if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
  173. # switch on of cd player
  174. self.audio_source = self.AUDIO_SOURCE_CD
  175. elif device in [self.spotify_state, self.mpd_state]:
  176. # switch on raspi-source
  177. self.audio_source = self.AUDIO_SOURCE_RASPI
  178. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  179. # switch on of amplifier -> select source and reset stored source value
  180. self.delayed_task.run()
  181. def send_audio_source(self):
  182. if self.audio_source == self.AUDIO_SOURCE_PC:
  183. logger.info("Sending IR command to change audio source to pc")
  184. self.remote_amplifier.set_line3()
  185. elif self.audio_source == self.AUDIO_SOURCE_CD:
  186. logger.info("Sending IR command to change audio source to cd")
  187. self.remote_amplifier.set_cd()
  188. elif self.audio_source == self.AUDIO_SOURCE_RASPI:
  189. logger.info("Sending IR command to change audio source to raspi")
  190. self.remote_amplifier.set_line1()
  191. self.audio_source = self.AUDIO_SOURCE_PC