Browse Source

Rework store and restore heating function data from and to database

tags/v1.2.6^0
Dirk Alders 1 year ago
parent
commit
882ea0230c
7 changed files with 66 additions and 31 deletions
  1. 3
    1
      base.py
  2. 23
    9
      function/db.py
  3. 7
    1
      function/first_floor_east.py
  4. 7
    1
      function/first_floor_west.py
  5. 13
    2
      function/ground_floor_west.py
  6. 12
    16
      function/modules.py
  7. 1
    1
      smart_brain.py

+ 3
- 1
base.py View File

76
             # register a callback to listen for data from external device
76
             # register a callback to listen for data from external device
77
             ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
77
             ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
78
         # send initial display data to videv interface
78
         # send initial display data to videv interface
79
-        self.__tx__(my_key, ext_device.get(ext_key))
79
+        data = ext_device.get(ext_key)
80
+        if data is not None:
81
+            self.__tx__(my_key, data)
80
 
82
 
81
     def __rx_ext_device_data__(self, ext_device, ext_key, data):
83
     def __rx_ext_device_data__(self, ext_device, ext_key, data):
82
         my_key = self.__display_dict__[(id(ext_device), ext_key)]
84
         my_key = self.__display_dict__[(id(ext_device), ext_key)]

+ 23
- 9
function/db.py View File

1
+from function.modules import heating_function
1
 import os
2
 import os
2
 import sqlite3
3
 import sqlite3
3
 
4
 
4
 db_file = os.path.join(os.path.dirname(__file__), '..', 'database.db')
5
 db_file = os.path.join(os.path.dirname(__file__), '..', 'database.db')
5
 
6
 
7
+db_mapping_radiator = {
8
+    0: heating_function.KEY_AWAY_MODE,
9
+    1: heating_function.KEY_SUMMER_MODE,
10
+    2: heating_function.KEY_USER_TEMPERATURE_SETPOINT,
11
+    3: heating_function.KEY_TEMPERATURE_SETPOINT
12
+}
13
+
6
 
14
 
7
 def get_radiator_data(topic):
15
 def get_radiator_data(topic):
8
-    return __storage__().get_radiator_data(topic)
16
+    db_data = __storage__().get_radiator_data(topic)
17
+    rv = {}
18
+    for index in db_mapping_radiator:
19
+        rv[db_mapping_radiator[index]] = db_data[index]
20
+    return rv
9
 
21
 
10
 
22
 
11
-def set_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint):
12
-    return __storage__().store_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint)
23
+def set_radiator_data(device, key, data):
24
+    if key in db_mapping_radiator.values():
25
+        db_data = []
26
+        for index in range(0, len(db_mapping_radiator)):
27
+            db_data.append(device.get(db_mapping_radiator[index]))
28
+        return __storage__().store_radiator_data(device.heating_valve.topic, db_data)
13
 
29
 
14
 
30
 
15
 class __storage__(object):
31
 class __storage__(object):
25
                     temperatur_setpoint real
41
                     temperatur_setpoint real
26
                 )""")
42
                 )""")
27
 
43
 
28
-    def store_radiator_data(self, topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint):
29
-        data = [topic, away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint]
44
+    def store_radiator_data(self, topic, target_data):
30
         try:
45
         try:
31
             with self.conn:
46
             with self.conn:
32
                 self.c.execute(
47
                 self.c.execute(
33
-                    'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', data)
48
+                    'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', [topic] + target_data)
34
         except sqlite3.IntegrityError:
49
         except sqlite3.IntegrityError:
35
-            data = [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint]
36
             db_data = self.get_radiator_data(topic)
50
             db_data = self.get_radiator_data(topic)
37
-            if db_data != data:
51
+            if db_data != target_data:
38
                 with self.conn:
52
                 with self.conn:
39
                     self.c.execute(
53
                     self.c.execute(
40
-                        'UPDATE radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ?, temperatur_setpoint = ? WHERE topic = ?', data + [topic])
54
+                        'UPDATE radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ?, temperatur_setpoint = ? WHERE topic = ?', target_data + [topic])
41
 
55
 
42
     def get_radiator_data(self, topic):
56
     def get_radiator_data(self, topic):
43
         """ returns a list [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint] or [None, None, None, None]"""
57
         """ returns a list [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint] or [None, None, None, None]"""

+ 7
- 1
function/first_floor_east.py View File

4
 
4
 
5
 import config
5
 import config
6
 import devices
6
 import devices
7
+from function.db import get_radiator_data, set_radiator_data
7
 from function.helpers import day_event
8
 from function.helpers import day_event
8
 from function.modules import brightness_choose_n_action, timer_on_activation, heating_function
9
 from function.modules import brightness_choose_n_action, timer_on_activation, heating_function
9
 from function.rooms import room, room_collection
10
 from function.rooms import room, room_collection
157
                                          self.bed_light_ma_powerplug.toggle_output_0_mcb)
158
                                          self.bed_light_ma_powerplug.toggle_output_0_mcb)
158
 
159
 
159
         # heating function
160
         # heating function
160
-        self.heating_function = heating_function(self.heating_valve)
161
+        self.heating_function = heating_function(
162
+            self.heating_valve,
163
+            config.DEFAULT_TEMPERATURE[self.heating_valve.topic],
164
+            **get_radiator_data(self.heating_valve.topic)
165
+        )
166
+        self.heating_function.add_callback(None, None, set_radiator_data, True)
161
 
167
 
162
         #
168
         #
163
         # Virtual Device Interface
169
         # Virtual Device Interface

+ 7
- 1
function/first_floor_west.py View File

4
 
4
 
5
 import config
5
 import config
6
 import devices
6
 import devices
7
+from function.db import get_radiator_data, set_radiator_data
7
 from function.modules import heating_function
8
 from function.modules import heating_function
8
 from function.rooms import room, room_collection
9
 from function.rooms import room, room_collection
9
 from function.videv import videv_switch_brightness, videv_switch_brightness_color_temp, videv_heating
10
 from function.videv import videv_switch_brightness, videv_switch_brightness_color_temp, videv_heating
58
         # Functionality initialisation
59
         # Functionality initialisation
59
         #
60
         #
60
         # heating function
61
         # heating function
61
-        self.heating_function = heating_function(self.heating_valve)
62
+        self.heating_function = heating_function(
63
+            self.heating_valve,
64
+            config.DEFAULT_TEMPERATURE[self.heating_valve.topic],
65
+            **get_radiator_data(self.heating_valve.topic)
66
+        )
67
+        self.heating_function.add_callback(None, None, set_radiator_data, True)
62
 
68
 
63
         #
69
         #
64
         # Virtual Device Interface
70
         # Virtual Device Interface

+ 13
- 2
function/ground_floor_west.py View File

4
 
4
 
5
 import config
5
 import config
6
 import devices
6
 import devices
7
+from function.db import get_radiator_data, set_radiator_data
7
 from function.modules import brightness_choose_n_action, heating_function, switched_light
8
 from function.modules import brightness_choose_n_action, heating_function, switched_light
8
 from function.rooms import room, room_collection
9
 from function.rooms import room, room_collection
9
 from function.videv import videv_switching, videv_switch_brightness_color_temp, videv_heating, videv_multistate, videv_audio_player
10
 from function.videv import videv_switching, videv_switch_brightness_color_temp, videv_heating, videv_multistate, videv_audio_player
69
         # Functionality initialisation
70
         # Functionality initialisation
70
         #
71
         #
71
         # heating function
72
         # heating function
72
-        self.heating_function = heating_function(self.heating_valve)
73
+        self.heating_function = heating_function(
74
+            self.heating_valve,
75
+            config.DEFAULT_TEMPERATURE[self.heating_valve.topic],
76
+            **get_radiator_data(self.heating_valve.topic)
77
+        )
78
+        self.heating_function.add_callback(None, None, set_radiator_data, True)
73
 
79
 
74
         #
80
         #
75
         # Virtual Device Interface
81
         # Virtual Device Interface
150
         self.audio_source = self.AUDIO_SOURCE_PC
156
         self.audio_source = self.AUDIO_SOURCE_PC
151
 
157
 
152
         # heating function
158
         # heating function
153
-        self.heating_function = heating_function(self.heating_valve)
159
+        self.heating_function = heating_function(
160
+            self.heating_valve,
161
+            config.DEFAULT_TEMPERATURE[self.heating_valve.topic],
162
+            **get_radiator_data(self.heating_valve.topic)
163
+        )
164
+        self.heating_function.add_callback(None, None, set_radiator_data, True)
154
 
165
 
155
         #
166
         #
156
         # Virtual Device Interface
167
         # Virtual Device Interface

+ 12
- 16
function/modules.py View File

14
 from base import common_base
14
 from base import common_base
15
 import config
15
 import config
16
 import devices
16
 import devices
17
-from function.db import get_radiator_data, set_radiator_data
18
 from function.helpers import day_state
17
 from function.helpers import day_state
19
 import logging
18
 import logging
20
 import task
19
 import task
153
     AWAY_REDUCTION = 5
152
     AWAY_REDUCTION = 5
154
     SUMMER_TEMPERATURE = 5
153
     SUMMER_TEMPERATURE = 5
155
 
154
 
156
-    def __init__(self, heating_valve):
155
+    def __init__(self, heating_valve, default_temperature, **kwargs):
157
         self.heating_valve = heating_valve
156
         self.heating_valve = heating_valve
158
-        self.default_temperature = config.DEFAULT_TEMPERATURE[heating_valve.topic]
159
-        db_data = get_radiator_data(heating_valve.topic)
157
+        self.default_temperature = default_temperature
158
+        #
160
         super().__init__({
159
         super().__init__({
161
-            self.KEY_USER_TEMPERATURE_SETPOINT: db_data[2] or self.default_temperature,
162
-            self.KEY_TEMPERATURE_SETPOINT: db_data[3] or self.default_temperature,
163
-            self.KEY_TEMPERATURE_CURRENT: None,
164
-            self.KEY_AWAY_MODE: db_data[0] or False,
165
-            self.KEY_SUMMER_MODE: db_data[1] or False,
166
-            self.KEY_START_BOOST: True,
167
-            self.KEY_SET_DEFAULT_TEMPERATURE: False,
168
-            self.KEY_BOOST_TIMER: 0
160
+            self.KEY_USER_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature),
161
+            self.KEY_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature),
162
+            self.KEY_TEMPERATURE_CURRENT: kwargs.get(self.KEY_TEMPERATURE_CURRENT, None),
163
+            self.KEY_AWAY_MODE: kwargs.get(self.KEY_AWAY_MODE, False),
164
+            self.KEY_SUMMER_MODE: kwargs.get(self.KEY_SUMMER_MODE, False),
165
+            self.KEY_START_BOOST: kwargs.get(self.KEY_START_BOOST, True),
166
+            self.KEY_SET_DEFAULT_TEMPERATURE: kwargs.get(self.KEY_SET_DEFAULT_TEMPERATURE, False),
167
+            self.KEY_BOOST_TIMER: kwargs.get(self.KEY_BOOST_TIMER, 0)
169
         })
168
         })
170
         #
169
         #
171
         self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
170
         self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
200
         self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
199
         self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
201
 
200
 
202
     def send_command(self, key, data, block_callback=[]):
201
     def send_command(self, key, data, block_callback=[]):
203
-        rv = super().set(key, data, block_callback)
204
-        set_radiator_data(self.heating_valve.topic, self[self.KEY_AWAY_MODE], self[self.KEY_SUMMER_MODE],
205
-                          self[self.KEY_USER_TEMPERATURE_SETPOINT], self[self.KEY_TEMPERATURE_SETPOINT])
206
-        return rv
202
+        return super().set(key, data, block_callback)
207
 
203
 
208
     def away_mode(self, device, key, value):
204
     def away_mode(self, device, key, value):
209
         if value is True:
205
         if value is True:

+ 1
- 1
smart_brain.py View File

13
 
13
 
14
 VERS_MAJOR = 1
14
 VERS_MAJOR = 1
15
 VERS_MINOR = 2
15
 VERS_MINOR = 2
16
-VERS_PATCH = 5
16
+VERS_PATCH = 6
17
 
17
 
18
 INFO_TOPIC = "__info__"
18
 INFO_TOPIC = "__info__"
19
 INFO_DATA = {
19
 INFO_DATA = {

Loading…
Cancel
Save