2022-12-20 14:05:32 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
import devices
|
|
|
|
import logging
|
2022-12-21 17:04:55 +01:00
|
|
|
import task
|
2022-12-20 14:05:32 +01:00
|
|
|
|
|
|
|
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):
|
2022-12-21 07:12:17 +01:00
|
|
|
def __init__(self, mqtt_client):
|
|
|
|
self.mqtt_client = mqtt_client
|
|
|
|
|
2022-12-20 14:05:32 +01:00
|
|
|
|
|
|
|
class room_shelly(room):
|
|
|
|
def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
|
2022-12-21 07:12:17 +01:00
|
|
|
super().__init__(mqtt_client)
|
2022-12-20 14:05:32 +01:00
|
|
|
self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
|
|
|
|
#
|
2022-12-25 16:31:22 +01:00
|
|
|
self.gui_switch_main_light = devices.nodered_gui_switch(mqtt_client, topic=topic_gui_switch)
|
2022-12-20 14:05:32 +01:00
|
|
|
#
|
|
|
|
# Callback initialisation
|
|
|
|
#
|
2022-12-25 16:31:22 +01:00
|
|
|
self.gui_switch_main_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.main_light_shelly.set_output_0_mcb)
|
|
|
|
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_main_light.set_state_mcb)
|
2022-12-21 17:04:55 +01:00
|
|
|
#
|
2022-12-22 07:55:56 +01:00
|
|
|
self.block_all_off = False
|
2022-12-21 17:04:55 +01:00
|
|
|
self.last_flash_data = None
|
2022-12-25 16:31:22 +01:00
|
|
|
self.delayed_task = task.delayed(.25, self.main_light_shelly.toggle_output_0_mcb, None, None, None)
|
2022-12-20 14:05:32 +01:00
|
|
|
|
2022-12-21 07:12:17 +01:00
|
|
|
def all_off(self, device=None, key=None, data=None):
|
2022-12-22 07:55:56 +01:00
|
|
|
if not self.block_all_off:
|
|
|
|
logger.info("Switching all off \"%s\"", type(self).__name__)
|
|
|
|
self.main_light_shelly.set_output_0(False)
|
|
|
|
self.main_light_shelly.set_output_1(False)
|
|
|
|
self.block_all_off = False
|
|
|
|
|
|
|
|
def all_off_feedback(self, device=None, key=None, data=None):
|
|
|
|
logger.info("Flashing \"%s\" main light", type(self).__name__)
|
|
|
|
if self.main_light_shelly.output_0 is False:
|
|
|
|
self.main_light_shelly.set_output_0(True)
|
|
|
|
self.block_all_off = True
|
|
|
|
self.delayed_task.run()
|
2022-12-20 14:05:32 +01:00
|
|
|
|
2022-12-21 17:04:55 +01:00
|
|
|
def flash_main_light(self, device, key, data):
|
|
|
|
if self.last_flash_data != data and data is True:
|
|
|
|
logger.info("Flashing \"%s\" main light", type(self).__name__)
|
2022-12-25 16:31:22 +01:00
|
|
|
self.main_light_shelly.toggle_output_0_mcb(device, key, data)
|
2022-12-21 17:04:55 +01:00
|
|
|
self.delayed_task.run()
|
|
|
|
self.last_flash_data = data
|
|
|
|
|
2022-12-20 14:05:32 +01:00
|
|
|
|
|
|
|
class room_shelly_tradfri_light(room_shelly):
|
2022-12-25 16:31:22 +01:00
|
|
|
def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
|
2022-12-20 14:05:32 +01:00
|
|
|
super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
|
2022-12-25 18:43:25 +01:00
|
|
|
self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
|
2022-12-20 14:05:32 +01:00
|
|
|
#
|
2022-12-25 16:31:22 +01:00
|
|
|
self.gui_br_ct_main_light = devices.nodered_gui_brightness_color_temp(mqtt_client, topic_gui_br_ct)
|
2022-12-20 14:05:32 +01:00
|
|
|
#
|
|
|
|
# Callback initialisation
|
|
|
|
#
|
2022-12-25 16:31:22 +01:00
|
|
|
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_br_ct_main_light.set_enable_mcb)
|
|
|
|
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_main_light.set_brightness_mcb)
|
|
|
|
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_main_light.set_color_temp_mcb)
|
|
|
|
self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
|
|
|
|
None, self.main_light_tradfri.set_brightness_mcb)
|
|
|
|
self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
|
|
|
|
None, self.main_light_tradfri.set_color_temp_mcb)
|
2022-12-21 07:12:17 +01:00
|
|
|
|
2022-12-20 14:05:32 +01:00
|
|
|
|
|
|
|
class room_shelly_silvercrest_light(room_shelly_tradfri_light):
|
2022-12-25 16:31:22 +01:00
|
|
|
def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
|
|
|
|
super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct)
|
2022-12-20 14:05:32 +01:00
|
|
|
#
|
|
|
|
# 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):
|
2022-12-25 16:31:22 +01:00
|
|
|
if data is True and self.main_light_shelly_last != data:
|
2022-12-20 14:05:32 +01:00
|
|
|
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": ""}')
|