Skip to content

Commit

Permalink
Add reporting mode mqtt-smarthome (#7)
Browse files Browse the repository at this point in the history
* reporting_method = mqtt-smarthome

* changes as discussed
  • Loading branch information
hobbyquaker authored and ThomDietrich committed Sep 12, 2017
1 parent c57ae3d commit c275a27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 5 additions & 3 deletions config.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

# The operation mode of the program. Determines wether retrieved sensor data is published via MQTT or stdout/file.
# Currently supported:
# mqtt-json - Publish to an mqtt broker, json encoded (Default)
# mqtt-homie - Publish to an mqtt broker following the Homie MQTT convention (https://github.com/marvinroger/homie)
# json - Print to stdout as json encoded string
# mqtt-json - Publish to an mqtt broker, json encoded (Default)
# 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
# (https://github.com/mqtt-smarthome/mqtt-smarthome)
# json - Print to stdout as json encoded string
#reporting_method = mqtt-json

# The bluetooth adapter that should be used to connect to Mi Flora devices (Default: hci0)
Expand Down
20 changes: 16 additions & 4 deletions miflora-mqtt-daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import os.path
import argparse
from time import sleep, localtime, strftime
from time import time, sleep, localtime, strftime
from collections import OrderedDict
from colorama import init as colorama_init
from colorama import Fore, Back, Style
Expand Down Expand Up @@ -127,7 +127,7 @@ def flores_to_openhab_items(flores, reporting_mode):
miflora_cache_timeout = sleep_period - 1

# 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)
sys.exit(1)
if not config['Sensors']:
Expand All @@ -137,7 +137,7 @@ def flores_to_openhab_items(flores, reporting_mode):
print_line('Configuration accepted', console=False, sd_notify=True)

# 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 ...')
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
Expand All @@ -146,6 +146,9 @@ def flores_to_openhab_items(flores, reporting_mode):
mqtt_client.will_set('{}/$announce'.format(base_topic), payload='{}', retain=True)
elif reporting_mode == 'mqtt-homie':
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'):
mqtt_client.username_pw_set(config['MQTT'].get('username'), config['MQTT'].get('password', None))
try:
Expand All @@ -156,6 +159,8 @@ def flores_to_openhab_items(flores, reporting_mode):
print_line('MQTT connection error. Please check your settings in the configuration file "config.ini"', error=True, sd_notify=True)
sys.exit(1)
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

Expand Down Expand Up @@ -303,6 +308,14 @@ def flores_to_openhab_items(flores, reporting_mode):
for [param, value] in data.items():
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
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':
data['timestamp'] = strftime('%Y-%m-%d %H:%M:%S', localtime())
data['name'] = flora_name
Expand All @@ -325,4 +338,3 @@ def flores_to_openhab_items(flores, reporting_mode):
if reporting_mode == 'mqtt-json':
mqtt_client.disconnect()
break

0 comments on commit c275a27

Please sign in to comment.