1 """Provides a light for Home Connect."""
3 from dataclasses
import dataclass
8 from homeconnect.api
import HomeConnectError
16 LightEntityDescription,
23 from .
import HomeConnectConfigEntry, get_dict_from_home_connect_error
24 from .api
import HomeConnectDevice
27 BSH_AMBIENT_LIGHT_BRIGHTNESS,
28 BSH_AMBIENT_LIGHT_COLOR,
29 BSH_AMBIENT_LIGHT_COLOR_CUSTOM_COLOR,
30 BSH_AMBIENT_LIGHT_CUSTOM_COLOR,
31 BSH_AMBIENT_LIGHT_ENABLED,
33 COOKING_LIGHTING_BRIGHTNESS,
35 REFRIGERATION_EXTERNAL_LIGHT_BRIGHTNESS,
36 REFRIGERATION_EXTERNAL_LIGHT_POWER,
37 REFRIGERATION_INTERNAL_LIGHT_BRIGHTNESS,
38 REFRIGERATION_INTERNAL_LIGHT_POWER,
39 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID,
41 from .entity
import HomeConnectEntity
43 _LOGGER = logging.getLogger(__name__)
46 @dataclass(frozen=True, kw_only=True)
48 """Light entity description."""
50 brightness_key: str |
None =
None
51 color_key: str |
None =
None
52 enable_custom_color_value_key: str |
None =
None
53 custom_color_key: str |
None =
None
54 brightness_scale: tuple[float, float] = (0.0, 100.0)
57 LIGHTS: tuple[HomeConnectLightEntityDescription, ...] = (
59 key=REFRIGERATION_INTERNAL_LIGHT_POWER,
60 brightness_key=REFRIGERATION_INTERNAL_LIGHT_BRIGHTNESS,
61 brightness_scale=(1.0, 100.0),
62 translation_key=
"internal_light",
65 key=REFRIGERATION_EXTERNAL_LIGHT_POWER,
66 brightness_key=REFRIGERATION_EXTERNAL_LIGHT_BRIGHTNESS,
67 brightness_scale=(1.0, 100.0),
68 translation_key=
"external_light",
72 brightness_key=COOKING_LIGHTING_BRIGHTNESS,
73 brightness_scale=(10.0, 100.0),
74 translation_key=
"cooking_lighting",
77 key=BSH_AMBIENT_LIGHT_ENABLED,
78 brightness_key=BSH_AMBIENT_LIGHT_BRIGHTNESS,
79 color_key=BSH_AMBIENT_LIGHT_COLOR,
80 enable_custom_color_value_key=BSH_AMBIENT_LIGHT_COLOR_CUSTOM_COLOR,
81 custom_color_key=BSH_AMBIENT_LIGHT_CUSTOM_COLOR,
82 brightness_scale=(10.0, 100.0),
83 translation_key=
"ambient_light",
90 entry: HomeConnectConfigEntry,
91 async_add_entities: AddEntitiesCallback,
93 """Set up the Home Connect light."""
96 """Get a list of entities."""
99 for description
in LIGHTS
100 for device
in entry.runtime_data.devices
101 if description.key
in device.appliance.status
108 """Light for Home Connect."""
110 entity_description: LightEntityDescription
113 self, device: HomeConnectDevice, desc: HomeConnectLightEntityDescription
115 """Initialize the entity."""
118 def get_setting_key_if_setting_exists(setting_key: str |
None) -> str |
None:
119 if setting_key
and setting_key
in device.appliance.status:
123 self.
_brightness_key_brightness_key = get_setting_key_if_setting_exists(desc.brightness_key)
125 desc.custom_color_key
127 self.
_color_key_color_key = get_setting_key_if_setting_exists(desc.color_key)
130 desc.custom_color_key
146 """Switch the light on, change brightness, change color."""
147 _LOGGER.debug(
"Switching light on for: %s", self.
namename)
149 await self.
hasshass.async_add_executor_job(
150 self.
devicedevice.appliance.set_setting, self.
bsh_keybsh_key,
True
152 except HomeConnectError
as err:
154 translation_domain=DOMAIN,
155 translation_key=
"turn_on_light",
156 translation_placeholders={
158 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
163 ATTR_RGB_COLOR
in kwargs
or ATTR_HS_COLOR
in kwargs
166 await self.
hasshass.async_add_executor_job(
167 self.
devicedevice.appliance.set_setting,
171 except HomeConnectError
as err:
173 translation_domain=DOMAIN,
174 translation_key=
"select_light_custom_color",
175 translation_placeholders={
177 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
181 if ATTR_RGB_COLOR
in kwargs:
182 hex_val = color_util.color_rgb_to_hex(*kwargs[ATTR_RGB_COLOR])
184 await self.
hasshass.async_add_executor_job(
185 self.
devicedevice.appliance.set_setting,
189 except HomeConnectError
as err:
191 translation_domain=DOMAIN,
192 translation_key=
"set_light_color",
193 translation_placeholders={
195 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
198 elif (ATTR_BRIGHTNESS
in kwargs
or ATTR_HS_COLOR
in kwargs)
and (
199 self.
_attr_brightness_attr_brightness
is not None or ATTR_BRIGHTNESS
in kwargs
201 brightness = 10 + ceil(
202 color_util.brightness_to_value(
208 hs_color = kwargs.get(ATTR_HS_COLOR, self.
_attr_hs_color_attr_hs_color)
210 if hs_color
is not None:
211 rgb = color_util.color_hsv_to_RGB(
212 hs_color[0], hs_color[1], brightness
214 hex_val = color_util.color_rgb_to_hex(*rgb)
216 await self.
hasshass.async_add_executor_job(
217 self.
devicedevice.appliance.set_setting,
221 except HomeConnectError
as err:
223 translation_domain=DOMAIN,
224 translation_key=
"set_light_color",
225 translation_placeholders={
227 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
231 elif self.
_brightness_key_brightness_key
and ATTR_BRIGHTNESS
in kwargs:
233 "Changing brightness for: %s, to: %s",
235 kwargs[ATTR_BRIGHTNESS],
238 color_util.brightness_to_value(
243 await self.
hasshass.async_add_executor_job(
246 except HomeConnectError
as err:
248 translation_domain=DOMAIN,
249 translation_key=
"set_light_brightness",
250 translation_placeholders={
252 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
259 """Switch the light off."""
260 _LOGGER.debug(
"Switching light off for: %s", self.
namename)
262 await self.
hasshass.async_add_executor_job(
263 self.
devicedevice.appliance.set_setting, self.
bsh_keybsh_key,
False
265 except HomeConnectError
as err:
267 translation_domain=DOMAIN,
268 translation_key=
"turn_off_light",
269 translation_placeholders={
271 SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.
entity_identity_id,
277 """Update the light's status."""
278 if self.
devicedevice.appliance.status.get(self.
bsh_keybsh_key, {}).
get(ATTR_VALUE)
is True:
281 self.
devicedevice.appliance.status.get(self.
bsh_keybsh_key, {}).
get(ATTR_VALUE)
is False
287 _LOGGER.debug(
"Updated, new light state: %s", self.
_attr_is_on_attr_is_on)
297 color_value = color.get(ATTR_VALUE)[1:]
298 rgb = color_util.rgb_hex_to_rgb_list(color_value)
300 hsv = color_util.color_RGB_to_hsv(*rgb)
306 "Updated, new color (%s) and new brightness (%s) ",
312 if brightness
is None:
318 _LOGGER.debug(
"Updated, new brightness: %s", self.
_attr_brightness_attr_brightness)
None async_entity_update(self)
None __init__(self, HomeConnectDevice device, HomeConnectLightEntityDescription desc)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
_attr_supported_color_modes
_enable_custom_color_value_key
str|UndefinedType|None name(self)
list[BaseAprilaireSensor] get_entities(type[BaseAprilaireSensor] entity_class, AprilaireCoordinator coordinator, str unique_id, tuple[AprilaireSensorDescription,...] descriptions)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, HomeConnectConfigEntry entry, AddEntitiesCallback async_add_entities)
dict[str, Any] get_dict_from_home_connect_error(api.HomeConnectError err)