[ltr501][pvvx_mithermometer][smt100] Convert static locals to instance members (#14569)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
This commit is contained in:
Jonathan Swoboda
2026-03-06 16:03:30 -05:00
committed by GitHub
parent 2c83c6a79f
commit 587bf68091
6 changed files with 28 additions and 24 deletions
+10 -11
View File
@@ -146,7 +146,6 @@ void LTRAlsPs501Component::update() {
void LTRAlsPs501Component::loop() {
ErrorCode err = i2c::ERROR_OK;
static uint8_t tries{0};
switch (this->state_) {
case State::DELAYED_SETUP:
@@ -175,20 +174,20 @@ void LTRAlsPs501Component::loop() {
case State::WAITING_FOR_DATA:
if (this->is_als_data_ready_(this->als_readings_) == LtrDataAvail::LTR_DATA_OK) {
tries = 0;
this->tries_ = 0;
ESP_LOGV(TAG, "Reading sensor data assuming gain = %.0fx, time = %d ms",
get_gain_coeff(this->als_readings_.gain), get_itime_ms(this->als_readings_.integration_time));
this->read_sensor_data_(this->als_readings_);
this->apply_lux_calculation_(this->als_readings_);
this->state_ = State::DATA_COLLECTED;
} else if (tries >= MAX_TRIES) {
} else if (this->tries_ >= MAX_TRIES) {
ESP_LOGW(TAG, "Can't get data after several tries. Aborting.");
tries = 0;
this->tries_ = 0;
this->status_set_warning();
this->state_ = State::IDLE;
return;
} else {
tries++;
this->tries_++;
}
break;
@@ -230,21 +229,21 @@ void LTRAlsPs501Component::loop() {
}
void LTRAlsPs501Component::check_and_trigger_ps_() {
static uint32_t last_high_trigger_time{0};
static uint32_t last_low_trigger_time{0};
uint16_t ps_data = this->read_ps_data_();
uint32_t now = millis();
if (ps_data != this->ps_readings_) {
this->ps_readings_ = ps_data;
// Higher values - object is closer to sensor
if (ps_data > this->ps_threshold_high_ && now - last_high_trigger_time >= this->ps_cooldown_time_s_ * 1000) {
last_high_trigger_time = now;
if (ps_data > this->ps_threshold_high_ &&
now - this->last_ps_high_trigger_time_ >= this->ps_cooldown_time_s_ * 1000) {
this->last_ps_high_trigger_time_ = now;
ESP_LOGD(TAG, "Proximity high threshold triggered. Value = %d, Trigger level = %d", ps_data,
this->ps_threshold_high_);
this->on_ps_high_trigger_callback_.call();
} else if (ps_data < this->ps_threshold_low_ && now - last_low_trigger_time >= this->ps_cooldown_time_s_ * 1000) {
last_low_trigger_time = now;
} else if (ps_data < this->ps_threshold_low_ &&
now - this->last_ps_low_trigger_time_ >= this->ps_cooldown_time_s_ * 1000) {
this->last_ps_low_trigger_time_ = now;
ESP_LOGD(TAG, "Proximity low threshold triggered. Value = %d, Trigger level = %d", ps_data,
this->ps_threshold_low_);
this->on_ps_low_trigger_callback_.call();
+3
View File
@@ -74,6 +74,7 @@ class LTRAlsPs501Component : public PollingComponent, public i2c::I2CDevice {
READY_TO_PUBLISH,
KEEP_PUBLISHING
} state_{State::NOT_INITIALIZED};
uint8_t tries_{0};
LtrType ltr_type_{LtrType::LTR_TYPE_ALS_ONLY};
@@ -130,6 +131,8 @@ class LTRAlsPs501Component : public PollingComponent, public i2c::I2CDevice {
PsGain501 ps_gain_{PsGain501::PS_GAIN_1};
uint16_t ps_threshold_high_{0xffff};
uint16_t ps_threshold_low_{0x0000};
uint32_t last_ps_high_trigger_time_{0};
uint32_t last_ps_low_trigger_time_{0};
//
// Sensors for publishing data
@@ -66,12 +66,11 @@ optional<ParseResult> PVVXMiThermometer::parse_header_(const esp32_ble_tracker::
return {};
}
static uint8_t last_frame_count = 0;
if (last_frame_count == raw[13]) {
ESP_LOGVV(TAG, "parse_header(): duplicate data packet received (%hhu).", last_frame_count);
if (this->last_frame_count_ == raw[13]) {
ESP_LOGVV(TAG, "parse_header(): duplicate data packet received (%hhu).", this->last_frame_count_);
return {};
}
last_frame_count = raw[13];
this->last_frame_count_ = raw[13];
return result;
}
@@ -39,6 +39,8 @@ class PVVXMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevic
sensor::Sensor *battery_voltage_{nullptr};
sensor::Sensor *signal_strength_{nullptr};
uint8_t last_frame_count_{0};
optional<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);
bool report_results_(const optional<ParseResult> &result, const char *address);
+7 -9
View File
@@ -12,10 +12,9 @@ void SMT100Component::update() {
}
void SMT100Component::loop() {
static char buffer[MAX_LINE_LENGTH];
while (this->available() != 0) {
if (readline_(read(), buffer, MAX_LINE_LENGTH) > 0) {
int counts = (int) strtol((strtok(buffer, ",")), nullptr, 10);
if (this->readline_(this->read(), this->readline_buffer_, MAX_LINE_LENGTH) > 0) {
int counts = (int) strtol((strtok(this->readline_buffer_, ",")), nullptr, 10);
float permittivity = (float) strtod((strtok(nullptr, ",")), nullptr);
float moisture = (float) strtod((strtok(nullptr, ",")), nullptr);
float temperature = (float) strtod((strtok(nullptr, ",")), nullptr);
@@ -56,7 +55,6 @@ void SMT100Component::dump_config() {
}
int SMT100Component::readline_(int readch, char *buffer, int len) {
static int pos = 0;
int rpos;
if (readch > 0) {
@@ -64,13 +62,13 @@ int SMT100Component::readline_(int readch, char *buffer, int len) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
rpos = this->readline_pos_;
this->readline_pos_ = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len - 1) {
buffer[pos++] = readch;
buffer[pos] = 0;
if (this->readline_pos_ < len - 1) {
buffer[this->readline_pos_++] = readch;
buffer[this->readline_pos_] = 0;
}
}
}
+3
View File
@@ -28,6 +28,9 @@ class SMT100Component : public PollingComponent, public uart::UARTDevice {
protected:
int readline_(int readch, char *buffer, int len);
char readline_buffer_[MAX_LINE_LENGTH]{};
int readline_pos_{0};
sensor::Sensor *counts_sensor_{nullptr};
sensor::Sensor *permittivity_sensor_{nullptr};
sensor::Sensor *moisture_sensor_{nullptr};