diff --git a/esphome/components/mpr121/__init__.py b/esphome/components/mpr121/__init__.py index 9e1bd3726d..04d35bee87 100644 --- a/esphome/components/mpr121/__init__.py +++ b/esphome/components/mpr121/__init__.py @@ -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) diff --git a/esphome/components/mpr121/binary_sensor.py b/esphome/components/mpr121/binary_sensor.py index 100dacd6dd..9c7364e156 100644 --- a/esphome/components/mpr121/binary_sensor.py +++ b/esphome/components/mpr121/binary_sensor.py @@ -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))