Compare commits

...

5 Commits

Author SHA1 Message Date
Wendelin 709c750abe Format 2026-05-19 12:50:50 +02:00
Wendelin 85014a469a add comment 2026-05-19 10:55:19 +02:00
Wendelin f6c11b5657 Update tests 2026-05-19 10:53:06 +02:00
Wendelin e17e823459 Use str instead of string 2026-05-19 10:52:25 +02:00
Wendelin a41cf33ffd Add comment attributes to automation items 2026-05-18 13:14:33 +02:00
3 changed files with 58 additions and 0 deletions
+1
View File
@@ -91,6 +91,7 @@ CONF_COMMAND_ON: Final = "command_on"
CONF_COMMAND_OPEN: Final = "command_open"
CONF_COMMAND_STATE: Final = "command_state"
CONF_COMMAND_STOP: Final = "command_stop"
CONF_COMMENT: Final = "comment"
CONF_CONDITION: Final = "condition"
CONF_CONDITIONS: Final = "conditions"
CONF_CONTINUE_ON_ERROR: Final = "continue_on_error"
@@ -38,6 +38,7 @@ from homeassistant.const import (
CONF_ATTRIBUTE,
CONF_BELOW,
CONF_CHOOSE,
CONF_COMMENT,
CONF_CONDITION,
CONF_CONDITIONS,
CONF_CONTINUE_ON_ERROR,
@@ -1458,6 +1459,7 @@ SCRIPT_SCHEMA = vol.All(ensure_list, [script_action])
SCRIPT_ACTION_BASE_SCHEMA: VolDictType = {
vol.Optional(CONF_ALIAS): string,
vol.Remove(CONF_COMMENT): str, # Is only used in frontend
vol.Optional(CONF_CONTINUE_ON_ERROR): boolean,
vol.Optional(CONF_ENABLED): vol.Any(boolean, template),
}
@@ -1525,6 +1527,7 @@ NUMERIC_STATE_THRESHOLD_SCHEMA = vol.Any(
CONDITION_BASE_SCHEMA: VolDictType = {
vol.Optional(CONF_ALIAS): string,
vol.Remove(CONF_COMMENT): str, # Is only used in frontend
vol.Optional(CONF_ENABLED): vol.Any(boolean, template),
}
@@ -1859,6 +1862,7 @@ TRIGGER_BASE_SCHEMA = vol.Schema(
vol.Optional(CONF_ID): str,
vol.Optional(CONF_VARIABLES): SCRIPT_VARIABLES_SCHEMA,
vol.Optional(CONF_ENABLED): vol.Any(boolean, template),
vol.Remove(CONF_COMMENT): str, # Is only used in frontend
}
)
+53
View File
@@ -1,6 +1,7 @@
"""Test config validators."""
from collections import OrderedDict
from collections.abc import Callable
from datetime import date, datetime, timedelta
import enum
from functools import partial
@@ -2031,3 +2032,55 @@ def test_stop_action_schema_error_false_with_response() -> None:
# no error with response_variable should work
config = schema({"stop": "Done", "response_variable": "result"})
assert config["response_variable"] == "result"
_COMMENT_SCHEMA_PARAMS = [
pytest.param(
cv.TRIGGER_BASE_SCHEMA,
{"platform": "event"},
id="trigger_base",
),
pytest.param(
cv.CONDITION_SCHEMA,
{"condition": "state", "entity_id": "sun.sun", "state": "above_horizon"},
id="condition",
),
pytest.param(
cv.script_action,
{"action": "test.foo"},
id="script_action",
),
]
@pytest.mark.parametrize(("validator", "base_config"), _COMMENT_SCHEMA_PARAMS)
@pytest.mark.usefixtures("hass")
def test_base_schemas_accept_comment(
validator: Callable[[dict[str, Any]], dict[str, Any]],
base_config: dict[str, Any],
) -> None:
"""Test that the comment field is accepted and stripped from the output."""
validated = validator({**base_config, "comment": "Single line"})
assert "comment" not in validated
@pytest.mark.parametrize(("validator", "base_config"), _COMMENT_SCHEMA_PARAMS)
@pytest.mark.parametrize(
"invalid_comment",
[
pytest.param(None, id="none"),
pytest.param(42, id="int"),
pytest.param(True, id="bool"),
pytest.param([], id="list"),
pytest.param({}, id="dict"),
],
)
@pytest.mark.usefixtures("hass")
def test_base_schemas_reject_invalid_comment(
validator: Callable[[dict[str, Any]], dict[str, Any]],
base_config: dict[str, Any],
invalid_comment: Any,
) -> None:
"""Test that script, condition, trigger base schemas reject non-string comments."""
with pytest.raises(vol.Invalid):
validator({**base_config, "comment": invalid_comment})