Skip to content

fix: reply with an error when Home Assistant rejects an entity update - #433

Merged
zachowj merged 7 commits into
zachowj:mainfrom
Swamp-Ig:validate-entity-updates
Nov 10, 2026
Merged

zachowj merged 7 commits into
zachowj:mainfrom
Swamp-Ig:validate-entity-updates

Conversation

@Swamp-Ig

@Swamp-Ig Swamp-Ig commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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/entity replies success even when Home Assistant refuses the update. The error only appears in the Home Assistant log, the entity keeps the old value (or shows unknown, 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 an invalid_format error 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's unknown_error and 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 unknown instead 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 (or date).

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 gives invalid_format too; it used to raise a TypeError that 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_error but 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:

  • non-numeric state on a sensor with a unit, numeric device class or state class
  • state longer than 255 characters (any platform)
  • select option that isn't in the options list
  • timestamp, date or time that can't be parsed
  • text that is too long, too short or doesn't match the pattern; too long used to be truncated and accepted

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 plain fix:.

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/entity updates) 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_format and not store the payload, and reject NaN/±Infinity before sending.

Test plan

  • Rejected updates for a numeric sensor, a 256-character state, an invalid timestamp, an unparseable time, text over its max length, a text pattern mismatch and an unlisted select option: each replies invalid_format with the expected message, keeps the previous state and attributes, and a following update succeeds
  • Undo test for all 8 platforms; leaving an attribute out of update_attrs makes it fail
  • Unexpected exception replies unknown_error, not success, and the failed update isn't published by the next write
  • CI steps run locally: pre-commit, hassfest, and the test suite against the pinned Home Assistant (2026.7.3) and the latest from PyPI (2026.10.4), 197 passed each
  • CI green
  • Smoke test against Home Assistant 2026.12.0.dev0 (results below)

Smoke test

A websocket client acting as Node-RED discovered each entity with a valid state, sent an invalid nodered/entity update, then an attributes-only update, then a valid state.

Case (invalid value) BEFORE (main) reply → state AFTER (this PR) reply → state
Sensor with unit °C (abc) success → 21.5 (unchanged) invalid_format: "…has the non-numeric value: 'abc'…" → 21.5
Timestamp sensor (not a date) success → unknown invalid_format: "'not a date' is not a valid timestamp" → unchanged
Sensor, 256 characters success → unknown invalid_format: "State is longer than 255 characters" → unchanged
Select, options a/b (c) success → unknown invalid_format: "'c' is not one of the options" → a
Text, max length 5 (too long) success → too l invalid_format: "…which is too long (maximum length 5)" → unchanged
Text, pattern ^\d+$ (abc) success → 123 (unchanged) invalid_format: "…does not match expected pattern ^\d+$" → 123
Time (not a time) success → unknown invalid_format: "'not a time' is not a valid time" → unchanged

The 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 sent 21, then abc. On AFTER the state stayed 21 and Node-RED logged:

[error] [ha-entity-config:Val Temp] Error updating entity. Error Message: {"code":"invalid_format","message":"Sensor sensor.val_temp has device class 'None', state class 'None' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'abc' (<class 'str'>) at 'state'. Got 'abc'"}

@Swamp-Ig
Swamp-Ig force-pushed the validate-entity-updates branch from 610bb48 to 98d2860 Compare November 10, 2026 03:36
@zachowj

zachowj commented Nov 10, 2026

Copy link
Copy Markdown
Owner

I was thinking maybe we should be using Probatio here instead of Voluptuous but that change can be handled in a separate PR.

@zachowj
zachowj merged commit 6ce4718 into zachowj:main Nov 10, 2026
4 checks passed
@Swamp-Ig

Swamp-Ig commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

@zachowj

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.

@Swamp-Ig
Swamp-Ig deleted the validate-entity-updates branch November 10, 2026 11:38
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