Simplify number_schema function (#4467)

This commit is contained in:
Jesse Hills 2023-02-20 10:13:35 +13:00 committed by GitHub
parent 72391389a3
commit add40c7652
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -159,7 +159,6 @@ validate_unit_of_measurement = cv.string_strict
NUMBER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend( NUMBER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
{ {
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTNumberComponent), cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTNumberComponent),
cv.GenerateID(): cv.declare_id(Number),
cv.Optional(CONF_ON_VALUE): automation.validate_automation( cv.Optional(CONF_ON_VALUE): automation.validate_automation(
{ {
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(NumberStateTrigger), cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(NumberStateTrigger),
@ -183,43 +182,25 @@ _UNDEF = object()
def number_schema( def number_schema(
class_: MockObjClass = _UNDEF, class_: MockObjClass,
*, *,
icon: str = _UNDEF, icon: str = _UNDEF,
entity_category: str = _UNDEF, entity_category: str = _UNDEF,
device_class: str = _UNDEF, device_class: str = _UNDEF,
unit_of_measurement: str = _UNDEF, unit_of_measurement: str = _UNDEF,
) -> cv.Schema: ) -> cv.Schema:
schema = NUMBER_SCHEMA schema = {cv.GenerateID(): cv.declare_id(class_)}
if class_ is not _UNDEF:
schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)}) for key, default, validator in [
if icon is not _UNDEF: (CONF_ICON, icon, cv.icon),
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon}) (CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
if entity_category is not _UNDEF: (CONF_DEVICE_CLASS, device_class, validate_device_class),
schema = schema.extend( (CONF_UNIT_OF_MEASUREMENT, unit_of_measurement, validate_unit_of_measurement),
{ ]:
cv.Optional( if default is not _UNDEF:
CONF_ENTITY_CATEGORY, default=entity_category schema[cv.Optional(key, default=default)] = validator
): cv.entity_category
} return NUMBER_SCHEMA.extend(schema)
)
if device_class is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_DEVICE_CLASS, default=device_class
): validate_device_class
}
)
if unit_of_measurement is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_UNIT_OF_MEASUREMENT, default=unit_of_measurement
): validate_unit_of_measurement
}
)
return schema
async def setup_number_core_( async def setup_number_core_(