smart_brain/function/__init__.py

173 lines
7.1 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
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
# TODO: Add brightness and color temp including disable of gui elements.
# TODO: Add lamp sleep_di
2022-12-19 16:57:59 +01:00
# TODO: Add input_device functions
# TODO: Clean-Up Node-Red
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 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
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):
def __init__(self, mqtt_client):
# http://shelly1l-E8DB84A254C7
2022-12-19 16:57:59 +01:00
super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi")
2022-12-19 16:11:35 +01:00
self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_madi")
self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di")
self.button_tradfri = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east")
class first_floor_east_living(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1l-3C6105E3F910
2022-12-19 16:57:59 +01:00
super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom")
2022-12-19 16:11:35 +01:00
self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/livingroom")
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")
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.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)
class all_functions(object):
2022-12-19 16:11:35 +01:00
def __init__(self, mqtt_client):
self.rooms = {}
self.__devices__ = None
#
# 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)
def ffe_off(self):
self.ffe_floor.off()
self.ffe_kitchen.off()
self.ffe_dining.off()
self.ffe_sleep_madi.off()
self.ffe_living.off()
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__