1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00

[template] Store alarm control panel codes in flash instead of heap (#13329)

This commit is contained in:
J. Nick Koston
2026-01-17 15:52:06 -10:00
committed by GitHub
parent 4cc0f874f7
commit d6a0c8ffbb
4 changed files with 20 additions and 8 deletions

View File

@@ -118,8 +118,7 @@ async def to_code(config):
var = await alarm_control_panel.new_alarm_control_panel(config) var = await alarm_control_panel.new_alarm_control_panel(config)
await cg.register_component(var, config) await cg.register_component(var, config)
if CONF_CODES in config: if CONF_CODES in config:
for acode in config[CONF_CODES]: cg.add(var.set_codes(config[CONF_CODES]))
cg.add(var.add_code(acode))
if CONF_REQUIRES_CODE_TO_ARM in config: if CONF_REQUIRES_CODE_TO_ARM in config:
cg.add(var.set_requires_code_to_arm(config[CONF_REQUIRES_CODE_TO_ARM])) cg.add(var.set_requires_code_to_arm(config[CONF_REQUIRES_CODE_TO_ARM]))

View File

@@ -206,7 +206,13 @@ bool TemplateAlarmControlPanel::is_code_valid_(optional<std::string> code) {
if (!this->codes_.empty()) { if (!this->codes_.empty()) {
if (code.has_value()) { if (code.has_value()) {
ESP_LOGVV(TAG, "Checking code: %s", code.value().c_str()); ESP_LOGVV(TAG, "Checking code: %s", code.value().c_str());
return (std::count(this->codes_.begin(), this->codes_.end(), code.value()) == 1); // Use strcmp for const char* comparison
const char *code_cstr = code.value().c_str();
for (const char *stored_code : this->codes_) {
if (strcmp(stored_code, code_cstr) == 0)
return true;
}
return false;
} }
ESP_LOGD(TAG, "No code provided"); ESP_LOGD(TAG, "No code provided");
return false; return false;

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cinttypes> #include <cinttypes>
#include <cstring>
#include <vector> #include <vector>
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
@@ -86,11 +87,14 @@ class TemplateAlarmControlPanel final : public alarm_control_panel::AlarmControl
AlarmSensorType type = ALARM_SENSOR_TYPE_DELAYED); AlarmSensorType type = ALARM_SENSOR_TYPE_DELAYED);
#endif #endif
/** add a code /** Set the codes (from initializer list).
* *
* @param code The code * @param codes The list of valid codes
*/ */
void add_code(const std::string &code) { this->codes_.push_back(code); } void set_codes(std::initializer_list<const char *> codes) { this->codes_ = codes; }
// Deleted overload to catch incorrect std::string usage at compile time
void set_codes(std::initializer_list<std::string> codes) = delete;
/** set requires a code to arm /** set requires a code to arm
* *
@@ -155,8 +159,8 @@ class TemplateAlarmControlPanel final : public alarm_control_panel::AlarmControl
uint32_t pending_time_; uint32_t pending_time_;
// the time in trigger // the time in trigger
uint32_t trigger_time_; uint32_t trigger_time_;
// a list of codes // a list of codes (const char* pointers to string literals in flash)
std::vector<std::string> codes_; FixedVector<const char *> codes_;
// requires a code to arm // requires a code to arm
bool requires_code_to_arm_ = false; bool requires_code_to_arm_ = false;
bool supports_arm_home_ = false; bool supports_arm_home_ = false;

View File

@@ -9,6 +9,8 @@ alarm_control_panel:
name: Alarm Panel name: Alarm Panel
codes: codes:
- "1234" - "1234"
- "5678"
- "0000"
requires_code_to_arm: true requires_code_to_arm: true
arming_home_time: 1s arming_home_time: 1s
arming_night_time: 1s arming_night_time: 1s
@@ -29,6 +31,7 @@ alarm_control_panel:
name: Alarm Panel 2 name: Alarm Panel 2
codes: codes:
- "1234" - "1234"
- "9999"
requires_code_to_arm: true requires_code_to_arm: true
arming_home_time: 1s arming_home_time: 1s
arming_night_time: 1s arming_night_time: 1s