Remove NONE_SENTINEL in favor of optional select in template (#101279)

pull/101815/head
Robert Resch 2023-10-11 13:04:40 +02:00 committed by GitHub
parent 8acb4dc1b6
commit 1a7601ebbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 59 deletions

View File

@ -37,8 +37,6 @@ from .const import DOMAIN
from .sensor import async_create_preview_sensor
from .template_entity import TemplateEntity
NONE_SENTINEL = "none"
def generate_schema(domain: str, flow_type: str) -> dict[vol.Marker, Any]:
"""Generate schema."""
@ -48,71 +46,50 @@ def generate_schema(domain: str, flow_type: str) -> dict[vol.Marker, Any]:
schema = {
vol.Optional(CONF_DEVICE_CLASS): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted(
[cls.value for cls in BinarySensorDeviceClass],
key=str.casefold,
),
],
options=[cls.value for cls in BinarySensorDeviceClass],
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="binary_sensor_device_class",
sort=True,
),
)
}
if domain == Platform.SENSOR:
schema = {
vol.Optional(
CONF_UNIT_OF_MEASUREMENT, default=NONE_SENTINEL
): selector.SelectSelector(
vol.Optional(CONF_UNIT_OF_MEASUREMENT): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted(
{
str(unit)
for units in DEVICE_CLASS_UNITS.values()
for unit in units
if unit is not None
},
key=str.casefold,
),
],
options=list(
{
str(unit)
for units in DEVICE_CLASS_UNITS.values()
for unit in units
if unit is not None
}
),
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="sensor_unit_of_measurement",
custom_value=True,
sort=True,
),
),
vol.Optional(
CONF_DEVICE_CLASS, default=NONE_SENTINEL
): selector.SelectSelector(
vol.Optional(CONF_DEVICE_CLASS): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted(
[
cls.value
for cls in SensorDeviceClass
if cls != SensorDeviceClass.ENUM
],
key=str.casefold,
),
cls.value
for cls in SensorDeviceClass
if cls != SensorDeviceClass.ENUM
],
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="sensor_device_class",
sort=True,
),
),
vol.Optional(
CONF_STATE_CLASS, default=NONE_SENTINEL
): selector.SelectSelector(
vol.Optional(CONF_STATE_CLASS): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted([cls.value for cls in SensorStateClass]),
],
options=[cls.value for cls in SensorStateClass],
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="sensor_state_class",
sort=True,
),
),
}
@ -144,15 +121,6 @@ async def choose_options_step(options: dict[str, Any]) -> str:
return cast(str, options["template_type"])
def _strip_sentinel(options: dict[str, Any]) -> None:
"""Convert sentinel to None."""
for key in (CONF_DEVICE_CLASS, CONF_STATE_CLASS, CONF_UNIT_OF_MEASUREMENT):
if key not in options:
continue
if options[key] == NONE_SENTINEL:
options.pop(key)
def _validate_unit(options: dict[str, Any]) -> None:
"""Validate unit of measurement."""
if (
@ -218,8 +186,6 @@ def validate_user_input(
user_input: dict[str, Any],
) -> dict[str, Any]:
"""Add template type to user input."""
if template_type in (Platform.BINARY_SENSOR, Platform.SENSOR):
_strip_sentinel(user_input)
if template_type == Platform.SENSOR:
_validate_unit(user_input)
_validate_state_class(user_input)
@ -316,7 +282,6 @@ def ws_start_preview(
errors[key.schema] = str(ex.msg)
if domain == Platform.SENSOR:
_strip_sentinel(user_input)
try:
_validate_unit(user_input)
except vol.Invalid as ex:
@ -386,7 +351,6 @@ def ws_start_preview(
)
return
_strip_sentinel(msg["user_input"])
preview_entity = CREATE_PREVIEW_ENTITY[template_type](hass, name, msg["user_input"])
preview_entity.hass = hass
preview_entity.registry_entry = entity_registry_entry

View File

@ -51,7 +51,6 @@
"selector": {
"binary_sensor_device_class": {
"options": {
"none": "[%key:component::template::selector::sensor_device_class::options::none%]",
"battery": "[%key:component::binary_sensor::entity_component::battery::name%]",
"battery_charging": "[%key:component::binary_sensor::entity_component::battery_charging::name%]",
"carbon_monoxide": "[%key:component::binary_sensor::entity_component::carbon_monoxide::name%]",
@ -83,7 +82,6 @@
},
"sensor_device_class": {
"options": {
"none": "No device class",
"apparent_power": "[%key:component::sensor::entity_component::apparent_power::name%]",
"aqi": "[%key:component::sensor::entity_component::aqi::name%]",
"atmospheric_pressure": "[%key:component::sensor::entity_component::atmospheric_pressure::name%]",
@ -137,7 +135,6 @@
},
"sensor_state_class": {
"options": {
"none": "No state class",
"measurement": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::measurement%]",
"total": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::total%]",
"total_increasing": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::total_increasing%]"