2019-04-17 12:06:00 +02:00
|
|
|
import esphome.codegen as cg
|
|
|
|
import esphome.config_validation as cv
|
|
|
|
from esphome.components import binary_sensor
|
2022-02-17 22:39:59 +01:00
|
|
|
from esphome.const import CONF_UID
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome.core import HexInt
|
2020-10-31 23:55:48 +01:00
|
|
|
from . import pn532_ns, PN532, CONF_PN532_ID
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
DEPENDENCIES = ["pn532"]
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def validate_uid(value):
|
|
|
|
value = cv.string_strict(value)
|
2021-03-07 20:03:16 +01:00
|
|
|
for x in value.split("-"):
|
2019-04-17 12:06:00 +02:00
|
|
|
if len(x) != 2:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise cv.Invalid(
|
|
|
|
"Each part (separated by '-') of the UID must be two characters "
|
|
|
|
"long."
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
try:
|
|
|
|
x = int(x, 16)
|
2020-09-16 12:12:40 +02:00
|
|
|
except ValueError as err:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise cv.Invalid(
|
|
|
|
"Valid characters for parts of a UID are 0123456789ABCDEF."
|
|
|
|
) from err
|
2019-04-17 12:06:00 +02:00
|
|
|
if x < 0 or x > 255:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise cv.Invalid(
|
|
|
|
"Valid values for UID parts (separated by '-') are 00 to FF"
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
PN532BinarySensor = pn532_ns.class_("PN532BinarySensor", binary_sensor.BinarySensor)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2022-02-17 22:39:59 +01:00
|
|
|
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(PN532BinarySensor).extend(
|
2021-03-07 20:03:16 +01:00
|
|
|
{
|
|
|
|
cv.GenerateID(CONF_PN532_ID): cv.use_id(PN532),
|
|
|
|
cv.Required(CONF_UID): validate_uid,
|
|
|
|
}
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
async def to_code(config):
|
2022-02-17 22:39:59 +01:00
|
|
|
var = await binary_sensor.new_binary_sensor(config)
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2021-05-24 10:58:29 +02:00
|
|
|
hub = await cg.get_variable(config[CONF_PN532_ID])
|
2019-04-17 12:06:00 +02:00
|
|
|
cg.add(hub.register_tag(var))
|
2021-03-07 20:03:16 +01:00
|
|
|
addr = [HexInt(int(x, 16)) for x in config[CONF_UID].split("-")]
|
2019-04-22 21:56:30 +02:00
|
|
|
cg.add(var.set_uid(addr))
|