aboutsummaryrefslogtreecommitdiff
path: root/write-influx/write_influx.py
diff options
context:
space:
mode:
Diffstat (limited to 'write-influx/write_influx.py')
-rw-r--r--write-influx/write_influx.py25
1 files changed, 20 insertions, 5 deletions
diff --git a/write-influx/write_influx.py b/write-influx/write_influx.py
index 78e4da5..7337c9d 100644
--- a/write-influx/write_influx.py
+++ b/write-influx/write_influx.py
@@ -9,7 +9,6 @@ from typing import List, Dict
class Topic(dict):
-
def __init__(self):
super().__init__()
self["__value__"] = None
@@ -43,7 +42,14 @@ def on_message(client, userdata, msg):
tags = {}
tags["device_id"] = topics[1]
# case of device attribute
- device_attributes = ["$homie", "$name", "$state", "$nodes", "$extensions", "$implementation"]
+ device_attributes = [
+ "$homie",
+ "$name",
+ "$state",
+ "$nodes",
+ "$extensions",
+ "$implementation",
+ ]
if len(topics) == 3 and topics[2] in device_attributes:
measurement = topics[2]
fields = {"value": payload}
@@ -60,11 +66,21 @@ def on_message(client, userdata, msg):
write_point(measurement, tags, fields)
-influx_client = InfluxDBClient("db", 8086, 'root', 'root', "homie")
+influx_client = InfluxDBClient(host="db",
+ port=8086,
+ username="admin",
+ password="mypassword",
+ database="homie")
influx_client.create_database("homie")
try:
- influx_client.create_retention_policy(name="two-years", database="homie", duration="18000h", default=True, replication=1)
+ influx_client.create_retention_policy(
+ name="two-years",
+ database="homie",
+ duration="18000h",
+ default=True,
+ replication=1,
+ )
except:
pass
@@ -81,7 +97,6 @@ def write_point(measurement, tags, fields):
print(json)
-
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message