Compare commits

..

2 Commits

Author SHA1 Message Date
Jesse Hills
8ae8600883 Add to tests 2021-12-20 08:17:28 +13:00
Jesse Hills
048ce79235 Create shutdown button platform 2021-12-20 07:55:52 +13:00
9 changed files with 43 additions and 42 deletions

View File

@@ -133,7 +133,6 @@ esphome/components/rf_bridge/* @jesserockz
esphome/components/rgbct/* @jesserockz
esphome/components/rtttl/* @glmnet
esphome/components/safe_mode/* @paulmonigatti
esphome/components/safe_mode/button/* @jesserockz
esphome/components/scd4x/* @sjtrny
esphome/components/script/* @esphome/core
esphome/components/sdm_meter/* @jesserockz @polyfaces

View File

@@ -1,24 +0,0 @@
#include "safe_mode_button.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.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

View File

@@ -4,19 +4,15 @@ from esphome.components import button
from esphome.const import (
CONF_ID,
ENTITY_CATEGORY_CONFIG,
ICON_RESTART_ALERT,
ICON_POWER,
)
from .. import safe_mode_ns
CODEOWNERS = ["@jesserockz"]
SafeModeButton = safe_mode_ns.class_("SafeModeButton", button.Button, cg.Component)
shutdown_ns = cg.esphome_ns.namespace("shutdown")
ShutdownButton = shutdown_ns.class_("ShutdownButton", button.Button, cg.Component)
CONFIG_SCHEMA = (
button.button_schema(
entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_RESTART_ALERT
)
.extend({cv.GenerateID(): cv.declare_id(SafeModeButton)})
button.button_schema(entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_POWER)
.extend({cv.GenerateID(): cv.declare_id(ShutdownButton)})
.extend(cv.COMPONENT_SCHEMA)
)

View File

@@ -0,0 +1,33 @@
#include "shutdown_button.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#ifdef USE_ESP32
#include <esp_sleep.h>
#endif
#ifdef USE_ESP8266
#include <Esp.h>
#endif
namespace esphome {
namespace shutdown {
static const char *const TAG = "shutdown.button";
void ShutdownButton::press_action() {
ESP_LOGI(TAG, "Shutting down...");
delay(100); // NOLINT
App.run_safe_shutdown_hooks();
#ifdef USE_ESP8266
ESP.deepSleep(0); // NOLINT(readability-static-accessed-through-instance)
#endif
#ifdef USE_ESP32
esp_deep_sleep_start();
#endif
}
void ShutdownButton::dump_config() { LOG_BUTTON("", "Shutdown Button", this); }
} // namespace shutdown
} // namespace esphome

View File

@@ -1,21 +1,18 @@
#pragma once
#include "esphome/components/button/button.h"
#include "esphome/components/ota/ota_component.h"
#include "esphome/core/component.h"
namespace esphome {
namespace safe_mode {
namespace shutdown {
class SafeModeButton : public button::Button, public Component {
class ShutdownButton : 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 shutdown
} // namespace esphome

View File

@@ -525,5 +525,5 @@ xpt2046:
button:
- platform: restart
name: Restart Button
- platform: safe_mode
name: Safe Mode Button
- platform: shutdown
name: Shutdown Button