Fix options flow changes silently discarded for pre-refactor UI entries (#1504)

* Fix options flow changes being silently discarded for UI-configured entries

validate() merges config_entry.options then config_entry.data, on the
assumption that data only ever holds YAML-imported settings (which should
win) or, for UI-created entries, just the entry name (harmless to apply
last).

That assumption doesn't hold for entries created before data/options were
split: their data still carries the full settings snapshot from initial
setup. Applying it after options means any change made through the options
flow for a key that already exists in data (e.g. adding a light) is
silently ignored, even though the options flow reports success and the
entry reloads without error.

Reproduced on a real entry: added a light via the options flow, entry
reloaded cleanly, but the light was never picked up by the switch's
service-call interceptor ("No switch found for entity_id=...") because
data still held the old light list and clobbered the updated options.

Fix: only let data win over options for genuinely YAML-imported entries
(config_entry.source == SOURCE_IMPORT), matching the existing use of that
check elsewhere in this file. For UI-configured entries, apply options
last so changes made through the options flow actually take effect.

* Add focused tests for the data/options merge order in validate()

Covers both source-specific contracts the merge logic relies on, per
review feedback on this PR:

- SOURCE_USER: options must win over data (this PR's actual fix - proven
  to fail against the pre-fix code, verified locally by reverting
  switch.py and re-running).
- SOURCE_IMPORT: data must keep winning over options (the existing,
  intentional YAML-precedence behavior - unchanged by this PR, verified
  to already pass against the pre-fix code too).

Verified against a real Home Assistant instance's test harness
(pytest-homeassistant-custom-component + the actual installed
homeassistant package), not just reasoned about statically.

---------

Co-authored-by: Bas Nijholt <bas@nijho.lt>
This commit is contained in:
Samson Brock 2026-09-06 02:00:02 -05:00 • committed by GitHub
commit 68c66a0ff3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 3 deletions

View file

@ -565,8 +565,20 @@ def validate(
if config_entry is not None:
assert service_data is None
assert defaults is None
data.update(config_entry.options) # come from options flow
data.update(config_entry.data) # all yaml settings come from data
if config_entry.source == SOURCE_IMPORT:
# YAML-configured entries: `data` is the authoritative YAML config
# and must win over any stray `options` from a prior UI setup.
data.update(config_entry.options)
data.update(config_entry.data)
else:
# UI-configured entries: settings are meant to live in `options`
# (see OptionsFlowHandler in config_flow.py). `data` here is
# either just the entry name, or - for entries created before
# data/options were split - a stale snapshot from initial setup.
# Applying it last would silently discard newer changes made
# through the options flow, so `options` must win instead.
data.update(config_entry.data)
data.update(config_entry.options)
else:
assert service_data is not None
changed_settings = {