mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Preload supported color properties in fritzbox lights (#133798)
This commit is contained in:
parent
6cbc803b28
commit
bbb5f9e717
@ -27,6 +27,7 @@ class FritzboxCoordinatorData:
|
|||||||
|
|
||||||
devices: dict[str, FritzhomeDevice]
|
devices: dict[str, FritzhomeDevice]
|
||||||
templates: dict[str, FritzhomeTemplate]
|
templates: dict[str, FritzhomeTemplate]
|
||||||
|
supported_color_properties: dict[str, tuple[dict, list]]
|
||||||
|
|
||||||
|
|
||||||
class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorData]):
|
class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorData]):
|
||||||
@ -49,7 +50,7 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorDat
|
|||||||
self.new_devices: set[str] = set()
|
self.new_devices: set[str] = set()
|
||||||
self.new_templates: set[str] = set()
|
self.new_templates: set[str] = set()
|
||||||
|
|
||||||
self.data = FritzboxCoordinatorData({}, {})
|
self.data = FritzboxCoordinatorData({}, {}, {})
|
||||||
|
|
||||||
async def async_setup(self) -> None:
|
async def async_setup(self) -> None:
|
||||||
"""Set up the coordinator."""
|
"""Set up the coordinator."""
|
||||||
@ -120,6 +121,7 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorDat
|
|||||||
|
|
||||||
devices = self.fritz.get_devices()
|
devices = self.fritz.get_devices()
|
||||||
device_data = {}
|
device_data = {}
|
||||||
|
supported_color_properties = self.data.supported_color_properties
|
||||||
for device in devices:
|
for device in devices:
|
||||||
# assume device as unavailable, see #55799
|
# assume device as unavailable, see #55799
|
||||||
if (
|
if (
|
||||||
@ -136,6 +138,13 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorDat
|
|||||||
|
|
||||||
device_data[device.ain] = device
|
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 = {}
|
template_data = {}
|
||||||
if self.has_templates:
|
if self.has_templates:
|
||||||
templates = self.fritz.get_templates()
|
templates = self.fritz.get_templates()
|
||||||
@ -145,7 +154,11 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorDat
|
|||||||
self.new_devices = device_data.keys() - self.data.devices.keys()
|
self.new_devices = device_data.keys() - self.data.devices.keys()
|
||||||
self.new_templates = template_data.keys() - self.data.templates.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:
|
async def _async_update_data(self) -> FritzboxCoordinatorData:
|
||||||
"""Fetch all device data."""
|
"""Fetch all device data."""
|
||||||
|
@ -57,7 +57,6 @@ class FritzboxLight(FritzBoxDeviceEntity, LightEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the FritzboxLight entity."""
|
"""Initialize the FritzboxLight entity."""
|
||||||
super().__init__(coordinator, ain, None)
|
super().__init__(coordinator, ain, None)
|
||||||
self._supported_hs: dict[int, list[int]] = {}
|
|
||||||
|
|
||||||
self._attr_supported_color_modes = {ColorMode.ONOFF}
|
self._attr_supported_color_modes = {ColorMode.ONOFF}
|
||||||
if self.data.has_color:
|
if self.data.has_color:
|
||||||
@ -65,6 +64,26 @@ class FritzboxLight(FritzBoxDeviceEntity, LightEntity):
|
|||||||
elif self.data.has_level:
|
elif self.data.has_level:
|
||||||
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
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
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""If the light is currently on or off."""
|
"""If the light is currently on or off."""
|
||||||
@ -148,30 +167,3 @@ class FritzboxLight(FritzBoxDeviceEntity, LightEntity):
|
|||||||
"""Turn the light off."""
|
"""Turn the light off."""
|
||||||
await self.hass.async_add_executor_job(self.data.set_state_off)
|
await self.hass.async_add_executor_job(self.data.set_state_off)
|
||||||
await self.coordinator.async_refresh()
|
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]),
|
|
||||||
]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user