Repository navigation
Replies: 2 comments 1 reply
|
It would probably be easier to follow if the bug fixes went into a separate pull request. I think I'd prefer having the companion handle holding the latest update from NR until the entity becomes available in HA. If we handle it the other way, wouldn't we need to introduce a new communication channel for HA to tell NR when the entity is ready, so NR can then send the update back to HA? Or am I misunderstanding how you envision that working? |
|
Closing this out. Bug 1 (updates sent before the entity is added are dropped) doesn't happen on any supported Home Assistant version. Since 2024.3 (home-assistant/core#111553), HA starts adding entities eagerly, and nothing in the add path waits on I/O. I traced it through the entity platform, the restore-state lookups and every companion platform, on 2024.5.0, 2026.7.3 and current dev. So the entity is registered before the next websocket message is handled. A smoke test against HA 2026.12.0.dev0 agrees. Sending discovery, an entity update, a config update and another entity update back to back, without waiting for replies, applied every update in order, for new entities, entities with a device, and registered entities rediscovered right after a restart. So rather than adding a queue, #429 adds a test that fails if adding an entity ever starts waiting, so we'd know if that changes. While checking this I found that an aborted add (for example, a disabled entity) left the node marked as discovered; #429 fixes that too. The other discovery bugs were fixed in #427 and #428. The validation work from #2090 is next. |
Uh oh!
There was an error while loading. Please reload this page.
While looking at state validation (zachowj/node-red-contrib-home-assistant-websocket#2090), I found two problems with how the companion handles discovery. I'd like to fix them first, because the validation work builds on the same code.
What happens today
1. Updates sent before the entity is added are dropped.
nodered/discoveryreplies with success as soon as it has dispatched. The entity is added asynchronously afterwards, and only connects itsnodered_entity_*dispatcher listener inasync_added_to_hass. If anodered/entityarrives in that gap, there's no listener, so the update is silently dropped. Node-RED still gets a success result. Contrib waiting for the discovery reply doesn't help, because the reply comes back before the entity exists.The window is short, but flows that fire on deploy or startup can hit it. With resend off, the entity then stays unknown or unavailable until the next message.
2. An invalid state in discovery breaks the entity.
If the discovery message carries a state HA rejects (for example a resent non-numeric state on a sensor with a unit), the first state write fails while HA is adding the entity. HA logs
Error adding entity ..., but discovery has already replied with success. What happens next depends on the HA version:Before 2026.10: the entity is left half added, but still registered and connected. A later valid
nodered/entityrecovers it. I confirmed this with a test against 2026.7.3.2026.10 and later: Always call Entity.add_to_platform_abort when entity fails to add home-assistant/core#179509 changed the failure path, so HA now unregisters the entity and sets
entity.hass = None. The companion removes its dispatcher listeners inasync_will_remove_from_hass, which this path doesn't call, so they stay connected to the dead entity. The discovery hash also stays inALREADY_DISCOVERED. As a result:nodered/entityupdates reach the dead entity, the write raises and is logged, and Node-RED still gets success;It stays broken until HA restarts or the integration reloads. I worked this out from reading the code and haven't reproduced it on 2026.10+ yet.
Also, rediscovering an entity that's already discovered only updates its config, so a valid state in a later discovery message is ignored. That's fine for reconnects, but it means discovery can't be used to fix case 2.
Proposal
async_on_removeinstead ofasync_will_remove_from_hass, and remove the discovery hash in the same callback. A failed add then leaves nothing behind, and the next discovery creates the entity normally.invalid_format, the same code the schema rejection uses) and don't mark the hash as discovered.nodered/entitymessages for a hash that has been dispatched but not yet added. Apply them once the entity is added, then reply. If the add fails, reply with the error.None of this changes the message format. Only the replies change: failures that are acknowledged as success today would come back as errors.
Questions
nodered/discoveryreply only once the entity has been added? The second is simpler, but it changes when that reply arrives, and older contrib versions might depend on the current timing.All reactions