mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add conditional visibility to blueprint inputs (#187538)
This commit is contained in:
@@ -10,5 +10,6 @@ CONF_HOMEASSISTANT = "homeassistant"
|
||||
CONF_MIN_VERSION = "min_version"
|
||||
CONF_AUTHOR = "author"
|
||||
CONF_COLLAPSED = "collapsed"
|
||||
CONF_VISIBLE = "visible"
|
||||
|
||||
DOMAIN = "blueprint"
|
||||
|
||||
@@ -21,6 +21,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.field_visibility import get_hidden_fields
|
||||
from homeassistant.util import yaml as yaml_util
|
||||
|
||||
from .const import (
|
||||
@@ -31,6 +32,7 @@ from .const import (
|
||||
CONF_MIN_VERSION,
|
||||
CONF_SOURCE_URL,
|
||||
CONF_USE_BLUEPRINT,
|
||||
CONF_VISIBLE,
|
||||
DOMAIN,
|
||||
)
|
||||
from .errors import (
|
||||
@@ -103,6 +105,21 @@ class Blueprint:
|
||||
inputs[key] = value
|
||||
return inputs
|
||||
|
||||
@property
|
||||
def input_visibility(self) -> dict[str, Any]:
|
||||
"""Return the visible condition of each input, including its section's."""
|
||||
visibility = {}
|
||||
for key, value in self.data[CONF_BLUEPRINT][CONF_INPUT].items():
|
||||
if value and CONF_INPUT in value:
|
||||
for name, blueprint_input in value[CONF_INPUT].items():
|
||||
visibility[name] = _combine_visible(
|
||||
value.get(CONF_VISIBLE),
|
||||
(blueprint_input or {}).get(CONF_VISIBLE),
|
||||
)
|
||||
else:
|
||||
visibility[key] = _combine_visible((value or {}).get(CONF_VISIBLE))
|
||||
return visibility
|
||||
|
||||
@property
|
||||
def metadata(self) -> dict[str, Any]:
|
||||
"""Return blueprint metadata."""
|
||||
@@ -162,6 +179,16 @@ class BlueprintInputs:
|
||||
if isinstance(blueprint_input, dict) and CONF_DEFAULT in blueprint_input:
|
||||
inputs_with_default[inp] = blueprint_input[CONF_DEFAULT]
|
||||
|
||||
for inp in get_hidden_fields(
|
||||
self.blueprint.input_visibility, inputs_with_default
|
||||
):
|
||||
blueprint_input = self.blueprint.inputs[inp]
|
||||
inputs_with_default[inp] = (
|
||||
blueprint_input.get(CONF_DEFAULT)
|
||||
if isinstance(blueprint_input, dict)
|
||||
else None
|
||||
)
|
||||
|
||||
return inputs_with_default
|
||||
|
||||
def validate(self) -> None:
|
||||
@@ -382,3 +409,15 @@ class DomainBlueprints:
|
||||
)
|
||||
|
||||
await self.hass.async_add_executor_job(populate)
|
||||
|
||||
|
||||
def _combine_visible(*options: Any) -> bool | list[Any]:
|
||||
"""Combine visible options so all of them must match."""
|
||||
conditions: list[Any] = []
|
||||
for visible in options:
|
||||
if visible is None or visible is True:
|
||||
continue
|
||||
if visible is False:
|
||||
return False
|
||||
conditions.extend(visible if isinstance(visible, list) else [visible])
|
||||
return conditions
|
||||
|
||||
@@ -15,6 +15,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import config_validation as cv, selector
|
||||
from homeassistant.helpers.field_visibility import VISIBLE_SCHEMA
|
||||
|
||||
from .const import (
|
||||
CONF_AUTHOR,
|
||||
@@ -25,6 +26,7 @@ from .const import (
|
||||
CONF_MIN_VERSION,
|
||||
CONF_SOURCE_URL,
|
||||
CONF_USE_BLUEPRINT,
|
||||
CONF_VISIBLE,
|
||||
)
|
||||
|
||||
|
||||
@@ -89,6 +91,7 @@ BLUEPRINT_INPUT_SCHEMA = probatio.Schema(
|
||||
probatio.Optional(CONF_DESCRIPTION): str,
|
||||
probatio.Optional(CONF_DEFAULT): cv.match_all,
|
||||
probatio.Optional(CONF_SELECTOR): selector.validate_selector,
|
||||
probatio.Optional(CONF_VISIBLE): VISIBLE_SCHEMA,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -98,6 +101,7 @@ BLUEPRINT_INPUT_SECTION_SCHEMA = probatio.Schema(
|
||||
probatio.Optional(CONF_ICON): str,
|
||||
probatio.Optional(CONF_DESCRIPTION): str,
|
||||
probatio.Optional(CONF_COLLAPSED): bool,
|
||||
probatio.Optional(CONF_VISIBLE): VISIBLE_SCHEMA,
|
||||
probatio.Required(CONF_INPUT, default=dict): {
|
||||
str: probatio.Any(
|
||||
None,
|
||||
|
||||
@@ -449,3 +449,125 @@ async def test_inputs_from_config_nonexisting_blueprint(
|
||||
await domain_bps.async_inputs_from_config(
|
||||
{"use_blueprint": {"path": "non-existing.yaml"}}
|
||||
)
|
||||
|
||||
|
||||
def _visible_blueprint(inputs: dict[str, Any]) -> models.Blueprint:
|
||||
return models.Blueprint(
|
||||
{
|
||||
"blueprint": {"name": "Hello", "domain": "automation", "input": inputs},
|
||||
"dim_mode": Input("dim_mode"),
|
||||
"brightness": Input("brightness"),
|
||||
},
|
||||
schema=BLUEPRINT_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
def _resolve(blueprint: models.Blueprint, values: dict[str, Any]) -> dict[str, Any]:
|
||||
inputs = models.BlueprintInputs(
|
||||
blueprint, {"use_blueprint": {"path": "bla", "input": values}}
|
||||
)
|
||||
inputs.validate()
|
||||
return inputs.async_substitute()
|
||||
|
||||
|
||||
VISIBLE_BRIGHTNESS = {"field": "dim_mode", "value": "fixed"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("brightness", "values", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
{"visible": VISIBLE_BRIGHTNESS},
|
||||
{"dim_mode": "none"},
|
||||
{"dim_mode": "none", "brightness": None},
|
||||
id="hidden_without_default",
|
||||
),
|
||||
pytest.param(
|
||||
{"default": 50, "visible": VISIBLE_BRIGHTNESS},
|
||||
{"dim_mode": "none", "brightness": 80},
|
||||
{"dim_mode": "none", "brightness": 50},
|
||||
id="hidden_uses_default",
|
||||
),
|
||||
pytest.param(
|
||||
{"default": 50, "visible": VISIBLE_BRIGHTNESS},
|
||||
{"dim_mode": "fixed", "brightness": 80},
|
||||
{"dim_mode": "fixed", "brightness": 80},
|
||||
id="visible",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_blueprint_inputs_visible(
|
||||
brightness: dict[str, Any], values: dict[str, Any], expected: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test a hidden input resolves to its default, or None without one."""
|
||||
blueprint = _visible_blueprint({"dim_mode": None, "brightness": brightness})
|
||||
assert _resolve(blueprint, values) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"inputs",
|
||||
[
|
||||
pytest.param({"dim_mode": {}, "brightness": {}}, id="top_level"),
|
||||
pytest.param(
|
||||
{"dim_mode": {}, "dimming": {"input": {"brightness": {}}}},
|
||||
id="in_section",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_blueprint_inputs_empty_definition(inputs: dict[str, Any]) -> None:
|
||||
"""Test empty input definitions have no visible condition."""
|
||||
blueprint = _visible_blueprint(inputs)
|
||||
assert _resolve(blueprint, {"dim_mode": "none", "brightness": 80}) == {
|
||||
"dim_mode": "none",
|
||||
"brightness": 80,
|
||||
}
|
||||
|
||||
|
||||
def test_blueprint_inputs_visible_missing() -> None:
|
||||
"""Test a visible input without a value or default is still required."""
|
||||
blueprint = _visible_blueprint(
|
||||
{"dim_mode": None, "brightness": {"visible": VISIBLE_BRIGHTNESS}}
|
||||
)
|
||||
inputs = models.BlueprintInputs(
|
||||
blueprint, {"use_blueprint": {"path": "bla", "input": {"dim_mode": "fixed"}}}
|
||||
)
|
||||
with pytest.raises(errors.MissingInput):
|
||||
inputs.validate()
|
||||
|
||||
|
||||
def test_blueprint_inputs_visible_section() -> None:
|
||||
"""Test inputs of a hidden section resolve like hidden inputs."""
|
||||
blueprint = _visible_blueprint(
|
||||
{
|
||||
"dim_mode": None,
|
||||
"dimming": {
|
||||
"visible": VISIBLE_BRIGHTNESS,
|
||||
"input": {"brightness": None},
|
||||
},
|
||||
}
|
||||
)
|
||||
assert _resolve(blueprint, {"dim_mode": "none"}) == {
|
||||
"dim_mode": "none",
|
||||
"brightness": None,
|
||||
}
|
||||
assert _resolve(blueprint, {"dim_mode": "fixed", "brightness": 80}) == {
|
||||
"dim_mode": "fixed",
|
||||
"brightness": 80,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"visible",
|
||||
[{"value": 1}, {"field": "dim_mode", "operator": "gt"}, "yes"],
|
||||
)
|
||||
def test_blueprint_invalid_visible(visible: Any) -> None:
|
||||
"""Test an invalid visible condition is rejected."""
|
||||
with pytest.raises(errors.InvalidBlueprint):
|
||||
_visible_blueprint({"dim_mode": None, "brightness": {"visible": visible}})
|
||||
with pytest.raises(errors.InvalidBlueprint):
|
||||
_visible_blueprint(
|
||||
{
|
||||
"dim_mode": None,
|
||||
"dimming": {"visible": visible, "input": {"brightness": None}},
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user