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(
{
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.GenerateID(CONF_TRIGGER_ID): cv.declare_id(NumberStateTrigger),
@ -183,43 +182,25 @@ _UNDEF = object()
def number_schema(
class_: MockObjClass = _UNDEF,
class_: MockObjClass,
*,
icon: str = _UNDEF,
entity_category: str = _UNDEF,
device_class: str = _UNDEF,
unit_of_measurement: str = _UNDEF,
) -> cv.Schema:
schema = NUMBER_SCHEMA
if class_ is not _UNDEF:
schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)})
if icon is not _UNDEF:
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon})
if entity_category is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_ENTITY_CATEGORY, default=entity_category
): cv.entity_category
}
)
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
schema = {cv.GenerateID(): cv.declare_id(class_)}
for key, default, validator in [
(CONF_ICON, icon, cv.icon),
(CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
(CONF_DEVICE_CLASS, device_class, validate_device_class),
(CONF_UNIT_OF_MEASUREMENT, unit_of_measurement, validate_unit_of_measurement),
]:
if default is not _UNDEF:
schema[cv.Optional(key, default=default)] = validator
return NUMBER_SCHEMA.extend(schema)
async def setup_number_core_(