Improve template sensor config flow validation (#99373)

This commit is contained in:
Erik Montnemery 2023-08-31 15:16:32 +02:00 committed by GitHub
parent 047f936d4c
commit f36a300651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 13 deletions

View File

@ -160,32 +160,43 @@ def _validate_unit(options: dict[str, Any]) -> None:
and (units := DEVICE_CLASS_UNITS.get(device_class)) is not None
and (unit := options.get(CONF_UNIT_OF_MEASUREMENT)) not in units
):
units_string = sorted(
[str(unit) if unit else "no unit of measurement" for unit in units],
sorted_units = sorted(
[f"'{str(unit)}'" if unit else "no unit of measurement" for unit in units],
key=str.casefold,
)
if len(sorted_units) == 1:
units_string = sorted_units[0]
else:
units_string = f"one of {', '.join(sorted_units)}"
raise vol.Invalid(
f"'{unit}' is not a valid unit for device class '{device_class}'; "
f"expected one of {', '.join(units_string)}"
f"expected {units_string}"
)
def _validate_state_class(options: dict[str, Any]) -> None:
"""Validate state class."""
if (
(device_class := options.get(CONF_DEVICE_CLASS))
(state_class := options.get(CONF_STATE_CLASS))
and (device_class := options.get(CONF_DEVICE_CLASS))
and (state_classes := DEVICE_CLASS_STATE_CLASSES.get(device_class)) is not None
and (state_class := options.get(CONF_STATE_CLASS)) not in state_classes
and state_class not in state_classes
):
state_classes_string = sorted(
[str(state_class) for state_class in state_classes],
sorted_state_classes = sorted(
[f"'{str(state_class)}'" for state_class in state_classes],
key=str.casefold,
)
if len(sorted_state_classes) == 0:
state_classes_string = "no state class"
elif len(sorted_state_classes) == 1:
state_classes_string = sorted_state_classes[0]
else:
state_classes_string = f"one of {', '.join(sorted_state_classes)}"
raise vol.Invalid(
f"'{state_class}' is not a valid state class for device class "
f"'{device_class}'; expected one of {', '.join(state_classes_string)}"
f"'{device_class}'; expected {state_classes_string}"
)

View File

@ -349,18 +349,62 @@ EARLY_END_ERROR = "invalid template (TemplateSyntaxError: unexpected 'end of tem
[
("binary_sensor", "{{", {}, {"state": EARLY_END_ERROR}),
("sensor", "{{", {}, {"state": EARLY_END_ERROR}),
(
"sensor",
"",
{"device_class": "aqi", "unit_of_measurement": "cats"},
{
"unit_of_measurement": (
"'cats' is not a valid unit for device class 'aqi'; "
"expected no unit of measurement"
),
},
),
(
"sensor",
"",
{"device_class": "temperature", "unit_of_measurement": "cats"},
{
"state_class": (
"'None' is not a valid state class for device class 'temperature'; "
"expected one of measurement"
),
"unit_of_measurement": (
"'cats' is not a valid unit for device class 'temperature'; "
"expected one of K, °C, °F"
"expected one of 'K', '°C', '°F'"
),
},
),
(
"sensor",
"",
{"device_class": "timestamp", "state_class": "measurement"},
{
"state_class": (
"'measurement' is not a valid state class for device class "
"'timestamp'; expected no state class"
),
},
),
(
"sensor",
"",
{"device_class": "aqi", "state_class": "total"},
{
"state_class": (
"'total' is not a valid state class for device class "
"'aqi'; expected 'measurement'"
),
},
),
(
"sensor",
"",
{"device_class": "energy", "state_class": "measurement"},
{
"state_class": (
"'measurement' is not a valid state class for device class "
"'energy'; expected one of 'total', 'total_increasing'"
),
"unit_of_measurement": (
"'None' is not a valid unit for device class 'energy'; "
"expected one of 'GJ', 'kWh', 'MJ', 'MWh', 'Wh'"
),
},
),