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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 gui_switch_feedback(self, device, key, data):
  14. self.gui_switch_main_light.set_feedback(data)
  15. class room_shelly(room):
  16. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
  17. self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
  18. #
  19. self.gui_switch_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_switch)
  20. #
  21. # Callback initialisation
  22. #
  23. self.gui_switch_main_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command)
  24. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_feedback)
  25. def all_off(self):
  26. self.main_light_shelly.set_output_0(False)
  27. self.main_light_shelly.set_output_1(False)
  28. def gui_switch_command(self, device, key, data):
  29. logger.info("Switching \"%s\" main light: %s", type(self).__name__, str(data))
  30. self.main_light_shelly.set_output_0(data)
  31. class room_shelly_tradfri_light(room_shelly):
  32. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  33. super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
  34. self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic=topic_tradfri_light)
  35. #
  36. self.gui_brightness_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_brightness)
  37. self.gui_brightness_main_light.enable(False)
  38. self.gui_brightness_main_light.set_feedback(0)
  39. self.gui_color_temp_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_color_temp)
  40. self.gui_color_temp_main_light.enable(False)
  41. self.gui_color_temp_main_light.set_feedback(0)
  42. #
  43. # Callback initialisation
  44. #
  45. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.enable_brightness_n_colortemp)
  46. self.main_light_tradfri.add_callback(
  47. devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_main_light)
  48. self.main_light_tradfri.add_callback(
  49. devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_main_light)
  50. self.gui_brightness_main_light.add_callback(
  51. devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_main_light)
  52. self.gui_color_temp_main_light.add_callback(
  53. devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_main_light)
  54. def enable_brightness_n_colortemp(self, devive, key, data):
  55. self.gui_brightness_main_light.enable(data)
  56. self.gui_color_temp_main_light.enable(data)
  57. if data is False:
  58. self.gui_brightness_main_light.set_feedback(0)
  59. self.gui_color_temp_main_light.set_feedback(0)
  60. else:
  61. self.gui_brightness_main_light.set_feedback(self.main_light_tradfri.brightness)
  62. self.gui_color_temp_main_light.set_feedback(self.main_light_tradfri.color_temp / 10)
  63. def set_gui_brightness_main_light(self, device, key, data):
  64. self.gui_brightness_main_light.set_feedback(data)
  65. def set_gui_color_temp_main_light(self, device, key, data):
  66. self.gui_color_temp_main_light.set_feedback(data / 10)
  67. def set_brightness_main_light(self, device, key, data):
  68. logger.info("Setting brightness \"%s\" main light: %.1f", type(self).__name__, data)
  69. self.main_light_tradfri.set_brightness(data)
  70. def set_color_temp_main_light(self, device, key, data):
  71. logger.info("Setting color_temp \"%s\" main light: %.1f", type(self).__name__, data)
  72. self.main_light_tradfri.set_color_temp(data * 10)
  73. class room_shelly_silvercrest_light(room_shelly_tradfri_light):
  74. def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
  75. super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp)
  76. #
  77. # Callback initialisation
  78. #
  79. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
  80. #
  81. self.main_light_shelly_last = None
  82. def get_initial_main_light_data(self, device, key, data):
  83. if data is True and self.main_light_shelly_last is not True:
  84. self.send_init_message_main_light()
  85. self.main_light_shelly_last = data
  86. def send_init_message_main_light(self):
  87. self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')