mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Add send_every to uart switch for recurring data (#1514)
This commit is contained in:
parent
520c4331e3
commit
08ecca86bc
4 changed files with 43 additions and 5 deletions
|
@ -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]))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<uint8_t> &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<uint8_t> data_;
|
||||
uint32_t send_every_;
|
||||
uint32_t last_transmission_;
|
||||
};
|
||||
|
||||
} // namespace uart
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue