Skip to content
Merged
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
21 changes: 14 additions & 7 deletions miflora-mqtt-daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,23 @@ def flores_to_openhab_items(flores, reporting_mode):
print_line('Announcing Mi Flora devices to MQTT broker for auto-discovery ...')
for [flora_name, flora] in flores.items():
topic_path = '{}/sensor/{}'.format(base_topic, flora_name)
base_payload = {
"state_topic": "{}/state".format(topic_path).lower()
}
for sensor, params in parameters.items():
payload = dict(base_payload.items())
payload['unit_of_measurement'] = params['unit']
payload['value_template'] = "{{ value_json.%s }}" % (sensor, )
for [sensor, params] in parameters.items():
payload = OrderedDict()
payload['name'] = "{} {}".format(flora_name, sensor.title())
payload['unique_id'] = "{}-{}".format(flora['mac'].lower().replace(":", ""), sensor)
payload['unit_of_measurement'] = params['unit']
if 'device_class' in params:
payload['device_class'] = params['device_class']
payload['state_topic'] = "{}/state".format(topic_path).lower()
payload['value_template'] = "{{{{ value_json.{} }}}}".format(sensor)
payload['device'] = {
'identifiers' : ["MiFlora{}".format(flora['mac'].lower().replace(":", ""))],
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what is this used for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary info for HA Device registry so Miflora Device is created upon HA MQTT discovery with its device connection (BTLE MAC address) and then all sensors like temperature, lux, moisture etc. are linked with this new device and identified with by theirs unique_id (btle_mac-temperature, btle_mac-light, ....). Details: https://www.home-assistant.io/integrations/sensor.mqtt/. There is also ha-core github discussion with mqtt json example: home-assistant/core#16943 (comment).

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Of course I'm aware of the device discovery feature :) I was asking about the identifiers specifically. Documentation says

A list of IDs that uniquely identify the device. For example a serial number.

But would you by any chance know what it is actually used for? I wonder whether or not more than one element per sensor would make sense... just curious :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are talking about unique_id: it is necessary to be unique for each entity(=sensor) - probably some unique index in device registry db/table. Each entity with unique_id must be then linked by 'device' section with its physical device (control unit with multiple sensors). If you look into new miflora the MQTT topic, then each sensor (temperature, lux, moisture, conductivity, battery) has different unique_id, but same parent device. That is how relationship is defined and HA know the connections.

Details:
https://developers.home-assistant.io/docs/device_registry_index/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding multiple Identifiers - don't know why there should be more than one unique identifiers... can't think of some use case when you would need more identifiers. But I'm not a HA guru... :)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope me neither. Maybe something for a later point. Feels like this whole concept of devices becomes more and more important in HA

'connections' : [["mac", flora['mac'].lower()]],
'manufacturer' : 'Xiaomi',
'name' : flora_name,
'model' : 'HHCCJCY01',
Comment thread
ThomDietrich marked this conversation as resolved.
'sw_version': flora['firmware']
}
mqtt_client.publish('{}/{}_{}/config'.format(topic_path, flora_name, sensor).lower(), json.dumps(payload), 1, True)
elif reporting_mode == 'wirenboard-mqtt':
print_line('Announcing Mi Flora devices to MQTT broker for auto-discovery ...')
Expand Down