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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import devices
  5. from function.helpers import now, sunset_time, sunrise_time
  6. import logging
  7. import task
  8. try:
  9. from config import APP_NAME as ROOT_LOGGER_NAME
  10. except ImportError:
  11. ROOT_LOGGER_NAME = 'root'
  12. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  13. class room(object):
  14. def __init__(self, mqtt_client):
  15. self.mqtt_client = mqtt_client
  16. class room_shelly(room):
  17. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
  18. super().__init__(mqtt_client)
  19. self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
  20. #
  21. self.gui_switch_main_light = devices.nodered_gui_switch(mqtt_client, topic=topic_gui_switch)
  22. #
  23. # Callback initialisation
  24. #
  25. self.gui_switch_main_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.main_light_shelly.set_output_0_mcb)
  26. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_main_light.set_state_mcb)
  27. #
  28. self.block_all_off = False
  29. self.last_flash_data = None
  30. self.delayed_task = task.delayed(.25, self.main_light_shelly.toggle_output_0_mcb, None, None, None)
  31. def all_off(self, device=None, key=None, data=None):
  32. if not self.block_all_off:
  33. logger.info("Switching all off \"%s\"", type(self).__name__)
  34. self.main_light_shelly.set_output_0(False)
  35. self.main_light_shelly.set_output_1(False)
  36. self.block_all_off = False
  37. def all_off_feedback(self, device=None, key=None, data=None):
  38. logger.info("Flashing \"%s\" main light", type(self).__name__)
  39. if self.main_light_shelly.output_0 is False:
  40. self.main_light_shelly.set_output_0(True)
  41. self.block_all_off = True
  42. self.delayed_task.run()
  43. def flash_main_light(self, device, key, data):
  44. if self.last_flash_data != data and data is True:
  45. logger.info("Flashing \"%s\" main light", type(self).__name__)
  46. self.main_light_shelly.toggle_output_0_mcb(device, key, data)
  47. self.delayed_task.run()
  48. self.last_flash_data = data
  49. class room_shelly_motion_sensor(room_shelly):
  50. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_motion_sensor_1, topic_motion_sensor_2=None, timer_value=30):
  51. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  52. self.timer_value = timer_value
  53. #
  54. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  55. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
  56. #
  57. self.motion_sensor_silvercrest_1 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_1)
  58. self.motion_sensor_silvercrest_1.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  59. #
  60. if topic_motion_sensor_2 is not None:
  61. self.motion_sensor_silvercrest_2 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_2)
  62. self.motion_sensor_silvercrest_2.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  63. #
  64. self.reset_timer()
  65. #
  66. cyclic_task = task.periodic(1, self.cyclic_task)
  67. cyclic_task.run()
  68. def set_motion_detected(self, device, key, data):
  69. if now() < sunrise_time(60) or now() > sunset_time(-60) or data is False:
  70. if device == self.motion_sensor_silvercrest_1:
  71. self.motion_detected_1 = data
  72. elif device == self.motion_sensor_silvercrest_2:
  73. self.motion_detected_2 = data
  74. if data is True:
  75. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.main_light_shelly.topic)
  76. self.main_light_shelly.set_output_0(True)
  77. def reload_timer(self, device, key, data):
  78. self.main_light_timer = self.timer_value
  79. def reset_timer(self, device=None, key=None, data=None):
  80. self.main_light_timer = None
  81. self.motion_detected_1 = False
  82. self.motion_detected_2 = False
  83. def cyclic_task(self, cyclic_task):
  84. if self.main_light_timer is not None:
  85. if self.main_light_timer <= 0:
  86. if not self.motion_detected_1 and not self.motion_detected_2:
  87. logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
  88. self.main_light_shelly.set_output_0(False)
  89. self.main_light_timer = None
  90. else:
  91. self.main_light_timer -= cyclic_task.cycle_time
  92. class room_shelly_tradfri_light(room_shelly):
  93. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  94. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  95. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
  96. #
  97. self.gui_br_ct_main_light = devices.nodered_gui_brightness_color_temp(mqtt_client, topic_gui_br_ct)
  98. #
  99. # Callback initialisation
  100. #
  101. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_br_ct_main_light.set_enable_mcb)
  102. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_main_light.set_brightness_mcb)
  103. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_main_light.set_color_temp_mcb)
  104. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
  105. None, self.main_light_tradfri.set_brightness_mcb)
  106. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
  107. None, self.main_light_tradfri.set_color_temp_mcb)
  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_br_ct):
  110. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct)
  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 != data:
  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": ""}')