This commit is contained in:
erikveg 2024-05-02 09:57:37 +00:00 committed by GitHub
commit d838c02cc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 139 additions and 0 deletions

View File

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

View File

@ -0,0 +1,2 @@
CODEOWNERS = ["@erikveg"]
DEPENDENCIES = ["i2c"]

View File

@ -0,0 +1,31 @@
#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);
this->publish_state(value);
}
}
} // namespace ain4_20ma
} // namespace esphome

View File

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

View File

@ -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=3,
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)

View File

@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4
sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s

View File

@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4
sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s

View File

@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 16
sda: 17
sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s

View File

@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 16
sda: 17
sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s

View File

@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4
sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s

View File

@ -0,0 +1,9 @@
i2c:
- id: i2c_ain4_20ma
scl: 5
sda: 4
sensor:
- platform: ain4_20ma
name: "Current"
update_interval: 60s