Skip to content

Fix missing color_mode initialization in MQTT JSON light schema#167429

Merged
jbouwh merged 1 commit into
home-assistant:devfrom
noerstad:fix/mqtt-json-light-color-mode
Apr 7, 2026
Merged

Fix missing color_mode initialization in MQTT JSON light schema#167429
jbouwh merged 1 commit into
home-assistant:devfrom
noerstad:fix/mqtt-json-light-color-mode

Conversation

@noerstad
Copy link
Copy Markdown
Contributor

@noerstad noerstad commented Apr 5, 2026

Proposed change

PR #162715 changed _attr_color_mode default from ColorMode.UNKNOWN to None and added a strict check that raises HomeAssistantError when a light is on but color_mode is None. The template schema (schema_template.py) was updated to initialize _attr_color_mode = ColorMode.UNKNOWN before the color mode branching logic, but the JSON schema (schema_json.py) was not.

This causes MQTT JSON schema lights to fail with:

does not report a color mode

This PR initializes _attr_color_mode = ColorMode.UNKNOWN before the color mode branching in _setup_from_config, and removes the unused _fixed_color_mode class variable.

Type of change

  • Bugfix (non-breaking change which fixes an issue)

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • Tests have been added to verify that the new code works.

Copilot AI review requested due to automatic review settings April 5, 2026 11:13
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @noerstad

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant Bot added bugfix cla-needed core integration: mqtt small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage Top 50 Integration is ranked within the top 50 by usage labels Apr 5, 2026
@home-assistant home-assistant Bot marked this pull request as draft April 5, 2026 11:13
@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Apr 5, 2026

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Apr 5, 2026

Hey there @emontnemery, @jbouwh, @bdraco, mind taking a look at this pull request as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mqtt can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign mqtt Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes MQTT JSON schema lights failing with “does not report a color mode” by aligning schema_json.py with the safety-net initialization approach already used in the template schema.

Changes:

  • Initialize _attr_color_mode to ColorMode.UNKNOWN during setup to avoid None when the entity is on.
  • Introduce _fixed_color_mode assignment for single supported-color-mode configurations.

Comment thread homeassistant/components/mqtt/light/schema_json.py Outdated
Comment thread homeassistant/components/mqtt/light/schema_json.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread homeassistant/components/mqtt/light/schema_json.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! As a fix only the assignment to an unknown color should be needed. The rest of the changes can be removed. The test is okay.

diff --git a/homeassistant/components/mqtt/light/schema_json.py b/homeassistant/components/mqtt/light/schema_json.py
index 111b176cb01..b388cdebb65 100644
--- a/homeassistant/components/mqtt/light/schema_json.py
+++ b/homeassistant/components/mqtt/light/schema_json.py
@@ -146,7 +146,6 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
     _entity_id_format = ENTITY_ID_FORMAT
     _attributes_extra_blocked = MQTT_LIGHT_ATTRIBUTES_BLOCKED
 
-    _fixed_color_mode: ColorMode | str | None = None
     _flash_times: dict[str, int | None]
     _topic: dict[str, str | None]
     _optimistic: bool
@@ -190,13 +189,11 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
         self._attr_supported_features |= (
             config[CONF_TRANSITION] and LightEntityFeature.TRANSITION
         )
-        self._fixed_color_mode = None
         self._attr_color_mode = ColorMode.UNKNOWN
         if supported_color_modes := self._config.get(CONF_SUPPORTED_COLOR_MODES):
             self._attr_supported_color_modes = supported_color_modes
             if self.supported_color_modes and len(self.supported_color_modes) == 1:
-                self._fixed_color_mode = next(iter(self.supported_color_modes))
-                self._attr_color_mode = self._fixed_color_mode
+                self._attr_color_mode = next(iter(self.supported_color_modes))
             else:
                 self._attr_color_mode = ColorMode.UNKNOWN
         elif config.get(CONF_BRIGHTNESS):
@@ -209,7 +206,7 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
             self._attr_color_mode = ColorMode.ONOFF
 
     def _update_color(self, values: dict[str, Any]) -> None:
-        color_mode: str = self._fixed_color_mode or values["color_mode"]
+        color_mode: str = values["color_mode"]
         if not self._supports_color_mode(color_mode):
             _LOGGER.warning(
                 "Invalid color mode '%s' received for entity %s",
@@ -340,12 +337,8 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
             self._attr_brightness = last_attributes.get(
                 ATTR_BRIGHTNESS, self.brightness
             )
-            self._attr_color_mode = cast(
-                ColorMode,
-                self._fixed_color_mode
-                if self._fixed_color_mode is not None
-                else last_attributes.get(ATTR_COLOR_MODE, self.color_mode)
-                or ColorMode.UNKNOWN,
+            self._attr_color_mode = last_attributes.get(
+                ATTR_COLOR_MODE, self.color_mode
             )
             self._attr_color_temp_kelvin = last_attributes.get(
                 ATTR_COLOR_TEMP_KELVIN, self.color_temp_kelvin

Comment thread homeassistant/components/mqtt/light/schema_json.py
Comment thread homeassistant/components/mqtt/light/schema_json.py
Comment thread homeassistant/components/mqtt/light/schema_json.py Outdated
Comment thread homeassistant/components/mqtt/light/schema_json.py Outdated
Comment thread homeassistant/components/mqtt/light/schema_json.py Outdated
Comment thread homeassistant/components/mqtt/light/schema_json.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft April 6, 2026 14:44
@jbouwh jbouwh added this to the 2026.4.2 milestone Apr 6, 2026
@noerstad noerstad force-pushed the fix/mqtt-json-light-color-mode branch from 630b4de to 661e240 Compare April 6, 2026 15:46
@jbouwh
Copy link
Copy Markdown
Contributor

jbouwh commented Apr 6, 2026

Looks good, please do no rewrite/replace previous commits

@jbouwh
Copy link
Copy Markdown
Contributor

jbouwh commented Apr 6, 2026

If you are ready please mark the PR "Ready for review"

Comment thread tests/components/mqtt/test_light_json.py Outdated
Comment thread tests/components/mqtt/test_light_json.py Outdated
Comment thread tests/components/mqtt/test_light_json.py Outdated
Copilot AI review requested due to automatic review settings April 6, 2026 16:51
@noerstad noerstad force-pushed the fix/mqtt-json-light-color-mode branch from 661e240 to c6ba343 Compare April 6, 2026 16:51
Comment thread tests/components/mqtt/test_light_json.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread tests/components/mqtt/test_light_json.py
Initialize `_attr_color_mode` to `ColorMode.UNKNOWN` before the
color mode branching logic in `_setup_from_config`, matching the
pattern already used in `schema_template.py` (added in home-assistant#162715).

Without this, `_attr_color_mode` can remain `None` (the new default
from home-assistant#162715) in edge cases during entity (re)setup, causing
"does not report a color mode" errors when the light is on.

Also removes the unused `_fixed_color_mode` class variable.
@noerstad noerstad force-pushed the fix/mqtt-json-light-color-mode branch from c6ba343 to f473c15 Compare April 6, 2026 19:09
@jbouwh
Copy link
Copy Markdown
Contributor

jbouwh commented Apr 6, 2026

Please do not force push

@jbouwh
Copy link
Copy Markdown
Contributor

jbouwh commented Apr 6, 2026

Can you please update the PR description, so that it matches the change?

Not needed anymore

Copy link
Copy Markdown
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @noerstad for your first contribution 👍

@jbouwh
Copy link
Copy Markdown
Contributor

jbouwh commented Apr 6, 2026

@noerstad Please mark the PR ready so that it can be merged

@noerstad noerstad marked this pull request as ready for review April 7, 2026 06:56
Copilot AI review requested due to automatic review settings April 7, 2026 06:56
@home-assistant home-assistant Bot requested a review from jbouwh April 7, 2026 06:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@jbouwh jbouwh merged commit 74a6f78 into home-assistant:dev Apr 7, 2026
50 of 51 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix cherry-picked cla-signed code-owner-approved core integration: mqtt Quality Scale: platinum small-pr PRs with less than 30 lines. Top 50 Integration is ranked within the top 50 by usage Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants