Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
34ba0f0d01 | |||
dd178d81a6 | |||
98f1acd2eb | |||
a33f9b23da | |||
0365b5de4a | |||
ba7a8b4750 | |||
5fc096c9c7 | |||
3a4d43c0c4 | |||
6b95bda1a4 | |||
ed13a96a4d | |||
d33901aef3 | |||
95bfa4937f | |||
51e14b7b1b | |||
54d72802c2 | |||
d98541b818 | |||
be3d70f942 | |||
40570998d0 | |||
036cddeb8e | |||
e755b75106 | |||
9d550f1b04 | |||
4ddc871aa1 | |||
1ef0f52d37 |
@ -1,847 +0,0 @@
|
||||
import colored
|
||||
import json
|
||||
import sys
|
||||
import task
|
||||
import time
|
||||
|
||||
COLOR_GUI_ACTIVE = colored.fg("light_blue")
|
||||
COLOR_GUI_PASSIVE = COLOR_GUI_ACTIVE + colored.attr("dim")
|
||||
COLOR_SHELLY = colored.fg("light_magenta")
|
||||
COLOR_POWERPLUG = colored.fg("light_cyan")
|
||||
COLOR_LIGHT_ACTIVE = colored.fg("yellow")
|
||||
COLOR_LIGHT_PASSIVE = COLOR_LIGHT_ACTIVE + colored.attr("dim")
|
||||
COLOR_MOTION_SENSOR = colored.fg("dark_orange_3b")
|
||||
COLOR_RADIATOR_VALVE = colored.fg("red")
|
||||
COLOR_REMOTE = colored.fg("green")
|
||||
|
||||
|
||||
def payload_filter(payload):
|
||||
try:
|
||||
return json.loads(payload)
|
||||
except json.decoder.JSONDecodeError:
|
||||
return payload.decode("utf-8")
|
||||
|
||||
|
||||
def command_int_value(value):
|
||||
try:
|
||||
return int(value)
|
||||
except TypeError:
|
||||
print("You need to give a integer parameter not '%s'" % str(value))
|
||||
|
||||
|
||||
def command_float_value(value):
|
||||
try:
|
||||
return float(value)
|
||||
except TypeError:
|
||||
print("You need to give a integer parameter not '%s'" % str(value))
|
||||
|
||||
|
||||
def devicename(topic):
|
||||
return " - ".join(topic.split('/')[1:])
|
||||
|
||||
|
||||
def percent_bar(value):
|
||||
rv = ""
|
||||
for i in range(0, 10):
|
||||
rv += u"\u25ac" if (value - 5) > 10*i else u"\u25ad"
|
||||
return rv
|
||||
|
||||
|
||||
def print_light(color, state, topic, description, led=False):
|
||||
if led is True:
|
||||
if state is True:
|
||||
icon = colored.fg('green') + "\u2b24" + color
|
||||
else:
|
||||
icon = colored.fg('light_gray') + "\u2b24" + color
|
||||
else:
|
||||
icon = u'\u2b24' if state is True else u'\u25ef'
|
||||
print(color + 10 * ' ' + icon + 9 * ' ' + devicename(topic), description + colored.attr("reset"))
|
||||
|
||||
|
||||
def print_switch(color, state, topic, description):
|
||||
icon = u'\u25a0' if state is True else u'\u25a1'
|
||||
print(color + 10 * ' ' + icon + 9 * ' ' + devicename(topic), description + colored.attr("reset"))
|
||||
|
||||
|
||||
def print_percent(color, prefix, perc_value, value_str, topic, description):
|
||||
if len(prefix) > 1 or len(value_str) > 7:
|
||||
raise ValueError("Length of prefix (%d) > 1 or length of value_str (%d) > 7" % (len(prefix), len(value_str)))
|
||||
print(color + prefix + percent_bar(perc_value), value_str + (8 - len(value_str)) * ' ' + devicename(topic), description + colored.attr("reset"))
|
||||
|
||||
|
||||
class base(object):
|
||||
AUTOSEND = True
|
||||
COMMANDS = []
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
self.mqtt_client = mqtt_client
|
||||
self.topic = topic
|
||||
#
|
||||
self.data = {}
|
||||
self.callbacks = {}
|
||||
self.names = {}
|
||||
self.commands = self.COMMANDS[:]
|
||||
#
|
||||
self.mqtt_client.add_callback(self.topic, self.__rx__)
|
||||
self.mqtt_client.add_callback(self.topic + '/#', self.__rx__)
|
||||
|
||||
def add_callback(self, key, callback, value):
|
||||
if self.callbacks.get(key) is None:
|
||||
self.callbacks[key] = []
|
||||
self.callbacks[key].append((callback, value))
|
||||
|
||||
def add_channel_name(self, key, name):
|
||||
self.names[key] = name
|
||||
|
||||
def capabilities(self):
|
||||
return self.commands
|
||||
|
||||
def store_data(self, *args, **kwargs):
|
||||
keys_changed = []
|
||||
for key in kwargs:
|
||||
if kwargs[key] is not None and kwargs[key] != self.data.get(key):
|
||||
keys_changed.append(key)
|
||||
self.data[key] = kwargs[key]
|
||||
for callback, value in self.callbacks.get(key, [(None, None)]):
|
||||
if callback is not None and (value is None or value == kwargs[key]):
|
||||
callback(self, key, kwargs[key])
|
||||
if self.AUTOSEND and len(keys_changed) > 0:
|
||||
self.__tx__(keys_changed)
|
||||
|
||||
def __tx__(self, keys_changed):
|
||||
self.mqtt_client.send(self.topic, json.dumps(self.data))
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
print("%s: __rx__ not handled!" % self.__class__.__name__)
|
||||
|
||||
|
||||
class shelly(base):
|
||||
KEY_OUTPUT_0 = "relay/0"
|
||||
KEY_OUTPUT_1 = "relay/1"
|
||||
KEY_INPUT_0 = "input/0"
|
||||
KEY_INPUT_1 = "input/1"
|
||||
KEY_LONGPUSH_0 = "longpush/0"
|
||||
KEY_LONGPUSH_1 = "longpush/1"
|
||||
#
|
||||
INPUT_FUNC_OUT1_FOLLOW = "out1_follow"
|
||||
INPUT_FUNC_OUT1_TRIGGER = "out1_trigger"
|
||||
INPUT_FUNC_OUT2_FOLLOW = "out2_follow"
|
||||
INPUT_FUNC_OUT2_TRIGGER = "out2_trigger"
|
||||
#
|
||||
COMMANDS = [
|
||||
"get_relay_0", "toggle_relay_0",
|
||||
"get_relay_1", "toggle_relay_1",
|
||||
"get_input_0", "toggle_input_0",
|
||||
"get_input_1", "toggle_input_1",
|
||||
"trigger_long_input_0", "trigger_long_input_1",
|
||||
]
|
||||
|
||||
def __init__(self, mqtt_client, topic, input_0_func=None, input_1_func=None, output_0_auto_off=None):
|
||||
super().__init__(mqtt_client, topic)
|
||||
#
|
||||
self.store_data(**{self.KEY_OUTPUT_0: "off", self.KEY_OUTPUT_1: "off", self.KEY_INPUT_0: "off", self.KEY_INPUT_1: "off"})
|
||||
self.__input_0_func = input_0_func
|
||||
self.__input_1_func = input_1_func
|
||||
self.__output_0_auto_off__ = output_0_auto_off
|
||||
if self.__output_0_auto_off__ is not None:
|
||||
self.__delayed_off__ = task.delayed(float(self.__output_0_auto_off__), self.__auto_off__, self.KEY_OUTPUT_0)
|
||||
#
|
||||
self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_OUTPUT_0, self.__start_auto_off__, "on")
|
||||
self.add_callback(self.KEY_OUTPUT_0, self.__stop_auto_off__, "off")
|
||||
self.add_callback(self.KEY_OUTPUT_1, self.print_formatted, None)
|
||||
#
|
||||
self.add_callback(self.KEY_INPUT_0, self.__input_function__, None)
|
||||
self.add_callback(self.KEY_INPUT_1, self.__input_function__, None)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
value = payload_filter(message.payload)
|
||||
if message.topic.startswith(self.topic) and message.topic.endswith("/command"):
|
||||
key = '/'.join(message.topic.split('/')[-3:-1])
|
||||
if value == 'toggle':
|
||||
self.__toggle_data__(key)
|
||||
else:
|
||||
self.__set_data__(key, value)
|
||||
|
||||
def __tx__(self, keys_changed):
|
||||
for key in keys_changed:
|
||||
self.mqtt_client.send(self.topic + '/' + key, self.data.get(key))
|
||||
|
||||
def __input_function__(self, device, key, data):
|
||||
if key == self.KEY_INPUT_0:
|
||||
func = self.__input_0_func
|
||||
elif key == self.KEY_INPUT_1:
|
||||
func = self.__input_1_func
|
||||
else:
|
||||
func = None
|
||||
if func == self.INPUT_FUNC_OUT1_FOLLOW:
|
||||
self.__set_data__(self.KEY_OUTPUT_0, data)
|
||||
elif func == self.INPUT_FUNC_OUT1_TRIGGER:
|
||||
self.__toggle_data__(self.KEY_OUTPUT_0)
|
||||
elif func == self.INPUT_FUNC_OUT2_FOLLOW:
|
||||
self.__set_data__(self.KEY_OUTPUT_1, data)
|
||||
elif func == self.INPUT_FUNC_OUT2_TRIGGER:
|
||||
self.__toggle_data__(self.KEY_OUTPUT_1)
|
||||
|
||||
def __start_auto_off__(self, device, key, data):
|
||||
self.__stop_auto_off__(device, key, data)
|
||||
if self.__output_0_auto_off__ is not None:
|
||||
self.__delayed_off__.run()
|
||||
|
||||
def __stop_auto_off__(self, device, key, data):
|
||||
if self.__output_0_auto_off__ is not None:
|
||||
if not self.__delayed_off__._stopped:
|
||||
self.__delayed_off__.stop()
|
||||
|
||||
def __auto_off__(self, key):
|
||||
if key == self.KEY_OUTPUT_0:
|
||||
self.__set_data__(key, 'off')
|
||||
|
||||
def __set_data__(self, key, value):
|
||||
if value in ["on", "off"]:
|
||||
self.store_data(**{key: value})
|
||||
else:
|
||||
print("Wrong value (%s)!" % repr(value))
|
||||
|
||||
def __toggle_data__(self, key):
|
||||
if key in self.data:
|
||||
self.__set_data__(key, 'on' if self.data.get(key) == 'off' else 'off')
|
||||
|
||||
def command(self, command):
|
||||
if command in self.COMMANDS:
|
||||
if command == self.COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
||||
elif command == self.COMMANDS[1]:
|
||||
self.__toggle_data__(self.KEY_OUTPUT_0)
|
||||
elif command == self.COMMANDS[2]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_1, self.data.get(self.KEY_OUTPUT_1))
|
||||
elif command == self.COMMANDS[3]:
|
||||
self.__toggle_data__(self.KEY_OUTPUT_1)
|
||||
elif command == self.COMMANDS[4]:
|
||||
self.print_formatted(self, self.KEY_INPUT_0, self.data.get(self.KEY_INPUT_0))
|
||||
elif command == self.COMMANDS[5]:
|
||||
self.__toggle_data__(self.KEY_INPUT_0)
|
||||
elif command == self.COMMANDS[6]:
|
||||
self.print_formatted(self, self.KEY_INPUT_1, self.data.get(self.KEY_INPUT_1))
|
||||
elif command == self.COMMANDS[7]:
|
||||
self.__toggle_data__(self.KEY_INPUT_1)
|
||||
elif command == self.COMMANDS[8]:
|
||||
self.__toggle_data__(self.KEY_INPUT_0)
|
||||
time.sleep(0.4)
|
||||
self.__set_data__(self.KEY_LONGPUSH_0, 'on')
|
||||
time.sleep(0.1)
|
||||
self.__set_data__(self.KEY_LONGPUSH_0, 'off')
|
||||
elif command == self.COMMANDS[9]:
|
||||
self.__toggle_data__(self.KEY_INPUT_1)
|
||||
time.sleep(0.4)
|
||||
self.__set_data__(self.KEY_LONGPUSH_1, 'on')
|
||||
time.sleep(0.1)
|
||||
self.__set_data__(self.KEY_LONGPUSH_1, 'off')
|
||||
else:
|
||||
print("%s: not yet implemented!" % command)
|
||||
else:
|
||||
print("Unknown command!")
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
if value is not None:
|
||||
info = (" - %ds" % self.__output_0_auto_off__) if self.__output_0_auto_off__ is not None and value == "on" else ""
|
||||
channel = "(%s%s)" % (self.names.get(key, key), info)
|
||||
print_light(COLOR_SHELLY, value == "on", self.topic, channel)
|
||||
|
||||
|
||||
class my_powerplug(base):
|
||||
KEY_OUTPUT_0 = "0"
|
||||
KEY_OUTPUT_1 = "1"
|
||||
KEY_OUTPUT_2 = "2"
|
||||
KEY_OUTPUT_3 = "3"
|
||||
COMMANDS = [
|
||||
"get_output_0", "toggle_output_0",
|
||||
"get_output_1", "toggle_output_1",
|
||||
"get_output_2", "toggle_output_2",
|
||||
"get_output_3", "toggle_output_3",
|
||||
]
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
#
|
||||
for i in range(0, 4):
|
||||
self.data[str(i)] = False
|
||||
self.add_callback(str(i), self.print_formatted, None)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
if message.topic.startswith(self.topic + '/output/') and message.topic.endswith('/set'):
|
||||
try:
|
||||
channels = [int(message.topic.split('/')[-2]) - 1]
|
||||
except ValueError:
|
||||
channels = range(0, 4)
|
||||
for channel in channels:
|
||||
payload = payload_filter(message.payload)
|
||||
if payload == "toggle":
|
||||
payload = not self.data.get(str(channel))
|
||||
self.store_data(**{str(channel): payload})
|
||||
|
||||
def __tx__(self, keys_changed):
|
||||
for key in keys_changed:
|
||||
self.mqtt_client.send(self.topic + "/output/" + str(int(key) + 1), json.dumps(self.data.get(key)))
|
||||
|
||||
def command(self, command):
|
||||
if command in self.COMMANDS:
|
||||
if command == self.COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
||||
elif command == self.COMMANDS[1]:
|
||||
self.store_data(**{self.KEY_OUTPUT_0: not self.data.get(self.KEY_OUTPUT_0)})
|
||||
elif command == self.COMMANDS[2]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_1, self.data.get(self.KEY_OUTPUT_1))
|
||||
elif command == self.COMMANDS[3]:
|
||||
self.store_data(**{self.KEY_OUTPUT_1: not self.data.get(self.KEY_OUTPUT_1)})
|
||||
elif command == self.COMMANDS[4]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_2, self.data.get(self.KEY_OUTPUT_2))
|
||||
elif command == self.COMMANDS[5]:
|
||||
self.store_data(**{self.KEY_OUTPUT_2: not self.data.get(self.KEY_OUTPUT_2)})
|
||||
elif command == self.COMMANDS[6]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_3, self.data.get(self.KEY_OUTPUT_3))
|
||||
elif command == self.COMMANDS[7]:
|
||||
self.store_data(**{self.KEY_OUTPUT_3: not self.data.get(self.KEY_OUTPUT_3)})
|
||||
else:
|
||||
print("%s: not yet implemented!" % command)
|
||||
else:
|
||||
print("Unknown command!")
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
if value is not None:
|
||||
print_light(COLOR_POWERPLUG, value, self.topic, "(%s)" % self.names.get(key, "Channel %d" % (int(key) + 1)))
|
||||
|
||||
|
||||
class silvercrest_powerplug(base):
|
||||
KEY_OUTPUT_0 = "state"
|
||||
#
|
||||
COMMANDS = [
|
||||
"get_state", "set_state", "unset_state",
|
||||
]
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
||||
#
|
||||
self.store_data(**{self.KEY_OUTPUT_0: "off"})
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
if message.topic == self.topic + '/set':
|
||||
STATES = ["on", "off", "toggle"]
|
||||
#
|
||||
state = json.loads(message.payload).get('state').lower()
|
||||
if state in STATES:
|
||||
if state == STATES[0]:
|
||||
self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
||||
elif state == STATES[1]:
|
||||
self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
||||
else:
|
||||
self.store_data(**{self.KEY_OUTPUT_0: "off" if self.data.get(self.KEY_OUTPUT_0) == "on" else "on"})
|
||||
|
||||
def command(self, command):
|
||||
if command in self.COMMANDS:
|
||||
if command == self.COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
||||
elif command == self.COMMANDS[1]:
|
||||
self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
||||
elif command == self.COMMANDS[2]:
|
||||
self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
||||
else:
|
||||
print("%s: not yet implemented!" % command)
|
||||
else:
|
||||
print("Unknown command!")
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
if value is not None:
|
||||
print_light(COLOR_POWERPLUG, value == "on", self.topic, "(%s)" % self.names.get(key, key))
|
||||
|
||||
|
||||
class silvercrest_motion_sensor(base):
|
||||
KEY_OCCUPANCY = "occupancy"
|
||||
COMMANDS = ['motion']
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.data[self.KEY_OCCUPANCY] = False
|
||||
self.add_callback(self.KEY_OCCUPANCY, self.print_formatted, None)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
pass
|
||||
|
||||
def command(self, command):
|
||||
try:
|
||||
command, value = command.split(' ')
|
||||
except ValueError:
|
||||
value = None
|
||||
else:
|
||||
value = json.loads(value)
|
||||
if command == self.COMMANDS[0]:
|
||||
self.store_data(**{self.KEY_OCCUPANCY: True})
|
||||
time.sleep(value or 10)
|
||||
self.store_data(**{self.KEY_OCCUPANCY: False})
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
if value is not None:
|
||||
print_light(COLOR_MOTION_SENSOR, value, self.topic, "")
|
||||
|
||||
|
||||
class tradfri_light(base):
|
||||
KEY_STATE = "state"
|
||||
KEY_BRIGHTNESS = "brightness"
|
||||
KEY_COLOR_TEMP = "color_temp"
|
||||
KEY_BRIGHTNESS_MOVE = "brightness_move"
|
||||
#
|
||||
STATE_COMMANDS = ("get_state", "toggle_state", )
|
||||
BRIGHTNESS_COMMANDS = ("get_brightness", "set_brightness",)
|
||||
COLOR_TEMP_COMMANDS = ("get_color_temp", "set_color_temp",)
|
||||
|
||||
def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False, send_on_power_on=True):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.send_on_power_on = send_on_power_on
|
||||
self.add_callback(self.KEY_STATE, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_BRIGHTNESS, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_COLOR_TEMP, self.print_formatted, None)
|
||||
#
|
||||
self.commands = []
|
||||
if enable_state:
|
||||
self.commands.extend(self.STATE_COMMANDS)
|
||||
if enable_brightness:
|
||||
self.commands.extend(self.BRIGHTNESS_COMMANDS)
|
||||
if enable_color_temp:
|
||||
self.commands.extend(self.COLOR_TEMP_COMMANDS)
|
||||
self.__init_data__(enable_state, enable_brightness, enable_color_temp)
|
||||
|
||||
def __init_data__(self, enable_state, enable_brightness, enable_color_temp):
|
||||
data = {}
|
||||
if enable_state:
|
||||
data[self.KEY_STATE] = 'off'
|
||||
self.commands.extend(self.STATE_COMMANDS)
|
||||
if enable_brightness:
|
||||
data[self.KEY_BRIGHTNESS] = 128
|
||||
self.brightnes_move = (0, time.time())
|
||||
self.commands.extend(self.BRIGHTNESS_COMMANDS)
|
||||
if enable_color_temp:
|
||||
data[self.KEY_COLOR_TEMP] = 352
|
||||
self.commands.extend(self.COLOR_TEMP_COMMANDS)
|
||||
self.store_data(**data)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
data = json.loads(message.payload)
|
||||
if self.data.get(self.KEY_STATE) == 'on' or data.get(self.KEY_STATE) in ['on', 'toggle']:
|
||||
if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
||||
for targetkey in data:
|
||||
value = data[targetkey]
|
||||
if targetkey in self.data.keys():
|
||||
if targetkey == self.KEY_STATE and value == "toggle":
|
||||
value = "on" if self.data.get(self.KEY_STATE) == "off" else "off"
|
||||
self.store_data(**{targetkey: value})
|
||||
else:
|
||||
if targetkey == self.KEY_BRIGHTNESS_MOVE:
|
||||
new_value = self.data.get(self.KEY_BRIGHTNESS) + (time.time() - self.brightnes_move[1]) * self.brightnes_move[0]
|
||||
if new_value < 0:
|
||||
new_value = 0
|
||||
if new_value > 255:
|
||||
new_value = 255
|
||||
self.store_data(**{self.KEY_BRIGHTNESS: int(new_value)})
|
||||
self.brightnes_move = (value, time.time())
|
||||
else:
|
||||
print("%s: UNKNOWN KEY %s" % (message.topic, targetkey))
|
||||
elif message.topic == self.topic + '/get':
|
||||
self.__tx__(None)
|
||||
|
||||
def command(self, command):
|
||||
try:
|
||||
command, value = command.split(' ')
|
||||
except ValueError:
|
||||
value = None
|
||||
if command in self.capabilities():
|
||||
if command == self.STATE_COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_STATE, self.data.get(self.KEY_STATE))
|
||||
elif command == self.STATE_COMMANDS[1]:
|
||||
self.store_data(**{self.KEY_STATE: 'off' if self.data.get(self.KEY_STATE) == 'on' else 'on'})
|
||||
elif command == self.BRIGHTNESS_COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
||||
elif command == self.BRIGHTNESS_COMMANDS[1]:
|
||||
self.store_data(**{self.KEY_BRIGHTNESS: command_int_value(value)})
|
||||
elif command == self.COLOR_TEMP_COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
||||
elif command == self.COLOR_TEMP_COMMANDS[1]:
|
||||
self.store_data(**{self.KEY_COLOR_TEMP: command_int_value(value)})
|
||||
else:
|
||||
print("%s: not yet implemented!" % command)
|
||||
else:
|
||||
print("Unknown command!")
|
||||
|
||||
def power_off(self, device, key, value):
|
||||
self.data[self.KEY_STATE] = 'off'
|
||||
self.print_formatted(self, self.KEY_STATE, 'off')
|
||||
|
||||
def power_on(self, device, key, value):
|
||||
if self.send_on_power_on:
|
||||
self.store_data(**{self.KEY_STATE: 'on'})
|
||||
else:
|
||||
self.data[self.KEY_STATE] = 'on'
|
||||
self.print_formatted(self, self.KEY_STATE, 'on')
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
if value is not None:
|
||||
color = COLOR_LIGHT_ACTIVE
|
||||
if key == self.KEY_STATE:
|
||||
print_light(COLOR_LIGHT_ACTIVE, value == "on", self.topic, "")
|
||||
self.print_formatted(device, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
||||
self.print_formatted(device, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
||||
elif key in [self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP]:
|
||||
perc_value = round(value * 100 / 256, 0) if key == self.KEY_BRIGHTNESS else round((value - 250) * 100 / 204, 0)
|
||||
print_percent(
|
||||
COLOR_LIGHT_PASSIVE if self.data.get(self.KEY_STATE) != "on" else COLOR_LIGHT_ACTIVE,
|
||||
'B' if key == gui_light.KEY_BRIGHTNESS else 'C',
|
||||
perc_value,
|
||||
"%3d%%" % perc_value,
|
||||
self.topic,
|
||||
""
|
||||
)
|
||||
|
||||
|
||||
class gui_light(tradfri_light):
|
||||
AUTOSEND = False
|
||||
#
|
||||
KEY_ENABLE = "enable"
|
||||
KEY_TIMER = "timer"
|
||||
KEY_LED_X = "led%d"
|
||||
|
||||
def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False):
|
||||
super().__init__(mqtt_client, topic, enable_state, enable_brightness, enable_color_temp)
|
||||
self.add_callback(self.KEY_ENABLE, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_TIMER, self.print_formatted, None)
|
||||
for i in range(0, 10):
|
||||
self.add_callback(self.KEY_LED_X % i, self.print_formatted, None)
|
||||
self.led_names = {}
|
||||
#
|
||||
self.maxvalue = None
|
||||
self.last_printed = None
|
||||
|
||||
def __init_data__(self, enable_state, enable_brightness, enable_color_temp):
|
||||
data = {}
|
||||
data[self.KEY_ENABLE] = False
|
||||
if enable_state:
|
||||
data[self.KEY_STATE] = False
|
||||
if enable_brightness:
|
||||
data[self.KEY_BRIGHTNESS] = 50
|
||||
if enable_color_temp:
|
||||
data[self.KEY_COLOR_TEMP] = 5
|
||||
data[self.KEY_TIMER] = '-'
|
||||
for i in range(0, 10):
|
||||
data[self.KEY_LED_X % i] = False
|
||||
self.store_data(**data)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
value = payload_filter(message.payload)
|
||||
if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
||||
targetkey = message.topic.split('/')[-2]
|
||||
if targetkey in self.data.keys():
|
||||
self.store_data(**{targetkey: value})
|
||||
else:
|
||||
print("Unknown key %s in %s::%s" % (targetkey, message.topic, self.__class__.__name__))
|
||||
elif message.topic == self.topic + '/get':
|
||||
self.__tx__(None)
|
||||
|
||||
def send(self, key, data):
|
||||
if data is not None:
|
||||
topic = self.topic + '/' + key
|
||||
self.mqtt_client.send(topic, json.dumps(data))
|
||||
|
||||
def command(self, command):
|
||||
try:
|
||||
command, value = command.split(' ')
|
||||
except ValueError:
|
||||
value = None
|
||||
if command in self.capabilities():
|
||||
if command == self.STATE_COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_STATE, self.data.get(self.KEY_STATE))
|
||||
elif command == self.STATE_COMMANDS[1]:
|
||||
self.send(self.KEY_STATE, not self.data.get(self.KEY_STATE))
|
||||
elif command == self.BRIGHTNESS_COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
||||
elif command == self.BRIGHTNESS_COMMANDS[1]:
|
||||
self.send(self.KEY_BRIGHTNESS, command_int_value(value))
|
||||
elif command == self.COLOR_TEMP_COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
||||
elif command == self.COLOR_TEMP_COMMANDS[1]:
|
||||
self.send(self.KEY_COLOR_TEMP, command_int_value(value))
|
||||
else:
|
||||
print("%s: not yet implemented!" % command)
|
||||
else:
|
||||
print("Unknown command!")
|
||||
|
||||
def add_led_name(self, key, name):
|
||||
self.led_names[key] = name
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
if value is not None:
|
||||
device = " - ".join(self.topic.split('/')[1:])
|
||||
if key == self.KEY_STATE:
|
||||
print_switch(COLOR_GUI_ACTIVE, value, self.topic, "")
|
||||
elif key == self.KEY_ENABLE:
|
||||
self.print_formatted(device, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
||||
self.print_formatted(device, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
||||
elif key in [self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP]:
|
||||
perc_value = round(value * 10 if key == self.KEY_COLOR_TEMP else value, 0)
|
||||
print_percent(
|
||||
COLOR_GUI_PASSIVE if not self.data.get(self.KEY_ENABLE, False) else COLOR_GUI_ACTIVE,
|
||||
'B' if key == self.KEY_BRIGHTNESS else 'C',
|
||||
perc_value,
|
||||
"%3d%%" % perc_value,
|
||||
self.topic,
|
||||
""
|
||||
)
|
||||
elif key == self.KEY_TIMER:
|
||||
if isinstance(value, (int, float, complex)) and not isinstance(value, bool):
|
||||
if self.maxvalue is None:
|
||||
self.maxvalue = value
|
||||
disp_value = value
|
||||
perc = disp_value / self.maxvalue * 100
|
||||
else:
|
||||
disp_value = 0
|
||||
perc = 0
|
||||
self.maxvalue = None
|
||||
self.last_printed = None
|
||||
if self.last_printed is None or abs(self.last_printed - perc) >= 4.95:
|
||||
print_percent(COLOR_GUI_ACTIVE, 't', perc, '%3d%%' % perc, self.topic, '(%.1f)' % disp_value)
|
||||
self.last_printed = perc
|
||||
elif key.startswith(self.KEY_LED_X[:-2]):
|
||||
print_light(COLOR_GUI_ACTIVE, value, self.topic, '(%s)' % self.led_names.get(key, key), True)
|
||||
|
||||
|
||||
class tradfri_button(base):
|
||||
KEY_ACTION = "action"
|
||||
#
|
||||
ACTION_TOGGLE = "toggle"
|
||||
ACTION_BRIGHTNESS_UP = "brightness_up_click"
|
||||
ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
|
||||
ACTION_RIGHT = "arrow_right_click"
|
||||
ACTION_LEFT = "arrow_left_click"
|
||||
ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
|
||||
ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
|
||||
ACTION_RIGHT_LONG = "arrow_right_hold"
|
||||
ACTION_LEFT_LONG = "arrow_left_hold"
|
||||
#
|
||||
COMMANDS = [ACTION_TOGGLE, ACTION_LEFT, ACTION_RIGHT, ACTION_BRIGHTNESS_UP, ACTION_BRIGHTNESS_DOWN,
|
||||
ACTION_LEFT_LONG, ACTION_RIGHT_LONG, ACTION_BRIGHTNESS_UP_LONG, ACTION_BRIGHTNESS_DOWN_LONG]
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
pass
|
||||
|
||||
def command(self, command):
|
||||
try:
|
||||
command, value = command.split(' ')
|
||||
except ValueError:
|
||||
value = None
|
||||
else:
|
||||
value = json.loads(value)
|
||||
if command in self.capabilities():
|
||||
action = self.COMMANDS[self.COMMANDS.index(command)]
|
||||
if self.COMMANDS.index(command) <= 4:
|
||||
self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
||||
elif self.COMMANDS.index(command) <= 8:
|
||||
self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
||||
time.sleep(value or 0.5)
|
||||
action = '_'.join(action.split('_')[:-1] + ['release'])
|
||||
self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
||||
|
||||
|
||||
class gui_led_array(base):
|
||||
AUTOSEND = False
|
||||
#
|
||||
KEY_LED_0 = "led0"
|
||||
KEY_LED_1 = "led1"
|
||||
KEY_LED_2 = "led2"
|
||||
KEY_LED_3 = "led3"
|
||||
KEY_LED_4 = "led4"
|
||||
KEY_LED_5 = "led5"
|
||||
KEY_LED_6 = "led6"
|
||||
KEY_LED_7 = "led7"
|
||||
KEY_LED_8 = "led8"
|
||||
KEY_LED_9 = "led9"
|
||||
|
||||
def __init__(self, mqtt_client, topic, ):
|
||||
super().__init__(mqtt_client, topic)
|
||||
for i in range(0, 10):
|
||||
key = getattr(self, "KEY_LED_%d" % i)
|
||||
self.data[key] = False
|
||||
self.add_callback(key, self.print_formatted, None)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
value = payload_filter(message.payload)
|
||||
if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
||||
targetkey = message.topic.split('/')[-2]
|
||||
if targetkey in self.data.keys():
|
||||
self.store_data(**{targetkey: value})
|
||||
else:
|
||||
print("Unknown key %s in %s" % (targetkey, self.__class__.__name__))
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
print_light(COLOR_GUI_ACTIVE, value, self.topic, '(%s)' % self.names.get(key, key), True)
|
||||
|
||||
|
||||
class remote(base):
|
||||
def __rx__(self, client, userdata, message):
|
||||
if message.topic == self.topic + "/VOLUP":
|
||||
if payload_filter(message.payload):
|
||||
icon = u'\u1403'
|
||||
else:
|
||||
icon = u'\u25a1'
|
||||
elif message.topic == self.topic + "/VOLDOWN":
|
||||
if payload_filter(message.payload):
|
||||
icon = u'\u1401'
|
||||
else:
|
||||
icon = u'\u25a1'
|
||||
else:
|
||||
return
|
||||
devicename = ' - '.join(self.topic.split('/')[1:-1])
|
||||
print(COLOR_REMOTE + 10 * ' ' + icon + 6 * ' ' + devicename + colored.attr("reset"))
|
||||
|
||||
|
||||
class brennenstuhl_radiator_valve(base):
|
||||
TEMP_RANGE = [10, 30]
|
||||
#
|
||||
KEY_TEMPERATURE_SETPOINT = "current_heating_setpoint"
|
||||
KEY_TEMPERATURE = "local_temperature"
|
||||
#
|
||||
COMMANDS = [
|
||||
"get_temperature_setpoint", "set_temperature_setpoint",
|
||||
]
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.store_data(**{
|
||||
self.KEY_TEMPERATURE_SETPOINT: 20,
|
||||
self.KEY_TEMPERATURE: 20.7,
|
||||
})
|
||||
self.add_callback(self.KEY_TEMPERATURE_SETPOINT, self.print_formatted, None)
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
if message.topic.startswith(self.topic) and message.topic.endswith("/set"):
|
||||
payload = payload_filter(message.payload)
|
||||
self.store_data(**payload)
|
||||
|
||||
def command(self, command):
|
||||
try:
|
||||
command, value = command.split(' ')
|
||||
except ValueError:
|
||||
value = None
|
||||
if command in self.COMMANDS:
|
||||
if command == self.COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_TEMPERATURE_SETPOINT, self.data.get(self.KEY_TEMPERATURE_SETPOINT))
|
||||
elif command == self.COMMANDS[1]:
|
||||
self.store_data(**{self.KEY_TEMPERATURE_SETPOINT: command_float_value(value)})
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
devicename = ' - '.join(self.topic.split('/')[1:])
|
||||
if key == self.KEY_TEMPERATURE_SETPOINT:
|
||||
perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
||||
perc = 100 if perc > 100 else perc
|
||||
perc = 0 if perc < 0 else perc
|
||||
print_percent(COLOR_RADIATOR_VALVE, '\u03d1', perc, "%4.1f°C" % value, self.topic, "")
|
||||
|
||||
|
||||
class gui_radiator_valve(base):
|
||||
AUTOSEND = False
|
||||
#
|
||||
TEMP_RANGE = [10, 30]
|
||||
#
|
||||
KEY_TIMER = "timer"
|
||||
KEY_TEMPERATURE = "temperature"
|
||||
KEY_SETPOINT_TEMP = "setpoint_temp"
|
||||
KEY_SETPOINT_TO_DEFAULT = "setpoint_to_default"
|
||||
KEY_BOOST = 'boost'
|
||||
KEY_AWAY = "away"
|
||||
KEY_SUMMER = "summer"
|
||||
KEY_ENABLE = "enable"
|
||||
#
|
||||
COMMANDS = [
|
||||
"get_temperature",
|
||||
"get_temperature_setpoint", "set_temperature_setpoint",
|
||||
"trigger_boost", "trigger_setpoint_to_default",
|
||||
"toggle_away", "toggle_summer",
|
||||
]
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_callback(self.KEY_SETPOINT_TEMP, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_TIMER, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_AWAY, self.print_formatted, None)
|
||||
self.add_callback(self.KEY_SUMMER, self.print_formatted, None)
|
||||
#
|
||||
self.store_data(**{
|
||||
self.KEY_TEMPERATURE: 20.7,
|
||||
self.KEY_SETPOINT_TEMP: 20,
|
||||
self.KEY_TIMER: 0,
|
||||
self.KEY_AWAY: False,
|
||||
self.KEY_SUMMER: False,
|
||||
self.KEY_ENABLE: True
|
||||
})
|
||||
|
||||
def __rx__(self, client, userdata, message):
|
||||
value = payload_filter(message.payload)
|
||||
if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
||||
targetkey = message.topic.split('/')[-2]
|
||||
if targetkey in self.data.keys():
|
||||
self.store_data(**{targetkey: value})
|
||||
else:
|
||||
print("Unknown key %s in %s::%s" % (targetkey, message.topic, self.__class__.__name__))
|
||||
elif message.topic == self.topic + '/get':
|
||||
self.__tx__(None)
|
||||
|
||||
def send(self, key, data):
|
||||
if data is not None:
|
||||
topic = self.topic + '/' + key
|
||||
self.mqtt_client.send(topic, json.dumps(data))
|
||||
|
||||
def command(self, command):
|
||||
try:
|
||||
command, value = command.split(' ')
|
||||
except ValueError:
|
||||
value = None
|
||||
if command in self.COMMANDS:
|
||||
if command == self.COMMANDS[0]:
|
||||
self.print_formatted(self, self.KEY_TEMPERATURE, self.data.get(self.KEY_TEMPERATURE))
|
||||
elif command == self.COMMANDS[1]:
|
||||
self.print_formatted(self, self.KEY_SETPOINT_TEMP, self.data.get(self.KEY_SETPOINT_TEMP))
|
||||
elif command == self.COMMANDS[2]:
|
||||
self.send(self.KEY_SETPOINT_TEMP, command_float_value(value))
|
||||
elif command == self.COMMANDS[3]:
|
||||
self.send(self.KEY_BOOST, True)
|
||||
elif command == self.COMMANDS[4]:
|
||||
self.send(self.KEY_SETPOINT_TO_DEFAULT, True)
|
||||
elif command == self.COMMANDS[5]:
|
||||
self.send(self.KEY_AWAY, not self.data.get(self.KEY_AWAY))
|
||||
elif command == self.COMMANDS[6]:
|
||||
self.send(self.KEY_SUMMER, not self.data.get(self.KEY_SUMMER))
|
||||
|
||||
def print_formatted(self, device, key, value):
|
||||
devicename = ' - '.join(self.topic.split('/')[1:])
|
||||
if key == self.KEY_TIMER:
|
||||
try:
|
||||
perc = 100 * value / 60
|
||||
except TypeError:
|
||||
value = 0
|
||||
perc = 0
|
||||
print_percent(COLOR_GUI_ACTIVE, 't', perc, "%4.1fmin" % value, self.topic, "(Timer)")
|
||||
elif key == self.KEY_TEMPERATURE:
|
||||
perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
||||
perc = 100 if perc > 100 else perc
|
||||
perc = 0 if perc < 0 else perc
|
||||
print_percent(COLOR_GUI_ACTIVE, '\u03d1', perc, "%4.1f°C" % value, self.topic, "(Temperature)")
|
||||
elif key == self.KEY_SETPOINT_TEMP:
|
||||
perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
||||
perc = 100 if perc > 100 else perc
|
||||
perc = 0 if perc < 0 else perc
|
||||
print_percent(COLOR_GUI_ACTIVE if self.data.get(self.KEY_ENABLE) else COLOR_GUI_PASSIVE,
|
||||
'\u03d1', perc, "%4.1f°C" % value, self.topic, "(Setpoint)")
|
||||
elif key == self.KEY_AWAY:
|
||||
print_switch(COLOR_GUI_ACTIVE, value, self.topic, "(Away Mode)")
|
||||
elif key == self.KEY_SUMMER:
|
||||
print_switch(COLOR_GUI_ACTIVE, value, self.topic, "(Summer Mode)")
|
@ -1,241 +0,0 @@
|
||||
import config
|
||||
from __simulation__.devices import shelly, silvercrest_powerplug, tradfri_light, tradfri_button, silvercrest_motion_sensor, my_powerplug, remote, brennenstuhl_radiator_valve
|
||||
from __simulation__.devices import gui_light, gui_led_array, gui_radiator_valve
|
||||
import inspect
|
||||
|
||||
|
||||
class base(object):
|
||||
def getmembers(self, prefix=''):
|
||||
rv = []
|
||||
for name, obj in inspect.getmembers(self):
|
||||
if prefix:
|
||||
full_name = prefix + '.' + name
|
||||
else:
|
||||
full_name = name
|
||||
if not name.startswith('_'):
|
||||
try:
|
||||
if obj.__module__.endswith('devices'):
|
||||
rv.append(full_name)
|
||||
else:
|
||||
rv.extend(obj.getmembers(full_name))
|
||||
except AttributeError:
|
||||
pass
|
||||
return rv
|
||||
|
||||
def getobjbyname(self, name):
|
||||
obj = self
|
||||
for subname in name.split('.'):
|
||||
obj = getattr(obj, subname)
|
||||
return obj
|
||||
|
||||
def command(self, full_command):
|
||||
try:
|
||||
parameter = " " + full_command.split(' ')[1]
|
||||
except IndexError:
|
||||
parameter = ""
|
||||
command = full_command.split(' ')[0].split('.')[-1] + parameter
|
||||
device_name = '.'.join(full_command.split(' ')[0].split('.')[:-1])
|
||||
self.getobjbyname(device_name).command(command)
|
||||
|
||||
|
||||
class gfw_floor(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI, True, True, True)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.main_light_zigbee_1 = tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_1_ZIGBEE, True, True, True, False)
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_1.power_on, "on")
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_1.power_off, "off")
|
||||
self.main_light_zigbee_2 = tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_2_ZIGBEE, True, True, True, False)
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_2.power_on, "on")
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_1.power_off, "off")
|
||||
|
||||
|
||||
class gfw_marion(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_GUI, True, False, False)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.radiator_valve = brennenstuhl_radiator_valve(mqtt_client, config.TOPIC_GFW_MARION_RADIATOR_VALVE_ZIGBEE)
|
||||
self.gui_radiator_valve = gui_radiator_valve(mqtt_client, config.TOPIC_GFW_MARION_RADIATOR_VALVE_GUI)
|
||||
|
||||
|
||||
class gfw_dirk(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI, True, True, True)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_ZIGBEE, True, True, True)
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
|
||||
#
|
||||
self.powerplug = my_powerplug(mqtt_client, config.TOPIC_GFW_DIRK_POWERPLUG)
|
||||
self.powerplug.add_channel_name(my_powerplug.KEY_OUTPUT_0, "Amplifier")
|
||||
self.powerplug.add_channel_name(my_powerplug.KEY_OUTPUT_1, "Desk_Light")
|
||||
self.powerplug.add_channel_name(my_powerplug.KEY_OUTPUT_2, "CD_Player")
|
||||
self.powerplug.add_channel_name(my_powerplug.KEY_OUTPUT_3, "PC_Dock")
|
||||
self.gui_amplifier = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_GUI, True, False, False)
|
||||
self.gui_desk_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI, True, True, True)
|
||||
self.desk_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_ZIGBEE, True, True, True)
|
||||
self.powerplug.add_callback(my_powerplug.KEY_OUTPUT_1, self.desk_light_zigbee.power_on, True)
|
||||
self.powerplug.add_callback(my_powerplug.KEY_OUTPUT_1, self.desk_light_zigbee.power_off, False)
|
||||
self.gui_cd_player = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_GUI, True, False, False)
|
||||
self.gui_pc_dock = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_GUI, True, False, False)
|
||||
#
|
||||
self.remote = remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
|
||||
#
|
||||
self.input_device = tradfri_button(mqtt_client, config.TOPIC_GFW_DIRK_INPUT_DEVICE)
|
||||
self.led_array = gui_led_array(mqtt_client, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED)
|
||||
self.led_array.add_channel_name(gui_led_array.KEY_LED_0, "Main Light")
|
||||
self.led_array.add_channel_name(gui_led_array.KEY_LED_1, "Desk Light")
|
||||
self.led_array.add_channel_name(gui_led_array.KEY_LED_2, "Amplifier")
|
||||
#
|
||||
self.radiator_valve = brennenstuhl_radiator_valve(mqtt_client, config.TOPIC_GFW_DIRK_RADIATOR_VALVE_ZIGBEE)
|
||||
self.gui_radiator_valve = gui_radiator_valve(mqtt_client, config.TOPIC_GFW_DIRK_RADIATOR_VALVE_GUI)
|
||||
|
||||
|
||||
class gfw(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.floor = gfw_floor(mqtt_client)
|
||||
self.marion = gfw_marion(mqtt_client)
|
||||
self.dirk = gfw_dirk(mqtt_client)
|
||||
|
||||
|
||||
class ffw_julian(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI, True, True, True)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_ZIGBEE, True, True, True)
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
|
||||
|
||||
|
||||
class ffw_livingroom(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_GUI, True, True, True)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
|
||||
|
||||
class ffw_sleep(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_GUI, True, True, False)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
|
||||
|
||||
class ffw_bath(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.radiator_valve = brennenstuhl_radiator_valve(mqtt_client, config.TOPIC_FFW_BATH_RADIATOR_VALVE_ZIGBEE)
|
||||
self.gui_radiator_valve = gui_radiator_valve(mqtt_client, config.TOPIC_FFW_BATH_RADIATOR_VALVE_GUI)
|
||||
|
||||
|
||||
class ffw(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.julian = ffw_julian(mqtt_client)
|
||||
self.livingroom = ffw_livingroom(mqtt_client)
|
||||
self.sleep = ffw_sleep(mqtt_client)
|
||||
self.bath = ffw_bath(mqtt_client)
|
||||
|
||||
|
||||
class ffe_floor(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_GUI, True, False, False)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
|
||||
|
||||
class ffe_kitchen(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_GUI, True, False, False)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
#
|
||||
self.gui_circulation_pump = gui_light(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI, True, False, False)
|
||||
self.circulation_pump = shelly(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_SHELLY,
|
||||
input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER, output_0_auto_off=10*60)
|
||||
self.circulation_pump.add_channel_name(shelly.KEY_OUTPUT_0, "Circulation Pump")
|
||||
|
||||
|
||||
class ffe_diningroom(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_GUI, True, False, False)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.gui_floor_lamp = gui_light(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_GUI, True, False, False)
|
||||
self.floor_lamp = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_POWERPLUG)
|
||||
self.floor_lamp.add_channel_name(silvercrest_powerplug.KEY_OUTPUT_0, "Floor Lamp")
|
||||
if config.CHRISTMAS:
|
||||
self.garland = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_DININGROOM_GARLAND_POWERPLUG)
|
||||
self.garland.add_channel_name(silvercrest_powerplug, "Garland")
|
||||
|
||||
|
||||
class ffe_sleep(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_GUI, True, True, True)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_ZIGBEE, True, True, True)
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
|
||||
#
|
||||
self.gui_bed_light_di = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_GUI, True, True, False)
|
||||
self.bed_light_di_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_ZIGBEE, True, True, False)
|
||||
self.gui_bed_light_ma = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_GUI, True, False, False)
|
||||
self.bed_light_ma_powerplug = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_POWERPLUG)
|
||||
#
|
||||
self.input_device = tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE)
|
||||
self.led_array = gui_led_array(mqtt_client, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_LED)
|
||||
self.led_array.add_channel_name(gui_led_array.KEY_LED_0, "Main Light")
|
||||
self.led_array.add_channel_name(gui_led_array.KEY_LED_1, "Bed Light Dirk")
|
||||
#
|
||||
self.radiator_valve = brennenstuhl_radiator_valve(mqtt_client, config.TOPIC_FFE_SLEEP_RADIATOR_VALVE_ZIGBEE)
|
||||
self.gui_radiator_valve = gui_radiator_valve(mqtt_client, config.TOPIC_FFE_SLEEP_RADIATOR_VALVE_GUI)
|
||||
|
||||
|
||||
class ffe_livingroom(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_GUI, True, True, True)
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_ZIGBEE, True, True, True)
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
|
||||
self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
|
||||
for i in range(1, 7):
|
||||
setattr(self, "floor_lamp_zigbee_%d" % i, tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_ZIGBEE % i, True, True, True))
|
||||
self.gui_floor_lamp = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_GUI, True, True, True)
|
||||
if config.CHRISTMAS:
|
||||
self.xmas_tree = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_POWERPLUG)
|
||||
self.xmas_tree.add_channel_name(silvercrest_powerplug, "Xmas-Tree")
|
||||
self.gui_xmas_tree = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_GUI)
|
||||
self.xmas_star = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_STAR_POWERPLUG)
|
||||
self.xmas_star.add_channel_name(silvercrest_powerplug, "Xmas-Star")
|
||||
|
||||
|
||||
class ffe(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.floor = ffe_floor(mqtt_client)
|
||||
self.kitchen = ffe_kitchen(mqtt_client)
|
||||
self.diningroom = ffe_diningroom(mqtt_client)
|
||||
self.sleep = ffe_sleep(mqtt_client)
|
||||
self.livingroom = ffe_livingroom(mqtt_client)
|
||||
|
||||
|
||||
class stairway(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gui_main_light = gui_light(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI, True, False, False)
|
||||
self.gui_main_light.add_led_name(self.gui_main_light.KEY_LED_X % 0, "Motion Ground Floor")
|
||||
self.gui_main_light.add_led_name(self.gui_main_light.KEY_LED_X % 1, "Motion First Floor")
|
||||
self.main_light = shelly(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY, input_0_func=shelly.INPUT_FUNC_OUT1_TRIGGER)
|
||||
self.main_light.add_channel_name(shelly.KEY_OUTPUT_0, "Main Light")
|
||||
self.motion_sensor_gf = silvercrest_motion_sensor(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF)
|
||||
self.motion_sensor_ff = silvercrest_motion_sensor(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF)
|
||||
|
||||
|
||||
class house(base):
|
||||
def __init__(self, mqtt_client):
|
||||
self.gfw = gfw(mqtt_client)
|
||||
self.ffw = ffw(mqtt_client)
|
||||
self.ffe = ffe(mqtt_client)
|
||||
self.stairway = stairway(mqtt_client)
|
@ -1,232 +0,0 @@
|
||||
import colored
|
||||
import time
|
||||
|
||||
|
||||
DT_TOGGLE = 0.1
|
||||
|
||||
|
||||
class test_smarthome(object):
|
||||
def __init__(self, house):
|
||||
self.house = house
|
||||
|
||||
def __smoke__(self):
|
||||
result = ""
|
||||
result += "Smoke-Test\n"
|
||||
result += " GUI Element exists for every shelly instance\n"
|
||||
result += self.__gui_element_exists__("shelly")
|
||||
result += " On-Off test for every shelly instance\n"
|
||||
result += self.__on_off_test__("shelly")
|
||||
result += " GUI Element exists for every silvercrest_powerplug instance\n"
|
||||
result += self.__gui_element_exists__("silvercrest_powerplug")
|
||||
result += " On-Off test for every silvercrest_powerplug instance\n"
|
||||
result += self.__on_off_test__("silvercrest_powerplug")
|
||||
result += " GUI Element exists for every my_powerplug instance and port\n"
|
||||
result += self.__gui_element_exists_my_powerplug__()
|
||||
result += " On-Off test for every my_powerplug instance\n"
|
||||
result += self.__on_off_test_my_powerplug__()
|
||||
result += " GUI Element exists for every tradfri_light instance\n"
|
||||
result += self.__gui_element_exists__("tradfri_light")
|
||||
result += " Enable and disable test for gui elements corresponding with tradfri_light\n"
|
||||
result += self.__br_ct_enable_test__()
|
||||
result += " Chnage brightness and color_temp by gui test\n"
|
||||
result += self.__br_ct_change_test__()
|
||||
return result
|
||||
|
||||
def __full__(self):
|
||||
result = "Full-Test"
|
||||
return result
|
||||
|
||||
def smoke(self):
|
||||
print(self.__smoke__())
|
||||
|
||||
def full(self):
|
||||
out = self.__smoke__()
|
||||
out += self.__full__()
|
||||
print(out)
|
||||
|
||||
def print_error(self, text, lvl=2):
|
||||
return lvl*" " + colored.fg("light_red") + '* ' + text + "\n" + colored.attr("reset")
|
||||
|
||||
def print_success(self, text, lvl=2):
|
||||
return lvl*" " + colored.fg("light_green") + '* ' + text + "\n" + colored.attr("reset")
|
||||
|
||||
def __gui_element_exists__(self, obj_name):
|
||||
result = ""
|
||||
for member in self.house.getmembers():
|
||||
obj = self.house.getobjbyname(member)
|
||||
if obj.__class__.__name__ == obj_name:
|
||||
if obj_name == "tradfri_light":
|
||||
basename = member[:member.index('_zigbee')]
|
||||
else:
|
||||
basename = member
|
||||
try:
|
||||
gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
|
||||
except AttributeError:
|
||||
result += self.print_error("No GUI element available, while testing %s (%s)." % (member, obj_name))
|
||||
else:
|
||||
result += self.print_success("GUI element available, while testing %s (%s)." % (member, obj_name))
|
||||
return result
|
||||
|
||||
def __gui_element_exists_my_powerplug__(self):
|
||||
result = ""
|
||||
for member in self.house.getmembers():
|
||||
obj = self.house.getobjbyname(member)
|
||||
if obj.__class__.__name__ == "my_powerplug":
|
||||
for channel in [obj.KEY_OUTPUT_0, obj.KEY_OUTPUT_1, obj.KEY_OUTPUT_2, obj.KEY_OUTPUT_3]:
|
||||
try:
|
||||
gui = self.house.getobjbyname('.'.join(member.split(
|
||||
'.')[:-1]) + '.gui_' + obj.names.get(channel.lower(), "__dummy__").lower())
|
||||
except AttributeError:
|
||||
result += self.print_error("No GUI element available, while testing %s (%s)." % (member, obj.names.get(channel)))
|
||||
else:
|
||||
result += self.print_success("GUI element available, while testing %s (%s)." % (member, obj.names.get(channel)))
|
||||
return result
|
||||
|
||||
def __on_off_test_my_powerplug__(self):
|
||||
result = ""
|
||||
for member in self.house.getmembers():
|
||||
obj = self.house.getobjbyname(member)
|
||||
if obj.__class__.__name__ == "my_powerplug":
|
||||
for channel in [obj.KEY_OUTPUT_0, obj.KEY_OUTPUT_1, obj.KEY_OUTPUT_2, obj.KEY_OUTPUT_3]:
|
||||
try:
|
||||
gui = self.house.getobjbyname('.'.join(member.split(
|
||||
'.')[:-1]) + '.gui_' + obj.names.get(channel.lower(), "__dummy__").lower())
|
||||
except AttributeError:
|
||||
raise AttributeError
|
||||
pass # exists test already covers non existing gui-elements
|
||||
else:
|
||||
success = True
|
||||
# Initial state equal between obj and gui
|
||||
obj_state = obj.data.get(channel)
|
||||
gui_state = gui.data.get(gui.KEY_STATE)
|
||||
if obj_state != gui_state:
|
||||
result += self.print_error("Initial state of %s (%s) is not equal to GUI state (%s), while testing %s (%s)" %
|
||||
("my_powerplug", obj_state, gui_state, member, obj.names.get(channel)))
|
||||
success = False
|
||||
# state obj change results in state change of obj and gui
|
||||
for i in range(1, 3):
|
||||
gui.command("toggle_state")
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
last_obj_state = obj_state
|
||||
obj_state = obj.data.get(channel)
|
||||
gui_state = gui.data.get(gui.KEY_STATE)
|
||||
if last_obj_state == obj_state:
|
||||
result += self.print_error("State after %d. toggle of gui state: State unchanged (%s), while testing %s (%s)" %
|
||||
(i, obj_state, member, obj.names.get(channel)))
|
||||
success = False
|
||||
if obj_state != gui_state:
|
||||
result += self.print_error("State after %d. toggle of gui state:: State of device (%s) is not equal to GUI state (%s), while testing %s (%s)" %
|
||||
(i, obj_state, gui_state, member, obj.names.get(channel)))
|
||||
success = False
|
||||
#
|
||||
if success:
|
||||
result += self.print_success("On-Off test successfull, while testing %s (%s)." % (member, obj.names.get(channel)))
|
||||
return result
|
||||
|
||||
def __on_off_test__(self, obj_name):
|
||||
result = ""
|
||||
for member in self.house.getmembers():
|
||||
obj = self.house.getobjbyname(member)
|
||||
if obj.__class__.__name__ == obj_name:
|
||||
try:
|
||||
gui = self.house.getobjbyname('.'.join(member.split('.')[:-1]) + '.gui_' + member.split('.')[-1])
|
||||
except AttributeError:
|
||||
pass # exists test already covers non existing gui-elements
|
||||
else:
|
||||
success = True
|
||||
# Initial state equal between obj and gui
|
||||
obj_state = obj.data.get(obj.KEY_OUTPUT_0).lower() == "on"
|
||||
gui_state = gui.data.get(gui.KEY_STATE)
|
||||
if obj_state != gui_state:
|
||||
result += self.print_error("Initial state of %s (%s) is not equal to GUI state (%s), while testing %s (%s)" %
|
||||
(obj_name, obj_state, gui_state, member, obj_name))
|
||||
success = False
|
||||
# state obj change results in state change of obj and gui
|
||||
for i in range(1, 3):
|
||||
gui.command("toggle_state")
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
last_obj_state = obj_state
|
||||
obj_state = obj.data.get(obj.KEY_OUTPUT_0).lower() == "on"
|
||||
gui_state = gui.data.get(gui.KEY_STATE)
|
||||
if last_obj_state == obj_state:
|
||||
result += self.print_error("State after %d. toggle of gui state: State unchanged (%s), while testing %s (%s)" %
|
||||
(i, obj_state, member, obj_name))
|
||||
success = False
|
||||
if obj_state != gui_state:
|
||||
result += self.print_error("State after %d. toggle of gui state:: State of device (%s) is not equal to GUI state (%s), while testing %s (%s)" %
|
||||
(i, obj_state, gui_state, member, obj_name))
|
||||
success = False
|
||||
#
|
||||
if success:
|
||||
result += self.print_success("On-Off test successfull, while testing %s." % (member))
|
||||
return result
|
||||
|
||||
def __br_ct_enable_test__(self):
|
||||
result = ""
|
||||
for member in self.house.getmembers():
|
||||
obj = self.house.getobjbyname(member)
|
||||
if obj.__class__.__name__ == "tradfri_light":
|
||||
basename = member[:member.index('_zigbee')]
|
||||
gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
|
||||
success = True
|
||||
#
|
||||
if gui.data.get(gui.KEY_ENABLE) != False:
|
||||
result += self.print_error("Inital enable state is not False, while testing %s." % (member))
|
||||
success = False
|
||||
#
|
||||
gui.command("toggle_state")
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
if gui.data.get(gui.KEY_ENABLE) != True:
|
||||
result += self.print_error("Enable state is not True after switching on, while testing %s." % (member))
|
||||
success = False
|
||||
#
|
||||
gui.command("toggle_state")
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
if gui.data.get(gui.KEY_ENABLE) != False:
|
||||
result += self.print_error("Enable state is not False after switching off, while testing %s." % (member))
|
||||
success = False
|
||||
#
|
||||
if success:
|
||||
result += self.print_success("Enable-Disable test successfull, while testing %s." % (member))
|
||||
return result
|
||||
|
||||
def __br_ct_change_test__(self):
|
||||
result = ""
|
||||
for member in self.house.getmembers():
|
||||
obj = self.house.getobjbyname(member)
|
||||
if obj.__class__.__name__ == "tradfri_light":
|
||||
basename = member[:member.index('_zigbee')]
|
||||
gui = self.house.getobjbyname('.'.join(basename.split('.')[:-1]) + '.gui_' + basename.split('.')[-1])
|
||||
success = True
|
||||
#
|
||||
if gui.data.get(gui.KEY_STATE) != True:
|
||||
gui.command("toggle_state")
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
if gui.data.get(gui.KEY_STATE) != True:
|
||||
result += self.print_error("Unable to switch on light, while testing %s." % (member))
|
||||
success = False
|
||||
continue
|
||||
#
|
||||
if "set_brightness" in obj.capabilities():
|
||||
brightness = gui.data.get(obj.KEY_BRIGHTNESS)
|
||||
targetvalue = brightness + (25 if brightness <= 50 else -25)
|
||||
gui.command("set_brightness %d" % targetvalue)
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
if gui.data.get(obj.KEY_BRIGHTNESS) != targetvalue:
|
||||
result += self.print_error("Brightness change by gui was not successfull, while testing %s." % (member))
|
||||
success = False
|
||||
if "set_color_temp" in obj.capabilities():
|
||||
color_temp = gui.data.get(obj.KEY_COLOR_TEMP)
|
||||
targetvalue = color_temp + (3 if color_temp <= 5 else -3)
|
||||
gui.command("set_color_temp %d" % targetvalue)
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
if gui.data.get(obj.KEY_COLOR_TEMP) != targetvalue:
|
||||
result += self.print_error("Color temperature change by gui was not successfull, while testing %s." % (member))
|
||||
success = False
|
||||
#
|
||||
gui.command("toggle_state")
|
||||
time.sleep(2 * DT_TOGGLE)
|
||||
#
|
||||
if success:
|
||||
result += self.print_success("Brightness-ColorTemp test successfull, while testing %s." % (member))
|
||||
return result
|
46
base.py
Normal file
46
base.py
Normal file
@ -0,0 +1,46 @@
|
||||
import logging
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
except ImportError:
|
||||
ROOT_LOGGER_NAME = 'root'
|
||||
|
||||
|
||||
class common_base(dict):
|
||||
DEFAULT_VALUES = {}
|
||||
|
||||
def __init__(self, default_values=None):
|
||||
super().__init__(default_values or self.DEFAULT_VALUES)
|
||||
self['__type__'] = self.__class__.__name__
|
||||
#
|
||||
self.__callback_list__ = []
|
||||
self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
def add_callback(self, key, data, callback, on_change_only=False):
|
||||
"""
|
||||
key: key or None for all keys
|
||||
data: data or None for all data
|
||||
"""
|
||||
cb_tup = (key, data, callback, on_change_only)
|
||||
if cb_tup not in self.__callback_list__:
|
||||
self.__callback_list__.append(cb_tup)
|
||||
|
||||
def set(self, key, data, block_callback=[]):
|
||||
value_changed = self[key] != data
|
||||
self[key] = data
|
||||
for cb_key, cb_data, callback, on_change_only in self.__callback_list__:
|
||||
if cb_key is None or key == cb_key: # key fits to callback definition
|
||||
if cb_data is None or cb_data == self[key]: # data fits to callback definition
|
||||
if value_changed or not on_change_only: # change status fits to callback definition
|
||||
if not callback in block_callback: # block given callbacks
|
||||
callback(self, key, self[key])
|
||||
|
||||
|
||||
class mqtt_base(common_base):
|
||||
def __init__(self, mqtt_client, topic, default_values=None):
|
||||
super().__init__(default_values)
|
||||
#
|
||||
self.mqtt_client = mqtt_client
|
||||
self.topic = topic
|
||||
for entry in self.topic.split('/'):
|
||||
self.logger = self.logger.getChild(entry)
|
@ -26,10 +26,9 @@ devices (DEVICES)
|
||||
|
||||
"""
|
||||
|
||||
__DEPENDENCIES__ = []
|
||||
|
||||
import json
|
||||
import logging
|
||||
import task
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
@ -48,6 +47,52 @@ def is_json(data):
|
||||
return True
|
||||
|
||||
|
||||
class group(object):
|
||||
def __init__(self, *args):
|
||||
super().__init__()
|
||||
self._members = args
|
||||
self._iter_counter = 0
|
||||
#
|
||||
self.methods = []
|
||||
for method in [m for m in args[0].__class__.__dict__.keys()]:
|
||||
if not method.startswith('_') and callable(getattr(args[0], method)): # add all public callable attributes to the list
|
||||
self.methods.append(method)
|
||||
#
|
||||
for member in self:
|
||||
methods = [m for m in member.__class__.__dict__.keys() if not m.startswith(
|
||||
'_') if not m.startswith('_') and callable(getattr(args[0], m))]
|
||||
if self.methods != methods:
|
||||
raise ValueError("All given instances needs to have same attributes:", self.methods, methods)
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
if self._iter_counter < len(self):
|
||||
self._iter_counter += 1
|
||||
return self._members[self._iter_counter - 1]
|
||||
self._iter_counter = 0
|
||||
raise StopIteration
|
||||
|
||||
def __getitem__(self, i):
|
||||
return self._members[i]
|
||||
|
||||
def __len__(self):
|
||||
return len(self._members)
|
||||
|
||||
def __getattribute__(self, name):
|
||||
def group_execution(*args, **kwargs):
|
||||
for member in self[:]:
|
||||
m = getattr(member, name)
|
||||
m(*args, **kwargs)
|
||||
try:
|
||||
rv = super().__getattribute__(name)
|
||||
except AttributeError:
|
||||
return group_execution
|
||||
else:
|
||||
return rv
|
||||
|
||||
|
||||
class base(dict):
|
||||
TX_TOPIC = "set"
|
||||
TX_VALUE = 0
|
||||
@ -214,6 +259,26 @@ class shelly(base):
|
||||
|
||||
def __init__(self, mqtt_client, topic):
|
||||
super().__init__(mqtt_client, topic)
|
||||
#
|
||||
self.output_key_delayed = None
|
||||
self.delayed_flash_task = task.delayed(0.3, self.flash_task)
|
||||
self.delayed_off_task = task.delayed(0.3, self.off_task)
|
||||
#
|
||||
self.all_off_requested = False
|
||||
|
||||
def flash_task(self, *args):
|
||||
if self.flash_active:
|
||||
self.pack(self.output_key_delayed, not self.get(self.output_key_delayed))
|
||||
self.output_key_delayed = None
|
||||
if self.all_off_requested:
|
||||
self.delayed_off_task.run()
|
||||
|
||||
def off_task(self, *args):
|
||||
self.all_off()
|
||||
|
||||
@property
|
||||
def flash_active(self):
|
||||
return self.output_key_delayed is not None
|
||||
|
||||
#
|
||||
# WARNING CALL
|
||||
@ -293,6 +358,23 @@ class shelly(base):
|
||||
self.logger.info("Toggeling output 1")
|
||||
self.set_output_1('toggle')
|
||||
|
||||
def flash_0_mcb(self, device, key, data):
|
||||
self.output_key_delayed = self.KEY_OUTPUT_0
|
||||
self.toggle_output_0_mcb(device, key, data)
|
||||
self.delayed_flash_task.run()
|
||||
|
||||
def flash_1_mcb(self, device, key, data):
|
||||
self.output_key_delayed = self.KEY_OUTPUT_1
|
||||
self.toggle_output_1_mcb(device, key, data)
|
||||
self.delayed_flash_task.run()
|
||||
|
||||
def all_off(self):
|
||||
if self.flash_active:
|
||||
self.all_off_requested = True
|
||||
else:
|
||||
self.set_output_0(False)
|
||||
self.set_output_1(False)
|
||||
|
||||
|
||||
class silvercrest_powerplug(base):
|
||||
KEY_LINKQUALITY = "linkquality"
|
||||
@ -335,6 +417,9 @@ class silvercrest_powerplug(base):
|
||||
self.logger.info("Toggeling output 0")
|
||||
self.set_output_0('toggle')
|
||||
|
||||
def all_off(self):
|
||||
self.set_output_0(False)
|
||||
|
||||
|
||||
class silvercrest_motion_sensor(base):
|
||||
KEY_BATTERY = "battery"
|
||||
@ -472,6 +557,9 @@ class my_powerplug(base):
|
||||
self.logger.info("Toggeling all outputs")
|
||||
self.set_output_0('toggle')
|
||||
|
||||
def all_off(self):
|
||||
self.set_output_all(False)
|
||||
|
||||
|
||||
class tradfri_light(base):
|
||||
KEY_LINKQUALITY = "linkquality"
|
||||
@ -492,17 +580,17 @@ class tradfri_light(base):
|
||||
|
||||
def unpack_filter(self, key):
|
||||
if key == self.KEY_BRIGHTNESS:
|
||||
self[key] = (self[key] - 1) * 100 / 254
|
||||
self[key] = round((self[key] - 1) * 100 / 253, 0)
|
||||
elif key == self.KEY_COLOR_TEMP:
|
||||
self[key] = (self[key] - 250) * 10 / 204
|
||||
self[key] = round((self[key] - 250) * 10 / 204, 0)
|
||||
else:
|
||||
super().unpack_filter(key)
|
||||
|
||||
def pack_filter(self, key, data):
|
||||
if key == self.KEY_BRIGHTNESS:
|
||||
return data * 254 / 100 + 1
|
||||
return round(data * 253 / 100 + 1, 0)
|
||||
elif key == self.KEY_COLOR_TEMP:
|
||||
return data * 204 / 10 + 250
|
||||
return round(data * 204 / 10 + 250, 0)
|
||||
else:
|
||||
return super().pack_filter(key, data)
|
||||
|
||||
@ -532,6 +620,9 @@ class tradfri_light(base):
|
||||
#
|
||||
# TX
|
||||
#
|
||||
def request_data(self, device=None, key=None, data=None):
|
||||
self.mqtt_client.send(self.topic + "/get", '{"%s": ""}' % self.KEY_OUTPUT_0)
|
||||
|
||||
def set_output_0(self, state):
|
||||
"""state: [True, False, 'toggle']"""
|
||||
self.pack(self.KEY_OUTPUT_0, state)
|
||||
@ -569,6 +660,9 @@ class tradfri_light(base):
|
||||
self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "Changing color temperature to %s", str(data))
|
||||
self.set_color_temp(data)
|
||||
|
||||
def all_off(self):
|
||||
self.set_output_0(False)
|
||||
|
||||
|
||||
class tradfri_button(base):
|
||||
ACTION_TOGGLE = "toggle"
|
||||
@ -614,180 +708,6 @@ class tradfri_button(base):
|
||||
return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
|
||||
|
||||
|
||||
class nodered_gui_leds(base):
|
||||
KEY_LED_0 = "led0"
|
||||
KEY_LED_1 = "led1"
|
||||
KEY_LED_2 = "led2"
|
||||
KEY_LED_3 = "led3"
|
||||
KEY_LED_4 = "led4"
|
||||
KEY_LED_5 = "led5"
|
||||
KEY_LED_6 = "led6"
|
||||
KEY_LED_7 = "led7"
|
||||
KEY_LED_8 = "led8"
|
||||
KEY_LED_9 = "led9"
|
||||
KEY_LED_LIST = [KEY_LED_0, KEY_LED_1, KEY_LED_2, KEY_LED_3, KEY_LED_4, KEY_LED_5, KEY_LED_6, KEY_LED_7, KEY_LED_8, KEY_LED_9]
|
||||
#
|
||||
TX_TYPE = base.TX_VALUE
|
||||
|
||||
def set_led(self, key, data):
|
||||
"""data: [True, False]"""
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.pack(key, data)
|
||||
|
||||
|
||||
class nodered_gui_timer(base):
|
||||
KEY_TIMER = "timer"
|
||||
#
|
||||
TX_TYPE = base.TX_VALUE
|
||||
|
||||
def set_timer(self, data):
|
||||
"""data: numeric"""
|
||||
self.pack(self.KEY_TIMER, data)
|
||||
|
||||
def set_timer_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_timer(data)
|
||||
|
||||
|
||||
class nodered_gui_button(base):
|
||||
KEY_STATE = "state"
|
||||
#
|
||||
RX_KEYS = [KEY_STATE]
|
||||
|
||||
#
|
||||
# RX
|
||||
#
|
||||
@property
|
||||
def state(self):
|
||||
"""rv: [True, False]"""
|
||||
return self.get(self.KEY_STATE)
|
||||
|
||||
|
||||
class nodered_gui_switch(nodered_gui_button):
|
||||
TX_TYPE = base.TX_VALUE
|
||||
|
||||
#
|
||||
# TX
|
||||
#
|
||||
def set_state(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_STATE, data)
|
||||
|
||||
def set_state_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_state(data)
|
||||
|
||||
|
||||
class nodered_gui_light(nodered_gui_switch, nodered_gui_leds, nodered_gui_timer):
|
||||
KEY_ENABLE = "enable"
|
||||
KEY_BRIGHTNESS = "brightness"
|
||||
KEY_COLOR_TEMP = "color_temp"
|
||||
#
|
||||
TX_TYPE = base.TX_VALUE
|
||||
#
|
||||
RX_KEYS = nodered_gui_switch.RX_KEYS + [KEY_ENABLE, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
|
||||
|
||||
#
|
||||
# RX
|
||||
#
|
||||
@property
|
||||
def enable(self):
|
||||
"""rv: [True, False]"""
|
||||
return self.get(self.KEY_ENABLE)
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
"""rv: [True, False]"""
|
||||
return self.get(self.KEY_BRIGHTNESS)
|
||||
|
||||
@property
|
||||
def color_temp(self):
|
||||
"""rv: [True, False]"""
|
||||
return self.get(self.KEY_COLOR_TEMP)
|
||||
|
||||
#
|
||||
# TX
|
||||
#
|
||||
def set_enable(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_ENABLE, data)
|
||||
|
||||
def set_enable_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_enable(data)
|
||||
|
||||
def set_brightness(self, data):
|
||||
"""data: [0%, ..., 100%]"""
|
||||
self.pack(self.KEY_BRIGHTNESS, data)
|
||||
|
||||
def set_brightness_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_brightness(data)
|
||||
|
||||
def set_color_temp(self, data):
|
||||
"""data: [0, ..., 10]"""
|
||||
self.pack(self.KEY_COLOR_TEMP, data)
|
||||
|
||||
def set_color_temp_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_color_temp(data)
|
||||
|
||||
|
||||
class nodered_gui_radiator(nodered_gui_timer):
|
||||
KEY_TEMPERATURE = "temperature"
|
||||
KEY_SETPOINT_TEMP = "setpoint_temp"
|
||||
KEY_SETPOINT_TO_DEFAULT = "setpoint_to_default"
|
||||
KEY_BOOST = 'boost'
|
||||
KEY_AWAY = "away"
|
||||
KEY_SUMMER = "summer"
|
||||
KEY_ENABLE = "enable"
|
||||
#
|
||||
RX_KEYS = [KEY_TEMPERATURE, KEY_SETPOINT_TEMP, KEY_SETPOINT_TO_DEFAULT, KEY_BOOST, KEY_AWAY, KEY_SUMMER]
|
||||
|
||||
#
|
||||
# TX
|
||||
#
|
||||
def set_temperature(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_TEMPERATURE, data)
|
||||
|
||||
def set_temperature_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_temperature(data)
|
||||
|
||||
def set_setpoint_temperature(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_SETPOINT_TEMP, data)
|
||||
|
||||
def set_setpoint_temperature_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_setpoint_temperature(data)
|
||||
|
||||
def set_away(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_AWAY, data)
|
||||
|
||||
def set_away_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_away(data)
|
||||
|
||||
def set_summer(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_SUMMER, data)
|
||||
|
||||
def set_summer_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_summer(data)
|
||||
|
||||
def set_enable(self, data):
|
||||
"""data: [True, False]"""
|
||||
self.pack(self.KEY_ENABLE, data)
|
||||
|
||||
def set_enable_mcb(self, device, key, data):
|
||||
self.logger.debug("Sending %s with content %s", key, str(data))
|
||||
self.set_enable(data)
|
||||
|
||||
|
||||
class brennenstuhl_heatingvalve(base):
|
||||
KEY_LINKQUALITY = "linkquality"
|
||||
KEY_BATTERY = "battery"
|
||||
|
@ -1,11 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
import config
|
||||
import devices
|
||||
from function.stairway import stairway
|
||||
from function.ground_floor_west import ground_floor_west_floor, ground_floor_west_marion, ground_floor_west_dirk
|
||||
from function.first_floor_west import first_floor_west_julian, first_floor_west_living, first_floor_west_bath, first_floor_west_sleep
|
||||
from function.first_floor_east import first_floor_east_floor, first_floor_east_kitchen, first_floor_east_dining, first_floor_east_sleep, first_floor_east_living
|
||||
from function.ground_floor_west import ground_floor_west
|
||||
from function.first_floor_west import first_floor_west
|
||||
from function.first_floor_east import first_floor_east
|
||||
from function.rooms import room_collection
|
||||
from function.videv import all_off
|
||||
import inspect
|
||||
import logging
|
||||
|
||||
@ -15,32 +18,21 @@ except ImportError:
|
||||
ROOT_LOGGER_NAME = 'root'
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
# TODO: implement garland (incl. day events like sunset, sunrise, ...)
|
||||
# TODO: implement warning message
|
||||
|
||||
|
||||
class all_functions(object):
|
||||
class all_functions(room_collection):
|
||||
def __init__(self, mqtt_client):
|
||||
self.mqtt_client = mqtt_client
|
||||
super().__init__(mqtt_client)
|
||||
#
|
||||
# Rooms
|
||||
#
|
||||
self.__devices__ = None
|
||||
# stairway
|
||||
self.stw_stairway = stairway(self.mqtt_client)
|
||||
self.stw = stairway(self.mqtt_client)
|
||||
# ground floor west
|
||||
self.gfw_floor = ground_floor_west_floor(self.mqtt_client)
|
||||
self.gfw_marion = ground_floor_west_marion(self.mqtt_client)
|
||||
self.gfw_dirk = ground_floor_west_dirk(self.mqtt_client)
|
||||
self.gfw = ground_floor_west(self.mqtt_client)
|
||||
# first floor west
|
||||
self.ffw_julian = first_floor_west_julian(self.mqtt_client)
|
||||
self.ffw_bath = first_floor_west_bath(self.mqtt_client)
|
||||
self.ffw_living = first_floor_west_living(self.mqtt_client)
|
||||
self.ffw_sleep = first_floor_west_sleep(self.mqtt_client)
|
||||
self.ffw = first_floor_west(self.mqtt_client)
|
||||
# first floor east
|
||||
self.ffe_floor = first_floor_east_floor(self.mqtt_client)
|
||||
self.ffe_kitchen = first_floor_east_kitchen(self.mqtt_client)
|
||||
self.ffe_dining = first_floor_east_dining(self.mqtt_client)
|
||||
self.ffe_sleep = first_floor_east_sleep(self.mqtt_client)
|
||||
self.ffe_living = first_floor_east_living(self.mqtt_client)
|
||||
self.ffe = first_floor_east(self.mqtt_client)
|
||||
#
|
||||
# Interactions
|
||||
#
|
||||
@ -49,83 +41,31 @@ class all_functions(object):
|
||||
# Off Buttons
|
||||
self.init_off_functionality()
|
||||
|
||||
def init_off_functionality(self):
|
||||
# Off Buttons
|
||||
self.gui_button_all_off = devices.nodered_gui_button(self.mqtt_client, "gui/all/common/off/button")
|
||||
self.gui_button_gfw_off = devices.nodered_gui_button(self.mqtt_client, "gui/gfw/common/off/button")
|
||||
self.gui_button_ffw_off = devices.nodered_gui_button(self.mqtt_client, "gui/ffw/common/off/button")
|
||||
self.gui_button_ffe_off = devices.nodered_gui_button(self.mqtt_client, "gui/ffe/common/off/button")
|
||||
#
|
||||
self.gui_button_all_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.all_off)
|
||||
self.gui_button_gfw_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.gfw_off)
|
||||
self.gui_button_ffw_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.ffw_off)
|
||||
self.gui_button_ffe_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.ffe_off)
|
||||
# Long push ffe_floor
|
||||
self.ffe_floor.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.ffe_floor.all_off_feedback)
|
||||
self.ffe_floor.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.ffe_off)
|
||||
# Long push stairway
|
||||
self.stw_stairway.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.stw_stairway.all_off_feedback)
|
||||
self.stw_stairway.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.all_off)
|
||||
# Long push input device
|
||||
self.ffe_sleep.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG, self.ffe_off)
|
||||
|
||||
def getmembers(self, prefix):
|
||||
rv = []
|
||||
for name, obj in inspect.getmembers(self):
|
||||
if name.startswith(prefix) and obj.__module__.split('.')[0] == 'function' and len(obj.__module__.split('.')) == 2:
|
||||
rv.append(obj)
|
||||
return rv
|
||||
|
||||
def common_off(self, device=None, key=None, data=None):
|
||||
logger.info("Switching \"common\" off.")
|
||||
for common in self.getmembers('common'):
|
||||
common.all_off()
|
||||
|
||||
def gfw_off(self, device=None, key=None, data=None):
|
||||
logger.info("Switching \"ground floor west\" off.")
|
||||
for gfw in self.getmembers('gfw'):
|
||||
gfw.all_off()
|
||||
|
||||
def ffw_off(self, device=None, key=None, data=None):
|
||||
logger.info("Switching \"first floor west\" off.")
|
||||
for ffw in self.getmembers('ffw'):
|
||||
ffw.all_off()
|
||||
|
||||
def ffe_off(self, device=None, key=None, data=None):
|
||||
logger.info("Switching \"first floor east\" off.")
|
||||
for ffe in self.getmembers('ffe'):
|
||||
ffe.all_off()
|
||||
|
||||
def all_off(self, device=None, key=None, data=None):
|
||||
self.common_off(device, key, data)
|
||||
self.gfw_off(device, key, data)
|
||||
self.ffw_off(device, key, data)
|
||||
self.ffe_off(device, key, data)
|
||||
|
||||
def init_cross_room_interactions(self):
|
||||
# shelly dirk input 1
|
||||
self.last_gfw_dirk_input_1 = None
|
||||
self.gfw_dirk.main_light_shelly.add_callback(devices.shelly.KEY_INPUT_1, None, self.gfw_dirk_input_1)
|
||||
self.gfw.dirk.main_light_shelly.add_callback(devices.shelly.KEY_INPUT_1, None, self.gfw_dirk_input_1)
|
||||
# tradfri button ffe_sleep right click
|
||||
self.ffe_sleep.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
|
||||
devices.tradfri_button.ACTION_RIGHT, self.ffe_floor.main_light_shelly.toggle_output_0_mcb)
|
||||
self.ffe_kitchen.circulation_pump.main_light_shelly.add_callback(
|
||||
devices.shelly.KEY_OUTPUT_0, True, self.ffw_bath.radiator_function.boost, True)
|
||||
self.ffe.sleep.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
|
||||
devices.tradfri_button.ACTION_RIGHT, self.ffe.floor.main_light_shelly.toggle_output_0_mcb)
|
||||
|
||||
def init_off_functionality(self):
|
||||
# ALL OFF - Virtual device
|
||||
self.videv_all_off = all_off(self.mqtt_client, config.TOPIC_ALL_OFF_VIDEV, self)
|
||||
|
||||
# ALL OFF - Long push stairway
|
||||
self.stw.stairway.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.stw.stairway.main_light_shelly.flash_0_mcb)
|
||||
self.stw.stairway.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.all_off)
|
||||
|
||||
# FFE ALL OFF - Long push ffe_floor
|
||||
self.ffe.floor.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.ffe.floor.main_light_shelly.flash_0_mcb)
|
||||
self.ffe.floor.main_light_shelly.add_callback(devices.shelly.KEY_LONGPUSH_0, True, self.ffe.all_off)
|
||||
|
||||
# FFE ALL OFF - Long push input device
|
||||
self.ffe.sleep.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG, self.ffe.all_off)
|
||||
|
||||
def gfw_dirk_input_1(self, device, key, data):
|
||||
if self.last_gfw_dirk_input_1 is not None:
|
||||
if self.last_gfw_dirk_input_1 != data:
|
||||
self.gfw_floor.main_light_shelly.toggle_output_0_mcb(device, key, data)
|
||||
self.gfw.floor.main_light_shelly.toggle_output_0_mcb(device, key, data)
|
||||
self.last_gfw_dirk_input_1 = data
|
||||
|
||||
def devicelist(self):
|
||||
if self.__devices__ is None:
|
||||
self.__devices__ = []
|
||||
for name, obj in inspect.getmembers(self):
|
||||
if obj.__class__.__module__ == "devices":
|
||||
self.__devices__.append(obj)
|
||||
elif obj.__class__.__module__.split('.')[0] == 'function':
|
||||
for devicename, device in inspect.getmembers(obj):
|
||||
if device.__class__.__module__ == "devices":
|
||||
self.__devices__.append(device)
|
||||
return self.__devices__
|
||||
|
@ -4,12 +4,12 @@ import sqlite3
|
||||
db_file = os.path.join(os.path.dirname(__file__), '..', 'database.db')
|
||||
|
||||
|
||||
def get_gui_radiator_data(topic):
|
||||
return __storage__().get_gui_radiator_data(topic)
|
||||
def get_radiator_data(topic):
|
||||
return __storage__().get_radiator_data(topic)
|
||||
|
||||
|
||||
def set_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint):
|
||||
return __storage__().store_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint)
|
||||
def set_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint):
|
||||
return __storage__().store_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint)
|
||||
|
||||
|
||||
class __storage__(object):
|
||||
@ -17,30 +17,31 @@ class __storage__(object):
|
||||
self.conn = sqlite3.connect(db_file)
|
||||
self.c = self.conn.cursor()
|
||||
with self.conn:
|
||||
self.c.execute("""CREATE TABLE IF NOT EXISTS gui_radiator (
|
||||
self.c.execute("""CREATE TABLE IF NOT EXISTS radiator (
|
||||
topic text PRIMARY KEY,
|
||||
away_mode integer,
|
||||
summer_mode integer,
|
||||
user_temperatur_setpoint real
|
||||
user_temperatur_setpoint real,
|
||||
temperatur_setpoint real
|
||||
)""")
|
||||
|
||||
def store_gui_radiator_data(self, topic, away_mode, summer_mode, user_temperatur_setpoint):
|
||||
data = [topic, away_mode, summer_mode, user_temperatur_setpoint]
|
||||
def store_radiator_data(self, topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint):
|
||||
data = [topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint]
|
||||
try:
|
||||
with self.conn:
|
||||
self.c.execute(
|
||||
'INSERT INTO gui_radiator VALUES (?, ?, ?, ?)', data)
|
||||
'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', data)
|
||||
except sqlite3.IntegrityError:
|
||||
data = [away_mode, summer_mode, user_temperatur_setpoint]
|
||||
db_data = self.get_gui_radiator_data(topic)
|
||||
data = [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint]
|
||||
db_data = self.get_radiator_data(topic)
|
||||
if db_data != data:
|
||||
with self.conn:
|
||||
self.c.execute(
|
||||
'UPDATE gui_radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ? WHERE topic = ?', data + [topic])
|
||||
'UPDATE radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ?, temperatur_setpoint = ? WHERE topic = ?', data + [topic])
|
||||
|
||||
def get_gui_radiator_data(self, topic):
|
||||
""" returns a list [away_mode, summer_mode, user_temperatur_setpoint] or [None, None, None]"""
|
||||
self.c.execute("SELECT * FROM gui_radiator WHERE topic=?", (topic, ))
|
||||
def get_radiator_data(self, topic):
|
||||
""" returns a list [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint] or [None, None, None, None]"""
|
||||
self.c.execute("SELECT * FROM radiator WHERE topic=?", (topic, ))
|
||||
data = self.c.fetchone()
|
||||
if data is not None:
|
||||
data = list(data)
|
||||
@ -48,7 +49,7 @@ class __storage__(object):
|
||||
data[2] = data[2] == 1
|
||||
return data[1:]
|
||||
else:
|
||||
return [None, None, None]
|
||||
return [None, None, None, None]
|
||||
|
||||
def __del__(self):
|
||||
self.conn.close()
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
import config
|
||||
import devices
|
||||
from function.modules import brightness_choose_n_action, circulation_pump, radiator_function
|
||||
from function.modules import brightness_choose_n_action, timer_on_activation, heating_function
|
||||
from function.rooms import room, room_collection
|
||||
from function.videv import videv_switching, videv_switch_brightness, videv_switching_timer, videv_switch_brightness_color_temp, videv_heating, videv_multistate
|
||||
import logging
|
||||
from function.rooms import room_shelly, room_shelly_motion_sensor, room_shelly_tradfri_light
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
except ImportError:
|
||||
@ -14,142 +16,203 @@ except ImportError:
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
|
||||
class first_floor_east_floor(room_shelly):
|
||||
class first_floor_east(room_collection):
|
||||
def __init__(self, mqtt_client):
|
||||
# http://shelly1l-3C6105E4E629
|
||||
super().__init__(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_GUI)
|
||||
super().__init__(mqtt_client)
|
||||
self.dining = first_floor_east_dining(mqtt_client)
|
||||
self.floor = first_floor_east_floor(mqtt_client)
|
||||
self.kitchen = first_floor_east_kitchen(mqtt_client)
|
||||
self.livingroom = first_floor_east_living(mqtt_client)
|
||||
self.sleep = first_floor_east_sleep(mqtt_client)
|
||||
|
||||
|
||||
class first_floor_east_kitchen(room_shelly):
|
||||
class first_floor_east_floor(room):
|
||||
def __init__(self, mqtt_client):
|
||||
# http://shelly1l-8CAAB5616C01
|
||||
super().__init__(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_SHELLY, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_GUI)
|
||||
#
|
||||
self.circulation_pump = circulation_pump(mqtt_client)
|
||||
self.circulation_pump.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.flash_main_light)
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-3C6105E4E629
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
def all_off(self, device=None, key=None, data=None):
|
||||
self.circulation_pump.all_off(device, key, data)
|
||||
return super().all_off(device, key, data)
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
|
||||
)
|
||||
|
||||
|
||||
class first_floor_east_dining(room_shelly):
|
||||
class first_floor_east_kitchen(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-8CAAB5616C01
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_SHELLY)
|
||||
# http://shelly1-e89f6d85a466/
|
||||
self.circulation_pump_shelly = devices.shelly(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_SHELLY)
|
||||
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Functionality initialisation
|
||||
#
|
||||
self.circulation_pump = timer_on_activation(self.circulation_pump_shelly, devices.shelly.KEY_OUTPUT_0, 10*60)
|
||||
self.circulation_pump_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.main_light_shelly.flash_0_mcb, True)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
|
||||
)
|
||||
self.circulation_pump_videv = videv_switching_timer(
|
||||
mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_VIDEV,
|
||||
self.circulation_pump_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.circulation_pump, timer_on_activation.KEY_TIMER
|
||||
)
|
||||
|
||||
|
||||
class first_floor_east_dining(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-84CCA8ADD055
|
||||
super().__init__(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_SHELLY, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_GUI)
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_SHELLY)
|
||||
self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_POWERPLUG)
|
||||
if config.CHRISTMAS:
|
||||
self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_DININGROOM_GARLAND_POWERPLUG)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
self.gui_floorlamp = devices.nodered_gui_switch(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_GUI)
|
||||
#
|
||||
# Callback initialisation
|
||||
# Functionality initialisation
|
||||
#
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_powerplug.set_output_0_mcb, True)
|
||||
self.gui_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_floorlamp.set_state_mcb)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class first_floor_east_sleep(room_shelly_tradfri_light):
|
||||
def __init__(self, mqtt_client):
|
||||
# http://shelly1l-E8DB84A254C7
|
||||
super().__init__(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_SHELLY, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_GUI, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_ZIGBEE)
|
||||
# bed light
|
||||
self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_ZIGBEE)
|
||||
self.gui_bed_light_di = devices.nodered_gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_GUI)
|
||||
self.bed_light_ma_powerplug = devices.silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_POWERPLUG)
|
||||
self.gui_bed_light_ma = devices.nodered_gui_switch(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_GUI)
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
|
||||
)
|
||||
self.floorlamp_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_VIDEV,
|
||||
self.floorlamp_powerplug, devices.silvercrest_powerplug.KEY_OUTPUT_0
|
||||
)
|
||||
if config.CHRISTMAS:
|
||||
self.garland_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_DININGROOM_GARLAND_VIDEV,
|
||||
self.garland_powerplug, devices.silvercrest_powerplug.KEY_OUTPUT_0
|
||||
)
|
||||
|
||||
|
||||
class first_floor_east_sleep(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-E8DB84A254C7
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_ZIGBEE)
|
||||
self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_ZIGBEE)
|
||||
self.bed_light_ma_powerplug = devices.silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_POWERPLUG)
|
||||
self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, config.TOPIC_FFE_SLEEP_HEATING_VALVE_ZIGBEE)
|
||||
self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE)
|
||||
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Functionality initialisation
|
||||
#
|
||||
# button / brightness function
|
||||
self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_LED)
|
||||
self.brightness_functions = brightness_choose_n_action(self.button_tradfri)
|
||||
self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0)
|
||||
self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri, devices.tradfri_light.KEY_OUTPUT_0)
|
||||
# radiator valve
|
||||
self.radiator_function = radiator_function(mqtt_client, config.TOPIC_FFE_SLEEP_RADIATOR_VALVE_ZIGBEE,
|
||||
config.TOPIC_FFE_SLEEP_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_FFE_SLEEP)
|
||||
#
|
||||
# Callback initialisation
|
||||
#
|
||||
# on/off with button
|
||||
|
||||
# button / main light
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
|
||||
self.main_light_shelly.toggle_output_0_mcb)
|
||||
# button / bed light
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
|
||||
self.bed_light_di_tradfri.toggle_output_0_mcb)
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT_LONG,
|
||||
self.bed_light_ma_powerplug.toggle_output_0_mcb)
|
||||
|
||||
# bed light
|
||||
# switch
|
||||
self.gui_bed_light_di.add_callback(devices.nodered_gui_light.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_bed_light_di.set_state_mcb)
|
||||
self.gui_bed_light_ma.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.bed_light_ma_powerplug.set_output_0_mcb)
|
||||
self.bed_light_ma_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_bed_light_ma.set_state_mcb)
|
||||
# brightness and color temperature
|
||||
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_bed_light_di.set_enable_mcb)
|
||||
self.gui_bed_light_di.add_callback(devices.nodered_gui_light.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_bed_light_di.set_enable_mcb)
|
||||
self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_bed_light_di.set_brightness_mcb)
|
||||
# heating function
|
||||
self.heating_function = heating_function(self.heating_valve)
|
||||
|
||||
def all_off(self, device=None, key=None, data=None):
|
||||
super().all_off(device, key, data)
|
||||
self.bed_light_di_tradfri.set_output_0(False)
|
||||
self.bed_light_ma_powerplug.set_output_0(False)
|
||||
|
||||
|
||||
class first_floor_east_living(room_shelly_tradfri_light):
|
||||
def __init__(self, mqtt_client):
|
||||
# http://shelly1l-3C6105E3F910
|
||||
super().__init__(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_SHELLY,
|
||||
config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_GUI, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_ZIGBEE)
|
||||
for i in range(1, 7):
|
||||
setattr(self, 'floorlamp_tradfri_%d' % i, devices.tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_ZIGBEE % i))
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
self.bed_light_di_videv = videv_switch_brightness(
|
||||
mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_VIDEV,
|
||||
self.bed_light_di_tradfri, devices.tradfri_light.KEY_OUTPUT_0,
|
||||
self.bed_light_di_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
)
|
||||
self.bed_light_ma_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_VIDEV,
|
||||
self.bed_light_ma_powerplug, devices.silvercrest_powerplug.KEY_OUTPUT_0
|
||||
)
|
||||
self.heating_function_videv = videv_heating(
|
||||
mqtt_client, config.TOPIC_FFE_SLEEP_HEATING_VALVE_VIDEV,
|
||||
self.heating_function
|
||||
)
|
||||
self.brightness_functions_device_videv = videv_multistate(
|
||||
mqtt_client, config.TOPIC_FFE_SLEEP_ACTIVE_BRIGHTNESS_DEVICE_VIDEV,
|
||||
brightness_choose_n_action.KEY_ACTIVE_DEVICE, self.brightness_functions, 2
|
||||
)
|
||||
|
||||
|
||||
class first_floor_east_living(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-3C6105E3F910
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_ZIGBEE)
|
||||
self.floorlamp_tradfri = devices.group(
|
||||
*[devices.tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_ZIGBEE % i) for i in range(1, 7)])
|
||||
if config.CHRISTMAS:
|
||||
self.powerplug_xmas_tree = devices.silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_POWERPLUG)
|
||||
self.powerplug_xmas_star = devices.silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_STAR_POWERPLUG)
|
||||
#
|
||||
self.gui_floorlamp = devices.nodered_gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_GUI)
|
||||
#
|
||||
if config.CHRISTMAS:
|
||||
self.gui_xmas_tree = devices.nodered_gui_switch(mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_GUI)
|
||||
#
|
||||
# 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_floorlamp.add_callback(devices.nodered_gui_light.KEY_STATE, None, device.set_output_0_mcb)
|
||||
self.gui_floorlamp.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, device.set_brightness_mcb)
|
||||
self.gui_floorlamp.add_callback(devices.nodered_gui_light.KEY_COLOR_TEMP, None, device.set_color_temp_mcb)
|
||||
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_floorlamp.set_state_mcb)
|
||||
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_floorlamp.set_enable_mcb)
|
||||
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_floorlamp.set_brightness_mcb)
|
||||
self.floorlamp_tradfri_1.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_floorlamp.set_color_temp_mcb)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
if config.CHRISTMAS:
|
||||
self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_xmas_tree.set_state_mcb)
|
||||
self.gui_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)
|
||||
# Functionality initialisation
|
||||
#
|
||||
# floor lamp synchronisation with main_light
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_tradfri.set_output_0_mcb, True)
|
||||
|
||||
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)
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
self.floorlamp_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_VIDEV,
|
||||
self.floorlamp_tradfri, devices.tradfri_light.KEY_OUTPUT_0,
|
||||
self.floorlamp_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.floorlamp_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
if config.CHRISTMAS:
|
||||
self.powerplug_xmas_tree.set_output_0(False)
|
||||
self.powerplug_xmas_star.set_output_0(False)
|
||||
|
||||
def __floorlamp_devices__(self):
|
||||
rv = []
|
||||
for i in range(1, 7):
|
||||
rv.append(getattr(self, 'floorlamp_tradfri_%d' % i))
|
||||
return rv
|
||||
self.xmas_tree_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_VIDEV,
|
||||
self.powerplug_xmas_tree, devices.silvercrest_powerplug.KEY_OUTPUT_0
|
||||
)
|
||||
|
@ -3,9 +3,11 @@
|
||||
#
|
||||
|
||||
import config
|
||||
import devices
|
||||
from function.modules import heating_function
|
||||
from function.rooms import room, room_collection
|
||||
from function.videv import videv_switch_brightness, videv_switch_brightness_color_temp, videv_heating
|
||||
import logging
|
||||
from function.modules import radiator_function
|
||||
from function.rooms import room_shelly, room_shelly_tradfri_light
|
||||
|
||||
|
||||
try:
|
||||
@ -15,29 +17,94 @@ except ImportError:
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
|
||||
class first_floor_west_julian(room_shelly_tradfri_light):
|
||||
# http://shelly1l-3C6105E43452
|
||||
class first_floor_west(room_collection):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
self.bath = first_floor_west_bath(mqtt_client)
|
||||
self.julian = first_floor_west_julian(mqtt_client)
|
||||
self.livingroom = first_floor_west_living(mqtt_client)
|
||||
self.sleep = first_floor_west_sleep(mqtt_client)
|
||||
|
||||
|
||||
class first_floor_west_bath(object):
|
||||
class first_floor_west_julian(room):
|
||||
def __init__(self, mqtt_client):
|
||||
# radiator valve
|
||||
self.radiator_function = radiator_function(mqtt_client, config.TOPIC_FFW_BATH_RADIATOR_VALVE_ZIGBEE,
|
||||
config.TOPIC_FFW_BATH_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_FFW_BATH)
|
||||
def all_off(self):
|
||||
pass
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-3C6105E43452
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
|
||||
|
||||
class first_floor_west_living(room_shelly_tradfri_light):
|
||||
# http://shelly1l-84CCA8ACE6A1
|
||||
class first_floor_west_bath(room):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY,
|
||||
config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_GUI, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_ZIGBEE)
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, config.TOPIC_FFW_BATH_HEATING_VALVE_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
#
|
||||
# Functionality initialisation
|
||||
#
|
||||
# heating function
|
||||
self.heating_function = heating_function(self.heating_valve)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.heating_function_videv = videv_heating(
|
||||
mqtt_client, config.TOPIC_FFW_BATH_HEATING_VALVE_VIDEV,
|
||||
self.heating_function
|
||||
)
|
||||
|
||||
|
||||
class first_floor_west_sleep(room_shelly_tradfri_light):
|
||||
# http://shelly1-3494546A51F2
|
||||
class first_floor_west_living(room):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_SHELLY, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_GUI, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_ZIGBEE)
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-84CCA8ACE6A1
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
|
||||
|
||||
class first_floor_west_sleep(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1-3494546A51F2
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness(
|
||||
mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS
|
||||
)
|
||||
|
@ -4,9 +4,10 @@
|
||||
|
||||
import config
|
||||
import devices
|
||||
from function.modules import brightness_choose_n_action, radiator_function
|
||||
from function.modules import brightness_choose_n_action, heating_function, switched_light
|
||||
from function.rooms import room, room_collection
|
||||
from function.videv import videv_switching, videv_switch_brightness_color_temp, videv_heating, videv_multistate, videv_audio_player
|
||||
import logging
|
||||
from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light
|
||||
import task
|
||||
|
||||
try:
|
||||
@ -16,41 +17,79 @@ except ImportError:
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
|
||||
class ground_floor_west_floor(room_shelly_silvercrest_light):
|
||||
# http://shelly1l-84CCA8AD1148
|
||||
class ground_floor_west(room_collection):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_1_ZIGBEE)
|
||||
#
|
||||
# Callback initialisation
|
||||
#
|
||||
self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_2_ZIGBEE)
|
||||
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.main_light_tradfri_2.set_brightness_mcb)
|
||||
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.main_light_tradfri_2.set_color_temp_mcb)
|
||||
|
||||
def send_init_message_main_light(self):
|
||||
super().send_init_message_main_light()
|
||||
self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
|
||||
super().__init__(mqtt_client)
|
||||
self.dirk = ground_floor_west_dirk(mqtt_client)
|
||||
self.floor = ground_floor_west_floor(mqtt_client)
|
||||
self.marion = ground_floor_west_marion(mqtt_client)
|
||||
|
||||
|
||||
class ground_floor_west_marion(room_shelly):
|
||||
# http://shelly1l-E8DB84A1E067
|
||||
class ground_floor_west_floor(room):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_MARION_MAIN_LIGHT_GUI)
|
||||
# radiator valve
|
||||
self.radiator_function = radiator_function(mqtt_client, config.TOPIC_GFW_MARION_RADIATOR_VALVE_ZIGBEE,
|
||||
config.TOPIC_GFW_MARION_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_GFW_MARION)
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-84CCA8AD1148
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.group(
|
||||
devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_ZIGBEE % 1),
|
||||
devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_ZIGBEE % 2)
|
||||
)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Functionality initialisation
|
||||
#
|
||||
# Request silvercrest data of lead light after power on
|
||||
switched_light(self.main_light_shelly, devices.shelly.KEY_OUTPUT_0, self.main_light_tradfri)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
|
||||
|
||||
class ground_floor_west_dirk(room_shelly_tradfri_light):
|
||||
class ground_floor_west_marion(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-E8DB84A1E067
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY)
|
||||
self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, config.TOPIC_GFW_MARION_HEATING_VALVE_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Functionality initialisation
|
||||
#
|
||||
# heating function
|
||||
self.heating_function = heating_function(self.heating_valve)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
|
||||
)
|
||||
self.heating_function_videv = videv_heating(
|
||||
mqtt_client, config.TOPIC_GFW_MARION_HEATING_VALVE_VIDEV,
|
||||
self.heating_function
|
||||
)
|
||||
|
||||
|
||||
class ground_floor_west_dirk(room):
|
||||
STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
|
||||
STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
|
||||
STATE_ACTIVE_DEVICE_AMPLIFIER = 2
|
||||
STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
|
||||
#
|
||||
LED_ACTIVE_DEVICE_MAIN_LIGHT = devices.nodered_gui_leds.KEY_LED_0
|
||||
LED_ACTIVE_DEVICE_DESK_LIGHT = devices.nodered_gui_leds.KEY_LED_1
|
||||
LED_ACTIVE_DEVICE_AMPLIFIER = devices.nodered_gui_leds.KEY_LED_2
|
||||
#
|
||||
KEY_POWERPLUG_AMPLIFIER = devices.my_powerplug.KEY_OUTPUT_0
|
||||
KEY_POWERPLUG_CD_PLAYER = devices.my_powerplug.KEY_OUTPUT_2
|
||||
KEY_POWERPLUG_DESK_LIGHT = devices.my_powerplug.KEY_OUTPUT_1
|
||||
@ -60,94 +99,102 @@ class ground_floor_west_dirk(room_shelly_tradfri_light):
|
||||
AUDIO_SOURCE_CD = 1
|
||||
AUDIO_SOURCE_RASPI = 2
|
||||
|
||||
# http://shelly1l-3C6105E44F27
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI, config.TOPIC_GFW_DIRK_MAIN_LIGHT_ZIGBEE)
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1l-3C6105E44F27
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_ZIGBEE)
|
||||
self.powerplug_common = devices.my_powerplug(mqtt_client, config.TOPIC_GFW_DIRK_POWERPLUG)
|
||||
self.desk_light_tradfri = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_ZIGBEE)
|
||||
self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_GFW_DIRK_INPUT_DEVICE)
|
||||
#
|
||||
self.gui_desk_light = devices.nodered_gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI)
|
||||
#
|
||||
self.gui_amplifier = devices.nodered_gui_switch(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_GUI)
|
||||
self.gui_cd_player = devices.nodered_gui_switch(mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_GUI)
|
||||
self.gui_pc_dock = devices.nodered_gui_switch(mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_GUI)
|
||||
#
|
||||
self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
|
||||
self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY)
|
||||
self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD)
|
||||
self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, config.TOPIC_GFW_DIRK_HEATING_VALVE_ZIGBEE)
|
||||
super().__init__(mqtt_client)
|
||||
#
|
||||
self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED)
|
||||
# Functionality initialisation
|
||||
#
|
||||
# Button - Brightness functionality
|
||||
self.brightness_functions = brightness_choose_n_action(self.button_tradfri)
|
||||
self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0)
|
||||
self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT)
|
||||
self.brightness_functions.add(self.remote_amplifier, self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER)
|
||||
#
|
||||
self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY)
|
||||
self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD)
|
||||
# radiator valve
|
||||
self.radiator_function = radiator_function(mqtt_client, config.TOPIC_GFW_DIRK_RADIATOR_VALVE_ZIGBEE,
|
||||
config.TOPIC_GFW_DIRK_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_GFW_DIRK)
|
||||
#
|
||||
self.delayed_task = task.delayed(1.0, self.send_audio_source)
|
||||
#
|
||||
# Callback initialisation
|
||||
#
|
||||
|
||||
# main light
|
||||
# Button - Main light
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
|
||||
self.main_light_shelly.toggle_output_0_mcb)
|
||||
|
||||
# desk light
|
||||
# switch
|
||||
# Button - Desk light
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT,
|
||||
self.powerplug_common.toggle_output_1_mcb)
|
||||
self.gui_desk_light.add_callback(devices.nodered_gui_light.KEY_STATE, None, self.powerplug_common.set_output_1_mcb)
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_desk_light.set_state_mcb)
|
||||
# brightness and color temp
|
||||
self.gui_desk_light.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.desk_light_tradfri.set_brightness_mcb)
|
||||
self.gui_desk_light.add_callback(devices.nodered_gui_light.KEY_COLOR_TEMP, None, self.desk_light_tradfri.set_color_temp_mcb)
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_desk_light.set_enable_mcb)
|
||||
self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_desk_light.set_brightness_mcb)
|
||||
self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_desk_light.set_color_temp_mcb)
|
||||
|
||||
# amplifier
|
||||
# Button - Amplifier
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT_LONG,
|
||||
self.powerplug_common.toggle_output_0_mcb)
|
||||
self.gui_amplifier.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_0_mcb)
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.gui_amplifier.set_state_mcb)
|
||||
# amplifier auto on
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation, True)
|
||||
self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation, True)
|
||||
self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation, True)
|
||||
# audio source
|
||||
# Button - CD player
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG,
|
||||
self.powerplug_common.toggle_output_2_mcb)
|
||||
# Button - PC dock
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
|
||||
self.powerplug_common.toggle_output_3_mcb)
|
||||
|
||||
# Mediaplayer - Amplifier auto on
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.powerplug_common.set_output_0_mcb, True)
|
||||
self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
|
||||
self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.powerplug_common.set_output_0_mcb, True)
|
||||
# Mediaplayer - Audio source selection
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, True, self.audio_source_selector, True)
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, True, self.audio_source_selector, True)
|
||||
self.spotify_state.add_callback(devices.status.KEY_STATE, True, self.audio_source_selector, True)
|
||||
self.mpd_state.add_callback(devices.status.KEY_STATE, True, self.audio_source_selector, True)
|
||||
self.audio_source = self.AUDIO_SOURCE_PC
|
||||
|
||||
# cd player
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG,
|
||||
self.powerplug_common.toggle_output_2_mcb)
|
||||
self.gui_cd_player.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_2_mcb)
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.gui_cd_player.set_state_mcb)
|
||||
# heating function
|
||||
self.heating_function = heating_function(self.heating_valve)
|
||||
|
||||
# pc dock
|
||||
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
|
||||
self.powerplug_common.toggle_output_3_mcb)
|
||||
self.gui_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb)
|
||||
self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_pc_dock.set_state_mcb)
|
||||
|
||||
def all_off(self, device=None, key=None, data=None):
|
||||
super().all_off(device, key, data)
|
||||
self.powerplug_common.set_output_all(False)
|
||||
|
||||
def cd_amplifier_synchronisation(self, device, key, data):
|
||||
logger.info("Syncing \"%s\" amplifier with cd player: %s", type(self).__name__, data)
|
||||
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
|
||||
|
||||
def raspi_amplifier_synchronisation(self, device, key, data):
|
||||
logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data)
|
||||
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
self.desk_light_videv = videv_switch_brightness_color_temp(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_VIDEV,
|
||||
self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT,
|
||||
self.desk_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
|
||||
self.desk_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
|
||||
)
|
||||
self.amplifier_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_VIDEV,
|
||||
self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER
|
||||
)
|
||||
self.cd_player_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_VIDEV,
|
||||
self.powerplug_common, self.KEY_POWERPLUG_CD_PLAYER
|
||||
)
|
||||
self.pc_dock_videv = videv_switching(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_VIDEV,
|
||||
self.powerplug_common, self.KEY_POWERPLUG_PC_DOCK
|
||||
)
|
||||
self.heating_function_videv = videv_heating(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_HEATING_VALVE_VIDEV,
|
||||
self.heating_function
|
||||
)
|
||||
self.brightness_functions_device_videv = videv_multistate(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_ACTIVE_BRIGHTNESS_DEVICE_VIDEV,
|
||||
brightness_choose_n_action.KEY_ACTIVE_DEVICE, self.brightness_functions, 3
|
||||
)
|
||||
self.audio_player_videv = videv_audio_player(
|
||||
mqtt_client, config.TOPIC_GFW_DIRK_AUDIO_PLAYER_VIDEV,
|
||||
self.spotify_state, self.mpd_state
|
||||
)
|
||||
#
|
||||
# Other stuff
|
||||
#
|
||||
self.delayed_task_remote = task.delayed(1.0, self.send_audio_source)
|
||||
|
||||
def audio_source_selector(self, device, key, data):
|
||||
if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
|
||||
@ -158,7 +205,7 @@ class ground_floor_west_dirk(room_shelly_tradfri_light):
|
||||
self.audio_source = self.AUDIO_SOURCE_RASPI
|
||||
elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
|
||||
# switch on of amplifier -> select source and reset stored source value
|
||||
self.delayed_task.run()
|
||||
self.delayed_task_remote.run()
|
||||
|
||||
def send_audio_source(self):
|
||||
if self.audio_source == self.AUDIO_SOURCE_PC:
|
||||
|
@ -1,11 +1,21 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
"""
|
||||
Functional Modules
|
||||
|
||||
Targets:
|
||||
* Device like structure to be compatible with videv
|
||||
- KEY_* as part of the class for all parameters which needs to be accessed from videv
|
||||
- Method *.set(key, data) to pass data from videv to Module
|
||||
- Method .add_calback(key, data, callback, on_change_only=False) to register videv actualisation on changes
|
||||
"""
|
||||
|
||||
from base import common_base
|
||||
import config
|
||||
import devices
|
||||
from function.db import get_gui_radiator_data, set_gui_radiator_data
|
||||
from function.rooms import room_shelly
|
||||
from function.db import get_radiator_data, set_radiator_data
|
||||
from function.helpers import now, sunset_time, sunrise_time
|
||||
import logging
|
||||
import task
|
||||
|
||||
@ -16,26 +26,30 @@ except ImportError:
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
|
||||
class brightness_choose_n_action(object):
|
||||
def __init__(self, mqtt_client, button_tradfri, topic_led):
|
||||
self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, topic_led)
|
||||
class switched_light(object):
|
||||
def __init__(self, sw_device, sw_key, li_device):
|
||||
sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, True, li_device.request_data, True)
|
||||
|
||||
|
||||
class brightness_choose_n_action(common_base):
|
||||
KEY_ACTIVE_DEVICE = 'active_device'
|
||||
#
|
||||
DEFAULT_VALUES = {KEY_ACTIVE_DEVICE: None}
|
||||
|
||||
def __init__(self, button_tradfri):
|
||||
super().__init__()
|
||||
# brightness change
|
||||
button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
|
||||
devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
|
||||
button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
|
||||
button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
|
||||
devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
|
||||
button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
|
||||
devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
|
||||
button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
|
||||
button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
|
||||
button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
|
||||
button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
|
||||
# device change
|
||||
button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
|
||||
button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
|
||||
button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP, self.choose_next_device)
|
||||
button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
|
||||
#
|
||||
self.brightness_device_list = []
|
||||
self.callback_device_list = []
|
||||
self.device_states = []
|
||||
self.active_device_state = None
|
||||
self.update_active_device_led()
|
||||
|
||||
def add(self, brightness_device, callback_device, callback_key):
|
||||
"""
|
||||
@ -46,8 +60,6 @@ class brightness_choose_n_action(object):
|
||||
callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
|
||||
* .add_callback(key, data or None, callback, on_changes_only)
|
||||
"""
|
||||
if len(self.brightness_device_list) >= len(devices.nodered_gui_leds.KEY_LED_LIST):
|
||||
raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
|
||||
self.brightness_device_list.append(brightness_device)
|
||||
self.callback_device_list.append((callback_device, callback_key))
|
||||
self.device_states.append(False)
|
||||
@ -57,42 +69,35 @@ class brightness_choose_n_action(object):
|
||||
def device_state_action(self, device, key, data):
|
||||
self.device_states[self.callback_device_list.index((device, key))] = data
|
||||
if data is True:
|
||||
self.active_device_state = self.callback_device_list.index((device, key))
|
||||
self.update_active_device_led()
|
||||
self.set(self.KEY_ACTIVE_DEVICE, self.callback_device_list.index((device, key)))
|
||||
else:
|
||||
self.choose_next_device()
|
||||
|
||||
def update_active_device_led(self):
|
||||
for i in range(0, len(self.brightness_device_list)):
|
||||
self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_LIST[i], self.active_device_state == i)
|
||||
if self[self.KEY_ACTIVE_DEVICE] is not None:
|
||||
if self.callback_device_list[self[self.KEY_ACTIVE_DEVICE]][0] == device:
|
||||
self.choose_next_device()
|
||||
|
||||
def choose_prev_device(self, device=None, key=None, data=None):
|
||||
if self.active_device_state is not None:
|
||||
start_value = self.active_device_state
|
||||
if self[self.KEY_ACTIVE_DEVICE] is not None:
|
||||
start_value = self[self.KEY_ACTIVE_DEVICE]
|
||||
for i in range(0, len(self.brightness_device_list)):
|
||||
target_state = (start_value - i - 1) % (len(self.brightness_device_list))
|
||||
if self.device_states[target_state]:
|
||||
self.active_device_state = target_state
|
||||
self.update_active_device_led()
|
||||
self.set(self.KEY_ACTIVE_DEVICE, target_state)
|
||||
return
|
||||
self.active_device_state = None
|
||||
self.update_active_device_led()
|
||||
self.set(self.KEY_ACTIVE_DEVICE, None)
|
||||
|
||||
def choose_next_device(self, device=None, key=None, data=None):
|
||||
if self.active_device_state is not None:
|
||||
start_value = self.active_device_state
|
||||
if self[self.KEY_ACTIVE_DEVICE] is not None:
|
||||
start_value = self[self.KEY_ACTIVE_DEVICE]
|
||||
for i in range(0, len(self.brightness_device_list)):
|
||||
target_state = (start_value + i + 1) % (len(self.brightness_device_list))
|
||||
if self.device_states[target_state]:
|
||||
self.active_device_state = target_state
|
||||
self.update_active_device_led()
|
||||
self.set(self.KEY_ACTIVE_DEVICE, target_state)
|
||||
return
|
||||
self.active_device_state = None
|
||||
self.update_active_device_led()
|
||||
self.set(self.KEY_ACTIVE_DEVICE, None)
|
||||
|
||||
def brightness_action(self, device, key, data):
|
||||
if self.active_device_state is not None:
|
||||
target = self.brightness_device_list[self.active_device_state]
|
||||
if self[self.KEY_ACTIVE_DEVICE] is not None:
|
||||
target = self.brightness_device_list[self[self.KEY_ACTIVE_DEVICE]]
|
||||
if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
|
||||
logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
|
||||
target.default_inc()
|
||||
@ -103,140 +108,214 @@ class brightness_choose_n_action(object):
|
||||
target.default_stop()
|
||||
|
||||
|
||||
class circulation_pump(room_shelly):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_SHELLY, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI)
|
||||
class timer_on_activation(common_base):
|
||||
KEY_TIMER = 'timer'
|
||||
#
|
||||
DEFAULT_VALUES = {
|
||||
KEY_TIMER: 0
|
||||
}
|
||||
|
||||
def __init__(self, sw_device, sw_key, timer_reload_value):
|
||||
super().__init__()
|
||||
#
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.circ_pump_actions, True)
|
||||
self.timer_reload_value = timer_reload_value
|
||||
#
|
||||
self.gui_main_light.set_timer('-')
|
||||
sw_device.add_callback(sw_key, None, self.circ_pump_actions, True)
|
||||
#
|
||||
self.ct = task.periodic(6, self.cyclic_task)
|
||||
self.pump_timer = None
|
||||
#
|
||||
self.ct.run()
|
||||
|
||||
def circ_pump_actions(self, device, key, data):
|
||||
if data is True:
|
||||
self.pump_timer = 10
|
||||
self.gui_main_light.set_timer(self.pump_timer)
|
||||
self.set(self.KEY_TIMER, self.timer_reload_value)
|
||||
else:
|
||||
self.pump_timer = None
|
||||
self.gui_main_light.set_timer('-')
|
||||
self.set(self.KEY_TIMER, 0)
|
||||
|
||||
def cyclic_task(self, rt):
|
||||
if self.pump_timer is not None:
|
||||
if self.pump_timer <= 0:
|
||||
self.pump_timer = None
|
||||
self.gui_main_light.set_timer('-')
|
||||
else:
|
||||
self.gui_main_light.set_timer(self.pump_timer)
|
||||
self.pump_timer -= self.ct.cycle_time / 60
|
||||
timer_value = self[self.KEY_TIMER] - self.ct.cycle_time
|
||||
if timer_value <= 0:
|
||||
self.set(self.KEY_TIMER, 0)
|
||||
else:
|
||||
self.set(self.KEY_TIMER, timer_value)
|
||||
|
||||
|
||||
class radiator_function(object):
|
||||
class heating_function(common_base):
|
||||
KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
|
||||
KEY_TEMPERATURE_SETPOINT = 'temperature_setpoint'
|
||||
KEY_TEMPERATURE_CURRENT = 'temperature_current'
|
||||
KEY_AWAY_MODE = 'away_mode'
|
||||
KEY_SUMMER_MODE = 'summer_mode'
|
||||
KEY_START_BOOST = 'start_boost'
|
||||
KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
|
||||
KEY_BOOST_TIMER = 'boost_timer'
|
||||
#
|
||||
BOOST_TEMPERATURE = 30
|
||||
AWAY_REDUCTION = 5
|
||||
SUMMER_TEMPERATURE = 5
|
||||
|
||||
def __init__(self, mqtt_client, topic_valve, topic_gui, default_temperature):
|
||||
self.default_temperature = default_temperature
|
||||
self.boost_timer = None
|
||||
# device initialisation
|
||||
self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, topic_valve)
|
||||
self.gui_heating = devices.nodered_gui_radiator(mqtt_client, topic_gui)
|
||||
# db-stored data initialisation
|
||||
db_data = get_gui_radiator_data(topic_gui)
|
||||
self.__away_mode__ = db_data[0] or False
|
||||
self.__summer_mode__ = db_data[1] or False
|
||||
self.__user_temperature_setpoint__ = db_data[2] or default_temperature
|
||||
if self.__away_mode__:
|
||||
self.away_mode(None, None, True)
|
||||
elif self.__summer_mode__:
|
||||
self.summer_mode(None, None, True)
|
||||
else:
|
||||
self.set_heating_setpoint(None, None, self.__user_temperature_setpoint__)
|
||||
# callback initialisation
|
||||
self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_TEMPERATURE, None, self.gui_heating.set_temperature_mcb)
|
||||
self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
|
||||
def __init__(self, heating_valve):
|
||||
self.heating_valve = heating_valve
|
||||
self.default_temperature = config.DEFAULT_TEMPERATURE[heating_valve.topic]
|
||||
db_data = get_radiator_data(heating_valve.topic)
|
||||
super().__init__({
|
||||
self.KEY_USER_TEMPERATURE_SETPOINT: db_data[2] or self.default_temperature,
|
||||
self.KEY_TEMPERATURE_SETPOINT: db_data[3] or self.default_temperature,
|
||||
self.KEY_TEMPERATURE_CURRENT: None,
|
||||
self.KEY_AWAY_MODE: db_data[0] or False,
|
||||
self.KEY_SUMMER_MODE: db_data[1] or False,
|
||||
self.KEY_START_BOOST: True,
|
||||
self.KEY_SET_DEFAULT_TEMPERATURE: False,
|
||||
self.KEY_BOOST_TIMER: 0
|
||||
})
|
||||
#
|
||||
self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TEMP, None, self.set_heating_setpoint)
|
||||
self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_BOOST, None, self.boost)
|
||||
self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TO_DEFAULT, None, self.setpoint_to_default)
|
||||
self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_AWAY, None, self.away_mode)
|
||||
self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SUMMER, None, self.summer_mode)
|
||||
self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
|
||||
#
|
||||
self.heating_valve.add_callback(self.heating_valve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
|
||||
self.heating_valve.add_callback(self.heating_valve.KEY_TEMPERATURE, None, self.get_radiator_temperature)
|
||||
#
|
||||
self.add_callback(self.KEY_USER_TEMPERATURE_SETPOINT, None, self.user_temperature_setpoint, False)
|
||||
self.add_callback(self.KEY_TEMPERATURE_SETPOINT, None, self.set_heating_setpoint, True)
|
||||
self.add_callback(self.KEY_AWAY_MODE, None, self.away_mode, True)
|
||||
self.add_callback(self.KEY_SUMMER_MODE, None, self.summer_mode, True)
|
||||
self.add_callback(self.KEY_SET_DEFAULT_TEMPERATURE, None, self.setpoint_to_default)
|
||||
self.add_callback(self.KEY_START_BOOST, True, self.boost, False)
|
||||
self.add_callback(self.KEY_BOOST_TIMER, 0, self.timer_expired, True)
|
||||
# cyclic task initialisation
|
||||
self.ct = task.periodic(1, self.cyclic_task)
|
||||
self.ct.run()
|
||||
|
||||
def timer_expired(self, device, data, key):
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
|
||||
self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.',
|
||||
self[self.KEY_TEMPERATURE_SETPOINT])
|
||||
|
||||
def cyclic_task(self, rt):
|
||||
if self.boost_timer is not None:
|
||||
self.gui_heating.set_timer(round(self.boost_timer / 60, 1))
|
||||
#
|
||||
self.boost_timer -= self.ct.cycle_time
|
||||
if self.boost_timer <= 0:
|
||||
self.cancel_boost()
|
||||
self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
|
||||
self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.', self.__user_temperature_setpoint__)
|
||||
timer_value = self[self.KEY_BOOST_TIMER] - self.ct.cycle_time
|
||||
if self[self.KEY_BOOST_TIMER] <= 0:
|
||||
self.set(self.KEY_BOOST_TIMER, 0)
|
||||
else:
|
||||
self.set(self.KEY_BOOST_TIMER, timer_value)
|
||||
|
||||
def cancel_boost(self, device=None, key=None, data=None):
|
||||
if self.boost_timer is not None:
|
||||
self.boost_timer = None
|
||||
self.gui_heating.set_timer('-')
|
||||
def cancel_boost(self):
|
||||
self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
|
||||
|
||||
def update_states(self, away_mode=None, summer_mode=None, user_temperature_setpoint=None):
|
||||
if away_mode is not None:
|
||||
self.__away_mode__ = away_mode
|
||||
if summer_mode is not None:
|
||||
self.__summer_mode__ = summer_mode
|
||||
if user_temperature_setpoint is not None:
|
||||
self.__user_temperature_setpoint__ = user_temperature_setpoint
|
||||
set_gui_radiator_data(self.gui_heating.topic, self.__away_mode__, self.__summer_mode__, self.__user_temperature_setpoint__)
|
||||
#
|
||||
self.gui_heating.set_away(self.__away_mode__)
|
||||
self.gui_heating.set_summer(self.__summer_mode__)
|
||||
self.gui_heating.set_enable(not self.__away_mode__ and not self.__summer_mode__)
|
||||
def set(self, key, data, block_callback=[]):
|
||||
rv = super().set(key, data, block_callback)
|
||||
set_radiator_data(self.heating_valve.topic, self[self.KEY_AWAY_MODE], self[self.KEY_SUMMER_MODE],
|
||||
self[self.KEY_USER_TEMPERATURE_SETPOINT], self[self.KEY_TEMPERATURE_SETPOINT])
|
||||
return rv
|
||||
|
||||
def away_mode(self, device, key, value):
|
||||
if value is True:
|
||||
self.cancel_boost()
|
||||
self.update_states(away_mode=value, summer_mode=False)
|
||||
self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__ - self.AWAY_REDUCTION)
|
||||
self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT] - self.AWAY_REDUCTION)
|
||||
else:
|
||||
self.update_states(away_mode=value)
|
||||
self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
|
||||
|
||||
def summer_mode(self, device, key, value):
|
||||
if value is True:
|
||||
self.cancel_boost()
|
||||
self.update_states(away_mode=False, summer_mode=value)
|
||||
self.heating_valve.set_heating_setpoint(self.SUMMER_TEMPERATURE)
|
||||
self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self.SUMMER_TEMPERATURE)
|
||||
else:
|
||||
self.update_states(summer_mode=value)
|
||||
self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
|
||||
|
||||
def boost(self, device, key, data):
|
||||
if self.boost_timer is None:
|
||||
if self[self.KEY_BOOST_TIMER] == 0:
|
||||
self.heating_valve.logger.info('Starting boost mode with setpoint %.1f°C.', self.BOOST_TEMPERATURE)
|
||||
self.boost_timer = 15*60
|
||||
self.heating_valve.set_heating_setpoint(self.BOOST_TEMPERATURE)
|
||||
self.set(self.KEY_BOOST_TIMER, 15*60)
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self.BOOST_TEMPERATURE)
|
||||
else:
|
||||
self.boost_timer += 15 * 60
|
||||
if self.boost_timer > 60 * 60:
|
||||
self.boost_timer = 60 * 60
|
||||
self.update_states(away_mode=False, summer_mode=False)
|
||||
self.set(self.KEY_BOOST_TIMER, min(self[self.KEY_BOOST_TIMER] + 15 * 60, 60 * 60))
|
||||
self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
|
||||
self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
|
||||
|
||||
def setpoint_to_default(self, device, key, data):
|
||||
self.set_heating_setpoint(device, key, self.default_temperature)
|
||||
self.cancel_boost()
|
||||
self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
|
||||
self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
|
||||
self.set(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature, [self.user_temperature_setpoint])
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature)
|
||||
|
||||
def user_temperature_setpoint(self, device, key, data):
|
||||
self.cancel_boost()
|
||||
self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
|
||||
self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
|
||||
self.set(self.KEY_TEMPERATURE_SETPOINT, data)
|
||||
|
||||
def set_heating_setpoint(self, device, key, data):
|
||||
self.cancel_boost()
|
||||
self.update_states(away_mode=False, summer_mode=False, user_temperature_setpoint=data)
|
||||
self.heating_valve.set_heating_setpoint(data)
|
||||
|
||||
def get_radiator_setpoint(self, device, key, data):
|
||||
if self.boost_timer is None and not self.__away_mode__ and not self.__summer_mode__:
|
||||
self.update_states(user_temperature_setpoint=data)
|
||||
else:
|
||||
self.update_states()
|
||||
self.gui_heating.set_setpoint_temperature(data)
|
||||
if self[self.KEY_BOOST_TIMER] == 0 and not self[self.KEY_AWAY_MODE] and not self[self.KEY_SUMMER_MODE]:
|
||||
self.set(self.KEY_USER_TEMPERATURE_SETPOINT, data, block_callback=[self.set_heating_setpoint])
|
||||
|
||||
def get_radiator_temperature(self, device, key, data):
|
||||
self.set(self.KEY_TEMPERATURE_CURRENT, data)
|
||||
|
||||
|
||||
class motion_sensor_light(common_base):
|
||||
KEY_TIMER = 'timer'
|
||||
KEY_MOTION_SENSOR = 'motion_%d'
|
||||
KEY_MOTION_SENSOR_0 = 'motion_%d' % 0
|
||||
KEY_MOTION_SENSOR_1 = 'motion_%d' % 1
|
||||
KEY_MOTION_SENSOR_2 = 'motion_%d' % 2
|
||||
KEY_MOTION_SENSOR_3 = 'motion_%d' % 3
|
||||
KEY_MOTION_SENSOR_4 = 'motion_%d' % 4
|
||||
|
||||
def __init__(self, sw_device, sw_method, *args, timer_value=30):
|
||||
"""
|
||||
sw_device is the device switching the light, args are 0-n motion sensors
|
||||
"""
|
||||
dv = dict.fromkeys([self.KEY_MOTION_SENSOR % i for i in range(0, len(args))])
|
||||
for key in dv:
|
||||
dv[key] = False
|
||||
dv[self.KEY_TIMER] = 0
|
||||
super().__init__(default_values=dv)
|
||||
#
|
||||
self.sw_device = sw_device
|
||||
self.sw_method = sw_method
|
||||
self.motion_sensors = args
|
||||
self.timer_reload_value = timer_value
|
||||
#
|
||||
sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
|
||||
sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
|
||||
for motion_sensor in args:
|
||||
motion_sensor.add_callback(motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected, True)
|
||||
#
|
||||
self.add_callback(self.KEY_TIMER, 0, self.timer_expired, True)
|
||||
#
|
||||
cyclic_task = task.periodic(1, self.cyclic_task)
|
||||
cyclic_task.run()
|
||||
|
||||
def reload_timer(self, device, key, data):
|
||||
self.set(self.KEY_TIMER, self.timer_reload_value)
|
||||
|
||||
def reset_timer(self, device=None, key=None, data=None):
|
||||
self.set(self.KEY_TIMER, 0)
|
||||
|
||||
def set_motion_detected(self, device, key, data):
|
||||
for sensor_index, arg_device in enumerate(self.motion_sensors):
|
||||
if arg_device.topic == device.topic:
|
||||
break
|
||||
self.set(self.KEY_MOTION_SENSOR % sensor_index, data)
|
||||
if now() < sunrise_time(60) or now() > sunset_time(-60):
|
||||
if data is True:
|
||||
logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.sw_device.topic)
|
||||
self.sw_method(True)
|
||||
|
||||
def motion_detected(self):
|
||||
for i in range(0, len(self.motion_sensors)):
|
||||
if self[self.KEY_MOTION_SENSOR % i]:
|
||||
return True
|
||||
return False
|
||||
|
||||
def timer_expired(self, device, key, data):
|
||||
logger.info("No motion and time ran out - Switching off main light %s", self.sw_device.topic)
|
||||
self.sw_method(False)
|
||||
|
||||
def cyclic_task(self, cyclic_task):
|
||||
min_value = 10 if self.motion_detected() else 0
|
||||
if self[self.KEY_TIMER] != 0:
|
||||
self.set(self.KEY_TIMER, max(min_value, self[self.KEY_TIMER] - cyclic_task.cycle_time))
|
||||
|
@ -2,11 +2,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
import config
|
||||
import devices
|
||||
from function.helpers import now, sunset_time, sunrise_time
|
||||
import logging
|
||||
import task
|
||||
import inspect
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
@ -19,130 +16,27 @@ class room(object):
|
||||
def __init__(self, mqtt_client):
|
||||
self.mqtt_client = mqtt_client
|
||||
|
||||
def all_off(self, device=None, key=None, data=None):
|
||||
logger.info("Switching all off \"%s\"", type(self).__name__)
|
||||
for name, obj in inspect.getmembers(self):
|
||||
try:
|
||||
if obj.__module__ == 'devices':
|
||||
obj.all_off()
|
||||
except AttributeError:
|
||||
pass # not a module or has no method all_off
|
||||
|
||||
class room_shelly(room):
|
||||
def __init__(self, mqtt_client, topic_shelly, topic_gui):
|
||||
super().__init__(mqtt_client)
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, topic_shelly)
|
||||
#
|
||||
self.gui_main_light = devices.nodered_gui_light(mqtt_client, topic_gui)
|
||||
#
|
||||
# Callback initialisation
|
||||
#
|
||||
self.gui_main_light.add_callback(devices.nodered_gui_light.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_main_light.set_state_mcb)
|
||||
#
|
||||
self.block_all_off = False
|
||||
self.last_flash_data = None
|
||||
self.delayed_task = task.delayed(.25, self.main_light_shelly.toggle_output_0_mcb, None, None, None)
|
||||
|
||||
class room_collection(object):
|
||||
ALLOWED_CLASSES = ("room", "room_collection")
|
||||
|
||||
def __init__(self, mqtt_client):
|
||||
self.mqtt_client = mqtt_client
|
||||
|
||||
def all_off(self, device=None, key=None, data=None):
|
||||
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()
|
||||
|
||||
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__)
|
||||
self.main_light_shelly.toggle_output_0_mcb(device, key, data)
|
||||
self.delayed_task.run()
|
||||
self.last_flash_data = data
|
||||
|
||||
|
||||
class room_shelly_motion_sensor(room_shelly):
|
||||
def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_motion_sensor_1, topic_motion_sensor_2=None, timer_value=30):
|
||||
super().__init__(mqtt_client, topic_shelly, topic_gui)
|
||||
self.timer_value = timer_value
|
||||
self.motion_detected_1 = False
|
||||
self.motion_detected_2 = False
|
||||
#
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
|
||||
#
|
||||
self.motion_sensor_silvercrest_1 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_1)
|
||||
self.motion_sensor_silvercrest_1.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
|
||||
#
|
||||
if topic_motion_sensor_2 is not None:
|
||||
self.motion_sensor_silvercrest_2 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_2)
|
||||
self.motion_sensor_silvercrest_2.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected)
|
||||
#
|
||||
self.reset_timer()
|
||||
#
|
||||
cyclic_task = task.periodic(1, self.cyclic_task)
|
||||
cyclic_task.run()
|
||||
|
||||
def set_motion_detected(self, device, key, data):
|
||||
if device == self.motion_sensor_silvercrest_1:
|
||||
self.motion_detected_1 = data
|
||||
elif device == self.motion_sensor_silvercrest_2:
|
||||
self.motion_detected_2 = data
|
||||
self.gui_main_light.set_led(devices.nodered_gui_light.KEY_LED_0, self.motion_detected_1)
|
||||
self.gui_main_light.set_led(devices.nodered_gui_light.KEY_LED_1, self.motion_detected_2)
|
||||
if now() < sunrise_time(60) or now() > sunset_time(-60):
|
||||
if data is True:
|
||||
logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.main_light_shelly.topic)
|
||||
self.main_light_shelly.set_output_0(True)
|
||||
|
||||
def reload_timer(self, device, key, data):
|
||||
self.main_light_timer = self.timer_value
|
||||
|
||||
def reset_timer(self, device=None, key=None, data=None):
|
||||
self.main_light_timer = None
|
||||
self.gui_main_light.set_timer('-')
|
||||
|
||||
def cyclic_task(self, cyclic_task):
|
||||
if self.main_light_timer is not None:
|
||||
if self.main_light_timer <= 0:
|
||||
logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
|
||||
self.main_light_shelly.set_output_0(False)
|
||||
self.main_light_timer = None
|
||||
self.gui_main_light.set_timer('-')
|
||||
else:
|
||||
self.gui_main_light.set_timer(self.main_light_timer)
|
||||
if (self.motion_detected_1 or self.motion_detected_2) and self.main_light_timer <= self.timer_value / 10:
|
||||
self.main_light_timer = self.timer_value / 10
|
||||
else:
|
||||
self.main_light_timer -= cyclic_task.cycle_time
|
||||
|
||||
|
||||
class room_shelly_tradfri_light(room_shelly):
|
||||
def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_tradfri_light):
|
||||
super().__init__(mqtt_client, topic_shelly, topic_gui)
|
||||
self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
|
||||
#
|
||||
# Callback initialisation
|
||||
#
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_main_light.set_state_mcb)
|
||||
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_main_light.set_enable_mcb)
|
||||
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_main_light.set_brightness_mcb)
|
||||
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_main_light.set_color_temp_mcb)
|
||||
self.gui_main_light.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.main_light_tradfri.set_brightness_mcb)
|
||||
self.gui_main_light.add_callback(devices.nodered_gui_light.KEY_COLOR_TEMP, None, self.main_light_tradfri.set_color_temp_mcb)
|
||||
|
||||
|
||||
class room_shelly_silvercrest_light(room_shelly_tradfri_light):
|
||||
def __init__(self, mqtt_client, topic_shelly, topic_gui, topic_tradfri_light):
|
||||
super().__init__(mqtt_client, topic_shelly, topic_gui, topic_tradfri_light)
|
||||
#
|
||||
# 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 != data:
|
||||
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": ""}')
|
||||
logger.info("Switching all off \"%s\"", type(self).__name__)
|
||||
for sub_name in dir(self):
|
||||
# attribute name is not private
|
||||
if not sub_name.startswith("__"):
|
||||
sub = getattr(self, sub_name)
|
||||
if sub.__class__.__bases__[0].__name__ in self.ALLOWED_CLASSES:
|
||||
sub.all_off()
|
||||
|
@ -3,9 +3,12 @@
|
||||
#
|
||||
|
||||
import config
|
||||
from function.modules import brightness_choose_n_action
|
||||
import devices
|
||||
import logging
|
||||
from function.rooms import room_shelly_motion_sensor
|
||||
from function.modules import motion_sensor_light
|
||||
from function.rooms import room, room_collection
|
||||
from function.videv import videv_switching_motion
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
except ImportError:
|
||||
@ -13,9 +16,37 @@ except ImportError:
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
|
||||
|
||||
|
||||
class stairway(room_shelly_motion_sensor):
|
||||
class stairway(room_collection):
|
||||
def __init__(self, mqtt_client):
|
||||
super().__init__(mqtt_client)
|
||||
self.stairway = stairway_stairway(mqtt_client)
|
||||
|
||||
|
||||
class stairway_stairway(room):
|
||||
def __init__(self, mqtt_client):
|
||||
#
|
||||
# Device initialisation
|
||||
#
|
||||
# http://shelly1-3494546A9364
|
||||
super().__init__(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI,
|
||||
config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF,
|
||||
timer_value=config.USER_ON_TIME_STAIRWAYS)
|
||||
self.main_light_shelly = devices.shelly(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY)
|
||||
self.motion_sensor_gf = devices.silvercrest_motion_sensor(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF)
|
||||
self.motion_sensor_ff = devices.silvercrest_motion_sensor(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF)
|
||||
super().__init__(mqtt_client)
|
||||
|
||||
#
|
||||
# Functionality initialisation
|
||||
#
|
||||
self.motion_sensor_light = motion_sensor_light(
|
||||
self.main_light_shelly, self.main_light_shelly.set_output_0,
|
||||
self.motion_sensor_gf, self.motion_sensor_ff,
|
||||
timer_value=config.USER_ON_TIME_STAIRWAYS
|
||||
)
|
||||
|
||||
#
|
||||
# Virtual Device Interface
|
||||
#
|
||||
self.main_light_videv = videv_switching_motion(
|
||||
mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_VIDEV,
|
||||
self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
|
||||
self.motion_sensor_light
|
||||
)
|
||||
|
317
function/videv.py
Normal file
317
function/videv.py
Normal file
@ -0,0 +1,317 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
"""
|
||||
Virtual Device(s)
|
||||
|
||||
Targets:
|
||||
* MQTT-Interface to control joined devices as one virtual device
|
||||
* Primary signal routing
|
||||
* No functionality should be implemented here
|
||||
"""
|
||||
|
||||
from base import mqtt_base
|
||||
from function.rooms import room, room_collection
|
||||
import json
|
||||
import time
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
except ImportError:
|
||||
ROOT_LOGGER_NAME = 'root'
|
||||
|
||||
|
||||
class base(mqtt_base):
|
||||
KEY_INFO = '__info__'
|
||||
|
||||
def __init__(self, mqtt_client, topic, default_values=None):
|
||||
super().__init__(mqtt_client, topic, default_values=default_values)
|
||||
self.__display_dict__ = {}
|
||||
self.__control_dict__ = {}
|
||||
self.__capabilities__ = None
|
||||
self.__active_tx__ = {}
|
||||
|
||||
def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
|
||||
"""
|
||||
listen to data changes of ext_device and update videv information
|
||||
"""
|
||||
if ext_device.__class__.__name__ == "group":
|
||||
# store information to identify callback from ext_device
|
||||
self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
|
||||
# register a callback to listen for data from external device
|
||||
ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
|
||||
else:
|
||||
# store information to identify callback from ext_device
|
||||
self.__display_dict__[(id(ext_device), ext_key)] = my_key
|
||||
# register a callback to listen for data from external device
|
||||
ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
|
||||
# send default data to videv interface
|
||||
|
||||
def __rx_ext_device_data__(self, ext_device, ext_key, data):
|
||||
my_key = self.__display_dict__[(id(ext_device), ext_key)]
|
||||
self[my_key] = data
|
||||
self.__tx__(my_key, data)
|
||||
|
||||
def __tx__(self, key, data):
|
||||
if key in self.__control_dict__:
|
||||
self.__active_tx__[key] = (time.time(), data)
|
||||
if type(data) not in (str, ):
|
||||
data = json.dumps(data)
|
||||
self.mqtt_client.send(self.topic + '/' + key, data)
|
||||
self.__tx_capabilities__()
|
||||
|
||||
def __tx_capabilities__(self):
|
||||
self.mqtt_client.send(self.topic + '/' + self.KEY_INFO, json.dumps(self.capabilities))
|
||||
|
||||
def add_control(self, my_key, ext_device, ext_key, on_change_only=True):
|
||||
"""
|
||||
listen to videv information and pass data to ext_device
|
||||
"""
|
||||
self[my_key] = None
|
||||
# store information to identify callback from videv
|
||||
self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
|
||||
# add callback for videv changes
|
||||
self.mqtt_client.add_callback(self.topic + '/' + my_key, self.__rx_videv_data__)
|
||||
|
||||
def __rx_videv_data__(self, client, userdata, message):
|
||||
my_key = message.topic.split('/')[-1]
|
||||
try:
|
||||
data = json.loads(message.payload)
|
||||
except json.decoder.JSONDecodeError:
|
||||
data = message.payload
|
||||
if my_key in self.__active_tx__:
|
||||
tm, tx_data = self.__active_tx__.pop(my_key)
|
||||
do_ex = data != tx_data and time.time() - tm < 2
|
||||
else:
|
||||
do_ex = True
|
||||
if do_ex:
|
||||
ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
|
||||
if my_key in self.keys():
|
||||
if data != self[my_key] or not on_change_only:
|
||||
ext_device.set(ext_key, data)
|
||||
self.set(my_key, data)
|
||||
else:
|
||||
self.logger.info("Ignoring rx message with topic %s", message.topic)
|
||||
|
||||
def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
|
||||
"""
|
||||
listen to data changes of ext_device and update videv information
|
||||
and
|
||||
listen to videv information and pass data to ext_device
|
||||
"""
|
||||
# add display
|
||||
self.add_display(my_key, ext_device, ext_key, on_change_only_disp)
|
||||
self.add_control(my_key, ext_device, ext_key, on_change_only_videv)
|
||||
|
||||
@property
|
||||
def capabilities(self):
|
||||
if self.__capabilities__ is None:
|
||||
self.__capabilities__ = {}
|
||||
self.__capabilities__['__type__'] = self.__class__.__name__
|
||||
for key in self.__control_dict__:
|
||||
if not key in self.__capabilities__:
|
||||
self.__capabilities__[key] = {}
|
||||
self.__capabilities__[key]['control'] = True
|
||||
for key in self.__display_dict__.values():
|
||||
if not key in self.__capabilities__:
|
||||
self.__capabilities__[key] = {}
|
||||
self.__capabilities__[key]['display'] = True
|
||||
return self.__capabilities__
|
||||
|
||||
|
||||
class videv_switching(base):
|
||||
KEY_STATE = 'state'
|
||||
|
||||
def __init__(self, mqtt_client, topic, sw_device, sw_key):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_routing(self.KEY_STATE, sw_device, sw_key)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_switching_timer(base):
|
||||
KEY_STATE = 'state'
|
||||
KEY_TIMER = 'timer'
|
||||
|
||||
def __init__(self, mqtt_client, topic, sw_device, sw_key, tm_device, tm_key):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_routing(self.KEY_STATE, sw_device, sw_key)
|
||||
self.add_display(self.KEY_TIMER, tm_device, tm_key)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_switching_motion(base):
|
||||
KEY_STATE = 'state'
|
||||
#
|
||||
KEY_TIMER = 'timer'
|
||||
KEY_MOTION_SENSOR = 'motion_%d'
|
||||
|
||||
def __init__(self, mqtt_client, topic, sw_device, sw_key, motion_function):
|
||||
self.motion_sensors = motion_function.motion_sensors
|
||||
#
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_routing(self.KEY_STATE, sw_device, sw_key)
|
||||
self.add_display(self.KEY_TIMER, motion_function, motion_function.KEY_TIMER)
|
||||
# motion sensor state
|
||||
for index, motion_sensor in enumerate(self.motion_sensors):
|
||||
self.add_display(self.KEY_MOTION_SENSOR % index, motion_sensor, motion_sensor.KEY_OCCUPANCY)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_switch_brightness(base):
|
||||
KEY_STATE = 'state'
|
||||
KEY_BRIGHTNESS = 'brightness'
|
||||
|
||||
def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_routing(self.KEY_STATE, sw_device, sw_key)
|
||||
self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_switch_brightness_color_temp(base):
|
||||
KEY_STATE = 'state'
|
||||
KEY_BRIGHTNESS = 'brightness'
|
||||
KEY_COLOR_TEMP = 'color_temp'
|
||||
|
||||
def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key, ct_device, ct_key):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.add_routing(self.KEY_STATE, sw_device, sw_key)
|
||||
self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
|
||||
self.add_routing(self.KEY_COLOR_TEMP, ct_device, ct_key)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_heating(base):
|
||||
KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
|
||||
KEY_VALVE_TEMPERATURE_SETPOINT = 'valve_temperature_setpoint'
|
||||
KEY_AWAY_MODE = 'away_mode'
|
||||
KEY_SUMMER_MODE = 'summer_mode'
|
||||
KEY_START_BOOST = 'start_boost'
|
||||
KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
|
||||
KEY_BOOST_TIMER = 'boost_timer'
|
||||
#
|
||||
KEY_TEMPERATURE = 'temperature'
|
||||
|
||||
def __init__(self, mqtt_client, topic, heating_function):
|
||||
super().__init__(mqtt_client, topic)
|
||||
#
|
||||
self.add_routing(self.KEY_USER_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_USER_TEMPERATURE_SETPOINT)
|
||||
self.add_routing(self.KEY_AWAY_MODE, heating_function, heating_function.KEY_AWAY_MODE)
|
||||
self.add_routing(self.KEY_SUMMER_MODE, heating_function, heating_function.KEY_SUMMER_MODE)
|
||||
#
|
||||
self.add_control(self.KEY_START_BOOST, heating_function, heating_function.KEY_START_BOOST, False)
|
||||
self.add_control(self.KEY_SET_DEFAULT_TEMPERATURE, heating_function, heating_function.KEY_SET_DEFAULT_TEMPERATURE, False)
|
||||
#
|
||||
self.add_display(self.KEY_VALVE_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_TEMPERATURE_SETPOINT)
|
||||
self.add_display(self.KEY_BOOST_TIMER, heating_function, heating_function.KEY_BOOST_TIMER)
|
||||
self.add_display(self.KEY_TEMPERATURE, heating_function, heating_function.KEY_TEMPERATURE_CURRENT, False)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_multistate(base):
|
||||
KEY_STATE = 'state_%d'
|
||||
|
||||
def __init__(self, mqtt_client, topic, key_for_device, device, num_states, default_values=None):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.num_states = num_states
|
||||
# send default values
|
||||
for i in range(0, num_states):
|
||||
self.__tx__(self.KEY_STATE % i, False)
|
||||
#
|
||||
device.add_callback(key_for_device, None, self.__index_rx__, True)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
def __index_rx__(self, device, key, data):
|
||||
for i in range(0, self.num_states):
|
||||
self.__tx__(self.KEY_STATE % i, i == data)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
|
||||
class videv_audio_player(base):
|
||||
KEY_ACTIVE_PLAYER = 'player_%d'
|
||||
KEY_TITLE = 'title'
|
||||
NO_TITLE = '---'
|
||||
|
||||
def __init__(self, mqtt_client, topic, *args):
|
||||
super().__init__(mqtt_client, topic)
|
||||
for i, device in enumerate(args):
|
||||
self.add_display(self.KEY_ACTIVE_PLAYER % i, device, device.KEY_STATE)
|
||||
#
|
||||
for audio_device in args:
|
||||
audio_device.add_callback(audio_device.KEY_TITLE, None, self.__title_rx__, True)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
def __title_rx__(self, device, key, data):
|
||||
self.__tx__(self.KEY_TITLE, data or self.NO_TITLE)
|
||||
|
||||
@property
|
||||
def capabilities(self):
|
||||
super().capabilities
|
||||
self.__capabilities__[self.KEY_TITLE] = {'display': True}
|
||||
return self.__capabilities__
|
||||
|
||||
|
||||
class all_off(base):
|
||||
ALLOWED_CLASSES = (room, room_collection, )
|
||||
|
||||
def __init__(self, mqtt_client, topic, room_collection):
|
||||
super().__init__(mqtt_client, topic)
|
||||
self.__room_collection__ = room_collection
|
||||
# init __inst_dict__
|
||||
self.__inst_dict__ = {}
|
||||
self.__add_instances__("all", self.__room_collection__)
|
||||
# register mqtt callbacks for all my keys
|
||||
for key in self.__inst_dict__:
|
||||
mqtt_client.add_callback(topic + "/" + key, self.all_off)
|
||||
#
|
||||
self.__tx_capabilities__()
|
||||
|
||||
def __check_inst_capabilities__(self, name, inst):
|
||||
# fits to specified classes
|
||||
if isinstance(inst, self.ALLOWED_CLASSES):
|
||||
try:
|
||||
# all_off method is callable
|
||||
return callable(inst.all_off)
|
||||
except AttributeError:
|
||||
# all_off method does not exist
|
||||
return False
|
||||
return False
|
||||
|
||||
def __add_instances__(self, name, inst, level=0):
|
||||
if self.__check_inst_capabilities__(name, inst):
|
||||
# add given instance to my __inst_dict__
|
||||
self.__inst_dict__[name] = inst
|
||||
# iterate over all attribute names of instance
|
||||
for sub_name in dir(inst):
|
||||
# attribute name is not private
|
||||
if not sub_name.startswith("__"):
|
||||
sub = getattr(inst, sub_name)
|
||||
# recurse with this object
|
||||
if level == 0:
|
||||
self.__add_instances__(sub_name, sub, level=level+1)
|
||||
else:
|
||||
self.__add_instances__(name + "/" + sub_name, sub, level=level+1)
|
||||
|
||||
def all_off(self, client, userdata, message):
|
||||
key = message.topic[len(self.topic) + 1:]
|
||||
self.__inst_dict__[key].all_off()
|
||||
self.__tx_capabilities__()
|
||||
|
||||
@property
|
||||
def capabilities(self):
|
||||
if self.__capabilities__ is None:
|
||||
self.__capabilities__ = {}
|
||||
self.__capabilities__['__type__'] = self.__class__.__name__
|
||||
for key in self.__inst_dict__:
|
||||
self.__capabilities__[key] = {}
|
||||
self.__capabilities__[key]['control'] = True
|
||||
return self.__capabilities__
|
@ -1,80 +0,0 @@
|
||||
import config
|
||||
import logging
|
||||
import mqtt
|
||||
import readline
|
||||
import report
|
||||
from __simulation__.rooms import house
|
||||
from __simulation__.test import test_smarthome
|
||||
import time
|
||||
|
||||
if __name__ == "__main__":
|
||||
report.stdoutLoggingConfigure(((config.APP_NAME, logging.WARNING), ), report.SHORT_FMT)
|
||||
#
|
||||
mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
|
||||
password=config.MQTT_PASSWORD, name=config.APP_NAME + '_simulation')
|
||||
#
|
||||
COMMANDS = ['quit', 'help', 'test.smoke', 'test.full']
|
||||
#
|
||||
h = house(mc)
|
||||
for name in h.getmembers():
|
||||
d = h.getobjbyname(name)
|
||||
for c in d.capabilities():
|
||||
COMMANDS.append(name + '.' + c)
|
||||
#
|
||||
ts = test_smarthome(h)
|
||||
|
||||
def reduced_list(text):
|
||||
"""
|
||||
Create reduced completation list
|
||||
"""
|
||||
reduced_list = {}
|
||||
for cmd in COMMANDS:
|
||||
next_dot = cmd[len(text):].find('.')
|
||||
if next_dot >= 0:
|
||||
reduced_list[cmd[:len(text) + next_dot + 1]] = None
|
||||
else:
|
||||
reduced_list[cmd] = None
|
||||
return reduced_list.keys()
|
||||
|
||||
def completer(text, state):
|
||||
"""
|
||||
Our custom completer function
|
||||
"""
|
||||
options = [x for x in reduced_list(text) if x.startswith(text)]
|
||||
return options[state]
|
||||
|
||||
def complete(text, state):
|
||||
for cmd in COMMANDS:
|
||||
if cmd.startswith(text):
|
||||
if not state:
|
||||
hit = ""
|
||||
index = 0
|
||||
sub_list = cmd.split('.')
|
||||
while len(text) >= len(hit):
|
||||
hit += sub_list[index] + '.'
|
||||
return hit # cmd
|
||||
else:
|
||||
state -= 1
|
||||
|
||||
readline.parse_and_bind("tab: complete")
|
||||
readline.set_completer(completer)
|
||||
time.sleep(0.3)
|
||||
print("\nEnter command: ")
|
||||
|
||||
while True:
|
||||
userfeedback = input('')
|
||||
command = userfeedback.split(' ')[0]
|
||||
if userfeedback == 'quit':
|
||||
break
|
||||
elif userfeedback == 'help':
|
||||
print("Help is not yet implemented!")
|
||||
elif userfeedback == 'test.full':
|
||||
ts.full()
|
||||
elif userfeedback == 'test.smoke':
|
||||
ts.smoke()
|
||||
elif command in COMMANDS[2:]:
|
||||
h.command(userfeedback)
|
||||
elif userfeedback != "":
|
||||
print("Unknown command!")
|
||||
else:
|
||||
print()
|
@ -7,6 +7,11 @@ import time
|
||||
|
||||
logger = logging.getLogger(config.APP_NAME)
|
||||
|
||||
# TODO: Restructure nodered gui (own heating page - with circulation pump)
|
||||
# TODO: Rework devices to base.mqtt (pack -> set, ...)
|
||||
# TODO: Implement handling of warnings (videv element to show in webapp?)
|
||||
# TODO: implement garland (incl. day events like sunset, sunrise, ...)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if config.DEBUG:
|
||||
@ -19,8 +24,5 @@ if __name__ == "__main__":
|
||||
|
||||
func = function.all_functions(mc)
|
||||
|
||||
# for device in func.devicelist():
|
||||
# device.add_warning_callback(None)
|
||||
|
||||
while (True):
|
||||
time.sleep(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user