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

ground_floor_west.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. from devdi import topic as props
  6. from devices import group
  7. from function.db import get_radiator_data, set_radiator_data
  8. from function.modules import brightness_choose_n_action, heating_function, switched_light
  9. from function.rooms import room, room_collection
  10. from function.videv import videv_switching, videv_switch_brightness_color_temp, videv_heating, videv_multistate, videv_audio_player
  11. import logging
  12. import task
  13. try:
  14. from config import APP_NAME as ROOT_LOGGER_NAME
  15. except ImportError:
  16. ROOT_LOGGER_NAME = 'root'
  17. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  18. loc = props.LOC_GFW
  19. class ground_floor_west(room_collection):
  20. def __init__(self, mqtt_client, pd, vd):
  21. super().__init__(mqtt_client, pd, vd)
  22. self.dirk = ground_floor_west_dirk(mqtt_client, pd, vd)
  23. self.floor = ground_floor_west_floor(mqtt_client, pd, vd)
  24. self.marion = ground_floor_west_marion(mqtt_client, pd, vd)
  25. class ground_floor_west_floor(room):
  26. def __init__(self, mqtt_client, pd, vd):
  27. roo = props.ROO_FLO
  28. #
  29. # Device initialisation
  30. #
  31. # http://shelly1l-84CCA8AD1148
  32. self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
  33. self.main_light_tradfri = pd.get(props.STG_ZGW, loc, roo, props.FUN_MAL)
  34. super().__init__(mqtt_client, pd, vd)
  35. #
  36. # Functionality initialisation
  37. #
  38. # Request silvercrest data of lead light after power on
  39. switched_light(self.main_light_shelly, self.main_light_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, self.main_light_shelly.KEY_OUTPUT_0,
  46. self.main_light_tradfri, self.main_light_tradfri.KEY_BRIGHTNESS,
  47. self.main_light_tradfri, self.main_light_tradfri.KEY_COLOR_TEMP
  48. )
  49. class ground_floor_west_marion(room):
  50. def __init__(self, mqtt_client, pd, vd):
  51. roo = props.ROO_MAR
  52. #
  53. # Device initialisation
  54. #
  55. # http://shelly1l-E8DB84A1E067
  56. # main light
  57. self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
  58. # heating function
  59. self.heating_valve = pd.get(props.STG_ZGW, loc, roo, props.FUN_HEA)
  60. # window light
  61. self.window_light = pd.get(props.STG_ZGW, loc, roo, props.FUN_WIL)
  62. super().__init__(mqtt_client, pd, vd)
  63. #
  64. # Functionality initialisation
  65. #
  66. # heating function
  67. self.heating_function = heating_function(
  68. self.heating_valve,
  69. config.DEFAULT_TEMPERATURE,
  70. **get_radiator_data(self.heating_valve.topic)
  71. )
  72. self.heating_function.add_callback(None, None, set_radiator_data, True)
  73. # main light
  74. self.main_light_shelly.add_callback(self.main_light_shelly.KEY_OUTPUT_0, None, self.window_light.set_output_0_mcb, True)
  75. #
  76. # Virtual Device Interface
  77. #
  78. self.main_light_videv = videv_switching(
  79. mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_VIDEV,
  80. self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0
  81. )
  82. self.heating_function_videv = videv_heating(
  83. mqtt_client, config.TOPIC_GFW_MARION_HEATING_VALVE_VIDEV,
  84. self.heating_function
  85. )
  86. # window lamp
  87. self.windowlamp_videv = videv_switch_brightness_color_temp(
  88. mqtt_client, config.TOPIC_GFW_MARION_WINDOW_LAMP_VIDEV,
  89. self.window_light, self.window_light.KEY_OUTPUT_0,
  90. self.window_light, self.window_light.KEY_BRIGHTNESS,
  91. self.window_light, self.window_light.KEY_COLOR_TEMP
  92. )
  93. class ground_floor_west_dirk(room):
  94. STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
  95. STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
  96. STATE_ACTIVE_DEVICE_AMPLIFIER = 2
  97. STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
  98. #
  99. AUDIO_SOURCE_PC = 0
  100. AUDIO_SOURCE_CD = 1
  101. AUDIO_SOURCE_RASPI = 2
  102. AUDIO_SOURCE_BT = 3
  103. def __init__(self, mqtt_client, pd, vd):
  104. roo = props.ROO_DIR
  105. #
  106. # Device initialisation
  107. #
  108. # http://shelly1l-3C6105E44F27
  109. # main light
  110. self.main_light_shelly = pd.get(props.STG_SHE, loc, roo, props.FUN_MAL)
  111. self.main_light_tradfri = pd.get(props.STG_ZGW, loc, roo, props.FUN_MAL)
  112. # powerplug
  113. self.powerplug_common = pd.get(props.STG_MYA, loc, roo, props.FUN_MPP)
  114. self.KEY_POWERPLUG_AMPLIFIER = self.powerplug_common.KEY_OUTPUT_0
  115. self.KEY_POWERPLUG_PHONO = self.powerplug_common.KEY_OUTPUT_1
  116. self.KEY_POWERPLUG_CD_PLAYER = self.powerplug_common.KEY_OUTPUT_2
  117. self.KEY_POWERPLUG_BT = self.powerplug_common.KEY_OUTPUT_3
  118. # dock
  119. self.dock_tradfri = pd.get(props.STG_ZGW, loc, roo, props.FUN_DCK)
  120. # desk light
  121. self.desk_light_tradfri = pd.get(props.STG_ZGW, loc, roo, props.FUN_DEL)
  122. # button
  123. self.button_tradfri = pd.get(props.STG_ZGW, loc, roo, props.FUN_INP)
  124. # hifi
  125. self.remote_amplifier = pd.get(props.STG_MYA, loc, roo, props.FUN_RCA)
  126. self.spotify_state = pd.get(props.STG_MYA, loc, roo, props.FUN_ASS)
  127. self.mpd_state = pd.get(props.STG_MYA, loc, roo, props.FUN_ASM)
  128. self.bt_state = pd.get(props.STG_MYA, loc, roo, props.FUN_ASB)
  129. # heating function
  130. self.heating_valve = pd.get(props.STG_ZGW, loc, roo, props.FUN_HEA)
  131. super().__init__(mqtt_client, pd, vd)
  132. #
  133. # Functionality initialisation
  134. #
  135. # Button - Brightness functionality
  136. self.brightness_functions = brightness_choose_n_action(self.button_tradfri)
  137. self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0)
  138. self.brightness_functions.add(self.desk_light_tradfri, self.desk_light_tradfri, self.desk_light_tradfri.KEY_OUTPUT_0)
  139. self.brightness_functions.add(self.remote_amplifier, self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER)
  140. # Button - Main light
  141. self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_TOGGLE,
  142. self.main_light_shelly.toggle_output_0_mcb)
  143. # Button - Desk light
  144. self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_RIGHT,
  145. self.desk_light_tradfri.toggle_output_0_mcb)
  146. # Button - Amplifier
  147. self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_LEFT_LONG,
  148. self.powerplug_common.toggle_output_0_mcb)
  149. # Button - CD player
  150. self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_RIGHT_LONG,
  151. self.powerplug_common.toggle_output_2_mcb)
  152. # Button - PC dock
  153. self.button_tradfri.add_callback(self.button_tradfri.KEY_ACTION, self.button_tradfri.ACTION_LEFT,
  154. self.dock_tradfri.toggle_output_0_mcb)
  155. # Mediaplayer - Amplifier auto on
  156. self.powerplug_common.add_callback(self.KEY_POWERPLUG_PHONO, None, self.powerplug_common.set_output_0_mcb, True)
  157. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.powerplug_common.set_output_0_mcb, True)
  158. self.powerplug_common.add_callback(self.KEY_POWERPLUG_BT, None, self.powerplug_common.set_output_0_mcb, True)
  159. self.spotify_state.add_callback(self.spotify_state.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
  160. self.mpd_state.add_callback(self.mpd_state.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
  161. self.bt_state.add_callback(self.bt_state.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
  162. # Mediaplayer - Audio source selection
  163. self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, True, self.audio_source_selector, True)
  164. self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, True, self.audio_source_selector, True)
  165. self.powerplug_common.add_callback(self.KEY_POWERPLUG_BT, True, self.audio_source_selector, True)
  166. self.spotify_state.add_callback(self.spotify_state.KEY_STATE, True, self.audio_source_selector, True)
  167. self.mpd_state.add_callback(self.mpd_state.KEY_STATE, True, self.audio_source_selector, True)
  168. self.bt_state.add_callback(self.bt_state.KEY_STATE, True, self.audio_source_selector, True)
  169. self.audio_source = self.AUDIO_SOURCE_PC
  170. # heating function
  171. self.heating_function = heating_function(
  172. self.heating_valve,
  173. config.DEFAULT_TEMPERATURE,
  174. **get_radiator_data(self.heating_valve.topic)
  175. )
  176. self.heating_function.add_callback(None, None, set_radiator_data, True)
  177. #
  178. # Virtual Device Interface
  179. #
  180. self.main_light_videv = videv_switch_brightness_color_temp(
  181. mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_VIDEV,
  182. self.main_light_shelly, self.main_light_shelly.KEY_OUTPUT_0,
  183. self.main_light_tradfri, self.main_light_tradfri.KEY_BRIGHTNESS,
  184. self.main_light_tradfri, self.main_light_tradfri.KEY_COLOR_TEMP
  185. )
  186. self.desk_light_videv = videv_switch_brightness_color_temp(
  187. mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_VIDEV,
  188. self.desk_light_tradfri, self.desk_light_tradfri.KEY_OUTPUT_0,
  189. self.desk_light_tradfri, self.desk_light_tradfri.KEY_BRIGHTNESS,
  190. self.desk_light_tradfri, self.desk_light_tradfri.KEY_COLOR_TEMP
  191. )
  192. self.amplifier_videv = videv_switching(
  193. mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_VIDEV,
  194. self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER
  195. )
  196. self.phono_videv = videv_switching(
  197. mqtt_client, config.TOPIC_GFW_DIRK_PHONO_VIDEV,
  198. self.powerplug_common, self.KEY_POWERPLUG_PHONO
  199. )
  200. self.cd_player_videv = videv_switching(
  201. mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_VIDEV,
  202. self.powerplug_common, self.KEY_POWERPLUG_CD_PLAYER
  203. )
  204. self.bt_videv = videv_switching(
  205. mqtt_client, config.TOPIC_GFW_DIRK_BT_VIDEV,
  206. self.powerplug_common, self.KEY_POWERPLUG_BT
  207. )
  208. self.pc_dock_videv = videv_switching(
  209. mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_VIDEV,
  210. self.dock_tradfri, self.dock_tradfri.KEY_OUTPUT_0
  211. )
  212. self.heating_function_videv = videv_heating(
  213. mqtt_client, config.TOPIC_GFW_DIRK_HEATING_VALVE_VIDEV,
  214. self.heating_function
  215. )
  216. self.brightness_functions_device_videv = videv_multistate(
  217. mqtt_client, config.TOPIC_GFW_DIRK_ACTIVE_BRIGHTNESS_DEVICE_VIDEV,
  218. brightness_choose_n_action.KEY_ACTIVE_DEVICE, self.brightness_functions, 3
  219. )
  220. self.audio_player_videv = videv_audio_player(
  221. mqtt_client, config.TOPIC_GFW_DIRK_AUDIO_PLAYER_VIDEV,
  222. self.spotify_state, self.mpd_state, self.bt_state
  223. )
  224. #
  225. # Other stuff
  226. #
  227. self.delayed_task_remote = task.delayed(1.0, self.send_audio_source)
  228. def audio_source_selector(self, device, key, data):
  229. if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
  230. # switch on of cd player
  231. self.audio_source = self.AUDIO_SOURCE_CD
  232. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_BT:
  233. # switch on of bluetooth
  234. self.audio_source = self.AUDIO_SOURCE_BT
  235. elif device in [self.spotify_state, self.mpd_state, self.bt_state]:
  236. # switch on raspi-source
  237. self.audio_source = self.AUDIO_SOURCE_RASPI
  238. elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
  239. # switch on of amplifier -> select source and reset stored source value
  240. self.delayed_task_remote.run()
  241. def send_audio_source(self):
  242. if self.audio_source == self.AUDIO_SOURCE_PC:
  243. logger.info("Sending IR command to change audio source to pc")
  244. self.remote_amplifier.set_line3()
  245. elif self.audio_source == self.AUDIO_SOURCE_CD:
  246. logger.info("Sending IR command to change audio source to cd")
  247. self.remote_amplifier.set_cd()
  248. elif self.audio_source == self.AUDIO_SOURCE_BT:
  249. logger.info("Sending IR command to change audio source to tuner")
  250. self.remote_amplifier.set_line2()
  251. elif self.audio_source == self.AUDIO_SOURCE_RASPI:
  252. logger.info("Sending IR command to change audio source to raspi")
  253. self.remote_amplifier.set_line1()
  254. self.audio_source = self.AUDIO_SOURCE_PC