mirror of
https://github.com/esphome/esphome.git
synced 2026-01-17 07:24:53 -07:00
Compare commits
1 Commits
sprintf_gr
...
tuya_text_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80f90650c5 |
@@ -11,7 +11,7 @@ ci:
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.14.13
|
||||
rev: v0.14.11
|
||||
hooks:
|
||||
# Run the linter.
|
||||
- id: ruff
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "am43_base.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
|
||||
namespace esphome {
|
||||
namespace am43 {
|
||||
@@ -8,9 +8,12 @@ namespace am43 {
|
||||
const uint8_t START_PACKET[5] = {0x00, 0xff, 0x00, 0x00, 0x9a};
|
||||
|
||||
std::string pkt_to_hex(const uint8_t *data, uint16_t len) {
|
||||
char buf[64]; // format_hex_size(31) = 63, fits 31 bytes of hex data
|
||||
format_hex_to(buf, sizeof(buf), data, len);
|
||||
return buf;
|
||||
char buf[64];
|
||||
memset(buf, 0, 64);
|
||||
for (int i = 0; i < len; i++)
|
||||
sprintf(&buf[i * 2], "%02x", data[i]);
|
||||
std::string ret = buf;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Am43Packet *Am43Encoder::get_battery_level_request() {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
@@ -45,16 +44,13 @@ void LightWaveRF::send_rx(const std::vector<uint8_t> &msg, uint8_t repeats, bool
|
||||
}
|
||||
|
||||
void LightWaveRF::print_msg_(uint8_t *msg, uint8_t len) {
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
|
||||
char buffer[65]; // max 10 entries * 6 chars + null
|
||||
char buffer[65];
|
||||
ESP_LOGD(TAG, " Received code (len:%i): ", len);
|
||||
|
||||
size_t pos = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), pos, "0x%02x, ", msg[i]);
|
||||
sprintf(&buffer[i * 6], "0x%02x, ", msg[i]);
|
||||
}
|
||||
ESP_LOGD(TAG, "[%s]", buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
void LightWaveRF::dump_config() {
|
||||
|
||||
@@ -85,8 +85,8 @@ optional<AEHAData> AEHAProtocol::decode(RemoteReceiveData src) {
|
||||
std::string AEHAProtocol::format_data_(const std::vector<uint8_t> &data) {
|
||||
std::string out;
|
||||
for (uint8_t byte : data) {
|
||||
char buf[8]; // "0x%02X," = 5 chars + null + margin
|
||||
snprintf(buf, sizeof(buf), "0x%02X,", byte);
|
||||
char buf[6];
|
||||
sprintf(buf, "0x%02X,", byte);
|
||||
out += buf;
|
||||
}
|
||||
out.pop_back();
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "raw_protocol.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -9,30 +8,36 @@ static const char *const TAG = "remote.raw";
|
||||
|
||||
bool RawDumper::dump(RemoteReceiveData src) {
|
||||
char buffer[256];
|
||||
size_t pos = buf_append_printf(buffer, sizeof(buffer), 0, "Received Raw: ");
|
||||
uint32_t buffer_offset = 0;
|
||||
buffer_offset += sprintf(buffer, "Received Raw: ");
|
||||
|
||||
for (int32_t i = 0; i < src.size() - 1; i++) {
|
||||
const int32_t value = src[i];
|
||||
size_t prev_pos = pos;
|
||||
const uint32_t remaining_length = sizeof(buffer) - buffer_offset;
|
||||
int written;
|
||||
|
||||
if (i + 1 < src.size() - 1) {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), pos, "%" PRId32 ", ", value);
|
||||
written = snprintf(buffer + buffer_offset, remaining_length, "%" PRId32 ", ", value);
|
||||
} else {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), pos, "%" PRId32, value);
|
||||
written = snprintf(buffer + buffer_offset, remaining_length, "%" PRId32, value);
|
||||
}
|
||||
|
||||
if (pos >= sizeof(buffer) - 1) {
|
||||
// buffer full, flush and continue
|
||||
buffer[prev_pos] = '\0';
|
||||
if (written < 0 || written >= int(remaining_length)) {
|
||||
// write failed, flush...
|
||||
buffer[buffer_offset] = '\0';
|
||||
ESP_LOGI(TAG, "%s", buffer);
|
||||
buffer_offset = 0;
|
||||
written = sprintf(buffer, " ");
|
||||
if (i + 1 < src.size() - 1) {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), 0, " %" PRId32 ", ", value);
|
||||
written += sprintf(buffer + written, "%" PRId32 ", ", value);
|
||||
} else {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), 0, " %" PRId32, value);
|
||||
written += sprintf(buffer + written, "%" PRId32, value);
|
||||
}
|
||||
}
|
||||
|
||||
buffer_offset += written;
|
||||
}
|
||||
if (pos != 0) {
|
||||
if (buffer_offset != 0) {
|
||||
ESP_LOGI(TAG, "%s", buffer);
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "remote_base.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include <cinttypes>
|
||||
@@ -170,31 +169,36 @@ void RemoteTransmitterBase::send_(uint32_t send_times, uint32_t send_wait) {
|
||||
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
|
||||
const auto &vec = this->temp_.get_data();
|
||||
char buffer[256];
|
||||
size_t pos = buf_append_printf(buffer, sizeof(buffer), 0,
|
||||
"Sending times=%" PRIu32 " wait=%" PRIu32 "ms: ", send_times, send_wait);
|
||||
uint32_t buffer_offset = 0;
|
||||
buffer_offset += sprintf(buffer, "Sending times=%" PRIu32 " wait=%" PRIu32 "ms: ", send_times, send_wait);
|
||||
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
const int32_t value = vec[i];
|
||||
size_t prev_pos = pos;
|
||||
const uint32_t remaining_length = sizeof(buffer) - buffer_offset;
|
||||
int written;
|
||||
|
||||
if (i + 1 < vec.size()) {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), pos, "%" PRId32 ", ", value);
|
||||
written = snprintf(buffer + buffer_offset, remaining_length, "%" PRId32 ", ", value);
|
||||
} else {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), pos, "%" PRId32, value);
|
||||
written = snprintf(buffer + buffer_offset, remaining_length, "%" PRId32, value);
|
||||
}
|
||||
|
||||
if (pos >= sizeof(buffer) - 1) {
|
||||
// buffer full, flush and continue
|
||||
buffer[prev_pos] = '\0';
|
||||
if (written < 0 || written >= int(remaining_length)) {
|
||||
// write failed, flush...
|
||||
buffer[buffer_offset] = '\0';
|
||||
ESP_LOGVV(TAG, "%s", buffer);
|
||||
buffer_offset = 0;
|
||||
written = sprintf(buffer, " ");
|
||||
if (i + 1 < vec.size()) {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), 0, " %" PRId32 ", ", value);
|
||||
written += sprintf(buffer + written, "%" PRId32 ", ", value);
|
||||
} else {
|
||||
pos = buf_append_printf(buffer, sizeof(buffer), 0, " %" PRId32, value);
|
||||
written += sprintf(buffer + written, "%" PRId32, value);
|
||||
}
|
||||
}
|
||||
|
||||
buffer_offset += written;
|
||||
}
|
||||
if (pos != 0) {
|
||||
if (buffer_offset != 0) {
|
||||
ESP_LOGVV(TAG, "%s", buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "rf_bridge.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include <cinttypes>
|
||||
#include <cstring>
|
||||
|
||||
@@ -73,9 +72,9 @@ bool RFBridgeComponent::parse_bridge_byte_(uint8_t byte) {
|
||||
|
||||
data.length = raw[2];
|
||||
data.protocol = raw[3];
|
||||
char next_byte[3]; // 2 hex chars + null
|
||||
char next_byte[3];
|
||||
for (uint8_t i = 0; i < data.length - 1; i++) {
|
||||
buf_append_printf(next_byte, sizeof(next_byte), 0, "%02X", raw[4 + i]);
|
||||
sprintf(next_byte, "%02X", raw[4 + i]);
|
||||
data.code += next_byte;
|
||||
}
|
||||
|
||||
@@ -91,10 +90,10 @@ bool RFBridgeComponent::parse_bridge_byte_(uint8_t byte) {
|
||||
|
||||
uint8_t buckets = raw[2] << 1;
|
||||
std::string str;
|
||||
char next_byte[3]; // 2 hex chars + null
|
||||
char next_byte[3];
|
||||
|
||||
for (uint32_t i = 0; i <= at; i++) {
|
||||
buf_append_printf(next_byte, sizeof(next_byte), 0, "%02X", raw[i]);
|
||||
sprintf(next_byte, "%02X", raw[i]);
|
||||
str += next_byte;
|
||||
if ((i > 3) && buckets) {
|
||||
buckets--;
|
||||
|
||||
@@ -8,20 +8,17 @@ from esphome.const import (
|
||||
CONF_ICON,
|
||||
CONF_ID,
|
||||
CONF_INDEX,
|
||||
CONF_LAMBDA,
|
||||
CONF_MODE,
|
||||
CONF_MQTT_ID,
|
||||
CONF_ON_VALUE,
|
||||
CONF_OPERATION,
|
||||
CONF_OPTION,
|
||||
CONF_OPTIONS,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_WEB_SERVER,
|
||||
)
|
||||
from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority
|
||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
|
||||
from esphome.core.entity_helpers import entity_duplicate_validator, setup_entity
|
||||
from esphome.cpp_generator import MockObjClass, TemplateArguments
|
||||
from esphome.cpp_types import global_ns
|
||||
from esphome.cpp_generator import MockObjClass
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
@@ -41,9 +38,6 @@ SelectSetAction = select_ns.class_("SelectSetAction", automation.Action)
|
||||
SelectSetIndexAction = select_ns.class_("SelectSetIndexAction", automation.Action)
|
||||
SelectOperationAction = select_ns.class_("SelectOperationAction", automation.Action)
|
||||
|
||||
# Conditions
|
||||
SelectIsCondition = select_ns.class_("SelectIsCondition", automation.Condition)
|
||||
|
||||
# Enums
|
||||
SelectOperation = select_ns.enum("SelectOperation")
|
||||
SELECT_OPERATION_OPTIONS = {
|
||||
@@ -171,41 +165,6 @@ async def select_set_index_to_code(config, action_id, template_arg, args):
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
"select.is",
|
||||
SelectIsCondition,
|
||||
OPERATION_BASE_SCHEMA.extend(
|
||||
{
|
||||
cv.Optional(CONF_OPTIONS): cv.All(
|
||||
cv.ensure_list(cv.string_strict), cv.Length(min=1)
|
||||
),
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
}
|
||||
).add_extra(cv.has_exactly_one_key(CONF_OPTIONS, CONF_LAMBDA)),
|
||||
)
|
||||
async def select_is_to_code(config, condition_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
if options := config.get(CONF_OPTIONS):
|
||||
# List of constant options
|
||||
# Create a constexpr and pass that with a template length
|
||||
arr_id = ID(
|
||||
f"{condition_id}_data",
|
||||
is_declaration=True,
|
||||
type=global_ns.namespace("constexpr char * const"),
|
||||
)
|
||||
arg = cg.static_const_array(arr_id, cg.ArrayInitializer(*options))
|
||||
template_arg = TemplateArguments(len(options), *template_arg)
|
||||
else:
|
||||
# Lambda
|
||||
arg = await cg.process_lambda(
|
||||
config[CONF_LAMBDA],
|
||||
[(global_ns.namespace("StringRef &").operator("const"), "current")] + args,
|
||||
return_type=cg.bool_,
|
||||
)
|
||||
template_arg = TemplateArguments(0, *template_arg)
|
||||
return cg.new_Pvariable(condition_id, template_arg, paren, arg)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"select.operation",
|
||||
SelectOperationAction,
|
||||
|
||||
@@ -66,34 +66,4 @@ template<typename... Ts> class SelectOperationAction : public Action<Ts...> {
|
||||
Select *select_;
|
||||
};
|
||||
|
||||
template<size_t N, typename... Ts> class SelectIsCondition : public Condition<Ts...> {
|
||||
public:
|
||||
SelectIsCondition(Select *parent, const char *const *option_list) : parent_(parent), option_list_(option_list) {}
|
||||
|
||||
bool check(const Ts &...x) override {
|
||||
auto current = this->parent_->current_option();
|
||||
for (size_t i = 0; i != N; i++) {
|
||||
if (current == this->option_list_[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
Select *parent_;
|
||||
const char *const *option_list_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SelectIsCondition<0, Ts...> : public Condition<Ts...> {
|
||||
public:
|
||||
SelectIsCondition(Select *parent, std::function<bool(const StringRef &, const Ts &...)> &&f)
|
||||
: parent_(parent), f_(f) {}
|
||||
|
||||
bool check(const Ts &...x) override { return this->f_(this->parent_->current_option(), x...); }
|
||||
|
||||
protected:
|
||||
Select *parent_;
|
||||
std::function<bool(const StringRef &, const Ts &...)> f_;
|
||||
};
|
||||
} // namespace esphome::select
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "spi_led_strip.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace spi_led_strip {
|
||||
@@ -48,14 +47,15 @@ void SpiLedStrip::dump_config() {
|
||||
void SpiLedStrip::write_state(light::LightState *state) {
|
||||
if (this->is_failed())
|
||||
return;
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
{
|
||||
char strbuf[49]; // format_hex_pretty_size(16) = 48, fits 16 bytes
|
||||
size_t len = std::min(this->buffer_size_, (size_t) 16);
|
||||
format_hex_pretty_to(strbuf, sizeof(strbuf), this->buf_, len, ' ');
|
||||
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
|
||||
char strbuf[49];
|
||||
size_t len = std::min(this->buffer_size_, (size_t) (sizeof(strbuf) - 1) / 3);
|
||||
memset(strbuf, 0, sizeof(strbuf));
|
||||
for (size_t i = 0; i != len; i++) {
|
||||
sprintf(strbuf + i * 3, "%02X ", this->buf_[i]);
|
||||
}
|
||||
esph_log_v(TAG, "write_state: buf = %s", strbuf);
|
||||
}
|
||||
#endif
|
||||
this->enable();
|
||||
this->write_array(this->buf_, this->buffer_size_);
|
||||
this->disable();
|
||||
|
||||
@@ -24,7 +24,7 @@ void TuyaTextSensor::setup() {
|
||||
}
|
||||
case TuyaDatapointType::ENUM: {
|
||||
char buf[4]; // uint8_t max is 3 digits + null
|
||||
snprintf(buf, sizeof(buf), "%u", datapoint.value_enum);
|
||||
buf_append_printf(buf, sizeof(buf), 0, "%u", datapoint.value_enum);
|
||||
ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, buf);
|
||||
this->publish_state(buf);
|
||||
break;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pylint==4.0.4
|
||||
flake8==7.3.0 # also change in .pre-commit-config.yaml when updating
|
||||
ruff==0.14.13 # also change in .pre-commit-config.yaml when updating
|
||||
ruff==0.14.12 # also change in .pre-commit-config.yaml when updating
|
||||
pyupgrade==3.21.2 # also change in .pre-commit-config.yaml when updating
|
||||
pre-commit
|
||||
|
||||
|
||||
@@ -53,17 +53,6 @@ binary_sensor:
|
||||
// Garage Door is closed.
|
||||
return false;
|
||||
}
|
||||
- platform: template
|
||||
id: select_binary_sensor
|
||||
name: Select is one or two
|
||||
condition:
|
||||
any:
|
||||
- select.is:
|
||||
id: template_select
|
||||
options: [one, two]
|
||||
- select.is:
|
||||
id: template_select
|
||||
lambda: return current == id(template_text).state;
|
||||
- platform: template
|
||||
id: other_binary_sensor
|
||||
name: "Garage Door Closed"
|
||||
@@ -331,7 +320,6 @@ valve:
|
||||
|
||||
text:
|
||||
- platform: template
|
||||
id: template_text
|
||||
name: "Template text"
|
||||
optimistic: true
|
||||
min_length: 0
|
||||
|
||||
Reference in New Issue
Block a user