fixed code

This commit is contained in:
mvturnho 2019-05-26 11:24:12 +02:00
parent 32e1e03141
commit 0c6eacb225
2 changed files with 27 additions and 2 deletions

View file

@ -2,6 +2,8 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c
from esphome.const import CONF_ID
from mpr121_const import CONF_RELEASE_DEBOUNCE, CONF_TOUCH_DEBOUNCE, \
CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD
DEPENDENCIES = ['i2c']
AUTO_LOAD = ['binary_sensor']
@ -13,10 +15,22 @@ MPR121Component = mpr121_ns.class_('MPR121Component', cg.Component, i2c.I2CDevic
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(MPR121Component),
cv.Optional(CONF_RELEASE_DEBOUNCE, default=0): cv.int_range(min=0, max=7),
cv.Optional(CONF_TOUCH_DEBOUNCE, default=0): cv.All(cv.Coerce(int), cv.Range(min=0, max=7)),
cv.Optional(CONF_TOUCH_THRESHOLD, default=0x06): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)),
cv.Optional(CONF_RELEASE_THRESHOLD, default=0x0b): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)),
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x5A))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
if CONF_TOUCH_DEBOUNCE in config:
cg.add(var.set_touch_debounce(config[CONF_TOUCH_DEBOUNCE]))
if CONF_RELEASE_DEBOUNCE in config:
cg.add(var.set_release_debounce(config[CONF_RELEASE_DEBOUNCE]))
if CONF_TOUCH_THRESHOLD in config:
cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD]))
if CONF_RELEASE_THRESHOLD in config:
cg.add(var.set_release_threshold(config[CONF_RELEASE_THRESHOLD]))
yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config)

View file

@ -2,6 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_CHANNEL, CONF_ID
from mpr121_const import CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD
from . import mpr121_ns, MPR121Component, CONF_MPR121_ID
DEPENDENCIES = ['mpr121']
@ -11,14 +12,24 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(MPR121Channel),
cv.GenerateID(CONF_MPR121_ID): cv.use_id(MPR121Component),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=11),
cv.Optional(CONF_TOUCH_THRESHOLD): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)),
cv.Optional(CONF_RELEASE_THRESHOLD): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)),
})
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield binary_sensor.register_binary_sensor(var, config)
cg.add(var.set_channel(config[CONF_CHANNEL]))
hub = yield cg.get_variable(config[CONF_MPR121_ID])
hub = yield cg.get_variable(config[CONF_MPR121_ID])
cg.add(var.set_channel(config[CONF_CHANNEL]))
if CONF_TOUCH_THRESHOLD in config:
cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD]))
else:
cg.add(var.set_touch_threshold(hub.get_touch_threshold()))
if CONF_RELEASE_THRESHOLD in config:
cg.add(var.set_release_threshold(config[CONF_RELEASE_THRESHOLD]))
else:
cg.add(var.set_release_threshold(hub.get_release_threshold()))
cg.add(hub.register_channel(var))