mirror of
https://github.com/esphome/esphome.git
synced 2025-11-03 00:21:56 +00:00
Compare commits
23 Commits
fan_no_dou
...
web_server
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca9b78100d | ||
|
|
edde2fc94c | ||
|
|
1fc3165b58 | ||
|
|
d25121a55c | ||
|
|
48a6d4c3f0 | ||
|
|
e91b0bb804 | ||
|
|
00abf7da72 | ||
|
|
afcce8e5c6 | ||
|
|
5b00ff1bf1 | ||
|
|
0c101768d7 | ||
|
|
2f56af0078 | ||
|
|
f502907c7a | ||
|
|
55af818629 | ||
|
|
c662697ca7 | ||
|
|
e28c152298 | ||
|
|
0b4d445794 | ||
|
|
4d1d37a911 | ||
|
|
8df5a3a630 | ||
|
|
5a5894eaa3 | ||
|
|
d9d2d2f6b9 | ||
|
|
30f2a4395f | ||
|
|
292abd1187 | ||
|
|
6d0527ff2a |
@@ -11,7 +11,7 @@ ci:
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.14.2
|
||||
rev: v0.14.3
|
||||
hooks:
|
||||
# Run the linter.
|
||||
- id: ruff
|
||||
|
||||
@@ -182,7 +182,7 @@ def validate_automation(extra_schema=None, extra_validators=None, single=False):
|
||||
value = cv.Schema([extra_validators])(value)
|
||||
if single:
|
||||
if len(value) != 1:
|
||||
raise cv.Invalid("Cannot have more than 1 automation for templates")
|
||||
raise cv.Invalid("This trigger allows only a single automation")
|
||||
return value[0]
|
||||
return value
|
||||
|
||||
|
||||
@@ -425,7 +425,7 @@ message ListEntitiesFanResponse {
|
||||
bool disabled_by_default = 9;
|
||||
string icon = 10 [(field_ifdef) = "USE_ENTITY_ICON"];
|
||||
EntityCategory entity_category = 11;
|
||||
repeated string supported_preset_modes = 12 [(container_pointer_no_template) = "std::vector<const char *>"];
|
||||
repeated string supported_preset_modes = 12 [(container_pointer) = "std::set"];
|
||||
uint32 device_id = 13 [(field_ifdef) = "USE_DEVICES"];
|
||||
}
|
||||
// Deprecated in API version 1.6 - only used in deprecated fields
|
||||
|
||||
@@ -410,8 +410,8 @@ uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *co
|
||||
}
|
||||
if (traits.supports_direction())
|
||||
msg.direction = static_cast<enums::FanDirection>(fan->direction);
|
||||
if (traits.supports_preset_modes() && fan->has_preset_mode())
|
||||
msg.set_preset_mode(StringRef(fan->get_preset_mode()));
|
||||
if (traits.supports_preset_modes())
|
||||
msg.set_preset_mode(StringRef(fan->preset_mode));
|
||||
return fill_and_encode_entity_state(fan, msg, FanStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@@ -423,7 +423,7 @@ uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *con
|
||||
msg.supports_speed = traits.supports_speed();
|
||||
msg.supports_direction = traits.supports_direction();
|
||||
msg.supported_speed_count = traits.supported_speed_count();
|
||||
msg.supported_preset_modes = &traits.supported_preset_modes();
|
||||
msg.supported_preset_modes = &traits.supported_preset_modes_for_api_();
|
||||
return fill_and_encode_entity_info(fan, msg, ListEntitiesFanResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::fan_command(const FanCommandRequest &msg) {
|
||||
|
||||
@@ -434,8 +434,7 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, st
|
||||
return APIError::OK;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
||||
uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer
|
||||
uint8_t *buffer_data = buffer.get_buffer()->data();
|
||||
|
||||
this->reusable_iovs_.clear();
|
||||
this->reusable_iovs_.reserve(packets.size());
|
||||
|
||||
@@ -230,8 +230,7 @@ APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer
|
||||
return APIError::OK;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
||||
uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer
|
||||
uint8_t *buffer_data = buffer.get_buffer()->data();
|
||||
|
||||
this->reusable_iovs_.clear();
|
||||
this->reusable_iovs_.reserve(packets.size());
|
||||
|
||||
@@ -355,8 +355,8 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(10, this->icon_ref_);
|
||||
#endif
|
||||
buffer.encode_uint32(11, static_cast<uint32_t>(this->entity_category));
|
||||
for (const char *it : *this->supported_preset_modes) {
|
||||
buffer.encode_string(12, it, strlen(it), true);
|
||||
for (const auto &it : *this->supported_preset_modes) {
|
||||
buffer.encode_string(12, it, true);
|
||||
}
|
||||
#ifdef USE_DEVICES
|
||||
buffer.encode_uint32(13, this->device_id);
|
||||
@@ -376,8 +376,8 @@ void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const {
|
||||
#endif
|
||||
size.add_uint32(1, static_cast<uint32_t>(this->entity_category));
|
||||
if (!this->supported_preset_modes->empty()) {
|
||||
for (const char *it : *this->supported_preset_modes) {
|
||||
size.add_length_force(1, strlen(it));
|
||||
for (const auto &it : *this->supported_preset_modes) {
|
||||
size.add_length_force(1, it.size());
|
||||
}
|
||||
}
|
||||
#ifdef USE_DEVICES
|
||||
|
||||
@@ -725,7 +725,7 @@ class ListEntitiesFanResponse final : public InfoResponseProtoMessage {
|
||||
bool supports_speed{false};
|
||||
bool supports_direction{false};
|
||||
int32_t supported_speed_count{0};
|
||||
const std::vector<const char *> *supported_preset_modes{};
|
||||
const std::set<std::string> *supported_preset_modes{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
void calculate_size(ProtoSize &size) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
|
||||
@@ -12,7 +12,7 @@ void CopyFan::setup() {
|
||||
this->oscillating = source_->oscillating;
|
||||
this->speed = source_->speed;
|
||||
this->direction = source_->direction;
|
||||
this->set_preset_mode_(source_->get_preset_mode());
|
||||
this->preset_mode = source_->preset_mode;
|
||||
this->publish_state();
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ void CopyFan::setup() {
|
||||
this->oscillating = source_->oscillating;
|
||||
this->speed = source_->speed;
|
||||
this->direction = source_->direction;
|
||||
this->set_preset_mode_(source_->get_preset_mode());
|
||||
this->preset_mode = source_->preset_mode;
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ void CopyFan::control(const fan::FanCall &call) {
|
||||
call2.set_speed(*call.get_speed());
|
||||
if (call.get_direction().has_value())
|
||||
call2.set_direction(*call.get_direction());
|
||||
if (call.has_preset_mode())
|
||||
if (!call.get_preset_mode().empty())
|
||||
call2.set_preset_mode(call.get_preset_mode());
|
||||
call2.perform();
|
||||
}
|
||||
|
||||
@@ -212,18 +212,18 @@ class FanPresetSetTrigger : public Trigger<std::string> {
|
||||
public:
|
||||
FanPresetSetTrigger(Fan *state) {
|
||||
state->add_on_state_callback([this, state]() {
|
||||
const auto *preset_mode = state->get_preset_mode();
|
||||
auto preset_mode = state->preset_mode;
|
||||
auto should_trigger = preset_mode != this->last_preset_mode_;
|
||||
this->last_preset_mode_ = preset_mode;
|
||||
if (should_trigger && preset_mode != nullptr) {
|
||||
if (should_trigger) {
|
||||
this->trigger(preset_mode);
|
||||
}
|
||||
});
|
||||
this->last_preset_mode_ = state->get_preset_mode();
|
||||
this->last_preset_mode_ = state->preset_mode;
|
||||
}
|
||||
|
||||
protected:
|
||||
const char *last_preset_mode_{nullptr};
|
||||
std::string last_preset_mode_;
|
||||
};
|
||||
|
||||
} // namespace fan
|
||||
|
||||
@@ -17,27 +17,6 @@ const LogString *fan_direction_to_string(FanDirection direction) {
|
||||
}
|
||||
}
|
||||
|
||||
FanCall &FanCall::set_preset_mode(const std::string &preset_mode) { return this->set_preset_mode(preset_mode.c_str()); }
|
||||
|
||||
FanCall &FanCall::set_preset_mode(const char *preset_mode) {
|
||||
if (preset_mode == nullptr || strlen(preset_mode) == 0) {
|
||||
this->preset_mode_ = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Find and validate pointer from traits immediately
|
||||
auto traits = this->parent_.get_traits();
|
||||
const char *validated_mode = traits.find_preset_mode(preset_mode);
|
||||
if (validated_mode != nullptr) {
|
||||
this->preset_mode_ = validated_mode; // Store pointer from traits
|
||||
} else {
|
||||
// Preset mode not found in traits - log warning and don't set
|
||||
ESP_LOGW(TAG, "%s: Preset mode '%s' not supported", this->parent_.get_name().c_str(), preset_mode);
|
||||
this->preset_mode_ = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void FanCall::perform() {
|
||||
ESP_LOGD(TAG, "'%s' - Setting:", this->parent_.get_name().c_str());
|
||||
this->validate_();
|
||||
@@ -53,8 +32,8 @@ void FanCall::perform() {
|
||||
if (this->direction_.has_value()) {
|
||||
ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(*this->direction_)));
|
||||
}
|
||||
if (this->has_preset_mode()) {
|
||||
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_);
|
||||
if (!this->preset_mode_.empty()) {
|
||||
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_.c_str());
|
||||
}
|
||||
this->parent_.control(*this);
|
||||
}
|
||||
@@ -67,15 +46,23 @@ void FanCall::validate_() {
|
||||
|
||||
// https://developers.home-assistant.io/docs/core/entity/fan/#preset-modes
|
||||
// "Manually setting a speed must disable any set preset mode"
|
||||
this->preset_mode_ = nullptr;
|
||||
this->preset_mode_.clear();
|
||||
}
|
||||
|
||||
if (!this->preset_mode_.empty()) {
|
||||
const auto &preset_modes = traits.supported_preset_modes();
|
||||
if (preset_modes.find(this->preset_mode_) == preset_modes.end()) {
|
||||
ESP_LOGW(TAG, "%s: Preset mode '%s' not supported", this->parent_.get_name().c_str(), this->preset_mode_.c_str());
|
||||
this->preset_mode_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// when turning on...
|
||||
if (!this->parent_.state && this->binary_state_.has_value() &&
|
||||
*this->binary_state_
|
||||
// ..,and no preset mode will be active...
|
||||
&& !this->has_preset_mode() &&
|
||||
this->parent_.get_preset_mode() == nullptr
|
||||
&& this->preset_mode_.empty() &&
|
||||
this->parent_.preset_mode.empty()
|
||||
// ...and neither current nor new speed is available...
|
||||
&& traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
|
||||
// ...set speed to 100%
|
||||
@@ -105,12 +92,11 @@ FanCall FanRestoreState::to_call(Fan &fan) {
|
||||
call.set_speed(this->speed);
|
||||
call.set_direction(this->direction);
|
||||
|
||||
auto traits = fan.get_traits();
|
||||
if (traits.supports_preset_modes()) {
|
||||
if (fan.get_traits().supports_preset_modes()) {
|
||||
// Use stored preset index to get preset name
|
||||
const auto &preset_modes = traits.supported_preset_modes();
|
||||
const auto &preset_modes = fan.get_traits().supported_preset_modes();
|
||||
if (this->preset_mode < preset_modes.size()) {
|
||||
call.set_preset_mode(preset_modes[this->preset_mode]);
|
||||
call.set_preset_mode(*std::next(preset_modes.begin(), this->preset_mode));
|
||||
}
|
||||
}
|
||||
return call;
|
||||
@@ -121,12 +107,13 @@ void FanRestoreState::apply(Fan &fan) {
|
||||
fan.speed = this->speed;
|
||||
fan.direction = this->direction;
|
||||
|
||||
// Use stored preset index to get preset name from traits
|
||||
const auto &preset_modes = fan.get_traits().supported_preset_modes();
|
||||
if (this->preset_mode < preset_modes.size()) {
|
||||
fan.set_preset_mode_(preset_modes[this->preset_mode]);
|
||||
if (fan.get_traits().supports_preset_modes()) {
|
||||
// Use stored preset index to get preset name
|
||||
const auto &preset_modes = fan.get_traits().supported_preset_modes();
|
||||
if (this->preset_mode < preset_modes.size()) {
|
||||
fan.preset_mode = *std::next(preset_modes.begin(), this->preset_mode);
|
||||
}
|
||||
}
|
||||
|
||||
fan.publish_state();
|
||||
}
|
||||
|
||||
@@ -135,29 +122,6 @@ FanCall Fan::turn_off() { return this->make_call().set_state(false); }
|
||||
FanCall Fan::toggle() { return this->make_call().set_state(!this->state); }
|
||||
FanCall Fan::make_call() { return FanCall(*this); }
|
||||
|
||||
const char *Fan::find_preset_mode_(const char *preset_mode) { return this->get_traits().find_preset_mode(preset_mode); }
|
||||
|
||||
bool Fan::set_preset_mode_(const char *preset_mode) {
|
||||
if (preset_mode == nullptr) {
|
||||
// Treat nullptr as clearing the preset mode
|
||||
if (this->preset_mode_ == nullptr) {
|
||||
return false; // No change
|
||||
}
|
||||
this->clear_preset_mode_();
|
||||
return true;
|
||||
}
|
||||
const char *validated = this->find_preset_mode_(preset_mode);
|
||||
if (validated == nullptr || this->preset_mode_ == validated) {
|
||||
return false; // Preset mode not supported or no change
|
||||
}
|
||||
this->preset_mode_ = validated;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fan::set_preset_mode_(const std::string &preset_mode) { return this->set_preset_mode_(preset_mode.c_str()); }
|
||||
|
||||
void Fan::clear_preset_mode_() { this->preset_mode_ = nullptr; }
|
||||
|
||||
void Fan::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
|
||||
void Fan::publish_state() {
|
||||
auto traits = this->get_traits();
|
||||
@@ -173,9 +137,8 @@ void Fan::publish_state() {
|
||||
if (traits.supports_direction()) {
|
||||
ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction)));
|
||||
}
|
||||
const char *preset = this->get_preset_mode();
|
||||
if (traits.supports_preset_modes() && preset != nullptr) {
|
||||
ESP_LOGD(TAG, " Preset Mode: %s", preset);
|
||||
if (traits.supports_preset_modes() && !this->preset_mode.empty()) {
|
||||
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode.c_str());
|
||||
}
|
||||
this->state_callback_.call();
|
||||
this->save_state_();
|
||||
@@ -219,24 +182,18 @@ void Fan::save_state_() {
|
||||
return;
|
||||
}
|
||||
|
||||
auto traits = this->get_traits();
|
||||
|
||||
FanRestoreState state{};
|
||||
state.state = this->state;
|
||||
state.oscillating = this->oscillating;
|
||||
state.speed = this->speed;
|
||||
state.direction = this->direction;
|
||||
|
||||
const char *preset = this->get_preset_mode();
|
||||
if (preset != nullptr) {
|
||||
const auto &preset_modes = traits.supported_preset_modes();
|
||||
// Find index of current preset mode (pointer comparison is safe since preset is from traits)
|
||||
for (size_t i = 0; i < preset_modes.size(); i++) {
|
||||
if (preset_modes[i] == preset) {
|
||||
state.preset_mode = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this->get_traits().supports_preset_modes() && !this->preset_mode.empty()) {
|
||||
const auto &preset_modes = this->get_traits().supported_preset_modes();
|
||||
// Store index of current preset mode
|
||||
auto preset_iterator = preset_modes.find(this->preset_mode);
|
||||
if (preset_iterator != preset_modes.end())
|
||||
state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
|
||||
}
|
||||
|
||||
this->rtc_.save(&state);
|
||||
@@ -259,8 +216,8 @@ void Fan::dump_traits_(const char *tag, const char *prefix) {
|
||||
}
|
||||
if (traits.supports_preset_modes()) {
|
||||
ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
|
||||
for (const char *s : traits.supported_preset_modes())
|
||||
ESP_LOGCONFIG(tag, "%s - %s", prefix, s);
|
||||
for (const std::string &s : traits.supported_preset_modes())
|
||||
ESP_LOGCONFIG(tag, "%s - %s", prefix, s.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -70,10 +70,11 @@ class FanCall {
|
||||
return *this;
|
||||
}
|
||||
optional<FanDirection> get_direction() const { return this->direction_; }
|
||||
FanCall &set_preset_mode(const std::string &preset_mode);
|
||||
FanCall &set_preset_mode(const char *preset_mode);
|
||||
const char *get_preset_mode() const { return this->preset_mode_; }
|
||||
bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
|
||||
FanCall &set_preset_mode(const std::string &preset_mode) {
|
||||
this->preset_mode_ = preset_mode;
|
||||
return *this;
|
||||
}
|
||||
std::string get_preset_mode() const { return this->preset_mode_; }
|
||||
|
||||
void perform();
|
||||
|
||||
@@ -85,7 +86,7 @@ class FanCall {
|
||||
optional<bool> oscillating_;
|
||||
optional<int> speed_;
|
||||
optional<FanDirection> direction_{};
|
||||
const char *preset_mode_{nullptr}; // Pointer to string in traits (after validation)
|
||||
std::string preset_mode_{};
|
||||
};
|
||||
|
||||
struct FanRestoreState {
|
||||
@@ -111,6 +112,8 @@ class Fan : public EntityBase {
|
||||
int speed{0};
|
||||
/// The current direction of the fan
|
||||
FanDirection direction{FanDirection::FORWARD};
|
||||
// The current preset mode of the fan
|
||||
std::string preset_mode{};
|
||||
|
||||
FanCall turn_on();
|
||||
FanCall turn_off();
|
||||
@@ -127,15 +130,8 @@ class Fan : public EntityBase {
|
||||
/// Set the restore mode of this fan.
|
||||
void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
|
||||
|
||||
/// Get the current preset mode (returns pointer to string stored in traits, or nullptr if not set)
|
||||
const char *get_preset_mode() const { return this->preset_mode_; }
|
||||
|
||||
/// Check if a preset mode is currently active
|
||||
bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
|
||||
|
||||
protected:
|
||||
friend FanCall;
|
||||
friend struct FanRestoreState;
|
||||
|
||||
virtual void control(const FanCall &call) = 0;
|
||||
|
||||
@@ -144,19 +140,9 @@ class Fan : public EntityBase {
|
||||
|
||||
void dump_traits_(const char *tag, const char *prefix);
|
||||
|
||||
/// Set the preset mode (finds and stores pointer from traits). Returns true if changed.
|
||||
bool set_preset_mode_(const char *preset_mode);
|
||||
/// Set the preset mode (finds and stores pointer from traits). Returns true if changed.
|
||||
bool set_preset_mode_(const std::string &preset_mode);
|
||||
/// Clear the preset mode
|
||||
void clear_preset_mode_();
|
||||
/// Find and return the matching preset mode pointer from traits, or nullptr if not found.
|
||||
const char *find_preset_mode_(const char *preset_mode);
|
||||
|
||||
CallbackManager<void()> state_callback_{};
|
||||
ESPPreferenceObject rtc_;
|
||||
FanRestoreMode restore_mode_;
|
||||
const char *preset_mode_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace fan
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <initializer_list>
|
||||
|
||||
namespace esphome {
|
||||
|
||||
#ifdef USE_API
|
||||
namespace api {
|
||||
class APIConnection;
|
||||
} // namespace api
|
||||
#endif
|
||||
|
||||
namespace fan {
|
||||
|
||||
class FanTraits {
|
||||
@@ -31,38 +36,27 @@ class FanTraits {
|
||||
/// Set whether this fan supports changing direction
|
||||
void set_direction(bool direction) { this->direction_ = direction; }
|
||||
/// Return the preset modes supported by the fan.
|
||||
const std::vector<const char *> &supported_preset_modes() const { return this->preset_modes_; }
|
||||
/// Set the preset modes supported by the fan (from initializer list).
|
||||
void set_supported_preset_modes(std::initializer_list<const char *> preset_modes) {
|
||||
this->preset_modes_ = preset_modes;
|
||||
}
|
||||
/// Set the preset modes supported by the fan (from vector).
|
||||
void set_supported_preset_modes(const std::vector<const char *> &preset_modes) { this->preset_modes_ = preset_modes; }
|
||||
|
||||
// Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
|
||||
void set_supported_preset_modes(const std::vector<std::string> &preset_modes) = delete;
|
||||
void set_supported_preset_modes(std::initializer_list<std::string> preset_modes) = delete;
|
||||
|
||||
std::set<std::string> supported_preset_modes() const { return this->preset_modes_; }
|
||||
/// Set the preset modes supported by the fan.
|
||||
void set_supported_preset_modes(const std::set<std::string> &preset_modes) { this->preset_modes_ = preset_modes; }
|
||||
/// Return if preset modes are supported
|
||||
bool supports_preset_modes() const { return !this->preset_modes_.empty(); }
|
||||
/// Find and return the matching preset mode pointer from supported modes, or nullptr if not found.
|
||||
const char *find_preset_mode(const char *preset_mode) const {
|
||||
if (preset_mode == nullptr)
|
||||
return nullptr;
|
||||
for (const char *mode : this->preset_modes_) {
|
||||
if (strcmp(mode, preset_mode) == 0) {
|
||||
return mode; // Return pointer from traits
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
#ifdef USE_API
|
||||
// The API connection is a friend class to access internal methods
|
||||
friend class api::APIConnection;
|
||||
// This method returns a reference to the internal preset modes set.
|
||||
// It is used by the API to avoid copying data when encoding messages.
|
||||
// Warning: Do not use this method outside of the API connection code.
|
||||
// It returns a reference to internal data that can be invalidated.
|
||||
const std::set<std::string> &supported_preset_modes_for_api_() const { return this->preset_modes_; }
|
||||
#endif
|
||||
bool oscillation_{false};
|
||||
bool speed_{false};
|
||||
bool direction_{false};
|
||||
int speed_count_{};
|
||||
std::vector<const char *> preset_modes_{};
|
||||
std::set<std::string> preset_modes_{};
|
||||
};
|
||||
|
||||
} // namespace fan
|
||||
|
||||
@@ -57,7 +57,7 @@ void HBridgeFan::control(const fan::FanCall &call) {
|
||||
this->oscillating = *call.get_oscillating();
|
||||
if (call.get_direction().has_value())
|
||||
this->direction = *call.get_direction();
|
||||
this->set_preset_mode_(call.get_preset_mode());
|
||||
this->preset_mode = call.get_preset_mode();
|
||||
|
||||
this->write_state_();
|
||||
this->publish_state();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
@@ -20,7 +22,7 @@ class HBridgeFan : public Component, public fan::Fan {
|
||||
void set_pin_a(output::FloatOutput *pin_a) { pin_a_ = pin_a; }
|
||||
void set_pin_b(output::FloatOutput *pin_b) { pin_b_ = pin_b; }
|
||||
void set_enable_pin(output::FloatOutput *enable) { enable_ = enable; }
|
||||
void set_preset_modes(std::initializer_list<const char *> presets) { preset_modes_ = presets; }
|
||||
void set_preset_modes(const std::set<std::string> &presets) { preset_modes_ = presets; }
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
@@ -36,7 +38,7 @@ class HBridgeFan : public Component, public fan::Fan {
|
||||
int speed_count_{};
|
||||
DecayMode decay_mode_{DECAY_MODE_SLOW};
|
||||
fan::FanTraits traits_;
|
||||
std::vector<const char *> preset_modes_{};
|
||||
std::set<std::string> preset_modes_{};
|
||||
|
||||
void control(const fan::FanCall &call) override;
|
||||
void write_state_();
|
||||
|
||||
@@ -671,18 +671,33 @@ async def write_image(config, all_frames=False):
|
||||
resize = config.get(CONF_RESIZE)
|
||||
if is_svg_file(path):
|
||||
# Local import so use of non-SVG files needn't require cairosvg installed
|
||||
from pyexpat import ExpatError
|
||||
from xml.etree.ElementTree import ParseError
|
||||
|
||||
from cairosvg import svg2png
|
||||
from cairosvg.helpers import PointError
|
||||
|
||||
if not resize:
|
||||
resize = (None, None)
|
||||
with open(path, "rb") as file:
|
||||
image = svg2png(
|
||||
file_obj=file,
|
||||
output_width=resize[0],
|
||||
output_height=resize[1],
|
||||
)
|
||||
image = Image.open(io.BytesIO(image))
|
||||
width, height = image.size
|
||||
try:
|
||||
with open(path, "rb") as file:
|
||||
image = svg2png(
|
||||
file_obj=file,
|
||||
output_width=resize[0],
|
||||
output_height=resize[1],
|
||||
)
|
||||
image = Image.open(io.BytesIO(image))
|
||||
width, height = image.size
|
||||
except (
|
||||
ValueError,
|
||||
ParseError,
|
||||
IndexError,
|
||||
ExpatError,
|
||||
AttributeError,
|
||||
TypeError,
|
||||
PointError,
|
||||
) as e:
|
||||
raise core.EsphomeError(f"Could not load SVG image {path}: {e}") from e
|
||||
else:
|
||||
image = Image.open(path)
|
||||
width, height = image.size
|
||||
|
||||
@@ -58,7 +58,7 @@ from .types import (
|
||||
FontEngine,
|
||||
IdleTrigger,
|
||||
ObjUpdateAction,
|
||||
PauseTrigger,
|
||||
PlainTrigger,
|
||||
lv_font_t,
|
||||
lv_group_t,
|
||||
lv_style_t,
|
||||
@@ -151,6 +151,13 @@ for w_type in WIDGET_TYPES.values():
|
||||
create_modify_schema(w_type),
|
||||
)(update_to_code)
|
||||
|
||||
SIMPLE_TRIGGERS = (
|
||||
df.CONF_ON_PAUSE,
|
||||
df.CONF_ON_RESUME,
|
||||
df.CONF_ON_DRAW_START,
|
||||
df.CONF_ON_DRAW_END,
|
||||
)
|
||||
|
||||
|
||||
def as_macro(macro, value):
|
||||
if value is None:
|
||||
@@ -244,9 +251,9 @@ def final_validation(configs):
|
||||
for w in refreshed_widgets:
|
||||
path = global_config.get_path_for_id(w)
|
||||
widget_conf = global_config.get_config_for_path(path[:-1])
|
||||
if not any(isinstance(v, Lambda) for v in widget_conf.values()):
|
||||
if not any(isinstance(v, (Lambda, dict)) for v in widget_conf.values()):
|
||||
raise cv.Invalid(
|
||||
f"Widget '{w}' does not have any templated properties to refresh",
|
||||
f"Widget '{w}' does not have any dynamic properties to refresh",
|
||||
)
|
||||
|
||||
|
||||
@@ -366,16 +373,16 @@ async def to_code(configs):
|
||||
conf[CONF_TRIGGER_ID], lv_component, templ
|
||||
)
|
||||
await build_automation(idle_trigger, [], conf)
|
||||
for conf in config.get(df.CONF_ON_PAUSE, ()):
|
||||
pause_trigger = cg.new_Pvariable(
|
||||
conf[CONF_TRIGGER_ID], lv_component, True
|
||||
)
|
||||
await build_automation(pause_trigger, [], conf)
|
||||
for conf in config.get(df.CONF_ON_RESUME, ()):
|
||||
resume_trigger = cg.new_Pvariable(
|
||||
conf[CONF_TRIGGER_ID], lv_component, False
|
||||
)
|
||||
await build_automation(resume_trigger, [], conf)
|
||||
for trigger_name in SIMPLE_TRIGGERS:
|
||||
if conf := config.get(trigger_name):
|
||||
trigger_var = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
||||
await build_automation(trigger_var, [], conf)
|
||||
cg.add(
|
||||
getattr(
|
||||
lv_component,
|
||||
f"set_{trigger_name.removeprefix('on_')}_trigger",
|
||||
)(trigger_var)
|
||||
)
|
||||
await add_on_boot_triggers(config.get(CONF_ON_BOOT, ()))
|
||||
|
||||
# This must be done after all widgets are created
|
||||
@@ -443,16 +450,15 @@ LVGL_SCHEMA = cv.All(
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(df.CONF_ON_PAUSE): validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PauseTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(df.CONF_ON_RESUME): validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PauseTrigger),
|
||||
}
|
||||
),
|
||||
**{
|
||||
cv.Optional(x): validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PlainTrigger),
|
||||
},
|
||||
single=True,
|
||||
)
|
||||
for x in SIMPLE_TRIGGERS
|
||||
},
|
||||
cv.Exclusive(df.CONF_WIDGETS, CONF_PAGES): cv.ensure_list(
|
||||
WIDGET_SCHEMA
|
||||
),
|
||||
|
||||
@@ -400,7 +400,8 @@ async def obj_refresh_to_code(config, action_id, template_arg, args):
|
||||
# must pass all widget-specific options here, even if not templated, but only do so if at least one is
|
||||
# templated. First filter out common style properties.
|
||||
config = {k: v for k, v in widget.config.items() if k not in ALL_STYLES}
|
||||
if any(isinstance(v, Lambda) for v in config.values()):
|
||||
# Check if v is a Lambda or a dict, implying it is dynamic
|
||||
if any(isinstance(v, (Lambda, dict)) for v in config.values()):
|
||||
await widget.type.to_code(widget, config)
|
||||
if (
|
||||
widget.type.w_type.value_property is not None
|
||||
|
||||
@@ -31,7 +31,7 @@ async def to_code(config):
|
||||
lvgl_static.add_event_cb(
|
||||
widget.obj,
|
||||
await pressed_ctx.get_lambda(),
|
||||
LV_EVENT.PRESSING,
|
||||
LV_EVENT.PRESSED,
|
||||
LV_EVENT.RELEASED,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -483,6 +483,8 @@ CONF_MSGBOXES = "msgboxes"
|
||||
CONF_OBJ = "obj"
|
||||
CONF_ONE_CHECKED = "one_checked"
|
||||
CONF_ONE_LINE = "one_line"
|
||||
CONF_ON_DRAW_START = "on_draw_start"
|
||||
CONF_ON_DRAW_END = "on_draw_end"
|
||||
CONF_ON_PAUSE = "on_pause"
|
||||
CONF_ON_RESUME = "on_resume"
|
||||
CONF_ON_SELECT = "on_select"
|
||||
|
||||
@@ -82,6 +82,18 @@ static void rounder_cb(lv_disp_drv_t *disp_drv, lv_area_t *area) {
|
||||
area->y2 = (area->y2 + draw_rounding) / draw_rounding * draw_rounding - 1;
|
||||
}
|
||||
|
||||
void LvglComponent::monitor_cb(lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px) {
|
||||
ESP_LOGVV(TAG, "Draw end: %" PRIu32 " pixels in %" PRIu32 " ms", px, time);
|
||||
auto *comp = static_cast<LvglComponent *>(disp_drv->user_data);
|
||||
comp->draw_end_();
|
||||
}
|
||||
|
||||
void LvglComponent::render_start_cb(lv_disp_drv_t *disp_drv) {
|
||||
ESP_LOGVV(TAG, "Draw start");
|
||||
auto *comp = static_cast<LvglComponent *>(disp_drv->user_data);
|
||||
comp->draw_start_();
|
||||
}
|
||||
|
||||
lv_event_code_t lv_api_event; // NOLINT
|
||||
lv_event_code_t lv_update_event; // NOLINT
|
||||
void LvglComponent::dump_config() {
|
||||
@@ -101,7 +113,10 @@ void LvglComponent::set_paused(bool paused, bool show_snow) {
|
||||
lv_disp_trig_activity(this->disp_); // resets the inactivity time
|
||||
lv_obj_invalidate(lv_scr_act());
|
||||
}
|
||||
this->pause_callbacks_.call(paused);
|
||||
if (paused && this->pause_callback_ != nullptr)
|
||||
this->pause_callback_->trigger();
|
||||
if (!paused && this->resume_callback_ != nullptr)
|
||||
this->resume_callback_->trigger();
|
||||
}
|
||||
|
||||
void LvglComponent::esphome_lvgl_init() {
|
||||
@@ -225,13 +240,6 @@ IdleTrigger::IdleTrigger(LvglComponent *parent, TemplatableValue<uint32_t> timeo
|
||||
});
|
||||
}
|
||||
|
||||
PauseTrigger::PauseTrigger(LvglComponent *parent, TemplatableValue<bool> paused) : paused_(std::move(paused)) {
|
||||
parent->add_on_pause_callback([this](bool pausing) {
|
||||
if (this->paused_.value() == pausing)
|
||||
this->trigger();
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef USE_LVGL_TOUCHSCREEN
|
||||
LVTouchListener::LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time, LvglComponent *parent) {
|
||||
this->set_parent(parent);
|
||||
@@ -474,6 +482,12 @@ void LvglComponent::setup() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (this->draw_start_callback_ != nullptr) {
|
||||
this->disp_drv_.render_start_cb = render_start_cb;
|
||||
}
|
||||
if (this->draw_end_callback_ != nullptr) {
|
||||
this->disp_drv_.monitor_cb = monitor_cb;
|
||||
}
|
||||
#if LV_USE_LOG
|
||||
lv_log_register_print_cb([](const char *buf) {
|
||||
auto next = strchr(buf, ')');
|
||||
@@ -502,8 +516,9 @@ void LvglComponent::loop() {
|
||||
if (this->paused_) {
|
||||
if (this->show_snow_)
|
||||
this->write_random_();
|
||||
} else {
|
||||
lv_timer_handler_run_in_period(5);
|
||||
}
|
||||
lv_timer_handler_run_in_period(5);
|
||||
}
|
||||
|
||||
#ifdef USE_LVGL_ANIMIMG
|
||||
|
||||
@@ -171,7 +171,9 @@ class LvglComponent : public PollingComponent {
|
||||
void add_on_idle_callback(std::function<void(uint32_t)> &&callback) {
|
||||
this->idle_callbacks_.add(std::move(callback));
|
||||
}
|
||||
void add_on_pause_callback(std::function<void(bool)> &&callback) { this->pause_callbacks_.add(std::move(callback)); }
|
||||
|
||||
static void monitor_cb(lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px);
|
||||
static void render_start_cb(lv_disp_drv_t *disp_drv);
|
||||
void dump_config() override;
|
||||
bool is_idle(uint32_t idle_ms) { return lv_disp_get_inactive_time(this->disp_) > idle_ms; }
|
||||
lv_disp_t *get_disp() { return this->disp_; }
|
||||
@@ -213,12 +215,20 @@ class LvglComponent : public PollingComponent {
|
||||
size_t draw_rounding{2};
|
||||
|
||||
display::DisplayRotation rotation{display::DISPLAY_ROTATION_0_DEGREES};
|
||||
void set_pause_trigger(Trigger<> *trigger) { this->pause_callback_ = trigger; }
|
||||
void set_resume_trigger(Trigger<> *trigger) { this->resume_callback_ = trigger; }
|
||||
void set_draw_start_trigger(Trigger<> *trigger) { this->draw_start_callback_ = trigger; }
|
||||
void set_draw_end_trigger(Trigger<> *trigger) { this->draw_end_callback_ = trigger; }
|
||||
|
||||
protected:
|
||||
// these functions are never called unless the callbacks are non-null since the
|
||||
// LVGL callbacks that call them are not set unless the start/end callbacks are non-null
|
||||
void draw_start_() const { this->draw_start_callback_->trigger(); }
|
||||
void draw_end_() const { this->draw_end_callback_->trigger(); }
|
||||
|
||||
void write_random_();
|
||||
void draw_buffer_(const lv_area_t *area, lv_color_t *ptr);
|
||||
void flush_cb_(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p);
|
||||
|
||||
std::vector<display::Display *> displays_{};
|
||||
size_t buffer_frac_{1};
|
||||
bool full_refresh_{};
|
||||
@@ -235,7 +245,10 @@ class LvglComponent : public PollingComponent {
|
||||
std::map<lv_group_t *, lv_obj_t *> focus_marks_{};
|
||||
|
||||
CallbackManager<void(uint32_t)> idle_callbacks_{};
|
||||
CallbackManager<void(bool)> pause_callbacks_{};
|
||||
Trigger<> *pause_callback_{};
|
||||
Trigger<> *resume_callback_{};
|
||||
Trigger<> *draw_start_callback_{};
|
||||
Trigger<> *draw_end_callback_{};
|
||||
lv_color_t *rotate_buf_{};
|
||||
};
|
||||
|
||||
@@ -248,14 +261,6 @@ class IdleTrigger : public Trigger<> {
|
||||
bool is_idle_{};
|
||||
};
|
||||
|
||||
class PauseTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit PauseTrigger(LvglComponent *parent, TemplatableValue<bool> paused);
|
||||
|
||||
protected:
|
||||
TemplatableValue<bool> paused_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class LvglAction : public Action<Ts...>, public Parented<LvglComponent> {
|
||||
public:
|
||||
explicit LvglAction(std::function<void(LvglComponent *)> &&lamb) : action_(std::move(lamb)) {}
|
||||
|
||||
@@ -3,6 +3,7 @@ import sys
|
||||
from esphome import automation, codegen as cg
|
||||
from esphome.const import CONF_MAX_VALUE, CONF_MIN_VALUE, CONF_TEXT, CONF_VALUE
|
||||
from esphome.cpp_generator import MockObj, MockObjClass
|
||||
from esphome.cpp_types import esphome_ns
|
||||
|
||||
from .defines import lvgl_ns
|
||||
from .lvcode import lv_expr
|
||||
@@ -42,8 +43,11 @@ lv_event_code_t = cg.global_ns.enum("lv_event_code_t")
|
||||
lv_indev_type_t = cg.global_ns.enum("lv_indev_type_t")
|
||||
lv_key_t = cg.global_ns.enum("lv_key_t")
|
||||
FontEngine = lvgl_ns.class_("FontEngine")
|
||||
PlainTrigger = esphome_ns.class_("Trigger<>", automation.Trigger.template())
|
||||
DrawEndTrigger = esphome_ns.class_(
|
||||
"Trigger<uint32_t, uint32_t>", automation.Trigger.template(cg.uint32, cg.uint32)
|
||||
)
|
||||
IdleTrigger = lvgl_ns.class_("IdleTrigger", automation.Trigger.template())
|
||||
PauseTrigger = lvgl_ns.class_("PauseTrigger", automation.Trigger.template())
|
||||
ObjUpdateAction = lvgl_ns.class_("ObjUpdateAction", automation.Action)
|
||||
LvglCondition = lvgl_ns.class_("LvglCondition", automation.Condition)
|
||||
LvglAction = lvgl_ns.class_("LvglAction", automation.Action)
|
||||
|
||||
@@ -12,241 +12,256 @@ CODEOWNERS = ["@bdm310"]
|
||||
|
||||
STATE_ARG = "state"
|
||||
|
||||
SDL_KEYMAP = {
|
||||
"SDLK_UNKNOWN": 0,
|
||||
"SDLK_FIRST": 0,
|
||||
"SDLK_BACKSPACE": 8,
|
||||
"SDLK_TAB": 9,
|
||||
"SDLK_CLEAR": 12,
|
||||
"SDLK_RETURN": 13,
|
||||
"SDLK_PAUSE": 19,
|
||||
"SDLK_ESCAPE": 27,
|
||||
"SDLK_SPACE": 32,
|
||||
"SDLK_EXCLAIM": 33,
|
||||
"SDLK_QUOTEDBL": 34,
|
||||
"SDLK_HASH": 35,
|
||||
"SDLK_DOLLAR": 36,
|
||||
"SDLK_AMPERSAND": 38,
|
||||
"SDLK_QUOTE": 39,
|
||||
"SDLK_LEFTPAREN": 40,
|
||||
"SDLK_RIGHTPAREN": 41,
|
||||
"SDLK_ASTERISK": 42,
|
||||
"SDLK_PLUS": 43,
|
||||
"SDLK_COMMA": 44,
|
||||
"SDLK_MINUS": 45,
|
||||
"SDLK_PERIOD": 46,
|
||||
"SDLK_SLASH": 47,
|
||||
"SDLK_0": 48,
|
||||
"SDLK_1": 49,
|
||||
"SDLK_2": 50,
|
||||
"SDLK_3": 51,
|
||||
"SDLK_4": 52,
|
||||
"SDLK_5": 53,
|
||||
"SDLK_6": 54,
|
||||
"SDLK_7": 55,
|
||||
"SDLK_8": 56,
|
||||
"SDLK_9": 57,
|
||||
"SDLK_COLON": 58,
|
||||
"SDLK_SEMICOLON": 59,
|
||||
"SDLK_LESS": 60,
|
||||
"SDLK_EQUALS": 61,
|
||||
"SDLK_GREATER": 62,
|
||||
"SDLK_QUESTION": 63,
|
||||
"SDLK_AT": 64,
|
||||
"SDLK_LEFTBRACKET": 91,
|
||||
"SDLK_BACKSLASH": 92,
|
||||
"SDLK_RIGHTBRACKET": 93,
|
||||
"SDLK_CARET": 94,
|
||||
"SDLK_UNDERSCORE": 95,
|
||||
"SDLK_BACKQUOTE": 96,
|
||||
"SDLK_a": 97,
|
||||
"SDLK_b": 98,
|
||||
"SDLK_c": 99,
|
||||
"SDLK_d": 100,
|
||||
"SDLK_e": 101,
|
||||
"SDLK_f": 102,
|
||||
"SDLK_g": 103,
|
||||
"SDLK_h": 104,
|
||||
"SDLK_i": 105,
|
||||
"SDLK_j": 106,
|
||||
"SDLK_k": 107,
|
||||
"SDLK_l": 108,
|
||||
"SDLK_m": 109,
|
||||
"SDLK_n": 110,
|
||||
"SDLK_o": 111,
|
||||
"SDLK_p": 112,
|
||||
"SDLK_q": 113,
|
||||
"SDLK_r": 114,
|
||||
"SDLK_s": 115,
|
||||
"SDLK_t": 116,
|
||||
"SDLK_u": 117,
|
||||
"SDLK_v": 118,
|
||||
"SDLK_w": 119,
|
||||
"SDLK_x": 120,
|
||||
"SDLK_y": 121,
|
||||
"SDLK_z": 122,
|
||||
"SDLK_DELETE": 127,
|
||||
"SDLK_WORLD_0": 160,
|
||||
"SDLK_WORLD_1": 161,
|
||||
"SDLK_WORLD_2": 162,
|
||||
"SDLK_WORLD_3": 163,
|
||||
"SDLK_WORLD_4": 164,
|
||||
"SDLK_WORLD_5": 165,
|
||||
"SDLK_WORLD_6": 166,
|
||||
"SDLK_WORLD_7": 167,
|
||||
"SDLK_WORLD_8": 168,
|
||||
"SDLK_WORLD_9": 169,
|
||||
"SDLK_WORLD_10": 170,
|
||||
"SDLK_WORLD_11": 171,
|
||||
"SDLK_WORLD_12": 172,
|
||||
"SDLK_WORLD_13": 173,
|
||||
"SDLK_WORLD_14": 174,
|
||||
"SDLK_WORLD_15": 175,
|
||||
"SDLK_WORLD_16": 176,
|
||||
"SDLK_WORLD_17": 177,
|
||||
"SDLK_WORLD_18": 178,
|
||||
"SDLK_WORLD_19": 179,
|
||||
"SDLK_WORLD_20": 180,
|
||||
"SDLK_WORLD_21": 181,
|
||||
"SDLK_WORLD_22": 182,
|
||||
"SDLK_WORLD_23": 183,
|
||||
"SDLK_WORLD_24": 184,
|
||||
"SDLK_WORLD_25": 185,
|
||||
"SDLK_WORLD_26": 186,
|
||||
"SDLK_WORLD_27": 187,
|
||||
"SDLK_WORLD_28": 188,
|
||||
"SDLK_WORLD_29": 189,
|
||||
"SDLK_WORLD_30": 190,
|
||||
"SDLK_WORLD_31": 191,
|
||||
"SDLK_WORLD_32": 192,
|
||||
"SDLK_WORLD_33": 193,
|
||||
"SDLK_WORLD_34": 194,
|
||||
"SDLK_WORLD_35": 195,
|
||||
"SDLK_WORLD_36": 196,
|
||||
"SDLK_WORLD_37": 197,
|
||||
"SDLK_WORLD_38": 198,
|
||||
"SDLK_WORLD_39": 199,
|
||||
"SDLK_WORLD_40": 200,
|
||||
"SDLK_WORLD_41": 201,
|
||||
"SDLK_WORLD_42": 202,
|
||||
"SDLK_WORLD_43": 203,
|
||||
"SDLK_WORLD_44": 204,
|
||||
"SDLK_WORLD_45": 205,
|
||||
"SDLK_WORLD_46": 206,
|
||||
"SDLK_WORLD_47": 207,
|
||||
"SDLK_WORLD_48": 208,
|
||||
"SDLK_WORLD_49": 209,
|
||||
"SDLK_WORLD_50": 210,
|
||||
"SDLK_WORLD_51": 211,
|
||||
"SDLK_WORLD_52": 212,
|
||||
"SDLK_WORLD_53": 213,
|
||||
"SDLK_WORLD_54": 214,
|
||||
"SDLK_WORLD_55": 215,
|
||||
"SDLK_WORLD_56": 216,
|
||||
"SDLK_WORLD_57": 217,
|
||||
"SDLK_WORLD_58": 218,
|
||||
"SDLK_WORLD_59": 219,
|
||||
"SDLK_WORLD_60": 220,
|
||||
"SDLK_WORLD_61": 221,
|
||||
"SDLK_WORLD_62": 222,
|
||||
"SDLK_WORLD_63": 223,
|
||||
"SDLK_WORLD_64": 224,
|
||||
"SDLK_WORLD_65": 225,
|
||||
"SDLK_WORLD_66": 226,
|
||||
"SDLK_WORLD_67": 227,
|
||||
"SDLK_WORLD_68": 228,
|
||||
"SDLK_WORLD_69": 229,
|
||||
"SDLK_WORLD_70": 230,
|
||||
"SDLK_WORLD_71": 231,
|
||||
"SDLK_WORLD_72": 232,
|
||||
"SDLK_WORLD_73": 233,
|
||||
"SDLK_WORLD_74": 234,
|
||||
"SDLK_WORLD_75": 235,
|
||||
"SDLK_WORLD_76": 236,
|
||||
"SDLK_WORLD_77": 237,
|
||||
"SDLK_WORLD_78": 238,
|
||||
"SDLK_WORLD_79": 239,
|
||||
"SDLK_WORLD_80": 240,
|
||||
"SDLK_WORLD_81": 241,
|
||||
"SDLK_WORLD_82": 242,
|
||||
"SDLK_WORLD_83": 243,
|
||||
"SDLK_WORLD_84": 244,
|
||||
"SDLK_WORLD_85": 245,
|
||||
"SDLK_WORLD_86": 246,
|
||||
"SDLK_WORLD_87": 247,
|
||||
"SDLK_WORLD_88": 248,
|
||||
"SDLK_WORLD_89": 249,
|
||||
"SDLK_WORLD_90": 250,
|
||||
"SDLK_WORLD_91": 251,
|
||||
"SDLK_WORLD_92": 252,
|
||||
"SDLK_WORLD_93": 253,
|
||||
"SDLK_WORLD_94": 254,
|
||||
"SDLK_WORLD_95": 255,
|
||||
"SDLK_KP0": 256,
|
||||
"SDLK_KP1": 257,
|
||||
"SDLK_KP2": 258,
|
||||
"SDLK_KP3": 259,
|
||||
"SDLK_KP4": 260,
|
||||
"SDLK_KP5": 261,
|
||||
"SDLK_KP6": 262,
|
||||
"SDLK_KP7": 263,
|
||||
"SDLK_KP8": 264,
|
||||
"SDLK_KP9": 265,
|
||||
"SDLK_KP_PERIOD": 266,
|
||||
"SDLK_KP_DIVIDE": 267,
|
||||
"SDLK_KP_MULTIPLY": 268,
|
||||
"SDLK_KP_MINUS": 269,
|
||||
"SDLK_KP_PLUS": 270,
|
||||
"SDLK_KP_ENTER": 271,
|
||||
"SDLK_KP_EQUALS": 272,
|
||||
"SDLK_UP": 273,
|
||||
"SDLK_DOWN": 274,
|
||||
"SDLK_RIGHT": 275,
|
||||
"SDLK_LEFT": 276,
|
||||
"SDLK_INSERT": 277,
|
||||
"SDLK_HOME": 278,
|
||||
"SDLK_END": 279,
|
||||
"SDLK_PAGEUP": 280,
|
||||
"SDLK_PAGEDOWN": 281,
|
||||
"SDLK_F1": 282,
|
||||
"SDLK_F2": 283,
|
||||
"SDLK_F3": 284,
|
||||
"SDLK_F4": 285,
|
||||
"SDLK_F5": 286,
|
||||
"SDLK_F6": 287,
|
||||
"SDLK_F7": 288,
|
||||
"SDLK_F8": 289,
|
||||
"SDLK_F9": 290,
|
||||
"SDLK_F10": 291,
|
||||
"SDLK_F11": 292,
|
||||
"SDLK_F12": 293,
|
||||
"SDLK_F13": 294,
|
||||
"SDLK_F14": 295,
|
||||
"SDLK_F15": 296,
|
||||
"SDLK_NUMLOCK": 300,
|
||||
"SDLK_CAPSLOCK": 301,
|
||||
"SDLK_SCROLLOCK": 302,
|
||||
"SDLK_RSHIFT": 303,
|
||||
"SDLK_LSHIFT": 304,
|
||||
"SDLK_RCTRL": 305,
|
||||
"SDLK_LCTRL": 306,
|
||||
"SDLK_RALT": 307,
|
||||
"SDLK_LALT": 308,
|
||||
"SDLK_RMETA": 309,
|
||||
"SDLK_LMETA": 310,
|
||||
"SDLK_LSUPER": 311,
|
||||
"SDLK_RSUPER": 312,
|
||||
"SDLK_MODE": 313,
|
||||
"SDLK_COMPOSE": 314,
|
||||
"SDLK_HELP": 315,
|
||||
"SDLK_PRINT": 316,
|
||||
"SDLK_SYSREQ": 317,
|
||||
"SDLK_BREAK": 318,
|
||||
"SDLK_MENU": 319,
|
||||
"SDLK_POWER": 320,
|
||||
"SDLK_EURO": 321,
|
||||
"SDLK_UNDO": 322,
|
||||
}
|
||||
SDL_KeyCode = cg.global_ns.enum("SDL_KeyCode")
|
||||
|
||||
SDL_KEYS = (
|
||||
"SDLK_UNKNOWN",
|
||||
"SDLK_RETURN",
|
||||
"SDLK_ESCAPE",
|
||||
"SDLK_BACKSPACE",
|
||||
"SDLK_TAB",
|
||||
"SDLK_SPACE",
|
||||
"SDLK_EXCLAIM",
|
||||
"SDLK_QUOTEDBL",
|
||||
"SDLK_HASH",
|
||||
"SDLK_PERCENT",
|
||||
"SDLK_DOLLAR",
|
||||
"SDLK_AMPERSAND",
|
||||
"SDLK_QUOTE",
|
||||
"SDLK_LEFTPAREN",
|
||||
"SDLK_RIGHTPAREN",
|
||||
"SDLK_ASTERISK",
|
||||
"SDLK_PLUS",
|
||||
"SDLK_COMMA",
|
||||
"SDLK_MINUS",
|
||||
"SDLK_PERIOD",
|
||||
"SDLK_SLASH",
|
||||
"SDLK_0",
|
||||
"SDLK_1",
|
||||
"SDLK_2",
|
||||
"SDLK_3",
|
||||
"SDLK_4",
|
||||
"SDLK_5",
|
||||
"SDLK_6",
|
||||
"SDLK_7",
|
||||
"SDLK_8",
|
||||
"SDLK_9",
|
||||
"SDLK_COLON",
|
||||
"SDLK_SEMICOLON",
|
||||
"SDLK_LESS",
|
||||
"SDLK_EQUALS",
|
||||
"SDLK_GREATER",
|
||||
"SDLK_QUESTION",
|
||||
"SDLK_AT",
|
||||
"SDLK_LEFTBRACKET",
|
||||
"SDLK_BACKSLASH",
|
||||
"SDLK_RIGHTBRACKET",
|
||||
"SDLK_CARET",
|
||||
"SDLK_UNDERSCORE",
|
||||
"SDLK_BACKQUOTE",
|
||||
"SDLK_a",
|
||||
"SDLK_b",
|
||||
"SDLK_c",
|
||||
"SDLK_d",
|
||||
"SDLK_e",
|
||||
"SDLK_f",
|
||||
"SDLK_g",
|
||||
"SDLK_h",
|
||||
"SDLK_i",
|
||||
"SDLK_j",
|
||||
"SDLK_k",
|
||||
"SDLK_l",
|
||||
"SDLK_m",
|
||||
"SDLK_n",
|
||||
"SDLK_o",
|
||||
"SDLK_p",
|
||||
"SDLK_q",
|
||||
"SDLK_r",
|
||||
"SDLK_s",
|
||||
"SDLK_t",
|
||||
"SDLK_u",
|
||||
"SDLK_v",
|
||||
"SDLK_w",
|
||||
"SDLK_x",
|
||||
"SDLK_y",
|
||||
"SDLK_z",
|
||||
"SDLK_CAPSLOCK",
|
||||
"SDLK_F1",
|
||||
"SDLK_F2",
|
||||
"SDLK_F3",
|
||||
"SDLK_F4",
|
||||
"SDLK_F5",
|
||||
"SDLK_F6",
|
||||
"SDLK_F7",
|
||||
"SDLK_F8",
|
||||
"SDLK_F9",
|
||||
"SDLK_F10",
|
||||
"SDLK_F11",
|
||||
"SDLK_F12",
|
||||
"SDLK_PRINTSCREEN",
|
||||
"SDLK_SCROLLLOCK",
|
||||
"SDLK_PAUSE",
|
||||
"SDLK_INSERT",
|
||||
"SDLK_HOME",
|
||||
"SDLK_PAGEUP",
|
||||
"SDLK_DELETE",
|
||||
"SDLK_END",
|
||||
"SDLK_PAGEDOWN",
|
||||
"SDLK_RIGHT",
|
||||
"SDLK_LEFT",
|
||||
"SDLK_DOWN",
|
||||
"SDLK_UP",
|
||||
"SDLK_NUMLOCKCLEAR",
|
||||
"SDLK_KP_DIVIDE",
|
||||
"SDLK_KP_MULTIPLY",
|
||||
"SDLK_KP_MINUS",
|
||||
"SDLK_KP_PLUS",
|
||||
"SDLK_KP_ENTER",
|
||||
"SDLK_KP_1",
|
||||
"SDLK_KP_2",
|
||||
"SDLK_KP_3",
|
||||
"SDLK_KP_4",
|
||||
"SDLK_KP_5",
|
||||
"SDLK_KP_6",
|
||||
"SDLK_KP_7",
|
||||
"SDLK_KP_8",
|
||||
"SDLK_KP_9",
|
||||
"SDLK_KP_0",
|
||||
"SDLK_KP_PERIOD",
|
||||
"SDLK_APPLICATION",
|
||||
"SDLK_POWER",
|
||||
"SDLK_KP_EQUALS",
|
||||
"SDLK_F13",
|
||||
"SDLK_F14",
|
||||
"SDLK_F15",
|
||||
"SDLK_F16",
|
||||
"SDLK_F17",
|
||||
"SDLK_F18",
|
||||
"SDLK_F19",
|
||||
"SDLK_F20",
|
||||
"SDLK_F21",
|
||||
"SDLK_F22",
|
||||
"SDLK_F23",
|
||||
"SDLK_F24",
|
||||
"SDLK_EXECUTE",
|
||||
"SDLK_HELP",
|
||||
"SDLK_MENU",
|
||||
"SDLK_SELECT",
|
||||
"SDLK_STOP",
|
||||
"SDLK_AGAIN",
|
||||
"SDLK_UNDO",
|
||||
"SDLK_CUT",
|
||||
"SDLK_COPY",
|
||||
"SDLK_PASTE",
|
||||
"SDLK_FIND",
|
||||
"SDLK_MUTE",
|
||||
"SDLK_VOLUMEUP",
|
||||
"SDLK_VOLUMEDOWN",
|
||||
"SDLK_KP_COMMA",
|
||||
"SDLK_KP_EQUALSAS400",
|
||||
"SDLK_ALTERASE",
|
||||
"SDLK_SYSREQ",
|
||||
"SDLK_CANCEL",
|
||||
"SDLK_CLEAR",
|
||||
"SDLK_PRIOR",
|
||||
"SDLK_RETURN2",
|
||||
"SDLK_SEPARATOR",
|
||||
"SDLK_OUT",
|
||||
"SDLK_OPER",
|
||||
"SDLK_CLEARAGAIN",
|
||||
"SDLK_CRSEL",
|
||||
"SDLK_EXSEL",
|
||||
"SDLK_KP_00",
|
||||
"SDLK_KP_000",
|
||||
"SDLK_THOUSANDSSEPARATOR",
|
||||
"SDLK_DECIMALSEPARATOR",
|
||||
"SDLK_CURRENCYUNIT",
|
||||
"SDLK_CURRENCYSUBUNIT",
|
||||
"SDLK_KP_LEFTPAREN",
|
||||
"SDLK_KP_RIGHTPAREN",
|
||||
"SDLK_KP_LEFTBRACE",
|
||||
"SDLK_KP_RIGHTBRACE",
|
||||
"SDLK_KP_TAB",
|
||||
"SDLK_KP_BACKSPACE",
|
||||
"SDLK_KP_A",
|
||||
"SDLK_KP_B",
|
||||
"SDLK_KP_C",
|
||||
"SDLK_KP_D",
|
||||
"SDLK_KP_E",
|
||||
"SDLK_KP_F",
|
||||
"SDLK_KP_XOR",
|
||||
"SDLK_KP_POWER",
|
||||
"SDLK_KP_PERCENT",
|
||||
"SDLK_KP_LESS",
|
||||
"SDLK_KP_GREATER",
|
||||
"SDLK_KP_AMPERSAND",
|
||||
"SDLK_KP_DBLAMPERSAND",
|
||||
"SDLK_KP_VERTICALBAR",
|
||||
"SDLK_KP_DBLVERTICALBAR",
|
||||
"SDLK_KP_COLON",
|
||||
"SDLK_KP_HASH",
|
||||
"SDLK_KP_SPACE",
|
||||
"SDLK_KP_AT",
|
||||
"SDLK_KP_EXCLAM",
|
||||
"SDLK_KP_MEMSTORE",
|
||||
"SDLK_KP_MEMRECALL",
|
||||
"SDLK_KP_MEMCLEAR",
|
||||
"SDLK_KP_MEMADD",
|
||||
"SDLK_KP_MEMSUBTRACT",
|
||||
"SDLK_KP_MEMMULTIPLY",
|
||||
"SDLK_KP_MEMDIVIDE",
|
||||
"SDLK_KP_PLUSMINUS",
|
||||
"SDLK_KP_CLEAR",
|
||||
"SDLK_KP_CLEARENTRY",
|
||||
"SDLK_KP_BINARY",
|
||||
"SDLK_KP_OCTAL",
|
||||
"SDLK_KP_DECIMAL",
|
||||
"SDLK_KP_HEXADECIMAL",
|
||||
"SDLK_LCTRL",
|
||||
"SDLK_LSHIFT",
|
||||
"SDLK_LALT",
|
||||
"SDLK_LGUI",
|
||||
"SDLK_RCTRL",
|
||||
"SDLK_RSHIFT",
|
||||
"SDLK_RALT",
|
||||
"SDLK_RGUI",
|
||||
"SDLK_MODE",
|
||||
"SDLK_AUDIONEXT",
|
||||
"SDLK_AUDIOPREV",
|
||||
"SDLK_AUDIOSTOP",
|
||||
"SDLK_AUDIOPLAY",
|
||||
"SDLK_AUDIOMUTE",
|
||||
"SDLK_MEDIASELECT",
|
||||
"SDLK_WWW",
|
||||
"SDLK_MAIL",
|
||||
"SDLK_CALCULATOR",
|
||||
"SDLK_COMPUTER",
|
||||
"SDLK_AC_SEARCH",
|
||||
"SDLK_AC_HOME",
|
||||
"SDLK_AC_BACK",
|
||||
"SDLK_AC_FORWARD",
|
||||
"SDLK_AC_STOP",
|
||||
"SDLK_AC_REFRESH",
|
||||
"SDLK_AC_BOOKMARKS",
|
||||
"SDLK_BRIGHTNESSDOWN",
|
||||
"SDLK_BRIGHTNESSUP",
|
||||
"SDLK_DISPLAYSWITCH",
|
||||
"SDLK_KBDILLUMTOGGLE",
|
||||
"SDLK_KBDILLUMDOWN",
|
||||
"SDLK_KBDILLUMUP",
|
||||
"SDLK_EJECT",
|
||||
"SDLK_SLEEP",
|
||||
"SDLK_APP1",
|
||||
"SDLK_APP2",
|
||||
"SDLK_AUDIOREWIND",
|
||||
"SDLK_AUDIOFASTFORWARD",
|
||||
"SDLK_SOFTLEFT",
|
||||
"SDLK_SOFTRIGHT",
|
||||
"SDLK_CALL",
|
||||
"SDLK_ENDCALL",
|
||||
)
|
||||
|
||||
SDL_KEYMAP = {key: getattr(SDL_KeyCode, key) for key in SDL_KEYS}
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
binary_sensor.binary_sensor_schema(BinarySensor)
|
||||
|
||||
@@ -29,7 +29,7 @@ void SpeedFan::control(const fan::FanCall &call) {
|
||||
this->oscillating = *call.get_oscillating();
|
||||
if (call.get_direction().has_value())
|
||||
this->direction = *call.get_direction();
|
||||
this->set_preset_mode_(call.get_preset_mode());
|
||||
this->preset_mode = call.get_preset_mode();
|
||||
|
||||
this->write_state_();
|
||||
this->publish_state();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
@@ -16,7 +18,7 @@ class SpeedFan : public Component, public fan::Fan {
|
||||
void set_output(output::FloatOutput *output) { this->output_ = output; }
|
||||
void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; }
|
||||
void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; }
|
||||
void set_preset_modes(std::initializer_list<const char *> presets) { this->preset_modes_ = presets; }
|
||||
void set_preset_modes(const std::set<std::string> &presets) { this->preset_modes_ = presets; }
|
||||
fan::FanTraits get_traits() override { return this->traits_; }
|
||||
|
||||
protected:
|
||||
@@ -28,7 +30,7 @@ class SpeedFan : public Component, public fan::Fan {
|
||||
output::BinaryOutput *direction_{nullptr};
|
||||
int speed_count_{};
|
||||
fan::FanTraits traits_;
|
||||
std::vector<const char *> preset_modes_{};
|
||||
std::set<std::string> preset_modes_{};
|
||||
};
|
||||
|
||||
} // namespace speed
|
||||
|
||||
@@ -138,6 +138,7 @@ def _concat_nodes_override(values: Iterator[Any]) -> Any:
|
||||
values = chain(head, values)
|
||||
raw = "".join([str(v) for v in values])
|
||||
|
||||
result = None
|
||||
try:
|
||||
# Attempt to parse the concatenated string into a Python literal.
|
||||
# This allows expressions like "1 + 2" to be evaluated to the integer 3.
|
||||
@@ -145,11 +146,16 @@ def _concat_nodes_override(values: Iterator[Any]) -> Any:
|
||||
# fall back to returning the raw string. This is consistent with
|
||||
# Home Assistant's behavior when evaluating templates
|
||||
result = literal_eval(raw)
|
||||
except (ValueError, SyntaxError, MemoryError, TypeError):
|
||||
pass
|
||||
else:
|
||||
if isinstance(result, set):
|
||||
# Sets are not supported, return raw string
|
||||
return raw
|
||||
|
||||
if not isinstance(result, str):
|
||||
return result
|
||||
|
||||
except (ValueError, SyntaxError, MemoryError, TypeError):
|
||||
pass
|
||||
return raw
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ void TemplateFan::control(const fan::FanCall &call) {
|
||||
this->oscillating = *call.get_oscillating();
|
||||
if (call.get_direction().has_value() && this->has_direction_)
|
||||
this->direction = *call.get_direction();
|
||||
this->set_preset_mode_(call.get_preset_mode());
|
||||
this->preset_mode = call.get_preset_mode();
|
||||
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/fan/fan.h"
|
||||
|
||||
@@ -14,7 +16,7 @@ class TemplateFan : public Component, public fan::Fan {
|
||||
void set_has_direction(bool has_direction) { this->has_direction_ = has_direction; }
|
||||
void set_has_oscillating(bool has_oscillating) { this->has_oscillating_ = has_oscillating; }
|
||||
void set_speed_count(int count) { this->speed_count_ = count; }
|
||||
void set_preset_modes(std::initializer_list<const char *> presets) { this->preset_modes_ = presets; }
|
||||
void set_preset_modes(const std::set<std::string> &presets) { this->preset_modes_ = presets; }
|
||||
fan::FanTraits get_traits() override { return this->traits_; }
|
||||
|
||||
protected:
|
||||
@@ -24,7 +26,7 @@ class TemplateFan : public Component, public fan::Fan {
|
||||
bool has_direction_{false};
|
||||
int speed_count_{0};
|
||||
fan::FanTraits traits_;
|
||||
std::vector<const char *> preset_modes_{};
|
||||
std::set<std::string> preset_modes_{};
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
#include <cinttypes>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
@@ -245,8 +246,8 @@ void AsyncWebServerRequest::redirect(const std::string &url) {
|
||||
}
|
||||
|
||||
void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code, const char *content_type) {
|
||||
// Set status code - use constants for common codes to avoid string allocation
|
||||
const char *status = nullptr;
|
||||
// Set status code - use constants for common codes, default to 500 for unknown codes
|
||||
const char *status;
|
||||
switch (code) {
|
||||
case 200:
|
||||
status = HTTPD_200;
|
||||
@@ -258,9 +259,10 @@ void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code
|
||||
status = HTTPD_409;
|
||||
break;
|
||||
default:
|
||||
status = HTTPD_500;
|
||||
break;
|
||||
}
|
||||
httpd_resp_set_status(*this, status == nullptr ? to_string(code).c_str() : status);
|
||||
httpd_resp_set_status(*this, status);
|
||||
|
||||
if (content_type && *content_type) {
|
||||
httpd_resp_set_type(*this, content_type);
|
||||
@@ -348,7 +350,13 @@ void AsyncWebServerResponse::addHeader(const char *name, const char *value) {
|
||||
httpd_resp_set_hdr(*this->req_, name, value);
|
||||
}
|
||||
|
||||
void AsyncResponseStream::print(float value) { this->print(to_string(value)); }
|
||||
void AsyncResponseStream::print(float value) {
|
||||
// Use stack buffer to avoid temporary string allocation
|
||||
// Size: sign (1) + digits (10) + decimal (1) + precision (6) + exponent (5) + null (1) = 24, use 32 for safety
|
||||
char buf[32];
|
||||
int len = snprintf(buf, sizeof(buf), "%f", value);
|
||||
this->content_.append(buf, len);
|
||||
}
|
||||
|
||||
void AsyncResponseStream::printf(const char *fmt, ...) {
|
||||
va_list args;
|
||||
@@ -594,16 +602,19 @@ bool AsyncEventSourceResponse::try_send_nodefer(const char *message, const char
|
||||
|
||||
event_buffer_.append(chunk_len_header);
|
||||
|
||||
// Use stack buffer for formatting numeric fields to avoid temporary string allocations
|
||||
// Size: "retry: " (7) + max uint32 (10 digits) + CRLF (2) + null (1) = 20 bytes, use 32 for safety
|
||||
constexpr size_t num_buf_size = 32;
|
||||
char num_buf[num_buf_size];
|
||||
|
||||
if (reconnect) {
|
||||
event_buffer_.append("retry: ", sizeof("retry: ") - 1);
|
||||
event_buffer_.append(to_string(reconnect));
|
||||
event_buffer_.append(CRLF_STR, CRLF_LEN);
|
||||
int len = snprintf(num_buf, num_buf_size, "retry: %" PRIu32 CRLF_STR, reconnect);
|
||||
event_buffer_.append(num_buf, len);
|
||||
}
|
||||
|
||||
if (id) {
|
||||
event_buffer_.append("id: ", sizeof("id: ") - 1);
|
||||
event_buffer_.append(to_string(id));
|
||||
event_buffer_.append(CRLF_STR, CRLF_LEN);
|
||||
int len = snprintf(num_buf, num_buf_size, "id: %" PRIu32 CRLF_STR, id);
|
||||
event_buffer_.append(num_buf, len);
|
||||
}
|
||||
|
||||
if (event && *event) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/watchdog.h>
|
||||
#include <zephyr/sys/reboot.h>
|
||||
#include <zephyr/random/rand32.h>
|
||||
#include <zephyr/random/random.h>
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
|
||||
@@ -576,10 +576,11 @@ void Application::yield_with_select_(uint32_t delay_ms) {
|
||||
// Update fd_set if socket list has changed
|
||||
if (this->socket_fds_changed_) {
|
||||
FD_ZERO(&this->base_read_fds_);
|
||||
// fd bounds are already validated in register_socket_fd() or guaranteed by platform design:
|
||||
// - ESP32: LwIP guarantees fd < FD_SETSIZE by design (LWIP_SOCKET_OFFSET = FD_SETSIZE - CONFIG_LWIP_MAX_SOCKETS)
|
||||
// - Other platforms: register_socket_fd() validates fd < FD_SETSIZE
|
||||
for (int fd : this->socket_fds_) {
|
||||
if (fd >= 0 && fd < FD_SETSIZE) {
|
||||
FD_SET(fd, &this->base_read_fds_);
|
||||
}
|
||||
FD_SET(fd, &this->base_read_fds_);
|
||||
}
|
||||
this->socket_fds_changed_ = false;
|
||||
}
|
||||
|
||||
@@ -350,7 +350,7 @@ def safe_exp(obj: SafeExpType) -> Expression:
|
||||
return IntLiteral(int(obj.total_seconds))
|
||||
if isinstance(obj, TimePeriodMinutes):
|
||||
return IntLiteral(int(obj.total_minutes))
|
||||
if isinstance(obj, tuple | list):
|
||||
if isinstance(obj, (tuple, list)):
|
||||
return ArrayInitializer(*[safe_exp(o) for o in obj])
|
||||
if obj is bool:
|
||||
return bool_
|
||||
|
||||
@@ -133,7 +133,6 @@ ignore = [
|
||||
"PLW1641", # Object does not implement `__hash__` method
|
||||
"PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable
|
||||
"PLW2901", # Outer {outer_kind} variable {name} overwritten by inner {inner_kind} target
|
||||
"UP038", # https://github.com/astral-sh/ruff/issues/7871 https://github.com/astral-sh/ruff/pull/16681
|
||||
]
|
||||
|
||||
[tool.ruff.lint.isort]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pylint==4.0.2
|
||||
flake8==7.3.0 # also change in .pre-commit-config.yaml when updating
|
||||
ruff==0.14.2 # also change in .pre-commit-config.yaml when updating
|
||||
ruff==0.14.3 # also change in .pre-commit-config.yaml when updating
|
||||
pyupgrade==3.21.0 # also change in .pre-commit-config.yaml when updating
|
||||
pre-commit
|
||||
|
||||
|
||||
@@ -14,12 +14,14 @@ interval:
|
||||
|
||||
// Test parse_json
|
||||
bool parse_ok = esphome::json::parse_json(json_str, [](JsonObject root) {
|
||||
if (root.containsKey("sensor") && root.containsKey("value")) {
|
||||
if (root["sensor"].is<const char*>() && root["value"].is<float>()) {
|
||||
const char* sensor = root["sensor"];
|
||||
float value = root["value"];
|
||||
ESP_LOGD("test", "Parsed: sensor=%s, value=%.1f", sensor, value);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD("test", "Parsed JSON missing required keys");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
ESP_LOGD("test", "Parse result (JSON syntax only): %s", parse_ok ? "success" : "failed");
|
||||
|
||||
@@ -68,5 +68,13 @@ lvgl:
|
||||
enter_button: pushbutton
|
||||
group: general
|
||||
initial_focus: lv_roller
|
||||
on_draw_start:
|
||||
- logger.log: draw started
|
||||
on_draw_end:
|
||||
- logger.log: draw ended
|
||||
- lvgl.pause:
|
||||
- component.update: tft_display
|
||||
- delay: 60s
|
||||
- lvgl.resume:
|
||||
|
||||
<<: !include common.yaml
|
||||
|
||||
@@ -14,10 +14,10 @@ display:
|
||||
binary_sensor:
|
||||
- platform: sdl
|
||||
id: key_up
|
||||
key: SDLK_a
|
||||
key: SDLK_UP
|
||||
- platform: sdl
|
||||
id: key_down
|
||||
key: SDLK_d
|
||||
key: SDLK_DOWN
|
||||
- platform: sdl
|
||||
id: key_enter
|
||||
key: SDLK_s
|
||||
key: SDLK_RETURN
|
||||
|
||||
29
tests/components/web_server_idf/common.yaml
Normal file
29
tests/components/web_server_idf/common.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
esphome:
|
||||
name: test-web-server-idf
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
network:
|
||||
|
||||
# Add some entities to test SSE event formatting
|
||||
sensor:
|
||||
- platform: template
|
||||
name: "Test Sensor"
|
||||
id: test_sensor
|
||||
update_interval: 60s
|
||||
lambda: "return 42.5;"
|
||||
|
||||
binary_sensor:
|
||||
- platform: template
|
||||
name: "Test Binary Sensor"
|
||||
id: test_binary_sensor
|
||||
lambda: "return true;"
|
||||
|
||||
switch:
|
||||
- platform: template
|
||||
name: "Test Switch"
|
||||
id: test_switch
|
||||
optimistic: true
|
||||
3
tests/components/web_server_idf/test.esp32-idf.yaml
Normal file
3
tests/components/web_server_idf/test.esp32-idf.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
<<: !include common.yaml
|
||||
|
||||
web_server:
|
||||
@@ -33,3 +33,4 @@ test_list:
|
||||
{{{ "x", "79"}, { "y", "82"}}}
|
||||
- '{{{"AA"}}}'
|
||||
- '"HELLO"'
|
||||
- '{ 79, 82 }'
|
||||
|
||||
@@ -34,3 +34,4 @@ test_list:
|
||||
{{{ "x", "${ position.x }"}, { "y", "${ position.y }"}}}
|
||||
- ${ '{{{"AA"}}}' }
|
||||
- ${ '"HELLO"' }
|
||||
- '{ ${position.x}, ${position.y} }'
|
||||
|
||||
Reference in New Issue
Block a user