Smarthome Functionen
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

__init__.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. import inspect
  7. import logging
  8. # TODO: implement first floor west and dirk (ground floor west)
  9. # TODO: implement bed light dirk fading by input device
  10. # TODO: implement heating function sleep_madi
  11. # TODO: implement garland
  12. # TODO: implement circulation pump
  13. # TODO: implement switch off functionality (except of switch off button transportation)
  14. # TODO: WARNING - Got a message from "shellies/floor_eg_w"with unparsed content "event" and "event_cnt"
  15. # TODO: implement warning message (incl. fixing all_functions.devicelist
  16. try:
  17. from config import APP_NAME as ROOT_LOGGER_NAME
  18. except ImportError:
  19. ROOT_LOGGER_NAME = 'root'
  20. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  21. class all_functions(object):
  22. def __init__(self, mqtt_client):
  23. self.rooms = {}
  24. self.__devices__ = None
  25. #
  26. # first floor east
  27. #
  28. self.ffe_floor = first_floor_east_floor(mqtt_client)
  29. self.ffe_kitchen = first_floor_east_kitchen(mqtt_client)
  30. self.ffe_dining = first_floor_east_dining(mqtt_client)
  31. self.ffe_sleep_madi = first_floor_east_sleep_madi(mqtt_client)
  32. self.ffe_living = first_floor_east_living(mqtt_client)
  33. #
  34. # ground floor west
  35. #
  36. self.gfw_floor = ground_floor_west_floor(mqtt_client)
  37. self.gfw_marion = ground_floor_west_marion(mqtt_client)
  38. self.gfw_dirk = ground_floor_west_dirk(mqtt_client)
  39. #
  40. # Input devices and Off Buttons
  41. #
  42. self.ffe_button_tradfri_sleep = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east")
  43. #
  44. # Callback initialisation
  45. #
  46. self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "toggle",
  47. self.ffe_sleep_madi.toggle_main_light)
  48. self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "brightness_up_click",
  49. self.ffe_sleep_madi.toggle_bed_light_di)
  50. self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "brightness_down_click",
  51. self.ffe_sleep_madi.toggle_bed_light_di)
  52. self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "arrow_right_click",
  53. self.ffe_floor.toggle_main_light)
  54. def devicelist(self):
  55. raise Exception
  56. # TODO: generate list by using getattr
  57. if self.__devices__ is None:
  58. self.__devices__ = []
  59. for room in self.rooms:
  60. for name, obj in inspect.getmembers(room):
  61. if type(obj) in devices.DEVICE_TYPE_LIST():
  62. self.__devices__.append(obj)
  63. return self.__devices__
  64. class room(object):
  65. def gui_switch_feedback(self, device, key, data):
  66. self.gui_switch_main_light.set_feedback(data)
  67. class room_shelly(room):
  68. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
  69. self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
  70. #
  71. self.gui_switch_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_switch)
  72. #
  73. # Callback initialisation
  74. #
  75. self.gui_switch_main_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command)
  76. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_feedback)
  77. def all_off(self):
  78. self.main_light_shelly.set_output_0(False)
  79. self.main_light_shelly.set_output_1(False)
  80. def gui_switch_command(self, device, key, data):
  81. logger.info("Switching \"%s\" main light: %s", type(self).__name__, str(data))
  82. self.main_light_shelly.set_output_0(data)
  83. class room_shelly_tradfri_light(room_shelly):
  84. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  85. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  86. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic=topic_tradfri_light)
  87. #
  88. self.gui_brightness_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_brightness)
  89. self.gui_brightness_main_light.enable(False)
  90. self.gui_brightness_main_light.set_feedback(0)
  91. self.gui_color_temp_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_color_temp)
  92. self.gui_color_temp_main_light.enable(False)
  93. self.gui_color_temp_main_light.set_feedback(0)
  94. #
  95. # Callback initialisation
  96. #
  97. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.enable_brightness_n_colortemp)
  98. self.main_light_tradfri.add_callback(
  99. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_main_light)
  100. self.main_light_tradfri.add_callback(
  101. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_main_light)
  102. self.gui_brightness_main_light.add_callback(
  103. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_main_light)
  104. self.gui_color_temp_main_light.add_callback(
  105. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_main_light)
  106. def enable_brightness_n_colortemp(self, devive, key, data):
  107. self.gui_brightness_main_light.enable(data)
  108. self.gui_color_temp_main_light.enable(data)
  109. if data is False:
  110. self.gui_brightness_main_light.set_feedback(0)
  111. self.gui_color_temp_main_light.set_feedback(0)
  112. else:
  113. self.gui_brightness_main_light.set_feedback(self.main_light_tradfri.brightness)
  114. self.gui_color_temp_main_light.set_feedback(self.main_light_tradfri.color_temp / 10)
  115. def set_gui_brightness_main_light(self, device, key, data):
  116. self.gui_brightness_main_light.set_feedback(data)
  117. def set_gui_color_temp_main_light(self, device, key, data):
  118. self.gui_color_temp_main_light.set_feedback(data / 10)
  119. def set_brightness_main_light(self, device, key, data):
  120. logger.info("Setting brightness \"%s\" main light: %.1f", type(self).__name__, data)
  121. self.main_light_tradfri.set_brightness(data)
  122. def set_color_temp_main_light(self, device, key, data):
  123. logger.info("Setting color_temp \"%s\" main light: %.1f", type(self).__name__, data)
  124. self.main_light_tradfri.set_color_temp(data * 10)
  125. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  126. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  127. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp)
  128. #
  129. # Callback initialisation
  130. #
  131. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  132. #
  133. self.main_light_shelly_last = None
  134. def get_initial_main_light_data(self, device, key, data):
  135. if data is True and self.main_light_shelly_last is not True:
  136. self.send_init_message_main_light()
  137. self.main_light_shelly_last = data
  138. def send_init_message_main_light(self):
  139. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')
  140. class first_floor_east_floor(room_shelly):
  141. def __init__(self, mqtt_client):
  142. # http://shelly1l-3C6105E4E629
  143. super().__init__(mqtt_client, "shellies/floor_madi", "gui/ffe_sw_floor")
  144. def toggle_main_light(self, device, key, data):
  145. logger.info("Toggeling \"%s\" main light", type(self).__name__)
  146. self.main_light_shelly.set_output_0("toggle")
  147. class first_floor_east_kitchen(room_shelly):
  148. # TODO: add circulation pump (switch, time)
  149. def __init__(self, mqtt_client):
  150. # http://shelly1l-8CAAB5616C01
  151. super().__init__(mqtt_client, "shellies/kitchen", "gui/ffe_sw_kitchen")
  152. class first_floor_east_dining(room_shelly):
  153. def __init__(self, mqtt_client):
  154. # http://shelly1l-84CCA8ADD055
  155. super().__init__(mqtt_client, "shellies/diningroom", "gui/ffe_sw_diningroom")
  156. self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/dining_floorlamp")
  157. if config.CHRISTMAS:
  158. self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, topic="zigbee_og_e/powerplug/aux")
  159. #
  160. self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_dining_floorlamp")
  161. #
  162. # Callback initialisation
  163. #
  164. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
  165. self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
  166. self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0,
  167. None, self.gui_switch_feedback_floorlamp)
  168. #
  169. self.main_light_shelly_last = None
  170. def floorlamp_synchronisation(self, device, key, data):
  171. if data != self.main_light_shelly_last:
  172. logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
  173. self.floorlamp_powerplug.set_output_0(data)
  174. self.main_light_shelly_last = data
  175. def gui_switch_command_floorlamp(self, device, key, data):
  176. logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
  177. self.floorlamp_powerplug.set_output_0(data)
  178. def gui_switch_feedback_floorlamp(self, device, key, data):
  179. self.gui_switch_floorlamp.set_feedback(data)
  180. class first_floor_east_sleep_madi(room_shelly_tradfri_light):
  181. def __init__(self, mqtt_client):
  182. # http://shelly1l-E8DB84A254C7
  183. super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi",
  184. "zigbee_og_e/light/sleep_madi", "gui/ffe_br_sleep_madi", "gui/ffe_ct_sleep_madi")
  185. #
  186. self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di")
  187. #
  188. self.gui_switch_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_sw_bed_light_di")
  189. self.gui_brightness_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_br_bed_light_di")
  190. self.gui_brightness_bed_light_di.enable(False)
  191. self.gui_brightness_bed_light_di.set_feedback(0)
  192. #
  193. # Callback initialisation
  194. #
  195. self.gui_switch_bed_light_di.add_callback(
  196. devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_bed_light_di)
  197. self.bed_light_di_tradfri.add_callback(
  198. devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_bed_light_di)
  199. self.bed_light_di_tradfri.add_callback(
  200. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_bed_light_di)
  201. self.gui_brightness_bed_light_di.add_callback(
  202. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_bed_light_di)
  203. def toggle_main_light(self, device, key, data):
  204. logger.info("Toggeling \"%s\" main light", type(self).__name__)
  205. self.main_light_shelly.set_output_0("toggle")
  206. def gui_switch_command_bed_light_di(self, device, key, data):
  207. logger.info("Switching \"%s\" bed light dirk: %s", type(self).__name__, str(data))
  208. self.bed_light_di_tradfri.set_output_0(data)
  209. def gui_switch_feedback_bed_light_di(self, device, key, data):
  210. self.gui_switch_bed_light_di.set_feedback(data)
  211. self.gui_brightness_bed_light_di.enable(data)
  212. if data is False:
  213. self.gui_brightness_bed_light_di.set_feedback(0)
  214. else:
  215. self.gui_brightness_bed_light_di.set_feedback(self.bed_light_di_tradfri.brightness)
  216. def set_gui_brightness_bed_light_di(self, device, key, data):
  217. self.gui_brightness_bed_light_di.set_feedback(data)
  218. def set_brightness_bed_light_di(self, device, key, data):
  219. logger.info("Setting brightness \"%s\" bed light dirk: %.1f", type(self).__name__, data)
  220. self.bed_light_di_tradfri.set_brightness(data)
  221. def toggle_bed_light_di(self, device, key, data):
  222. logger.info("Toggeling \"%s\" bed light dirk", type(self).__name__)
  223. self.bed_light_di_tradfri.set_output_0("toggle")
  224. class first_floor_east_living(room_shelly_tradfri_light):
  225. def __init__(self, mqtt_client):
  226. # http://shelly1l-3C6105E3F910
  227. super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom",
  228. "zigbee_og_e/light/livingroom", "gui/ffe_br_livingroom", "gui/ffe_ct_livingroom")
  229. for i in range(1, 7):
  230. setattr(self, 'floorlamp_tradfri_%d' % i,
  231. devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/living_floorlamp_%d" % i))
  232. #
  233. self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_living_floorlamp")
  234. self.gui_brightness_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_br_livingroom_floorlamp")
  235. self.gui_brightness_floorlamp.enable(False)
  236. self.gui_brightness_floorlamp.set_feedback(0)
  237. self.gui_color_temp_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_ct_livingroom_floorlamp")
  238. self.gui_color_temp_floorlamp.enable(False)
  239. self.gui_color_temp_floorlamp.set_feedback(0)
  240. #
  241. # Callback initialisation
  242. #
  243. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
  244. self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
  245. self.floorlamp_tradfri_1.add_callback(
  246. devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp)
  247. self.floorlamp_tradfri_1.add_callback(
  248. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_floorlamp)
  249. self.floorlamp_tradfri_1.add_callback(
  250. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_floorlamp)
  251. self.gui_brightness_floorlamp.add_callback(
  252. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_floorlamp)
  253. self.gui_color_temp_floorlamp.add_callback(
  254. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_floorlamp)
  255. #
  256. self.main_light_shelly_last = None
  257. def __floorlamp_devices__(self):
  258. rv = []
  259. for i in range(1, 7):
  260. rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
  261. return rv
  262. def floorlamp_synchronisation(self, device, key, data):
  263. if data != self.main_light_shelly_last:
  264. logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
  265. for device in self.__floorlamp_devices__():
  266. device.set_output_0(data)
  267. self.main_light_shelly_last = data
  268. def gui_switch_command_floorlamp(self, device, key, data):
  269. logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
  270. for device in self.__floorlamp_devices__():
  271. device.set_output_0(data)
  272. def gui_switch_feedback_floorlamp(self, device, key, data):
  273. self.gui_switch_floorlamp.set_feedback(data)
  274. self.gui_brightness_floorlamp.enable(data)
  275. self.gui_color_temp_floorlamp.enable(data)
  276. if data is False:
  277. self.gui_brightness_floorlamp.set_feedback(0)
  278. self.gui_color_temp_floorlamp.set_feedback(0)
  279. else:
  280. self.gui_brightness_floorlamp.set_feedback(self.floorlamp_tradfri_1.brightness)
  281. self.gui_color_temp_floorlamp.set_feedback(self.floorlamp_tradfri_1.color_temp / 10)
  282. def set_gui_brightness_floorlamp(self, device, key, data):
  283. self.gui_brightness_floorlamp.set_feedback(data)
  284. def set_gui_color_temp_floorlamp(self, device, key, data):
  285. self.gui_color_temp_floorlamp.set_feedback(data / 10)
  286. def set_brightness_floorlamp(self, device, key, data):
  287. logger.info("Setting brightness \"%s\" floorlamp: %.1f", type(self).__name__, data)
  288. for device in self.__floorlamp_devices__():
  289. device.set_brightness(data)
  290. def set_color_temp_floorlamp(self, device, key, data):
  291. logger.info("Setting color_temp \"%s\" floorlamp: %.1f", type(self).__name__, data)
  292. for device in self.__floorlamp_devices__():
  293. device.set_color_temp(data * 10)
  294. class ground_floor_west_floor(room_shelly_silvercrest_light):
  295. # https://shelly1l-84CCA8AD1148
  296. def __init__(self, mqtt_client):
  297. super().__init__(mqtt_client, "shellies/floor_eg_w", "gui/gfw_sw_floor",
  298. "zigbee_eg_w/light/floor_eg_w/a", "gui/gfw_br_floor", "gui/gfw_ct_floor")
  299. #
  300. # Callback initialisation
  301. #
  302. self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/floor_eg_w/b")
  303. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
  304. None, self.sync_brightness_main_light)
  305. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
  306. None, self.sync_color_temp_main_light)
  307. def send_init_message_main_light(self):
  308. return super().send_init_message_main_light()
  309. self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
  310. def sync_brightness_main_light(self, device, key, data):
  311. self.main_light_tradfri_2.set_brightness(data)
  312. def sync_color_temp_main_light(self, device, key, data):
  313. self.main_light_tradfri_2.set_color_temp(data)
  314. class ground_floor_west_marion(room_shelly):
  315. # https://shelly1l-E8DB84A1E067
  316. def __init__(self, mqtt_client):
  317. super().__init__(mqtt_client, "shellies/marion", "gui/gfw_sw_marion")
  318. class ground_floor_west_dirk(room_shelly_tradfri_light):
  319. # https://shelly1l-3C6105E44F27
  320. def __init__(self, mqtt_client):
  321. super().__init__(mqtt_client, "shellies/dirk", "gui/gfw_sw_dirk",
  322. "zigbee_eg_w/light/dirk", "gui/gfw_br_dirk", "gui/gfw_ct_dirk")