Fix MQTT JSON light restoring None color_mode on startup#168608
Merged
jbouwh merged 2 commits intoApr 20, 2026
Conversation
When an optimistic MQTT JSON light was off at the time HA saved its state, `LightEntity.state_attributes` stores `color_mode: None` in the saved attributes. On restart, the restore path in `_subscribe_topics` would overwrite the correctly initialized `_attr_color_mode` with `None`, because `last_attributes.get(ATTR_COLOR_MODE, self.color_mode)` returns the stored `None` value (the default fallback is only used when the key is missing). Turning the light on would then raise "does not report a color mode" from `LightEntity.state_attributes`. Use `or self.color_mode` to fall back to the correctly initialized value when the saved attribute is `None`. All valid `ColorMode` values are truthy non-empty strings, so this is safe.
Contributor
|
Hey there @emontnemery, @jbouwh, @bdraco, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a startup restore edge case for optimistic MQTT JSON lights where a previously saved color_mode: None could overwrite the correctly initialized _attr_color_mode, leading to errors on the next turn_on.
Changes:
- Prevent restoring
Noneinto_attr_color_modeby falling back to the configured/default color mode. - Add a regression test that simulates restoring an
OFFstate withcolor_mode: Noneand verifiesturn_onstill works.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| homeassistant/components/mqtt/light/schema_json.py | Adjusts optimistic restore logic to avoid restoring None into color_mode. |
| tests/components/mqtt/test_light_json.py | Adds regression coverage for restoring an OFF state containing color_mode: None. |
Contributor
Author
|
Test failure seem to be unrelated flakey test. Hopefully a re-run should do it |
jbouwh
approved these changes
Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
When an optimistic MQTT JSON light was off at the time Home Assistant saved its state,
LightEntity.state_attributesstorescolor_mode: Nonein the saved attributes (becausecolor_modeis forced toNonewheneveris_onisFalse).On restart, the restore path in
MqttLightJson._subscribe_topicswould overwrite the correctly initialized_attr_color_modewithNone, because:dict.get(key, default)returns the storedNonevalue — the default fallback is only used when the key is missing, not when the stored value isNone.Subsequently turning the light on would then raise "does not report a color mode" from
LightEntity.state_attributes, because the light reportsis_on=Truewithcolor_mode=None.The fix uses
or self.color_modeto fall back to the value set by_setup_from_configwhen the saved attribute isNone. All validColorModevalues are truthy non-empty strings, so this is safe.This is a follow-up to #167429, which addressed a related initialization path but did not catch the state-restore path.
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.If the code does not interact with devices: