|
|
@@ -131,6 +131,8 @@ if reporting_mode == 'mqtt-homie':
|
|
|
default_base_topic = 'homie'
|
|
|
elif reporting_mode == 'homeassistant-mqtt':
|
|
|
default_base_topic = 'homeassistant'
|
|
|
+elif reporting_mode == 'thingsboard-json':
|
|
|
+ default_base_topic = 'v1/devices/me/telemetry'
|
|
|
else:
|
|
|
default_base_topic = 'miflora'
|
|
|
|
|
|
@@ -140,7 +142,7 @@ sleep_period = config['Daemon'].getint('period', 300)
|
|
|
miflora_cache_timeout = sleep_period - 1
|
|
|
|
|
|
# Check configuration
|
|
|
-if reporting_mode not in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt']:
|
|
|
+if reporting_mode not in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json']:
|
|
|
print_line('Configuration parameter reporting_mode set to an invalid value', error=True, sd_notify=True)
|
|
|
sys.exit(1)
|
|
|
if not config['Sensors']:
|
|
|
@@ -150,7 +152,7 @@ if not config['Sensors']:
|
|
|
print_line('Configuration accepted', console=False, sd_notify=True)
|
|
|
|
|
|
# MQTT connection
|
|
|
-if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome', 'homeassistant-mqtt']:
|
|
|
+if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json']:
|
|
|
print_line('Connecting to MQTT broker ...')
|
|
|
mqtt_client = mqtt.Client()
|
|
|
mqtt_client.on_connect = on_connect
|
|
|
@@ -185,8 +187,9 @@ if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome', 'homeassistan
|
|
|
else:
|
|
|
if reporting_mode == 'mqtt-smarthome':
|
|
|
mqtt_client.publish('{}/connected'.format(base_topic), payload='1', retain=True)
|
|
|
- mqtt_client.loop_start()
|
|
|
- sleep(1.0) # some slack to establish the connection
|
|
|
+ if reporting_mode != 'thingsboard-json':
|
|
|
+ mqtt_client.loop_start()
|
|
|
+ sleep(1.0) # some slack to establish the connection
|
|
|
|
|
|
sd_notifier.notify('READY=1')
|
|
|
|
|
|
@@ -337,11 +340,18 @@ while True:
|
|
|
for param,_ in parameters.items():
|
|
|
data[param] = flora['poller'].parameter_value(param)
|
|
|
print_line('Result: {}'.format(json.dumps(data)))
|
|
|
-
|
|
|
+
|
|
|
if reporting_mode == 'mqtt-json':
|
|
|
print_line('Publishing to MQTT topic "{}/{}"'.format(base_topic, flora_name))
|
|
|
mqtt_client.publish('{}/{}'.format(base_topic, flora_name), json.dumps(data))
|
|
|
sleep(0.5) # some slack for the publish roundtrip and callback function
|
|
|
+ elif reporting_mode == 'thingsboard-json':
|
|
|
+ print_line('Publishing to MQTT topic "{}" username "{}"'.format(base_topic, flora_name))
|
|
|
+ mqtt_client.username_pw_set(flora_name)
|
|
|
+ mqtt_client.reconnect()
|
|
|
+ sleep(1.0)
|
|
|
+ mqtt_client.publish('{}'.format(base_topic), json.dumps(data))
|
|
|
+ sleep(0.5) # some slack for the publish roundtrip and callback function
|
|
|
elif reporting_mode == 'homeassistant-mqtt':
|
|
|
print_line('Publishing to MQTT topic "{}/sensor/{}/state"'.format(base_topic, flora_name).lower())
|
|
|
mqtt_client.publish('{}/sensor/{}/state'.format(base_topic, flora_name).lower(), json.dumps(data))
|