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 <qris.online@gmail.com>

* fixed class names

* fix lint errors
This commit is contained in:
Michiel van Turnhout 2019-02-24 21:48:28 +01:00 committed by Otto Winter
parent 2e5fd7e90d
commit 92f8b043ce
4 changed files with 85 additions and 0 deletions

View file

@ -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)

View file

@ -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'

View file

@ -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'

View file

@ -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