Skip to content
Merged
Changes from 1 commit
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
29 changes: 13 additions & 16 deletions homeassistant/components/tuya/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_HS,
COLOR_MODE_ONOFF,
ColorMode,
LightEntity,
LightEntityDescription,
)
Expand Down Expand Up @@ -401,7 +398,7 @@ def __init__(
super().__init__(device, device_manager)
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
self._attr_supported_color_modes = {COLOR_MODE_ONOFF}
self._attr_supported_color_modes = {ColorMode.ONOFF}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs say we shouldn't add ONOFF unless no other color modes have been added

https://developers.home-assistant.io/docs/core/entity/light?_highlight=color#color-modes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have adjusted the logic to start with an empty set, and only add ONOFF if it is still empty after initialisation.


# Determine DPCodes
self._color_mode_dpcode = self.find_dpcode(
Expand All @@ -412,7 +409,7 @@ def __init__(
description.brightness, dptype=DPType.INTEGER, prefer_function=True
):
self._brightness = int_type
self._attr_supported_color_modes.add(COLOR_MODE_BRIGHTNESS)
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
self._brightness_max = self.find_dpcode(
description.brightness_max, dptype=DPType.INTEGER
)
Expand All @@ -424,13 +421,13 @@ def __init__(
description.color_temp, dptype=DPType.INTEGER, prefer_function=True
):
self._color_temp = int_type
self._attr_supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)

if (
dpcode := self.find_dpcode(description.color_data, prefer_function=True)
) and self.get_dptype(dpcode) == DPType.JSON:
self._color_data_dpcode = dpcode
self._attr_supported_color_modes.add(COLOR_MODE_HS)
self._attr_supported_color_modes.add(ColorMode.HS)
if dpcode in self.device.function:
values = cast(str, self.device.function[dpcode].values)
else:
Expand Down Expand Up @@ -484,7 +481,7 @@ def turn_on(self, **kwargs: Any) -> None:
]
elif self._color_data_type and (
ATTR_HS_COLOR in kwargs
or (ATTR_BRIGHTNESS in kwargs and self.color_mode == COLOR_MODE_HS)
or (ATTR_BRIGHTNESS in kwargs and self.color_mode == ColorMode.HS)
):
if self._color_mode_dpcode:
commands += [
Expand Down Expand Up @@ -527,7 +524,7 @@ def turn_on(self, **kwargs: Any) -> None:

if (
ATTR_BRIGHTNESS in kwargs
and self.color_mode != COLOR_MODE_HS
and self.color_mode != ColorMode.HS
and self._brightness
):
brightness = kwargs[ATTR_BRIGHTNESS]
Expand Down Expand Up @@ -578,7 +575,7 @@ def turn_off(self, **kwargs: Any) -> None:
def brightness(self) -> int | None:
"""Return the brightness of the light."""
# If the light is currently in color mode, extract the brightness from the color data
if self.color_mode == COLOR_MODE_HS and (color_data := self._get_color_data()):
if self.color_mode == ColorMode.HS and (color_data := self._get_color_data()):
return color_data.brightness

if not self._brightness:
Expand Down Expand Up @@ -640,20 +637,20 @@ def hs_color(self) -> tuple[float, float] | None:
return color_data.hs_color

@property
def color_mode(self) -> str:
def color_mode(self) -> ColorMode:
"""Return the color_mode of the light."""
# We consider it to be in HS color mode, when work mode is anything
# else than "white".
if (
self._color_mode_dpcode
and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE
):
return COLOR_MODE_HS
return ColorMode.HS
if self._color_temp:
return COLOR_MODE_COLOR_TEMP
return ColorMode.COLOR_TEMP
if self._brightness:
return COLOR_MODE_BRIGHTNESS
return COLOR_MODE_ONOFF
return ColorMode.BRIGHTNESS
return ColorMode.ONOFF

def _get_color_data(self) -> ColorData | None:
"""Get current color data from device."""
Expand Down