Improve/fix Home Assistant MQTT integration
See original GitHub issueCould you please make small addition in your code so Home Assistant (HA) integration via internal HA MQTT Mosquitto broker works properly? The current version of miflora-mqtt-daemon allows only partial integration because of missing information in MQTT data which is sent by miflora-mqtt-daemon resulting in partial plant sensor visibility and HA GUI warnings like “This entity does not have a unique ID, therefore its settings cannot be managed from the UI”…
Goals:
The main goal of this request is to have each Miflora Plant sensor visible under Mosquitto broker as a separate HA Device with its Entities/Sensors (temperature, moisture, conductivity, light intensity, battery). Result:
Another goal is to be able to place miflora sensor into HA “Areas”. This requires the main goal to be implemented (miflora sensor needs to be visible as Device in HA not as separate entities/sensors).
Result:
Explanation: 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.
Tested patch which needs to be merged:
diff --git a/miflora-mqtt-daemon.py b/miflora-mqtt-daemon.py
index 1209779..096c5d3 100755
--- a/miflora-mqtt-daemon.py
+++ b/miflora-mqtt-daemon.py
@@ -307,6 +307,15 @@ elif reporting_mode == 'homeassistant-mqtt':
payload['name'] = "{} {}".format(flora_name, sensor.title())
if 'device_class' in params:
payload['device_class'] = params['device_class']
+ payload['unique_id'] = "%s-%s" % ( flora['mac'].lower().replace(":", ""), sensor, )
+ payload['device'] = {
+ 'identifiers': [ "MiFlora{}".format( flora['mac'].lower().replace(":", "") )],
+ 'connections': [[ "mac", flora['mac'].lower() ]],
+ 'manufacturer': 'Xiaomi',
+ 'name': flora_name,
+ 'model': 'HHCCJCY01',
+ '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 ...')
Thank you!
PS: In case no new Miflora devices are visible in your HA after patching miflora-mqtt-daemon, try restarting HA.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (10 by maintainers)
I believe so. Check this out: https://github.com/home-assistant/core/issues/10220
The
name
field from your PR is used as the entity_id. Why HA installation works with the latest version of this daemonBelieve it or not, that is why even though I removed “/state” initially, I left “/sensor/” in. For #35 I envision “miflora/config/…”
Great, should I create git pull request or attached patch is enough?
I’m running miflora-mqtt-daemon on multiple separate raspberrypi-zero devices which work as btle gateways for nearby miflora sensors. Hass.io is on a separate central device.
I’m also thinking to enhance miflora-mqtt-daemon with passive btle readings by default (each miflora broadcasts everything except battery status multiple times per minute) which will save miflora batteries in long-term use. Active btle pooling only few times per day for battery status. Passive btle Miflora reading, see ESPhome or HA component sensor.mitemp_bt. Miflora-mqtt-daemon would be first to combine passive and active (battery status) reading.
po 23. 3. 2020 v 15:38 odesílatel Thomas Dietrich notifications@github.com napsal:
– Moris Bangoura