[lock] Store lock state strings in flash on ESP8266 (#12163)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1334,7 +1334,7 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf
|
||||
const auto traits = obj->get_traits();
|
||||
int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
|
||||
int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
|
||||
char buf[16];
|
||||
char buf[PSTR_LOCAL_SIZE];
|
||||
|
||||
if (start_config == DETAIL_ALL) {
|
||||
JsonArray opt = root["modes"].to<JsonArray>();
|
||||
@@ -1484,7 +1484,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[PSTR_LOCAL_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);
|
||||
}
|
||||
@@ -1645,7 +1646,7 @@ std::string WebServer::alarm_control_panel_json(alarm_control_panel::AlarmContro
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
|
||||
char buf[16];
|
||||
char buf[PSTR_LOCAL_SIZE];
|
||||
set_json_icon_state_value(root, obj, "alarm-control-panel", PSTR_LOCAL(alarm_control_panel_state_to_string(value)),
|
||||
value, start_config);
|
||||
if (start_config == DETAIL_ALL) {
|
||||
|
||||
Reference in New Issue
Block a user