Explorar el Código

Rework store and restore heating function data from and to database

tags/v1.2.6^0
Dirk Alders hace 1 año
padre
commit
882ea0230c
Se han modificado 7 ficheros con 66 adiciones y 31 borrados
  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 Ver fichero

@@ -76,7 +76,9 @@ class videv_base(mqtt_base):
76 76
             # register a callback to listen for data from external device
77 77
             ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
78 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 83
     def __rx_ext_device_data__(self, ext_device, ext_key, data):
82 84
         my_key = self.__display_dict__[(id(ext_device), ext_key)]

+ 23
- 9
function/db.py Ver fichero

@@ -1,15 +1,31 @@
1
+from function.modules import heating_function
1 2
 import os
2 3
 import sqlite3
3 4
 
4 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 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 31
 class __storage__(object):
@@ -25,19 +41,17 @@ class __storage__(object):
25 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 45
         try:
31 46
             with self.conn:
32 47
                 self.c.execute(
33
-                    'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', data)
48
+                    'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', [topic] + target_data)
34 49
         except sqlite3.IntegrityError:
35
-            data = [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint]
36 50
             db_data = self.get_radiator_data(topic)
37
-            if db_data != data:
51
+            if db_data != target_data:
38 52
                 with self.conn:
39 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 56
     def get_radiator_data(self, topic):
43 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 Ver fichero

@@ -4,6 +4,7 @@
4 4
 
5 5
 import config
6 6
 import devices
7
+from function.db import get_radiator_data, set_radiator_data
7 8
 from function.helpers import day_event
8 9
 from function.modules import brightness_choose_n_action, timer_on_activation, heating_function
9 10
 from function.rooms import room, room_collection
@@ -157,7 +158,12 @@ class first_floor_east_sleep(room):
157 158
                                          self.bed_light_ma_powerplug.toggle_output_0_mcb)
158 159
 
159 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 169
         # Virtual Device Interface

+ 7
- 1
function/first_floor_west.py Ver fichero

@@ -4,6 +4,7 @@
4 4
 
5 5
 import config
6 6
 import devices
7
+from function.db import get_radiator_data, set_radiator_data
7 8
 from function.modules import heating_function
8 9
 from function.rooms import room, room_collection
9 10
 from function.videv import videv_switch_brightness, videv_switch_brightness_color_temp, videv_heating
@@ -58,7 +59,12 @@ class first_floor_west_bath(room):
58 59
         # Functionality initialisation
59 60
         #
60 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 70
         # Virtual Device Interface

+ 13
- 2
function/ground_floor_west.py Ver fichero

@@ -4,6 +4,7 @@
4 4
 
5 5
 import config
6 6
 import devices
7
+from function.db import get_radiator_data, set_radiator_data
7 8
 from function.modules import brightness_choose_n_action, heating_function, switched_light
8 9
 from function.rooms import room, room_collection
9 10
 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):
69 70
         # Functionality initialisation
70 71
         #
71 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 81
         # Virtual Device Interface
@@ -150,7 +156,12 @@ class ground_floor_west_dirk(room):
150 156
         self.audio_source = self.AUDIO_SOURCE_PC
151 157
 
152 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 167
         # Virtual Device Interface

+ 12
- 16
function/modules.py Ver fichero

@@ -14,7 +14,6 @@ Targets:
14 14
 from base import common_base
15 15
 import config
16 16
 import devices
17
-from function.db import get_radiator_data, set_radiator_data
18 17
 from function.helpers import day_state
19 18
 import logging
20 19
 import task
@@ -153,19 +152,19 @@ class heating_function(common_base):
153 152
     AWAY_REDUCTION = 5
154 153
     SUMMER_TEMPERATURE = 5
155 154
 
156
-    def __init__(self, heating_valve):
155
+    def __init__(self, heating_valve, default_temperature, **kwargs):
157 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 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 170
         self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
@@ -200,10 +199,7 @@ class heating_function(common_base):
200 199
         self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
201 200
 
202 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 204
     def away_mode(self, device, key, value):
209 205
         if value is True:

+ 1
- 1
smart_brain.py Ver fichero

@@ -13,7 +13,7 @@ logger = logging.getLogger(config.APP_NAME)
13 13
 
14 14
 VERS_MAJOR = 1
15 15
 VERS_MINOR = 2
16
-VERS_PATCH = 5
16
+VERS_PATCH = 6
17 17
 
18 18
 INFO_TOPIC = "__info__"
19 19
 INFO_DATA = {

Loading…
Cancelar
Guardar