Simplify binary_sensor_schema function (#4469)

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

View File

@ -15,18 +15,24 @@ AnalogThresholdBinarySensor = analog_threshold_ns.class_(
CONF_UPPER = "upper"
CONF_LOWER = "lower"
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(AnalogThresholdBinarySensor),
cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor),
cv.Required(CONF_THRESHOLD): cv.Any(
cv.float_,
cv.Schema(
{cv.Required(CONF_UPPER): cv.float_, cv.Required(CONF_LOWER): cv.float_}
CONFIG_SCHEMA = (
binary_sensor.binary_sensor_schema(AnalogThresholdBinarySensor)
.extend(
{
cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor),
cv.Required(CONF_THRESHOLD): cv.Any(
cv.float_,
cv.Schema(
{
cv.Required(CONF_UPPER): cv.float_,
cv.Required(CONF_LOWER): cv.float_,
}
),
),
),
}
).extend(cv.COMPONENT_SCHEMA)
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):

View File

@ -393,28 +393,21 @@ def binary_sensor_schema(
entity_category: str = _UNDEF,
device_class: str = _UNDEF,
) -> cv.Schema:
schema = BINARY_SENSOR_SCHEMA
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
}
)
return schema
# Not cv.optional
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),
]:
if default is not _UNDEF:
schema[cv.Optional(key, default=default)] = validator
return BINARY_SENSOR_SCHEMA.extend(schema)
async def setup_binary_sensor_core_(var, config):

View File

@ -30,9 +30,8 @@ def check_button(obj):
CONFIG_SCHEMA = cv.All(
binary_sensor.BINARY_SENSOR_SCHEMA.extend(
binary_sensor.binary_sensor_schema(MatrixKeypadBinarySensor).extend(
{
cv.GenerateID(): cv.declare_id(MatrixKeypadBinarySensor),
cv.GenerateID(CONF_KEYPAD_ID): cv.use_id(MatrixKeypad),
cv.Optional(CONF_ROW): cv.int_,
cv.Optional(CONF_COL): cv.int_,

View File

@ -9,9 +9,8 @@ tm1637_ns = cg.esphome_ns.namespace("tm1637")
TM1637Display = tm1637_ns.class_("TM1637Display", cg.PollingComponent)
TM1637Key = tm1637_ns.class_("TM1637Key", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(TM1637Key).extend(
{
cv.GenerateID(): cv.declare_id(TM1637Key),
cv.GenerateID(CONF_TM1637_ID): cv.use_id(TM1637Display),
cv.Required(CONF_KEY): cv.int_range(min=0, max=15),
}

View File

@ -6,9 +6,8 @@ from ..display import tm1638_ns, TM1638Component, CONF_TM1638_ID
TM1638Key = tm1638_ns.class_("TM1638Key", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(TM1638Key).extend(
{
cv.GenerateID(): cv.declare_id(TM1638Key),
cv.GenerateID(CONF_TM1638_ID): cv.use_id(TM1638Component),
cv.Required(CONF_KEY): cv.int_range(min=0, max=15),
}