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

ground_floor_west.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. from function.db import get_radiator_data, set_radiator_data
  7. from function.modules import brightness_choose_n_action, heating_function, switched_light
  8. from function.rooms import room, room_collection
  9. from function.videv import videv_switching, videv_switch_brightness_color_temp, videv_heating, videv_multistate, videv_audio_player
  10. import logging
  11. import task
  12. try:
  13. from config import APP_NAME as ROOT_LOGGER_NAME
  14. except ImportError:
  15. ROOT_LOGGER_NAME = 'root'
  16. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  17. class ground_floor_west(room_collection):
  18. def __init__(self, mqtt_client):
  19. super().__init__(mqtt_client)
  20. self.dirk = ground_floor_west_dirk(mqtt_client)
  21. self.floor = ground_floor_west_floor(mqtt_client)
  22. self.marion = ground_floor_west_marion(mqtt_client)
  23. class ground_floor_west_floor(room):
  24. def __init__(self, mqtt_client):
  25. #
  26. # Device initialisation
  27. #
  28. # http://shelly1l-84CCA8AD1148
  29. self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY)
  30. self.main_light_tradfri = devices.group(
  31. devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_ZIGBEE % 1),
  32. devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_ZIGBEE % 2)
  33. )
  34. super().__init__(mqtt_client)
  35. #
  36. # Functionality initialisation
  37. #
  38. # Request silvercrest data of lead light after power on
  39. switched_light(self.main_light_shelly, devices.shelly.KEY_OUTPUT_0, self.main_light_tradfri)
  40. #
  41. # Virtual Device Interface
  42. #
  43. self.main_light_videv = videv_switch_brightness_color_temp(
  44. mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_VIDEV,
  45. self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
  46. self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
  47. self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
  48. )
  49. class ground_floor_west_marion(room):
  50. def __init__(self, mqtt_client):
  51. #
  52. # Device initialisation
  53. #
  54. # http://shelly1l-E8DB84A1E067
  55. self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY)
  56. self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, config.TOPIC_GFW_MARION_HEATING_VALVE_ZIGBEE)
  57. super().__init__(mqtt_client)
  58. #
  59. # Functionality initialisation
  60. #
  61. # heating function
  62. self.heating_function = heating_function(
  63. self.heating_valve,
  64. config.DEFAULT_TEMPERATURE[self.heating_valve.topic],
  65. **get_radiator_data(self.heating_valve.topic)
  66. )
  67. self.heating_function.add_callback(None, None, set_radiator_data, True)
  68. #
  69. # Virtual Device Interface
  70. #
  71. self.main_light_videv = videv_switching(
  72. mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_VIDEV,
  73. self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
  74. )
  75. self.heating_function_videv = videv_heating(
  76. mqtt_client, config.TOPIC_GFW_MARION_HEATING_VALVE_VIDEV,
  77. self.heating_function
  78. )
  79. class ground_floor_west_dirk(room):
  80. STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
  81. STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
  82. STATE_ACTIVE_DEVICE_AMPLIFIER = 2
  83. STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
  84. #
  85. KEY_POWERPLUG_AMPLIFIER = devices.my_powerplug.KEY_OUTPUT_0
  86. KEY_POWERPLUG_CD_PLAYER = devices.my_powerplug.KEY_OUTPUT_2
  87. KEY_POWERPLUG_DESK_LIGHT = devices.my_powerplug.KEY_OUTPUT_1
  88. KEY_POWERPLUG_PC_DOCK = devices.my_powerplug.KEY_OUTPUT_3
  89. #
  90. AUDIO_SOURCE_PC = 0
  91. AUDIO_SOURCE_CD = 1
  92. AUDIO_SOURCE_RASPI = 2
  93. def __init__(self, mqtt_client):
  94. #
  95. # Device initialisation
  96. #
  97. # http://shelly1l-3C6105E44F27
  98. self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY)
  99. self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_ZIGBEE)
  100. self.powerplug_common = devices.my_powerplug(mqtt_client, config.TOPIC_GFW_DIRK_POWERPLUG)
  101. self.desk_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_ZIGBEE)
  102. self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_GFW_DIRK_INPUT_DEVICE)
  103. self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
  104. self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY)
  105. self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD)
  106. self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, config.TOPIC_GFW_DIRK_HEATING_VALVE_ZIGBEE)
  107. super().__init__(mqtt_client)
  108. #
  109. # Functionality initialisation
  110. #
  111. # Button - Brightness functionality
  112. self.brightness_functions = brightness_choose_n_action(self.button_tradfri)
  113. self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0)
  114. self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT)
  115. self.brightness_functions.add(self.remote_amplifier, self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER)
  116. # Button - Main light
  117. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
  118. self.main_light_shelly.toggle_output_0_mcb)
  119. # Button - Desk light
  120. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT,
  121. self.powerplug_common.toggle_output_1_mcb)
  122. # Button - Amplifier
  123. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT_LONG,
  124. self.powerplug_common.toggle_output_0_mcb)
  125. # Button - CD player
  126. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG,
  127. self.powerplug_common.toggle_output_2_mcb)
  128. # Button - PC dock
  129. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
  130. self.powerplug_common.toggle_output_3_mcb)
  131. # Mediaplayer - Amplifier auto on
  132. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.powerplug_common.set_output_0_mcb, True)
  133. self.spotify_state.add_callback(devices.audio_status.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
  134. self.mpd_state.add_callback(devices.audio_status.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
  135. # Mediaplayer - Audio source selection
  136. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, True, self.audio_source_selector, True)
  137. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, True, self.audio_source_selector, True)
  138. self.spotify_state.add_callback(devices.audio_status.KEY_STATE, True, self.audio_source_selector, True)
  139. self.mpd_state.add_callback(devices.audio_status.KEY_STATE, True, self.audio_source_selector, True)
  140. self.audio_source = self.AUDIO_SOURCE_PC
  141. # heating function
  142. self.heating_function = heating_function(
  143. self.heating_valve,
  144. config.DEFAULT_TEMPERATURE[self.heating_valve.topic],
  145. **get_radiator_data(self.heating_valve.topic)
  146. )
  147. self.heating_function.add_callback(None, None, set_radiator_data, True)
  148. #
  149. # Virtual Device Interface
  150. #
  151. self.main_light_videv = videv_switch_brightness_color_temp(
  152. mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_VIDEV,
  153. self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
  154. self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
  155. self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
  156. )
  157. self.desk_light_videv = videv_switch_brightness_color_temp(
  158. mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_VIDEV,
  159. self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT,
  160. self.desk_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
  161. self.desk_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
  162. )
  163. self.amplifier_videv = videv_switching(
  164. mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_VIDEV,
  165. self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER
  166. )
  167. self.cd_player_videv = videv_switching(
  168. mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_VIDEV,
  169. self.powerplug_common, self.KEY_POWERPLUG_CD_PLAYER
  170. )
  171. self.pc_dock_videv = videv_switching(
  172. mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_VIDEV,
  173. self.powerplug_common, self.KEY_POWERPLUG_PC_DOCK
  174. )
  175. self.heating_function_videv = videv_heating(
  176. mqtt_client, config.TOPIC_GFW_DIRK_HEATING_VALVE_VIDEV,
  177. self.heating_function
  178. )
  179. self.brightness_functions_device_videv = videv_multistate(
  180. mqtt_client, config.TOPIC_GFW_DIRK_ACTIVE_BRIGHTNESS_DEVICE_VIDEV,
  181. brightness_choose_n_action.KEY_ACTIVE_DEVICE, self.brightness_functions, 3
  182. )
  183. self.audio_player_videv = videv_audio_player(
  184. mqtt_client, config.TOPIC_GFW_DIRK_AUDIO_PLAYER_VIDEV,
  185. self.spotify_state, self.mpd_state
  186. )
  187. #
  188. # Other stuff
  189. #
  190. self.delayed_task_remote = task.delayed(1.0, self.send_audio_source)
  191. def audio_source_selector(self, device, key, data):
  192. if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
  193. # switch on of cd player
  194. self.audio_source = self.AUDIO_SOURCE_CD
  195. elif device in [self.spotify_state, self.mpd_state]:
  196. # switch on raspi-source
  197. self.audio_source = self.AUDIO_SOURCE_RASPI
  198. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  199. # switch on of amplifier -> select source and reset stored source value
  200. self.delayed_task_remote.run()
  201. def send_audio_source(self):
  202. if self.audio_source == self.AUDIO_SOURCE_PC:
  203. logger.info("Sending IR command to change audio source to pc")
  204. self.remote_amplifier.set_line3()
  205. elif self.audio_source == self.AUDIO_SOURCE_CD:
  206. logger.info("Sending IR command to change audio source to cd")
  207. self.remote_amplifier.set_cd()
  208. elif self.audio_source == self.AUDIO_SOURCE_RASPI:
  209. logger.info("Sending IR command to change audio source to raspi")
  210. self.remote_amplifier.set_line1()
  211. self.audio_source = self.AUDIO_SOURCE_PC