smart_brain/function/first_floor_east.py

182 lines
10 KiB
Python
Raw Normal View History

2022-12-20 14:05:32 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import config
import devices
import json
2022-12-20 14:05:32 +01:00
import logging
from function.rooms import room_shelly, room_shelly_tradfri_light
from function.helpers import changed_value_indicator
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 first_floor_east_floor(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1l-3C6105E4E629
super().__init__(mqtt_client, "shellies/floor_madi", "gui/ffe/floor/main_light/switch")
2022-12-20 14:05:32 +01:00
class first_floor_east_kitchen(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1l-8CAAB5616C01
super().__init__(mqtt_client, "shellies/kitchen", "gui/ffe/kitchen/main_light/switch")
2022-12-20 14:05:32 +01:00
class first_floor_east_dining(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1l-84CCA8ADD055
super().__init__(mqtt_client, "shellies/diningroom", "gui/ffe/diningroom/main_light/switch")
2022-12-20 14:05:32 +01:00
self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/dining_floorlamp")
if config.CHRISTMAS:
self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, topic="zigbee_og_e/powerplug/aux")
#
self.gui_switch_floorlamp = devices.nodered_gui_switch(mqtt_client, topic="gui/ffe/diningroom/floorlamp/switch")
2022-12-20 14:05:32 +01:00
#
# Callback initialisation
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_powerplug.set_output_0_mcb, True)
self.gui_switch_floorlamp.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.floorlamp_powerplug.set_output_0_mcb)
self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_switch_floorlamp.set_state_mcb)
2022-12-20 14:05:32 +01:00
def all_off(self, device=None, key=None, data=None):
super().all_off(device, key, data)
self.floorlamp_powerplug.set_output_0(False)
if config.CHRISTMAS:
self.garland_powerplug.set_output_0(False)
2022-12-20 14:05:32 +01:00
class first_floor_east_sleep(room_shelly_tradfri_light):
2022-12-20 14:05:32 +01:00
def __init__(self, mqtt_client):
# http://shelly1l-E8DB84A254C7
super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe/sleep/main_light/switch",
"zigbee_og_e/light/sleep_madi", "gui/ffe/sleep/main_light/br_ct")
2022-12-20 14:05:32 +01:00
#
self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di")
#
self.gui_switch_bed_light_di = devices.nodered_gui_switch(mqtt_client, "gui/ffe/sleep/bed_light_di/switch")
self.gui_br_ct_bed_light_di = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/ffe/sleep/bed_light_di/br_ct")
self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, "gui/ffe/sleep/active_device_state/led")
2022-12-20 14:05:32 +01:00
#
2022-12-22 07:55:56 +01:00
self.button_tradfri = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east")
#
2022-12-20 14:05:32 +01:00
# Callback initialisation
#
# button
2022-12-22 07:55:56 +01:00
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
self.main_light_shelly.toggle_output_0_mcb)
2022-12-22 07:55:56 +01:00
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP,
self.bed_light_di_tradfri.toggle_output_0_mcb)
2022-12-22 07:55:56 +01:00
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN,
self.bed_light_di_tradfri.toggle_output_0_mcb)
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.fade_light)
# bed light
# switch
self.gui_switch_bed_light_di.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.bed_light_di_tradfri.set_output_0_mcb)
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_bed_light_di.set_state_mcb)
# brightness and color temperature
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_bed_light_di.set_enable_mcb)
self.gui_br_ct_bed_light_di.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
None, self.bed_light_di_tradfri.set_brightness_mcb)
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_bed_light_di.set_enable_mcb)
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_bed_light_di.set_brightness_mcb)
# fade function
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.state_machine_last_activated_device)
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.state_machine_last_activated_device)
self.last_activated_device = None
2022-12-22 07:55:56 +01:00
def all_off(self, device=None, key=None, data=None):
super().all_off(device, key, data)
2022-12-22 07:55:56 +01:00
self.bed_light_di_tradfri.set_output_0(False)
def state_machine_last_activated_device(self, device, topic, data):
if data is True:
self.last_activated_device = device.topic
elif data is False:
if device.topic == self.main_light_shelly.topic and self.bed_light_di_tradfri.output_0:
self.last_activated_device = self.bed_light_di_tradfri.topic
elif device.topic == self.bed_light_di_tradfri.topic and self.main_light_shelly.output_0:
self.last_activated_device = self.main_light_shelly.topic
else:
self.last_activated_device = None
self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_0, self.last_activated_device == self.main_light_shelly.topic)
self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_1, self.last_activated_device == self.bed_light_di_tradfri.topic)
def fade_light(self, device, topic, data):
if self.last_activated_device == self.main_light_shelly.topic:
target = self.main_light_tradfri
elif self.last_activated_device == self.bed_light_di_tradfri.topic:
target = self.bed_light_di_tradfri
else:
return
2022-12-22 07:55:56 +01:00
if (data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG):
logger.info("Increasing brightness \"%s\" %s", type(self).__name__, target.topic)
target.default_inc()
2022-12-22 07:55:56 +01:00
elif (data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG):
logger.info("Decreasing brightness \"%s\" %s", type(self).__name__, target.topic)
target.default_dec()
2022-12-22 07:55:56 +01:00
elif (data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]):
logger.info("Stoping brightness change \"%s\" %s", type(self).__name__, target.topic)
target.default_stop()
2022-12-20 19:38:57 +01:00
2022-12-20 14:05:32 +01:00
class first_floor_east_living(room_shelly_tradfri_light):
def __init__(self, mqtt_client):
# http://shelly1l-3C6105E3F910
super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe/livingroom/main_light/switch",
"zigbee_og_e/light/livingroom", "gui/ffe/livingroom/main_light/br_ct")
2022-12-20 14:05:32 +01:00
for i in range(1, 7):
setattr(self, 'floorlamp_tradfri_%d' % i, devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/living_floorlamp_%d" % i))
#
2022-12-24 12:08:08 +01:00
if config.CHRISTMAS:
self.powerplug_xmas_tree = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/xmas-tree")
2022-12-24 12:49:16 +01:00
self.powerplug_xmas_star = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/xmas-star")
2022-12-20 14:05:32 +01:00
#
self.gui_switch_floorlamp = devices.nodered_gui_switch(mqtt_client, topic="gui/ffe/livingroom/floorlamp/switch")
self.gui_br_ct_floorlamp = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/ffe/livingroom/floorlamp/br_ct")
2022-12-20 14:05:32 +01:00
#
2022-12-24 12:08:08 +01:00
if config.CHRISTMAS:
self.gui_switch_xmas_tree = devices.nodered_gui_switch(mqtt_client, "gui/ffe/livingroom/xmas_tree/switch")
2022-12-24 12:08:08 +01:00
#
2022-12-20 14:05:32 +01:00
# Callback initialisation
#
# floor lamp
for device in self.__floorlamp_devices__():
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, device.set_output_0_mcb, True)
self.gui_switch_floorlamp.add_callback(devices.nodered_gui_switch.KEY_STATE, None, device.set_output_0_mcb)
self.gui_br_ct_floorlamp.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS, None, device.set_brightness_mcb)
self.gui_br_ct_floorlamp.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP, None, device.set_color_temp_mcb)
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_floorlamp.set_state_mcb)
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_floorlamp.set_enable_mcb)
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_floorlamp.set_brightness_mcb)
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_floorlamp.set_color_temp_mcb)
2022-12-20 14:05:32 +01:00
#
2022-12-24 12:08:08 +01:00
if config.CHRISTMAS:
self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_switch_xmas_tree.set_state_mcb)
self.gui_switch_xmas_tree.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_xmas_tree.set_output_0_mcb)
#
self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_star.set_output_0_mcb)
2022-12-24 12:08:08 +01:00
#
self.cvi = changed_value_indicator()
2022-12-20 14:05:32 +01:00
def all_off(self, device=None, key=None, data=None):
super().all_off(device, key, data)
for floorlamp in self.__floorlamp_devices__():
floorlamp.set_output_0(False)
if config.CHRISTMAS:
self.powerplug_xmas_tree.set_output_0(False)
self.powerplug_xmas_star.set_output_0(False)
2022-12-20 14:05:32 +01:00
def __floorlamp_devices__(self):
rv = []
for i in range(1, 7):
rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
return rv