forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * first working * feat complete * add CODEOWNERS * renamed to spi, reset optional * add test * fix CODEOWNERS
- Loading branch information
Showing
7 changed files
with
1,253 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome import automation, pins | ||
from esphome.components import spi | ||
from esphome.const import CONF_ID, CONF_ON_TAG, CONF_TRIGGER_ID, CONF_RESET_PIN, CONF_CS_PIN | ||
|
||
CODEOWNERS = ['@glmnet'] | ||
DEPENDENCIES = ['spi'] | ||
AUTO_LOAD = ['binary_sensor'] | ||
MULTI_CONF = True | ||
|
||
|
||
rc522_spi_ns = cg.esphome_ns.namespace('rc522_spi') | ||
RC522 = rc522_spi_ns.class_('RC522', cg.PollingComponent, spi.SPIDevice) | ||
RC522Trigger = rc522_spi_ns.class_('RC522Trigger', automation.Trigger.template(cg.std_string)) | ||
|
||
CONFIG_SCHEMA = cv.Schema({ | ||
cv.GenerateID(): cv.declare_id(RC522), | ||
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema, | ||
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema, | ||
cv.Optional(CONF_ON_TAG): automation.validate_automation({ | ||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(RC522Trigger), | ||
}), | ||
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema()) | ||
|
||
|
||
def to_code(config): | ||
var = cg.new_Pvariable(config[CONF_ID]) | ||
yield cg.register_component(var, config) | ||
yield spi.register_spi_device(var, config) | ||
|
||
if CONF_RESET_PIN in config: | ||
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN]) | ||
cg.add(var.set_reset_pin(reset)) | ||
|
||
for conf in config.get(CONF_ON_TAG, []): | ||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) | ||
cg.add(var.register_trigger(trigger)) | ||
yield automation.build_automation(trigger, [(cg.std_string, 'x')], conf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.components import binary_sensor | ||
from esphome.const import CONF_UID, CONF_ID | ||
from esphome.core import HexInt | ||
from . import rc522_spi_ns, RC522 | ||
|
||
DEPENDENCIES = ['rc522_spi'] | ||
|
||
CONF_RC522_ID = 'rc522_id' | ||
|
||
|
||
def validate_uid(value): | ||
value = cv.string_strict(value) | ||
for x in value.split('-'): | ||
if len(x) != 2: | ||
raise cv.Invalid("Each part (separated by '-') of the UID must be two characters " | ||
"long.") | ||
try: | ||
x = int(x, 16) | ||
except ValueError as err: | ||
raise cv.Invalid("Valid characters for parts of a UID are 0123456789ABCDEF.") from err | ||
if x < 0 or x > 255: | ||
raise cv.Invalid("Valid values for UID parts (separated by '-') are 00 to FF") | ||
return value | ||
|
||
|
||
RC522BinarySensor = rc522_spi_ns.class_('RC522BinarySensor', binary_sensor.BinarySensor) | ||
|
||
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({ | ||
cv.GenerateID(): cv.declare_id(RC522BinarySensor), | ||
cv.GenerateID(CONF_RC522_ID): cv.use_id(RC522), | ||
cv.Required(CONF_UID): validate_uid, | ||
}) | ||
|
||
|
||
def to_code(config): | ||
var = cg.new_Pvariable(config[CONF_ID]) | ||
yield binary_sensor.register_binary_sensor(var, config) | ||
|
||
hub = yield cg.get_variable(config[CONF_RC522_ID]) | ||
cg.add(hub.register_tag(var)) | ||
addr = [HexInt(int(x, 16)) for x in config[CONF_UID].split('-')] | ||
cg.add(var.set_uid(addr)) |
Oops, something went wrong.