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_UPPER = "upper"
CONF_LOWER = "lower" CONF_LOWER = "lower"
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend( CONFIG_SCHEMA = (
binary_sensor.binary_sensor_schema(AnalogThresholdBinarySensor)
.extend(
{ {
cv.GenerateID(): cv.declare_id(AnalogThresholdBinarySensor),
cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor), cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor),
cv.Required(CONF_THRESHOLD): cv.Any( cv.Required(CONF_THRESHOLD): cv.Any(
cv.float_, cv.float_,
cv.Schema( cv.Schema(
{cv.Required(CONF_UPPER): cv.float_, cv.Required(CONF_LOWER): cv.float_} {
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): async def to_code(config):

View file

@ -393,28 +393,21 @@ def binary_sensor_schema(
entity_category: str = _UNDEF, entity_category: str = _UNDEF,
device_class: str = _UNDEF, device_class: str = _UNDEF,
) -> cv.Schema: ) -> cv.Schema:
schema = BINARY_SENSOR_SCHEMA schema = {}
if class_ is not _UNDEF: if class_ is not _UNDEF:
schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)}) # Not cv.optional
if icon is not _UNDEF: schema[cv.GenerateID()] = cv.declare_id(class_)
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon})
if entity_category is not _UNDEF: for key, default, validator in [
schema = schema.extend( (CONF_ICON, icon, cv.icon),
{ (CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
cv.Optional( (CONF_DEVICE_CLASS, device_class, validate_device_class),
CONF_ENTITY_CATEGORY, default=entity_category ]:
): cv.entity_category if default is not _UNDEF:
} schema[cv.Optional(key, default=default)] = validator
)
if device_class is not _UNDEF: return BINARY_SENSOR_SCHEMA.extend(schema)
schema = schema.extend(
{
cv.Optional(
CONF_DEVICE_CLASS, default=device_class
): validate_device_class
}
)
return schema
async def setup_binary_sensor_core_(var, config): async def setup_binary_sensor_core_(var, config):

View file

@ -30,9 +30,8 @@ def check_button(obj):
CONFIG_SCHEMA = cv.All( 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.GenerateID(CONF_KEYPAD_ID): cv.use_id(MatrixKeypad),
cv.Optional(CONF_ROW): cv.int_, cv.Optional(CONF_ROW): cv.int_,
cv.Optional(CONF_COL): 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) TM1637Display = tm1637_ns.class_("TM1637Display", cg.PollingComponent)
TM1637Key = tm1637_ns.class_("TM1637Key", binary_sensor.BinarySensor) 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.GenerateID(CONF_TM1637_ID): cv.use_id(TM1637Display),
cv.Required(CONF_KEY): cv.int_range(min=0, max=15), 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) 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.GenerateID(CONF_TM1638_ID): cv.use_id(TM1638Component),
cv.Required(CONF_KEY): cv.int_range(min=0, max=15), cv.Required(CONF_KEY): cv.int_range(min=0, max=15),
} }