mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
add on/off options for uart switch (#5539)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
b0ac729a8e
commit
c19dbdb02d
4 changed files with 60 additions and 15 deletions
|
@ -9,14 +9,24 @@ DEPENDENCIES = ["uart"]
|
||||||
|
|
||||||
UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Component)
|
UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Component)
|
||||||
|
|
||||||
|
CONF_TURN_OFF = "turn_off"
|
||||||
|
CONF_TURN_ON = "turn_on"
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
switch.switch_schema(UARTSwitch, block_inverted=True)
|
switch.switch_schema(UARTSwitch, block_inverted=True)
|
||||||
.extend(
|
.extend(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_DATA): validate_raw_data,
|
cv.Required(CONF_DATA): cv.Any(
|
||||||
|
validate_raw_data,
|
||||||
|
cv.Schema(
|
||||||
|
{
|
||||||
|
cv.Optional(CONF_TURN_OFF): validate_raw_data,
|
||||||
|
cv.Optional(CONF_TURN_ON): validate_raw_data,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
),
|
||||||
cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds,
|
cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds,
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
.extend(uart.UART_DEVICE_SCHEMA)
|
.extend(uart.UART_DEVICE_SCHEMA)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
|
@ -29,9 +39,20 @@ async def to_code(config):
|
||||||
await uart.register_uart_device(var, config)
|
await uart.register_uart_device(var, config)
|
||||||
|
|
||||||
data = config[CONF_DATA]
|
data = config[CONF_DATA]
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, dict):
|
||||||
data = [HexInt(x) for x in data]
|
if data_on := data.get(CONF_TURN_ON):
|
||||||
cg.add(var.set_data(data))
|
if isinstance(data_on, bytes):
|
||||||
|
data_on = [HexInt(x) for x in data_on]
|
||||||
|
cg.add(var.set_data_on(data_on))
|
||||||
|
if data_off := data.get(CONF_TURN_OFF):
|
||||||
|
if isinstance(data_off, bytes):
|
||||||
|
data_off = [HexInt(x) for x in data_off]
|
||||||
|
cg.add(var.set_data_off(data_off))
|
||||||
|
else:
|
||||||
|
data = config[CONF_DATA]
|
||||||
|
if isinstance(data, bytes):
|
||||||
|
data = [HexInt(x) for x in data]
|
||||||
|
cg.add(var.set_data_on(data))
|
||||||
|
cg.add(var.set_single_state(True))
|
||||||
if CONF_SEND_EVERY in config:
|
if CONF_SEND_EVERY in config:
|
||||||
cg.add(var.set_send_every(config[CONF_SEND_EVERY]))
|
cg.add(var.set_send_every(config[CONF_SEND_EVERY]))
|
||||||
|
|
|
@ -7,28 +7,41 @@ namespace uart {
|
||||||
static const char *const TAG = "uart.switch";
|
static const char *const TAG = "uart.switch";
|
||||||
|
|
||||||
void UARTSwitch::loop() {
|
void UARTSwitch::loop() {
|
||||||
if (this->state && this->send_every_) {
|
if (this->send_every_) {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (now - this->last_transmission_ > this->send_every_) {
|
if (now - this->last_transmission_ > this->send_every_) {
|
||||||
this->write_command_();
|
this->write_command_(this->state);
|
||||||
this->last_transmission_ = now;
|
this->last_transmission_ = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UARTSwitch::write_command_() {
|
void UARTSwitch::write_command_(bool state) {
|
||||||
ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str());
|
if (state && !this->data_on_.empty()) {
|
||||||
this->write_array(this->data_.data(), this->data_.size());
|
ESP_LOGD(TAG, "'%s': Sending on data...", this->get_name().c_str());
|
||||||
|
this->write_array(this->data_on_.data(), this->data_on_.size());
|
||||||
|
}
|
||||||
|
if (!state && !this->data_off_.empty()) {
|
||||||
|
ESP_LOGD(TAG, "'%s': Sending off data...", this->get_name().c_str());
|
||||||
|
this->write_array(this->data_off_.data(), this->data_off_.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UARTSwitch::write_state(bool state) {
|
void UARTSwitch::write_state(bool state) {
|
||||||
|
if (!this->single_state_) {
|
||||||
|
this->publish_state(state);
|
||||||
|
this->write_command_(state);
|
||||||
|
this->last_transmission_ = millis();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!state) {
|
if (!state) {
|
||||||
this->publish_state(false);
|
this->publish_state(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->publish_state(true);
|
this->publish_state(true);
|
||||||
this->write_command_();
|
this->write_command_(true);
|
||||||
|
|
||||||
if (this->send_every_ == 0) {
|
if (this->send_every_ == 0) {
|
||||||
this->publish_state(false);
|
this->publish_state(false);
|
||||||
|
@ -36,6 +49,7 @@ void UARTSwitch::write_state(bool state) {
|
||||||
this->last_transmission_ = millis();
|
this->last_transmission_ = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UARTSwitch::dump_config() {
|
void UARTSwitch::dump_config() {
|
||||||
LOG_SWITCH("", "UART Switch", this);
|
LOG_SWITCH("", "UART Switch", this);
|
||||||
if (this->send_every_) {
|
if (this->send_every_) {
|
||||||
|
|
|
@ -13,15 +13,19 @@ class UARTSwitch : public switch_::Switch, public UARTDevice, public Component {
|
||||||
public:
|
public:
|
||||||
void loop() override;
|
void loop() override;
|
||||||
|
|
||||||
void set_data(const std::vector<uint8_t> &data) { data_ = data; }
|
void set_data_on(const std::vector<uint8_t> &data) { this->data_on_ = data; }
|
||||||
|
void set_data_off(const std::vector<uint8_t> &data) { this->data_off_ = data; }
|
||||||
void set_send_every(uint32_t send_every) { this->send_every_ = send_every; }
|
void set_send_every(uint32_t send_every) { this->send_every_ = send_every; }
|
||||||
|
void set_single_state(bool single) { this->single_state_ = single; }
|
||||||
|
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void write_command_();
|
void write_command_(bool state);
|
||||||
void write_state(bool state) override;
|
void write_state(bool state) override;
|
||||||
std::vector<uint8_t> data_;
|
std::vector<uint8_t> data_on_;
|
||||||
|
std::vector<uint8_t> data_off_;
|
||||||
|
bool single_state_{false};
|
||||||
uint32_t send_every_;
|
uint32_t send_every_;
|
||||||
uint32_t last_transmission_;
|
uint32_t last_transmission_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -2588,6 +2588,12 @@ switch:
|
||||||
name: UART Recurring Output
|
name: UART Recurring Output
|
||||||
data: [0xDE, 0xAD, 0xBE, 0xEF]
|
data: [0xDE, 0xAD, 0xBE, 0xEF]
|
||||||
send_every: 1s
|
send_every: 1s
|
||||||
|
- platform: uart
|
||||||
|
uart_id: uart_0
|
||||||
|
name: "UART On/Off"
|
||||||
|
data:
|
||||||
|
turn_on: "TurnOn\r\n"
|
||||||
|
turn_off: "TurnOff\r\n"
|
||||||
- platform: template
|
- platform: template
|
||||||
assumed_state: true
|
assumed_state: true
|
||||||
name: Stepper Switch
|
name: Stepper Switch
|
||||||
|
|
Loading…
Reference in a new issue