Simplify sensor schema generation (#4462)

* Simplify sensor schema generation

* Mark class not optional

* Fix assignment
This commit is contained in:
Paulus Schoutsen 2023-02-19 14:20:13 -05:00 committed by GitHub
parent 36a1f6cfb1
commit 40e2832e67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -272,48 +272,24 @@ def sensor_schema(
state_class: str = _UNDEF, state_class: str = _UNDEF,
entity_category: str = _UNDEF, entity_category: str = _UNDEF,
) -> cv.Schema: ) -> cv.Schema:
schema = SENSOR_SCHEMA schema = {}
if class_ is not _UNDEF: if class_ is not _UNDEF:
schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)}) # Not optional.
if unit_of_measurement is not _UNDEF: schema[cv.GenerateID()] = cv.declare_id(class_)
schema = schema.extend(
{ for key, default, validator in [
cv.Optional( (CONF_UNIT_OF_MEASUREMENT, unit_of_measurement, validate_unit_of_measurement),
CONF_UNIT_OF_MEASUREMENT, default=unit_of_measurement (CONF_ICON, icon, validate_icon),
): validate_unit_of_measurement (CONF_ACCURACY_DECIMALS, accuracy_decimals, validate_accuracy_decimals),
} (CONF_DEVICE_CLASS, device_class, validate_device_class),
) (CONF_STATE_CLASS, state_class, validate_state_class),
if icon is not _UNDEF: (CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): validate_icon}) ]:
if accuracy_decimals is not _UNDEF: if default is not _UNDEF:
schema = schema.extend( schema[cv.Optional(key, default=default)] = validator
{
cv.Optional( return SENSOR_SCHEMA.extend(schema)
CONF_ACCURACY_DECIMALS, default=accuracy_decimals
): validate_accuracy_decimals,
}
)
if device_class is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_DEVICE_CLASS, default=device_class
): validate_device_class
}
)
if state_class is not _UNDEF:
schema = schema.extend(
{cv.Optional(CONF_STATE_CLASS, default=state_class): validate_state_class}
)
if entity_category is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_ENTITY_CATEGORY, default=entity_category
): cv.entity_category
}
)
return schema
@FILTER_REGISTRY.register("offset", OffsetFilter, cv.float_) @FILTER_REGISTRY.register("offset", OffsetFilter, cv.float_)