Compare commits

...

4 Commits

7 changed files with 403 additions and 114 deletions

View File

@ -1,17 +1,18 @@
import colored
import json
import sys
import task
import time
# TODO: implement my powerplug
COLOR_GUI_ACTIVE = colored.fg("light_blue")
COLOR_GUI_PASSIVE = COLOR_GUI_ACTIVE + colored.attr("dim")
COLOR_SHELLY_1 = colored.fg("light_magenta")
COLOR_SHELLY_2 = colored.fg("light_cyan")
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("red")
COLOR_MOTION_SENSOR = colored.fg("dark_orange_3b")
COLOR_RADIATOR_VALVE = colored.fg("red")
COLOR_REMOTE = colored.fg("green")
def payload_filter(payload):
@ -21,12 +22,19 @@ def payload_filter(payload):
return payload.decode("utf-8")
def percent_print(value):
def percent_bar(value):
rv = ""
for i in range(0, 10):
if (value - 5) > 10*i:
sys.stdout.write(u"\u25ac")
else:
sys.stdout.write(u"\u25ad")
rv += u"\u25ac" if (value - 5) > 10*i else u"\u25ad"
return rv
def green_led():
return colored.fg('green') + "\u2b24" + colored.attr("reset")
def grey_led():
return colored.fg('light_gray') + "\u2b24" + colored.attr("reset")
def command_int_value(value):
@ -36,6 +44,13 @@ def command_int_value(value):
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))
class base(object):
AUTOSEND = True
COMMANDS = []
@ -46,6 +61,7 @@ class base(object):
#
self.data = {}
self.callbacks = {}
self.names = {}
self.commands = self.COMMANDS[:]
#
self.mqtt_client.add_callback(self.topic, self.__rx__)
@ -56,6 +72,9 @@ class base(object):
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
@ -81,49 +100,125 @@ class base(object):
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", "set_relay_0", "unset_relay_0",
"get_relay_1", "set_relay_1", "unset_relay_1",
"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):
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.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 == self.topic + "/relay/0/command":
if value in ['on', 'off']:
self.store_data(**{self.KEY_OUTPUT_0: value})
elif value == 'toggle':
self.store_data(**{self.KEY_OUTPUT_0: 'on' if self.data.get(self.KEY_OUTPUT_0) == 'off' else 'off'})
elif message.topic == self.topic + "/relay/1/command":
if value in ['on', 'off']:
self.store_data(**{self.KEY_OUTPUT_1: value})
elif value == 'toggle':
self.store_data(**{self.KEY_OUTPUT_1: 'on' if self.data.get(self.KEY_OUTPUT_1) == 'off' else 'off'})
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.store_data(**{self.KEY_OUTPUT_0: "on"})
self.__toggle_data__(self.KEY_OUTPUT_0)
elif command == self.COMMANDS[2]:
self.store_data(**{self.KEY_OUTPUT_0: "off"})
elif command == self.COMMANDS[3]:
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.store_data(**{self.KEY_OUTPUT_1: "on"})
self.print_formatted(self, self.KEY_INPUT_0, self.data.get(self.KEY_INPUT_0))
elif command == self.COMMANDS[5]:
self.store_data(**{self.KEY_OUTPUT_1: "off"})
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:
@ -131,41 +226,43 @@ class shelly(base):
def print_formatted(self, device, key, value):
if value is not None:
if key == shelly.KEY_OUTPUT_0:
color = COLOR_SHELLY_1
else:
color = COLOR_SHELLY_2
if value == "on":
print(color + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
else:
print(color + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
icon = u'\u2b24' if value == "on" else u'\u25ef'
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)
devicename = " - ".join(self.topic.split('/')[1:])
print(COLOR_SHELLY + 10 * ' ' + icon + 9 * ' ' + devicename + ' ' + channel + colored.attr("reset"))
class my_powerplug(base):
KEY_OUTPUT_0 = "0"
KEY_OUTPUT_1 = "1"
KEY_OUTPUT_2 = "2"
KEY_OUTPUT_3 = "3"
COMMANDS = [
"get_state", "set_state", "unset_state",
"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, names=[]):
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
#
self.names = []
#
for i in range(0, 4):
self.data[str(i)] = False
try:
self.names.append(names[i])
except IndexError:
self.names.append("Channel %d" % (i + 1))
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'):
channel = int(message.topic.split('/')[-2]) - 1
payload = payload_filter(message.payload)
if payload == "toggle":
payload = not self.data.get(str(channel))
self.store_data(**{str(channel): payload})
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:
@ -176,9 +273,19 @@ class my_powerplug(base):
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'})
self.store_data(**{self.KEY_OUTPUT_0: not self.data.get(self.KEY_OUTPUT_0)})
elif command == self.COMMANDS[2]:
self.store_data(**{self.KEY_OUTPUT_0: 'off'})
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:
@ -186,12 +293,10 @@ class my_powerplug(base):
def print_formatted(self, device, key, value):
if value is not None:
if value:
print(COLOR_SHELLY_1 + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')
[1:]) + " (%s)" % self.names[int(key)] + colored.attr("reset"))
else:
print(COLOR_SHELLY_1 + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')
[1:]) + " (%s)" % self.names[int(key)] + colored.attr("reset"))
icon = u'\u2b24' if value else u'\u25ef'
channel = "(%s)" % self.names.get(key, "Channel %d" % (int(key) + 1))
devicename = " - ".join(self.topic.split('/')[1:])
print(COLOR_POWERPLUG + 10 * ' ' + icon + 9 * ' ' + devicename + ' ' + channel + colored.attr("reset"))
class silvercrest_powerplug(base):
@ -235,10 +340,10 @@ class silvercrest_powerplug(base):
def print_formatted(self, device, key, value):
if value is not None:
if value == "on":
print(COLOR_SHELLY_1 + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
else:
print(COLOR_SHELLY_1 + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
icon = u'\u2b24' if value == "on" else u'\u25ef'
channel = "(%s)" % self.names.get(key, key)
devicename = " - ".join(self.topic.split('/')[1:])
print(COLOR_POWERPLUG + 10 * ' ' + icon + 9 * ' ' + devicename + ' ' + channel + colored.attr("reset"))
class silvercrest_motion_sensor(base):
@ -268,9 +373,9 @@ class silvercrest_motion_sensor(base):
def print_formatted(self, device, key, value):
if value is not None:
if value:
print(COLOR_MOTION_SENSOR + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
print(COLOR_MOTION_SENSOR + 10 * ' ' + u'\u2b24' + 9 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
else:
print(COLOR_MOTION_SENSOR + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
print(COLOR_MOTION_SENSOR + 10 * ' ' + u'\u25ef' + 9 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
class tradfri_light(base):
@ -279,7 +384,7 @@ class tradfri_light(base):
KEY_COLOR_TEMP = "color_temp"
KEY_BRIGHTNESS_MOVE = "brightness_move"
#
STATE_COMMANDS = ("get_state", "change_state", )
STATE_COMMANDS = ("get_state", "toggle_state", )
BRIGHTNESS_COMMANDS = ("get_brightness", "set_brightness",)
COLOR_TEMP_COMMANDS = ("get_color_temp", "set_color_temp",)
@ -376,9 +481,9 @@ class tradfri_light(base):
color = COLOR_LIGHT_ACTIVE
if key == self.KEY_STATE:
if value == "on":
print(color + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
print(color + 10 * ' ' + u'\u2b24' + 9 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
else:
print(color + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
print(color + 10 * ' ' + u'\u25ef' + 9 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
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]:
@ -390,8 +495,8 @@ class tradfri_light(base):
value = round((value - 250) * 100 / 204, 0)
sys.stdout.write(color)
sys.stdout.write('B' if key == gui_light.KEY_BRIGHTNESS else 'C')
percent_print(value)
sys.stdout.write("%3d%% " % value)
sys.stdout.write(percent_bar(value))
sys.stdout.write("%3d%%" % value + 5 * " ")
print(" - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
@ -459,9 +564,9 @@ class gui_light(tradfri_light):
color = COLOR_GUI_ACTIVE
if key == self.KEY_STATE:
if value == True:
print(color + 10 * ' ' + u'\u25a0' + 6 * ' ' + " - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
print(color + 10 * ' ' + u'\u25a0' + 9 * ' ' + " - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
else:
print(color + 10 * ' ' + u'\u25a1' + 6*' ' + " - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
print(color + 10 * ' ' + u'\u25a1' + 9*' ' + " - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
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))
@ -471,8 +576,8 @@ class gui_light(tradfri_light):
value = round(value * 10 if key == self.KEY_COLOR_TEMP else value, 0)
sys.stdout.write(color)
sys.stdout.write('B' if key == self.KEY_BRIGHTNESS else 'C')
percent_print(value)
sys.stdout.write("%3d%% " % value)
sys.stdout.write(percent_bar(value))
sys.stdout.write("%3d%%" % value + 5 * " ")
print(" - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
@ -530,16 +635,11 @@ class gui_led_array(base):
KEY_LED_8 = "led8"
KEY_LED_9 = "led9"
def __init__(self, mqtt_client, topic, names=[]):
def __init__(self, mqtt_client, topic, ):
super().__init__(mqtt_client, topic)
self.names = {}
for i in range(0, 10):
key = getattr(self, "KEY_LED_%d" % i)
self.data[key] = False
try:
self.names[key] = names[i]
except IndexError:
self.names[key] = key
self.add_callback(key, self.print_formatted, None)
def __rx__(self, client, userdata, message):
@ -552,9 +652,159 @@ class gui_led_array(base):
print("Unknown key %s in %s" % (targetkey, self.__class__.__name__))
def print_formatted(self, device, key, value):
if value:
color = colored.fg('green')
led = green_led() if value else grey_led()
channel = '(%s)' % self.names.get(key, key)
devicename = ' - '.join(self.topic.split('/')[1:-1])
print(10 * ' ' + led + 9 * ' ' + COLOR_GUI_ACTIVE + devicename + ' ' + channel + colored.attr("reset"))
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:
color = ""
print(color + 10 * ' ' + u"\u2b24" + 6 * ' ' + COLOR_GUI_ACTIVE +
' - '.join(self.topic.split('/')[:-1]) + ' (%s)' % self.names[key] + colored.attr("reset"))
return
devicename = ' - '.join(self.topic.split('/')[1:-1])
print(COLOR_REMOTE + 10 * ' ' + icon + 6 * ' ' + devicename + colored.attr("reset"))
class gui_timer(base):
AUTOSEND = False
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
self.maxvalue = None
self.last_printed = None
def __rx__(self, client, userdata, message):
payload = payload_filter(message.payload)
if message.topic.startswith(self.topic) and message.topic.endswith('/feedback/set'):
if isinstance(payload, (int, float, complex)) and not isinstance(payload, bool):
if self.maxvalue is None:
self.maxvalue = payload
perc = payload / self.maxvalue * 100
if self.last_printed is None or abs(self.last_printed - perc) >= 4.8:
sys.stdout.write(COLOR_GUI_ACTIVE + 't' + percent_bar(perc))
print('%3d%%' % perc + 2 * ' ' + ' - '.join(self.topic.split('/')[1:-1]) + ' (%.1f)' % payload + colored.attr('reset'))
self.last_printed = perc
else:
self.maxvalue = None
self.last_printed = None
sys.stdout.write(COLOR_GUI_ACTIVE + 't' + percent_bar(0))
print('%3d%%' % 0 + 2 * ' ' + ' - '.join(self.topic.split('/')[1:-1]) + colored.attr('reset'))
else:
print("Unknown Message")
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
sys.stdout.write(COLOR_RADIATOR_VALVE + '\u03d1' + percent_bar(perc))
sys.stdout.write("%4.1f°C" % value + 3 * " ")
print(devicename + colored.attr("reset"))
class gui_radiator_valve(base):
AUTOSEND = False
#
TEMP_RANGE = [10, 30]
#
KEY_BOOST_LED = "boost_led"
KEY_TEMPERATURE_SETPOINT = "temperature_setpoint"
#
COMMANDS = [
"get_temperature_setpoint", "set_temperature_setpoint",
"get_boost_state", "trigger_boost",
]
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
self.add_callback(self.KEY_TEMPERATURE_SETPOINT, self.print_formatted, None)
self.add_callback(self.KEY_BOOST_LED, 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)
if type(payload) == bool:
self.store_data(**{self.KEY_BOOST_LED: payload})
else:
self.store_data(**{self.KEY_TEMPERATURE_SETPOINT: payload})
else:
print(message.topic)
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]:
################################### TEMPORARY!!! ###################################
self.mqtt_client.send(self.topic + "/feedback/set", command_float_value(value))
################################### TEMPORARY!!! ###################################
elif command == self.COMMANDS[2]:
self.print_formatted(self, self.KEY_BOOST_LED, self.data.get(self.KEY_BOOST_LED))
elif command == self.COMMANDS[3]:
################################### TEMPORARY!!! ###################################
self.mqtt_client.send(self.topic + "/state", json.dumps(True))
################################### TEMPORARY!!! ###################################
def print_formatted(self, device, key, value):
devicename = ' - '.join(self.topic.split('/')[1:])
if key == self.KEY_BOOST_LED:
led = green_led() if value else grey_led()
print(10 * ' ' + led + 9 * ' ' + COLOR_GUI_ACTIVE + devicename + " (Boost)" + colored.attr("reset"))
elif 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
sys.stdout.write(COLOR_GUI_ACTIVE + '\u03d1' + percent_bar(perc))
sys.stdout.write("%4.1f°C" % value + 3 * " ")
print(devicename + colored.attr("reset"))

View File

@ -1,14 +1,8 @@
import config
from __simulation__.devices import shelly, silvercrest_powerplug, gui_light, tradfri_light, tradfri_button, gui_led_array, silvercrest_motion_sensor, my_powerplug
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_timer, gui_radiator_valve
import inspect
# TODO: ffe_floor: Implement longpress shelly
# TODO: gfw_dirk: Implement 2nd input for floor light
# TODO: ffe_sleep: Implement heating valve
# TODO: gfw_dirk: Add at least brightness amplifier remote feedback to console
# TODO: ffe_diningroom: Implement garland gui_switch
# TODO: ffe_kitchen: Implement circulation pump
class base(object):
def getmembers(self, prefix=''):
@ -47,7 +41,8 @@ class base(object):
class gfw_floor(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY)
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")
@ -60,27 +55,38 @@ class gfw_floor(base):
class gfw_marion(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY)
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")
class gfw_dirk(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY)
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.gui_br_ct_main_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI_BR_CT, False, True, True)
#
self.powerplug = my_powerplug(mqtt_client, config.TOPIC_GFW_DIRK_POWERPLUG, ["Amplifier", "Desk Light", "CD-Player", "PC-Dock"])
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_switch_amplifier = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_GUI_SWITCH, True, False, False)
self.gui_switch_desk_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI_SWITCH, True, False, False)
self.gui_br_ct_desk_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI_BR_CT, False, True, True)
self.gui_switch_cd_player = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_GUI_SWITCH, True, False, False)
self.gui_switch_pc_dock = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_GUI_SWITCH, 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, ["Main Light", "Desk Light", "Amplifier"])
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")
class gfw(base):
@ -93,7 +99,8 @@ class gfw(base):
class ffw_julian(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY)
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")
@ -103,7 +110,8 @@ class ffw_julian(base):
class ffw_livingroom(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY)
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(base):
@ -115,28 +123,40 @@ class ffw(base):
class ffe_floor(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY)
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_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_SHELLY)
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_switch_circulation_pump = gui_light(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI_SWITCH, 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")
self.gui_timer_circulation_pump = gui_timer(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI_TIMER)
class ffe_diningroom(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_SHELLY)
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_switch_floor_lamp = gui_light(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_GUI_SWITCH, 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")
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_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_SHELLY)
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")
@ -147,13 +167,21 @@ class ffe_sleep(base):
self.gui_br_ct_bed_light_di = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_GUI_BR_CT, False, True, False)
#
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, ["Main Light", "Bed Light Dirk"])
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.heating_valve = brennenstuhl_radiator_valve(mqtt_client, config.TOPIC_FFE_SLEEP_RADIATOR_VALVE_ZIGBEE)
self.gui_heating_valve = gui_radiator_valve(mqtt_client, "gui/ffe_ts_sleep_madi")
self.gui_heating_valve_boost_led = gui_radiator_valve(mqtt_client, "gui/ffe_bl_sleep_madi")
self.gui_heating_valve_boost_button = gui_radiator_valve(mqtt_client, "gui/ffe_bo_sleep_madi")
class ffe_livingroom(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_SHELLY)
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")
@ -176,7 +204,8 @@ class ffe(base):
class stairway(base):
def __init__(self, mqtt_client):
self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI_SWITCH, True, False, False)
self.main_light = shelly(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY)
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)

View File

@ -801,7 +801,7 @@ class brennenstuhl_heatingvalve(base):
self.KEY_CHILD_LOCK: "UNLOCK", self.KEY_VALVE_DETECTION: "ON", self.KEY_SYSTEM_MODE: "heat"}))
def warning_call_condition(self):
return self.get(self.KEY_BATTERY) <= BATTERY_WARN_LEVEL
return self.get(self.KEY_BATTERY, 100) <= BATTERY_WARN_LEVEL
def warning_text(self):
return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))

View File

@ -16,8 +16,12 @@ except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
# TODO: implement TOPIC-changes in config.py (gfw.floor.main_light)
# TODO: implement gui element for tradfri light as one class
# TODO: implement devices.nodered_gui_heatvalve incl. setpoint, boost, ... + replace led with timer
# TODO: improve heating function
# TODO: implement timer and motion leds for stairwasy
# TODO: implement devices.nodered_gui_timer (for circulation pump)
# implement devices.nodered_gui_heatvalve incl. setpoint, boost, ... and functions from funtion.module
# improve devices.brennenstuhl_heatvalve
# improve the implementation in common if highly reduced, just move it to function.__init__.py
# TODO: implement garland (incl. day events like sunset, sunrise, ...)
@ -71,6 +75,9 @@ class all_functions(object):
# 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)
@ -121,7 +128,7 @@ class all_functions(object):
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_main_light(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):

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
import config
import devices
from function.rooms import room_shelly
from function.modules import heating_function_brennenstuhl
@ -18,11 +19,11 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
class common_circulation_pump(room_shelly):
def __init__(self, mqtt_client):
# http://shelly1-E89F6D85A466
super().__init__(mqtt_client, "shellies/ffe/kitchen/circulation_pump", "gui/none/common/circulation_pump/switch")
super().__init__(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_SHELLY, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI_SWITCH)
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.circ_pump_actions, True)
#
self.gui_timer_view = devices.nodered_gui_heatvalve(mqtt_client, "gui/ffe_circ_pump_timer")
self.gui_timer_view = devices.nodered_gui_heatvalve(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI_TIMER)
self.gui_timer_view.set_feedback('-')
#
self.ct = task.periodic(6, self.cyclic_task)

View File

@ -20,11 +20,11 @@ class ground_floor_west_floor(room_shelly_silvercrest_light):
# http://shelly1l-84CCA8AD1148
def __init__(self, mqtt_client):
super().__init__(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI_SWITCH,
config.TOPIC_GFW_FLOOR_MAIN_LIGHT_A_ZIGBEE, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI_BR_CT)
config.TOPIC_GFW_FLOOR_MAIN_LIGHT_1_ZIGBEE, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI_BR_CT)
#
# Callback initialisation
#
self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_B_ZIGBEE)
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)

View File

@ -66,5 +66,7 @@ if __name__ == "__main__":
print("Help is not yet implemented!")
elif command in COMMANDS[2:]:
h.command(userfeedback)
else:
elif userfeedback != "":
print("Unknown command!")
else:
print()