Browse Source

first testable db implementation for storing radiator gui values

tags/v1.0.0
Dirk Alders 1 year ago
parent
commit
a3e4b8839c
3 changed files with 85 additions and 22 deletions
  1. 1
    0
      .gitignore
  2. 51
    0
      function/db.py
  3. 33
    22
      function/modules.py

+ 1
- 0
.gitignore View File

1
 config.py
1
 config.py
2
+database.db
2
 
3
 
3
 # ---> Linux
4
 # ---> Linux
4
 *~
5
 *~

+ 51
- 0
function/db.py View File

1
+import sqlite3
2
+
3
+
4
+def get_gui_radiator_data(topic):
5
+    return __storage__().get_gui_radiator_data(topic)
6
+
7
+
8
+def set_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint):
9
+    return __storage__().store_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint)
10
+
11
+
12
+class __storage__(object):
13
+    def __init__(self):
14
+        self.conn = sqlite3.connect("database.db")
15
+        self.c = self.conn.cursor()
16
+        with self.conn:
17
+            self.c.execute("""CREATE TABLE IF NOT EXISTS gui_radiator (
18
+                    topic text PRIMARY KEY,
19
+                    away_mode integer,
20
+                    summer_mode integer,
21
+                    user_temperatur_setpoint real
22
+                )""")
23
+
24
+    def store_gui_radiator_data(self, topic, away_mode, summer_mode, user_temperatur_setpoint):
25
+        data = [topic, away_mode, summer_mode, user_temperatur_setpoint]
26
+        try:
27
+            with self.conn:
28
+                self.c.execute(
29
+                    'INSERT INTO gui_radiator VALUES (?, ?, ?, ?)', data)
30
+        except sqlite3.IntegrityError:
31
+            data = [away_mode, summer_mode, user_temperatur_setpoint]
32
+            db_data = self.get_gui_radiator_data(topic)
33
+            if db_data != data:
34
+                with self.conn:
35
+                    self.c.execute(
36
+                        'UPDATE gui_radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ? WHERE topic = ?', data + [topic])
37
+
38
+    def get_gui_radiator_data(self, topic):
39
+        """ returns a list [away_mode, summer_mode, user_temperatur_setpoint] or [None, None, None]"""
40
+        self.c.execute("SELECT * FROM gui_radiator WHERE topic=?", (topic, ))
41
+        data = self.c.fetchone()
42
+        if data is not None:
43
+            data = list(data)
44
+            data[1] = data[1] == 1
45
+            data[2] = data[2] == 1
46
+            return data[1:]
47
+        else:
48
+            return [None, None, None]
49
+
50
+    def __del__(self):
51
+        self.conn.close()

+ 33
- 22
function/modules.py View File

4
 
4
 
5
 import config
5
 import config
6
 import devices
6
 import devices
7
+from function.db import get_gui_radiator_data, set_gui_radiator_data
7
 from function.rooms import room_shelly
8
 from function.rooms import room_shelly
8
 import logging
9
 import logging
9
 import task
10
 import task
140
 
141
 
141
     def __init__(self, mqtt_client, topic_valve, topic_gui, default_temperature):
142
     def __init__(self, mqtt_client, topic_valve, topic_gui, default_temperature):
142
         self.default_temperature = default_temperature
143
         self.default_temperature = default_temperature
143
-        self.regular_temp_setpoint = self.default_temperature
144
-        self.__away_mode__ = False
145
-        self.__summer_mode__ = False
146
-        #
147
-        self.ct = task.periodic(1, self.cyclic_task)
148
-        #
144
+        self.boost_timer = None
145
+        # device initialisation
149
         self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, topic_valve)
146
         self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, topic_valve)
150
         self.gui_heating = devices.nodered_gui_radiator(mqtt_client, topic_gui)
147
         self.gui_heating = devices.nodered_gui_radiator(mqtt_client, topic_gui)
151
-        #
152
-        self.heating_valve.set_heating_setpoint(self.default_temperature)
148
+        # db-stored data initialisation
149
+        db_data = get_gui_radiator_data(topic_gui)
150
+        self.__away_mode__ = db_data[0] or False
151
+        self.__summer_mode__ = db_data[1] or False
152
+        self.__user_temperature_setpoint__ = db_data[2] or default_temperature
153
+        if self.__away_mode__:
154
+            self.away_mode(None, None, True)
155
+        elif self.__summer_mode__:
156
+            self.summer_mode(None, None, True)
157
+        else:
158
+            self.set_heating_setpoint(None, None, self.__user_temperature_setpoint__)
159
+        # callback initialisation
153
         self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_TEMPERATURE, None, self.gui_heating.set_temperature_mcb)
160
         self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_TEMPERATURE, None, self.gui_heating.set_temperature_mcb)
154
         self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
161
         self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
155
         #
162
         #
158
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TO_DEFAULT, None, self.setpoint_to_default)
165
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TO_DEFAULT, None, self.setpoint_to_default)
159
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_AWAY, None, self.away_mode)
166
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_AWAY, None, self.away_mode)
160
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SUMMER, None, self.summer_mode)
167
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SUMMER, None, self.summer_mode)
161
-        #
162
-        self.boost_timer = None
163
-        #
168
+        # cyclic task initialisation
169
+        self.ct = task.periodic(1, self.cyclic_task)
164
         self.ct.run()
170
         self.ct.run()
165
 
171
 
166
     def cyclic_task(self, rt):
172
     def cyclic_task(self, rt):
170
             self.boost_timer -= self.ct.cycle_time
176
             self.boost_timer -= self.ct.cycle_time
171
             if self.boost_timer <= 0:
177
             if self.boost_timer <= 0:
172
                 self.cancel_boost()
178
                 self.cancel_boost()
173
-                self.heating_valve.set_heating_setpoint(self.regular_temp_setpoint)
179
+                self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
180
+                self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.', self.__user_temperature_setpoint__)
174
 
181
 
175
     def cancel_boost(self, device=None, key=None, data=None):
182
     def cancel_boost(self, device=None, key=None, data=None):
176
         if self.boost_timer is not None:
183
         if self.boost_timer is not None:
177
-            self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.', self.regular_temp_setpoint)
178
             self.boost_timer = None
184
             self.boost_timer = None
179
             self.gui_heating.set_timer('-')
185
             self.gui_heating.set_timer('-')
180
 
186
 
181
-    def update_states(self, away_mode=None, summer_mode=None):
187
+    def update_states(self, away_mode=None, summer_mode=None, user_temperature_setpoint=None):
182
         if away_mode is not None:
188
         if away_mode is not None:
183
             self.__away_mode__ = away_mode
189
             self.__away_mode__ = away_mode
184
         if summer_mode is not None:
190
         if summer_mode is not None:
185
             self.__summer_mode__ = summer_mode
191
             self.__summer_mode__ = summer_mode
192
+        if user_temperature_setpoint is not None:
193
+            self.__user_temperature_setpoint__ = user_temperature_setpoint
194
+        set_gui_radiator_data(self.gui_heating.topic, self.__away_mode__, self.__summer_mode__, self.__user_temperature_setpoint__)
195
+        #
186
         self.gui_heating.set_away(self.__away_mode__)
196
         self.gui_heating.set_away(self.__away_mode__)
187
         self.gui_heating.set_summer(self.__summer_mode__)
197
         self.gui_heating.set_summer(self.__summer_mode__)
188
         self.gui_heating.set_enable(not self.__away_mode__ and not self.__summer_mode__)
198
         self.gui_heating.set_enable(not self.__away_mode__ and not self.__summer_mode__)
191
         if value is True:
201
         if value is True:
192
             self.cancel_boost()
202
             self.cancel_boost()
193
             self.update_states(away_mode=value, summer_mode=False)
203
             self.update_states(away_mode=value, summer_mode=False)
194
-            self.heating_valve.set_heating_setpoint(self.default_temperature - self.AWAY_REDUCTION)
204
+            self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__ - self.AWAY_REDUCTION)
195
         else:
205
         else:
196
             self.update_states(away_mode=value)
206
             self.update_states(away_mode=value)
197
-            self.heating_valve.set_heating_setpoint(self.default_temperature)
207
+            self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
198
 
208
 
199
     def summer_mode(self, device, key, value):
209
     def summer_mode(self, device, key, value):
200
         if value is True:
210
         if value is True:
203
             self.heating_valve.set_heating_setpoint(self.SUMMER_TEMPERATURE)
213
             self.heating_valve.set_heating_setpoint(self.SUMMER_TEMPERATURE)
204
         else:
214
         else:
205
             self.update_states(summer_mode=value)
215
             self.update_states(summer_mode=value)
206
-            self.heating_valve.set_heating_setpoint(self.default_temperature)
216
+            self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
207
 
217
 
208
     def boost(self, device, key, data):
218
     def boost(self, device, key, data):
209
         if self.boost_timer is None:
219
         if self.boost_timer is None:
220
         self.set_heating_setpoint(device, key, self.default_temperature)
230
         self.set_heating_setpoint(device, key, self.default_temperature)
221
 
231
 
222
     def set_heating_setpoint(self, device, key, data):
232
     def set_heating_setpoint(self, device, key, data):
223
-        self.heating_valve.set_heating_setpoint(data)
224
         self.cancel_boost()
233
         self.cancel_boost()
225
-        self.update_states(away_mode=False, summer_mode=False)
234
+        self.update_states(away_mode=False, summer_mode=False, user_temperature_setpoint=data)
235
+        self.heating_valve.set_heating_setpoint(data)
226
 
236
 
227
     def get_radiator_setpoint(self, device, key, data):
237
     def get_radiator_setpoint(self, device, key, data):
228
-        self.gui_heating.set_setpoint_temperature(data)
229
-        self.update_states()
230
         if self.boost_timer is None and not self.__away_mode__ and not self.__summer_mode__:
238
         if self.boost_timer is None and not self.__away_mode__ and not self.__summer_mode__:
231
-            self.regular_temp_setpoint = data
239
+            self.update_states(user_temperature_setpoint=data)
240
+        else:
241
+            self.update_states()
242
+        self.gui_heating.set_setpoint_temperature(data)

Loading…
Cancel
Save