Add on_safe_mode trigger (#6790)

This commit is contained in:
Keith Burzinski 2024-05-22 14:43:13 -05:00 committed by GitHub
parent 1ca7c2d7dd
commit 9a6fde21ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 0 deletions

View File

@ -7,15 +7,20 @@ from esphome.const import (
CONF_NUM_ATTEMPTS, CONF_NUM_ATTEMPTS,
CONF_REBOOT_TIMEOUT, CONF_REBOOT_TIMEOUT,
CONF_SAFE_MODE, CONF_SAFE_MODE,
CONF_TRIGGER_ID,
KEY_PAST_SAFE_MODE, KEY_PAST_SAFE_MODE,
) )
from esphome.core import CORE, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
from esphome import automation
CODEOWNERS = ["@paulmonigatti", "@jsuanet", "@kbx81"] CODEOWNERS = ["@paulmonigatti", "@jsuanet", "@kbx81"]
CONF_ON_SAFE_MODE = "on_safe_mode"
safe_mode_ns = cg.esphome_ns.namespace("safe_mode") safe_mode_ns = cg.esphome_ns.namespace("safe_mode")
SafeModeComponent = safe_mode_ns.class_("SafeModeComponent", cg.Component) SafeModeComponent = safe_mode_ns.class_("SafeModeComponent", cg.Component)
SafeModeTrigger = safe_mode_ns.class_("SafeModeTrigger", automation.Trigger.template())
def _remove_id_if_disabled(value): def _remove_id_if_disabled(value):
@ -34,6 +39,11 @@ CONFIG_SCHEMA = cv.All(
cv.Optional( cv.Optional(
CONF_REBOOT_TIMEOUT, default="5min" CONF_REBOOT_TIMEOUT, default="5min"
): cv.positive_time_period_milliseconds, ): cv.positive_time_period_milliseconds,
cv.Optional(CONF_ON_SAFE_MODE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SafeModeTrigger),
}
),
} }
).extend(cv.COMPONENT_SCHEMA), ).extend(cv.COMPONENT_SCHEMA),
_remove_id_if_disabled, _remove_id_if_disabled,
@ -48,6 +58,10 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)
for conf in config.get(CONF_ON_SAFE_MODE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
condition = var.should_enter_safe_mode( condition = var.should_enter_safe_mode(
config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT] config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT]
) )

View File

@ -0,0 +1,17 @@
#pragma once
#include "safe_mode.h"
#include "esphome/core/automation.h"
namespace esphome {
namespace safe_mode {
class SafeModeTrigger : public Trigger<> {
public:
explicit SafeModeTrigger(SafeModeComponent *parent) {
parent->add_on_safe_mode_callback([this, parent]() { trigger(); });
}
};
} // namespace safe_mode
} // namespace esphome

View File

@ -94,6 +94,8 @@ bool SafeModeComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t en
ESP_LOGW(TAG, "SAFE MODE IS ACTIVE"); ESP_LOGW(TAG, "SAFE MODE IS ACTIVE");
this->safe_mode_callback_.call();
return true; return true;
} else { } else {
// increment counter // increment counter

View File

@ -25,6 +25,10 @@ class SafeModeComponent : public Component {
void on_safe_shutdown() override; void on_safe_shutdown() override;
void add_on_safe_mode_callback(std::function<void()> &&callback) {
this->safe_mode_callback_.add(std::move(callback));
}
protected: protected:
void write_rtc_(uint32_t val); void write_rtc_(uint32_t val);
uint32_t read_rtc_(); uint32_t read_rtc_();
@ -35,6 +39,7 @@ class SafeModeComponent : public Component {
uint32_t safe_mode_rtc_value_; uint32_t safe_mode_rtc_value_;
uint8_t safe_mode_num_attempts_; uint8_t safe_mode_num_attempts_;
ESPPreferenceObject rtc_; ESPPreferenceObject rtc_;
CallbackManager<void()> safe_mode_callback_{};
static const uint32_t ENTER_SAFE_MODE_MAGIC = static const uint32_t ENTER_SAFE_MODE_MAGIC =
0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot 0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot

View File

@ -5,6 +5,8 @@ wifi:
safe_mode: safe_mode:
num_attempts: 3 num_attempts: 3
reboot_timeout: 2min reboot_timeout: 2min
on_safe_mode:
- logger.log: Time for safe mode
button: button:
- platform: safe_mode - platform: safe_mode