-
Notifications
You must be signed in to change notification settings - Fork 0
/
miflora_tasmota2ha_discovery_messenger.py
177 lines (161 loc) · 6.64 KB
/
miflora_tasmota2ha_discovery_messenger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import sys
import json
import argparse
import re
import os.path
import paho.mqtt.client as mqtt
from collections import OrderedDict
project_name = 'Mi Flora Tasmota 2 Homeassistant Discovery Messenger Tool'
project_url = 'https://github.com/tbail'
# Parse the commandline
parser = argparse.ArgumentParser(description=project_name, epilog='For further details see: ' + project_url)
parser.add_argument('--host', help='set name or ip of mqtt host. Defaults to "localhost"', default='localhost')
parser.add_argument('--port', help='set mqtt port. Defaults to "1883"', type=int, default=1883)
parser.add_argument('--user', help='set mqtt user. Defaults to "None"', default=None)
parser.add_argument('--pwd', help='set mqtt password. Defaults to "None"', default=None)
parser.add_argument('--mac', help='set mac address of the mi flora sensor to discover', default='112233445566')
parse_args = parser.parse_args()
mac = parse_args.mac.lower()
mac_suffix = mac[-6:]
mac_pretty = (':'.join(re.findall('..', mac))).upper()
mqtt_host = parse_args.host
mqtt_port = parse_args.port
mqtt_user = parse_args.user
mqtt_password = parse_args.pwd
print(project_name)
print('Source:', project_url)
print('Connecting to MQTT broker ...')
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(mqtt_user, mqtt_password)
try:
mqtt_client.connect(mqtt_host, port=mqtt_port, keepalive=60)
except:
print(f'MQTT connection error. Please check your settings.')
sys.exit(1)
# Discover temperature
topic = f'homeassistant/sensor/miflora_{mac}/temperature/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_temperature'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} Temperature'
payload['val_tpl'] ='{{value_json.Temperature | is_defined}}'
payload['force_update'] = 'true'
payload['unit_of_meas'] = '°C'
payload['state_class'] = 'measurement'
payload['device_class'] = 'temperature'
payload['expire_after'] = '3600'
payload['dev'] = {
'ids' : [f'miflora_{mac}'],
'mf' : 'Xiaomi',
'name' : f'Mi Flora {mac_pretty}',
'mdl' : 'HHCCJCY01HHCC'}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: Temperature')
# Discover illuminance
topic = f'homeassistant/sensor/miflora_{mac}/illuminance/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_illuminance'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} Illuminance'
payload['val_tpl'] ='{{value_json.Illuminance | is_defined}}'
payload['force_update'] = 'true'
payload['unit_of_meas'] = 'lx'
payload['state_class'] = 'measurement'
payload['device_class'] = 'illuminance'
payload['expire_after'] = '3600'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: Illuminance')
# Discover moisture
topic = f'homeassistant/sensor/miflora_{mac}/moisture/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_moisture'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} Moisture'
payload['val_tpl'] ='{{value_json.Moisture | is_defined}}'
payload['force_update'] = 'true'
payload['unit_of_meas'] = '%'
payload['state_class'] = 'measurement'
payload['device_class'] = 'humidity'
payload['expire_after'] = '3600'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: Moisture')
# Discover fertility
topic = f'homeassistant/sensor/miflora_{mac}/fertility/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_fertility'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} Fertility'
payload['val_tpl'] ='{{value_json.Fertility | is_defined}}'
payload['force_update'] = 'true'
payload['unit_of_meas'] = 'µs/cm'
payload['ic'] = 'mdi:leaf'
payload['state_class'] = 'measurement'
payload['expire_after'] = '3600'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: Fertility')
# Discover battery
topic = f'homeassistant/sensor/miflora_{mac}/battery/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_battery'
payload['ent_cat'] = 'diagnostic'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} Battery'
payload['val_tpl'] ='{{value_json.Battery | is_defined}}'
payload['force_update'] = 'true'
payload['unit_of_meas'] = '%'
payload['device_class'] = 'battery'
payload['expire_after'] = '129600'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: Battery')
# Discover rssi
topic = f'homeassistant/sensor/miflora_{mac}/rssi/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_rssi'
payload['ent_cat'] = 'diagnostic'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} RSSI'
payload['val_tpl'] ='{{value_json.RSSI | is_defined}}'
payload['force_update'] = 'true'
payload['unit_of_meas'] = 'dB'
payload['device_class'] = 'signal_strength'
payload['expire_after'] = '86400'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: RSSI')
# Discover mac
topic = f'homeassistant/sensor/miflora_{mac}/mac/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_mac'
payload['ent_cat'] = 'diagnostic'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} MAC'
payload['val_tpl'] ='{{value_json.mac | is_defined}}'
payload['expire_after'] = '86400'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: MAC Address')
# Discover firmware
topic = f'homeassistant/sensor/miflora_{mac}/firmware/config'
payload = OrderedDict()
payload['~'] = f'tele/tasmota_ble'
payload['uniq_id'] = f'miflora_{mac}_firmware'
payload['ent_cat'] = 'diagnostic'
payload['stat_t'] = f'~/Flora{mac_suffix}'
payload['name'] = f'Mi Flora {mac_pretty} Firmware'
payload['val_tpl'] ='{{value_json.Firmware | is_defined}}'
payload['expire_after'] = '86400'
payload['dev'] = {'ids' : [f'miflora_{mac}']}
mqtt_client.publish(topic, json.dumps(payload), 1, retain=True)
print(f'Published dicovery message: Firmware')
print(f'HA discovery messages for seneor {mac_pretty} send.')