mirror of
https://github.com/esphome/esphome.git
synced 2026-01-31 00:42:07 -07:00
Compare commits
7 Commits
lazy_time_
...
peername_n
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c08726036e | ||
|
|
9dcb469460 | ||
|
|
d602a2e5e4 | ||
|
|
dcab12adae | ||
|
|
fb714636e3 | ||
|
|
05a431ea54 | ||
|
|
1a34b4e7d7 |
@@ -133,8 +133,8 @@ void APIConnection::start() {
|
||||
return;
|
||||
}
|
||||
// Initialize client name with peername (IP address) until Hello message provides actual name
|
||||
const char *peername = this->helper_->get_client_peername();
|
||||
this->helper_->set_client_name(peername, strlen(peername));
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
this->helper_->set_client_name(this->helper_->get_peername_to(peername), strlen(peername));
|
||||
}
|
||||
|
||||
APIConnection::~APIConnection() {
|
||||
@@ -179,8 +179,8 @@ void APIConnection::begin_iterator_(ActiveIterator type) {
|
||||
|
||||
void APIConnection::loop() {
|
||||
if (this->flags_.next_close) {
|
||||
// requested a disconnect
|
||||
this->helper_->close();
|
||||
// requested a disconnect - don't close socket here, let APIServer::loop() do it
|
||||
// so getpeername() still works for the disconnect trigger
|
||||
this->flags_.remove = true;
|
||||
return;
|
||||
}
|
||||
@@ -293,7 +293,8 @@ bool APIConnection::send_disconnect_response(const DisconnectRequest &msg) {
|
||||
return this->send_message(resp, DisconnectResponse::MESSAGE_TYPE);
|
||||
}
|
||||
void APIConnection::on_disconnect_response(const DisconnectResponse &value) {
|
||||
this->helper_->close();
|
||||
// Don't close socket here, let APIServer::loop() do it
|
||||
// so getpeername() still works for the disconnect trigger
|
||||
this->flags_.remove = true;
|
||||
}
|
||||
|
||||
@@ -1524,8 +1525,11 @@ void APIConnection::complete_authentication_() {
|
||||
this->flags_.connection_state = static_cast<uint8_t>(ConnectionState::AUTHENTICATED);
|
||||
this->log_client_(ESPHOME_LOG_LEVEL_DEBUG, LOG_STR("connected"));
|
||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
this->parent_->get_client_connected_trigger()->trigger(std::string(this->helper_->get_client_name()),
|
||||
std::string(this->helper_->get_client_peername()));
|
||||
{
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
this->parent_->get_client_connected_trigger()->trigger(std::string(this->helper_->get_client_name()),
|
||||
std::string(this->helper_->get_peername_to(peername)));
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_HOMEASSISTANT_TIME
|
||||
if (homeassistant::global_homeassistant_time != nullptr) {
|
||||
@@ -1544,8 +1548,9 @@ bool APIConnection::send_hello_response(const HelloRequest &msg) {
|
||||
this->helper_->set_client_name(msg.client_info.c_str(), msg.client_info.size());
|
||||
this->client_api_version_major_ = msg.api_version_major;
|
||||
this->client_api_version_minor_ = msg.api_version_minor;
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu32 ".%" PRIu32, this->helper_->get_client_name(),
|
||||
this->helper_->get_client_peername(), this->client_api_version_major_, this->client_api_version_minor_);
|
||||
this->helper_->get_peername_to(peername), this->client_api_version_major_, this->client_api_version_minor_);
|
||||
|
||||
HelloResponse resp;
|
||||
resp.api_version_major = 1;
|
||||
@@ -1862,7 +1867,8 @@ void APIConnection::on_no_setup_connection() {
|
||||
this->log_client_(ESPHOME_LOG_LEVEL_DEBUG, LOG_STR("no connection setup"));
|
||||
}
|
||||
void APIConnection::on_fatal_error() {
|
||||
this->helper_->close();
|
||||
// Don't close socket here - keep it open so getpeername() works for logging
|
||||
// Socket will be closed when client is removed from the list in APIServer::loop()
|
||||
this->flags_.remove = true;
|
||||
}
|
||||
|
||||
@@ -2218,12 +2224,14 @@ void APIConnection::process_state_subscriptions_() {
|
||||
#endif // USE_API_HOMEASSISTANT_STATES
|
||||
|
||||
void APIConnection::log_client_(int level, const LogString *message) {
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
esp_log_printf_(level, TAG, __LINE__, ESPHOME_LOG_FORMAT("%s (%s): %s"), this->helper_->get_client_name(),
|
||||
this->helper_->get_client_peername(), LOG_STR_ARG(message));
|
||||
this->helper_->get_peername_to(peername), LOG_STR_ARG(message));
|
||||
}
|
||||
|
||||
void APIConnection::log_warning_(const LogString *message, APIError err) {
|
||||
ESP_LOGW(TAG, "%s (%s): %s %s errno=%d", this->helper_->get_client_name(), this->helper_->get_client_peername(),
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
ESP_LOGW(TAG, "%s (%s): %s %s errno=%d", this->helper_->get_client_name(), this->helper_->get_peername_to(peername),
|
||||
LOG_STR_ARG(message), LOG_STR_ARG(api_error_to_logstr(err)), errno);
|
||||
}
|
||||
|
||||
|
||||
@@ -281,8 +281,10 @@ class APIConnection final : public APIServerConnection {
|
||||
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
|
||||
|
||||
const char *get_name() const { return this->helper_->get_client_name(); }
|
||||
/// Get peer name (IP address) - cached at connection init time
|
||||
const char *get_peername() const { return this->helper_->get_client_peername(); }
|
||||
/// Get peer name (IP address) into caller-provided buffer, returns buf for convenience
|
||||
const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
|
||||
return this->helper_->get_peername_to(buf);
|
||||
}
|
||||
|
||||
protected:
|
||||
// Helper function to handle authentication completion
|
||||
|
||||
@@ -16,7 +16,12 @@ static const char *const TAG = "api.frame_helper";
|
||||
static constexpr size_t API_MAX_LOG_BYTES = 168;
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
|
||||
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, this->client_peername_, ##__VA_ARGS__)
|
||||
#define HELPER_LOG(msg, ...) \
|
||||
do { \
|
||||
char peername_buf[socket::SOCKADDR_STR_LEN]; \
|
||||
this->get_peername_to(peername_buf); \
|
||||
ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, peername_buf, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define HELPER_LOG(msg, ...) ((void) 0)
|
||||
#endif
|
||||
@@ -240,13 +245,20 @@ APIError APIFrameHelper::try_send_tx_buf_() {
|
||||
return APIError::OK; // All buffers sent successfully
|
||||
}
|
||||
|
||||
const char *APIFrameHelper::get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
|
||||
if (this->socket_) {
|
||||
this->socket_->getpeername_to(buf);
|
||||
} else {
|
||||
buf[0] = '\0';
|
||||
}
|
||||
return buf.data();
|
||||
}
|
||||
|
||||
APIError APIFrameHelper::init_common_() {
|
||||
if (state_ != State::INITIALIZE || this->socket_ == nullptr) {
|
||||
HELPER_LOG("Bad state for init %d", (int) state_);
|
||||
return APIError::BAD_STATE;
|
||||
}
|
||||
// Cache peername now while socket is valid - needed for error logging after socket failure
|
||||
this->socket_->getpeername_to(this->client_peername_);
|
||||
int err = this->socket_->setblocking(false);
|
||||
if (err != 0) {
|
||||
state_ = State::FAILED;
|
||||
|
||||
@@ -90,8 +90,9 @@ class APIFrameHelper {
|
||||
|
||||
// Get client name (null-terminated)
|
||||
const char *get_client_name() const { return this->client_name_; }
|
||||
// Get client peername/IP (null-terminated, cached at init time for availability after socket failure)
|
||||
const char *get_client_peername() const { return this->client_peername_; }
|
||||
// Get client peername/IP into caller-provided buffer (fetches on-demand from socket)
|
||||
// Returns pointer to buf for convenience in printf-style calls
|
||||
const char *get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const;
|
||||
// Set client name from buffer with length (truncates if needed)
|
||||
void set_client_name(const char *name, size_t len) {
|
||||
size_t copy_len = std::min(len, sizeof(this->client_name_) - 1);
|
||||
@@ -105,6 +106,8 @@ class APIFrameHelper {
|
||||
bool can_write_without_blocking() { return this->state_ == State::DATA && this->tx_buf_count_ == 0; }
|
||||
int getpeername(struct sockaddr *addr, socklen_t *addrlen) { return socket_->getpeername(addr, addrlen); }
|
||||
APIError close() {
|
||||
if (state_ == State::CLOSED)
|
||||
return APIError::OK; // Already closed
|
||||
state_ = State::CLOSED;
|
||||
int err = this->socket_->close();
|
||||
if (err == -1)
|
||||
@@ -231,8 +234,6 @@ class APIFrameHelper {
|
||||
|
||||
// Client name buffer - stores name from Hello message or initial peername
|
||||
char client_name_[CLIENT_INFO_NAME_MAX_LEN]{};
|
||||
// Cached peername/IP address - captured at init time for availability after socket failure
|
||||
char client_peername_[socket::SOCKADDR_STR_LEN]{};
|
||||
|
||||
// Group smaller types together
|
||||
uint16_t rx_buf_len_ = 0;
|
||||
|
||||
@@ -29,7 +29,12 @@ static constexpr size_t PROLOGUE_INIT_LEN = 12; // strlen("NoiseAPIInit")
|
||||
static constexpr size_t API_MAX_LOG_BYTES = 168;
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
|
||||
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, this->client_peername_, ##__VA_ARGS__)
|
||||
#define HELPER_LOG(msg, ...) \
|
||||
do { \
|
||||
char peername_buf[socket::SOCKADDR_STR_LEN]; \
|
||||
this->get_peername_to(peername_buf); \
|
||||
ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, peername_buf, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define HELPER_LOG(msg, ...) ((void) 0)
|
||||
#endif
|
||||
|
||||
@@ -21,7 +21,12 @@ static const char *const TAG = "api.plaintext";
|
||||
static constexpr size_t API_MAX_LOG_BYTES = 168;
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
|
||||
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, this->client_peername_, ##__VA_ARGS__)
|
||||
#define HELPER_LOG(msg, ...) \
|
||||
do { \
|
||||
char peername_buf[socket::SOCKADDR_STR_LEN]; \
|
||||
this->get_peername_to(peername_buf); \
|
||||
ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, peername_buf, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define HELPER_LOG(msg, ...) ((void) 0)
|
||||
#endif
|
||||
|
||||
@@ -192,11 +192,15 @@ void APIServer::loop() {
|
||||
ESP_LOGV(TAG, "Remove connection %s", client->get_name());
|
||||
|
||||
#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
|
||||
// Save client info before removal for the trigger
|
||||
// Save client info before closing socket and removal for the trigger
|
||||
char peername_buf[socket::SOCKADDR_STR_LEN];
|
||||
std::string client_name(client->get_name());
|
||||
std::string client_peername(client->get_peername());
|
||||
std::string client_peername(client->get_peername_to(peername_buf));
|
||||
#endif
|
||||
|
||||
// Close socket now (was deferred from on_fatal_error to allow getpeername)
|
||||
client->helper_->close();
|
||||
|
||||
// Swap with the last element and pop (avoids expensive vector shifts)
|
||||
if (client_index < this->clients_.size() - 1) {
|
||||
std::swap(this->clients_[client_index], this->clients_.back());
|
||||
|
||||
@@ -62,7 +62,7 @@ class RealTimeClock : public PollingComponent {
|
||||
void apply_timezone_();
|
||||
#endif
|
||||
|
||||
LazyCallbackManager<void()> time_sync_callback_;
|
||||
CallbackManager<void()> time_sync_callback_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
|
||||
|
||||
@@ -12,8 +12,8 @@ from esphome.components.packet_transport import (
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DATA, CONF_ID, CONF_PORT, CONF_TRIGGER_ID
|
||||
from esphome.core import ID, Lambda
|
||||
from esphome.cpp_generator import ExpressionStatement, MockObj
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import literal
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DEPENDENCIES = ["network"]
|
||||
@@ -24,6 +24,8 @@ udp_ns = cg.esphome_ns.namespace("udp")
|
||||
UDPComponent = udp_ns.class_("UDPComponent", cg.Component)
|
||||
UDPWriteAction = udp_ns.class_("UDPWriteAction", automation.Action)
|
||||
trigger_args = cg.std_vector.template(cg.uint8)
|
||||
trigger_argname = "data"
|
||||
trigger_argtype = [(trigger_args, trigger_argname)]
|
||||
|
||||
CONF_ADDRESSES = "addresses"
|
||||
CONF_LISTEN_ADDRESS = "listen_address"
|
||||
@@ -111,13 +113,14 @@ async def to_code(config):
|
||||
cg.add(var.set_addresses([str(addr) for addr in config[CONF_ADDRESSES]]))
|
||||
if on_receive := config.get(CONF_ON_RECEIVE):
|
||||
on_receive = on_receive[0]
|
||||
trigger = cg.new_Pvariable(on_receive[CONF_TRIGGER_ID])
|
||||
trigger_id = cg.new_Pvariable(on_receive[CONF_TRIGGER_ID])
|
||||
trigger = await automation.build_automation(
|
||||
trigger, [(trigger_args, "data")], on_receive
|
||||
trigger_id, trigger_argtype, on_receive
|
||||
)
|
||||
trigger = Lambda(str(ExpressionStatement(trigger.trigger(MockObj("data")))))
|
||||
trigger = await cg.process_lambda(trigger, [(trigger_args, "data")])
|
||||
cg.add(var.add_listener(trigger))
|
||||
trigger_lambda = await cg.process_lambda(
|
||||
trigger.trigger(literal(trigger_argname)), trigger_argtype
|
||||
)
|
||||
cg.add(var.add_listener(trigger_lambda))
|
||||
cg.add(var.set_should_listen())
|
||||
|
||||
|
||||
|
||||
@@ -430,12 +430,14 @@ void VoiceAssistant::client_subscription(api::APIConnection *client, bool subscr
|
||||
}
|
||||
|
||||
if (this->api_client_ != nullptr) {
|
||||
char current_peername[socket::SOCKADDR_STR_LEN];
|
||||
char new_peername[socket::SOCKADDR_STR_LEN];
|
||||
ESP_LOGE(TAG,
|
||||
"Multiple API Clients attempting to connect to Voice Assistant\n"
|
||||
"Current client: %s (%s)\n"
|
||||
"New client: %s (%s)",
|
||||
this->api_client_->get_name(), this->api_client_->get_peername(), client->get_name(),
|
||||
client->get_peername());
|
||||
this->api_client_->get_name(), this->api_client_->get_peername_to(current_peername), client->get_name(),
|
||||
client->get_peername_to(new_peername));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -278,9 +278,13 @@ LAMBDA_PROG = re.compile(r"\bid\(\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\)(\.?)")
|
||||
|
||||
class Lambda:
|
||||
def __init__(self, value):
|
||||
from esphome.cpp_generator import Expression, statement
|
||||
|
||||
# pylint: disable=protected-access
|
||||
if isinstance(value, Lambda):
|
||||
self._value = value._value
|
||||
elif isinstance(value, Expression):
|
||||
self._value = str(statement(value))
|
||||
else:
|
||||
self._value = value
|
||||
self._parts = None
|
||||
|
||||
@@ -462,6 +462,16 @@ def statement(expression: Expression | Statement) -> Statement:
|
||||
return ExpressionStatement(expression)
|
||||
|
||||
|
||||
def literal(name: str) -> "MockObj":
|
||||
"""Create a literal name that will appear in the generated code
|
||||
not surrounded by quotes.
|
||||
|
||||
:param name: The name of the literal.
|
||||
:return: The literal as a MockObj.
|
||||
"""
|
||||
return MockObj(name, "")
|
||||
|
||||
|
||||
def variable(
|
||||
id_: ID, rhs: SafeExpType, type_: "MockObj" = None, register=True
|
||||
) -> "MockObj":
|
||||
@@ -665,7 +675,7 @@ async def get_variable_with_full_id(id_: ID) -> tuple[ID, "MockObj"]:
|
||||
|
||||
|
||||
async def process_lambda(
|
||||
value: Lambda,
|
||||
value: Lambda | Expression,
|
||||
parameters: TemplateArgsType,
|
||||
capture: str = "",
|
||||
return_type: SafeExpType = None,
|
||||
@@ -689,6 +699,14 @@ async def process_lambda(
|
||||
|
||||
if value is None:
|
||||
return None
|
||||
# Inadvertently passing a malformed parameters value will lead to the build process mysteriously hanging at the
|
||||
# "Generating C++ source..." stage, so check here to save the developer's hair.
|
||||
assert isinstance(parameters, list) and all(
|
||||
isinstance(p, tuple) and len(p) == 2 for p in parameters
|
||||
)
|
||||
if isinstance(value, Expression):
|
||||
value = Lambda(value)
|
||||
|
||||
parts = value.parts[:]
|
||||
for i, id in enumerate(value.requires_ids):
|
||||
full_id, var = await get_variable_with_full_id(id)
|
||||
|
||||
@@ -347,3 +347,280 @@ class TestMockObj:
|
||||
assert isinstance(actual, cg.MockObj)
|
||||
assert actual.base == "foo.eek"
|
||||
assert actual.op == "."
|
||||
|
||||
|
||||
class TestStatementFunction:
|
||||
"""Tests for the statement() function."""
|
||||
|
||||
def test_statement__expression_converted_to_statement(self):
|
||||
"""Test that expressions are converted to ExpressionStatement."""
|
||||
expr = cg.RawExpression("foo()")
|
||||
result = cg.statement(expr)
|
||||
|
||||
assert isinstance(result, cg.ExpressionStatement)
|
||||
assert str(result) == "foo();"
|
||||
|
||||
def test_statement__statement_unchanged(self):
|
||||
"""Test that statements are returned unchanged."""
|
||||
stmt = cg.RawStatement("foo()")
|
||||
result = cg.statement(stmt)
|
||||
|
||||
assert result is stmt
|
||||
assert str(result) == "foo()"
|
||||
|
||||
def test_statement__expression_statement_unchanged(self):
|
||||
"""Test that ExpressionStatement is returned unchanged."""
|
||||
stmt = cg.ExpressionStatement(42)
|
||||
result = cg.statement(stmt)
|
||||
|
||||
assert result is stmt
|
||||
assert str(result) == "42;"
|
||||
|
||||
def test_statement__line_comment_unchanged(self):
|
||||
"""Test that LineComment is returned unchanged."""
|
||||
stmt = cg.LineComment("This is a comment")
|
||||
result = cg.statement(stmt)
|
||||
|
||||
assert result is stmt
|
||||
assert str(result) == "// This is a comment"
|
||||
|
||||
|
||||
class TestLiteralFunction:
|
||||
"""Tests for the literal() function."""
|
||||
|
||||
def test_literal__creates_mockobj(self):
|
||||
"""Test that literal() creates a MockObj."""
|
||||
result = cg.literal("MY_CONSTANT")
|
||||
|
||||
assert isinstance(result, cg.MockObj)
|
||||
assert result.base == "MY_CONSTANT"
|
||||
assert result.op == ""
|
||||
|
||||
def test_literal__string_representation(self):
|
||||
"""Test that literal names appear unquoted in generated code."""
|
||||
result = cg.literal("nullptr")
|
||||
|
||||
assert str(result) == "nullptr"
|
||||
|
||||
def test_literal__can_be_used_in_expressions(self):
|
||||
"""Test that literals can be used as part of larger expressions."""
|
||||
null_lit = cg.literal("nullptr")
|
||||
expr = cg.CallExpression(cg.RawExpression("my_func"), null_lit)
|
||||
|
||||
assert str(expr) == "my_func(nullptr)"
|
||||
|
||||
def test_literal__common_cpp_literals(self):
|
||||
"""Test common C++ literal values."""
|
||||
test_cases = [
|
||||
("nullptr", "nullptr"),
|
||||
("true", "true"),
|
||||
("false", "false"),
|
||||
("NULL", "NULL"),
|
||||
("NAN", "NAN"),
|
||||
]
|
||||
|
||||
for name, expected in test_cases:
|
||||
result = cg.literal(name)
|
||||
assert str(result) == expected
|
||||
|
||||
|
||||
class TestLambdaConstructor:
|
||||
"""Tests for the Lambda class constructor in core/__init__.py."""
|
||||
|
||||
def test_lambda__from_string(self):
|
||||
"""Test Lambda constructor with string argument."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return x + 1;")
|
||||
|
||||
assert lambda_obj.value == "return x + 1;"
|
||||
assert str(lambda_obj) == "return x + 1;"
|
||||
|
||||
def test_lambda__from_expression(self):
|
||||
"""Test Lambda constructor with Expression argument."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
expr = cg.RawExpression("x + 1")
|
||||
lambda_obj = Lambda(expr)
|
||||
|
||||
# Expression should be converted to statement (with semicolon)
|
||||
assert lambda_obj.value == "x + 1;"
|
||||
|
||||
def test_lambda__from_lambda(self):
|
||||
"""Test Lambda constructor with another Lambda argument."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
original = Lambda("return x + 1;")
|
||||
copy = Lambda(original)
|
||||
|
||||
assert copy.value == original.value
|
||||
assert copy.value == "return x + 1;"
|
||||
|
||||
def test_lambda__parts_parsing(self):
|
||||
"""Test that Lambda correctly parses parts with id() references."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return id(my_sensor).state;")
|
||||
parts = lambda_obj.parts
|
||||
|
||||
# Parts should be split by LAMBDA_PROG regex: text, id, op, text
|
||||
assert len(parts) == 4
|
||||
assert parts[0] == "return "
|
||||
assert parts[1] == "my_sensor"
|
||||
assert parts[2] == "."
|
||||
assert parts[3] == "state;"
|
||||
|
||||
def test_lambda__requires_ids(self):
|
||||
"""Test that Lambda correctly extracts required IDs."""
|
||||
from esphome.core import ID, Lambda
|
||||
|
||||
lambda_obj = Lambda("return id(sensor1).state + id(sensor2).value;")
|
||||
ids = lambda_obj.requires_ids
|
||||
|
||||
assert len(ids) == 2
|
||||
assert all(isinstance(id_obj, ID) for id_obj in ids)
|
||||
assert ids[0].id == "sensor1"
|
||||
assert ids[1].id == "sensor2"
|
||||
|
||||
def test_lambda__no_ids(self):
|
||||
"""Test Lambda with no id() references."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return 42;")
|
||||
ids = lambda_obj.requires_ids
|
||||
|
||||
assert len(ids) == 0
|
||||
|
||||
def test_lambda__comment_removal(self):
|
||||
"""Test that comments are removed when parsing parts."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return id(sensor).state; // Get sensor state")
|
||||
parts = lambda_obj.parts
|
||||
|
||||
# Comment should be replaced with space, not affect parsing
|
||||
assert "my_sensor" not in str(parts)
|
||||
|
||||
def test_lambda__multiline_string(self):
|
||||
"""Test Lambda with multiline string."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
code = """if (id(sensor).state > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;"""
|
||||
lambda_obj = Lambda(code)
|
||||
|
||||
assert lambda_obj.value == code
|
||||
assert "sensor" in [id_obj.id for id_obj in lambda_obj.requires_ids]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestProcessLambda:
|
||||
"""Tests for the process_lambda() async function."""
|
||||
|
||||
async def test_process_lambda__none_value(self):
|
||||
"""Test that None returns None."""
|
||||
result = await cg.process_lambda(None, [])
|
||||
|
||||
assert result is None
|
||||
|
||||
async def test_process_lambda__with_expression(self):
|
||||
"""Test process_lambda with Expression argument."""
|
||||
|
||||
expr = cg.RawExpression("return x + 1")
|
||||
result = await cg.process_lambda(expr, [(int, "x")])
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
assert "x + 1" in str(result)
|
||||
|
||||
async def test_process_lambda__simple_lambda_no_ids(self):
|
||||
"""Test process_lambda with simple Lambda without id() references."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return x + 1;")
|
||||
result = await cg.process_lambda(lambda_obj, [(int, "x")])
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
# Should have parameter
|
||||
lambda_str = str(result)
|
||||
assert "int32_t x" in lambda_str
|
||||
assert "return x + 1;" in lambda_str
|
||||
|
||||
async def test_process_lambda__with_return_type(self):
|
||||
"""Test process_lambda with return type specified."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return x > 0;")
|
||||
result = await cg.process_lambda(lambda_obj, [(int, "x")], return_type=bool)
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
lambda_str = str(result)
|
||||
assert "-> bool" in lambda_str
|
||||
|
||||
async def test_process_lambda__with_capture(self):
|
||||
"""Test process_lambda with capture specified."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return captured + x;")
|
||||
result = await cg.process_lambda(lambda_obj, [(int, "x")], capture="captured")
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
lambda_str = str(result)
|
||||
assert "[captured]" in lambda_str
|
||||
|
||||
async def test_process_lambda__empty_capture(self):
|
||||
"""Test process_lambda with empty capture (stateless lambda)."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return x + 1;")
|
||||
result = await cg.process_lambda(lambda_obj, [(int, "x")], capture="")
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
lambda_str = str(result)
|
||||
assert "[]" in lambda_str
|
||||
|
||||
async def test_process_lambda__no_parameters(self):
|
||||
"""Test process_lambda with no parameters."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return 42;")
|
||||
result = await cg.process_lambda(lambda_obj, [])
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
lambda_str = str(result)
|
||||
# Should have empty parameter list
|
||||
assert "()" in lambda_str
|
||||
|
||||
async def test_process_lambda__multiple_parameters(self):
|
||||
"""Test process_lambda with multiple parameters."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return x + y + z;")
|
||||
result = await cg.process_lambda(
|
||||
lambda_obj, [(int, "x"), (float, "y"), (bool, "z")]
|
||||
)
|
||||
|
||||
assert isinstance(result, cg.LambdaExpression)
|
||||
lambda_str = str(result)
|
||||
assert "int32_t x" in lambda_str
|
||||
assert "float y" in lambda_str
|
||||
assert "bool z" in lambda_str
|
||||
|
||||
async def test_process_lambda__parameter_validation(self):
|
||||
"""Test that malformed parameters raise assertion error."""
|
||||
from esphome.core import Lambda
|
||||
|
||||
lambda_obj = Lambda("return x;")
|
||||
|
||||
# Test invalid parameter format (not list of tuples)
|
||||
with pytest.raises(AssertionError):
|
||||
await cg.process_lambda(lambda_obj, "invalid")
|
||||
|
||||
# Test invalid tuple format (not 2-element tuples)
|
||||
with pytest.raises(AssertionError):
|
||||
await cg.process_lambda(lambda_obj, [(int, "x", "extra")])
|
||||
|
||||
# Test invalid tuple format (single element)
|
||||
with pytest.raises(AssertionError):
|
||||
await cg.process_lambda(lambda_obj, [(int,)])
|
||||
|
||||
Reference in New Issue
Block a user