Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New component AIN4-20mA current measurement sensor #6665

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ esphome/components/ade7953_i2c/* @angelnu
esphome/components/ade7953_spi/* @angelnu
esphome/components/ads1118/* @solomondg1
esphome/components/ags10/* @mak-42
esphome/components/ain4_20ma/* @erikveg
esphome/components/airthings_ble/* @jeromelaban
esphome/components/airthings_wave_base/* @jeromelaban @kpfleming @ncareau
esphome/components/airthings_wave_mini/* @ncareau
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/ain4_20ma/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CODEOWNERS = ["@erikveg"]
DEPENDENCIES = ["i2c"]
33 changes: 33 additions & 0 deletions esphome/components/ain4_20ma/ain4_20ma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "ain4_20ma.h"
#include "esphome/core/log.h"

namespace esphome {
namespace ain4_20ma {

static const char *const TAG = "ain4_20ma";

void Ain420maComponent::dump_config() {
ESP_LOGCONFIG(TAG, "AIN4-20mA Sensor:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Sensor:", this);
}

void Ain420maComponent::update() {
uint8_t data[2];

i2c::ErrorCode err = this->read_register(0x20, data, 2);
if (err != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Error reading data from AIN4-20mA");
this->publish_state(NAN);
} else {
uint16_t value;
value = data[0] | (data[1] << 8);
float current;
current = value / 100.0;
this->publish_state(current);
}
}

} // namespace ain4_20ma
} // namespace esphome
18 changes: 18 additions & 0 deletions esphome/components/ain4_20ma/ain4_20ma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"

namespace esphome {
namespace ain4_20ma {

class Ain420maComponent : public i2c::I2CDevice, public PollingComponent, public sensor::Sensor {
public:
float get_setup_priority() const override { return setup_priority::DATA; }
void dump_config() override;
void update() override;
};

} // namespace ain4_20ma
} // namespace esphome
33 changes: 33 additions & 0 deletions esphome/components/ain4_20ma/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
DEVICE_CLASS_CURRENT,
STATE_CLASS_MEASUREMENT,
)

DEPENDENCIES = ["i2c"]

ain4_20ma_ns = cg.esphome_ns.namespace("ain4_20ma")

Ain420maComponent = ain4_20ma_ns.class_(
"Ain420maComponent", cg.PollingComponent, i2c.I2CDevice, sensor.Sensor
)

CONFIG_SCHEMA = (
sensor.sensor_schema(
Ain420maComponent,
unit_of_measurement="mA",
accuracy_decimals=2,
device_class=DEVICE_CLASS_CURRENT,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(default_address=0x55))
)


async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
9 changes: 9 additions & 0 deletions tests/components/ain4_20ma/test.esp32-c3-idf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4

sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s
9 changes: 9 additions & 0 deletions tests/components/ain4_20ma/test.esp32-c3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4

sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s
9 changes: 9 additions & 0 deletions tests/components/ain4_20ma/test.esp32-idf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 16
sda: 17

sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s
9 changes: 9 additions & 0 deletions tests/components/ain4_20ma/test.esp32.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 16
sda: 17

sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s
9 changes: 9 additions & 0 deletions tests/components/ain4_20ma/test.esp8266.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4

sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s
9 changes: 9 additions & 0 deletions tests/components/ain4_20ma/test.rp2040.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4

sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s