[wifi_info] Reduce heap usage by up to 1.7KB in scan_results sensor (#11723)

This commit is contained in:
J. Nick Koston
2025-11-05 09:14:21 -06:00
committed by GitHub
parent 32975c9d8b
commit 6b522dfee6

View File

@@ -10,6 +10,8 @@
namespace esphome {
namespace wifi_info {
static constexpr size_t MAX_STATE_LENGTH = 255;
class IPAddressWiFiInfo : public PollingComponent, public text_sensor::TextSensor {
public:
void update() override {
@@ -71,11 +73,14 @@ class ScanResultsWiFiInfo : public PollingComponent, public text_sensor::TextSen
scan_results += "dB\n";
}
if (this->last_scan_results_ != scan_results) {
this->last_scan_results_ = scan_results;
// There's a limit of 255 characters per state.
// Longer states just don't get sent so we truncate it.
this->publish_state(scan_results.substr(0, 255));
if (scan_results.length() > MAX_STATE_LENGTH) {
scan_results.resize(MAX_STATE_LENGTH);
}
if (this->last_scan_results_ != scan_results) {
this->last_scan_results_ = scan_results;
this->publish_state(scan_results);
}
}
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }