From 56faebe142cb86b7ed7e82d3a25c6bd009f23e4d Mon Sep 17 00:00:00 2001 From: Jan Willhaus Date: Thu, 9 Feb 2017 21:15:25 +0100 Subject: [PATCH] Initial commit of miflora mqtt daemon --- config.ini | 16 ++++++++++++++ mqtt-flora.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 3 files changed, 76 insertions(+) create mode 100644 config.ini create mode 100755 mqtt-flora.py create mode 100644 requirements.txt diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..74b28e1 --- /dev/null +++ b/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 diff --git a/mqtt-flora.py b/mqtt-flora.py new file mode 100755 index 0000000..53eb403 --- /dev/null +++ b/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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7aab02c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +miflora==0.1.15 +paho-mqtt==1.2 +wheel==0.24.0