Remove deprecated white_value support from template light (#76923)

This commit is contained in:
Erik Montnemery 2022-08-17 13:53:56 +02:00 committed by GitHub
parent 4cc1428eea
commit 426a620084
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 159 deletions

View file

@ -12,12 +12,10 @@ from homeassistant.components.light import (
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_TRANSITION,
ATTR_WHITE_VALUE,
ENTITY_ID_FORMAT,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_WHITE_VALUE,
LightEntity,
LightEntityFeature,
)
@ -88,16 +86,14 @@ LIGHT_SCHEMA = vol.All(
vol.Optional(CONF_TEMPERATURE_TEMPLATE): cv.template,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_WHITE_VALUE_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_WHITE_VALUE_TEMPLATE): cv.template,
}
).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
)
PLATFORM_SCHEMA = vol.All(
# CONF_WHITE_VALUE_* is deprecated, support will be removed in release 2022.9
cv.deprecated(CONF_WHITE_VALUE_ACTION),
cv.deprecated(CONF_WHITE_VALUE_TEMPLATE),
cv.removed(CONF_WHITE_VALUE_ACTION),
cv.removed(CONF_WHITE_VALUE_TEMPLATE),
PLATFORM_SCHEMA.extend(
{vol.Required(CONF_LIGHTS): cv.schema_with_slug_keys(LIGHT_SCHEMA)}
),
@ -171,12 +167,6 @@ class LightTemplate(TemplateEntity, LightEntity):
if (color_action := config.get(CONF_COLOR_ACTION)) is not None:
self._color_script = Script(hass, color_action, friendly_name, DOMAIN)
self._color_template = config.get(CONF_COLOR_TEMPLATE)
self._white_value_script = None
if (white_value_action := config.get(CONF_WHITE_VALUE_ACTION)) is not None:
self._white_value_script = Script(
hass, white_value_action, friendly_name, DOMAIN
)
self._white_value_template = config.get(CONF_WHITE_VALUE_TEMPLATE)
self._effect_script = None
if (effect_action := config.get(CONF_EFFECT_ACTION)) is not None:
self._effect_script = Script(hass, effect_action, friendly_name, DOMAIN)
@ -190,7 +180,6 @@ class LightTemplate(TemplateEntity, LightEntity):
self._brightness = None
self._temperature = None
self._color = None
self._white_value = None
self._effect = None
self._effect_list = None
self._max_mireds = None
@ -223,11 +212,6 @@ class LightTemplate(TemplateEntity, LightEntity):
return super().min_mireds
@property
def white_value(self) -> int | None:
"""Return the white value."""
return self._white_value
@property
def hs_color(self) -> tuple[float, float] | None:
"""Return the hue and saturation color value [float, float]."""
@ -253,8 +237,6 @@ class LightTemplate(TemplateEntity, LightEntity):
supported_features |= SUPPORT_COLOR_TEMP
if self._color_script is not None:
supported_features |= SUPPORT_COLOR
if self._white_value_script is not None:
supported_features |= SUPPORT_WHITE_VALUE
if self._effect_script is not None:
supported_features |= LightEntityFeature.EFFECT
if self._supports_transition is True:
@ -312,14 +294,6 @@ class LightTemplate(TemplateEntity, LightEntity):
self._update_color,
none_on_template_error=True,
)
if self._white_value_template:
self.add_template_attribute(
"_white_value",
self._white_value_template,
None,
self._update_white_value,
none_on_template_error=True,
)
if self._effect_list_template:
self.add_template_attribute(
"_effect_list",
@ -361,13 +335,6 @@ class LightTemplate(TemplateEntity, LightEntity):
self._brightness = kwargs[ATTR_BRIGHTNESS]
optimistic_set = True
if self._white_value_template is None and ATTR_WHITE_VALUE in kwargs:
_LOGGER.debug(
"Optimistically setting white value to %s", kwargs[ATTR_WHITE_VALUE]
)
self._white_value = kwargs[ATTR_WHITE_VALUE]
optimistic_set = True
if self._temperature_template is None and ATTR_COLOR_TEMP in kwargs:
_LOGGER.debug(
"Optimistically setting color temperature to %s",
@ -404,14 +371,6 @@ class LightTemplate(TemplateEntity, LightEntity):
run_variables=common_params,
context=self._context,
)
elif ATTR_WHITE_VALUE in kwargs and self._white_value_script:
common_params["white_value"] = kwargs[ATTR_WHITE_VALUE]
await self.async_run_script(
self._white_value_script,
run_variables=common_params,
context=self._context,
)
elif ATTR_EFFECT in kwargs and self._effect_script:
effect = kwargs[ATTR_EFFECT]
if effect not in self._effect_list:
@ -486,29 +445,6 @@ class LightTemplate(TemplateEntity, LightEntity):
)
self._brightness = None
@callback
def _update_white_value(self, white_value):
"""Update the white value from the template."""
try:
if white_value in (None, "None", ""):
self._white_value = None
return
if 0 <= int(white_value) <= 255:
self._white_value = int(white_value)
else:
_LOGGER.error(
"Received invalid white value: %s for entity %s. Expected: 0-255",
white_value,
self.entity_id,
)
self._white_value = None
except ValueError:
_LOGGER.error(
"Template must supply an integer white_value from 0-255, or 'None'",
exc_info=True,
)
self._white_value = None
@callback
def _update_effect_list(self, effect_list):
"""Update the effect list from the template."""

View file

@ -9,12 +9,10 @@ from homeassistant.components.light import (
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_TRANSITION,
ATTR_WHITE_VALUE,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_TRANSITION,
SUPPORT_WHITE_VALUE,
ColorMode,
LightEntityFeature,
)
@ -616,97 +614,6 @@ async def test_off_action_optimistic(
assert state.attributes["supported_features"] == supported_features
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"supported_features,supported_color_modes,expected_color_mode",
[(SUPPORT_WHITE_VALUE, [ColorMode.RGBW], ColorMode.UNKNOWN)],
)
@pytest.mark.parametrize(
"light_config",
[
{
"test_template_light": {
**OPTIMISTIC_WHITE_VALUE_LIGHT_CONFIG,
"value_template": "{{1 == 1}}",
}
},
],
)
async def test_white_value_action_no_template(
hass,
setup_light,
calls,
supported_color_modes,
supported_features,
expected_color_mode,
):
"""Test setting white value with optimistic template."""
state = hass.states.get("light.test_template_light")
assert state.attributes.get("white_value") is None
await hass.services.async_call(
light.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_template_light", ATTR_WHITE_VALUE: 124},
blocking=True,
)
assert len(calls) == 1
assert calls[-1].data["action"] == "set_white_value"
assert calls[-1].data["caller"] == "light.test_template_light"
assert calls[-1].data["white_value"] == 124
state = hass.states.get("light.test_template_light")
assert state.attributes.get("white_value") == 124
assert state.state == STATE_ON
assert state.attributes["color_mode"] == expected_color_mode # hs_color is None
assert state.attributes["supported_color_modes"] == supported_color_modes
assert state.attributes["supported_features"] == supported_features
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"supported_features,supported_color_modes,expected_color_mode",
[(SUPPORT_WHITE_VALUE, [ColorMode.RGBW], ColorMode.UNKNOWN)],
)
@pytest.mark.parametrize(
"expected_white_value,white_value_template",
[
(255, "{{255}}"),
(None, "{{256}}"),
(None, "{{x-12}}"),
(None, "{{ none }}"),
(None, ""),
],
)
async def test_white_value_template(
hass,
expected_white_value,
supported_features,
supported_color_modes,
expected_color_mode,
count,
white_value_template,
):
"""Test the template for the white value."""
light_config = {
"test_template_light": {
**OPTIMISTIC_WHITE_VALUE_LIGHT_CONFIG,
"value_template": "{{ 1 == 1 }}",
"white_value_template": white_value_template,
}
}
await async_setup_light(hass, count, light_config)
state = hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("white_value") == expected_white_value
assert state.state == STATE_ON
assert state.attributes["color_mode"] == expected_color_mode # hs_color is None
assert state.attributes["supported_color_modes"] == supported_color_modes
assert state.attributes["supported_features"] == supported_features
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"supported_features,supported_color_modes,expected_color_mode",