Compare commits

...

4 Commits

Author SHA1 Message Date
epenet
af4aa449b7 Adjust 2026-02-05 20:05:48 +00:00
epenet
bf18f85f7e Adjust 2026-02-05 20:05:36 +00:00
epenet
1c0b32121c Adjust default _attr_color_mode 2026-02-05 20:05:36 +00:00
epenet
933860f7e5 Remove legacy fallback in light color_mode property 2026-02-05 20:05:36 +00:00
4 changed files with 6 additions and 69 deletions

View File

@@ -779,7 +779,7 @@ class LightEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
entity_description: LightEntityDescription
_attr_brightness: int | None = None
_attr_color_mode: ColorMode | None = None
_attr_color_mode: ColorMode = ColorMode.UNKNOWN
_attr_color_temp_kelvin: int | None = None
_attr_effect_list: list[str] | None = None
_attr_effect: str | None = None
@@ -801,43 +801,10 @@ class LightEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
return self._attr_brightness
@cached_property
def color_mode(self) -> ColorMode | None:
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
return self._attr_color_mode
@property
def _light_internal_color_mode(self) -> str:
"""Return the color mode of the light with backwards compatibility."""
if (color_mode := self.color_mode) is None:
# Backwards compatibility for color_mode added in 2021.4
# Warning added in 2024.3, break in 2025.3
if not self.__color_mode_reported and self.__should_report_light_issue():
self.__color_mode_reported = True
report_issue = self._suggest_report_issue()
_LOGGER.warning(
(
"%s (%s) does not report a color mode, this will stop working "
"in Home Assistant Core 2025.3, please %s"
),
self.entity_id,
type(self),
report_issue,
)
supported = self._light_internal_supported_color_modes
if ColorMode.HS in supported and self.hs_color is not None:
return ColorMode.HS
if ColorMode.COLOR_TEMP in supported and self.color_temp_kelvin is not None:
return ColorMode.COLOR_TEMP
if ColorMode.BRIGHTNESS in supported and self.brightness is not None:
return ColorMode.BRIGHTNESS
if ColorMode.ONOFF in supported:
return ColorMode.ONOFF
return ColorMode.UNKNOWN
return color_mode
@cached_property
def hs_color(self) -> tuple[float, float] | None:
"""Return the hue and saturation color value [float, float]."""
@@ -1084,7 +1051,7 @@ class LightEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
)
_is_on = self.is_on
color_mode = self._light_internal_color_mode if _is_on else None
color_mode = self.color_mode if _is_on else None
effect: str | None
if LightEntityFeature.EFFECT in supported_features:

View File

@@ -195,9 +195,9 @@ class TasmotaLight(
return self._brightness
@property
def color_mode(self) -> ColorMode | None:
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
return self._color_mode
return self._color_mode # type: ignore[return-value]
@property
def color_temp_kelvin(self) -> int | None:

View File

@@ -1837,7 +1837,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
),
TypeHintMatch(
function_name="color_mode",
return_type=["ColorMode", None],
return_type=["ColorMode"],
mandatory=True,
),
TypeHintMatch(

View File

@@ -2379,36 +2379,6 @@ def test_filter_supported_color_modes() -> None:
assert light.filter_supported_color_modes(supported) == {light.ColorMode.BRIGHTNESS}
@pytest.mark.parametrize(
("color_mode", "supported_color_modes", "warning_expected"),
[
(None, {light.ColorMode.ONOFF}, True),
(light.ColorMode.ONOFF, {light.ColorMode.ONOFF}, False),
],
)
async def test_report_no_color_mode(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
color_mode: str,
supported_color_modes: set[str],
warning_expected: bool,
) -> None:
"""Test a light setting no color mode."""
class MockLightEntityEntity(light.LightEntity):
_attr_color_mode = color_mode
_attr_is_on = True
_attr_supported_features = light.LightEntityFeature.EFFECT
_attr_supported_color_modes = supported_color_modes
entity = MockLightEntityEntity()
platform = MockEntityPlatform(hass, domain="test", platform_name="test")
await platform.async_add_entities([entity])
entity._async_calculate_state()
expected_warning = "does not report a color mode"
assert (expected_warning in caplog.text) is warning_expected
@pytest.mark.parametrize(
("color_mode", "supported_color_modes", "warning_expected"),
[