1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-21 12:28:17 +00:00

Simplify button_schema function (#4468)

This commit is contained in:
Jesse Hills 2023-02-20 10:13:37 +13:00 committed by GitHub
parent add40c7652
commit 04c12823b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 38 deletions

View File

@ -57,34 +57,23 @@ _UNDEF = object()
def button_schema( def button_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,
) -> cv.Schema: ) -> cv.Schema:
schema = BUTTON_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( ]:
{ if default is not _UNDEF:
cv.Optional( schema[cv.Optional(key, default=default)] = validator
CONF_ENTITY_CATEGORY, default=entity_category
): cv.entity_category return BUTTON_SCHEMA.extend(schema)
}
)
if device_class is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_DEVICE_CLASS, default=device_class
): validate_device_class
}
)
return schema
async def setup_button_core_(var, config): async def setup_button_core_(var, config):

View File

@ -6,13 +6,16 @@ from .. import output_ns
OutputButton = output_ns.class_("OutputButton", button.Button, cg.Component) OutputButton = output_ns.class_("OutputButton", button.Button, cg.Component)
CONFIG_SCHEMA = button.BUTTON_SCHEMA.extend( CONFIG_SCHEMA = (
{ button.button_schema(OutputButton)
cv.GenerateID(): cv.declare_id(OutputButton), .extend(
cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput), {
cv.Required(CONF_DURATION): cv.positive_time_period_milliseconds, cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput),
} cv.Required(CONF_DURATION): cv.positive_time_period_milliseconds,
).extend(cv.COMPONENT_SCHEMA) }
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config): async def to_code(config):

View File

@ -1,15 +1,10 @@
import esphome.config_validation as cv
from esphome.components import button from esphome.components import button
from .. import template_ns from .. import template_ns
TemplateButton = template_ns.class_("TemplateButton", button.Button) TemplateButton = template_ns.class_("TemplateButton", button.Button)
CONFIG_SCHEMA = button.BUTTON_SCHEMA.extend( CONFIG_SCHEMA = button.button_schema(TemplateButton)
{
cv.GenerateID(): cv.declare_id(TemplateButton),
}
)
async def to_code(config): async def to_code(config):

View File

@ -12,11 +12,12 @@ WakeOnLanButton = wake_on_lan_ns.class_("WakeOnLanButton", button.Button, cg.Com
DEPENDENCIES = ["network"] DEPENDENCIES = ["network"]
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
button.BUTTON_SCHEMA.extend(cv.COMPONENT_SCHEMA).extend( button.button_schema(WakeOnLanButton)
.extend(cv.COMPONENT_SCHEMA)
.extend(
cv.Schema( cv.Schema(
{ {
cv.Required(CONF_TARGET_MAC_ADDRESS): cv.mac_address, cv.Required(CONF_TARGET_MAC_ADDRESS): cv.mac_address,
cv.GenerateID(): cv.declare_id(WakeOnLanButton),
} }
), ),
), ),