mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Adding ADE7953 irq_pin (#1359)
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							5a2b14cfa4
						
					
				
				
					commit
					7e40d4246c
				
			| @@ -8,6 +8,9 @@ static const char *TAG = "ade7953"; | |||||||
|  |  | ||||||
| void ADE7953::dump_config() { | void ADE7953::dump_config() { | ||||||
|   ESP_LOGCONFIG(TAG, "ADE7953:"); |   ESP_LOGCONFIG(TAG, "ADE7953:"); | ||||||
|  |   if (this->has_irq_) { | ||||||
|  |     ESP_LOGCONFIG(TAG, "  IRQ Pin: GPIO%u", this->irq_pin_number_); | ||||||
|  |   } | ||||||
|   LOG_I2C_DEVICE(this); |   LOG_I2C_DEVICE(this); | ||||||
|   LOG_UPDATE_INTERVAL(this); |   LOG_UPDATE_INTERVAL(this); | ||||||
|   LOG_SENSOR("  ", "Voltage Sensor", this->voltage_sensor_); |   LOG_SENSOR("  ", "Voltage Sensor", this->voltage_sensor_); | ||||||
|   | |||||||
| @@ -9,6 +9,10 @@ namespace ade7953 { | |||||||
|  |  | ||||||
| class ADE7953 : public i2c::I2CDevice, public PollingComponent { | class ADE7953 : public i2c::I2CDevice, public PollingComponent { | ||||||
|  public: |  public: | ||||||
|  |   void set_irq_pin(uint8_t irq_pin) { | ||||||
|  |     has_irq_ = true; | ||||||
|  |     irq_pin_number_ = irq_pin; | ||||||
|  |   } | ||||||
|   void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } |   void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } | ||||||
|   void set_current_a_sensor(sensor::Sensor *current_a_sensor) { current_a_sensor_ = current_a_sensor; } |   void set_current_a_sensor(sensor::Sensor *current_a_sensor) { current_a_sensor_ = current_a_sensor; } | ||||||
|   void set_current_b_sensor(sensor::Sensor *current_b_sensor) { current_b_sensor_ = current_b_sensor; } |   void set_current_b_sensor(sensor::Sensor *current_b_sensor) { current_b_sensor_ = current_b_sensor; } | ||||||
| @@ -20,6 +24,11 @@ class ADE7953 : public i2c::I2CDevice, public PollingComponent { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void setup() override { |   void setup() override { | ||||||
|  |     if (this->has_irq_) { | ||||||
|  |       auto pin = GPIOPin(this->irq_pin_number_, INPUT); | ||||||
|  |       this->irq_pin_ = &pin; | ||||||
|  |       this->irq_pin_->setup(); | ||||||
|  |     } | ||||||
|     this->set_timeout(100, [this]() { |     this->set_timeout(100, [this]() { | ||||||
|       this->ade_write_<uint8_t>(0x0010, 0x04); |       this->ade_write_<uint8_t>(0x0010, 0x04); | ||||||
|       this->ade_write_<uint8_t>(0x00FE, 0xAD); |       this->ade_write_<uint8_t>(0x00FE, 0xAD); | ||||||
| @@ -55,6 +64,9 @@ class ADE7953 : public i2c::I2CDevice, public PollingComponent { | |||||||
|     return result; |     return result; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   bool has_irq_ = false; | ||||||
|  |   uint8_t irq_pin_number_; | ||||||
|  |   GPIOPin *irq_pin_{nullptr}; | ||||||
|   bool is_setup_{false}; |   bool is_setup_{false}; | ||||||
|   sensor::Sensor *voltage_sensor_{nullptr}; |   sensor::Sensor *voltage_sensor_{nullptr}; | ||||||
|   sensor::Sensor *current_a_sensor_{nullptr}; |   sensor::Sensor *current_a_sensor_{nullptr}; | ||||||
|   | |||||||
| @@ -1,14 +1,16 @@ | |||||||
| import esphome.codegen as cg | import esphome.codegen as cg | ||||||
| import esphome.config_validation as cv | import esphome.config_validation as cv | ||||||
| from esphome.components import sensor, i2c | from esphome.components import sensor, i2c | ||||||
|  | from esphome import pins | ||||||
| from esphome.const import CONF_ID, CONF_VOLTAGE, \ | from esphome.const import CONF_ID, CONF_VOLTAGE, \ | ||||||
|     UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT |     UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT | ||||||
|  |  | ||||||
| DEPENDENCIES = ['i2c'] | DEPENDENCIES = ['i2c'] | ||||||
|  |  | ||||||
| ace7953_ns = cg.esphome_ns.namespace('ade7953') | ade7953_ns = cg.esphome_ns.namespace('ade7953') | ||||||
| ADE7953 = ace7953_ns.class_('ADE7953', cg.PollingComponent, i2c.I2CDevice) | ADE7953 = ade7953_ns.class_('ADE7953', cg.PollingComponent, i2c.I2CDevice) | ||||||
|  |  | ||||||
|  | CONF_IRQ_PIN = 'irq_pin' | ||||||
| CONF_CURRENT_A = 'current_a' | CONF_CURRENT_A = 'current_a' | ||||||
| CONF_CURRENT_B = 'current_b' | CONF_CURRENT_B = 'current_b' | ||||||
| CONF_ACTIVE_POWER_A = 'active_power_a' | CONF_ACTIVE_POWER_A = 'active_power_a' | ||||||
| @@ -16,7 +18,7 @@ CONF_ACTIVE_POWER_B = 'active_power_b' | |||||||
|  |  | ||||||
| CONFIG_SCHEMA = cv.Schema({ | CONFIG_SCHEMA = cv.Schema({ | ||||||
|     cv.GenerateID(): cv.declare_id(ADE7953), |     cv.GenerateID(): cv.declare_id(ADE7953), | ||||||
|  |     cv.Optional(CONF_IRQ_PIN): pins.input_pin, | ||||||
|     cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1), |     cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1), | ||||||
|     cv.Optional(CONF_CURRENT_A): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), |     cv.Optional(CONF_CURRENT_A): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), | ||||||
|     cv.Optional(CONF_CURRENT_B): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), |     cv.Optional(CONF_CURRENT_B): sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2), | ||||||
| @@ -30,6 +32,9 @@ def to_code(config): | |||||||
|     yield cg.register_component(var, config) |     yield cg.register_component(var, config) | ||||||
|     yield i2c.register_i2c_device(var, config) |     yield i2c.register_i2c_device(var, config) | ||||||
|  |  | ||||||
|  |     if CONF_IRQ_PIN in config: | ||||||
|  |         cg.add(var.set_irq_pin(config[CONF_IRQ_PIN])) | ||||||
|  |  | ||||||
|     for key in [CONF_VOLTAGE, CONF_CURRENT_A, CONF_CURRENT_B, CONF_ACTIVE_POWER_A, |     for key in [CONF_VOLTAGE, CONF_CURRENT_A, CONF_CURRENT_B, CONF_ACTIVE_POWER_A, | ||||||
|                 CONF_ACTIVE_POWER_B]: |                 CONF_ACTIVE_POWER_B]: | ||||||
|         if key not in config: |         if key not in config: | ||||||
|   | |||||||
| @@ -338,6 +338,7 @@ sensor: | |||||||
|       - binary_sensor: bin3 |       - binary_sensor: bin3 | ||||||
|         value: 100.0 |         value: 100.0 | ||||||
|   - platform: ade7953 |   - platform: ade7953 | ||||||
|  |     irq_pin: GPIO16 | ||||||
|     voltage: |     voltage: | ||||||
|       name: ADE7953 Voltage |       name: ADE7953 Voltage | ||||||
|     current_a: |     current_a: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user