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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. class room_shelly(room):
  16. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
  17. super().__init__(mqtt_client)
  18. self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
  19. #
  20. self.gui_switch_main_light = devices.nodered_gui_switch(mqtt_client, topic=topic_gui_switch)
  21. #
  22. # Callback initialisation
  23. #
  24. self.gui_switch_main_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.main_light_shelly.set_output_0_mcb)
  25. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_main_light.set_state_mcb)
  26. #
  27. self.block_all_off = False
  28. self.last_flash_data = None
  29. self.delayed_task = task.delayed(.25, self.main_light_shelly.toggle_output_0_mcb, None, None, None)
  30. def all_off(self, device=None, key=None, data=None):
  31. if not self.block_all_off:
  32. logger.info("Switching all off \"%s\"", type(self).__name__)
  33. self.main_light_shelly.set_output_0(False)
  34. self.main_light_shelly.set_output_1(False)
  35. self.block_all_off = False
  36. def all_off_feedback(self, device=None, key=None, data=None):
  37. logger.info("Flashing \"%s\" main light", type(self).__name__)
  38. if self.main_light_shelly.output_0 is False:
  39. self.main_light_shelly.set_output_0(True)
  40. self.block_all_off = True
  41. self.delayed_task.run()
  42. def flash_main_light(self, device, key, data):
  43. if self.last_flash_data != data and data is True:
  44. logger.info("Flashing \"%s\" main light", type(self).__name__)
  45. self.main_light_shelly.toggle_output_0_mcb(device, key, data)
  46. self.delayed_task.run()
  47. self.last_flash_data = data
  48. class room_shelly_motion_sensor(room_shelly):
  49. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_motion_sensor_1, topic_motion_sensor_2=None, timer_value=30):
  50. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  51. self.timer_value = timer_value
  52. #
  53. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  54. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
  55. #
  56. self.motion_sensor_silvercrest_1 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_1)
  57. self.motion_sensor_silvercrest_1.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  58. #
  59. if topic_motion_sensor_2 is not None:
  60. self.motion_sensor_silvercrest_2 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_2)
  61. self.motion_sensor_silvercrest_2.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
  62. #
  63. self.reset_timer()
  64. #
  65. cyclic_task = task.periodic(1, self.cyclic_task)
  66. cyclic_task.run()
  67. def set_motion_detected(self, device, key, data):
  68. if device == self.motion_sensor_silvercrest_1:
  69. self.motion_detected_1 = data
  70. elif device == self.motion_sensor_silvercrest_2:
  71. self.motion_detected_2 = data
  72. if data is True:
  73. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.main_light_shelly.topic)
  74. self.main_light_shelly.set_output_0(True)
  75. def reload_timer(self, device, key, data):
  76. self.main_light_timer = self.timer_value
  77. def reset_timer(self, device=None, key=None, data=None):
  78. self.main_light_timer = None
  79. self.motion_detected_1 = False
  80. self.motion_detected_2 = False
  81. def cyclic_task(self, cyclic_task):
  82. if self.main_light_timer is not None:
  83. if self.main_light_timer <= 0:
  84. if not self.motion_detected_1 and not self.motion_detected_2:
  85. logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
  86. self.main_light_shelly.set_output_0(False)
  87. self.main_light_timer = None
  88. else:
  89. self.main_light_timer -= cyclic_task.cycle_time
  90. class room_shelly_tradfri_light(room_shelly):
  91. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  92. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  93. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
  94. #
  95. self.gui_br_ct_main_light = devices.nodered_gui_brightness_color_temp(mqtt_client, topic_gui_br_ct)
  96. #
  97. # Callback initialisation
  98. #
  99. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_br_ct_main_light.set_enable_mcb)
  100. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_main_light.set_brightness_mcb)
  101. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_main_light.set_color_temp_mcb)
  102. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
  103. None, self.main_light_tradfri.set_brightness_mcb)
  104. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
  105. None, self.main_light_tradfri.set_color_temp_mcb)
  106. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  107. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  108. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct)
  109. #
  110. # Callback initialisation
  111. #
  112. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  113. #
  114. self.main_light_shelly_last = None
  115. def get_initial_main_light_data(self, device, key, data):
  116. if data is True and self.main_light_shelly_last != data:
  117. self.send_init_message_main_light()
  118. self.main_light_shelly_last = data
  119. def send_init_message_main_light(self):
  120. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')