Allow removal of sensor settings in scrape (#90412)
* Allow removal of sensor settings in scrape * Adjust * Adjust * Add comment * Simplify * Simplify * Adjust * Don't allow empty string * Only allow None * Use default as None * Use sentinel "none" * Not needed * Adjust unit of measurement * Add translation keys for "none" * Use translations * Sort * Add enum and timestamp * Use translation references * Remove default and set suggested_values * Disallow enum device class * Adjust tests * Adjust _strip_sentinel
This commit is contained in:
parent
ea32cc5d92
commit
4f54e33f67
4 changed files with 294 additions and 18 deletions
|
@ -95,6 +95,8 @@ RESOURCE_SETUP = {
|
|||
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): TextSelector(),
|
||||
}
|
||||
|
||||
NONE_SENTINEL = "none"
|
||||
|
||||
SENSOR_SETUP = {
|
||||
vol.Required(CONF_SELECT): TextSelector(),
|
||||
vol.Optional(CONF_INDEX, default=0): NumberSelector(
|
||||
|
@ -102,28 +104,45 @@ SENSOR_SETUP = {
|
|||
),
|
||||
vol.Optional(CONF_ATTRIBUTE): TextSelector(),
|
||||
vol.Optional(CONF_VALUE_TEMPLATE): TemplateSelector(),
|
||||
vol.Optional(CONF_DEVICE_CLASS): SelectSelector(
|
||||
vol.Required(CONF_DEVICE_CLASS): SelectSelector(
|
||||
SelectSelectorConfig(
|
||||
options=[cls.value for cls in SensorDeviceClass],
|
||||
options=[NONE_SENTINEL]
|
||||
+ sorted(
|
||||
[
|
||||
cls.value
|
||||
for cls in SensorDeviceClass
|
||||
if cls != SensorDeviceClass.ENUM
|
||||
]
|
||||
),
|
||||
mode=SelectSelectorMode.DROPDOWN,
|
||||
translation_key="device_class",
|
||||
)
|
||||
),
|
||||
vol.Optional(CONF_STATE_CLASS): SelectSelector(
|
||||
vol.Required(CONF_STATE_CLASS): SelectSelector(
|
||||
SelectSelectorConfig(
|
||||
options=[cls.value for cls in SensorStateClass],
|
||||
options=[NONE_SENTINEL] + sorted([cls.value for cls in SensorStateClass]),
|
||||
mode=SelectSelectorMode.DROPDOWN,
|
||||
translation_key="state_class",
|
||||
)
|
||||
),
|
||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT): SelectSelector(
|
||||
vol.Required(CONF_UNIT_OF_MEASUREMENT): SelectSelector(
|
||||
SelectSelectorConfig(
|
||||
options=[cls.value for cls in UnitOfTemperature],
|
||||
options=[NONE_SENTINEL] + sorted([cls.value for cls in UnitOfTemperature]),
|
||||
custom_value=True,
|
||||
mode=SelectSelectorMode.DROPDOWN,
|
||||
translation_key="unit_of_measurement",
|
||||
)
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
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 options[key] == NONE_SENTINEL:
|
||||
options.pop(key)
|
||||
|
||||
|
||||
async def validate_rest_setup(
|
||||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
|
@ -150,6 +169,7 @@ async def validate_sensor_setup(
|
|||
# Standard behavior is to merge the result with the options.
|
||||
# In this case, we want to add a sub-item so we update the options directly.
|
||||
sensors: list[dict[str, Any]] = handler.options.setdefault(SENSOR_DOMAIN, [])
|
||||
_strip_sentinel(user_input)
|
||||
sensors.append(user_input)
|
||||
return {}
|
||||
|
||||
|
@ -181,7 +201,11 @@ async def get_edit_sensor_suggested_values(
|
|||
) -> dict[str, Any]:
|
||||
"""Return suggested values for sensor editing."""
|
||||
idx: int = handler.flow_state["_idx"]
|
||||
return cast(dict[str, Any], handler.options[SENSOR_DOMAIN][idx])
|
||||
suggested_values: dict[str, Any] = dict(handler.options[SENSOR_DOMAIN][idx])
|
||||
for key in (CONF_DEVICE_CLASS, CONF_STATE_CLASS, CONF_UNIT_OF_MEASUREMENT):
|
||||
if not suggested_values.get(key):
|
||||
suggested_values[key] = NONE_SENTINEL
|
||||
return suggested_values
|
||||
|
||||
|
||||
async def validate_sensor_edit(
|
||||
|
@ -194,6 +218,7 @@ async def validate_sensor_edit(
|
|||
# In this case, we want to add a sub-item so we update the options directly.
|
||||
idx: int = handler.flow_state["_idx"]
|
||||
handler.options[SENSOR_DOMAIN][idx].update(user_input)
|
||||
_strip_sentinel(handler.options[SENSOR_DOMAIN][idx])
|
||||
return {}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue