Merge branch 'lock_state_store_progmem_esp8266' into integration

This commit is contained in:
J. Nick Koston
2025-11-28 10:13:34 -06:00
4 changed files with 24 additions and 15 deletions

View File

@@ -7,21 +7,21 @@ namespace esphome::lock {
static const char *const TAG = "lock";
const char *lock_state_to_string(LockState state) {
const LogString *lock_state_to_string(LockState state) {
switch (state) {
case LOCK_STATE_LOCKED:
return "LOCKED";
return LOG_STR("LOCKED");
case LOCK_STATE_UNLOCKED:
return "UNLOCKED";
return LOG_STR("UNLOCKED");
case LOCK_STATE_JAMMED:
return "JAMMED";
return LOG_STR("JAMMED");
case LOCK_STATE_LOCKING:
return "LOCKING";
return LOG_STR("LOCKING");
case LOCK_STATE_UNLOCKING:
return "UNLOCKING";
return LOG_STR("UNLOCKING");
case LOCK_STATE_NONE:
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
@@ -52,7 +52,7 @@ void Lock::publish_state(LockState state) {
this->state = state;
this->rtc_.save(&this->state);
ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), lock_state_to_string(state));
ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), LOG_STR_ARG(lock_state_to_string(state)));
this->state_callback_.call();
#if defined(USE_LOCK) && defined(USE_CONTROLLER_REGISTRY)
ControllerRegistry::notify_lock_update(this);
@@ -65,8 +65,7 @@ void LockCall::perform() {
ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
this->validate_();
if (this->state_.has_value()) {
const char *state_s = lock_state_to_string(*this->state_);
ESP_LOGD(TAG, " State: %s", state_s);
ESP_LOGD(TAG, " State: %s", LOG_STR_ARG(lock_state_to_string(*this->state_)));
}
this->parent_->control(*this);
}
@@ -74,7 +73,7 @@ void LockCall::validate_() {
if (this->state_.has_value()) {
auto state = *this->state_;
if (!this->parent_->traits.supports_state(state)) {
ESP_LOGW(TAG, " State %s is not supported by this device!", lock_state_to_string(*this->state_));
ESP_LOGW(TAG, " State %s is not supported by this device!", LOG_STR_ARG(lock_state_to_string(*this->state_)));
this->state_.reset();
}
}

View File

@@ -30,7 +30,10 @@ enum LockState : uint8_t {
LOCK_STATE_LOCKING = 4,
LOCK_STATE_UNLOCKING = 5
};
const char *lock_state_to_string(LockState state);
const LogString *lock_state_to_string(LockState state);
/// Maximum length of lock state string (including null terminator): "UNLOCKING" = 10
static constexpr size_t LOCK_STATE_STR_SIZE = 10;
class LockTraits {
public:

View File

@@ -48,8 +48,14 @@ void MQTTLockComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfi
bool MQTTLockComponent::send_initial_state() { return this->publish_state(); }
bool MQTTLockComponent::publish_state() {
std::string payload = lock_state_to_string(this->lock_->state);
return this->publish(this->get_state_topic_(), payload);
#ifdef USE_STORE_LOG_STR_IN_FLASH
char buf[LOCK_STATE_STR_SIZE];
strncpy_P(buf, (PGM_P) lock_state_to_string(this->lock_->state), sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
return this->publish(this->get_state_topic_(), buf);
#else
return this->publish(this->get_state_topic_(), LOG_STR_ARG(lock_state_to_string(this->lock_->state)));
#endif
}
} // namespace mqtt

View File

@@ -1482,7 +1482,8 @@ std::string WebServer::lock_json(lock::Lock *obj, lock::LockState value, JsonDet
json::JsonBuilder builder;
JsonObject root = builder.root();
set_json_icon_state_value(root, obj, "lock", lock::lock_state_to_string(value), value, start_config);
char buf[lock::LOCK_STATE_STR_SIZE];
set_json_icon_state_value(root, obj, "lock", PSTR_LOCAL(lock::lock_state_to_string(value)), value, start_config);
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}