smart_brain/function/__init__.py

396 lines
19 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
2022-12-19 16:11:35 +01:00
import config
import devices
import inspect
import logging
# TODO: implement bed light dirk fading by input device
# TODO: implement heating function sleep_madi
# TODO: implement circulation pump
# TODO: implement switch off functionality (except of switch off button transportation)
2022-12-20 10:18:32 +01:00
# TODO: implement garland (incl. day events like sunset, sunrise, ...)
# TODO: implement existing nodered functionality "dirk" (ground floor west)
# TODO: implement warning message (incl. fixing all_functions.devicelist
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
2022-12-19 16:11:35 +01:00
class all_functions(object):
def __init__(self, mqtt_client):
self.rooms = {}
self.__devices__ = None
#
2022-12-20 10:18:32 +01:00
# ground floor west
#
self.gfw_floor = ground_floor_west_floor(mqtt_client)
self.gfw_marion = ground_floor_west_marion(mqtt_client)
self.gfw_dirk = ground_floor_west_dirk(mqtt_client)
#
# first floor west
#
self.ffw_julian = first_floor_west_julian(mqtt_client)
self.ffw_living = first_floor_west_living(mqtt_client)
#
# first floor east
#
self.ffe_floor = first_floor_east_floor(mqtt_client)
self.ffe_kitchen = first_floor_east_kitchen(mqtt_client)
self.ffe_dining = first_floor_east_dining(mqtt_client)
self.ffe_sleep_madi = first_floor_east_sleep_madi(mqtt_client)
self.ffe_living = first_floor_east_living(mqtt_client)
#
2022-12-20 10:18:32 +01:00
# Input devices sleep_madi
#
self.ffe_button_tradfri_sleep = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east")
self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "toggle",
self.ffe_sleep_madi.toggle_main_light)
self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "brightness_up_click",
self.ffe_sleep_madi.toggle_bed_light_di)
self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "brightness_down_click",
self.ffe_sleep_madi.toggle_bed_light_di)
self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "arrow_right_click",
self.ffe_floor.toggle_main_light)
def devicelist(self):
raise Exception
# TODO: generate list by using getattr
if self.__devices__ is None:
self.__devices__ = []
for room in self.rooms:
for name, obj in inspect.getmembers(room):
if type(obj) in devices.DEVICE_TYPE_LIST():
self.__devices__.append(obj)
return self.__devices__
2022-12-19 16:11:35 +01:00
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": ""}')
2022-12-20 10:18:32 +01:00
class ground_floor_west_floor(room_shelly_silvercrest_light):
# https://shelly1l-84CCA8AD1148
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/floor_eg_w", "gui/gfw_sw_floor",
"zigbee_eg_w/light/floor_eg_w/a", "gui/gfw_br_floor", "gui/gfw_ct_floor")
#
# Callback initialisation
#
self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/floor_eg_w/b")
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
None, self.sync_brightness_main_light)
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
None, self.sync_color_temp_main_light)
def send_init_message_main_light(self):
return super().send_init_message_main_light()
self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
def sync_brightness_main_light(self, device, key, data):
self.main_light_tradfri_2.set_brightness(data)
def sync_color_temp_main_light(self, device, key, data):
self.main_light_tradfri_2.set_color_temp(data)
class ground_floor_west_marion(room_shelly):
# https://shelly1l-E8DB84A1E067
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/marion", "gui/gfw_sw_marion")
class ground_floor_west_dirk(room_shelly_tradfri_light):
# https://shelly1l-3C6105E44F27
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/dirk", "gui/gfw_sw_dirk",
"zigbee_eg_w/light/dirk", "gui/gfw_br_dirk", "gui/gfw_ct_dirk")
class first_floor_west_julian(room_shelly_tradfri_light):
# http://shelly1l-3C6105E43452
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/julian", "gui/ffw_sw_julian",
"zigbee_og_e/light/julian", "gui/ffw_br_julian", "gui/ffw_ct_julian")
class first_floor_west_living(room_shelly):
# http://shelly1l-84CCA8ACE6A1
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/living_mika", "gui/ffw_sw_living")
2022-12-19 16:11:35 +01:00
class first_floor_east_floor(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1l-3C6105E4E629
2022-12-19 16:57:59 +01:00
super().__init__(mqtt_client, "shellies/floor_madi", "gui/ffe_sw_floor")
2022-12-19 16:11:35 +01:00
def toggle_main_light(self, device, key, data):
logger.info("Toggeling \"%s\" main light", type(self).__name__)
self.main_light_shelly.set_output_0("toggle")
2022-12-19 16:11:35 +01:00
class first_floor_east_kitchen(room_shelly):
# TODO: add circulation pump (switch, time)
def __init__(self, mqtt_client):
# http://shelly1l-8CAAB5616C01
2022-12-19 16:57:59 +01:00
super().__init__(mqtt_client, "shellies/kitchen", "gui/ffe_sw_kitchen")
2022-12-19 16:11:35 +01:00
class first_floor_east_dining(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1l-84CCA8ADD055
2022-12-19 16:57:59 +01:00
super().__init__(mqtt_client, "shellies/diningroom", "gui/ffe_sw_diningroom")
2022-12-19 16:11:35 +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")
#
2022-12-19 16:57:59 +01:00
self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_dining_floorlamp")
2022-12-19 16:11:35 +01:00
#
# Callback initialisation
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0,
None, self.gui_switch_feedback_floorlamp)
#
self.main_light_shelly_last = None
def floorlamp_synchronisation(self, device, key, data):
if data != self.main_light_shelly_last:
logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
self.floorlamp_powerplug.set_output_0(data)
self.main_light_shelly_last = data
def gui_switch_command_floorlamp(self, device, key, data):
logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
self.floorlamp_powerplug.set_output_0(data)
def gui_switch_feedback_floorlamp(self, device, key, data):
self.gui_switch_floorlamp.set_feedback(data)
class first_floor_east_sleep_madi(room_shelly_tradfri_light):
2022-12-19 16:11:35 +01:00
def __init__(self, mqtt_client):
# http://shelly1l-E8DB84A254C7
super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi",
"zigbee_og_e/light/sleep_madi", "gui/ffe_br_sleep_madi", "gui/ffe_ct_sleep_madi")
#
2022-12-19 16:11:35 +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(mqtt_client, "gui/ffe_sw_bed_light_di")
self.gui_brightness_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_br_bed_light_di")
self.gui_brightness_bed_light_di.enable(False)
self.gui_brightness_bed_light_di.set_feedback(0)
#
# Callback initialisation
#
self.gui_switch_bed_light_di.add_callback(
devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_bed_light_di)
self.bed_light_di_tradfri.add_callback(
devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_bed_light_di)
self.bed_light_di_tradfri.add_callback(
devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_bed_light_di)
self.gui_brightness_bed_light_di.add_callback(
devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_bed_light_di)
def toggle_main_light(self, device, key, data):
logger.info("Toggeling \"%s\" main light", type(self).__name__)
self.main_light_shelly.set_output_0("toggle")
2022-12-19 16:11:35 +01:00
def gui_switch_command_bed_light_di(self, device, key, data):
logger.info("Switching \"%s\" bed light dirk: %s", type(self).__name__, str(data))
self.bed_light_di_tradfri.set_output_0(data)
2022-12-19 16:11:35 +01:00
def gui_switch_feedback_bed_light_di(self, device, key, data):
self.gui_switch_bed_light_di.set_feedback(data)
self.gui_brightness_bed_light_di.enable(data)
if data is False:
self.gui_brightness_bed_light_di.set_feedback(0)
else:
self.gui_brightness_bed_light_di.set_feedback(self.bed_light_di_tradfri.brightness)
def set_gui_brightness_bed_light_di(self, device, key, data):
self.gui_brightness_bed_light_di.set_feedback(data)
def set_brightness_bed_light_di(self, device, key, data):
logger.info("Setting brightness \"%s\" bed light dirk: %.1f", type(self).__name__, data)
self.bed_light_di_tradfri.set_brightness(data)
def toggle_bed_light_di(self, device, key, data):
logger.info("Toggeling \"%s\" bed light dirk", type(self).__name__)
self.bed_light_di_tradfri.set_output_0("toggle")
class first_floor_east_living(room_shelly_tradfri_light):
2022-12-19 16:11:35 +01:00
def __init__(self, mqtt_client):
# http://shelly1l-3C6105E3F910
super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom",
"zigbee_og_e/light/livingroom", "gui/ffe_br_livingroom", "gui/ffe_ct_livingroom")
2022-12-19 16:11:35 +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-19 16:57:59 +01:00
self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_living_floorlamp")
self.gui_brightness_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_br_livingroom_floorlamp")
self.gui_brightness_floorlamp.enable(False)
self.gui_brightness_floorlamp.set_feedback(0)
self.gui_color_temp_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_ct_livingroom_floorlamp")
self.gui_color_temp_floorlamp.enable(False)
self.gui_color_temp_floorlamp.set_feedback(0)
2022-12-19 16:11:35 +01:00
#
# Callback initialisation
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation)
self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp)
self.floorlamp_tradfri_1.add_callback(
devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp)
self.floorlamp_tradfri_1.add_callback(
devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_floorlamp)
self.floorlamp_tradfri_1.add_callback(
devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_floorlamp)
self.gui_brightness_floorlamp.add_callback(
devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_floorlamp)
self.gui_color_temp_floorlamp.add_callback(
devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_floorlamp)
2022-12-19 16:11:35 +01:00
#
self.main_light_shelly_last = None
def __floorlamp_devices__(self):
rv = []
for i in range(1, 7):
rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
return rv
def floorlamp_synchronisation(self, device, key, data):
if data != self.main_light_shelly_last:
logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data))
for device in self.__floorlamp_devices__():
device.set_output_0(data)
self.main_light_shelly_last = data
def gui_switch_command_floorlamp(self, device, key, data):
logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data))
for device in self.__floorlamp_devices__():
device.set_output_0(data)
def gui_switch_feedback_floorlamp(self, device, key, data):
self.gui_switch_floorlamp.set_feedback(data)
self.gui_brightness_floorlamp.enable(data)
self.gui_color_temp_floorlamp.enable(data)
if data is False:
self.gui_brightness_floorlamp.set_feedback(0)
self.gui_color_temp_floorlamp.set_feedback(0)
else:
self.gui_brightness_floorlamp.set_feedback(self.floorlamp_tradfri_1.brightness)
self.gui_color_temp_floorlamp.set_feedback(self.floorlamp_tradfri_1.color_temp / 10)
def set_gui_brightness_floorlamp(self, device, key, data):
self.gui_brightness_floorlamp.set_feedback(data)
def set_gui_color_temp_floorlamp(self, device, key, data):
self.gui_color_temp_floorlamp.set_feedback(data / 10)
def set_brightness_floorlamp(self, device, key, data):
logger.info("Setting brightness \"%s\" floorlamp: %.1f", type(self).__name__, data)
for device in self.__floorlamp_devices__():
device.set_brightness(data)
def set_color_temp_floorlamp(self, device, key, data):
logger.info("Setting color_temp \"%s\" floorlamp: %.1f", type(self).__name__, data)
for device in self.__floorlamp_devices__():
device.set_color_temp(data * 10)