mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-01 02:28:28 +02:00
FIX: Unnecessary flash writes by ModbusSwitch component (#3648)
This commit is contained in:
@@ -2,20 +2,10 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import switch
|
||||
from esphome.const import CONF_INTERLOCK, CONF_PIN, CONF_RESTORE_MODE
|
||||
from esphome.const import CONF_INTERLOCK, CONF_PIN
|
||||
from .. import gpio_ns
|
||||
|
||||
GPIOSwitch = gpio_ns.class_("GPIOSwitch", switch.Switch, cg.Component)
|
||||
GPIOSwitchRestoreMode = gpio_ns.enum("GPIOSwitchRestoreMode")
|
||||
|
||||
RESTORE_MODES = {
|
||||
"RESTORE_DEFAULT_OFF": GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_DEFAULT_OFF,
|
||||
"RESTORE_DEFAULT_ON": GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_DEFAULT_ON,
|
||||
"ALWAYS_OFF": GPIOSwitchRestoreMode.GPIO_SWITCH_ALWAYS_OFF,
|
||||
"ALWAYS_ON": GPIOSwitchRestoreMode.GPIO_SWITCH_ALWAYS_ON,
|
||||
"RESTORE_INVERTED_DEFAULT_OFF": GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_OFF,
|
||||
"RESTORE_INVERTED_DEFAULT_ON": GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_ON,
|
||||
}
|
||||
|
||||
CONF_INTERLOCK_WAIT_TIME = "interlock_wait_time"
|
||||
CONFIG_SCHEMA = (
|
||||
@@ -23,9 +13,6 @@ CONFIG_SCHEMA = (
|
||||
.extend(
|
||||
{
|
||||
cv.Required(CONF_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum(
|
||||
RESTORE_MODES, upper=True, space="_"
|
||||
),
|
||||
cv.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_id(switch.Switch)),
|
||||
cv.Optional(
|
||||
CONF_INTERLOCK_WAIT_TIME, default="0ms"
|
||||
@@ -43,8 +30,6 @@ async def to_code(config):
|
||||
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||
cg.add(var.set_pin(pin))
|
||||
|
||||
cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))
|
||||
|
||||
if CONF_INTERLOCK in config:
|
||||
interlock = []
|
||||
for it in config[CONF_INTERLOCK]:
|
||||
|
||||
@@ -10,27 +10,7 @@ float GPIOSwitch::get_setup_priority() const { return setup_priority::HARDWARE;
|
||||
void GPIOSwitch::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up GPIO Switch '%s'...", this->name_.c_str());
|
||||
|
||||
bool initial_state = false;
|
||||
switch (this->restore_mode_) {
|
||||
case GPIO_SWITCH_RESTORE_DEFAULT_OFF:
|
||||
initial_state = this->get_initial_state().value_or(false);
|
||||
break;
|
||||
case GPIO_SWITCH_RESTORE_DEFAULT_ON:
|
||||
initial_state = this->get_initial_state().value_or(true);
|
||||
break;
|
||||
case GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_OFF:
|
||||
initial_state = !this->get_initial_state().value_or(true);
|
||||
break;
|
||||
case GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_ON:
|
||||
initial_state = !this->get_initial_state().value_or(false);
|
||||
break;
|
||||
case GPIO_SWITCH_ALWAYS_OFF:
|
||||
initial_state = false;
|
||||
break;
|
||||
case GPIO_SWITCH_ALWAYS_ON:
|
||||
initial_state = true;
|
||||
break;
|
||||
}
|
||||
bool initial_state = Switch::get_initial_state_with_restore_mode();
|
||||
|
||||
// write state before setup
|
||||
if (initial_state) {
|
||||
@@ -49,28 +29,6 @@ void GPIOSwitch::setup() {
|
||||
void GPIOSwitch::dump_config() {
|
||||
LOG_SWITCH("", "GPIO Switch", this);
|
||||
LOG_PIN(" Pin: ", this->pin_);
|
||||
const LogString *restore_mode = LOG_STR("");
|
||||
switch (this->restore_mode_) {
|
||||
case GPIO_SWITCH_RESTORE_DEFAULT_OFF:
|
||||
restore_mode = LOG_STR("Restore (Defaults to OFF)");
|
||||
break;
|
||||
case GPIO_SWITCH_RESTORE_DEFAULT_ON:
|
||||
restore_mode = LOG_STR("Restore (Defaults to ON)");
|
||||
break;
|
||||
case GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_ON:
|
||||
restore_mode = LOG_STR("Restore inverted (Defaults to ON)");
|
||||
break;
|
||||
case GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_OFF:
|
||||
restore_mode = LOG_STR("Restore inverted (Defaults to OFF)");
|
||||
break;
|
||||
case GPIO_SWITCH_ALWAYS_OFF:
|
||||
restore_mode = LOG_STR("Always OFF");
|
||||
break;
|
||||
case GPIO_SWITCH_ALWAYS_ON:
|
||||
restore_mode = LOG_STR("Always ON");
|
||||
break;
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Restore Mode: %s", LOG_STR_ARG(restore_mode));
|
||||
if (!this->interlock_.empty()) {
|
||||
ESP_LOGCONFIG(TAG, " Interlocks:");
|
||||
for (auto *lock : this->interlock_) {
|
||||
@@ -111,7 +69,6 @@ void GPIOSwitch::write_state(bool state) {
|
||||
this->pin_->digital_write(state);
|
||||
this->publish_state(state);
|
||||
}
|
||||
void GPIOSwitch::set_restore_mode(GPIOSwitchRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
|
||||
void GPIOSwitch::set_interlock(const std::vector<Switch *> &interlock) { this->interlock_ = interlock; }
|
||||
|
||||
} // namespace gpio
|
||||
|
||||
@@ -9,21 +9,10 @@
|
||||
namespace esphome {
|
||||
namespace gpio {
|
||||
|
||||
enum GPIOSwitchRestoreMode {
|
||||
GPIO_SWITCH_RESTORE_DEFAULT_OFF,
|
||||
GPIO_SWITCH_RESTORE_DEFAULT_ON,
|
||||
GPIO_SWITCH_ALWAYS_OFF,
|
||||
GPIO_SWITCH_ALWAYS_ON,
|
||||
GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_OFF,
|
||||
GPIO_SWITCH_RESTORE_INVERTED_DEFAULT_ON,
|
||||
};
|
||||
|
||||
class GPIOSwitch : public switch_::Switch, public Component {
|
||||
public:
|
||||
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
||||
|
||||
void set_restore_mode(GPIOSwitchRestoreMode restore_mode);
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
float get_setup_priority() const override;
|
||||
@@ -37,7 +26,6 @@ class GPIOSwitch : public switch_::Switch, public Component {
|
||||
void write_state(bool state) override;
|
||||
|
||||
GPIOPin *pin_;
|
||||
GPIOSwitchRestoreMode restore_mode_{GPIO_SWITCH_RESTORE_DEFAULT_OFF};
|
||||
std::vector<Switch *> interlock_;
|
||||
uint32_t interlock_wait_time_{0};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user