aboutsummaryrefslogtreecommitdiffstats
path: root/ticker_tibber.py
diff options
context:
space:
mode:
Diffstat (limited to 'ticker_tibber.py')
-rw-r--r--ticker_tibber.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/ticker_tibber.py b/ticker_tibber.py
new file mode 100644
index 0000000..166a1e1
--- /dev/null
+++ b/ticker_tibber.py
@@ -0,0 +1,71 @@
+"""
+Stream Data from Tibber Pulse API.
+
+Cf. https://github.com/Danielhiversen/pyTibber
+"""
+
+import asyncio
+import aiohttp
+import tibber
+# import pprint
+import time
+import Common.influx as influx
+
+
+async def _callback(pkg):
+ data = pkg.get("data")
+ if data is None:
+ return
+
+ measurements = data.get("liveMeasurement")
+
+ # print(type(measurements))
+ # print(measurements.keys())
+
+ # dump some data
+ # print(int(time.time()))
+ # # pprint.pprint(data.get("liveMeasurement"))
+ # pprint.pprint(measurements)
+
+ # collect some data
+ epoch = int(time.time())
+ lines = []
+ # power
+ line = "pulse_power value=%ii %i" % (measurements['power'], epoch)
+ lines.append(line)
+ # voltages per phase
+ for kk in [ 1, 2, 3 ]:
+ key = "voltagePhase%i" % kk
+ if not measurements[key] == None:
+ line = "pulse_%s value=%.1f %i" % \
+ (key, measurements[key], epoch)
+ lines.append(line)
+ # signal strength
+ if not measurements['signalStrength'] == None:
+ line = "pulse_signalStrength value=%ii %i" % \
+ (measurements['signalStrength'], epoch)
+ lines.append(line)
+
+ # send to database
+ post_data = "\n".join(lines)
+ # print(post_data)
+ influx.post_data(post_data)
+
+
+async def run():
+ async with aiohttp.ClientSession() as session:
+ tibber_connection = tibber.Tibber(ACCESS_TOKEN, websession=session)
+ await tibber_connection.update_info()
+ home = tibber_connection.get_homes()[0]
+ await home.rt_subscribe(_callback)
+
+
+if __name__ == '__main__':
+ # ACCESS_TOKEN = tibber.DEMO_TOKEN
+ with open('userpass_tibber', 'r') as f:
+ line = f.readline()
+ line = line.strip()
+ ACCESS_TOKEN = line
+ loop = asyncio.get_event_loop()
+ asyncio.ensure_future(run())
+ loop.run_forever()