aboutsummaryrefslogtreecommitdiffstats
path: root/neohub2pgsql.py
diff options
context:
space:
mode:
Diffstat (limited to 'neohub2pgsql.py')
-rwxr-xr-xneohub2pgsql.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/neohub2pgsql.py b/neohub2pgsql.py
new file mode 100755
index 0000000..0c760e5
--- /dev/null
+++ b/neohub2pgsql.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python3
+
+import os
+import asyncio
+import neohubapi.neohub as neohub
+
+import psycopg2
+
+neohub_ip = os.environ['el_neohub_ip']
+neohub_port = os.environ['el_neohub_port']
+pg_db = os.environ['el_pg_db']
+pg_host = os.environ['el_pg_host']
+pg_user = os.environ['el_pg_user']
+pg_pass = os.environ['el_pg_pass']
+pg_table = "neohub"
+
+values = []
+
+async def run():
+ # Legacy connection
+ hub = neohub.NeoHub(neohub_ip, int(neohub_port))
+ # Or, for a websocket connection:
+ # hub = neohub.Neohub(port=4243, token='xxx-xxxxxxx')
+ system = await hub.get_system()
+ hub_data, devices = await hub.get_live_data()
+ for device in devices['thermostats']:
+ print(f"Temperature in zone {device.name}: {device}")
+ values.append((device.time, device.device_id, device.away, device.heat_mode, device.heat_on, device.current_floor_temperature, device.target_temperature, device.temperature))
+
+asyncio.run(run())
+
+conn = psycopg2.connect(database=pg_db, host=pg_host, user=pg_user, password=pg_pass)
+cur = conn.cursor()
+
+try:
+ cur.executemany("INSERT INTO " + pg_table + " (time, device_id, away, heat_mode, heat_on, current_floor_temperature, target_temperature, temperature) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)", values)
+ conn.commit()
+except Exception as e:
+ conn.rollback()
+ raise e