mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-28 00:38:28 +02:00
Add shutdown and safe_mode button (#2918)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Jos Suanet <jos@suanet.net>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import button
|
||||
from esphome.components.ota import OTAComponent
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_OTA,
|
||||
DEVICE_CLASS_RESTART,
|
||||
ENTITY_CATEGORY_CONFIG,
|
||||
ICON_RESTART_ALERT,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["ota"]
|
||||
|
||||
safe_mode_ns = cg.esphome_ns.namespace("safe_mode")
|
||||
SafeModeButton = safe_mode_ns.class_("SafeModeButton", button.Button, cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
button.button_schema(
|
||||
device_class=DEVICE_CLASS_RESTART,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_RESTART_ALERT,
|
||||
)
|
||||
.extend({cv.GenerateID(): cv.declare_id(SafeModeButton)})
|
||||
.extend({cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent)})
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await button.register_button(var, config)
|
||||
|
||||
ota = await cg.get_variable(config[CONF_OTA])
|
||||
cg.add(var.set_ota(ota))
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "safe_mode_button.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/application.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace safe_mode {
|
||||
|
||||
static const char *const TAG = "safe_mode.button";
|
||||
|
||||
void SafeModeButton::set_ota(ota::OTAComponent *ota) { this->ota_ = ota; }
|
||||
|
||||
void SafeModeButton::press_action() {
|
||||
ESP_LOGI(TAG, "Restarting device in safe mode...");
|
||||
this->ota_->set_safe_mode_pending(true);
|
||||
|
||||
// Let MQTT settle a bit
|
||||
delay(100); // NOLINT
|
||||
App.safe_reboot();
|
||||
}
|
||||
|
||||
void SafeModeButton::dump_config() { LOG_BUTTON("", "Safe Mode Button", this); }
|
||||
|
||||
} // namespace safe_mode
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/ota/ota_component.h"
|
||||
#include "esphome/components/button/button.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace safe_mode {
|
||||
|
||||
class SafeModeButton : public button::Button, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void set_ota(ota::OTAComponent *ota);
|
||||
|
||||
protected:
|
||||
ota::OTAComponent *ota_;
|
||||
void press_action() override;
|
||||
};
|
||||
|
||||
} // namespace safe_mode
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user