Add test_supported_features and fix the problem introduced in #565 (#575)

* Update test_switch.py

* Update test_switch.py

* test is now done.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix the test only.

test is backwards compatible with the old method.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix _supported_to_attributes

everything works now.

* pre-commit fixes

cannot fix the `function too complex` problem.

* ignore test_switch.py in `pre-commit-config.yaml`

* Add ignore C901 to test_supported_features

* remove commented out code

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bas Nijholt <basnijholt@gmail.com>
Co-authored-by: Bas Nijholt <bas@nijho.lt>
This commit is contained in:
Benjamin Auquite 2023-04-27 14:56:49 -05:00 • committed by GitHub
commit 47c5ea93e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 15 deletions

View file

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