lps22: add a component (#7540)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Keith Burzinski <kbx81x@gmail.com>
This commit is contained in:
Simonas Kazlauskas 2025-07-09 00:13:58 +03:00 committed by GitHub
parent b3d7f001af
commit 691cc5f7dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 205 additions and 0 deletions

View File

@ -254,6 +254,7 @@ esphome/components/ln882x/* @lamauny
esphome/components/lock/* @esphome/core
esphome/components/logger/* @esphome/core
esphome/components/logger/select/* @clydebarrow
esphome/components/lps22/* @nagisa
esphome/components/ltr390/* @latonita @sjtrny
esphome/components/ltr501/* @latonita
esphome/components/ltr_als_ps/* @latonita

View File

View File

@ -0,0 +1,75 @@
#include "lps22.h"
namespace esphome {
namespace lps22 {
static constexpr const char *const TAG = "lps22";
static constexpr uint8_t WHO_AM_I = 0x0F;
static constexpr uint8_t LPS22HB_ID = 0xB1;
static constexpr uint8_t LPS22HH_ID = 0xB3;
static constexpr uint8_t CTRL_REG2 = 0x11;
static constexpr uint8_t CTRL_REG2_ONE_SHOT_MASK = 0b1;
static constexpr uint8_t STATUS = 0x27;
static constexpr uint8_t STATUS_T_DA_MASK = 0b10;
static constexpr uint8_t STATUS_P_DA_MASK = 0b01;
static constexpr uint8_t TEMP_L = 0x2b;
static constexpr uint8_t PRES_OUT_XL = 0x28;
static constexpr uint8_t REF_P_XL = 0x28;
static constexpr uint8_t READ_ATTEMPTS = 10;
static constexpr uint8_t READ_INTERVAL = 5;
static constexpr float PRESSURE_SCALE = 1.0f / 4096.0f;
static constexpr float TEMPERATURE_SCALE = 0.01f;
void LPS22Component::setup() {
uint8_t value = 0x00;
this->read_register(WHO_AM_I, &value, 1);
if (value != LPS22HB_ID && value != LPS22HH_ID) {
ESP_LOGW(TAG, "device IDs as %02x, which isn't a known LPS22HB or LPS22HH ID", value);
this->mark_failed();
}
}
void LPS22Component::dump_config() {
ESP_LOGCONFIG(TAG, "LPS22:");
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
}
void LPS22Component::update() {
uint8_t value = 0x00;
this->read_register(CTRL_REG2, &value, 1);
value |= CTRL_REG2_ONE_SHOT_MASK;
this->write_register(CTRL_REG2, &value, 1);
this->set_retry(READ_INTERVAL, READ_ATTEMPTS, [this](uint8_t _) { return this->try_read_(); });
}
RetryResult LPS22Component::try_read_() {
uint8_t value = 0x00;
this->read_register(STATUS, &value, 1);
const uint8_t expected_status_mask = STATUS_T_DA_MASK | STATUS_P_DA_MASK;
if ((value & expected_status_mask) != expected_status_mask) {
ESP_LOGD(TAG, "STATUS not ready: %x", value);
return RetryResult::RETRY;
}
if (this->temperature_sensor_ != nullptr) {
uint8_t t_buf[2]{0};
this->read_register(TEMP_L, t_buf, 2);
int16_t encoded = static_cast<int16_t>(encode_uint16(t_buf[1], t_buf[0]));
float temp = TEMPERATURE_SCALE * static_cast<float>(encoded);
this->temperature_sensor_->publish_state(temp);
}
if (this->pressure_sensor_ != nullptr) {
uint8_t p_buf[3]{0};
this->read_register(PRES_OUT_XL, p_buf, 3);
uint32_t p_lsb = encode_uint24(p_buf[2], p_buf[1], p_buf[0]);
this->pressure_sensor_->publish_state(PRESSURE_SCALE * static_cast<float>(p_lsb));
}
return RetryResult::DONE;
}
} // namespace lps22
} // namespace esphome

View File

@ -0,0 +1,27 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace lps22 {
class LPS22Component : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
public:
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { this->temperature_sensor_ = temperature_sensor; }
void set_pressure_sensor(sensor::Sensor *pressure_sensor) { this->pressure_sensor_ = pressure_sensor; }
void setup() override;
void update() override;
void dump_config() override;
protected:
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *pressure_sensor_{nullptr};
RetryResult try_read_();
};
} // namespace lps22
} // namespace esphome

View File

@ -0,0 +1,58 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
CONF_TEMPERATURE,
CONF_PRESSURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
ICON_THERMOMETER,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
)
CODEOWNERS = ["@nagisa"]
DEPENDENCIES = ["i2c"]
lps22 = cg.esphome_ns.namespace("lps22")
LPS22Component = lps22.class_("LPS22Component", cg.PollingComponent, i2c.I2CDevice)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(LPS22Component),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
icon=ICON_THERMOMETER,
accuracy_decimals=2,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=2,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x5D)) # can also be 0x5C
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))

View File

@ -0,0 +1,8 @@
sensor:
- platform: lps22
address: 0x5d
update_interval: 10s
temperature:
name: "LPS22 Temperature"
pressure:
name: "LPS22 Pressure"

View File

@ -0,0 +1,6 @@
i2c:
- id: i2c_lps22
scl: 16
sda: 17
<<: !include common.yaml

View File

@ -0,0 +1,6 @@
i2c:
- id: i2c_lps22
scl: 5
sda: 4
<<: !include common.yaml

View File

@ -0,0 +1,6 @@
i2c:
- id: i2c_lps22
scl: 5
sda: 4
<<: !include common.yaml

View File

@ -0,0 +1,6 @@
i2c:
- id: i2c_lps22
scl: 16
sda: 17
<<: !include common.yaml

View File

@ -0,0 +1,6 @@
i2c:
- id: i2c_lps22
scl: 5
sda: 4
<<: !include common.yaml

View File

@ -0,0 +1,6 @@
i2c:
- id: i2c_lps22
scl: 5
sda: 4
<<: !include common.yaml