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