|
|
@@ -8,12 +8,16 @@ from time import sleep, localtime, strftime
|
|
|
from colorama import init as colorama_init
|
|
|
from colorama import Fore, Back, Style
|
|
|
from configparser import ConfigParser
|
|
|
-import unicodedata
|
|
|
+from unidecode import unidecode
|
|
|
from miflora.miflora_poller import MiFloraPoller, MI_BATTERY, MI_CONDUCTIVITY, MI_LIGHT, MI_MOISTURE, MI_TEMPERATURE
|
|
|
import paho.mqtt.client as mqtt
|
|
|
import sdnotify
|
|
|
|
|
|
-parameters = [MI_BATTERY, MI_CONDUCTIVITY, MI_LIGHT, MI_MOISTURE, MI_TEMPERATURE]
|
|
|
+parameters = {MI_BATTERY: dict(pretty='Sensor Battery Level', typeformat='%d', unit='%'),
|
|
|
+ MI_CONDUCTIVITY: dict(pretty='Soil Conductivity/Fertility', typeformat='%d', unit='µS/cm'),
|
|
|
+ MI_LIGHT: dict(pretty='Sunlight Intensity', typeformat='%d', unit='lux'),
|
|
|
+ MI_MOISTURE: dict(pretty='Soil Moisture', typeformat='%d', unit='%'),
|
|
|
+ MI_TEMPERATURE: dict(pretty='Air Temperature', typeformat='%.1f', unit='°C')}
|
|
|
|
|
|
# Intro
|
|
|
colorama_init()
|
|
|
@@ -28,7 +32,6 @@ if False:
|
|
|
# Systemd Service Notifications - https://github.com/bb4242/sdnotify
|
|
|
sd_notifier = sdnotify.SystemdNotifier()
|
|
|
|
|
|
-
|
|
|
def print_line(text, error = False, warning=False, sd_notify=False, console=True):
|
|
|
timestamp = strftime('%Y-%m-%d %H:%M:%S', localtime())
|
|
|
if console:
|
|
|
@@ -58,19 +61,31 @@ def on_publish(client, userdata, mid):
|
|
|
pass
|
|
|
|
|
|
|
|
|
-def flores_to_openhab_items(flores):
|
|
|
+def flores_to_openhab_items(flores, reporting_mode):
|
|
|
items = list()
|
|
|
- items.append('// Generated by miflora-mqtt-daemon. Adapt to your needs!\n\n// Mi Flora specific groups')
|
|
|
- for param in parameters:
|
|
|
- items.append('Group g{} "Mi Flora{} elements" (gAll)'.format(param.capitalize(), param.capitalize()))
|
|
|
- for [flora_name, flora] in flores.items():
|
|
|
- items.append('\n// Mi Flora "{}" ({})'.format(flora['pretty'], flora['mac']))
|
|
|
- for param in parameters:
|
|
|
- basic = 'Number {}_{}'.format(flora['location'], flora_name.capitalize())
|
|
|
- parameter = '"{} {} {} [%.0f]" <text> (g{}, g{})'.format(flora['location'], flora['pretty'], param.capitalize(), flora['location'], param.capitalize())
|
|
|
- channel = '{{mqtt="<[broker:{}/{}:state:JSONPATH($.{})]"}}'.format(base_topic, flora_name, param)
|
|
|
- items.append(' '.join([basic, parameter, channel]))
|
|
|
- print('\n'.join(items))
|
|
|
+ items.append('// miflora.items - Generated by miflora-mqtt-daemon.')
|
|
|
+ items.append('// Adapt to your needs! Things you probably want to modify:')
|
|
|
+ items.append('// Room group names, icons,')
|
|
|
+ items.append('// "gAll", "broker", "UnknownRoom"')
|
|
|
+ items.append('')
|
|
|
+ items.append('// Mi Flora specific groups')
|
|
|
+ for param, param_properties in parameters.items():
|
|
|
+ items.append('Group g{} "Mi Flora {} elements" (gAll)'.format(param.capitalize(), param_properties['pretty']))
|
|
|
+ if reporting_mode == 'mqtt-json':
|
|
|
+ for [flora_name, flora] in flores.items():
|
|
|
+ location = flora['location'] if flora['location'] else 'UnknownRoom'
|
|
|
+ items.append('\n// Mi Flora "{}" ({})'.format(flora['pretty'], flora['mac']))
|
|
|
+ for [param, param_properties] in parameters.items():
|
|
|
+ basic = 'Number {}_{}_{}'.format(location, flora_name.capitalize(), param.capitalize())
|
|
|
+ label = '"{} {} {} [{} {}]"'.format(location, flora['pretty'], param_properties['pretty'], param_properties['typeformat'], param_properties['unit'].replace('%', '%%'))
|
|
|
+ details = '<text> (g{}, g{})'.format(location, param.capitalize())
|
|
|
+ channel = '{{mqtt="<[broker:{}/{}:state:JSONPATH($.{})]"}}'.format(base_topic, flora_name, param)
|
|
|
+ items.append(' '.join([basic, label, details, channel]))
|
|
|
+ items.append('')
|
|
|
+ print('\n'.join(items))
|
|
|
+ #elif reporting_mode == 'mqtt-homie':
|
|
|
+ else:
|
|
|
+ print('Sorry, given reporting_mode not supported for the export to openHAB items')
|
|
|
|
|
|
|
|
|
# Load configuration file
|
|
|
@@ -133,7 +148,10 @@ for [name, mac] in config['Sensors'].items():
|
|
|
name, location = name.split("@")
|
|
|
location = location.replace(' ', '-')
|
|
|
name_pretty = name
|
|
|
- name_clean = unicodedata.normalize('NFKD', name.lower()).replace(' ', '-')
|
|
|
+ name_clean = name.lower()
|
|
|
+ for o, n in [[' ', '-'], ['ä', 'ae'], ['ö', 'oe'], ['ü', 'ue'], ['ß', 'ss']]:
|
|
|
+ name_clean = name_clean.replace(o, n)
|
|
|
+ name_clean = unidecode(name_clean)
|
|
|
|
|
|
|
|
|
flora = dict()
|
|
|
@@ -215,7 +233,7 @@ elif reporting_mode == 'mqtt-homie':
|
|
|
|
|
|
print_line('Initialization complete, starting MQTT publish loop', console=False, sd_notify=True)
|
|
|
|
|
|
-#flores_to_openhab_items(flores)
|
|
|
+#flores_to_openhab_items(flores, reporting_mode)
|
|
|
|
|
|
# Sensor data retrieval and publication
|
|
|
while True:
|
|
|
@@ -237,7 +255,7 @@ while True:
|
|
|
print_line('Failed to retrieve data from Mi Flora sensor "{}" ({})'.format(flora['pretty'], flora['mac']), error = True, sd_notify = True)
|
|
|
continue
|
|
|
|
|
|
- for param in parameters:
|
|
|
+ for param,_ in parameters.items():
|
|
|
data[param] = flora['poller'].parameter_value(param)
|
|
|
print_line('Result: {}'.format(json.dumps(data)))
|
|
|
|