Update the patched UART component to ESPHome 2026.11.0
ESPHome 2026.11's tuya component calls uart.new_uart_component(), which the 2026.10.2 copy didn't have, so the build failed. Copy the UART component from 2026.11.0, reapply the PA13/PA14 patch, regenerate the patch file and point the refs at v1.0.6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bcc6b3a42b
commit
1457e25d98
39 files changed
+907
-304
No files matched your search
@@ -1,8 +1,7 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import event, uart
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_EVENT_TYPES, CONF_ID
|
||||
from esphome.core import ID
|
||||
from esphome.const import CONF_EVENT_TYPES
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from .. import uart_ns
|
||||
@@ -12,9 +11,10 @@ CODEOWNERS = ["@eoasmxd"]
|
||||
DEPENDENCIES = ["uart"]
|
||||
|
||||
UARTEvent = uart_ns.class_("UARTEvent", event.Event, uart.UARTDevice, cg.Component)
|
||||
UARTEventMatcher = uart_ns.struct("UARTEventMatcher")
|
||||
|
||||
|
||||
def validate_event_types(value) -> list[tuple[str, str | list[int]]]:
|
||||
def validate_event_types(value) -> list[tuple[str, list[int]]]:
|
||||
if not isinstance(value, list):
|
||||
raise cv.Invalid("Event type must be a list of key-value mappings.")
|
||||
|
||||
@@ -44,7 +44,7 @@ def validate_event_types(value) -> list[tuple[str, str | list[int]]]:
|
||||
try:
|
||||
# Try to validate as string
|
||||
match_data_str = cv.string_strict(match_data)
|
||||
processed.append((event_name, match_data_str))
|
||||
processed.append((event_name, list(match_data_str.encode("utf-8"))))
|
||||
continue
|
||||
except cv.Invalid:
|
||||
pass # Not string either
|
||||
@@ -56,6 +56,10 @@ def validate_event_types(value) -> list[tuple[str, str | list[int]]]:
|
||||
|
||||
if not processed:
|
||||
raise cv.Invalid("event_types must contain at least one event mapping.")
|
||||
if len(processed) > 0xFFFF:
|
||||
raise cv.Invalid("event_types can have at most 65535 entries.")
|
||||
if any(len(data) > 0xFFFF for _, data in processed):
|
||||
raise cv.Invalid("Event match data can be at most 65535 bytes.")
|
||||
|
||||
return processed
|
||||
|
||||
@@ -77,14 +81,23 @@ async def to_code(config: ConfigType) -> None:
|
||||
var = await event.new_event(config, event_types=event_names)
|
||||
await cg.register_component(var, config)
|
||||
await uart.register_uart_device(var, config)
|
||||
for i, (event_name, match_data) in enumerate(config[CONF_EVENT_TYPES]):
|
||||
if isinstance(match_data, str):
|
||||
match_data = [ord(c) for c in match_data]
|
||||
|
||||
match_data_var_id = ID(
|
||||
f"match_data_{config[CONF_ID]}_{i}", is_declaration=True, type=cg.uint8
|
||||
matchers = []
|
||||
max_len = 0
|
||||
for event_name, match_data in config[CONF_EVENT_TYPES]:
|
||||
match_data = [int(b) for b in match_data]
|
||||
data = (
|
||||
cg.shared_progmem_array("uart_event_match", cg.uint8, match_data)
|
||||
if match_data
|
||||
else cg.nullptr
|
||||
)
|
||||
match_data_var = cg.static_const_array(
|
||||
match_data_var_id, cg.ArrayInitializer(*match_data)
|
||||
matchers.append(
|
||||
cg.StructInitializer(
|
||||
UARTEventMatcher,
|
||||
("event_name", event_name),
|
||||
("data", data),
|
||||
("data_len", len(match_data)),
|
||||
)
|
||||
)
|
||||
cg.add(var.add_event_matcher(event_name, match_data_var, len(match_data)))
|
||||
max_len = max(max_len, len(match_data))
|
||||
table = cg.shared_progmem_array("uart_event_matchers", UARTEventMatcher, matchers)
|
||||
cg.add(var.set_matchers(table, len(matchers), max_len))
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "uart_event.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace esphome::uart {
|
||||
|
||||
static const char *const TAG = "uart.event";
|
||||
ESPHOME_LOG_TAG(TAG, "uart.event");
|
||||
|
||||
void UARTEvent::setup() {}
|
||||
|
||||
@@ -12,26 +12,23 @@ void UARTEvent::dump_config() { LOG_EVENT("", "UART Event", this); }
|
||||
|
||||
void UARTEvent::loop() { this->read_data_(); }
|
||||
|
||||
void UARTEvent::add_event_matcher(const char *event_name, const uint8_t *match_data, size_t match_data_len) {
|
||||
this->matchers_.push_back({event_name, match_data, match_data_len});
|
||||
if (match_data_len > this->max_matcher_len_) {
|
||||
this->max_matcher_len_ = match_data_len;
|
||||
}
|
||||
}
|
||||
|
||||
void UARTEvent::read_data_() {
|
||||
while (this->available()) {
|
||||
uint8_t data;
|
||||
this->read_byte(&data);
|
||||
this->read_available([this](uint8_t data) {
|
||||
this->buffer_.push_back(data);
|
||||
|
||||
bool match_found = false;
|
||||
for (const auto &matcher : this->matchers_) {
|
||||
for (uint16_t i = 0; i < this->matcher_count_; i++) {
|
||||
const UARTEventMatcher &matcher = this->matchers_[i];
|
||||
if (this->buffer_.size() < matcher.data_len) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (std::equal(matcher.data, matcher.data + matcher.data_len, this->buffer_.end() - matcher.data_len)) {
|
||||
const uint8_t *tail = this->buffer_.data() + this->buffer_.size() - matcher.data_len;
|
||||
size_t pos = 0;
|
||||
// The pattern is in flash; ESP8266 only allows word loads there, so read it with progmem_read_byte
|
||||
while (pos < matcher.data_len && progmem_read_byte(matcher.data + pos) == tail[pos])
|
||||
pos++;
|
||||
if (pos == matcher.data_len) {
|
||||
this->trigger(matcher.event_name);
|
||||
this->buffer_.clear();
|
||||
match_found = true;
|
||||
@@ -42,7 +39,7 @@ void UARTEvent::read_data_() {
|
||||
if (!match_found && this->max_matcher_len_ > 0 && this->buffer_.size() > this->max_matcher_len_) {
|
||||
this->buffer_.erase(this->buffer_.begin());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace esphome::uart
|
||||
@@ -1,31 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/components/event/event.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace esphome::uart {
|
||||
|
||||
/// An event type and the byte pattern that fires it; `data` may point to PROGMEM.
|
||||
struct UARTEventMatcher {
|
||||
const char *event_name;
|
||||
const uint8_t *data;
|
||||
size_t data_len;
|
||||
};
|
||||
// Codegen keeps the matchers in a flash table read with plain loads, which ESP8266 only allows for whole words.
|
||||
static_assert(std::is_same_v<decltype(UARTEventMatcher::event_name), const char *>,
|
||||
"UARTEventMatcher fields must stay word sized");
|
||||
static_assert(std::is_same_v<decltype(UARTEventMatcher::data), const uint8_t *>,
|
||||
"UARTEventMatcher fields must stay word sized");
|
||||
static_assert(std::is_same_v<decltype(UARTEventMatcher::data_len), size_t>,
|
||||
"UARTEventMatcher fields must stay word sized");
|
||||
#ifdef USE_ESP8266
|
||||
static_assert(sizeof(UARTEventMatcher) == 3 * sizeof(uint32_t), "UARTEventMatcher is read from flash with word loads");
|
||||
#endif
|
||||
|
||||
class UARTEvent final : public event::Event, public UARTDevice, public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
void add_event_matcher(const char *event_name, const uint8_t *match_data, size_t match_data_len);
|
||||
/// `matchers` is a codegen PROGMEM table that must outlive the component.
|
||||
void set_matchers(const UARTEventMatcher *matchers, uint16_t count, uint16_t max_len) {
|
||||
this->matchers_ = matchers;
|
||||
this->matcher_count_ = count;
|
||||
this->max_matcher_len_ = max_len;
|
||||
}
|
||||
|
||||
protected:
|
||||
struct EventMatcher {
|
||||
const char *event_name;
|
||||
const uint8_t *data;
|
||||
size_t data_len;
|
||||
};
|
||||
|
||||
void read_data_();
|
||||
std::vector<EventMatcher> matchers_;
|
||||
const UARTEventMatcher *matchers_{nullptr};
|
||||
std::vector<uint8_t> buffer_;
|
||||
size_t max_matcher_len_ = 0;
|
||||
uint16_t matcher_count_{0};
|
||||
uint16_t max_matcher_len_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome::uart
|
||||
Reference in new issue
Block a user