Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mode 'mqtt-pure' #169

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Currently supported:
#
# mqtt-json - Publish to an MQTT broker in a proprietary json format (Default)
# mqtt-pure - Nothin fancy, just the bare information
# mqtt-homie - Publish to an MQTT broker following the Homie MQTT convention
# (https://github.com/marvinroger/homie)
# mqtt-smarthome - Publish to an MQTT broker following the mqtt-smarthome proposal
Expand Down Expand Up @@ -53,7 +54,7 @@

# The MQTT base topic to publish all Mi Flora sensor data topics under.
# Default depends on the configured reporting_method
#base_topic = miflora # Default for: mqtt-json, mqtt-smarthome, homeassistant-mqtt
#base_topic = miflora # Default for: mqtt-json, mqtt-pure, mqtt-smarthome, homeassistant-mqtt
#base_topic = homie # Default for: mqtt-homie
#base_topic = gladys/master/device # Default for: gladys-mqtt
#base_topic = v1/devices/me/telemetry # Default for: thingsboard-json
Expand Down
10 changes: 8 additions & 2 deletions miflora-mqtt-daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def on_publish(client, userdata, mid):
miflora_cache_timeout = sleep_period - 1

# Check configuration
if reporting_mode not in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json', 'wirenboard-mqtt']:
if reporting_mode not in ['mqtt-json', 'mqtt-pure', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json', 'wirenboard-mqtt']:
print_line('Configuration parameter reporting_mode set to an invalid value', error=True, sd_notify=True)
sys.exit(1)
if not config['Sensors']:
Expand All @@ -132,7 +132,7 @@ def on_publish(client, userdata, mid):
print_line('Configuration accepted', console=False, sd_notify=True)

# MQTT connection
if reporting_mode in ['mqtt-json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json', 'wirenboard-mqtt']:
if reporting_mode in ['mqtt-json', 'mqtt-pure', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json', 'wirenboard-mqtt']:
print_line('Connecting to MQTT broker ...')
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
Expand Down Expand Up @@ -231,6 +231,7 @@ def on_publish(client, userdata, mid):
mqtt_client.publish('{}/$announce'.format(base_topic), json.dumps(flores_info), retain=True)
sleep(0.5) # some slack for the publish roundtrip and callback function
print()

elif reporting_mode == 'mqtt-homie':
mqtt_client = OrderedDict()
print_line('Announcing Mi Flora devices to MQTT broker for auto-discovery ...')
Expand Down Expand Up @@ -429,6 +430,11 @@ def on_publish(client, userdata, mid):
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 == 'mqtt-pure':
print_line('Publishing data to MQTT base topic "{}/{}"'.format(base_topic, flora_name.lower()))
for [param, value] in data.items():
mqtt_client.publish('{}/{}/{}'.format(base_topic, flora_name.lower(), param), value, 1, True)
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)
Expand Down