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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. try:
  10. from config import APP_NAME as ROOT_LOGGER_NAME
  11. except ImportError:
  12. ROOT_LOGGER_NAME = 'root'
  13. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  14. class first_floor_east_floor(room_shelly):
  15. def __init__(self, mqtt_client):
  16. # http://shelly1l-3C6105E4E629
  17. super().__init__(mqtt_client, "shellies/floor_madi", "gui/ffe_sw_floor")
  18. class first_floor_east_kitchen(room_shelly):
  19. # TODO: add circulation pump (switch, time)
  20. def __init__(self, mqtt_client):
  21. # http://shelly1l-8CAAB5616C01
  22. super().__init__(mqtt_client, "shellies/kitchen", "gui/ffe_sw_kitchen")
  23. class first_floor_east_dining(room_shelly):
  24. def __init__(self, mqtt_client):
  25. # http://shelly1l-84CCA8ADD055
  26. super().__init__(mqtt_client, "shellies/diningroom", "gui/ffe_sw_diningroom")
  27. self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/dining_floorlamp")
  28. if config.CHRISTMAS:
  29. self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, topic="zigbee_og_e/powerplug/aux")
  30. #
  31. self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_dining_floorlamp")
  32. #
  33. # Callback initialisation
  34. #
  35. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
  36. self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
  37. self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0,
  38. None, self.gui_switch_feedback_floorlamp)
  39. #
  40. self.main_light_shelly_last = None
  41. def floorlamp_synchronisation(self, device, key, data):
  42. if data != self.main_light_shelly_last:
  43. logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
  44. self.floorlamp_powerplug.set_output_0(data)
  45. self.main_light_shelly_last = data
  46. def gui_switch_command_floorlamp(self, device, key, data):
  47. logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
  48. self.floorlamp_powerplug.set_output_0(data)
  49. def gui_switch_feedback_floorlamp(self, device, key, data):
  50. self.gui_switch_floorlamp.set_feedback(data)
  51. class first_floor_east_sleep_madi(room_shelly_tradfri_light):
  52. def __init__(self, mqtt_client):
  53. # http://shelly1l-E8DB84A254C7
  54. super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi",
  55. "zigbee_og_e/light/sleep_madi", "gui/ffe_br_sleep_madi", "gui/ffe_ct_sleep_madi")
  56. #
  57. self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di")
  58. #
  59. self.gui_switch_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_sw_bed_light_di")
  60. self.gui_brightness_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_br_bed_light_di")
  61. self.gui_brightness_bed_light_di.enable(False)
  62. self.gui_brightness_bed_light_di.set_feedback(0)
  63. #
  64. # Callback initialisation
  65. #
  66. self.init_bed_light_functions()
  67. self.init_fade_function()
  68. # bed light
  69. def init_bed_light_functions(self):
  70. self.gui_switch_bed_light_di.add_callback(
  71. devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_bed_light_di)
  72. self.bed_light_di_tradfri.add_callback(
  73. devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_bed_light_di)
  74. self.bed_light_di_tradfri.add_callback(
  75. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_bed_light_di)
  76. self.gui_brightness_bed_light_di.add_callback(
  77. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_bed_light_di)
  78. def gui_switch_command_bed_light_di(self, device, key, data):
  79. logger.info("Switching \"%s\" bed light dirk: %s", type(self).__name__, str(data))
  80. self.bed_light_di_tradfri.set_output_0(data)
  81. def gui_switch_feedback_bed_light_di(self, device, key, data):
  82. self.gui_switch_bed_light_di.set_feedback(data)
  83. self.gui_brightness_bed_light_di.enable(data)
  84. if data is False:
  85. self.gui_brightness_bed_light_di.set_feedback(0)
  86. else:
  87. self.gui_brightness_bed_light_di.set_feedback(self.bed_light_di_tradfri.brightness)
  88. def set_gui_brightness_bed_light_di(self, device, key, data):
  89. self.gui_brightness_bed_light_di.set_feedback(data)
  90. def set_brightness_bed_light_di(self, device, key, data):
  91. logger.info("Setting brightness \"%s\" bed light dirk: %.1f", type(self).__name__, data)
  92. self.bed_light_di_tradfri.set_brightness(data)
  93. def toggle_bed_light_di(self, device, key, data):
  94. logger.info("Toggeling \"%s\" bed light dirk", type(self).__name__)
  95. self.bed_light_di_tradfri.set_output_0("toggle")
  96. # fade
  97. def init_fade_function(self):
  98. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.state_machine_last_activated_device)
  99. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0,
  100. None, self.state_machine_last_activated_device)
  101. #
  102. self.last_activated_device = None
  103. def state_machine_last_activated_device(self, device, topic, data):
  104. if data is True:
  105. self.last_activated_device = device.topic
  106. elif data is False:
  107. if device.topic == self.main_light_shelly.topic and self.bed_light_di_tradfri.output_0:
  108. self.last_activated_device = self.bed_light_di_tradfri.topic
  109. elif device.topic == self.bed_light_di_tradfri.topic and self.main_light_shelly.output_0:
  110. self.last_activated_device = self.main_light_shelly.topic
  111. else:
  112. self.last_activated_device = None
  113. self.mqtt_client.send("gui/ffe_led_main_light_sleep/set",
  114. json.dumps(self.last_activated_device == self.main_light_shelly.topic))
  115. self.mqtt_client.send("gui/ffe_led_bed_light_sleep_di/set",
  116. json.dumps(self.last_activated_device == self.bed_light_di_tradfri.topic))
  117. def fade_light(self, device, topic, data):
  118. if self.last_activated_device == self.main_light_shelly.topic:
  119. target = self.main_light_tradfri
  120. elif self.last_activated_device == self.bed_light_di_tradfri.topic:
  121. target = self.bed_light_di_tradfri
  122. else:
  123. return
  124. if (data == 'brightness_up_hold'):
  125. logger.info("Increasing brightness \"%s\" %s", type(self).__name__, target.topic)
  126. target.brightness_inc()
  127. elif (data == 'brightness_down_hold'):
  128. logger.info("Decreasing brightness \"%s\" %s", type(self).__name__, target.topic)
  129. target.brightness_dec()
  130. elif (data.startswith('brightness') and data.endswith('release')):
  131. logger.info("Stoping brightness change \"%s\" %s", type(self).__name__, target.topic)
  132. target.brightness_stop()
  133. class first_floor_east_living(room_shelly_tradfri_light):
  134. def __init__(self, mqtt_client):
  135. # http://shelly1l-3C6105E3F910
  136. super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom",
  137. "zigbee_og_e/light/livingroom", "gui/ffe_br_livingroom", "gui/ffe_ct_livingroom")
  138. for i in range(1, 7):
  139. setattr(self, 'floorlamp_tradfri_%d' % i,
  140. devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/living_floorlamp_%d" % i))
  141. #
  142. self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_living_floorlamp")
  143. self.gui_brightness_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_br_livingroom_floorlamp")
  144. self.gui_brightness_floorlamp.enable(False)
  145. self.gui_brightness_floorlamp.set_feedback(0)
  146. self.gui_color_temp_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_ct_livingroom_floorlamp")
  147. self.gui_color_temp_floorlamp.enable(False)
  148. self.gui_color_temp_floorlamp.set_feedback(0)
  149. #
  150. # Callback initialisation
  151. #
  152. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
  153. self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
  154. self.floorlamp_tradfri_1.add_callback(
  155. devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp)
  156. self.floorlamp_tradfri_1.add_callback(
  157. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_floorlamp)
  158. self.floorlamp_tradfri_1.add_callback(
  159. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_floorlamp)
  160. self.gui_brightness_floorlamp.add_callback(
  161. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_floorlamp)
  162. self.gui_color_temp_floorlamp.add_callback(
  163. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_floorlamp)
  164. #
  165. self.main_light_shelly_last = None
  166. def __floorlamp_devices__(self):
  167. rv = []
  168. for i in range(1, 7):
  169. rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
  170. return rv
  171. def floorlamp_synchronisation(self, device, key, data):
  172. if data != self.main_light_shelly_last:
  173. logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
  174. for device in self.__floorlamp_devices__():
  175. device.set_output_0(data)
  176. self.main_light_shelly_last = data
  177. def gui_switch_command_floorlamp(self, device, key, data):
  178. logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
  179. for device in self.__floorlamp_devices__():
  180. device.set_output_0(data)
  181. def gui_switch_feedback_floorlamp(self, device, key, data):
  182. self.gui_switch_floorlamp.set_feedback(data)
  183. self.gui_brightness_floorlamp.enable(data)
  184. self.gui_color_temp_floorlamp.enable(data)
  185. if data is False:
  186. self.gui_brightness_floorlamp.set_feedback(0)
  187. self.gui_color_temp_floorlamp.set_feedback(0)
  188. else:
  189. self.gui_brightness_floorlamp.set_feedback(self.floorlamp_tradfri_1.brightness)
  190. self.gui_color_temp_floorlamp.set_feedback(self.floorlamp_tradfri_1.color_temp / 10)
  191. def set_gui_brightness_floorlamp(self, device, key, data):
  192. self.gui_brightness_floorlamp.set_feedback(data)
  193. def set_gui_color_temp_floorlamp(self, device, key, data):
  194. self.gui_color_temp_floorlamp.set_feedback(data / 10)
  195. def set_brightness_floorlamp(self, device, key, data):
  196. logger.info("Setting brightness \"%s\" floorlamp: %.1f", type(self).__name__, data)
  197. for device in self.__floorlamp_devices__():
  198. device.set_brightness(data)
  199. def set_color_temp_floorlamp(self, device, key, data):
  200. logger.info("Setting color_temp \"%s\" floorlamp: %.1f", type(self).__name__, data)
  201. for device in self.__floorlamp_devices__():
  202. device.set_color_temp(data * 10)