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.

rooms.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.motion_detected_1 = False
  87. self.motion_detected_2 = False
  88. def cyclic_task(self, cyclic_task):
  89. if self.main_light_timer is not None:
  90. if self.main_light_timer <= 0:
  91. logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
  92. self.main_light_shelly.set_output_0(False)
  93. self.main_light_timer = None
  94. self.gui_main_light.set_timer('-')
  95. else:
  96. self.gui_main_light.set_timer(self.main_light_timer)
  97. if (self.motion_detected_1 or self.motion_detected_2) and self.main_light_timer <= self.timer_value / 10:
  98. self.main_light_timer = self.timer_value / 10
  99. else:
  100. self.main_light_timer -= cyclic_task.cycle_time
  101. class room_shelly_tradfri_light(room_shelly):
  102. def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_tradfri_light):
  103. super().__init__(mqtt_client, topic_shelly, topic_gui)
  104. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
  105. #
  106. # Callback initialisation
  107. #
  108. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_main_light.set_state_mcb)
  109. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_main_light.set_enable_mcb)
  110. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_main_light.set_brightness_mcb)
  111. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_main_light.set_color_temp_mcb)
  112. self.gui_main_light.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.main_light_tradfri.set_brightness_mcb)
  113. self.gui_main_light.add_callback(devices.nodered_gui_light.KEY_COLOR_TEMP, None, self.main_light_tradfri.set_color_temp_mcb)
  114. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  115. def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_tradfri_light):
  116. super().__init__(mqtt_client, topic_shelly, topic_gui, topic_tradfri_light)
  117. #
  118. # Callback initialisation
  119. #
  120. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  121. #
  122. self.main_light_shelly_last = None
  123. def get_initial_main_light_data(self, device, key, data):
  124. if data is True and self.main_light_shelly_last != data:
  125. self.send_init_message_main_light()
  126. self.main_light_shelly_last = data
  127. def send_init_message_main_light(self):
  128. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')