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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. import logging
  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. def gui_switch_feedback(self, device, key, data):
  16. self.gui_switch_main_light.set_feedback(data)
  17. class room_shelly(room):
  18. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
  19. super().__init__(mqtt_client)
  20. self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
  21. #
  22. self.gui_switch_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_switch)
  23. #
  24. # Callback initialisation
  25. #
  26. self.gui_switch_main_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command)
  27. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_feedback)
  28. def all_off(self, device=None, key=None, data=None):
  29. logger.info("Switching all off \"%s\"", type(self).__name__)
  30. self.main_light_shelly.set_output_0(False)
  31. self.main_light_shelly.set_output_1(False)
  32. def gui_switch_command(self, device, key, data):
  33. logger.info("Switching \"%s\" main light: %s", type(self).__name__, str(data))
  34. self.main_light_shelly.set_output_0(data)
  35. def toggle_main_light(self, device, key, data):
  36. logger.info("Toggeling \"%s\" main light", type(self).__name__)
  37. self.main_light_shelly.set_output_0("toggle")
  38. class room_shelly_tradfri_light(room_shelly):
  39. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  40. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  41. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic=topic_tradfri_light)
  42. #
  43. self.gui_brightness_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_brightness)
  44. self.gui_brightness_main_light.enable(False)
  45. self.gui_brightness_main_light.set_feedback(0)
  46. self.gui_color_temp_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_color_temp)
  47. self.gui_color_temp_main_light.enable(False)
  48. self.gui_color_temp_main_light.set_feedback(0)
  49. #
  50. # Callback initialisation
  51. #
  52. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.enable_brightness_n_colortemp)
  53. self.main_light_tradfri.add_callback(
  54. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_main_light)
  55. self.main_light_tradfri.add_callback(
  56. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_main_light)
  57. self.gui_brightness_main_light.add_callback(
  58. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_main_light)
  59. self.gui_color_temp_main_light.add_callback(
  60. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_main_light)
  61. def enable_brightness_n_colortemp(self, devive, key, data):
  62. self.gui_brightness_main_light.enable(data)
  63. self.gui_color_temp_main_light.enable(data)
  64. if data is False:
  65. self.gui_brightness_main_light.set_feedback(0)
  66. self.gui_color_temp_main_light.set_feedback(0)
  67. else:
  68. self.gui_brightness_main_light.set_feedback(self.main_light_tradfri.brightness)
  69. self.gui_color_temp_main_light.set_feedback(self.main_light_tradfri.color_temp / 10)
  70. def set_gui_brightness_main_light(self, device, key, data):
  71. self.gui_brightness_main_light.set_feedback(data)
  72. def set_gui_color_temp_main_light(self, device, key, data):
  73. self.gui_color_temp_main_light.set_feedback(data / 10)
  74. def set_brightness_main_light(self, device, key, data):
  75. logger.info("Setting brightness \"%s\" main light: %.1f", type(self).__name__, data)
  76. self.main_light_tradfri.set_brightness(data)
  77. def set_color_temp_main_light(self, device, key, data):
  78. logger.info("Setting color_temp \"%s\" main light: %.1f", type(self).__name__, data)
  79. self.main_light_tradfri.set_color_temp(data * 10)
  80. def fade_light(self, device, topic, data):
  81. if (data == 'brightness_up_hold'):
  82. logger.info("Increasing brightness \"%s\" main light", type(self).__name__)
  83. self.main_light_tradfri.brightness_inc()
  84. elif (data == 'brightness_down_hold'):
  85. logger.info("Decreasing brightness \"%s\" main light", type(self).__name__)
  86. self.main_light_tradfri.brightness_dec()
  87. elif (data.startswith('brightness') and data.endswith('release')):
  88. logger.info("Stoping brightness change \"%s\" main light", type(self).__name__)
  89. self.main_light_tradfri.brightness_stop()
  90. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  91. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  92. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp)
  93. #
  94. # Callback initialisation
  95. #
  96. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  97. #
  98. self.main_light_shelly_last = None
  99. def get_initial_main_light_data(self, device, key, data):
  100. if data is True and self.main_light_shelly_last is not True:
  101. self.send_init_message_main_light()
  102. self.main_light_shelly_last = data
  103. def send_init_message_main_light(self):
  104. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')