Individual manual control of brightness and color (#1356)

* refactor: introduce light control parameter enum

* refactor: replace manual control flag with parameter enum

* test: update deprecated color temp attribute

* build: set execution bits on task scripts

* feat: individual manual control of brightness and color

* test: add tests for individual manual control evaluation

* fix: sequential manual changes not always detected

If multiple attributes of a light were changed within an interval, only the last change was detected because the check in the interval only used the latest event. For example, if there was a brightness change and a following color change, only the color attribute was detected as manually controlled. To fix this, the manual control attribute flags are now set directly from the event handler so that all events are processed.

* fix: invalid service description

* docs: fix missing space in config description

* refactor: pluralize multivalued bitmask enum name
This commit is contained in:
Mario Guggenberger 2025-12-23 07:55:16 +01:00 committed by GitHub
commit f84ee445b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 846 additions and 293 deletions

View file

@ -4,16 +4,26 @@ from unittest.mock import Mock
import pytest
from homeassistant.components.adaptive_lighting.adaptation_utils import (
LightControlAttributes,
ServiceData,
_create_service_call_data_iterator,
_has_relevant_service_data_attributes,
_remove_redundant_attributes,
_split_service_call_data,
get_light_control_attributes,
has_brightness_attribute,
has_color_attribute,
has_effect_attribute,
manual_control_event_attribute_to_flags,
prepare_adaptation_data,
)
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_TRANSITION,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_ON
@ -338,3 +348,135 @@ def fixture_hass_states_mock():
hass = Mock()
hass.states.get.return_value = Mock(attributes={ATTR_BRIGHTNESS: 10})
return hass
@pytest.mark.parametrize(
("attribute", "expected_str", "has_any", "has_none", "has_all"),
[
(LightControlAttributes.NONE, "NONE", False, True, False),
(LightControlAttributes.BRIGHTNESS, "BRIGHTNESS", True, False, False),
(LightControlAttributes.COLOR, "COLOR", True, False, False),
(
LightControlAttributes.BRIGHTNESS | LightControlAttributes.COLOR,
"BRIGHTNESS|COLOR",
True,
False,
True,
),
(
LightControlAttributes.ALL,
"BRIGHTNESS|COLOR",
True,
False,
True,
),
],
)
def test_light_control_attribute_flags(
attribute: LightControlAttributes,
expected_str: str,
has_any: bool,
has_none: bool,
has_all: bool,
):
"""Test helper methods and string conversion for the light attribute flag."""
assert str(attribute) == expected_str
assert attribute.has_any() is has_any
assert attribute.has_none() is has_none
assert attribute.has_all() is has_all
@pytest.mark.parametrize(
("manual_control_attribute", "expected_flag"),
[
(True, LightControlAttributes.ALL),
(False, LightControlAttributes.NONE),
("brightness", LightControlAttributes.BRIGHTNESS),
("color", LightControlAttributes.COLOR),
("unsupported", LightControlAttributes.NONE),
],
)
def test_manual_control_event_attribute_to_flags(
manual_control_attribute: bool | str,
expected_flag: LightControlAttributes,
):
"""Test mapping of manual control events to attribute flags."""
assert (
manual_control_event_attribute_to_flags(manual_control_attribute)
== expected_flag
)
@pytest.mark.parametrize(
("service_data", "expected"),
[
({ATTR_BRIGHTNESS: 125}, True),
({ATTR_BRIGHTNESS_PCT: 50}, True),
({ATTR_BRIGHTNESS_PCT: 50, ATTR_COLOR_TEMP_KELVIN: 3500}, True),
({ATTR_BRIGHTNESS_PCT: 50, "unknown": "foo"}, True),
({ATTR_COLOR_TEMP_KELVIN: 3500}, False),
({}, False),
],
)
def test_has_brightness_attribute(service_data: ServiceData, expected: bool):
"""Test detection of brightness attributes in service data."""
assert has_brightness_attribute(service_data) is expected
@pytest.mark.parametrize(
("service_data", "expected"),
[
({ATTR_HS_COLOR: (10, 20)}, True),
({ATTR_COLOR_TEMP_KELVIN: 5000}, True),
({ATTR_COLOR_TEMP_KELVIN: 5000, ATTR_BRIGHTNESS: 125}, True),
({ATTR_COLOR_TEMP_KELVIN: 5000, "unknown": "foo"}, True),
({ATTR_BRIGHTNESS: 125}, False),
({}, False),
],
)
def test_has_color_attribute(service_data: ServiceData, expected: bool):
"""Test detection of color attributes in service data."""
assert has_color_attribute(service_data) is expected
@pytest.mark.parametrize(
("service_data", "expected"),
[
({ATTR_EFFECT: "colorloop"}, True),
({ATTR_FLASH: "short"}, True),
({ATTR_EFFECT: "colorloop", ATTR_FLASH: "short"}, True),
({ATTR_EFFECT: "colorloop", "unknown": "foo"}, True),
({}, False),
],
)
def test_has_effect_attribute(service_data: ServiceData, expected: bool):
"""Test detection of effect attributes in service data."""
assert has_effect_attribute(service_data) is expected
@pytest.mark.parametrize(
("service_data", "expected_flags"),
[
({ATTR_BRIGHTNESS: 1}, LightControlAttributes.BRIGHTNESS),
({ATTR_HS_COLOR: (1, 2)}, LightControlAttributes.COLOR),
(
{ATTR_BRIGHTNESS: 1, ATTR_HS_COLOR: (1, 2)},
LightControlAttributes.BRIGHTNESS | LightControlAttributes.COLOR,
),
(
{ATTR_EFFECT: "colorloop"},
LightControlAttributes.BRIGHTNESS | LightControlAttributes.COLOR,
),
(
{ATTR_FLASH: "short"},
LightControlAttributes.BRIGHTNESS | LightControlAttributes.COLOR,
),
({}, LightControlAttributes.NONE),
],
)
def test_get_light_control_attributes(
service_data: ServiceData,
expected_flags: LightControlAttributes,
):
"""Test determination of light control attributes."""
assert get_light_control_attributes(service_data) == expected_flags