diff --git a/esphome/components/uart/switch/__init__.py b/esphome/components/uart/switch/__init__.py index 6cc11d8bbe..0c9ebe56f7 100644 --- a/esphome/components/uart/switch/__init__.py +++ b/esphome/components/uart/switch/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch, uart -from esphome.const import CONF_DATA, CONF_ID, CONF_INVERTED +from esphome.const import CONF_DATA, CONF_ID, CONF_INVERTED, CONF_SEND_EVERY from esphome.core import HexInt from .. import uart_ns, validate_raw_data @@ -14,6 +14,7 @@ CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(UARTSwitch), cv.Required(CONF_DATA): validate_raw_data, cv.Optional(CONF_INVERTED): cv.invalid("UART switches do not support inverted mode!"), + cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds, }).extend(uart.UART_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA) @@ -27,3 +28,6 @@ def to_code(config): if isinstance(data, bytes): data = [HexInt(x) for x in data] cg.add(var.set_data(data)) + + if CONF_SEND_EVERY in config: + cg.add(var.set_send_every(config[CONF_SEND_EVERY])) diff --git a/esphome/components/uart/switch/uart_switch.cpp b/esphome/components/uart/switch/uart_switch.cpp index 9974ee1179..a1ebb6dbb9 100644 --- a/esphome/components/uart/switch/uart_switch.cpp +++ b/esphome/components/uart/switch/uart_switch.cpp @@ -6,6 +6,21 @@ namespace uart { static const char *TAG = "uart.switch"; +void UARTSwitch::loop() { + if (this->state && this->send_every_) { + const uint32_t now = millis(); + if (now - this->last_transmission_ > this->send_every_) { + this->write_command_(); + this->last_transmission_ = now; + } + } +} + +void UARTSwitch::write_command_() { + ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str()); + this->write_array(this->data_.data(), this->data_.size()); +} + void UARTSwitch::write_state(bool state) { if (!state) { this->publish_state(false); @@ -13,11 +28,20 @@ void UARTSwitch::write_state(bool state) { } this->publish_state(true); - ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str()); - this->write_array(this->data_.data(), this->data_.size()); - this->publish_state(false); + this->write_command_(); + + if (this->send_every_ == 0) { + this->publish_state(false); + } else { + this->last_transmission_ = millis(); + } +} +void UARTSwitch::dump_config() { + LOG_SWITCH("", "UART Switch", this); + if (this->send_every_) { + ESP_LOGCONFIG(TAG, " Send Every: %u", this->send_every_); + } } -void UARTSwitch::dump_config() { LOG_SWITCH("", "UART Switch", this); } } // namespace uart } // namespace esphome diff --git a/esphome/components/uart/switch/uart_switch.h b/esphome/components/uart/switch/uart_switch.h index c8a1b0d8c5..4c82d5680a 100644 --- a/esphome/components/uart/switch/uart_switch.h +++ b/esphome/components/uart/switch/uart_switch.h @@ -9,13 +9,19 @@ namespace uart { class UARTSwitch : public switch_::Switch, public UARTDevice, public Component { public: + void loop() override; + void set_data(const std::vector &data) { data_ = data; } + void set_send_every(uint32_t send_every) { this->send_every_ = send_every; } void dump_config() override; protected: + void write_command_(); void write_state(bool state) override; std::vector data_; + uint32_t send_every_; + uint32_t last_transmission_; }; } // namespace uart diff --git a/tests/test1.yaml b/tests/test1.yaml index 5da2ee03fe..e76d1d24c0 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1617,6 +1617,10 @@ switch: - platform: uart name: 'UART Bytes Output' data: [0xDE, 0xAD, 0xBE, 0xEF] + - platform: uart + name: 'UART Recurring Output' + data: [0xDE, 0xAD, 0xBE, 0xEF] + send_every: 1s - platform: template assumed_state: yes name: Stepper Switch