mirror of
https://github.com/esphome/esphome.git
synced 2026-10-06 19:06:37 +00:00
Merge branch 'dev' into partition-table-ota
This commit is contained in:
@@ -11,11 +11,19 @@ def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
|
||||
async def to_code(config):
|
||||
await original_to_code(config)
|
||||
# Enable BLE proto message types for benchmarks. The real
|
||||
# bluetooth_proxy component is ESP32-only; a lightweight stub
|
||||
# header in tests/benchmarks/stubs/ satisfies the include.
|
||||
# Enable proxy proto message types for benchmarks. The real
|
||||
# components have hardware dependencies (BLE/UART/RMT); lightweight
|
||||
# stub headers in tests/benchmarks/stubs/ satisfy the includes.
|
||||
cg.add_define("USE_BLUETOOTH_PROXY")
|
||||
cg.add_define("BLUETOOTH_PROXY_MAX_CONNECTIONS", 3)
|
||||
cg.add_define("BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE", 16)
|
||||
cg.add_define("USE_ZWAVE_PROXY")
|
||||
cg.add_define("USE_INFRARED")
|
||||
cg.add_define("USE_IR_RF")
|
||||
cg.add_define("USE_RADIO_FREQUENCY")
|
||||
cg.add_define("USE_SERIAL_PROXY")
|
||||
cg.add_define("SERIAL_PROXY_COUNT", 0)
|
||||
cg.add_define("ESPHOME_ENTITY_INFRARED_COUNT", 0)
|
||||
cg.add_define("ESPHOME_ENTITY_RADIO_FREQUENCY_COUNT", 0)
|
||||
|
||||
manifest.to_code = to_code
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
// Encode/decode microbenchmarks for proxy message families that carry
|
||||
// high-volume traffic (Z-Wave, IR/RF, serial). Mirrors the existing
|
||||
// BluetoothLERawAdvertisementsResponse benchmarks in bench_proto_encode.cpp.
|
||||
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "esphome/components/api/api_pb2.h"
|
||||
#include "esphome/components/api/api_buffer.h"
|
||||
|
||||
namespace esphome::api::benchmarks {
|
||||
|
||||
static constexpr int kInnerIterations = 2000;
|
||||
|
||||
// Encodes `src` into `out`. Caller owns `out` and must keep it alive across
|
||||
// the decode loop (decoded messages may store pointers back into its bytes).
|
||||
template<typename T> static void encode_into(APIBuffer &out, const T &src) {
|
||||
out.resize(src.calculate_size());
|
||||
ProtoWriteBuffer writer(&out, 0);
|
||||
src.encode(writer);
|
||||
}
|
||||
|
||||
// --- ZWaveProxyFrame (Z-Wave frame, ~16 bytes payload) ---
|
||||
|
||||
#ifdef USE_ZWAVE_PROXY
|
||||
|
||||
static const uint8_t kZWaveFrameData[] = {0x01, 0x09, 0x00, 0x13, 0x01, 0x02, 0x00, 0x00,
|
||||
0x25, 0x00, 0x05, 0xC4, 0x00, 0x00, 0x00, 0x00};
|
||||
|
||||
static void Encode_ZWaveProxyFrame(benchmark::State &state) {
|
||||
ZWaveProxyFrame msg;
|
||||
msg.data = kZWaveFrameData;
|
||||
msg.data_len = sizeof(kZWaveFrameData);
|
||||
APIBuffer buffer;
|
||||
buffer.resize(msg.calculate_size());
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
ProtoWriteBuffer writer(&buffer, 0);
|
||||
msg.encode(writer);
|
||||
}
|
||||
benchmark::DoNotOptimize(buffer.data());
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Encode_ZWaveProxyFrame);
|
||||
|
||||
static void Decode_ZWaveProxyFrame(benchmark::State &state) {
|
||||
ZWaveProxyFrame source;
|
||||
source.data = kZWaveFrameData;
|
||||
source.data_len = sizeof(kZWaveFrameData);
|
||||
APIBuffer encoded;
|
||||
encode_into(encoded, source);
|
||||
const uint8_t *data = encoded.data();
|
||||
size_t size = encoded.size();
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
ZWaveProxyFrame msg;
|
||||
msg.decode(data, size);
|
||||
benchmark::DoNotOptimize(msg);
|
||||
}
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Decode_ZWaveProxyFrame);
|
||||
|
||||
static const uint8_t kZWaveRequestData[] = {0xDE, 0xAD, 0xBE, 0xEF};
|
||||
|
||||
static void Decode_ZWaveProxyRequest(benchmark::State &state) {
|
||||
ZWaveProxyRequest source;
|
||||
source.type = enums::ZWAVE_PROXY_REQUEST_TYPE_HOME_ID_CHANGE;
|
||||
source.data = kZWaveRequestData;
|
||||
source.data_len = sizeof(kZWaveRequestData);
|
||||
APIBuffer encoded;
|
||||
encode_into(encoded, source);
|
||||
const uint8_t *data = encoded.data();
|
||||
size_t size = encoded.size();
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
ZWaveProxyRequest msg;
|
||||
msg.decode(data, size);
|
||||
benchmark::DoNotOptimize(msg);
|
||||
}
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Decode_ZWaveProxyRequest);
|
||||
|
||||
#endif // USE_ZWAVE_PROXY
|
||||
|
||||
// --- SerialProxyDataReceived encode + SerialProxyWriteRequest decode ---
|
||||
//
|
||||
// SerialProxyWriteRequest is decode-only (SOURCE_CLIENT) but has the same
|
||||
// wire layout as SerialProxyDataReceived, so we encode via the latter and
|
||||
// decode as the former.
|
||||
|
||||
#ifdef USE_SERIAL_PROXY
|
||||
|
||||
static constexpr size_t kSerialPayloadSize = 64;
|
||||
static const uint8_t kSerialPayload[kSerialPayloadSize] = {
|
||||
0x55, 0xAA, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
|
||||
0xCD, 0xEF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
|
||||
0xFF, 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0,
|
||||
0xF0, 0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F, 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF};
|
||||
|
||||
static void Encode_SerialProxyDataReceived(benchmark::State &state) {
|
||||
SerialProxyDataReceived msg;
|
||||
msg.instance = 0;
|
||||
msg.set_data(kSerialPayload, kSerialPayloadSize);
|
||||
APIBuffer buffer;
|
||||
buffer.resize(msg.calculate_size());
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
ProtoWriteBuffer writer(&buffer, 0);
|
||||
msg.encode(writer);
|
||||
}
|
||||
benchmark::DoNotOptimize(buffer.data());
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Encode_SerialProxyDataReceived);
|
||||
|
||||
static void Decode_SerialProxyWriteRequest(benchmark::State &state) {
|
||||
SerialProxyDataReceived source;
|
||||
source.instance = 0;
|
||||
source.set_data(kSerialPayload, kSerialPayloadSize);
|
||||
APIBuffer encoded;
|
||||
encode_into(encoded, source);
|
||||
const uint8_t *data = encoded.data();
|
||||
size_t size = encoded.size();
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
SerialProxyWriteRequest msg;
|
||||
msg.decode(data, size);
|
||||
benchmark::DoNotOptimize(msg);
|
||||
}
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Decode_SerialProxyWriteRequest);
|
||||
|
||||
#endif // USE_SERIAL_PROXY
|
||||
|
||||
// --- InfraredRFReceiveEvent encode (100 sint32 timings) +
|
||||
// InfraredRFTransmitRawTimingsRequest decode (hand-built wire bytes) ---
|
||||
|
||||
#if defined(USE_IR_RF) || defined(USE_RADIO_FREQUENCY)
|
||||
|
||||
// Mark/space pairs simulating a typical RC-5 / NEC capture (100 timings).
|
||||
static std::vector<int32_t> make_ir_timings_100() {
|
||||
std::vector<int32_t> v;
|
||||
v.reserve(100);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
v.push_back((i % 2 == 0) ? 560 : -560);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static const std::vector<int32_t> &get_ir_timings_100() {
|
||||
static const std::vector<int32_t> timings = make_ir_timings_100();
|
||||
return timings;
|
||||
}
|
||||
|
||||
static void Encode_InfraredRFReceiveEvent(benchmark::State &state) {
|
||||
InfraredRFReceiveEvent msg;
|
||||
msg.key = 0xDEADBEEF;
|
||||
msg.timings = &get_ir_timings_100();
|
||||
APIBuffer buffer;
|
||||
buffer.resize(msg.calculate_size());
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
ProtoWriteBuffer writer(&buffer, 0);
|
||||
msg.encode(writer);
|
||||
}
|
||||
benchmark::DoNotOptimize(buffer.data());
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Encode_InfraredRFReceiveEvent);
|
||||
|
||||
static void CalculateSize_InfraredRFReceiveEvent(benchmark::State &state) {
|
||||
InfraredRFReceiveEvent msg;
|
||||
msg.key = 0xDEADBEEF;
|
||||
msg.timings = &get_ir_timings_100();
|
||||
|
||||
for (auto _ : state) {
|
||||
uint32_t result = 0;
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
result += msg.calculate_size();
|
||||
}
|
||||
benchmark::DoNotOptimize(result);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(CalculateSize_InfraredRFReceiveEvent);
|
||||
|
||||
// Hand-built wire bytes for InfraredRFTransmitRawTimingsRequest (decode-only,
|
||||
// no sister message with identical wire layout).
|
||||
// field 2 (key, fixed32): tag=0x15, 4 LE bytes
|
||||
// field 3 (carrier_frequency): tag=0x18, varint
|
||||
// field 4 (repeat_count): tag=0x20, varint
|
||||
// field 5 (timings, packed sint32): tag=0x2A, length varint, packed payload
|
||||
// field 6 (modulation): tag=0x30, varint
|
||||
static APIBuffer build_infrared_rf_transmit_wire() {
|
||||
uint8_t bytes[256];
|
||||
size_t len = 0;
|
||||
|
||||
auto put_byte = [&](uint8_t b) { bytes[len++] = b; };
|
||||
auto put_varint = [&](uint32_t v) {
|
||||
while (v >= 0x80) {
|
||||
bytes[len++] = static_cast<uint8_t>((v & 0x7F) | 0x80);
|
||||
v >>= 7;
|
||||
}
|
||||
bytes[len++] = static_cast<uint8_t>(v);
|
||||
};
|
||||
auto encode_zigzag = [](int32_t v) -> uint32_t {
|
||||
return (static_cast<uint32_t>(v) << 1) ^ static_cast<uint32_t>(v >> 31);
|
||||
};
|
||||
|
||||
put_byte(0x15);
|
||||
put_byte(0xEF);
|
||||
put_byte(0xBE);
|
||||
put_byte(0xAD);
|
||||
put_byte(0xDE);
|
||||
put_byte(0x18);
|
||||
put_varint(38000);
|
||||
put_byte(0x20);
|
||||
put_varint(2);
|
||||
|
||||
uint8_t packed[200];
|
||||
size_t packed_len = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int32_t value = (i % 2 == 0) ? 560 : -560;
|
||||
uint32_t zz = encode_zigzag(value);
|
||||
while (zz >= 0x80) {
|
||||
packed[packed_len++] = static_cast<uint8_t>((zz & 0x7F) | 0x80);
|
||||
zz >>= 7;
|
||||
}
|
||||
packed[packed_len++] = static_cast<uint8_t>(zz);
|
||||
}
|
||||
put_byte(0x2A);
|
||||
put_varint(static_cast<uint32_t>(packed_len));
|
||||
std::memcpy(bytes + len, packed, packed_len);
|
||||
len += packed_len;
|
||||
// field 6: modulation = 1 (non-zero so it's actually emitted and exercises
|
||||
// decode_varint for this field, matching the documented layout above).
|
||||
put_byte(0x30);
|
||||
put_varint(1);
|
||||
|
||||
APIBuffer buf;
|
||||
buf.resize(len);
|
||||
std::memcpy(buf.data(), bytes, len);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void Decode_InfraredRFTransmitRawTimingsRequest(benchmark::State &state) {
|
||||
auto encoded = build_infrared_rf_transmit_wire();
|
||||
const uint8_t *data = encoded.data();
|
||||
size_t size = encoded.size();
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
InfraredRFTransmitRawTimingsRequest msg;
|
||||
msg.decode(data, size);
|
||||
benchmark::DoNotOptimize(msg);
|
||||
}
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(Decode_InfraredRFTransmitRawTimingsRequest);
|
||||
|
||||
#endif // USE_IR_RF || USE_RADIO_FREQUENCY
|
||||
|
||||
} // namespace esphome::api::benchmarks
|
||||
@@ -0,0 +1,45 @@
|
||||
// Stub for benchmark builds — provides the minimal interface that
|
||||
// api_connection.cpp and Application need when USE_INFRARED is defined,
|
||||
// without pulling in the real remote_base/RMT dependencies.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
|
||||
namespace esphome::infrared {
|
||||
|
||||
class Infrared;
|
||||
|
||||
class InfraredCall {
|
||||
public:
|
||||
explicit InfraredCall(Infrared *parent) : parent_(parent) {}
|
||||
InfraredCall &set_carrier_frequency(uint32_t /*frequency*/) { return *this; }
|
||||
InfraredCall &set_raw_timings_packed(const uint8_t * /*data*/, uint16_t /*length*/, uint16_t /*count*/) {
|
||||
return *this;
|
||||
}
|
||||
InfraredCall &set_repeat_count(uint32_t /*count*/) { return *this; }
|
||||
void perform() {}
|
||||
|
||||
protected:
|
||||
Infrared *parent_;
|
||||
};
|
||||
|
||||
class InfraredTraits {
|
||||
public:
|
||||
uint32_t get_receiver_frequency_hz() const { return 0; }
|
||||
};
|
||||
|
||||
class Infrared : public Component, public EntityBase {
|
||||
public:
|
||||
Infrared() = default;
|
||||
InfraredTraits &get_traits() { return this->traits_; }
|
||||
const InfraredTraits &get_traits() const { return this->traits_; }
|
||||
InfraredCall make_call() { return InfraredCall(this); }
|
||||
uint32_t get_capability_flags() const { return 0; }
|
||||
|
||||
protected:
|
||||
InfraredTraits traits_;
|
||||
};
|
||||
|
||||
} // namespace esphome::infrared
|
||||
@@ -0,0 +1,51 @@
|
||||
// Stub for benchmark builds — provides the minimal interface that
|
||||
// api_connection.cpp and Application need when USE_RADIO_FREQUENCY is defined.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
|
||||
namespace esphome::radio_frequency {
|
||||
|
||||
enum RadioFrequencyModulation : uint32_t {
|
||||
RADIO_FREQUENCY_MODULATION_OOK = 0,
|
||||
};
|
||||
|
||||
class RadioFrequency;
|
||||
|
||||
class RadioFrequencyCall {
|
||||
public:
|
||||
explicit RadioFrequencyCall(RadioFrequency *parent) : parent_(parent) {}
|
||||
RadioFrequencyCall &set_frequency(uint32_t /*frequency*/) { return *this; }
|
||||
RadioFrequencyCall &set_modulation(RadioFrequencyModulation /*mod*/) { return *this; }
|
||||
RadioFrequencyCall &set_repeat_count(uint32_t /*count*/) { return *this; }
|
||||
RadioFrequencyCall &set_raw_timings_packed(const uint8_t * /*data*/, uint16_t /*length*/, uint16_t /*count*/) {
|
||||
return *this;
|
||||
}
|
||||
void perform() {}
|
||||
|
||||
protected:
|
||||
RadioFrequency *parent_;
|
||||
};
|
||||
|
||||
class RadioFrequencyTraits {
|
||||
public:
|
||||
uint32_t get_frequency_min_hz() const { return 0; }
|
||||
uint32_t get_frequency_max_hz() const { return 0; }
|
||||
uint32_t get_supported_modulations() const { return 0; }
|
||||
};
|
||||
|
||||
class RadioFrequency : public Component, public EntityBase {
|
||||
public:
|
||||
RadioFrequency() = default;
|
||||
RadioFrequencyTraits &get_traits() { return this->traits_; }
|
||||
const RadioFrequencyTraits &get_traits() const { return this->traits_; }
|
||||
RadioFrequencyCall make_call() { return RadioFrequencyCall(this); }
|
||||
uint32_t get_capability_flags() const { return 0; }
|
||||
|
||||
protected:
|
||||
RadioFrequencyTraits traits_;
|
||||
};
|
||||
|
||||
} // namespace esphome::radio_frequency
|
||||
@@ -0,0 +1,46 @@
|
||||
// Stub for benchmark builds — provides the minimal interface that
|
||||
// api_connection.cpp and Application need when USE_SERIAL_PROXY is defined,
|
||||
// without pulling in the real UART implementation.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include "esphome/components/api/api_pb2.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
namespace api {
|
||||
class APIConnection;
|
||||
} // namespace api
|
||||
|
||||
namespace uart {
|
||||
enum class UARTFlushResult : uint8_t {
|
||||
UART_FLUSH_RESULT_SUCCESS,
|
||||
UART_FLUSH_RESULT_ASSUMED_SUCCESS,
|
||||
UART_FLUSH_RESULT_TIMEOUT,
|
||||
UART_FLUSH_RESULT_FAILED,
|
||||
};
|
||||
} // namespace uart
|
||||
|
||||
namespace serial_proxy {
|
||||
|
||||
class SerialProxy {
|
||||
public:
|
||||
void set_instance_index(uint32_t index) { this->instance_index_ = index; }
|
||||
uint32_t get_instance_index() const { return this->instance_index_; }
|
||||
const char *get_name() const { return ""; }
|
||||
api::enums::SerialProxyPortType get_port_type() const { return {}; }
|
||||
api::APIConnection *get_api_connection() { return nullptr; }
|
||||
void serial_proxy_request(api::APIConnection *conn, api::enums::SerialProxyRequestType type) {}
|
||||
void configure(uint32_t baudrate, bool flow_control, uint8_t parity, uint32_t stop_bits, uint32_t data_size) {}
|
||||
void write_from_client(const uint8_t *data, size_t len) {}
|
||||
void set_modem_pins(uint32_t line_states) {}
|
||||
uint32_t get_modem_pins() const { return 0; }
|
||||
uart::UARTFlushResult flush_port() { return uart::UARTFlushResult::UART_FLUSH_RESULT_SUCCESS; }
|
||||
|
||||
protected:
|
||||
uint32_t instance_index_{0};
|
||||
};
|
||||
|
||||
} // namespace serial_proxy
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,29 @@
|
||||
// Stub for benchmark builds — provides the minimal interface that
|
||||
// api_connection.cpp needs when USE_ZWAVE_PROXY is defined,
|
||||
// without pulling in the real UART-based ZWaveProxy implementation.
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/api/api_pb2.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace api {
|
||||
class APIConnection;
|
||||
} // namespace api
|
||||
|
||||
namespace zwave_proxy {
|
||||
|
||||
class ZWaveProxy {
|
||||
public:
|
||||
api::APIConnection *get_api_connection() { return nullptr; }
|
||||
void zwave_proxy_request(api::APIConnection *conn, api::enums::ZWaveProxyRequestType type) {}
|
||||
void send_frame(const uint8_t *data, size_t length) {}
|
||||
void api_connection_authenticated(api::APIConnection *conn) {}
|
||||
uint32_t get_feature_flags() const { return 0; }
|
||||
uint32_t get_home_id() { return 0; }
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
extern ZWaveProxy *global_zwave_proxy;
|
||||
|
||||
} // namespace zwave_proxy
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,14 @@
|
||||
audio:
|
||||
codecs:
|
||||
flac:
|
||||
buffer_memory: internal
|
||||
mp3:
|
||||
buffer_memory: psram
|
||||
opus:
|
||||
floating_point: false
|
||||
state_memory: psram
|
||||
pseudostack:
|
||||
threadsafe: false
|
||||
buffer_memory: internal
|
||||
size: 80000
|
||||
wav:
|
||||
@@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
||||
@@ -7,3 +7,4 @@ media_source:
|
||||
initial_static_delay: 5ms
|
||||
static_delay_adjustable: true
|
||||
fixed_delay: 480us
|
||||
decode_memory: internal
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
esphome:
|
||||
name: climate-control-action-test
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
globals:
|
||||
- id: test_target_temp
|
||||
type: float
|
||||
initial_value: "21.5"
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
id: temp_sensor
|
||||
name: "Temp"
|
||||
lambda: 'return 20.0;'
|
||||
update_interval: 60s
|
||||
|
||||
climate:
|
||||
- platform: thermostat
|
||||
id: test_climate
|
||||
name: "Test Climate"
|
||||
sensor: temp_sensor
|
||||
min_idle_time: 30s
|
||||
min_heating_off_time: 300s
|
||||
min_heating_run_time: 300s
|
||||
min_cooling_off_time: 300s
|
||||
min_cooling_run_time: 300s
|
||||
heat_action:
|
||||
- logger.log: heating
|
||||
idle_action:
|
||||
- logger.log: idle
|
||||
cool_action:
|
||||
- logger.log: cooling
|
||||
heat_cool_mode:
|
||||
- logger.log: heat_cool
|
||||
preset:
|
||||
- name: Default
|
||||
default_target_temperature_low: 18 °C
|
||||
default_target_temperature_high: 22 °C
|
||||
visual:
|
||||
min_temperature: 10 °C
|
||||
max_temperature: 30 °C
|
||||
|
||||
button:
|
||||
# mode only
|
||||
- platform: template
|
||||
id: btn_mode
|
||||
name: "Set Mode Heat"
|
||||
on_press:
|
||||
- climate.control:
|
||||
id: test_climate
|
||||
mode: HEAT
|
||||
|
||||
# mode + target_temperature_low + target_temperature_high
|
||||
- platform: template
|
||||
id: btn_mode_temps
|
||||
name: "Set Mode Temps"
|
||||
on_press:
|
||||
- climate.control:
|
||||
id: test_climate
|
||||
mode: HEAT_COOL
|
||||
target_temperature_low: 19.0 °C
|
||||
target_temperature_high: 23.0 °C
|
||||
|
||||
# target_temperature_low only
|
||||
- platform: template
|
||||
id: btn_low_only
|
||||
name: "Set Low Only"
|
||||
on_press:
|
||||
- climate.control:
|
||||
id: test_climate
|
||||
target_temperature_low: 17.5 °C
|
||||
|
||||
# Lambda path: target_temperature_high computed at runtime
|
||||
- platform: template
|
||||
id: btn_lambda_high
|
||||
name: "Lambda High"
|
||||
on_press:
|
||||
- climate.control:
|
||||
id: test_climate
|
||||
target_temperature_high: !lambda "return id(test_target_temp);"
|
||||
|
||||
# mode only — turn off via mode
|
||||
- platform: template
|
||||
id: btn_off
|
||||
name: "Set Off"
|
||||
on_press:
|
||||
- climate.control:
|
||||
id: test_climate
|
||||
mode: "OFF"
|
||||
@@ -0,0 +1,84 @@
|
||||
"""Integration test for climate ControlAction.
|
||||
|
||||
Tests that climate.control automation actions work correctly with the
|
||||
single stateless apply lambda/function pointer implementation. Exercises
|
||||
multiple field combinations and the lambda path.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from aioesphomeapi import (
|
||||
ButtonInfo,
|
||||
ClimateInfo,
|
||||
ClimateMode,
|
||||
ClimateState,
|
||||
EntityState,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from .state_utils import InitialStateHelper, require_entity
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_climate_control_action(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""Test climate ControlAction with constants and lambdas."""
|
||||
loop = asyncio.get_running_loop()
|
||||
async with run_compiled(yaml_config), api_client_connected() as client:
|
||||
climate_state_future: asyncio.Future[ClimateState] | None = None
|
||||
|
||||
def on_state(state: EntityState) -> None:
|
||||
if (
|
||||
isinstance(state, ClimateState)
|
||||
and climate_state_future is not None
|
||||
and not climate_state_future.done()
|
||||
):
|
||||
climate_state_future.set_result(state)
|
||||
|
||||
async def wait_for_climate_state(timeout: float = 5.0) -> ClimateState:
|
||||
nonlocal climate_state_future
|
||||
climate_state_future = loop.create_future()
|
||||
try:
|
||||
return await asyncio.wait_for(climate_state_future, timeout)
|
||||
finally:
|
||||
climate_state_future = None
|
||||
|
||||
entities, _ = await client.list_entities_services()
|
||||
initial_state_helper = InitialStateHelper(entities)
|
||||
client.subscribe_states(initial_state_helper.on_state_wrapper(on_state))
|
||||
await initial_state_helper.wait_for_initial_states()
|
||||
|
||||
require_entity(entities, "test_climate", ClimateInfo)
|
||||
|
||||
async def press_and_wait(name: str) -> ClimateState:
|
||||
btn = require_entity(entities, name.lower().replace(" ", "_"), ButtonInfo)
|
||||
client.button_command(btn.key)
|
||||
return await wait_for_climate_state()
|
||||
|
||||
# mode only — set HEAT
|
||||
state = await press_and_wait("Set Mode Heat")
|
||||
assert state.mode == ClimateMode.HEAT
|
||||
|
||||
# mode + target_temperature_low + target_temperature_high
|
||||
state = await press_and_wait("Set Mode Temps")
|
||||
assert state.mode == ClimateMode.HEAT_COOL
|
||||
assert state.target_temperature_low == pytest.approx(19.0, abs=0.5)
|
||||
assert state.target_temperature_high == pytest.approx(23.0, abs=0.5)
|
||||
|
||||
# target_temperature_low only
|
||||
state = await press_and_wait("Set Low Only")
|
||||
assert state.target_temperature_low == pytest.approx(17.5, abs=0.5)
|
||||
|
||||
# lambda path: target_temperature_high computed at runtime
|
||||
state = await press_and_wait("Lambda High")
|
||||
assert state.target_temperature_high == pytest.approx(21.5, abs=0.5)
|
||||
|
||||
# mode only — turn off via mode
|
||||
state = await press_and_wait("Set Off")
|
||||
assert state.mode == ClimateMode.OFF
|
||||
@@ -122,10 +122,19 @@ def test_main_all_tests_should_run(
|
||||
"esphome/helpers.py",
|
||||
]
|
||||
|
||||
# Stable, deterministic stand-in for the tests/integration/ glob so the
|
||||
# bucket assertions don't drift with the real test count.
|
||||
fake_test_files = [f"tests/integration/test_{i:03d}.py" for i in range(15)]
|
||||
|
||||
# Run main function with mocked argv
|
||||
with (
|
||||
patch("sys.argv", ["determine-jobs.py"]),
|
||||
patch.object(determine_jobs, "_is_clang_tidy_full_scan", return_value=False),
|
||||
patch.object(
|
||||
determine_jobs,
|
||||
"_all_integration_test_files",
|
||||
return_value=fake_test_files,
|
||||
),
|
||||
patch.object(
|
||||
determine_jobs,
|
||||
"get_changed_components",
|
||||
@@ -161,8 +170,24 @@ def test_main_all_tests_should_run(
|
||||
output = json.loads(captured.out)
|
||||
|
||||
assert output["integration_tests"] is True
|
||||
assert output["integration_tests_run_all"] is True
|
||||
assert output["integration_test_files"] == []
|
||||
# run_all=True expands to the full glob and pre-buckets into 3 parts.
|
||||
# Each bucket's `tests` is a JSON list of file paths.
|
||||
assert isinstance(output["integration_test_buckets"], list)
|
||||
assert len(output["integration_test_buckets"]) == 3
|
||||
assert [b["name"] for b in output["integration_test_buckets"]] == [
|
||||
"1/3",
|
||||
"2/3",
|
||||
"3/3",
|
||||
]
|
||||
for bucket in output["integration_test_buckets"]:
|
||||
assert isinstance(bucket["tests"], list)
|
||||
for path in bucket["tests"]:
|
||||
assert isinstance(path, str)
|
||||
bucket_files = [f for b in output["integration_test_buckets"] for f in b["tests"]]
|
||||
assert bucket_files == fake_test_files
|
||||
# Bucket sizes are balanced (max-min difference at most 1).
|
||||
sizes = [len(b["tests"]) for b in output["integration_test_buckets"]]
|
||||
assert max(sizes) - min(sizes) <= 1
|
||||
assert output["clang_tidy"] is True
|
||||
assert output["clang_tidy_mode"] in ["nosplit", "split"]
|
||||
assert output["clang_format"] is True
|
||||
@@ -247,8 +272,7 @@ def test_main_no_tests_should_run(
|
||||
output = json.loads(captured.out)
|
||||
|
||||
assert output["integration_tests"] is False
|
||||
assert output["integration_tests_run_all"] is False
|
||||
assert output["integration_test_files"] == []
|
||||
assert output["integration_test_buckets"] == []
|
||||
assert output["clang_tidy"] is False
|
||||
assert output["clang_tidy_mode"] == "disabled"
|
||||
assert output["clang_format"] is False
|
||||
@@ -332,8 +356,7 @@ def test_main_with_branch_argument(
|
||||
output = json.loads(captured.out)
|
||||
|
||||
assert output["integration_tests"] is False
|
||||
assert output["integration_tests_run_all"] is False
|
||||
assert output["integration_test_files"] == []
|
||||
assert output["integration_test_buckets"] == []
|
||||
assert output["clang_tidy"] is True
|
||||
assert output["clang_tidy_mode"] in ["nosplit", "split"]
|
||||
assert output["clang_format"] is False
|
||||
@@ -357,6 +380,59 @@ def test_main_with_branch_argument(
|
||||
assert output["cpp_unit_tests_components"] == ["mqtt"]
|
||||
|
||||
|
||||
def test_compute_integration_test_buckets_empty() -> None:
|
||||
"""No integration tests scheduled => (False, [])."""
|
||||
run, buckets = determine_jobs._compute_integration_test_buckets(False, [])
|
||||
assert run is False
|
||||
assert buckets == []
|
||||
|
||||
|
||||
def test_compute_integration_test_buckets_below_threshold() -> None:
|
||||
"""A small explicit list (<= threshold) => single 1/1 bucket with that list."""
|
||||
files = [f"tests/integration/test_{name}.py" for name in ("c", "a", "b")]
|
||||
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
|
||||
assert run is True
|
||||
assert buckets == [{"name": "1/1", "tests": sorted(files)}]
|
||||
|
||||
|
||||
def test_compute_integration_test_buckets_at_threshold_stays_single() -> None:
|
||||
"""Exactly INTEGRATION_TESTS_SPLIT_THRESHOLD files => still one bucket
|
||||
(the split kicks in only when count is strictly greater than threshold)."""
|
||||
files = [
|
||||
f"tests/integration/test_{i:02d}.py"
|
||||
for i in range(determine_jobs.INTEGRATION_TESTS_SPLIT_THRESHOLD)
|
||||
]
|
||||
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
|
||||
assert run is True
|
||||
assert len(buckets) == 1
|
||||
assert buckets[0]["name"] == "1/1"
|
||||
assert buckets[0]["tests"] == sorted(files)
|
||||
|
||||
|
||||
def test_compute_integration_test_buckets_just_over_threshold_splits() -> None:
|
||||
"""One file over the threshold triggers the 3-bucket fan-out, balanced."""
|
||||
n = determine_jobs.INTEGRATION_TESTS_SPLIT_THRESHOLD + 1
|
||||
files = [f"tests/integration/test_{i:02d}.py" for i in range(n)]
|
||||
run, buckets = determine_jobs._compute_integration_test_buckets(False, files)
|
||||
assert run is True
|
||||
assert [b["name"] for b in buckets] == ["1/3", "2/3", "3/3"]
|
||||
union = [path for b in buckets for path in b["tests"]]
|
||||
assert union == sorted(files)
|
||||
sizes = [len(b["tests"]) for b in buckets]
|
||||
assert max(sizes) - min(sizes) <= 1
|
||||
|
||||
|
||||
def test_compute_integration_test_buckets_run_all_with_empty_glob_disables_run() -> (
|
||||
None
|
||||
):
|
||||
"""run_all=True but glob returns no files => run suppressed (otherwise
|
||||
pytest would collect tests outside tests/integration/)."""
|
||||
with patch.object(determine_jobs, "_all_integration_test_files", return_value=[]):
|
||||
run, buckets = determine_jobs._compute_integration_test_buckets(True, [])
|
||||
assert run is False
|
||||
assert buckets == []
|
||||
|
||||
|
||||
def test_determine_integration_tests(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user