Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 19KB

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