diff --git a/esphome/components/hbridge/switch/__init__.py b/esphome/components/hbridge/switch/__init__.py index 6b47de42de..e26bd6b1d8 100644 --- a/esphome/components/hbridge/switch/__init__.py +++ b/esphome/components/hbridge/switch/__init__.py @@ -2,7 +2,7 @@ from esphome import pins import esphome.codegen as cg from esphome.components import switch import esphome.config_validation as cv -from esphome.const import CONF_PULSE_LENGTH, CONF_WAIT_TIME +from esphome.const import CONF_OPTIMISTIC, CONF_PULSE_LENGTH, CONF_WAIT_TIME from .. import hbridge_ns @@ -23,6 +23,7 @@ CONFIG_SCHEMA = ( CONF_PULSE_LENGTH, default="100ms" ): cv.positive_time_period_milliseconds, cv.Optional(CONF_WAIT_TIME): cv.positive_time_period_milliseconds, + cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean, } ) .extend(cv.COMPONENT_SCHEMA) @@ -38,5 +39,6 @@ async def to_code(config): off_pin = await cg.gpio_pin_expression(config[CONF_OFF_PIN]) cg.add(var.set_off_pin(off_pin)) cg.add(var.set_pulse_length(config[CONF_PULSE_LENGTH])) + cg.add(var.set_optimistic(config[CONF_OPTIMISTIC])) if wait_time := config.get(CONF_WAIT_TIME): cg.add(var.set_wait_time(wait_time)) diff --git a/esphome/components/hbridge/switch/hbridge_switch.cpp b/esphome/components/hbridge/switch/hbridge_switch.cpp index b05faaa264..7717c03e5a 100644 --- a/esphome/components/hbridge/switch/hbridge_switch.cpp +++ b/esphome/components/hbridge/switch/hbridge_switch.cpp @@ -58,19 +58,23 @@ void HBridgeSwitch::timer_fn_() { this->relay_state_ = RELAY_STATE_SWITCHING_OFF; } next_timeout = this->pulse_length_; + if (!this->optimistic_) + this->publish_state(this->desired_state_); break; case RELAY_STATE_SWITCHING_ON: this->on_pin_->digital_write(false); this->relay_state_ = RELAY_STATE_ON; - this->publish_state(true); + if (this->optimistic_) + this->publish_state(true); next_timeout = this->wait_time_; break; case RELAY_STATE_SWITCHING_OFF: this->off_pin_->digital_write(false); this->relay_state_ = RELAY_STATE_OFF; - this->publish_state(false); + if (this->optimistic_) + this->publish_state(false); next_timeout = this->wait_time_; break; } diff --git a/esphome/components/hbridge/switch/hbridge_switch.h b/esphome/components/hbridge/switch/hbridge_switch.h index 6c2abe1494..5fb0ee835d 100644 --- a/esphome/components/hbridge/switch/hbridge_switch.h +++ b/esphome/components/hbridge/switch/hbridge_switch.h @@ -23,6 +23,7 @@ class HBridgeSwitch : public switch_::Switch, public Component { void set_off_pin(GPIOPin *pin) { this->off_pin_ = pin; } void set_pulse_length(uint32_t pulse_length) { this->pulse_length_ = pulse_length; } void set_wait_time(uint32_t wait_time) { this->wait_time_ = wait_time; } + void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } // ========== INTERNAL METHODS ========== // (In most use cases you won't need these) @@ -42,6 +43,7 @@ class HBridgeSwitch : public switch_::Switch, public Component { GPIOPin *off_pin_; uint32_t pulse_length_; uint32_t wait_time_{0}; + bool optimistic_{false}; }; } // namespace hbridge