diff --git a/base.py b/base.py index 32fb460..186887d 100644 --- a/base.py +++ b/base.py @@ -76,7 +76,9 @@ class videv_base(mqtt_base): # 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 initial display data to videv interface - self.__tx__(my_key, ext_device.get(ext_key)) + data = ext_device.get(ext_key) + if data is not None: + self.__tx__(my_key, data) def __rx_ext_device_data__(self, ext_device, ext_key, data): my_key = self.__display_dict__[(id(ext_device), ext_key)] diff --git a/function/db.py b/function/db.py index 2802f01..e0a080f 100644 --- a/function/db.py +++ b/function/db.py @@ -1,15 +1,31 @@ +from function.modules import heating_function import os import sqlite3 db_file = os.path.join(os.path.dirname(__file__), '..', 'database.db') +db_mapping_radiator = { + 0: heating_function.KEY_AWAY_MODE, + 1: heating_function.KEY_SUMMER_MODE, + 2: heating_function.KEY_USER_TEMPERATURE_SETPOINT, + 3: heating_function.KEY_TEMPERATURE_SETPOINT +} + def get_radiator_data(topic): - return __storage__().get_radiator_data(topic) + db_data = __storage__().get_radiator_data(topic) + rv = {} + for index in db_mapping_radiator: + rv[db_mapping_radiator[index]] = db_data[index] + return rv -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) +def set_radiator_data(device, key, data): + if key in db_mapping_radiator.values(): + db_data = [] + for index in range(0, len(db_mapping_radiator)): + db_data.append(device.get(db_mapping_radiator[index])) + return __storage__().store_radiator_data(device.heating_valve.topic, db_data) class __storage__(object): @@ -25,19 +41,17 @@ class __storage__(object): temperatur_setpoint real )""") - 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] + def store_radiator_data(self, topic, target_data): try: with self.conn: self.c.execute( - 'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', data) + 'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', [topic] + target_data) except sqlite3.IntegrityError: - data = [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint] db_data = self.get_radiator_data(topic) - if db_data != data: + if db_data != target_data: with self.conn: self.c.execute( - 'UPDATE radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ?, temperatur_setpoint = ? WHERE topic = ?', data + [topic]) + 'UPDATE radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ?, temperatur_setpoint = ? WHERE topic = ?', target_data + [topic]) def get_radiator_data(self, topic): """ returns a list [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint] or [None, None, None, None]""" diff --git a/function/first_floor_east.py b/function/first_floor_east.py index fe15422..8774364 100644 --- a/function/first_floor_east.py +++ b/function/first_floor_east.py @@ -4,6 +4,7 @@ import config import devices +from function.db import get_radiator_data, set_radiator_data from function.helpers import day_event from function.modules import brightness_choose_n_action, timer_on_activation, heating_function from function.rooms import room, room_collection @@ -157,7 +158,12 @@ class first_floor_east_sleep(room): self.bed_light_ma_powerplug.toggle_output_0_mcb) # heating function - self.heating_function = heating_function(self.heating_valve) + self.heating_function = heating_function( + self.heating_valve, + config.DEFAULT_TEMPERATURE[self.heating_valve.topic], + **get_radiator_data(self.heating_valve.topic) + ) + self.heating_function.add_callback(None, None, set_radiator_data, True) # # Virtual Device Interface diff --git a/function/first_floor_west.py b/function/first_floor_west.py index 37d406b..c4bf8b6 100644 --- a/function/first_floor_west.py +++ b/function/first_floor_west.py @@ -4,6 +4,7 @@ import config import devices +from function.db import get_radiator_data, set_radiator_data 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 @@ -58,7 +59,12 @@ class first_floor_west_bath(room): # Functionality initialisation # # heating function - self.heating_function = heating_function(self.heating_valve) + self.heating_function = heating_function( + self.heating_valve, + config.DEFAULT_TEMPERATURE[self.heating_valve.topic], + **get_radiator_data(self.heating_valve.topic) + ) + self.heating_function.add_callback(None, None, set_radiator_data, True) # # Virtual Device Interface diff --git a/function/ground_floor_west.py b/function/ground_floor_west.py index 3577fe1..76add53 100644 --- a/function/ground_floor_west.py +++ b/function/ground_floor_west.py @@ -4,6 +4,7 @@ import config import devices +from function.db import get_radiator_data, set_radiator_data 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 @@ -69,7 +70,12 @@ class ground_floor_west_marion(room): # Functionality initialisation # # heating function - self.heating_function = heating_function(self.heating_valve) + self.heating_function = heating_function( + self.heating_valve, + config.DEFAULT_TEMPERATURE[self.heating_valve.topic], + **get_radiator_data(self.heating_valve.topic) + ) + self.heating_function.add_callback(None, None, set_radiator_data, True) # # Virtual Device Interface @@ -150,7 +156,12 @@ class ground_floor_west_dirk(room): self.audio_source = self.AUDIO_SOURCE_PC # heating function - self.heating_function = heating_function(self.heating_valve) + self.heating_function = heating_function( + self.heating_valve, + config.DEFAULT_TEMPERATURE[self.heating_valve.topic], + **get_radiator_data(self.heating_valve.topic) + ) + self.heating_function.add_callback(None, None, set_radiator_data, True) # # Virtual Device Interface diff --git a/function/modules.py b/function/modules.py index 028640a..24ee11a 100644 --- a/function/modules.py +++ b/function/modules.py @@ -14,7 +14,6 @@ Targets: from base import common_base import config import devices -from function.db import get_radiator_data, set_radiator_data from function.helpers import day_state import logging import task @@ -153,19 +152,19 @@ class heating_function(common_base): AWAY_REDUCTION = 5 SUMMER_TEMPERATURE = 5 - def __init__(self, heating_valve): + def __init__(self, heating_valve, default_temperature, **kwargs): self.heating_valve = heating_valve - self.default_temperature = config.DEFAULT_TEMPERATURE[heating_valve.topic] - db_data = get_radiator_data(heating_valve.topic) + self.default_temperature = default_temperature + # 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.KEY_USER_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature), + self.KEY_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature), + self.KEY_TEMPERATURE_CURRENT: kwargs.get(self.KEY_TEMPERATURE_CURRENT, None), + self.KEY_AWAY_MODE: kwargs.get(self.KEY_AWAY_MODE, False), + self.KEY_SUMMER_MODE: kwargs.get(self.KEY_SUMMER_MODE, False), + self.KEY_START_BOOST: kwargs.get(self.KEY_START_BOOST, True), + self.KEY_SET_DEFAULT_TEMPERATURE: kwargs.get(self.KEY_SET_DEFAULT_TEMPERATURE, False), + self.KEY_BOOST_TIMER: kwargs.get(self.KEY_BOOST_TIMER, 0) }) # self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT]) @@ -200,10 +199,7 @@ class heating_function(common_base): self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired]) def send_command(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 + return super().set(key, data, block_callback) def away_mode(self, device, key, value): if value is True: diff --git a/smart_brain.py b/smart_brain.py index 96e2366..505d34b 100644 --- a/smart_brain.py +++ b/smart_brain.py @@ -13,7 +13,7 @@ logger = logging.getLogger(config.APP_NAME) VERS_MAJOR = 1 VERS_MINOR = 2 -VERS_PATCH = 5 +VERS_PATCH = 6 INFO_TOPIC = "__info__" INFO_DATA = {