Repository navigation
fix: reply with an error when Home Assistant rejects an entity update - #433
Conversation
610bb48 to
98d2860
Compare
|
I was thinking maybe we should be using Probatio here instead of Voluptuous but that change can be handled in a separate PR. |
Yeh I was thinking the same thing, however HA has said in their policy that there's no plans of retiring the vol shim anytime soon due to lots of custom integrations depending on it, and us doing so would mean losing compat with anything prior to 2022.09, so on that basis I thought best to leave it for now. My thought was that if we need anything in newer HA versions, then upgrade, but otherwise maybe leave it for 6-12 months before changing. There's no pressing call to upgrade really given the above. |
The companion half of the plan agreed in zachowj/node-red-contrib-home-assistant-websocket#2090: Home Assistant does the validating, and the companion reports what it rejects instead of acknowledging it.
Today
nodered/entityreplies success even when Home Assistant refuses the update. The error only appears in the Home Assistant log, the entity keeps the old value (or showsunknown, or a shortened text), and for numeric sensors later attribute-only updates are silently dropped too, because every write re-reads the rejected value. With this PR the reply is aninvalid_formaterror carrying Home Assistant's message, the entity is left exactly as it was, and the next valid update works.Summary
069342a Reply with an error when an entity update fails
The shared update path from #429 no longer catches and logs exceptions. A state Home Assistant rejects is replied to with
invalid_format(the same code the message schema and the discovery check from #428 use); anything unexpected gets the websocket API'sunknown_errorand is logged by Home Assistant.A rejected update is undone: each entity lists the attributes an entity update can change (
update_attrs), and they are restored if validation fails. The update, the check and the restore all run without awaiting, so nothing else can see the half-applied update. A test applies an update that sets every field to each platform and fails if anything it changed isn't restored, so the list can't silently fall behind.Config updates (
nodered/entity/update_config) aren't validated against the current state. If they were, a config change that the current state doesn't fit would be rejected, and so would a new state that fits only the new config, leaving the entity stuck.b30d78f Reject states longer than 255 characters
Home Assistant stores a longer state as
unknown(and logs an error) instead of raising, so the companion checks the length itself. This applies to every platform and to discovery.580f390 Reject select options that aren't listed
Home Assistant shows an option that isn't in the options list as
unknowninstead of raising, so the companion checks the incoming option. Only the incoming state is checked, so changing the options never fails because of the current selection.953228f Reject timestamps and dates that can't be parsed
A timestamp or date sensor that couldn't parse its state logged an error and showed
unknown. It now replies'<value>' is not a valid timestamp(ordate).26d26f5 Reject times that can't be parsed
Same for time entities, which logged an exception and showed
unknown. A number sent as a time now givesinvalid_formattoo; it used to raise aTypeErrorthat was logged and replied to with success.d11086d Reject text values instead of truncating them
Text entities cut values longer than the max length short, with a warning in the log. They now leave the value alone and let Home Assistant's text validation reject it, which also covers the min length and the pattern (those already raised, but the error was swallowed).
98d2860 Undo an entity update that fails unexpectedly (optional)
The first commit only undoes an update when validation rejects it. If an update fails with any other exception, the reply is
unknown_errorbut the half-applied values stay on the entity, and the next write publishes them (an attributes-only update, or losing the connection). This commit undoes the update whatever it raises. It's separate because it goes beyond rejecting invalid states; see the open question below.Behaviour changes
Updates that used to get a success reply now get an error, so a flow that sends one of these will see a failure where it saw none before:
Each of these already failed in Home Assistant, but quietly. The text truncation is the one case where a value that was accepted, in shortened form, is now refused. @zachowj, your call whether that warrants
fix!:and a major release; I've left the title as a plainfix:.Open question: the last commit also undoes updates that fail for reasons other than validation, which is broader than #2090 asked for. I think it's the safer behaviour, since an update that replied with an error shouldn't show up later, but it can be dropped without affecting the rest. Happy either way.
Not covered: attributes and binary sensors (left out on #2090), and number entities, where Home Assistant doesn't reject non-numeric values. Contrib's number node already only sends numbers; NaN and ±Infinity will be rejected there in the contrib PR. Discovery updates for an existing entity (as opposed to
nodered/entityupdates) still log errors and reply success, as before.Contrib
No contrib change is needed for this to work: the rejected update is logged on the entity config node in Node-RED with Home Assistant's message (smoke test below). But contrib catches that error without failing the node, so the node still shows success, and with Resend on it stores the rejected value to send again on reconnect. The contrib PR from #2090 will fail the node with a validation status for
invalid_formatand not store the payload, and reject NaN/±Infinity before sending.Test plan
invalid_formatwith the expected message, keeps the previous state and attributes, and a following update succeedsupdate_attrsmakes it failunknown_error, not success, and the failed update isn't published by the next writeSmoke test
A websocket client acting as Node-RED discovered each entity with a valid state, sent an invalid
nodered/entityupdate, then an attributes-only update, then a valid state.main) reply → stateabc)21.5(unchanged)invalid_format: "…has the non-numeric value: 'abc'…" →21.5not a date)unknowninvalid_format: "'not a date' is not a valid timestamp" → unchangedunknowninvalid_format: "State is longer than 255 characters" → unchangeda/b(c)unknowninvalid_format: "'c' is not one of the options" →atoo long)too linvalid_format: "…which is too long (maximum length 5)" → unchanged^\d+$(abc)123(unchanged)invalid_format: "…does not match expected pattern ^\d+$" →123not a time)unknowninvalid_format: "'not a time' is not a valid time" → unchangedThe attributes-only update that followed was dropped on BEFORE for the numeric sensor (success reply, attribute never written) and written on AFTER in every case. The final valid state was applied in every case on both.
With Node-RED (current contrib
main): a sensor node with unit °C was sent21, thenabc. On AFTER the state stayed21and Node-RED logged: