Skip to content

Commit

Permalink
Revert "Remove deprecated supported features warning in LightEntity" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arturpragacz authored and frenck committed Jan 6, 2025
1 parent 5337ab2 commit 9a9514d
Show file tree
Hide file tree
Showing 3 changed files with 381 additions and 50 deletions.
81 changes: 76 additions & 5 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def filter_turn_off_params(
if not params:
return params

supported_features = light.supported_features
supported_features = light.supported_features_compat

if LightEntityFeature.FLASH not in supported_features:
params.pop(ATTR_FLASH, None)
Expand All @@ -366,7 +366,7 @@ def filter_turn_off_params(

def filter_turn_on_params(light: LightEntity, params: dict[str, Any]) -> dict[str, Any]:
"""Filter out params not supported by the light."""
supported_features = light.supported_features
supported_features = light.supported_features_compat

if LightEntityFeature.EFFECT not in supported_features:
params.pop(ATTR_EFFECT, None)
Expand Down Expand Up @@ -1093,7 +1093,7 @@ def effect(self) -> str | None:
def capability_attributes(self) -> dict[str, Any]:
"""Return capability attributes."""
data: dict[str, Any] = {}
supported_features = self.supported_features
supported_features = self.supported_features_compat
supported_color_modes = self._light_internal_supported_color_modes

if ColorMode.COLOR_TEMP in supported_color_modes:
Expand Down Expand Up @@ -1255,11 +1255,12 @@ def __validate_supported_color_modes(
def state_attributes(self) -> dict[str, Any] | None:
"""Return state attributes."""
data: dict[str, Any] = {}
supported_features = self.supported_features
supported_features = self.supported_features_compat
supported_color_modes = self.supported_color_modes
legacy_supported_color_modes = (
supported_color_modes or self._light_internal_supported_color_modes
)
supported_features_value = supported_features.value
_is_on = self.is_on
color_mode = self._light_internal_color_mode if _is_on else None

Expand All @@ -1278,6 +1279,13 @@ def state_attributes(self) -> dict[str, Any] | None:
data[ATTR_BRIGHTNESS] = self.brightness
else:
data[ATTR_BRIGHTNESS] = None
elif supported_features_value & _DEPRECATED_SUPPORT_BRIGHTNESS.value:
# Backwards compatibility for ambiguous / incomplete states
# Warning is printed by supported_features_compat, remove in 2025.1
if _is_on:
data[ATTR_BRIGHTNESS] = self.brightness
else:
data[ATTR_BRIGHTNESS] = None

if color_temp_supported(supported_color_modes):
if color_mode == ColorMode.COLOR_TEMP:
Expand All @@ -1292,6 +1300,21 @@ def state_attributes(self) -> dict[str, Any] | None:
else:
data[ATTR_COLOR_TEMP_KELVIN] = None
data[_DEPRECATED_ATTR_COLOR_TEMP.value] = None
elif supported_features_value & _DEPRECATED_SUPPORT_COLOR_TEMP.value:
# Backwards compatibility
# Warning is printed by supported_features_compat, remove in 2025.1
if _is_on:
color_temp_kelvin = self.color_temp_kelvin
data[ATTR_COLOR_TEMP_KELVIN] = color_temp_kelvin
if color_temp_kelvin:
data[_DEPRECATED_ATTR_COLOR_TEMP.value] = (
color_util.color_temperature_kelvin_to_mired(color_temp_kelvin)
)
else:
data[_DEPRECATED_ATTR_COLOR_TEMP.value] = None
else:
data[ATTR_COLOR_TEMP_KELVIN] = None
data[_DEPRECATED_ATTR_COLOR_TEMP.value] = None

if color_supported(legacy_supported_color_modes) or color_temp_supported(
legacy_supported_color_modes
Expand Down Expand Up @@ -1329,7 +1352,24 @@ def _light_internal_supported_color_modes(self) -> set[ColorMode] | set[str]:
type(self),
report_issue,
)
return {ColorMode.ONOFF}
supported_features = self.supported_features_compat
supported_features_value = supported_features.value
supported_color_modes: set[ColorMode] = set()

if supported_features_value & _DEPRECATED_SUPPORT_COLOR_TEMP.value:
supported_color_modes.add(ColorMode.COLOR_TEMP)
if supported_features_value & _DEPRECATED_SUPPORT_COLOR.value:
supported_color_modes.add(ColorMode.HS)
if (
not supported_color_modes
and supported_features_value & _DEPRECATED_SUPPORT_BRIGHTNESS.value
):
supported_color_modes = {ColorMode.BRIGHTNESS}

if not supported_color_modes:
supported_color_modes = {ColorMode.ONOFF}

return supported_color_modes

@cached_property
def supported_color_modes(self) -> set[ColorMode] | set[str] | None:
Expand All @@ -1341,6 +1381,37 @@ def supported_features(self) -> LightEntityFeature:
"""Flag supported features."""
return self._attr_supported_features

@property
def supported_features_compat(self) -> LightEntityFeature:
"""Return the supported features as LightEntityFeature.
Remove this compatibility shim in 2025.1 or later.
"""
features = self.supported_features
if type(features) is not int: # noqa: E721
return features
new_features = LightEntityFeature(features)
if self._deprecated_supported_features_reported is True:
return new_features
self._deprecated_supported_features_reported = True
report_issue = self._suggest_report_issue()
report_issue += (
" and reference "
"https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation"
)
_LOGGER.warning(
(
"Entity %s (%s) is using deprecated supported features"
" values which will be removed in HA Core 2025.1. Instead it should use"
" %s and color modes, please %s"
),
self.entity_id,
type(self),
repr(new_features),
report_issue,
)
return new_features

def __should_report_light_issue(self) -> bool:
"""Return if light color mode issues should be reported."""
if not self.platform:
Expand Down
3 changes: 1 addition & 2 deletions tests/components/light/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
DOMAIN,
ColorMode,
LightEntity,
LightEntityFeature,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -157,7 +156,7 @@ class MockLight(MockToggleEntity, LightEntity):

_attr_max_color_temp_kelvin = DEFAULT_MAX_KELVIN
_attr_min_color_temp_kelvin = DEFAULT_MIN_KELVIN
supported_features = LightEntityFeature(0)
supported_features = 0

brightness = None
color_temp_kelvin = None
Expand Down
Loading

0 comments on commit 9a9514d

Please sign in to comment.