Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.py 2.0KB

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