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
7 changed files with 76 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button
from esphome.const import (
CONF_ID,
ENTITY_CATEGORY_CONFIG,
ICON_POWER,
)
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_POWER)
.extend({cv.GenerateID(): cv.declare_id(ShutdownButton)})
.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)

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

@@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/button/button.h"
#include "esphome/core/component.h"
namespace esphome {
namespace shutdown {
class ShutdownButton : public button::Button, public Component {
public:
void dump_config() override;
protected:
void press_action() override;
};
} // namespace shutdown
} // namespace esphome

View File

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