107 行
4.8 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import config
import devices
import logging
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
class room(object):
def gui_switch_feedback(self, device, key, data):
self.gui_switch_main_light.set_feedback(data)
class room_shelly(room):
def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
#
self.gui_switch_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_switch)
#
# Callback initialisation
#
self.gui_switch_main_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command)
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_feedback)
def all_off(self):
self.main_light_shelly.set_output_0(False)
self.main_light_shelly.set_output_1(False)
def gui_switch_command(self, device, key, data):
logger.info("Switching \"%s\" main light: %s", type(self).__name__, str(data))
self.main_light_shelly.set_output_0(data)
class room_shelly_tradfri_light(room_shelly):
def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic=topic_tradfri_light)
#
self.gui_brightness_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_brightness)
self.gui_brightness_main_light.enable(False)
self.gui_brightness_main_light.set_feedback(0)
self.gui_color_temp_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_color_temp)
self.gui_color_temp_main_light.enable(False)
self.gui_color_temp_main_light.set_feedback(0)
#
# Callback initialisation
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.enable_brightness_n_colortemp)
self.main_light_tradfri.add_callback(
devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_main_light)
self.main_light_tradfri.add_callback(
devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_main_light)
self.gui_brightness_main_light.add_callback(
devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_main_light)
self.gui_color_temp_main_light.add_callback(
devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_main_light)
def enable_brightness_n_colortemp(self, devive, key, data):
self.gui_brightness_main_light.enable(data)
self.gui_color_temp_main_light.enable(data)
if data is False:
self.gui_brightness_main_light.set_feedback(0)
self.gui_color_temp_main_light.set_feedback(0)
else:
self.gui_brightness_main_light.set_feedback(self.main_light_tradfri.brightness)
self.gui_color_temp_main_light.set_feedback(self.main_light_tradfri.color_temp / 10)
def set_gui_brightness_main_light(self, device, key, data):
self.gui_brightness_main_light.set_feedback(data)
def set_gui_color_temp_main_light(self, device, key, data):
self.gui_color_temp_main_light.set_feedback(data / 10)
def set_brightness_main_light(self, device, key, data):
logger.info("Setting brightness \"%s\" main light: %.1f", type(self).__name__, data)
self.main_light_tradfri.set_brightness(data)
def set_color_temp_main_light(self, device, key, data):
logger.info("Setting color_temp \"%s\" main light: %.1f", type(self).__name__, data)
self.main_light_tradfri.set_color_temp(data * 10)
class room_shelly_silvercrest_light(room_shelly_tradfri_light):
def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp):
super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp)
#
# Callback initialisation
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
#
self.main_light_shelly_last = None
def get_initial_main_light_data(self, device, key, data):
if data is True and self.main_light_shelly_last is not True:
self.send_init_message_main_light()
self.main_light_shelly_last = data
def send_init_message_main_light(self):
self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')