Skip to content

Commit 1a9e273

Browse files
committed
Fix missing color_mode initialization in MQTT JSON light schema
Initialize `_fixed_color_mode` and `_attr_color_mode` before the color mode branching logic in `_setup_from_config`, matching the pattern already used in `schema_template.py` (added in #162715). Without this, `_attr_color_mode` can remain `None` (the new default from #162715) in edge cases during entity (re)setup, causing "does not report a color mode" errors when the light is on. Also use `_fixed_color_mode` in `_update_color()` to skip color mode changes for single-mode lights, preventing the fixed mode from being overwritten by state updates. This matches the guard pattern used in `schema_template.py`.
1 parent b5480da commit 1a9e273

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

homeassistant/components/mqtt/light/schema_json.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,13 @@ def _setup_from_config(self, config: ConfigType) -> None:
190190
self._attr_supported_features |= (
191191
config[CONF_TRANSITION] and LightEntityFeature.TRANSITION
192192
)
193+
self._fixed_color_mode = None
194+
self._attr_color_mode = ColorMode.UNKNOWN
193195
if supported_color_modes := self._config.get(CONF_SUPPORTED_COLOR_MODES):
194196
self._attr_supported_color_modes = supported_color_modes
195197
if self.supported_color_modes and len(self.supported_color_modes) == 1:
196-
self._attr_color_mode = next(iter(self.supported_color_modes))
198+
self._fixed_color_mode = next(iter(self.supported_color_modes))
199+
self._attr_color_mode = self._fixed_color_mode
197200
else:
198201
self._attr_color_mode = ColorMode.UNKNOWN
199202
elif config.get(CONF_BRIGHTNESS):
@@ -206,6 +209,8 @@ def _setup_from_config(self, config: ConfigType) -> None:
206209
self._attr_color_mode = ColorMode.ONOFF
207210

208211
def _update_color(self, values: dict[str, Any]) -> None:
212+
if self._fixed_color_mode:
213+
return
209214
color_mode: str = values["color_mode"]
210215
if not self._supports_color_mode(color_mode):
211216
_LOGGER.warning(

0 commit comments

Comments
 (0)