From db666e44a7756f50d60202ac84019bc9a338eab6 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 3 Mar 2025 15:35:52 +1300 Subject: [PATCH] [ltr390] Move calculation to allow dynamic setting of gain and resolution (#8343) --- esphome/components/ltr390/ltr390.cpp | 14 +++++++------- esphome/components/ltr390/ltr390.h | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/esphome/components/ltr390/ltr390.cpp b/esphome/components/ltr390/ltr390.cpp index 198d15ebd8..84b2eba2de 100644 --- a/esphome/components/ltr390/ltr390.cpp +++ b/esphome/components/ltr390/ltr390.cpp @@ -1,7 +1,7 @@ #include "ltr390.h" +#include #include "esphome/core/hal.h" #include "esphome/core/log.h" -#include namespace esphome { namespace ltr390 { @@ -91,7 +91,12 @@ void LTR390Component::read_uvs_() { uint32_t uv = *val; if (this->uvi_sensor_ != nullptr) { - this->uvi_sensor_->publish_state((uv / this->sensitivity_uv_) * this->wfac_); + // Set sensitivity by linearly scaling against known value in the datasheet + float gain_scale_uv = GAINVALUES[this->gain_uv_] / GAIN_MAX; + float intg_scale_uv = (RESOLUTIONVALUE[this->res_uv_] * 100) / INTG_MAX; + float sensitivity_uv = SENSITIVITY_MAX * gain_scale_uv * intg_scale_uv; + + this->uvi_sensor_->publish_state((uv / sensitivity_uv) * this->wfac_); } if (this->uv_sensor_ != nullptr) { @@ -166,11 +171,6 @@ void LTR390Component::setup() { return; } - // Set sensitivity by linearly scaling against known value in the datasheet - float gain_scale_uv = GAINVALUES[this->gain_uv_] / GAIN_MAX; - float intg_scale_uv = (RESOLUTIONVALUE[this->res_uv_] * 100) / INTG_MAX; - this->sensitivity_uv_ = SENSITIVITY_MAX * gain_scale_uv * intg_scale_uv; - // Set sensor read state this->reading_ = false; diff --git a/esphome/components/ltr390/ltr390.h b/esphome/components/ltr390/ltr390.h index 24afd3c411..7359cbd336 100644 --- a/esphome/components/ltr390/ltr390.h +++ b/esphome/components/ltr390/ltr390.h @@ -77,7 +77,6 @@ class LTR390Component : public PollingComponent, public i2c::I2CDevice { LTR390GAIN gain_uv_; LTR390RESOLUTION res_als_; LTR390RESOLUTION res_uv_; - float sensitivity_uv_; float wfac_; sensor::Sensor *light_sensor_{nullptr};