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

Add color_mode white #51411

Merged
merged 4 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 18 additions & 13 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
COLOR_MODE_RGB = "rgb"
COLOR_MODE_RGBW = "rgbw"
COLOR_MODE_RGBWW = "rgbww"
COLOR_MODE_WHITE = "white" # Must *NOT* be the only supported mode

VALID_COLOR_MODES = {
COLOR_MODE_ONOFF,
Expand All @@ -71,6 +72,7 @@
COLOR_MODE_RGB,
COLOR_MODE_RGBW,
COLOR_MODE_RGBWW,
COLOR_MODE_WHITE,
}
COLOR_MODES_BRIGHTNESS = VALID_COLOR_MODES - {COLOR_MODE_ONOFF}
COLOR_MODES_COLOR = {
Expand All @@ -90,6 +92,7 @@ def valid_supported_color_modes(color_modes: Iterable[str]) -> set[str]:
or COLOR_MODE_UNKNOWN in color_modes
or (COLOR_MODE_BRIGHTNESS in color_modes and len(color_modes) > 1)
or (COLOR_MODE_ONOFF in color_modes and len(color_modes) > 1)
or (COLOR_MODE_WHITE in color_modes and len(color_modes) == 1)
):
raise vol.Error(f"Invalid supported_color_modes {sorted(color_modes)}")
return color_modes
Expand Down Expand Up @@ -151,6 +154,7 @@ def get_supported_color_modes(hass: HomeAssistant, entity_id: str) -> set | None
ATTR_MAX_MIREDS = "max_mireds"
ATTR_COLOR_NAME = "color_name"
ATTR_WHITE_VALUE = "white_value"
ATTR_WHITE = "white"

# Brightness of the light, 0..255 or percentage
ATTR_BRIGHTNESS = "brightness"
Expand Down Expand Up @@ -195,6 +199,19 @@ def get_supported_color_modes(hass: HomeAssistant, entity_id: str) -> set | None
vol.Exclusive(ATTR_BRIGHTNESS_STEP, ATTR_BRIGHTNESS): VALID_BRIGHTNESS_STEP,
vol.Exclusive(ATTR_BRIGHTNESS_STEP_PCT, ATTR_BRIGHTNESS): VALID_BRIGHTNESS_STEP_PCT,
vol.Exclusive(ATTR_COLOR_NAME, COLOR_GROUP): cv.string,
vol.Exclusive(ATTR_COLOR_TEMP, COLOR_GROUP): vol.All(
vol.Coerce(int), vol.Range(min=1)
),
vol.Exclusive(ATTR_KELVIN, COLOR_GROUP): cv.positive_int,
vol.Exclusive(ATTR_HS_COLOR, COLOR_GROUP): vol.All(
vol.ExactSequence(
(
vol.All(vol.Coerce(float), vol.Range(min=0, max=360)),
vol.All(vol.Coerce(float), vol.Range(min=0, max=100)),
)
),
vol.Coerce(tuple),
),
vol.Exclusive(ATTR_RGB_COLOR, COLOR_GROUP): vol.All(
vol.ExactSequence((cv.byte,) * 3), vol.Coerce(tuple)
),
Expand All @@ -207,19 +224,7 @@ def get_supported_color_modes(hass: HomeAssistant, entity_id: str) -> set | None
vol.Exclusive(ATTR_XY_COLOR, COLOR_GROUP): vol.All(
vol.ExactSequence((cv.small_float, cv.small_float)), vol.Coerce(tuple)
),
vol.Exclusive(ATTR_HS_COLOR, COLOR_GROUP): vol.All(
vol.ExactSequence(
(
vol.All(vol.Coerce(float), vol.Range(min=0, max=360)),
vol.All(vol.Coerce(float), vol.Range(min=0, max=100)),
)
),
vol.Coerce(tuple),
),
vol.Exclusive(ATTR_COLOR_TEMP, COLOR_GROUP): vol.All(
vol.Coerce(int), vol.Range(min=1)
),
vol.Exclusive(ATTR_KELVIN, COLOR_GROUP): cv.positive_int,
vol.Exclusive(ATTR_WHITE, COLOR_GROUP): True,
ATTR_WHITE_VALUE: vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
ATTR_FLASH: VALID_FLASH,
ATTR_EFFECT: cv.string,
Expand Down
30 changes: 26 additions & 4 deletions homeassistant/components/light/reproduce_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Iterable
import logging
from types import MappingProxyType
from typing import Any
from typing import Any, cast

from homeassistant.const import (
ATTR_ENTITY_ID,
Expand All @@ -31,6 +31,7 @@
ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR,
ATTR_TRANSITION,
ATTR_WHITE,
ATTR_WHITE_VALUE,
ATTR_XY_COLOR,
COLOR_MODE_COLOR_TEMP,
Expand All @@ -39,6 +40,7 @@
COLOR_MODE_RGBW,
COLOR_MODE_RGBWW,
COLOR_MODE_UNKNOWN,
COLOR_MODE_WHITE,
COLOR_MODE_XY,
DOMAIN,
)
Expand Down Expand Up @@ -78,6 +80,8 @@
COLOR_MODE_XY: ATTR_XY_COLOR,
}

COLOR_MODE_TO_PARAMETER = {COLOR_MODE_WHITE: (ATTR_WHITE, True)}

DEPRECATED_GROUP = [
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_NAME,
Expand All @@ -93,6 +97,17 @@
)


def _color_mode_same(cur_state: State, state: State) -> bool:
"""Test if color_mode is same."""
cur_color_mode = cur_state.attributes.get(ATTR_COLOR_MODE, COLOR_MODE_UNKNOWN)
saved_color_mode = state.attributes.get(ATTR_COLOR_MODE, COLOR_MODE_UNKNOWN)

# Guard for scenes etc. which where created before color modes were introduced
if saved_color_mode == COLOR_MODE_UNKNOWN:
return True
return cast(bool, cur_color_mode == saved_color_mode)
Copy link
Contributor Author

@emontnemery emontnemery Jun 3, 2021

Choose a reason for hiding this comment

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

I'm not entirely sure why this is needed, is it in case __eq__ is overridden to not return a bool?
Would it be more correct to hint that cur_color_mode and saved_color_mode are strings?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, if typing the attributes avoids the cast, that's better.



async def _async_reproduce_state(
hass: HomeAssistant,
state: State,
Expand All @@ -119,9 +134,13 @@ async def _async_reproduce_state(
_LOGGER.warning(DEPRECATION_WARNING, deprecated_attrs)

# Return if we are already at the right state.
if cur_state.state == state.state and all(
check_attr_equal(cur_state.attributes, state.attributes, attr)
for attr in ATTR_GROUP + COLOR_GROUP
if (
cur_state.state == state.state
and _color_mode_same(cur_state, state)
and all(
check_attr_equal(cur_state.attributes, state.attributes, attr)
for attr in ATTR_GROUP + COLOR_GROUP
)
):
return

Expand Down Expand Up @@ -154,6 +173,9 @@ async def _async_reproduce_state(
)
return
service_data[color_attr] = state.attributes[color_attr]
if color_key_value := COLOR_MODE_TO_PARAMETER.get(color_mode):
color_parameter, color_value = color_key_value
service_data[color_parameter] = color_value
else:
# Fall back to Choosing the first color that is specified
for color_attr in COLOR_GROUP:
Expand Down
2 changes: 2 additions & 0 deletions tests/components/light/test_reproduce_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async def test_reproducing_states(hass, caplog):
light.COLOR_MODE_RGBW,
light.COLOR_MODE_RGBWW,
light.COLOR_MODE_UNKNOWN,
light.COLOR_MODE_WHITE,
light.COLOR_MODE_XY,
),
)
Expand Down Expand Up @@ -205,6 +206,7 @@ async def test_filter_color_modes(hass, caplog, color_mode):
light.COLOR_MODE_RGBW: VALID_RGBW_COLOR,
light.COLOR_MODE_RGBWW: VALID_RGBWW_COLOR,
light.COLOR_MODE_UNKNOWN: {**VALID_HS_COLOR, **VALID_WHITE_VALUE},
light.COLOR_MODE_WHITE: {light.ATTR_WHITE: True},
light.COLOR_MODE_XY: VALID_XY_COLOR,
}
expected = expected_map[color_mode]
Expand Down