This commit is contained in:
David Woodhouse 2024-09-07 21:12:56 +01:00
parent 1a34516956
commit b5b5afffc0
3 changed files with 11 additions and 3 deletions

View file

@ -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))

View file

@ -58,11 +58,14 @@ 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;
if (this->optimistic_)
this->publish_state(true);
next_timeout = this->wait_time_;
break;
@ -70,6 +73,7 @@ void HBridgeSwitch::timer_fn_() {
case RELAY_STATE_SWITCHING_OFF:
this->off_pin_->digital_write(false);
this->relay_state_ = RELAY_STATE_OFF;
if (this->optimistic_)
this->publish_state(false);
next_timeout = this->wait_time_;
break;

View file

@ -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