Validate supported_color_modes for MQTT JSON light (#48836)
parent
e988062034
commit
5e8559e3cc
|
@ -73,6 +73,20 @@ VALID_COLOR_MODES = {
|
|||
COLOR_MODES_BRIGHTNESS = VALID_COLOR_MODES - {COLOR_MODE_ONOFF}
|
||||
COLOR_MODES_COLOR = {COLOR_MODE_HS, COLOR_MODE_RGB, COLOR_MODE_XY}
|
||||
|
||||
|
||||
def valid_supported_color_modes(color_modes):
|
||||
"""Validate the given color modes."""
|
||||
color_modes = set(color_modes)
|
||||
if (
|
||||
not color_modes
|
||||
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)
|
||||
):
|
||||
raise vol.Error(f"Invalid supported_color_modes {sorted(color_modes)}")
|
||||
return color_modes
|
||||
|
||||
|
||||
# Float that represents transition time in seconds to make change.
|
||||
ATTR_TRANSITION = "transition"
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ from homeassistant.components.light import (
|
|||
SUPPORT_WHITE_VALUE,
|
||||
VALID_COLOR_MODES,
|
||||
LightEntity,
|
||||
valid_supported_color_modes,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_BRIGHTNESS,
|
||||
|
@ -130,7 +131,10 @@ PLATFORM_SCHEMA_JSON = vol.All(
|
|||
vol.Optional(CONF_RGB, default=DEFAULT_RGB): cv.boolean,
|
||||
vol.Optional(CONF_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
||||
vol.Inclusive(CONF_SUPPORTED_COLOR_MODES, "color_mode"): vol.All(
|
||||
cv.ensure_list, [vol.In(VALID_COLOR_MODES)], vol.Unique()
|
||||
cv.ensure_list,
|
||||
[vol.In(VALID_COLOR_MODES)],
|
||||
vol.Unique(),
|
||||
valid_supported_color_modes,
|
||||
),
|
||||
vol.Optional(CONF_WHITE_VALUE, default=DEFAULT_WHITE_VALUE): cv.boolean,
|
||||
vol.Optional(CONF_XY, default=DEFAULT_XY): cv.boolean,
|
||||
|
|
|
@ -188,6 +188,33 @@ async def test_fail_setup_if_color_mode_deprecated(hass, mqtt_mock, deprecated):
|
|||
assert hass.states.get("light.test") is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"supported_color_modes", [["onoff", "rgb"], ["brightness", "rgb"], ["unknown"]]
|
||||
)
|
||||
async def test_fail_setup_if_color_modes_invalid(
|
||||
hass, mqtt_mock, supported_color_modes
|
||||
):
|
||||
"""Test if setup fails if supported color modes is invalid."""
|
||||
config = {
|
||||
light.DOMAIN: {
|
||||
"brightness": True,
|
||||
"color_mode": True,
|
||||
"command_topic": "test_light_rgb/set",
|
||||
"name": "test",
|
||||
"platform": "mqtt",
|
||||
"schema": "json",
|
||||
"supported_color_modes": supported_color_modes,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
light.DOMAIN,
|
||||
config,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("light.test") is None
|
||||
|
||||
|
||||
async def test_rgb_light(hass, mqtt_mock):
|
||||
"""Test RGB light flags brightness support."""
|
||||
assert await async_setup_component(
|
||||
|
|
Loading…
Reference in New Issue