Compare commits

...

3 Commits

7 changed files with 68 additions and 92 deletions

View File

@ -35,7 +35,6 @@ try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
BATTERY_WARN_LEVEL = 5
@ -65,6 +64,9 @@ class base(dict):
# data storage
self.mqtt_client = mqtt_client
self.topic = topic
self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
for entry in self.topic.split('/'):
self.logger = self.logger.getChild(entry)
# initialisations
dict.__init__(self)
mqtt_client.add_callback(
@ -94,17 +96,17 @@ class base(dict):
self.__previous__[key] = prev_value
# Filter, if needed
self.unpack_filter(key)
logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key)))
self.logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key)))
self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
elif key not in self.RX_IGNORE_KEYS:
logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key)
self.logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key)
else:
logger.debug("Ignoring key %s", key)
self.logger.debug("Ignoring key %s", key)
def unpack(self, message):
content_key = message.topic[len(self.topic) + 1:]
if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0):
logger.debug("Unpacking content_key \"%s\" from message.", content_key)
self.logger.debug("Unpacking content_key \"%s\" from message.", content_key)
if is_json(message.payload):
data = json.loads(message.payload)
if type(data) is dict:
@ -118,7 +120,7 @@ class base(dict):
content_key, message.payload.decode('utf-8'))
self.warning_caller()
else:
logger.debug("Ignoring topic %s", content_key)
self.logger.debug("Ignoring topic %s", content_key)
def pack_filter(self, key, data):
if key in self.TX_FILTER_DATA_KEYS:
@ -137,10 +139,9 @@ class base(dict):
data = self.pack_filter(key, data)
if self.TX_TOPIC is not None:
if self.TX_TYPE < 0:
logger.error(
"Unknown tx type. Set TX_TYPE of class to a known value")
self.logger.error("Unknown tx type. Set TX_TYPE of class to a known value")
else:
logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data))
self.logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data))
if self.TX_TYPE == self.TX_DICT:
self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
else:
@ -148,8 +149,7 @@ class base(dict):
data = json.dumps(data)
self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
else:
logger.error(
"Unknown tx toptic. Set TX_TOPIC of class to a known value")
self.logger.error("Unknown tx toptic. Set TX_TOPIC of class to a known value")
def add_callback(self, key, data, callback, on_change_only=False):
"""
@ -175,7 +175,7 @@ class base(dict):
def warning_caller(self):
if self.warning_call_condition():
warn_txt = self.warning_text()
logger.warning(warn_txt)
self.logger.warning(warn_txt)
if self.warning_callback is not None:
self.warning_callback(self, warn_txt)
@ -215,12 +215,6 @@ class shelly(base):
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
def add_on_off_callback_0(self, callback):
self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
def add_on_off_callback_1(self, callback):
self.add_callback(self.KEY_OUTPUT_1, None, callback, True)
#
# WARNING CALL
#
@ -280,11 +274,11 @@ class shelly(base):
self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, data):
logger.info("%s: Toggeling output 0", self.topic)
self.logger.info("%s: Toggeling output 0", self.topic)
self.set_output_0('toggle')
def set_output_1(self, state):
@ -292,11 +286,11 @@ class shelly(base):
self.pack(self.KEY_OUTPUT_1, state)
def set_output_1_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
self.set_output_1(data)
def toggle_output_1_mcb(self, device, key, data):
logger.info("%s: Toggeling output 1", self.topic)
self.logger.info("%s: Toggeling output 1", self.topic)
self.set_output_1('toggle')
@ -313,9 +307,6 @@ class silvercrest_powerplug(base):
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
def add_default_on_off_callback(self, callback):
self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
#
# RX
#
@ -337,11 +328,11 @@ class silvercrest_powerplug(base):
self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, data):
logger.info("%s: Toggeling output 0", self.topic)
self.logger.info("%s: Toggeling output 0", self.topic)
self.set_output_0('toggle')
@ -389,18 +380,6 @@ class my_powerplug(base):
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
def add_on_off_callback_0(self, callback):
self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
def add_on_off_callback_1(self, callback):
self.add_callback(self.KEY_OUTPUT_1, None, callback, True)
def add_on_off_callback_2(self, callback):
self.add_callback(self.KEY_OUTPUT_2, None, callback, True)
def add_on_off_callback_3(self, callback):
self.add_callback(self.KEY_OUTPUT_3, None, callback, True)
#
# RX
#
@ -438,11 +417,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, data):
logger.info("%s: Toggeling output 0", self.topic)
self.logger.info("%s: Toggeling output 0", self.topic)
self.set_output_0('toggle')
def set_output_1(self, state):
@ -450,11 +429,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_1, state)
def set_output_1_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
self.set_output_1(data)
def toggle_output_1_mcb(self, device, key, data):
logger.info("%s: Toggeling output 1", self.topic)
self.logger.info("%s: Toggeling output 1", self.topic)
self.set_output_1('toggle')
def set_output_2(self, state):
@ -462,11 +441,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_2, state)
def set_output_2_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data))
self.set_output_2(data)
def toggle_output_2_mcb(self, device, key, data):
logger.info("%s: Toggeling output 2", self.topic)
self.logger.info("%s: Toggeling output 2", self.topic)
self.set_output_2('toggle')
def set_output_3(self, state):
@ -474,11 +453,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_3, state)
def set_output_3_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data))
self.set_output_3(data)
def toggle_output_3_mcb(self, device, key, data):
logger.info("%s: Toggeling output 3", self.topic)
self.logger.info("%s: Toggeling output 3", self.topic)
self.set_output_3('toggle')
def set_output_all(self, state):
@ -486,11 +465,11 @@ class my_powerplug(base):
self.pack(self.KEY_OUTPUT_ALL, state)
def set_output_all_mcb(self, device, key, data):
logger.info("%s: Changing all outputs to %s", self.topic, str(data))
self.logger.info("%s: Changing all outputs to %s", self.topic, str(data))
self.set_output_all(data)
def toggle_output_all_mcb(self, device, key, data):
logger.info("%s: Toggeling all outputs", self.topic)
self.logger.info("%s: Toggeling all outputs", self.topic)
self.set_output_0('toggle')
@ -511,9 +490,6 @@ class tradfri_light(base):
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
def add_on_off_callback_0(self, callback):
self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
def unpack_filter(self, key):
if key == self.KEY_BRIGHTNESS:
self[key] = (self[key] - 1) * 100 / 254
@ -561,11 +537,11 @@ class tradfri_light(base):
self.pack(self.KEY_OUTPUT_0, state)
def set_output_0_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
self.set_output_0(data)
def toggle_output_0_mcb(self, device, key, data):
logger.info("%s: Toggeling output 0", self.topic)
self.logger.info("%s: Toggeling output 0", self.topic)
self.set_output_0('toggle')
def set_brightness(self, brightness):
@ -573,7 +549,7 @@ class tradfri_light(base):
self.pack(self.KEY_BRIGHTNESS, brightness)
def set_brightness_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data))
self.set_brightness(data)
def default_inc(self, speed=40):
@ -590,7 +566,7 @@ class tradfri_light(base):
self.pack(self.KEY_COLOR_TEMP, color_temp)
def set_color_temp_mcb(self, device, key, data):
logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data))
self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data))
self.set_color_temp(data)
@ -720,7 +696,7 @@ class nodered_gui_switch(nodered_gui_button):
self.pack(self.KEY_STATE, data)
def set_state_mcb(self, device, key, data):
logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.set_state(data)
@ -759,7 +735,7 @@ class nodered_gui_brightness_color_temp(base):
self.pack(self.KEY_ENABLE, data)
def set_enable_mcb(self, device, key, data):
logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.set_enable(data)
def set_brightness(self, data):
@ -767,7 +743,7 @@ class nodered_gui_brightness_color_temp(base):
self.pack(self.KEY_BRIGHTNESS, data)
def set_brightness_mcb(self, device, key, data):
logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.set_brightness(data)
def set_color_temp(self, data):
@ -775,7 +751,7 @@ class nodered_gui_brightness_color_temp(base):
self.pack(self.KEY_COLOR_TEMP, data)
def set_color_temp_mcb(self, device, key, data):
logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.set_color_temp(data)
@ -797,7 +773,7 @@ class nodered_gui_leds(base):
def set_led(self, key, data):
"""data: [True, False]"""
logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.logger.debug("%s: Sending %s with content %s", self.topic, key, str(data))
self.pack(key, data)
@ -852,7 +828,7 @@ class brennenstuhl_heatingvalve(base):
self.pack(self.KEY_HEATING_SETPOINT, setpoint)
def set_heating_setpoint_mcb(self, device, key, data):
logger.info("%s: Changing heating setpoint to %s", self.topic, str(data))
self.logger.info("%s: Changing heating setpoint to %s", self.topic, str(data))
self.set_heating_setpoint(data)
@ -915,7 +891,7 @@ class status(base):
self.pack(self.KEY_STATE + "/" + str(num), data)
def set_state_mcb(self, device, key, data):
logger.info("%s: Changing state to %s", self.topic, str(data))
self.logger.info("%s: Changing state to %s", self.topic, str(data))
self.set_state(data)

View File

@ -59,10 +59,10 @@ class all_functions(object):
def init_off_functionality(self):
# Off Buttons
self.gui_button_all_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/all/common/off/button")
self.gui_button_gfw_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/gfw/common/off/button")
self.gui_button_ffw_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/ffw/common/off/button")
self.gui_button_ffe_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/ffe/common/off/button")
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)

View File

@ -63,8 +63,8 @@ class first_floor_east_sleep(room_shelly_tradfri_light):
self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE)
#
self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_LED)
self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly.add_on_off_callback_0)
self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri.add_on_off_callback_0)
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)
#
# Callback initialisation
#

View File

@ -77,9 +77,9 @@ class ground_floor_west_dirk(room_shelly_tradfri_light):
self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
#
self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED)
self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly.add_on_off_callback_0)
self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common.add_on_off_callback_1)
self.brightness_functions.add(self.remote_amplifier, self.powerplug_common.add_on_off_callback_0)
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)

View File

@ -28,46 +28,46 @@ class brightness_choose_n_action(object):
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)
#
self.device_list = []
self.brightness_device_list = []
self.callback_device_list = []
self.device_states = []
self.active_device_state = None
self.update_active_device_led()
def add(self, device, add_callback):
def add(self, brightness_device, callback_device, callback_key):
"""
device: A device for brightness function needs to have the following methods:
brightness_device: A device for brightness function needs to have the following methods:
* .default_inc()
* .default_dec()
* .default_stop()
add_callback: A on/off callback which tages the <function_to_be_executed> as argument and
- which calls the callback only on changes
- which calls with the following arguments: device, key, data
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.device_list) >= len(devices.nodered_gui_leds.RX_KEYS):
if len(self.brightness_device_list) >= len(devices.nodered_gui_leds.RX_KEYS):
raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
self.device_list.append(device)
self.callback_device_list.append(add_callback.__self__)
self.brightness_device_list.append(brightness_device)
self.callback_device_list.append((callback_device, callback_key))
self.device_states.append(False)
add_callback(self.device_state_action)
callback_device.add_callback(callback_key, True, self.device_state_action, True)
callback_device.add_callback(callback_key, False, self.device_state_action, True)
def device_state_action(self, device, key, data):
self.device_states[self.callback_device_list.index(device)] = 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)
self.active_device_state = self.callback_device_list.index((device, key))
self.update_active_device_led()
else:
self.choose_next_device()
def update_active_device_led(self):
for i in range(0, len(self.device_list)):
for i in range(0, len(self.brightness_device_list)):
self.gui_led_active_device.set_led(devices.nodered_gui_leds.RX_KEYS[i], self.active_device_state == i)
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
for i in range(0, len(self.device_list)):
target_state = (start_value - i - 1) % (len(self.device_list))
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()
@ -78,8 +78,8 @@ class brightness_choose_n_action(object):
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
for i in range(0, len(self.device_list)):
target_state = (start_value + i + 1) % (len(self.device_list))
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()
@ -89,7 +89,7 @@ class brightness_choose_n_action(object):
def brightness_action(self, device, key, data):
if self.active_device_state is not None:
target = self.device_list[self.active_device_state]
target = self.brightness_device_list[self.active_device_state]
if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
target.default_inc()

View File

@ -15,7 +15,7 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
class stairway(room_shelly_motion_sensor):
def __init__(self, mqtt_client):
# http://
# http://shelly1-3494546A9364
super().__init__(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI_SWITCH,
config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF,
config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MIN_ON_TIME)

2
mqtt

@ -1 +1 @@
Subproject commit 79ac04ffdb61ea61334b2bb90bee565472957657
Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301