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.

first_floor_east.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. import json
  7. import logging
  8. from function.rooms import room_shelly, room_shelly_tradfri_light
  9. from function.helpers import changed_value_indicator
  10. try:
  11. from config import APP_NAME as ROOT_LOGGER_NAME
  12. except ImportError:
  13. ROOT_LOGGER_NAME = 'root'
  14. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  15. class first_floor_east_floor(room_shelly):
  16. def __init__(self, mqtt_client):
  17. # http://shelly1l-3C6105E4E629
  18. super().__init__(mqtt_client, "shellies/floor_madi", "gui/ffe_sw_floor")
  19. class first_floor_east_kitchen(room_shelly):
  20. # TODO: add circulation pump (switch, time)
  21. def __init__(self, mqtt_client):
  22. # http://shelly1l-8CAAB5616C01
  23. super().__init__(mqtt_client, "shellies/kitchen", "gui/ffe_sw_kitchen")
  24. class first_floor_east_dining(room_shelly):
  25. def __init__(self, mqtt_client):
  26. # http://shelly1l-84CCA8ADD055
  27. super().__init__(mqtt_client, "shellies/diningroom", "gui/ffe_sw_diningroom")
  28. self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/dining_floorlamp")
  29. if config.CHRISTMAS:
  30. self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, topic="zigbee_og_e/powerplug/aux")
  31. #
  32. self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_dining_floorlamp")
  33. #
  34. # Callback initialisation
  35. #
  36. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
  37. self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
  38. self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0,
  39. None, self.gui_switch_feedback_floorlamp)
  40. #
  41. self.cvi = changed_value_indicator()
  42. def all_off(self, device=None, key=None, data=None):
  43. super().all_off(device, key, data)
  44. self.floorlamp_powerplug.set_output_0(False)
  45. def floorlamp_synchronisation(self, device, key, data):
  46. if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
  47. logger.info("Syncing \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
  48. self.floorlamp_powerplug.set_output_0(data)
  49. def gui_switch_command_floorlamp(self, device, key, data):
  50. logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
  51. self.floorlamp_powerplug.set_output_0(data)
  52. def gui_switch_feedback_floorlamp(self, device, key, data):
  53. self.gui_switch_floorlamp.set_feedback(data)
  54. class first_floor_east_sleep_madi(room_shelly_tradfri_light):
  55. def __init__(self, mqtt_client):
  56. # http://shelly1l-E8DB84A254C7
  57. super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi",
  58. "zigbee_og_e/light/sleep_madi", "gui/ffe_br_sleep_madi", "gui/ffe_ct_sleep_madi")
  59. #
  60. self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di")
  61. #
  62. self.gui_switch_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_sw_bed_light_di")
  63. self.gui_brightness_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_br_bed_light_di")
  64. self.gui_brightness_bed_light_di.enable(False)
  65. self.gui_brightness_bed_light_di.set_feedback(0)
  66. #
  67. self.button_tradfri = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east")
  68. #
  69. # Callback initialisation
  70. #
  71. self.init_bed_light_functions()
  72. self.init_fade_function()
  73. #
  74. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
  75. self.toggle_main_light)
  76. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP,
  77. self.toggle_bed_light_di)
  78. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN,
  79. self.toggle_bed_light_di)
  80. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None,
  81. self.fade_light)
  82. def all_off(self, device=None, key=None, data=None):
  83. super().all_off(device, key, data)
  84. self.bed_light_di_tradfri.set_output_0(False)
  85. # bed light
  86. def init_bed_light_functions(self):
  87. self.gui_switch_bed_light_di.add_callback(
  88. devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_bed_light_di)
  89. self.bed_light_di_tradfri.add_callback(
  90. devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_bed_light_di)
  91. self.bed_light_di_tradfri.add_callback(
  92. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_bed_light_di)
  93. self.gui_brightness_bed_light_di.add_callback(
  94. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_bed_light_di)
  95. def gui_switch_command_bed_light_di(self, device, key, data):
  96. logger.info("Switching \"%s\" bed light dirk: %s", type(self).__name__, str(data))
  97. self.bed_light_di_tradfri.set_output_0(data)
  98. def gui_switch_feedback_bed_light_di(self, device, key, data):
  99. self.gui_switch_bed_light_di.set_feedback(data)
  100. self.gui_brightness_bed_light_di.enable(data)
  101. if data is False:
  102. self.gui_brightness_bed_light_di.set_feedback(0)
  103. else:
  104. self.gui_brightness_bed_light_di.set_feedback(self.bed_light_di_tradfri.brightness)
  105. def set_gui_brightness_bed_light_di(self, device, key, data):
  106. self.gui_brightness_bed_light_di.set_feedback(data)
  107. def set_brightness_bed_light_di(self, device, key, data):
  108. logger.info("Setting brightness \"%s\" bed light dirk: %.1f", type(self).__name__, data)
  109. self.bed_light_di_tradfri.set_brightness(data)
  110. def toggle_bed_light_di(self, device, key, data):
  111. logger.info("Toggeling \"%s\" bed light dirk", type(self).__name__)
  112. self.bed_light_di_tradfri.set_output_0("toggle")
  113. # fade
  114. def init_fade_function(self):
  115. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.state_machine_last_activated_device)
  116. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0,
  117. None, self.state_machine_last_activated_device)
  118. #
  119. self.last_activated_device = None
  120. def state_machine_last_activated_device(self, device, topic, data):
  121. if data is True:
  122. self.last_activated_device = device.topic
  123. elif data is False:
  124. if device.topic == self.main_light_shelly.topic and self.bed_light_di_tradfri.output_0:
  125. self.last_activated_device = self.bed_light_di_tradfri.topic
  126. elif device.topic == self.bed_light_di_tradfri.topic and self.main_light_shelly.output_0:
  127. self.last_activated_device = self.main_light_shelly.topic
  128. else:
  129. self.last_activated_device = None
  130. self.mqtt_client.send("gui/ffe_led_main_light_sleep/set",
  131. json.dumps(self.last_activated_device == self.main_light_shelly.topic))
  132. self.mqtt_client.send("gui/ffe_led_bed_light_sleep_di/set",
  133. json.dumps(self.last_activated_device == self.bed_light_di_tradfri.topic))
  134. def fade_light(self, device, topic, data):
  135. if self.last_activated_device == self.main_light_shelly.topic:
  136. target = self.main_light_tradfri
  137. elif self.last_activated_device == self.bed_light_di_tradfri.topic:
  138. target = self.bed_light_di_tradfri
  139. else:
  140. return
  141. if (data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG):
  142. logger.info("Increasing brightness \"%s\" %s", type(self).__name__, target.topic)
  143. target.default_inc()
  144. elif (data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG):
  145. logger.info("Decreasing brightness \"%s\" %s", type(self).__name__, target.topic)
  146. target.default_dec()
  147. elif (data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]):
  148. logger.info("Stoping brightness change \"%s\" %s", type(self).__name__, target.topic)
  149. target.default_stop()
  150. class first_floor_east_living(room_shelly_tradfri_light):
  151. def __init__(self, mqtt_client):
  152. # http://shelly1l-3C6105E3F910
  153. super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom",
  154. "zigbee_og_e/light/livingroom", "gui/ffe_br_livingroom", "gui/ffe_ct_livingroom")
  155. for i in range(1, 7):
  156. setattr(self, 'floorlamp_tradfri_%d' % i,
  157. devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/living_floorlamp_%d" % i))
  158. if config.CHRISTMAS:
  159. self.powerplug_xmas_tree = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/xmas-tree")
  160. #
  161. self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_living_floorlamp")
  162. self.gui_brightness_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_br_livingroom_floorlamp")
  163. self.gui_brightness_floorlamp.enable(False)
  164. self.gui_brightness_floorlamp.set_feedback(0)
  165. self.gui_color_temp_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_ct_livingroom_floorlamp")
  166. self.gui_color_temp_floorlamp.enable(False)
  167. self.gui_color_temp_floorlamp.set_feedback(0)
  168. #
  169. if config.CHRISTMAS:
  170. self.gui_switch_xmas_tree = devices.nodered_gui(mqtt_client, "gui/ffe_sw_livingroom_xmas_tree")
  171. #
  172. # Callback initialisation
  173. #
  174. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
  175. self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
  176. self.floorlamp_tradfri_1.add_callback(
  177. devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp)
  178. self.floorlamp_tradfri_1.add_callback(
  179. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_floorlamp)
  180. self.floorlamp_tradfri_1.add_callback(
  181. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_floorlamp)
  182. self.gui_brightness_floorlamp.add_callback(
  183. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_floorlamp)
  184. self.gui_color_temp_floorlamp.add_callback(
  185. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_floorlamp)
  186. #
  187. if config.CHRISTMAS:
  188. self.powerplug_xmas_tree.add_callback(
  189. devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_action)
  190. self.gui_switch_xmas_tree.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_xmas)
  191. #
  192. self.cvi = changed_value_indicator()
  193. def all_off(self, device=None, key=None, data=None):
  194. super().all_off(device, key, data)
  195. for floorlamp in self.__floorlamp_devices__():
  196. floorlamp.set_output_0(False)
  197. def __floorlamp_devices__(self):
  198. rv = []
  199. for i in range(1, 7):
  200. rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
  201. return rv
  202. def floorlamp_synchronisation(self, device, key, data):
  203. if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
  204. logger.info("Syncing \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
  205. for device in self.__floorlamp_devices__():
  206. device.set_output_0(data)
  207. def gui_switch_command_floorlamp(self, device, key, data):
  208. logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
  209. for device in self.__floorlamp_devices__():
  210. device.set_output_0(data)
  211. def gui_switch_feedback_floorlamp(self, device, key, data):
  212. self.gui_switch_floorlamp.set_feedback(data)
  213. self.gui_brightness_floorlamp.enable(data)
  214. self.gui_color_temp_floorlamp.enable(data)
  215. if data is False:
  216. self.gui_brightness_floorlamp.set_feedback(0)
  217. self.gui_color_temp_floorlamp.set_feedback(0)
  218. else:
  219. self.gui_brightness_floorlamp.set_feedback(self.floorlamp_tradfri_1.brightness)
  220. self.gui_color_temp_floorlamp.set_feedback(self.floorlamp_tradfri_1.color_temp / 10)
  221. def set_gui_brightness_floorlamp(self, device, key, data):
  222. self.gui_brightness_floorlamp.set_feedback(data)
  223. def set_gui_color_temp_floorlamp(self, device, key, data):
  224. self.gui_color_temp_floorlamp.set_feedback(data / 10)
  225. def set_brightness_floorlamp(self, device, key, data):
  226. logger.info("Setting brightness \"%s\" floorlamp: %.1f", type(self).__name__, data)
  227. for device in self.__floorlamp_devices__():
  228. device.set_brightness(data)
  229. def set_color_temp_floorlamp(self, device, key, data):
  230. logger.info("Setting color_temp \"%s\" floorlamp: %.1f", type(self).__name__, data)
  231. for device in self.__floorlamp_devices__():
  232. device.set_color_temp(data * 10)
  233. def powerplug_xmas_action(self, device, key, data):
  234. self.gui_switch_xmas_tree.set_feedback(data)
  235. def gui_switch_command_xmas(self, device, key, data):
  236. logger.info("Switching \"%s\" xmas-tree: %s", type(self).__name__, data)
  237. self.powerplug_xmas_tree.set_output_0(data)