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.

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