|
@@ -5,7 +5,7 @@ import re
|
|
|
import json
|
|
import json
|
|
|
import os.path
|
|
import os.path
|
|
|
import argparse
|
|
import argparse
|
|
|
-from time import sleep, localtime, strftime
|
|
|
|
|
|
|
+from time import time, sleep, localtime, strftime
|
|
|
from collections import OrderedDict
|
|
from collections import OrderedDict
|
|
|
from colorama import init as colorama_init
|
|
from colorama import init as colorama_init
|
|
|
from colorama import Fore, Back, Style
|
|
from colorama import Fore, Back, Style
|
|
@@ -127,7 +127,7 @@ sleep_period = config['Daemon'].getint('period', 300)
|
|
|
miflora_cache_timeout = sleep_period - 1
|
|
miflora_cache_timeout = sleep_period - 1
|
|
|
|
|
|
|
|
# Check configuration
|
|
# Check configuration
|
|
|
-if not reporting_mode in ['mqtt-json', 'mqtt-homie', 'json']:
|
|
|
|
|
|
|
+if not reporting_mode in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome']:
|
|
|
print_line('Configuration parameter reporting_mode set to an invalid value', error=True, sd_notify=True)
|
|
print_line('Configuration parameter reporting_mode set to an invalid value', error=True, sd_notify=True)
|
|
|
sys.exit(1)
|
|
sys.exit(1)
|
|
|
if not config['Sensors']:
|
|
if not config['Sensors']:
|
|
@@ -137,7 +137,7 @@ if not config['Sensors']:
|
|
|
print_line('Configuration accepted', console=False, sd_notify=True)
|
|
print_line('Configuration accepted', console=False, sd_notify=True)
|
|
|
|
|
|
|
|
# MQTT connection
|
|
# MQTT connection
|
|
|
-if reporting_mode in ['mqtt-json', 'mqtt-homie']:
|
|
|
|
|
|
|
+if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome']:
|
|
|
print_line('Connecting to MQTT broker ...')
|
|
print_line('Connecting to MQTT broker ...')
|
|
|
mqtt_client = mqtt.Client()
|
|
mqtt_client = mqtt.Client()
|
|
|
mqtt_client.on_connect = on_connect
|
|
mqtt_client.on_connect = on_connect
|
|
@@ -146,6 +146,9 @@ if reporting_mode in ['mqtt-json', 'mqtt-homie']:
|
|
|
mqtt_client.will_set('{}/$announce'.format(base_topic), payload='{}', retain=True)
|
|
mqtt_client.will_set('{}/$announce'.format(base_topic), payload='{}', retain=True)
|
|
|
elif reporting_mode == 'mqtt-homie':
|
|
elif reporting_mode == 'mqtt-homie':
|
|
|
mqtt_client.will_set('{}/{}/$online'.format(base_topic, device_id), payload='false', retain=True)
|
|
mqtt_client.will_set('{}/{}/$online'.format(base_topic, device_id), payload='false', retain=True)
|
|
|
|
|
+ elif reporting_mode == 'mqtt-smarthome':
|
|
|
|
|
+ mqtt_client.will_set('{}/connected'.format(base_topic), payload='0', retain=True)
|
|
|
|
|
+
|
|
|
if config['MQTT'].get('username'):
|
|
if config['MQTT'].get('username'):
|
|
|
mqtt_client.username_pw_set(config['MQTT'].get('username'), config['MQTT'].get('password', None))
|
|
mqtt_client.username_pw_set(config['MQTT'].get('username'), config['MQTT'].get('password', None))
|
|
|
try:
|
|
try:
|
|
@@ -156,6 +159,8 @@ if reporting_mode in ['mqtt-json', 'mqtt-homie']:
|
|
|
print_line('MQTT connection error. Please check your settings in the configuration file "config.ini"', error=True, sd_notify=True)
|
|
print_line('MQTT connection error. Please check your settings in the configuration file "config.ini"', error=True, sd_notify=True)
|
|
|
sys.exit(1)
|
|
sys.exit(1)
|
|
|
else:
|
|
else:
|
|
|
|
|
+ if reporting_mode == 'mqtt-smarthome':
|
|
|
|
|
+ mqtt_client.publish('{}/connected'.format(base_topic), payload='1', retain=True)
|
|
|
mqtt_client.loop_start()
|
|
mqtt_client.loop_start()
|
|
|
sleep(1.0) # some slack to establish the connection
|
|
sleep(1.0) # some slack to establish the connection
|
|
|
|
|
|
|
@@ -303,6 +308,14 @@ while True:
|
|
|
for [param, value] in data.items():
|
|
for [param, value] in data.items():
|
|
|
mqtt_client.publish('{}/{}/{}/{}'.format(base_topic, device_id, flora_name, param), value, 1, False)
|
|
mqtt_client.publish('{}/{}/{}/{}'.format(base_topic, device_id, flora_name, param), value, 1, False)
|
|
|
sleep(0.5) # some slack for the publish roundtrip and callback function
|
|
sleep(0.5) # some slack for the publish roundtrip and callback function
|
|
|
|
|
+ elif reporting_mode == 'mqtt-smarthome':
|
|
|
|
|
+ for [param, value] in data.items():
|
|
|
|
|
+ print_line('Publishing data to MQTT topic "{}/status/{}/{}"'.format(base_topic, flora_name, param))
|
|
|
|
|
+ payload = dict()
|
|
|
|
|
+ payload['val'] = value
|
|
|
|
|
+ payload['ts'] = int(round(time() * 1000))
|
|
|
|
|
+ mqtt_client.publish('{}/status/{}/{}'.format(base_topic, flora_name, param), json.dumps(payload), retain=True)
|
|
|
|
|
+ sleep(0.5) # some slack for the publish roundtrip and callback function
|
|
|
elif reporting_mode == 'json':
|
|
elif reporting_mode == 'json':
|
|
|
data['timestamp'] = strftime('%Y-%m-%d %H:%M:%S', localtime())
|
|
data['timestamp'] = strftime('%Y-%m-%d %H:%M:%S', localtime())
|
|
|
data['name'] = flora_name
|
|
data['name'] = flora_name
|
|
@@ -325,4 +338,3 @@ while True:
|
|
|
if reporting_mode == 'mqtt-json':
|
|
if reporting_mode == 'mqtt-json':
|
|
|
mqtt_client.disconnect()
|
|
mqtt_client.disconnect()
|
|
|
break
|
|
break
|
|
|
-
|
|
|