Fix missing color_mode initialization in MQTT JSON light schema#167429
Conversation
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
|
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
|
There was a problem hiding this comment.
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_modetoColorMode.UNKNOWNduring setup to avoidNonewhen the entity is on. - Introduce
_fixed_color_modeassignment for single supported-color-mode configurations.
b674b04 to
1a9e273
Compare
jbouwh
left a comment
There was a problem hiding this comment.
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_kelvin630b4de to
661e240
Compare
|
Looks good, please do no rewrite/replace previous commits |
|
If you are ready please mark the PR "Ready for review" |
661e240 to
c6ba343
Compare
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.
c6ba343 to
f473c15
Compare
|
Please do not force push |
|
Not needed anymore |
|
@noerstad Please mark the PR ready so that it can be merged |
Proposed change
PR #162715 changed
_attr_color_modedefault fromColorMode.UNKNOWNtoNoneand added a strict check that raisesHomeAssistantErrorwhen a light is on butcolor_mode is None. The template schema (schema_template.py) was updated to initialize_attr_color_mode = ColorMode.UNKNOWNbefore the color mode branching logic, but the JSON schema (schema_json.py) was not.This causes MQTT JSON schema lights to fail with:
This PR initializes
_attr_color_mode = ColorMode.UNKNOWNbefore the color mode branching in_setup_from_config, and removes the unused_fixed_color_modeclass variable.Type of change
Additional information
Checklist