Smarthome Functionen
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

first_floor_east.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_motion_sensor, 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_motion_sensor):
  16. def __init__(self, mqtt_client):
  17. # http://shelly1l-3C6105E4E629
  18. super().__init__(mqtt_client, "shellies/ffe/floor/main_light", "gui/ffe/floor/main_light/switch", "zigbee/ffe/floor/motion_sensor")
  19. class first_floor_east_kitchen(room_shelly):
  20. def __init__(self, mqtt_client):
  21. # http://shelly1l-8CAAB5616C01
  22. super().__init__(mqtt_client, "shellies/ffe/kitchen/main_light", "gui/ffe/kitchen/main_light/switch")
  23. class first_floor_east_dining(room_shelly):
  24. def __init__(self, mqtt_client):
  25. # http://shelly1l-84CCA8ADD055
  26. super().__init__(mqtt_client, "shellies/ffe/diningroom/main_light", "gui/ffe/diningroom/main_light/switch")
  27. self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, "zigbee/ffe/dinigroom/powerplug_floorlamp")
  28. if config.CHRISTMAS:
  29. self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, topic="zigbee/ffe/dinigroom/garland")
  30. #
  31. self.gui_switch_floorlamp = devices.nodered_gui_switch(mqtt_client, topic="gui/ffe/diningroom/floorlamp/switch")
  32. #
  33. # Callback initialisation
  34. #
  35. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_powerplug.set_output_0_mcb, True)
  36. self.gui_switch_floorlamp.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.floorlamp_powerplug.set_output_0_mcb)
  37. self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_switch_floorlamp.set_state_mcb)
  38. def all_off(self, device=None, key=None, data=None):
  39. super().all_off(device, key, data)
  40. self.floorlamp_powerplug.set_output_0(False)
  41. if config.CHRISTMAS:
  42. self.garland_powerplug.set_output_0(False)
  43. class first_floor_east_sleep(room_shelly_tradfri_light):
  44. def __init__(self, mqtt_client):
  45. # http://shelly1l-E8DB84A254C7
  46. super().__init__(mqtt_client, "shellies/ffe/sleep/main_light", "gui/ffe/sleep/main_light/switch",
  47. "zigbee/ffe/sleep/main_light", "gui/ffe/sleep/main_light/br_ct")
  48. #
  49. self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, "zigbee/ffe/sleep/bed_light_di")
  50. #
  51. self.gui_switch_bed_light_di = devices.nodered_gui_switch(mqtt_client, "gui/ffe/sleep/bed_light_di/switch")
  52. self.gui_br_ct_bed_light_di = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/ffe/sleep/bed_light_di/br_ct")
  53. self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, "gui/ffe/sleep/active_device_state/led")
  54. #
  55. self.button_tradfri = devices.tradfri_button(mqtt_client, "zigbee/ffe/sleep/input_device")
  56. #
  57. # Callback initialisation
  58. #
  59. # button
  60. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
  61. self.main_light_shelly.toggle_output_0_mcb)
  62. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP,
  63. self.bed_light_di_tradfri.toggle_output_0_mcb)
  64. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN,
  65. self.bed_light_di_tradfri.toggle_output_0_mcb)
  66. self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.fade_light)
  67. # bed light
  68. # switch
  69. self.gui_switch_bed_light_di.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.bed_light_di_tradfri.set_output_0_mcb)
  70. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_bed_light_di.set_state_mcb)
  71. # brightness and color temperature
  72. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_bed_light_di.set_enable_mcb)
  73. self.gui_br_ct_bed_light_di.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
  74. None, self.bed_light_di_tradfri.set_brightness_mcb)
  75. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_bed_light_di.set_enable_mcb)
  76. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_bed_light_di.set_brightness_mcb)
  77. # fade function
  78. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.state_machine_last_activated_device)
  79. self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.state_machine_last_activated_device)
  80. self.last_activated_device = None
  81. def all_off(self, device=None, key=None, data=None):
  82. super().all_off(device, key, data)
  83. self.bed_light_di_tradfri.set_output_0(False)
  84. def state_machine_last_activated_device(self, device, topic, data):
  85. if data is True:
  86. self.last_activated_device = device.topic
  87. elif data is False:
  88. if device.topic == self.main_light_shelly.topic and self.bed_light_di_tradfri.output_0:
  89. self.last_activated_device = self.bed_light_di_tradfri.topic
  90. elif device.topic == self.bed_light_di_tradfri.topic and self.main_light_shelly.output_0:
  91. self.last_activated_device = self.main_light_shelly.topic
  92. else:
  93. self.last_activated_device = None
  94. self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_0, self.last_activated_device == self.main_light_shelly.topic)
  95. self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_1, self.last_activated_device == self.bed_light_di_tradfri.topic)
  96. def fade_light(self, device, topic, data):
  97. if self.last_activated_device == self.main_light_shelly.topic:
  98. target = self.main_light_tradfri
  99. elif self.last_activated_device == self.bed_light_di_tradfri.topic:
  100. target = self.bed_light_di_tradfri
  101. else:
  102. return
  103. if (data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG):
  104. logger.info("Increasing brightness \"%s\" %s", type(self).__name__, target.topic)
  105. target.default_inc()
  106. elif (data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG):
  107. logger.info("Decreasing brightness \"%s\" %s", type(self).__name__, target.topic)
  108. target.default_dec()
  109. elif (data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]):
  110. logger.info("Stoping brightness change \"%s\" %s", type(self).__name__, target.topic)
  111. target.default_stop()
  112. class first_floor_east_living(room_shelly_tradfri_light):
  113. def __init__(self, mqtt_client):
  114. # http://shelly1l-3C6105E3F910
  115. super().__init__(mqtt_client, "shellies/ffe/livingroom/main_light", "gui/ffe/livingroom/main_light/switch",
  116. "zigbee/ffe/livingroom/main_light", "gui/ffe/livingroom/main_light/br_ct")
  117. for i in range(1, 7):
  118. setattr(self, 'floorlamp_tradfri_%d' % i, devices.tradfri_light(mqtt_client, "zigbee/ffe/livingroom/floorlamp_%d" % i))
  119. #
  120. if config.CHRISTMAS:
  121. self.powerplug_xmas_tree = devices.silvercrest_powerplug(mqtt_client, "zigbee/ffe/livingroom/powerplug_xmas-tree")
  122. self.powerplug_xmas_star = devices.silvercrest_powerplug(mqtt_client, "zigbee/ffe/livingroom/powerplug_xmas-star")
  123. #
  124. self.gui_switch_floorlamp = devices.nodered_gui_switch(mqtt_client, "gui/ffe/livingroom/floorlamp/switch")
  125. self.gui_br_ct_floorlamp = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/ffe/livingroom/floorlamp/br_ct")
  126. #
  127. if config.CHRISTMAS:
  128. self.gui_switch_xmas_tree = devices.nodered_gui_switch(mqtt_client, "gui/ffe/livingroom/xmas_tree/switch")
  129. #
  130. # Callback initialisation
  131. #
  132. # floor lamp
  133. for device in self.__floorlamp_devices__():
  134. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, device.set_output_0_mcb, True)
  135. self.gui_switch_floorlamp.add_callback(devices.nodered_gui_switch.KEY_STATE, None, device.set_output_0_mcb)
  136. self.gui_br_ct_floorlamp.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS, None, device.set_brightness_mcb)
  137. self.gui_br_ct_floorlamp.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP, None, device.set_color_temp_mcb)
  138. self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_floorlamp.set_state_mcb)
  139. self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_floorlamp.set_enable_mcb)
  140. self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_floorlamp.set_brightness_mcb)
  141. self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_floorlamp.set_color_temp_mcb)
  142. #
  143. if config.CHRISTMAS:
  144. self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_switch_xmas_tree.set_state_mcb)
  145. self.gui_switch_xmas_tree.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_xmas_tree.set_output_0_mcb)
  146. #
  147. self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_star.set_output_0_mcb)
  148. #
  149. self.cvi = changed_value_indicator()
  150. def all_off(self, device=None, key=None, data=None):
  151. super().all_off(device, key, data)
  152. for floorlamp in self.__floorlamp_devices__():
  153. floorlamp.set_output_0(False)
  154. if config.CHRISTMAS:
  155. self.powerplug_xmas_tree.set_output_0(False)
  156. self.powerplug_xmas_star.set_output_0(False)
  157. def __floorlamp_devices__(self):
  158. rv = []
  159. for i in range(1, 7):
  160. rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
  161. return rv