From 92f8b043ce940fd565b3d79562ba415de60f5e34 Mon Sep 17 00:00:00 2001 From: Michiel van Turnhout Date: Sun, 24 Feb 2019 21:48:28 +0100 Subject: [PATCH] Add MPR121 Capacitive Touch Sensor (#449) * module mpr121 * added mpr121 sensor and binary sensor * added tests * removed CONF_CHANNELS, it is not supported in the esphome-code mpr121 code * changed namespace for mpr121 to binary_sensor * Update esphome/components/binary_sensor/mpr121.py Co-Authored-By: mvturnho * fixed class names * fix lint errors --- esphome/components/binary_sensor/mpr121.py | 28 +++++++++++++++++++++ esphome/components/mpr121.py | 29 ++++++++++++++++++++++ esphome/const.py | 1 + tests/test3.yaml | 27 ++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 esphome/components/binary_sensor/mpr121.py create mode 100644 esphome/components/mpr121.py diff --git a/esphome/components/binary_sensor/mpr121.py b/esphome/components/binary_sensor/mpr121.py new file mode 100644 index 0000000000..402d30ec38 --- /dev/null +++ b/esphome/components/binary_sensor/mpr121.py @@ -0,0 +1,28 @@ +import voluptuous as vol + +from esphome.components import binary_sensor +from esphome.components.mpr121 import MPR121Component, CONF_MPR121_ID +import esphome.config_validation as cv +from esphome.const import CONF_CHANNEL, CONF_NAME +from esphome.cpp_generator import get_variable + +DEPENDENCIES = ['mpr121'] +MPR121Channel = binary_sensor.binary_sensor_ns.class_( + 'MPR121Channel', binary_sensor.BinarySensor) + +PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(MPR121Channel), + cv.GenerateID(CONF_MPR121_ID): cv.use_variable_id(MPR121Component), + vol.Required(CONF_CHANNEL): vol.All(vol.Coerce(int), vol.Range(min=0, max=11)) +})) + + +def to_code(config): + for hub in get_variable(config[CONF_MPR121_ID]): + yield + rhs = MPR121Channel.new(config[CONF_NAME], config[CONF_CHANNEL]) + binary_sensor.register_binary_sensor(hub.add_channel(rhs), config) + + +def to_hass_config(data, config): + return binary_sensor.core_to_hass_config(data, config) diff --git a/esphome/components/mpr121.py b/esphome/components/mpr121.py new file mode 100644 index 0000000000..dab4d75e51 --- /dev/null +++ b/esphome/components/mpr121.py @@ -0,0 +1,29 @@ +import voluptuous as vol + +from esphome.components import i2c, binary_sensor +import esphome.config_validation as cv +from esphome.const import CONF_ADDRESS, CONF_ID +from esphome.cpp_generator import Pvariable +from esphome.cpp_helpers import setup_component +from esphome.cpp_types import App, Component + +DEPENDENCIES = ['i2c'] +MULTI_CONF = True + +CONF_MPR121_ID = 'mpr121_id' +MPR121Component = binary_sensor.binary_sensor_ns.class_('MPR121Component', Component, i2c.I2CDevice) + +CONFIG_SCHEMA = vol.Schema({ + cv.GenerateID(): cv.declare_variable_id(MPR121Component), + vol.Optional(CONF_ADDRESS): cv.i2c_address +}).extend(cv.COMPONENT_SCHEMA.schema) + + +def to_code(config): + rhs = App.make_mpr121(config.get(CONF_ADDRESS)) + var = Pvariable(config[CONF_ID], rhs) + + setup_component(var, config) + + +BUILD_FLAGS = '-DUSE_MPR121' diff --git a/esphome/const.py b/esphome/const.py index b298ddc07f..7eaafc67b0 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -59,6 +59,7 @@ CONF_PCA9685 = 'pca9685' CONF_PCA9685_ID = 'pca9685_id' CONF_OUTPUT = 'output' CONF_CHANNEL = 'channel' +CONF_CHANNELS = 'channels' CONF_LIGHT = 'light' CONF_RED = 'red' CONF_GREEN = 'green' diff --git a/tests/test3.yaml b/tests/test3.yaml index 16ab0546dc..ec68e6c503 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -102,6 +102,10 @@ time: apds9960: address: 0x20 update_interval: 60s + +mpr121: + id: mpr121_first + address: 0x5A binary_sensor: - platform: apds9960 @@ -119,6 +123,23 @@ binary_sensor: - platform: homeassistant entity_id: binary_sensor.hello_world id: ha_hello_world_binary +#mpr121 + - platform: mpr121 + id: touchkey0 + channel: 0 + name: "touchkey0" + - platform: mpr121 + channel: 1 + name: "touchkey1" + - platform: mpr121 + channel: 2 + name: "touchkey2" + - platform: mpr121 + channel: 3 + name: "touchkey3" + on_press: + then: + - switch.toggle: mpr121_toggle remote_receiver: pin: GPIO12 @@ -158,6 +179,12 @@ script: then: - lambda: 'ESP_LOGD("main", "Hello World!");' +switch: + - platform: template + name: "mpr121_toggle" + id: mpr121_toggle + optimistic: True + stepper: - platform: uln2003 id: my_stepper