Merge branch 'main' into add-watched-lights

This commit is contained in:
Bas Nijholt 2023-04-27 12:59:37 -07:00 • committed by GitHub
commit ea73228ef8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 15 deletions

View file

@ -157,12 +157,13 @@ from .const import (
) )
_SUPPORT_OPTS = { _SUPPORT_OPTS = {
"brightness": SUPPORT_BRIGHTNESS, COLOR_MODE_BRIGHTNESS: SUPPORT_BRIGHTNESS,
"color_temp": SUPPORT_COLOR_TEMP, COLOR_MODE_COLOR_TEMP: SUPPORT_COLOR_TEMP,
"color": SUPPORT_COLOR, CONST_COLOR: SUPPORT_COLOR,
"transition": SUPPORT_TRANSITION, ATTR_TRANSITION: SUPPORT_TRANSITION,
} }
VALID_COLOR_MODES = { VALID_COLOR_MODES = {
COLOR_MODE_BRIGHTNESS: ATTR_BRIGHTNESS, COLOR_MODE_BRIGHTNESS: ATTR_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP: ATTR_COLOR_TEMP_KELVIN, COLOR_MODE_COLOR_TEMP: ATTR_COLOR_TEMP_KELVIN,
@ -644,16 +645,18 @@ def _expand_light_groups(hass: HomeAssistant, lights: list[str]) -> list[str]:
def _supported_to_attributes(supported): def _supported_to_attributes(supported):
supported_attributes = {} supported_attributes = {}
supports_colors = False supports_colors = False
for mode, attr in VALID_COLOR_MODES.items(): for mode in supported:
if mode not in supported: attr = VALID_COLOR_MODES.get(mode)
continue if attr:
supported_attributes[attr] = True supported_attributes[attr] = True
if ( if attr in COLOR_ATTRS:
not supports_colors supports_colors = True
and mode != COLOR_MODE_BRIGHTNESS # ATTR_SUPPORTED_FEATURES only
and mode != COLOR_MODE_COLOR_TEMP elif mode in _SUPPORT_OPTS:
): supported_attributes[mode] = True
supports_colors = True if CONST_COLOR in supported_attributes:
supports_colors = True
supported_attributes.pop(CONST_COLOR)
return supported_attributes, supports_colors return supported_attributes, supports_colors

View file

@ -3,9 +3,10 @@
import asyncio import asyncio
from copy import deepcopy from copy import deepcopy
import datetime import datetime
import itertools
import logging import logging
from random import randint from random import randint
from unittest.mock import patch from unittest.mock import MagicMock, patch
from homeassistant.components.adaptive_lighting.const import ( from homeassistant.components.adaptive_lighting.const import (
ADAPT_BRIGHTNESS_SWITCH, ADAPT_BRIGHTNESS_SWITCH,
@ -27,6 +28,7 @@ from homeassistant.components.adaptive_lighting.const import (
CONF_TRANSITION, CONF_TRANSITION,
CONF_TURN_ON_LIGHTS, CONF_TURN_ON_LIGHTS,
CONF_USE_DEFAULTS, CONF_USE_DEFAULTS,
CONST_COLOR,
DEFAULT_MAX_BRIGHTNESS, DEFAULT_MAX_BRIGHTNESS,
DEFAULT_NAME, DEFAULT_NAME,
DEFAULT_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS,
@ -39,7 +41,10 @@ from homeassistant.components.adaptive_lighting.const import (
UNDO_UPDATE_LISTENER, UNDO_UPDATE_LISTENER,
) )
from homeassistant.components.adaptive_lighting.switch import ( from homeassistant.components.adaptive_lighting.switch import (
_SUPPORT_OPTS,
VALID_COLOR_MODES,
_attributes_have_changed, _attributes_have_changed,
_supported_features,
color_difference_redmean, color_difference_redmean,
create_context, create_context,
is_our_context, is_our_context,
@ -49,9 +54,13 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT, ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_TEMP_KELVIN, ATTR_COLOR_TEMP_KELVIN,
ATTR_MAX_COLOR_TEMP_KELVIN,
ATTR_MIN_COLOR_TEMP_KELVIN,
ATTR_RGB_COLOR, ATTR_RGB_COLOR,
ATTR_SUPPORTED_COLOR_MODES,
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_XY_COLOR, ATTR_XY_COLOR,
COLOR_MODE_BRIGHTNESS,
) )
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.components.light import SERVICE_TURN_OFF from homeassistant.components.light import SERVICE_TURN_OFF
@ -519,6 +528,79 @@ async def test_turn_on_off_listener_not_tracking_untracked_lights(hass):
assert light not in switch.turn_on_off_listener.lights assert light not in switch.turn_on_off_listener.lights
def test_supported_features(hass): # noqa: C901
"""Test the supported features of a light."""
possible_legacy_features = {}
MAX_COMBINATIONS = 4 # maximum number of elements that can be combined
for i in range(1, min(MAX_COMBINATIONS, len(_SUPPORT_OPTS)) + 1):
for combination in itertools.combinations(_SUPPORT_OPTS.keys(), i):
key = "_".join(combination)
value = [v for k, v in _SUPPORT_OPTS.items() if k in combination]
possible_legacy_features[key] = value
possible_color_modes = {}
for i in range(1, len(VALID_COLOR_MODES) + 1):
for combination in itertools.combinations(VALID_COLOR_MODES.keys(), i):
key = "_".join(combination)
value = [v for k, v in VALID_COLOR_MODES.items() if k in combination]
possible_color_modes[key] = value
# create a mock HomeAssistant object
hass = MagicMock()
# iterate over possible legacy features
for feature_key, feature_values in possible_legacy_features.items():
# _LOGGER.debug(feature_values)
# set the attributes of the mock state object to the possible legacy feature values
state_attrs = {ATTR_SUPPORTED_FEATURES: sum(feature_values)}
hass.states.get.return_value.attributes = state_attrs
# iterate over possible color modes
for mode_key, mode_values in possible_color_modes.items():
# _LOGGER.debug(mode_values)
# set the attributes of the mock state object to the possible color mode values
state_attrs[ATTR_SUPPORTED_COLOR_MODES] = set(mode_values)
hass.states.get.return_value.attributes = state_attrs
# Handle both the new and the old _supported_features.
result = _supported_features(hass, ENTITY_LIGHT)
supported, supports_colors = (
result if isinstance(result, tuple) else (result, None)
)
expected_supported = {} if supports_colors is not None else set()
for mode, attr in VALID_COLOR_MODES.items():
if mode in mode_values:
if supports_colors is None:
expected_supported.add(mode)
else:
expected_supported[attr] = True
if supports_colors is True:
expected_supported[COLOR_MODE_BRIGHTNESS] = True
for opt, value in _SUPPORT_OPTS.items():
if value in feature_values:
if supports_colors is None:
expected_supported.add(opt)
else:
if supports_colors is True:
expected_supported[COLOR_MODE_BRIGHTNESS] = True
if opt in VALID_COLOR_MODES:
expected_supported[VALID_COLOR_MODES[opt]] = True
elif opt != CONST_COLOR:
expected_supported[opt] = True
if ATTR_MIN_COLOR_TEMP_KELVIN in supported:
supported.pop(ATTR_MIN_COLOR_TEMP_KELVIN)
if ATTR_MAX_COLOR_TEMP_KELVIN in supported:
supported.pop(ATTR_MAX_COLOR_TEMP_KELVIN)
assert supported == expected_supported, (
f"\nExpected supported: {expected_supported}\n"
f"Actual supported: {supported}\n"
f"feature_values: {feature_values}\n"
f"mode_values: {mode_values}\n"
f"supports_colors: {supports_colors}\n"
)
@pytest.mark.dependency(depends=GLOBAL_TEST_DEPENDENCIES) @pytest.mark.dependency(depends=GLOBAL_TEST_DEPENDENCIES)
async def test_manual_control(hass): async def test_manual_control(hass):
"""Test the 'manual control' tracking.""" """Test the 'manual control' tracking."""