Ver código fonte

Initial commit of miflora mqtt daemon

Jan Willhaus 8 anos atrás
commit
56faebe142
3 arquivos alterados com 76 adições e 0 exclusões
  1. 16 0
      config.ini
  2. 57 0
      mqtt-flora.py
  3. 3 0
      requirements.txt

+ 16 - 0
config.ini

@@ -0,0 +1,16 @@
+[mqtt]
+
+hostname = homeassistant
+port = 1883
+timeout = 60
+topic_prefix = miflora
+
+[miflora]
+
+timeout = 600
+sleep = 120
+
+[sensors]
+
+Feinblatt=C4:7C:8D:61:7E:0B
+Drachenbaum=C4:7C:8D:61:9D:8D

+ 57 - 0
mqtt-flora.py

@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+
+import paho.mqtt.client as mqtt
+import time
+from miflora.miflora_poller import MiFloraPoller, \
+    MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE
+from configparser import ConfigParser
+import json
+
+parameters = [MI_TEMPERATURE,
+              MI_LIGHT,
+              MI_MOISTURE,
+              MI_CONDUCTIVITY]
+
+config = ConfigParser(delimiters=('=', ))
+config.read('config.ini')
+
+sleep_time = config['miflora'].getint('sleep', 60)
+topic_prefix = config['mqtt'].get('topic_prefix', 'miflora')
+miflora_timeout = config['miflora'].getint('timeout', 600)
+
+# The callback for when the client receives a CONNACK response from the server.
+def on_connect(client, userdata, flags, rc):
+    print("Connected with result code "+str(rc))
+
+# Initialize Flora sensors
+
+flores = {}
+for flora in config['sensors'].items():
+    print('Adding', flora[0])
+    flores[flora[0]] = MiFloraPoller(
+        mac=flora[1],
+        cache_timeout=miflora_timeout)
+
+
+client = mqtt.Client()
+client.on_connect = on_connect
+client.connect(config['mqtt'].get('hostname', 'homeassistant'),
+               config['mqtt'].getint('port', 1883),
+               config['mqtt'].getint('timeout', 60))
+client.loop_start()
+
+while True:
+
+    for flora in flores:
+        print('Publishing for', flora)
+
+        data = {}
+        for param in parameters:
+            data[param] = flores.get(flora).parameter_value(param)
+
+        client.publish("{}/{}".format(
+            topic_prefix,
+            flora), json.dumps(data))
+
+    print('Sleeping ...')
+    time.sleep(sleep_time)

+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+miflora==0.1.15
+paho-mqtt==1.2
+wheel==0.24.0