Skip to content
Merged
Changes from all commits
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
46 changes: 31 additions & 15 deletions homeassistant/components/homematic/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_TRANSITION,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_HS,
SUPPORT_EFFECT,
SUPPORT_TRANSITION,
LightEntity,
Expand All @@ -21,8 +21,6 @@
from .const import ATTR_DISCOVER_DEVICES
from .entity import HMDevice

SUPPORT_HOMEMATIC = SUPPORT_BRIGHTNESS


def setup_platform(
hass: HomeAssistant,
Expand Down Expand Up @@ -62,30 +60,48 @@ def is_on(self):
return False

@property
def supported_features(self):
"""Flag supported features."""
features = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
def color_mode(self) -> str:
"""Return the color mode of the light."""
if "COLOR" in self._hmdevice.WRITENODE:
return COLOR_MODE_HS
if hasattr(self._hmdevice, "get_color_temp"):
return COLOR_MODE_COLOR_TEMP
return COLOR_MODE_BRIGHTNESS

@property
def supported_color_modes(self) -> set[str] | None:
"""Flag supported color modes."""
color_modes = set()

if "COLOR" in self._hmdevice.WRITENODE:
features |= SUPPORT_COLOR
color_modes.add(COLOR_MODE_HS)
if hasattr(self._hmdevice, "get_color_temp"):
color_modes.add(COLOR_MODE_COLOR_TEMP)
if not color_modes:
color_modes.add(COLOR_MODE_BRIGHTNESS)

return color_modes
Comment on lines +63 to +83
Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 4, 2022

Choose a reason for hiding this comment

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

This code is assuming:

  • A homematic light's features can dynamically change, and is not necessarily known when the HMLight object instantiated
  • There are homematic lights which can support both hs color and color temperature

Looking at the code in pyhomematic, I think neither of this is true; IPKeySwitchLevel and ColorEffectLight supports hs color, but not color temperature, ColdWarmDimmer supports color temperature but not hs color and there's no light supporting both.

@danielperna84, @pvizeli can you confirm this is the case?

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.

I don't know about the second one but the first one is wrong. Nothing is dynamic, all is known after the HMLight object is created


@property
def supported_features(self):
"""Flag supported features."""
features = SUPPORT_TRANSITION
if "PROGRAM" in self._hmdevice.WRITENODE:
features |= SUPPORT_EFFECT
if hasattr(self._hmdevice, "get_color_temp"):
features |= SUPPORT_COLOR_TEMP
return features

@property
def hs_color(self):
"""Return the hue and saturation color value [float, float]."""
if not self.supported_features & SUPPORT_COLOR:
if COLOR_MODE_HS not in self.supported_color_modes:
return None
hue, sat = self._hmdevice.get_hs_color(self._channel)
return hue * 360.0, sat * 100.0

@property
def color_temp(self):
"""Return the color temp in mireds [int]."""
if not self.supported_features & SUPPORT_COLOR_TEMP:
if COLOR_MODE_COLOR_TEMP not in self.supported_color_modes:
return None
hm_color_temp = self._hmdevice.get_color_temp(self._channel)
return self.max_mireds - (self.max_mireds - self.min_mireds) * hm_color_temp
Expand Down Expand Up @@ -119,7 +135,7 @@ def turn_on(self, **kwargs):
):
self._hmdevice.on(self._channel)

if ATTR_HS_COLOR in kwargs and self.supported_features & SUPPORT_COLOR:
if ATTR_HS_COLOR in kwargs:
self._hmdevice.set_hs_color(
hue=kwargs[ATTR_HS_COLOR][0] / 360.0,
saturation=kwargs[ATTR_HS_COLOR][1] / 100.0,
Expand All @@ -146,7 +162,7 @@ def _init_data_struct(self):
self._state = "LEVEL"
self._data[self._state] = None

if self.supported_features & SUPPORT_COLOR:
if COLOR_MODE_HS in self.supported_color_modes:
self._data.update({"COLOR": None})
if self.supported_features & SUPPORT_EFFECT:
self._data.update({"PROGRAM": None})