Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for attribute caching to the light platform #106260

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from enum import IntFlag, StrEnum
import logging
import os
from typing import Any, Self, cast, final
from typing import TYPE_CHECKING, Any, Self, cast, final

import voluptuous as vol

Expand All @@ -33,6 +33,11 @@
from homeassistant.loader import bind_hass
import homeassistant.util.color as color_util

if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property

DOMAIN = "light"
SCAN_INTERVAL = timedelta(seconds=30)
DATA_PROFILES = "light_profiles"
Expand Down Expand Up @@ -820,7 +825,25 @@ class LightEntityDescription(ToggleEntityDescription, frozen_or_thawed=True):
"""A class that describes binary sensor entities."""


class LightEntity(ToggleEntity):
CACHED_PROPERTIES_WITH_ATTR_ = {
"brightness",
"color_mode",
"hs_color",
"xy_color",
"rgb_color",
"rgbw_color",
"rgbww_color",
"color_temp",
"min_mireds",
"max_mireds",
"effect_list",
"effect",
"supported_color_modes",
"supported_features",
}


class LightEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Base class for light entities."""

_entity_component_unrecorded_attributes = frozenset(
Expand Down Expand Up @@ -855,12 +878,12 @@ class LightEntity(ToggleEntity):
_attr_supported_features: LightEntityFeature = LightEntityFeature(0)
_attr_xy_color: tuple[float, float] | None = None

@property
@cached_property
def brightness(self) -> int | None:
"""Return the brightness of this light between 0..255."""
return self._attr_brightness

@property
@cached_property
def color_mode(self) -> ColorMode | str | None:
"""Return the color mode of the light."""
return self._attr_color_mode
Expand All @@ -885,22 +908,22 @@ def _light_internal_color_mode(self) -> str:

return color_mode

@property
@cached_property
def hs_color(self) -> tuple[float, float] | None:
"""Return the hue and saturation color value [float, float]."""
return self._attr_hs_color

@property
@cached_property
def xy_color(self) -> tuple[float, float] | None:
"""Return the xy color value [float, float]."""
return self._attr_xy_color

@property
@cached_property
def rgb_color(self) -> tuple[int, int, int] | None:
"""Return the rgb color value [int, int, int]."""
return self._attr_rgb_color

@property
@cached_property
def rgbw_color(self) -> tuple[int, int, int, int] | None:
"""Return the rgbw color value [int, int, int, int]."""
return self._attr_rgbw_color
Expand All @@ -911,12 +934,12 @@ def _light_internal_rgbw_color(self) -> tuple[int, int, int, int] | None:
rgbw_color = self.rgbw_color
return rgbw_color

@property
@cached_property
def rgbww_color(self) -> tuple[int, int, int, int, int] | None:
"""Return the rgbww color value [int, int, int, int, int]."""
return self._attr_rgbww_color

@property
@cached_property
def color_temp(self) -> int | None:
"""Return the CT color value in mireds."""
return self._attr_color_temp
Expand All @@ -928,12 +951,12 @@ def color_temp_kelvin(self) -> int | None:
return color_util.color_temperature_mired_to_kelvin(self.color_temp)
return self._attr_color_temp_kelvin

@property
@cached_property
def min_mireds(self) -> int:
"""Return the coldest color_temp that this light supports."""
return self._attr_min_mireds

@property
@cached_property
def max_mireds(self) -> int:
"""Return the warmest color_temp that this light supports."""
return self._attr_max_mireds
Expand All @@ -952,12 +975,12 @@ def max_color_temp_kelvin(self) -> int:
return color_util.color_temperature_mired_to_kelvin(self.min_mireds)
return self._attr_max_color_temp_kelvin

@property
@cached_property
def effect_list(self) -> list[str] | None:
"""Return the list of supported effects."""
return self._attr_effect_list

@property
@cached_property
def effect(self) -> str | None:
"""Return the current effect."""
return self._attr_effect
Expand Down Expand Up @@ -1138,12 +1161,12 @@ def _light_internal_supported_color_modes(self) -> set[ColorMode] | set[str]:

return supported_color_modes

@property
@cached_property
def supported_color_modes(self) -> set[ColorMode] | set[str] | None:
"""Flag supported color modes."""
return self._attr_supported_color_modes

@property
@cached_property
def supported_features(self) -> LightEntityFeature:
"""Flag supported features."""
return self._attr_supported_features