-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow MQTT device based auto discovery #109030
Conversation
Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
What would a sample payload look like? |
Have a look at the tests. I'll update the PR docs btw. |
Example added in PR documentation |
@Koenkk can you comment if this would help in your opinion? |
I haven't looked at the code yet because I'm on vacation, so I have a few questions...
I assume the old way of configuring will remain? Removing it (even in a few years) would be a massive breaking change because a lot of different integrations/devices use MQTT discovery. |
Yes
Yes, but in the current implementation the central config overrides the individual settings (maybe I should change this for availability)
The device entry en entity id's should not change, but it would be good to add a test that proves how a migration is possible,
Yes, they can be used together, it is not a breaking change |
@jbouwh It would be nice if a common command and state topic could be specified (device-wide), and overridden on a per-entity basis. In Z2M, the command and state topic is identical for every entity on that device, there is no reason to specify a per-entity state/command topic. The code is already setup to extract a specific json attribute from the state MQTT message. So being able to specify a default state/command topic used by all entities (unless overridden) will reduce the size of this message drastically. Not sure if message size matters, but presumably smaller messages are quicker to transmit/process. |
And entity based override would make sense for In the current code the central config overrides the entity specific config, but can be changed easily. I want to limit the amount of supported parameters in the device context for this PR. Edit: Added |
Thanks @jbouwh Tomorrow I'll have access to my dev env, so I will play with Shellies Discovery and this PR over the next few days and come back to you. |
I was more thinking a top level section on this JSON (ie. same level as 'dev', 'cmp', 'o') of:
Then the code simply does the check if cmp.entity.state_topic is defined, use it, otherwise use def.state_topic. Then you don't need to repeat the state topic for every entity in the JSON. It's a small optimization, not a hill I'll die on, but one that will cut down the size of the message. Note: I'm thinking of my devices that produce 80+ entities ... so you're reducing 80+ duplicate declarations of the state/command topic. |
Ah, okay. At the moment the shared options are at root level of the |
This would be really useful for z2m indeed, currently z2m subscribes to Home Assistant discovery topic on startup which generate a huge amount of messages and degrades performance because of this (Koenkk/zigbee2mqtt#20648) |
After all, it seems it makes sense to have some more shared options added, like |
I think for Z2M it would also be beneficial if the
|
With the current PR implementation you can use the |
Sorry, I missed the part below the fold (after the example payload). All good! |
Looks good. Running one last profile now. |
@jbouwh Asked for a bit of a summary of the performance changes here. Sadly I has already deleted the screenshots, but the after profile screenshot is here: #109030 (comment) The baseline was 10000 packets, with device discovery it 6250 packet reads so a 37.5% reduction in I/O. Processing less I/O reduced the paho client run time by ~18%. The grouped discovery reduced the run time in the HA client by ~12%. |
Also that reminds me we have some useless threading locks we can get rid of since everything is running in the main thread now. |
Nice |
@bdraco, @jbouwh this PR adds a complete new schema for discovering MQTT entities, and we will have to support both schemas indefinitely. A reduction of 12% is not nearly enough to motivate that IMO. Unless I misunderstand the message above, I think we should revert this PR before we release HA Core 2024.6. |
Not sure it really moves the needle (or is much different), but the overall combined runtime reduction between the paho and ha client code is little bit more than that at @ ~16% The performance improvement in HA wasn't what pushed this over the top for me though. During testing we kept having the MQTT client drop messages because it would overwhelm the queue. While we were able to increased the maximum number of queued messages for the addon, thats only solves it for a subset of users. Without a path to reducing the number of messages (discovery or otherwise), MQTT isn't going to be reliable at high volume since HA will not be able to keep up with the broker. Everything fell apart at ~1900 entities before (1960 was the magic number on my system that was always reliable). Its also reasonable to say that we will only support a maximum of ~1800 entities and anything beyond that is not supported. |
We call this 100000s of times if there are many subscriptions home-assistant#109030 (comment)
#118757 Is the replacement |
Revert "Allow MQTT device based auto discovery (home-assistant#109030)" This reverts commit 585892f.
Proposed change
The current MQTT discovery model only allows to setup per component, if a device has multiple entities to published, the device context, availability and origin information needs to be duplicated.
This PR reduces IO overhead in the paho client on processing discovery packages: The baseline was 10000 packets, with device discovery it 6250 packet reads so a 37.5% reduction in I/O. Processing less I/O reduced the paho client run time by ~18%. The grouped discovery reduced the run time in the HA client by ~12%.
This PR adds a way to discover all components for a device with one discovery. The device, availability and origin mapping is only submitted once. Also the following options are allowed in the shared context:
state_topic
command_topic
encoding
qos
Except for
device
andorigin
options, supported shared options can be overridden in the component config.Discovery updates and removal is fully supported.
The device based discovery coexists with the component based discovery, for 18 months. We start sending deprecation warnings after 6 months for the single component schema.
Example
The example below is of a device based auto discovery that supplies a
sensor
andbinary_sensor
.Discovery topic:
Payload:
Note that a required
platform
option is added to identify the component platform. The keys under thecomponents
(abbreviatedcmp
), are treated asobject_id
and are used to create a unique devicediscovery_id
. In this case thediscover_id's
aretest123 bins1
andtest123 sens1
.In case a
node_id
is specified (e.g.node123
) in the device discovery topic ...... this will become part of the
object_id
.The
discovery_id
's becometest123 node123 bins1
andtest123 node123 sens1
.Migration from single topic to device based discovery
To allow a smooth migration from single topic discovery to device based discovery, the
discovery_id
for anmqtt
item must be the same. Migration is supported of the single topic id has anode_id
and aobject_id
. After migration theobject
id moves inside the discovery payload and the previousnode_id
becomes the newobject_id
of the device discovery topic.Example (device automation):
Discovery topic single:
homeassistant/device_automation/0AFFD2/bla/config
Discovery id:
0AFFD2 bla
Discovery payload single:
Migrated:
Discovery topic device:
homeassistant/device/0AFFD2/config
Discovery id:
0AFFD2 bla
Discovery payload device:
If the new device discovery payload has the same
discovery_id
and comes after the single discovery payload. Home Assistant will publishNone
(retained) to the single discovery payload to remove it.Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: