diff --git a/homeassistant/components/fritzbox/coordinator.py b/homeassistant/components/fritzbox/coordinator.py index 52fa3ba1a12b5..a6a30ffdc6aff 100644 --- a/homeassistant/components/fritzbox/coordinator.py +++ b/homeassistant/components/fritzbox/coordinator.py @@ -27,6 +27,7 @@ class FritzboxCoordinatorData: devices: dict[str, FritzhomeDevice] templates: dict[str, FritzhomeTemplate] + supported_color_properties: dict[str, tuple[dict, list]] class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorData]): @@ -49,7 +50,7 @@ def __init__(self, hass: HomeAssistant, name: str) -> None: self.new_devices: set[str] = set() self.new_templates: set[str] = set() - self.data = FritzboxCoordinatorData({}, {}) + self.data = FritzboxCoordinatorData({}, {}, {}) async def async_setup(self) -> None: """Set up the coordinator.""" @@ -120,6 +121,7 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData: devices = self.fritz.get_devices() device_data = {} + supported_color_properties = self.data.supported_color_properties for device in devices: # assume device as unavailable, see #55799 if ( @@ -136,6 +138,13 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData: device_data[device.ain] = device + # pre-load supported colors and color temps for new devices + if device.has_color and device.ain not in supported_color_properties: + supported_color_properties[device.ain] = ( + device.get_colors(), + device.get_color_temps(), + ) + template_data = {} if self.has_templates: templates = self.fritz.get_templates() @@ -145,7 +154,11 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData: self.new_devices = device_data.keys() - self.data.devices.keys() self.new_templates = template_data.keys() - self.data.templates.keys() - return FritzboxCoordinatorData(devices=device_data, templates=template_data) + return FritzboxCoordinatorData( + devices=device_data, + templates=template_data, + supported_color_properties=supported_color_properties, + ) async def _async_update_data(self) -> FritzboxCoordinatorData: """Fetch all device data.""" diff --git a/homeassistant/components/fritzbox/light.py b/homeassistant/components/fritzbox/light.py index d347f6898c027..36cb7dc8cff04 100644 --- a/homeassistant/components/fritzbox/light.py +++ b/homeassistant/components/fritzbox/light.py @@ -57,7 +57,6 @@ def __init__( ) -> None: """Initialize the FritzboxLight entity.""" super().__init__(coordinator, ain, None) - self._supported_hs: dict[int, list[int]] = {} self._attr_supported_color_modes = {ColorMode.ONOFF} if self.data.has_color: @@ -65,6 +64,26 @@ def __init__( elif self.data.has_level: self._attr_supported_color_modes = {ColorMode.BRIGHTNESS} + (supported_colors, supported_color_temps) = ( + coordinator.data.supported_color_properties.get(self.data.ain, ({}, [])) + ) + + # Fritz!DECT 500 only supports 12 values for hue, with 3 saturations each. + # Map supported colors to dict {hue: [sat1, sat2, sat3]} for easier lookup + self._supported_hs: dict[int, list[int]] = {} + for values in supported_colors.values(): + hue = int(values[0][0]) + self._supported_hs[hue] = [ + int(values[0][1]), + int(values[1][1]), + int(values[2][1]), + ] + + if supported_color_temps: + # only available for color bulbs + self._attr_max_color_temp_kelvin = int(max(supported_color_temps)) + self._attr_min_color_temp_kelvin = int(min(supported_color_temps)) + @property def is_on(self) -> bool: """If the light is currently on or off.""" @@ -148,30 +167,3 @@ async def async_turn_off(self, **kwargs: Any) -> None: """Turn the light off.""" await self.hass.async_add_executor_job(self.data.set_state_off) await self.coordinator.async_refresh() - - async def async_added_to_hass(self) -> None: - """Get light attributes from device after entity is added to hass.""" - await super().async_added_to_hass() - - def _get_color_data() -> tuple[dict, list]: - return (self.data.get_colors(), self.data.get_color_temps()) - - ( - supported_colors, - supported_color_temps, - ) = await self.hass.async_add_executor_job(_get_color_data) - - if supported_color_temps: - # only available for color bulbs - self._attr_max_color_temp_kelvin = int(max(supported_color_temps)) - self._attr_min_color_temp_kelvin = int(min(supported_color_temps)) - - # Fritz!DECT 500 only supports 12 values for hue, with 3 saturations each. - # Map supported colors to dict {hue: [sat1, sat2, sat3]} for easier lookup - for values in supported_colors.values(): - hue = int(values[0][0]) - self._supported_hs[hue] = [ - int(values[0][1]), - int(values[1][1]), - int(values[2][1]), - ]