From 193a98bfb68033859ba47676e6a6a316f2690595 Mon Sep 17 00:00:00 2001 From: Anton Viktorov Date: Thu, 23 May 2024 11:53:41 +0200 Subject: [PATCH] text sensors --- CODEOWNERS | 1 + esphome/components/msa3xx/__init__.py | 2 + esphome/components/msa3xx/binary_sensor.py | 1 + esphome/components/msa3xx/msa3xx.cpp | 21 ++++++++++- esphome/components/msa3xx/msa3xx.h | 7 ++++ esphome/components/msa3xx/sensor.py | 1 + esphome/components/msa3xx/text_sensor.py | 44 ++++++++++++++++++++++ 7 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 esphome/components/msa3xx/text_sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index 22e581275b..4305cfa6fe 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -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 diff --git a/esphome/components/msa3xx/__init__.py b/esphome/components/msa3xx/__init__.py index 93adb3b1f2..f2766673f3 100644 --- a/esphome/components/msa3xx/__init__.py +++ b/esphome/components/msa3xx/__init__.py @@ -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" diff --git a/esphome/components/msa3xx/binary_sensor.py b/esphome/components/msa3xx/binary_sensor.py index 6ee5f49224..43e90150ca 100644 --- a/esphome/components/msa3xx/binary_sensor.py +++ b/esphome/components/msa3xx/binary_sensor.py @@ -2,6 +2,7 @@ import esphome.config_validation as cv from . import MSA3xxComponent, CONF_MSA3XX_ID +CODEOWNERS = ["@latonita"] DEPENDENCIES = ["msa3xx"] diff --git a/esphome/components/msa3xx/msa3xx.cpp b/esphome/components/msa3xx/msa3xx.cpp index 7e9e9ec0d9..3e10a92793 100644 --- a/esphome/components/msa3xx/msa3xx.cpp +++ b/esphome/components/msa3xx/msa3xx.cpp @@ -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(accel * LSB_COEFF); + return static_cast(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); diff --git a/esphome/components/msa3xx/msa3xx.h b/esphome/components/msa3xx/msa3xx.h index cf79438cef..b5d232fc00 100644 --- a/esphome/components/msa3xx/msa3xx.h +++ b/esphome/components/msa3xx/msa3xx.h @@ -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); diff --git a/esphome/components/msa3xx/sensor.py b/esphome/components/msa3xx/sensor.py index 371d5d82d3..54f1d85743 100644 --- a/esphome/components/msa3xx/sensor.py +++ b/esphome/components/msa3xx/sensor.py @@ -12,6 +12,7 @@ from esphome.const import ( ) from . import MSA3xxComponent, CONF_MSA3XX_ID +CODEOWNERS = ["@latonita"] DEPENDENCIES = ["msa3xx"] diff --git a/esphome/components/msa3xx/text_sensor.py b/esphome/components/msa3xx/text_sensor.py new file mode 100644 index 0000000000..ed69edec50 --- /dev/null +++ b/esphome/components/msa3xx/text_sensor.py @@ -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)