From 4e8a616b23df1d1a96d32263021737799cff52b4 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Sun, 2 Jun 2024 22:12:18 +0200 Subject: [PATCH] Continue Flask migration. --- thermostat.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/thermostat.py b/thermostat.py index dbd08f4..459ca04 100644 --- a/thermostat.py +++ b/thermostat.py @@ -16,6 +16,7 @@ import requests import subprocess from threading import Thread import sqlalchemy.exc +from sqlalchemy.dialects.sqlite import insert as sqlite_upsert # This code has been written for # python3 3.11.2-1+b1 @@ -150,7 +151,7 @@ def get_forced_mode(): #row = cur.fetchone() print(row) data = dict(zip(['value', 'timestamp'], row)) - timestamp = datetime.fromisoformat(data['timestamp']).replace(tzinfo=timezone.utc).timestamp() + timestamp = data['timestamp'].replace(tzinfo=timezone.utc).timestamp() # We ignore old targets but never ignore absence modes if data['value'] in targets and time.time() - timestamp > forced_mode_duration: logging.debug("Ignoring old set mode.") @@ -199,8 +200,13 @@ class Set_mode_thermostat(Resource): db.session.commit() return "OK", 201 except sqlalchemy.exc.IntegrityError as e: - logging.error("catched") - return "K0", 400 + try: + cur_mode = db.session.execute(db.select(Set_mode).filter_by(name="mode")).scalar_one() + cur_mode = data + db.session.commit() + except Exception as e: + logging.error(e) + return "K0", 400 except Exception as e: logging.error(e) return "K0", 400