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

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

View File

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