Smarthome Functionen
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. from function.helpers import now, sunset_time, sunrise_time
  7. import logging
  8. import task
  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 room(object):
  15. def __init__(self, mqtt_client):
  16. self.mqtt_client = mqtt_client
  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_switch(mqtt_client, topic=topic_gui_switch)
  23. #
  24. # Callback initialisation
  25. #
  26. self.gui_switch_main_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.main_light_shelly.set_output_0_mcb)
  27. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_main_light.set_state_mcb)
  28. #
  29. self.block_all_off = False
  30. self.last_flash_data = None
  31. self.delayed_task = task.delayed(.25, self.main_light_shelly.toggle_output_0_mcb, 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 flash_main_light(self, device, key, data):
  45. if self.last_flash_data != data and data is True:
  46. logger.info("Flashing \"%s\" main light", type(self).__name__)
  47. self.main_light_shelly.toggle_output_0_mcb(device, key, data)
  48. self.delayed_task.run()
  49. self.last_flash_data = data
  50. class room_shelly_motion_sensor(room_shelly):
  51. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_motion_sensor_1, topic_motion_sensor_2=None, timer_value=config.DEFAULT_ON_TIME_LIGHT):
  52. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  53. self.timer_value = timer_value
  54. #
  55. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  56. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
  57. #
  58. self.motion_sensor_silvercrest_1 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_1)
  59. self.motion_sensor_silvercrest_1.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  60. #
  61. if topic_motion_sensor_2 is not None:
  62. self.motion_sensor_silvercrest_2 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_2)
  63. self.motion_sensor_silvercrest_2.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  64. #
  65. self.reset_timer()
  66. #
  67. cyclic_task = task.periodic(1, self.cyclic_task)
  68. cyclic_task.run()
  69. def set_motion_detected(self, device, key, data):
  70. if now() < sunrise_time(60) or now() > sunset_time(-60) or data is False:
  71. if device == self.motion_sensor_silvercrest_1:
  72. self.motion_detected_1 = data
  73. elif device == self.motion_sensor_silvercrest_2:
  74. self.motion_detected_2 = data
  75. if data is True:
  76. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.main_light_shelly.topic)
  77. self.main_light_shelly.set_output_0(True)
  78. def reload_timer(self, device, key, data):
  79. self.main_light_timer = self.timer_value
  80. def reset_timer(self, device=None, key=None, data=None):
  81. self.main_light_timer = None
  82. self.motion_detected_1 = False
  83. self.motion_detected_2 = False
  84. def cyclic_task(self, cyclic_task):
  85. if self.main_light_timer is not None:
  86. if self.main_light_timer <= 0:
  87. if not self.motion_detected_1 and not self.motion_detected_2:
  88. logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
  89. self.main_light_shelly.set_output_0(False)
  90. self.main_light_timer = None
  91. else:
  92. self.main_light_timer -= cyclic_task.cycle_time
  93. class room_shelly_tradfri_light(room_shelly):
  94. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  95. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  96. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
  97. #
  98. self.gui_br_ct_main_light = devices.nodered_gui_brightness_color_temp(mqtt_client, topic_gui_br_ct)
  99. #
  100. # Callback initialisation
  101. #
  102. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_br_ct_main_light.set_enable_mcb)
  103. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_main_light.set_brightness_mcb)
  104. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_main_light.set_color_temp_mcb)
  105. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
  106. None, self.main_light_tradfri.set_brightness_mcb)
  107. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
  108. None, self.main_light_tradfri.set_color_temp_mcb)
  109. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  110. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  111. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct)
  112. #
  113. # Callback initialisation
  114. #
  115. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  116. #
  117. self.main_light_shelly_last = None
  118. def get_initial_main_light_data(self, device, key, data):
  119. if data is True and self.main_light_shelly_last != data:
  120. self.send_init_message_main_light()
  121. self.main_light_shelly_last = data
  122. def send_init_message_main_light(self):
  123. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')