1 """Support for AVM FRITZ!SmartHome lightbulbs."""
3 from __future__
import annotations
5 from typing
import Any, cast
7 from requests.exceptions
import HTTPError
11 ATTR_COLOR_TEMP_KELVIN,
19 from .const
import COLOR_MODE, LOGGER
20 from .coordinator
import FritzboxConfigEntry, FritzboxDataUpdateCoordinator
21 from .entity
import FritzBoxDeviceEntity
26 entry: FritzboxConfigEntry,
27 async_add_entities: AddEntitiesCallback,
29 """Set up the FRITZ!SmartHome light from ConfigEntry."""
30 coordinator = entry.runtime_data
33 def _add_entities(devices: set[str] |
None =
None) ->
None:
36 devices = coordinator.new_devices
42 if coordinator.data.devices[ain].has_lightbulb
45 entry.async_on_unload(coordinator.async_add_listener(_add_entities))
47 _add_entities(set(coordinator.data.devices))
51 """The light class for FRITZ!SmartHome lightbulbs."""
55 coordinator: FritzboxDataUpdateCoordinator,
58 """Initialize the FritzboxLight entity."""
59 super().
__init__(coordinator, ain,
None)
60 self._supported_hs: dict[int, list[int]] = {}
70 """If the light is currently on or off."""
75 """Return the current Brightness."""
80 """Return the hs color value."""
84 return (hue,
float(saturation) * 100.0 / 255.0)
88 """Return the CT color value."""
93 """Return the color mode of the light."""
97 return ColorMode.COLOR_TEMP
99 return ColorMode.BRIGHTNESS
100 return ColorMode.ONOFF
103 """Turn the light on."""
104 if kwargs.get(ATTR_BRIGHTNESS)
is not None:
105 level = kwargs[ATTR_BRIGHTNESS]
107 if kwargs.get(ATTR_HS_COLOR)
is not None:
112 unmapped_hue =
int(kwargs[ATTR_HS_COLOR][0] % 360)
113 unmapped_saturation = round(
114 cast(float, kwargs[ATTR_HS_COLOR][1]) * 255.0 / 100.0
116 await self.
hasshasshass.async_add_executor_job(
117 self.
datadatadatadatadata.set_unmapped_color, (unmapped_hue, unmapped_saturation)
120 except HTTPError
as err:
121 if err.response.status_code != 400:
124 "fritzbox does not support method 'setunmappedcolor', fallback to"
129 self._supported_hs.keys(), key=
lambda x: abs(x - unmapped_hue)
132 self._supported_hs[hue],
133 key=
lambda x: abs(x - unmapped_saturation),
135 await self.
hasshasshass.async_add_executor_job(
139 if kwargs.get(ATTR_COLOR_TEMP_KELVIN)
is not None:
140 await self.
hasshasshass.async_add_executor_job(
148 """Turn the light off."""
153 """Get light attributes from device after entity is added to hass."""
156 def _get_color_data() -> tuple[dict, list]:
161 supported_color_temps,
162 ) = await self.
hasshasshass.async_add_executor_job(_get_color_data)
164 if supported_color_temps:
171 for values
in supported_colors.values():
172 hue =
int(values[0][0])
173 self._supported_hs[hue] = [
FritzhomeDevice data(self)
FritzhomeEntityBase data(self)
int color_temp_kelvin(self)
None async_turn_off(self, **Any kwargs)
_attr_min_color_temp_kelvin
tuple[float, float] hs_color(self)
None async_turn_on(self, **Any kwargs)
None async_added_to_hass(self)
None __init__(self, FritzboxDataUpdateCoordinator coordinator, str ain)
_attr_max_color_temp_kelvin
_attr_supported_color_modes
ColorMode color_mode(self)
None async_setup_entry(HomeAssistant hass, FritzboxConfigEntry entry, AddEntitiesCallback async_add_entities)