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

rooms.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import devices
  5. import logging
  6. import task
  7. try:
  8. from config import APP_NAME as ROOT_LOGGER_NAME
  9. except ImportError:
  10. ROOT_LOGGER_NAME = 'root'
  11. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  12. class room(object):
  13. def __init__(self, mqtt_client):
  14. self.mqtt_client = mqtt_client
  15. def gui_switch_feedback(self, device, key, data):
  16. self.gui_switch_main_light.set_feedback(data)
  17. class room_shelly(room):
  18. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
  19. super().__init__(mqtt_client)
  20. self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
  21. #
  22. self.gui_switch_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_switch)
  23. #
  24. # Callback initialisation
  25. #
  26. self.gui_switch_main_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command)
  27. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_feedback)
  28. #
  29. self.block_all_off = False
  30. self.last_flash_data = None
  31. self.delayed_task = task.delayed(.25, self.toggle_main_light, None, None, None)
  32. def all_off(self, device=None, key=None, data=None):
  33. if not self.block_all_off:
  34. logger.info("Switching all off \"%s\"", type(self).__name__)
  35. self.main_light_shelly.set_output_0(False)
  36. self.main_light_shelly.set_output_1(False)
  37. self.block_all_off = False
  38. def all_off_feedback(self, device=None, key=None, data=None):
  39. logger.info("Flashing \"%s\" main light", type(self).__name__)
  40. if self.main_light_shelly.output_0 is False:
  41. self.main_light_shelly.set_output_0(True)
  42. self.block_all_off = True
  43. self.delayed_task.run()
  44. def gui_switch_command(self, device, key, data):
  45. logger.info("Switching \"%s\" main light: %s", type(self).__name__, str(data))
  46. self.main_light_shelly.set_output_0(data)
  47. def toggle_main_light(self, device, key, data):
  48. logger.info("Toggeling \"%s\" main light", type(self).__name__)
  49. self.main_light_shelly.set_output_0("toggle")
  50. def flash_main_light(self, device, key, data):
  51. if self.last_flash_data != data and data is True:
  52. logger.info("Flashing \"%s\" main light", type(self).__name__)
  53. self.toggle_main_light(device, key, data)
  54. self.delayed_task.run()
  55. self.last_flash_data = data
  56. class room_shelly_tradfri_light(room_shelly):
  57. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  58. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  59. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic=topic_tradfri_light)
  60. #
  61. self.gui_brightness_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_brightness)
  62. self.gui_brightness_main_light.enable(False)
  63. self.gui_brightness_main_light.set_feedback(0)
  64. self.gui_color_temp_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_color_temp)
  65. self.gui_color_temp_main_light.enable(False)
  66. self.gui_color_temp_main_light.set_feedback(0)
  67. #
  68. # Callback initialisation
  69. #
  70. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.enable_brightness_n_colortemp)
  71. self.main_light_tradfri.add_callback(
  72. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_main_light)
  73. self.main_light_tradfri.add_callback(
  74. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_main_light)
  75. self.gui_brightness_main_light.add_callback(
  76. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_main_light)
  77. self.gui_color_temp_main_light.add_callback(
  78. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_main_light)
  79. def enable_brightness_n_colortemp(self, devive, key, data):
  80. self.gui_brightness_main_light.enable(data)
  81. self.gui_color_temp_main_light.enable(data)
  82. if data is False:
  83. self.gui_brightness_main_light.set_feedback(0)
  84. self.gui_color_temp_main_light.set_feedback(0)
  85. else:
  86. self.gui_brightness_main_light.set_feedback(self.main_light_tradfri.brightness)
  87. self.gui_color_temp_main_light.set_feedback(self.main_light_tradfri.color_temp / 10)
  88. def set_gui_brightness_main_light(self, device, key, data):
  89. self.gui_brightness_main_light.set_feedback(data)
  90. def set_gui_color_temp_main_light(self, device, key, data):
  91. self.gui_color_temp_main_light.set_feedback(data / 10)
  92. def set_brightness_main_light(self, device, key, data):
  93. logger.info("Setting brightness \"%s\" main light: %.1f", type(self).__name__, data)
  94. self.main_light_tradfri.set_brightness(data)
  95. def set_color_temp_main_light(self, device, key, data):
  96. logger.info("Setting color_temp \"%s\" main light: %.1f", type(self).__name__, data)
  97. self.main_light_tradfri.set_color_temp(data * 10)
  98. def fade_light(self, device, topic, data):
  99. if (data == 'brightness_up_hold'):
  100. logger.info("Increasing brightness \"%s\" main light", type(self).__name__)
  101. self.main_light_tradfri.brightness_inc()
  102. elif (data == 'brightness_down_hold'):
  103. logger.info("Decreasing brightness \"%s\" main light", type(self).__name__)
  104. self.main_light_tradfri.brightness_dec()
  105. elif (data.startswith('brightness') and data.endswith('release')):
  106. logger.info("Stoping brightness change \"%s\" main light", type(self).__name__)
  107. self.main_light_tradfri.brightness_stop()
  108. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  109. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  110. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp)
  111. #
  112. # Callback initialisation
  113. #
  114. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  115. #
  116. self.main_light_shelly_last = None
  117. def get_initial_main_light_data(self, device, key, data):
  118. if data is True and self.main_light_shelly_last is not True:
  119. self.send_init_message_main_light()
  120. self.main_light_shelly_last = data
  121. def send_init_message_main_light(self):
  122. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')