text sensors

This commit is contained in:
Anton Viktorov 2024-05-23 11:53:41 +02:00
parent d7e0ca6eed
commit 193a98bfb6
7 changed files with 76 additions and 1 deletions

View file

@ -253,6 +253,7 @@ esphome/components/mopeka_std_check/* @Fabian-Schmidt
esphome/components/mpl3115a2/* @kbickar
esphome/components/mpu6886/* @fabaff
esphome/components/ms8607/* @e28eta
esphome/components/msa3xx/* @latonita
esphome/components/network/* @esphome/core
esphome/components/nextion/* @edwardtfn @senexcrenshaw
esphome/components/nextion/binary_sensor/* @senexcrenshaw

View file

@ -15,9 +15,11 @@ from esphome.const import (
CONF_TRANSFORM,
)
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "binary_sensor", "text_sensor"]
CONF_MSA3XX_ID = "msa3xx_id"
CONF_MIRROR_Z = "mirror_z"

View file

@ -2,6 +2,7 @@
import esphome.config_validation as cv
from . import MSA3xxComponent, CONF_MSA3XX_ID
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["msa3xx"]

View file

@ -171,6 +171,11 @@ void MSA3xxComponent::dump_config() {
LOG_SENSOR(" ", "Acceleration Y", this->acceleration_y_sensor_);
LOG_SENSOR(" ", "Acceleration Z", this->acceleration_z_sensor_);
#endif
#ifdef USE_TEXT_SENSOR
LOG_TEXT_SENSOR(" ", "Orientation XY", this->orientation_xy_text_sensor_);
LOG_TEXT_SENSOR(" ", "Orientation Z", this->orientation_z_text_sensor_);
#endif
}
bool MSA3xxComponent::read_data_() {
@ -256,6 +261,19 @@ void MSA3xxComponent::update() {
this->acceleration_z_sensor_->publish_state(this->data_.z);
#endif
#ifdef USE_TEXT_SENSOR
if (this->orientation_xy_text_sensor_ != nullptr &&
this->status_.orientation.orient_xy != this->status_.orientation_old.orient_xy) {
this->orientation_xy_text_sensor_->publish_state(orientation_xy_to_string(this->status_.orientation.orient_xy));
}
if (this->orientation_z_text_sensor_ != nullptr &&
this->status_.orientation.orient_z != this->status_.orientation_old.orient_z) {
this->orientation_z_text_sensor_->publish_state(orientation_z_to_string(this->status_.orientation.orient_z));
}
this->status_.orientation_old = this->status_.orientation;
#endif
this->status_clear_warning();
}
float MSA3xxComponent::get_setup_priority() const { return setup_priority::DATA; }
@ -320,8 +338,9 @@ void MSA3xxComponent::setup_offset_(float offset_x, float offset_y, float offset
auto offset_g_to_lsb = [](float accel) -> int8_t {
float acccel_clamped = clamp(accel, G_OFFSET_MIN, G_OFFSET_MAX);
return static_cast<int8_t>(accel * LSB_COEFF);
return static_cast<int8_t>(acccel_clamped * LSB_COEFF);
};
offset[0] = offset_g_to_lsb(offset_x);
offset[1] = offset_g_to_lsb(offset_y);
offset[2] = offset_g_to_lsb(offset_z);

View file

@ -2,6 +2,7 @@
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/automation.h"
@ -226,6 +227,11 @@ class MSA3xxComponent : public PollingComponent, public i2c::I2CDevice {
SUB_SENSOR(acceleration_z)
#endif
#ifdef USE_TEXT_SENSOR
SUB_TEXT_SENSOR(orientation_xy)
SUB_TEXT_SENSOR(orientation_z)
#endif
Trigger<> *get_tap_trigger() { return &this->tap_trigger_; }
Trigger<> *get_double_tap_trigger() { return &this->double_tap_trigger_; }
Trigger<> *get_orientation_trigger() { return &this->orientation_trigger_; }
@ -256,6 +262,7 @@ class MSA3xxComponent : public PollingComponent, public i2c::I2CDevice {
struct {
RegMotionInterrupt motion_int;
RegOrientationStatus orientation;
RegOrientationStatus orientation_old;
} status_{};
void setup_odr_(DataRate rate);

View file

@ -12,6 +12,7 @@ from esphome.const import (
)
from . import MSA3xxComponent, CONF_MSA3XX_ID
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["msa3xx"]

View file

@ -0,0 +1,44 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor
from esphome.const import (
CONF_NAME,
)
from . import MSA3xxComponent, CONF_MSA3XX_ID
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["msa3xx"]
CONF_ORIENTATION_XY = "orientation_xy"
CONF_ORIENTATION_Z = "orientation_z"
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(CONF_MSA3XX_ID): cv.use_id(MSA3xxComponent),
cv.Optional(CONF_ORIENTATION_XY): cv.maybe_simple_value(
text_sensor.text_sensor_schema(),
key=CONF_NAME,
),
cv.Optional(CONF_ORIENTATION_Z): cv.maybe_simple_value(
text_sensor.text_sensor_schema(),
key=CONF_NAME,
),
}
).extend(cv.COMPONENT_SCHEMA)
)
async def setup_conf(config, key, hub):
if sensor_config := config.get(key):
var = await text_sensor.new_text_sensor(sensor_config)
cg.add(getattr(hub, f"set_{key}_text_sensor")(var))
async def to_code(config):
hub = await cg.get_variable(config[CONF_MSA3XX_ID])
SENSORS = [CONF_ORIENTATION_XY, CONF_ORIENTATION_Z]
for key in SENSORS:
await setup_conf(config, key, hub)