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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_auto_off(room_shelly):
  49. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, timer_value=30):
  50. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  51. self.timer_value = timer_value
  52. self.main_light_timer = None
  53. #
  54. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  55. #
  56. cyclic_task = task.periodic(1, self.cyclic_task)
  57. cyclic_task.run()
  58. def reload_timer(self, device, key, data):
  59. self.main_light_timer = self.timer_value
  60. def cyclic_task(self, cyclic_task):
  61. if self.main_light_timer is not None:
  62. self.main_light_timer -= cyclic_task.cycle_time
  63. if self.main_light_timer <= 0:
  64. self.main_light_shelly.set_output_0(False)
  65. self.main_light_timer = None
  66. class room_shelly_tradfri_light(room_shelly):
  67. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  68. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  69. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
  70. #
  71. self.gui_br_ct_main_light = devices.nodered_gui_brightness_color_temp(mqtt_client, topic_gui_br_ct)
  72. #
  73. # Callback initialisation
  74. #
  75. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_br_ct_main_light.set_enable_mcb)
  76. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_main_light.set_brightness_mcb)
  77. self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_main_light.set_color_temp_mcb)
  78. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
  79. None, self.main_light_tradfri.set_brightness_mcb)
  80. self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
  81. None, self.main_light_tradfri.set_color_temp_mcb)
  82. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  83. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
  84. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct)
  85. #
  86. # Callback initialisation
  87. #
  88. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  89. #
  90. self.main_light_shelly_last = None
  91. def get_initial_main_light_data(self, device, key, data):
  92. if data is True and self.main_light_shelly_last != data:
  93. self.send_init_message_main_light()
  94. self.main_light_shelly_last = data
  95. def send_init_message_main_light(self):
  96. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')