Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix matter light color capabilities bit map #88693

Merged
merged 7 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
68 changes: 42 additions & 26 deletions homeassistant/components/matter/light.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Matter light."""
from __future__ import annotations

from enum import IntFlag
from typing import Any

from chip.clusters import Objects as clusters
Expand Down Expand Up @@ -78,6 +79,23 @@ def supports_brightness(self) -> bool:
return False
return ColorMode.BRIGHTNESS in self._attr_supported_color_modes

def supports_color_feature(self, feature: ColorControlCapabilities) -> bool:
ArturoGuerra marked this conversation as resolved.
Show resolved Hide resolved
"""Return if the device supports the given color feature."""
featuremap = self.get_matter_attribute_value(
ArturoGuerra marked this conversation as resolved.
Show resolved Hide resolved
clusters.ColorControl.Attributes.FeatureMap,
)

if featuremap is None:
return False

LOGGER.debug(
"Got featuremap %s for %s",
featuremap,
self.entity_id,
)

return bool(featuremap & feature == feature)

async def _set_xy_color(self, xy_color: tuple[float, float]) -> None:
"""Set xy color."""

Expand Down Expand Up @@ -260,11 +278,17 @@ async def async_turn_on(self, **kwargs: Any) -> None:
color_temp = kwargs.get(ATTR_COLOR_TEMP)
brightness = kwargs.get(ATTR_BRIGHTNESS)

if hs_color is not None and self.supports_color:
if hs_color is not None and self.supports_color_feature(
ColorControlCapabilities.HS
):
await self._set_hs_color(hs_color)
elif xy_color is not None:
elif xy_color is not None and self.supports_color_feature(
ColorControlCapabilities.XY
):
await self._set_xy_color(xy_color)
elif color_temp is not None and self.supports_color_temperature:
elif color_temp is not None and self.supports_color_feature(
ColorControlCapabilities.TEMP
):
await self._set_color_temp(color_temp)

if brightness is not None and self.supports_brightness:
Expand All @@ -284,7 +308,6 @@ async def async_turn_off(self, **kwargs: Any) -> None:
@callback
def _update_from_device(self) -> None:
"""Update from device."""

if self._attr_supported_color_modes is None:
# work out what (color)features are supported
supported_color_modes: set[ColorMode] = set()
Expand All @@ -297,30 +320,13 @@ def _update_from_device(self) -> None:
if self._entity_info.endpoint.has_attribute(
None, clusters.ColorControl.Attributes.ColorMode
):
# device has some color support, check which color modes
# are supported with the featuremap on the ColorControl cluster
color_feature_map = self.get_matter_attribute_value(
clusters.ColorControl.Attributes.FeatureMap,
)
if (
color_feature_map
& clusters.ColorControl.Attributes.CurrentHue.attribute_id
):
if self.supports_color_feature(ColorControlCapabilities.HS):
supported_color_modes.add(ColorMode.HS)
if (
color_feature_map
& clusters.ColorControl.Attributes.CurrentX.attribute_id
):

if self.supports_color_feature(ColorControlCapabilities.XY):
supported_color_modes.add(ColorMode.XY)

# color temperature support detection using the featuremap is not reliable
# (temporary?) fallback to checking the value
if (
self.get_matter_attribute_value(
clusters.ColorControl.Attributes.ColorTemperatureMireds
)
is not None
):
if self.supports_color_feature(ColorControlCapabilities.TEMP):
supported_color_modes.add(ColorMode.COLOR_TEMP)

self._attr_supported_color_modes = supported_color_modes
Expand Down Expand Up @@ -351,11 +357,21 @@ def _update_from_device(self) -> None:
self._attr_brightness = self._get_brightness()


class ColorControlCapabilities(IntFlag):
ArturoGuerra marked this conversation as resolved.
Show resolved Hide resolved
"""Color control capabilities bitmap."""

HS = 0x1
EHUE = 0x2
CL = 0x4
XY = 0x8
TEMP = 0x10
ArturoGuerra marked this conversation as resolved.
Show resolved Hide resolved


# Discovery schema(s) to map Matter Attributes to HA entities
DISCOVERY_SCHEMAS = [
MatterDiscoverySchema(
platform=Platform.LIGHT,
entity_description=LightEntityDescription(key="ExtendedMatterLight"),
entity_description=LightEntityDescription(key="MatterLight"),
entity_class=MatterLight,
required_attributes=(clusters.OnOff.Attributes.OnOff,),
optional_attributes=(
Expand Down
8 changes: 4 additions & 4 deletions tests/components/matter/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async def test_extended_color_light(
"turn_on",
{
"entity_id": entity_id,
"hs_color": (0, 0),
"hs_color": (236.69291338582678, 100.0),
},
blocking=True,
)
Expand All @@ -299,9 +299,9 @@ async def test_extended_color_light(
call(
node_id=1,
endpoint_id=1,
command=clusters.ColorControl.Commands.MoveToColor(
colorX=21168,
colorY=21561,
command=clusters.ColorControl.Commands.MoveToHueAndSaturation(
hue=167,
saturation=254,
transitionTime=0,
optionsMask=0,
optionsOverride=0,
Expand Down