diff --git a/esphome/components/mcp23008/__init__.py b/esphome/components/mcp23008/__init__.py index 4241b6ba48..858b37b60a 100644 --- a/esphome/components/mcp23008/__init__.py +++ b/esphome/components/mcp23008/__init__.py @@ -2,7 +2,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins from esphome.components import i2c -from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED +from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED, CONF_OPEN_DRAIN_INTERRUPT DEPENDENCIES = ['i2c'] MULTI_CONF = True @@ -20,6 +20,7 @@ MCP23008GPIOPin = mcp23008_ns.class_('MCP23008GPIOPin', cg.GPIOPin) CONFIG_SCHEMA = cv.Schema({ cv.Required(CONF_ID): cv.declare_id(MCP23008), + cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean, }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20)) @@ -27,6 +28,7 @@ def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield i2c.register_i2c_device(var, config) + cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT])) CONF_MCP23008 = 'mcp23008' diff --git a/esphome/components/mcp23008/mcp23008.cpp b/esphome/components/mcp23008/mcp23008.cpp index bf5bb55f2e..22c7322458 100644 --- a/esphome/components/mcp23008/mcp23008.cpp +++ b/esphome/components/mcp23008/mcp23008.cpp @@ -14,8 +14,10 @@ void MCP23008::setup() { return; } - // all pins input - this->write_reg_(MCP23008_IODIR, 0xFF); + if (this->open_drain_ints_) { + // enable open-drain interrupt pins, 3.3V-safe + this->write_reg_(MCP23008_IOCON, 0x04); + } } bool MCP23008::digital_read(uint8_t pin) { uint8_t bit = pin % 8; diff --git a/esphome/components/mcp23008/mcp23008.h b/esphome/components/mcp23008/mcp23008.h index b4e5d75fd4..e30a924dde 100644 --- a/esphome/components/mcp23008/mcp23008.h +++ b/esphome/components/mcp23008/mcp23008.h @@ -39,6 +39,8 @@ class MCP23008 : public Component, public i2c::I2CDevice { void digital_write(uint8_t pin, bool value); void pin_mode(uint8_t pin, uint8_t mode); + void set_open_drain_ints(const bool value) { open_drain_ints_ = value; } + float get_setup_priority() const override; protected: @@ -50,6 +52,7 @@ class MCP23008 : public Component, public i2c::I2CDevice { void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a); uint8_t olat_{0x00}; + bool open_drain_ints_; }; class MCP23008GPIOPin : public GPIOPin { diff --git a/esphome/components/mcp23017/__init__.py b/esphome/components/mcp23017/__init__.py index 4b798bf434..34f94b293a 100644 --- a/esphome/components/mcp23017/__init__.py +++ b/esphome/components/mcp23017/__init__.py @@ -2,7 +2,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins from esphome.components import i2c -from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED +from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED, CONF_OPEN_DRAIN_INTERRUPT DEPENDENCIES = ['i2c'] MULTI_CONF = True @@ -20,6 +20,7 @@ MCP23017GPIOPin = mcp23017_ns.class_('MCP23017GPIOPin', cg.GPIOPin) CONFIG_SCHEMA = cv.Schema({ cv.Required(CONF_ID): cv.declare_id(MCP23017), + cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean, }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20)) @@ -27,6 +28,7 @@ def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield i2c.register_i2c_device(var, config) + cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT])) CONF_MCP23017 = 'mcp23017' diff --git a/esphome/components/mcp23017/mcp23017.cpp b/esphome/components/mcp23017/mcp23017.cpp index 9653aa680d..ec972668ef 100644 --- a/esphome/components/mcp23017/mcp23017.cpp +++ b/esphome/components/mcp23017/mcp23017.cpp @@ -14,9 +14,11 @@ void MCP23017::setup() { return; } - // all pins input - this->write_reg_(MCP23017_IODIRA, 0xFF); - this->write_reg_(MCP23017_IODIRB, 0xFF); + if (this->open_drain_ints_) { + // enable open-drain interrupt pins, 3.3V-safe + this->write_reg_(MCP23017_IOCONA, 0x04); + this->write_reg_(MCP23017_IOCONB, 0x04); + } } bool MCP23017::digital_read(uint8_t pin) { uint8_t bit = pin % 8; diff --git a/esphome/components/mcp23017/mcp23017.h b/esphome/components/mcp23017/mcp23017.h index 4389eeb6ff..5656dcc58d 100644 --- a/esphome/components/mcp23017/mcp23017.h +++ b/esphome/components/mcp23017/mcp23017.h @@ -51,6 +51,8 @@ class MCP23017 : public Component, public i2c::I2CDevice { void digital_write(uint8_t pin, bool value); void pin_mode(uint8_t pin, uint8_t mode); + void set_open_drain_ints(const bool value) { open_drain_ints_ = value; } + float get_setup_priority() const override; protected: @@ -63,6 +65,7 @@ class MCP23017 : public Component, public i2c::I2CDevice { uint8_t olat_a_{0x00}; uint8_t olat_b_{0x00}; + bool open_drain_ints_; }; class MCP23017GPIOPin : public GPIOPin { diff --git a/esphome/const.py b/esphome/const.py index 184954b4c6..0fec577d19 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -357,6 +357,7 @@ CONF_ON_VALUE = 'on_value' CONF_ON_VALUE_RANGE = 'on_value_range' CONF_ONE = 'one' CONF_OPEN_ACTION = 'open_action' +CONF_OPEN_DRAIN_INTERRUPT = 'open_drain_interrupt' CONF_OPEN_DURATION = 'open_duration' CONF_OPEN_ENDSTOP = 'open_endstop' CONF_OPTIMISTIC = 'optimistic' diff --git a/tests/test1.yaml b/tests/test1.yaml index cda2abfeb5..84dd8eeaf4 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1921,10 +1921,12 @@ pcf8574: mcp23017: - id: 'mcp23017_hub' + open_drain_interrupt: 'true' mcp23008: - id: 'mcp23008_hub' address: 0x22 + open_drain_interrupt: 'true' mcp23016: - id: 'mcp23016_hub'