Skip to content

Home Assistant MQTT Sensor discovery enhancements, areas support.#102

Merged
ThomDietrich merged 2 commits into
ThomDietrich:masterfrom
bangom:ha-disc-enh
Mar 28, 2020
Merged

Home Assistant MQTT Sensor discovery enhancements, areas support.#102
ThomDietrich merged 2 commits into
ThomDietrich:masterfrom
bangom:ha-disc-enh

Conversation

@bangom
Copy link
Copy Markdown
Contributor

@bangom bangom commented Mar 23, 2020

According to HA MQTT Sensor Discovery (https://www.home-assistant.io/integrations/sensor.mqtt/) miflora-mqtt-daemon should also send sensor's "unique_id" along with it's "parent device" information (identifiers, connections, firmware, ... ) configuration variables.
After that, the discovered MQTT sensor is added automatically into HA Device Registry (https://developers.home-assistant.io/docs/device_registry_index/) and visible in HA GUI. Also, Miflora sensor can be then used with HA Areas.

Whole enhancement is discussed in #101 .

Comment thread miflora-mqtt-daemon.py
Comment thread miflora-mqtt-daemon.py
payload['device_class'] = params['device_class']
payload['unique_id'] = "{}-{}".format(flora['mac'].lower().replace(":", ""), 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

@ThomDietrich
Copy link
Copy Markdown
Owner

In the Home Assistant device view the battery level for smartphones is given. Seems like that is not supported by MQTT Sensor. Do you know more about that?

@bangom
Copy link
Copy Markdown
Contributor Author

bangom commented Mar 25, 2020

In the Home Assistant device view the battery level for smartphones is given. Seems like that is not supported by MQTT Sensor. Do you know more about that?

Can you clarify what you mean? Battery levels on my screenshot ( #101 ) are Miflora battery levels... or do you mean something else?

@ThomDietrich
Copy link
Copy Markdown
Owner

ThomDietrich commented Mar 25, 2020

...on my screenshot...

Aha! Didn't see them. That is pretty cool!
I expected that a device would need to specifically state that/where/how battery state data is provided.

All good. I will test and merge this evening

@ThomDietrich
Copy link
Copy Markdown
Owner

@bangom I tried your PR and with that the HA integration. I have Bluetooth connection issues on my RPI0W and will need to look into this.
Anyhow, I added some cosmetic improvements to your PR. If you have the chance to double check, that would be amazing. I'll merge after.

@ThomDietrich
Copy link
Copy Markdown
Owner

I have Bluetooth connection issues

Turns out that my two years old sensors with firmware version 2.6.2 are not supported by btlewrap 🤷‍♂ Just in case you are interested

@ThomDietrich ThomDietrich merged commit 3541efa into ThomDietrich:master Mar 28, 2020
@bangom
Copy link
Copy Markdown
Contributor Author

bangom commented Mar 29, 2020

@bangom I tried your PR and with that the HA integration. I have Bluetooth connection issues on my RPI0W and will need to look into this.
Anyhow, I added some cosmetic improvements to your PR. If you have the chance to double check, that would be amazing. I'll merge after.

Seems OK to me, tested latest git version with merged pull.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants