Smarthome Functionen
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

rooms.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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):
  19. super().__init__(mqtt_client)
  20. self.main_light_shelly = devices.shelly(mqtt_client, topic_shelly)
  21. #
  22. self.gui_main_light = devices.nodered_gui_light(mqtt_client, topic_gui)
  23. #
  24. # Callback initialisation
  25. #
  26. self.gui_main_light.add_callback(devices.nodered_gui_light.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_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, topic_motion_sensor_1, topic_motion_sensor_2=None, timer_value=30):
  52. super().__init__(mqtt_client, topic_shelly, topic_gui)
  53. self.timer_value = timer_value
  54. self.motion_detected_1 = False
  55. self.motion_detected_2 = False
  56. #
  57. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  58. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
  59. #
  60. self.motion_sensor_silvercrest_1 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_1)
  61. self.motion_sensor_silvercrest_1.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  62. #
  63. if topic_motion_sensor_2 is not None:
  64. self.motion_sensor_silvercrest_2 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_2)
  65. self.motion_sensor_silvercrest_2.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  66. #
  67. self.reset_timer()
  68. #
  69. cyclic_task = task.periodic(1, self.cyclic_task)
  70. cyclic_task.run()
  71. def set_motion_detected(self, device, key, data):
  72. if device == self.motion_sensor_silvercrest_1:
  73. self.motion_detected_1 = data
  74. elif device == self.motion_sensor_silvercrest_2:
  75. self.motion_detected_2 = data
  76. self.gui_main_light.set_led(devices.nodered_gui_light.KEY_LED_0, self.motion_detected_1)
  77. self.gui_main_light.set_led(devices.nodered_gui_light.KEY_LED_1, self.motion_detected_2)
  78. if now() < sunrise_time(60) or now() > sunset_time(-60):
  79. if data is True:
  80. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.main_light_shelly.topic)
  81. self.main_light_shelly.set_output_0(True)
  82. def reload_timer(self, device, key, data):
  83. self.main_light_timer = self.timer_value
  84. def reset_timer(self, device=None, key=None, data=None):
  85. self.main_light_timer = None
  86. self.gui_main_light.set_timer('-')
  87. def cyclic_task(self, cyclic_task):
  88. if self.main_light_timer is not None:
  89. if self.main_light_timer <= 0:
  90. logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
  91. self.main_light_shelly.set_output_0(False)
  92. self.main_light_timer = None
  93. self.gui_main_light.set_timer('-')
  94. else:
  95. self.gui_main_light.set_timer(self.main_light_timer)
  96. if (self.motion_detected_1 or self.motion_detected_2) and self.main_light_timer <= self.timer_value / 10:
  97. self.main_light_timer = self.timer_value / 10
  98. else:
  99. self.main_light_timer -= cyclic_task.cycle_time
  100. class room_shelly_tradfri_light(room_shelly):
  101. def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_tradfri_light):
  102. super().__init__(mqtt_client, topic_shelly, topic_gui)
  103. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
  104. #
  105. # Callback initialisation
  106. #
  107. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_main_light.set_state_mcb)
  108. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_main_light.set_enable_mcb)
  109. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_main_light.set_brightness_mcb)
  110. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_main_light.set_color_temp_mcb)
  111. self.gui_main_light.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.main_light_tradfri.set_brightness_mcb)
  112. self.gui_main_light.add_callback(devices.nodered_gui_light.KEY_COLOR_TEMP, None, self.main_light_tradfri.set_color_temp_mcb)
  113. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  114. def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_tradfri_light):
  115. super().__init__(mqtt_client, topic_shelly, topic_gui, topic_tradfri_light)
  116. #
  117. # Callback initialisation
  118. #
  119. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  120. #
  121. self.main_light_shelly_last = None
  122. def get_initial_main_light_data(self, device, key, data):
  123. if data is True and self.main_light_shelly_last != data:
  124. self.send_init_message_main_light()
  125. self.main_light_shelly_last = data
  126. def send_init_message_main_light(self):
  127. self.main_light_tradfri.request_data()