Compare commits

..
22 changed files with 48 additions and 255 deletions

No files matched your search

@@ -6,6 +6,8 @@ from esphome.const import (
)
from esphome.types import ConfigType
AUTO_LOAD = ["factory_reset"]
CONFIG_SCHEMA = button.button_schema(
button.ApplyButton,
device_class=DEVICE_CLASS_RESTART,
@@ -1,4 +1,4 @@
#include "factory_reset_button.h"
#include "factory_reset_device.h"
#include "esphome/core/defines.h"
@@ -11,7 +11,7 @@
namespace esphome::factory_reset {
ESPHOME_LOG_TAG(TAG, "factory_reset.button");
ESPHOME_LOG_TAG(TAG, "factory_reset");
static void reset_and_reboot() {
global_preferences->reset();
@@ -2,7 +2,7 @@
namespace esphome::factory_reset {
/// Run by the factory reset button.
/// Run by the factory reset button and switch.
void factory_reset_device();
} // namespace esphome::factory_reset
@@ -1,23 +1,21 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_RESTART_ALERT
from esphome.types import ConfigType
from .. import factory_reset_ns
FactoryResetSwitch = factory_reset_ns.class_(
"FactoryResetSwitch", switch.Switch, cg.Component
)
AUTO_LOAD = ["factory_reset"]
CONFIG_SCHEMA = switch.switch_schema(
FactoryResetSwitch,
switch.ApplySwitch,
block_inverted=True,
icon=ICON_RESTART_ALERT,
entity_category=ENTITY_CATEGORY_CONFIG,
).extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_component(var, config)
await switch.sub_switches(config).create(
config,
None,
apply="factory_reset::factory_reset_device()",
publish="momentary",
)
@@ -1,41 +0,0 @@
#include "factory_reset_switch.h"
#include "esphome/core/defines.h"
#ifdef USE_OPENTHREAD
#include "esphome/components/openthread/openthread.h"
#endif
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome::factory_reset {
ESPHOME_LOG_TAG(TAG, "factory_reset.switch");
void FactoryResetSwitch::dump_config() { LOG_SWITCH("", "Factory Reset Switch", this); }
void FactoryResetSwitch::write_state(bool state) {
// Acknowledge
this->publish_state(false);
if (state) {
ESP_LOGI(TAG, "Resetting");
// Let MQTT settle a bit
delay(100); // NOLINT
#ifdef USE_OPENTHREAD
openthread::global_openthread_component->on_factory_reset(FactoryResetSwitch::factory_reset_callback);
#else
global_preferences->reset();
App.safe_reboot();
#endif
}
}
#ifdef USE_OPENTHREAD
void FactoryResetSwitch::factory_reset_callback() {
global_preferences->reset();
App.safe_reboot();
}
#endif
} // namespace esphome::factory_reset
@@ -1,20 +0,0 @@
#pragma once
#include "esphome/components/switch/switch.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
namespace esphome::factory_reset {
class FactoryResetSwitch final : public switch_::Switch, public Component {
public:
void dump_config() override;
#ifdef USE_OPENTHREAD
static void factory_reset_callback();
#endif
protected:
void write_state(bool state) override;
};
} // namespace esphome::factory_reset
@@ -2,6 +2,8 @@ from esphome.components import button
from esphome.const import DEVICE_CLASS_RESTART, ENTITY_CATEGORY_CONFIG, ICON_RESTART
from esphome.types import ConfigType
AUTO_LOAD = ["restart"]
CONFIG_SCHEMA = button.button_schema(
button.ApplyButton,
icon=ICON_RESTART,
@@ -1,11 +1,11 @@
#include "restart_button.h"
#include "restart.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome::restart {
ESPHOME_LOG_TAG(TAG, "restart.button");
ESPHOME_LOG_TAG(TAG, "restart");
void restart_device() {
ESP_LOGI(TAG, "Restarting device");
@@ -2,7 +2,7 @@
namespace esphome::restart {
/// Run by the restart button.
/// Run by the restart button and switch.
void restart_device();
} // namespace esphome::restart
@@ -1,20 +1,18 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_RESTART
from esphome.types import ConfigType
restart_ns = cg.esphome_ns.namespace("restart")
RestartSwitch = restart_ns.class_("RestartSwitch", switch.Switch, cg.Component)
AUTO_LOAD = ["restart"]
CONFIG_SCHEMA = switch.switch_schema(
RestartSwitch,
switch.ApplySwitch,
icon=ICON_RESTART,
entity_category=ENTITY_CATEGORY_CONFIG,
block_inverted=True,
).extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_component(var, config)
await switch.sub_switches(config).create(
config, None, apply="restart::restart_device()", publish="momentary"
)
@@ -1,23 +0,0 @@
#include "restart_switch.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome::restart {
ESPHOME_LOG_TAG(TAG, "restart");
void RestartSwitch::write_state(bool state) {
// Acknowledge
this->publish_state(false);
if (state) {
ESP_LOGI(TAG, "Restarting device");
// Let MQTT settle a bit
delay(100); // NOLINT
App.safe_reboot();
}
}
void RestartSwitch::dump_config() { LOG_SWITCH("", "Restart Switch", this); }
} // namespace esphome::restart
@@ -1,19 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/switch/switch.h"
namespace esphome::restart {
class RestartSwitch final : public switch_::Switch, public Component {
public:
// User provided, not "= default": `new(p) RestartSwitch()` would zero-fill .bss that is already zero.
RestartSwitch() {}
void dump_config() override;
protected:
void write_state(bool state) override;
};
} // namespace esphome::restart
+1 -1
View File
@@ -25,7 +25,7 @@ class SafeModeComponent final : public Component {
/// Set to true if the next startup will enter safe mode
void set_safe_mode_pending(const bool &pending);
bool get_safe_mode_pending();
/// Set safe mode pending and reboot; run by the safe mode button.
/// Set safe mode pending and reboot; run by the safe mode button and switch.
void restart_in_safe_mode();
void dump_config() override;
+11 -18
View File
@@ -1,29 +1,22 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_SAFE_MODE, ENTITY_CATEGORY_CONFIG, ICON_RESTART_ALERT
from esphome.types import ConfigType
from .. import SafeModeComponent, safe_mode_ns
from .. import SafeModeComponent
DEPENDENCIES = ["safe_mode"]
SafeModeSwitch = safe_mode_ns.class_("SafeModeSwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = (
switch.switch_schema(
SafeModeSwitch,
block_inverted=True,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RESTART_ALERT,
)
.extend({cv.GenerateID(CONF_SAFE_MODE): cv.use_id(SafeModeComponent)})
.extend(cv.COMPONENT_SCHEMA)
)
CONFIG_SCHEMA = switch.switch_schema(
switch.ApplySwitch,
block_inverted=True,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RESTART_ALERT,
).extend({cv.GenerateID(CONF_SAFE_MODE): cv.use_id(SafeModeComponent)})
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_component(var, config)
await cg.add_variable(var.set_safe_mode, config[CONF_SAFE_MODE])
switches = switch.sub_switches(config, parent=config[CONF_SAFE_MODE])
await switches.create(
config, None, apply="restart_in_safe_mode()", publish="momentary"
)
@@ -1,26 +0,0 @@
#include "safe_mode_switch.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome::safe_mode {
ESPHOME_LOG_TAG(TAG, "safe_mode.switch");
void SafeModeSwitch::write_state(bool state) {
// Acknowledge
this->publish_state(false);
if (state) {
ESP_LOGI(TAG, "Restarting in safe mode");
this->safe_mode_component_->set_safe_mode_pending(true);
// Let MQTT settle a bit
delay(100); // NOLINT
App.safe_reboot();
}
}
void SafeModeSwitch::dump_config() { LOG_SWITCH("", "Safe Mode Switch", this); }
} // namespace esphome::safe_mode
@@ -1,19 +0,0 @@
#pragma once
#include "esphome/components/safe_mode/safe_mode.h"
#include "esphome/components/switch/switch.h"
#include "esphome/core/component.h"
namespace esphome::safe_mode {
class SafeModeSwitch final : public switch_::Switch, public Component {
public:
void dump_config() override;
void set_safe_mode(SafeModeComponent *safe_mode_component) { this->safe_mode_component_ = safe_mode_component; }
protected:
SafeModeComponent *safe_mode_component_;
void write_state(bool state) override;
};
} // namespace esphome::safe_mode
@@ -2,6 +2,8 @@ from esphome.components import button
from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_POWER
from esphome.types import ConfigType
AUTO_LOAD = ["shutdown"]
CONFIG_SCHEMA = button.button_schema(
button.ApplyButton, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_POWER
)
@@ -1,4 +1,4 @@
#include "shutdown_button.h"
#include "shutdown.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
@@ -12,7 +12,7 @@
namespace esphome::shutdown {
ESPHOME_LOG_TAG(TAG, "shutdown.button");
ESPHOME_LOG_TAG(TAG, "shutdown");
void shutdown_device() {
ESP_LOGI(TAG, "Shutting down");
@@ -2,7 +2,7 @@
namespace esphome::shutdown {
/// Run by the shutdown button.
/// Run by the shutdown button and switch.
void shutdown_device();
} // namespace esphome::shutdown
@@ -1,20 +1,18 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_POWER
from esphome.types import ConfigType
shutdown_ns = cg.esphome_ns.namespace("shutdown")
ShutdownSwitch = shutdown_ns.class_("ShutdownSwitch", switch.Switch, cg.Component)
AUTO_LOAD = ["shutdown"]
CONFIG_SCHEMA = switch.switch_schema(
ShutdownSwitch,
switch.ApplySwitch,
icon=ICON_POWER,
entity_category=ENTITY_CATEGORY_CONFIG,
block_inverted=True,
).extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_component(var, config)
await switch.sub_switches(config).create(
config, None, apply="shutdown::shutdown_device()", publish="momentary"
)
@@ -1,36 +0,0 @@
#include "shutdown_switch.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#ifdef USE_ESP32
#include <esp_sleep.h>
#endif
#ifdef USE_ESP8266
#include <Esp.h>
#endif
namespace esphome::shutdown {
ESPHOME_LOG_TAG(TAG, "shutdown.switch");
void ShutdownSwitch::dump_config() { LOG_SWITCH("", "Shutdown Switch", this); }
void ShutdownSwitch::write_state(bool state) {
// Acknowledge
this->publish_state(false);
if (state) {
ESP_LOGI(TAG, "Shutting down");
delay(100); // NOLINT
App.safe_shutdown(TEARDOWN_TIMEOUT_REBOOT_MS);
#ifdef USE_ESP8266
ESP.deepSleep(0); // NOLINT(readability-static-accessed-through-instance)
#endif
#ifdef USE_ESP32
esp_deep_sleep_start();
#endif
}
}
} // namespace esphome::shutdown
@@ -1,16 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/switch/switch.h"
namespace esphome::shutdown {
class ShutdownSwitch final : public switch_::Switch, public Component {
public:
void dump_config() override;
protected:
void write_state(bool state) override;
};
} // namespace esphome::shutdown