Format code with Google formatter

This commit is contained in:
2025-05-06 18:35:23 +02:00
parent d46c6e5084
commit 7f0e9d9324
6 changed files with 99 additions and 102 deletions
+2 -2
View File
@@ -294,8 +294,8 @@ bool NukiLockComponent::update_battery_report_() {
1000.0f); 1000.0f);
battery_resistance_sensor_->publish_state(batteryReport.batteryResistance / battery_resistance_sensor_->publish_state(batteryReport.batteryResistance /
1000.0f); 1000.0f);
last_action_lowest_voltage_sensor_->publish_state(batteryReport.lowestVoltage / last_action_lowest_voltage_sensor_->publish_state(
1000.0f); batteryReport.lowestVoltage / 1000.0f);
last_action_start_voltage_sensor_->publish_state(batteryReport.startVoltage / last_action_start_voltage_sensor_->publish_state(batteryReport.startVoltage /
1000.0f); 1000.0f);
last_action_lock_distance_sensor_->publish_state(batteryReport.lockDistance); last_action_lock_distance_sensor_->publish_state(batteryReport.lockDistance);
+1 -1
View File
@@ -25,7 +25,7 @@ class NukiLockComponent : public lock::Lock,
restart_after_beacon_latency_(0), restart_after_beacon_latency_(0),
update_scheduled_(false), update_scheduled_(false),
request_battery_reports_(false), request_battery_reports_(false),
pin_set_(false){}; pin_set_(false) {};
// Component. // Component.
void setup() override; void setup() override;
+60 -53
View File
@@ -3,9 +3,9 @@
#include <sstream> #include <sstream>
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/components/network/util.h" #include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#ifdef USE_LOGGER #ifdef USE_LOGGER
#include "esphome/components/logger/logger.h" #include "esphome/components/logger/logger.h"
@@ -15,16 +15,16 @@ namespace esphome {
namespace syslog { namespace syslog {
std::string remove_ansi_colors(const std::string& input) { std::string remove_ansi_colors(const std::string &input) {
std::string result; std::string result;
bool escape_sequence = false; bool escape_sequence = false;
for (char c : input) { for (char c : input) {
if (c == '\033') { // Start of an escape sequence if (c == '\033') { // Start of an escape sequence
escape_sequence = true; escape_sequence = true;
} else if (escape_sequence && c == 'm') { // End of an escape sequence } else if (escape_sequence && c == 'm') { // End of an escape sequence
escape_sequence = false; escape_sequence = false;
} else if (!escape_sequence) { // Normal character } else if (!escape_sequence) { // Normal character
result += c; result += c;
} }
} }
@@ -33,24 +33,24 @@ std::string remove_ansi_colors(const std::string& input) {
} }
enum Priority { enum Priority {
EMERG = 0, /* System is unusable */ EMERG = 0, /* System is unusable */
ALERT = 1, /* Action must be taken immediately */ ALERT = 1, /* Action must be taken immediately */
CRIT = 2, /* Critical conditions */ CRIT = 2, /* Critical conditions */
ERR = 3, /* Error conditions */ ERR = 3, /* Error conditions */
WARNING = 4, /* Warning conditions */ WARNING = 4, /* Warning conditions */
NOTICE = 5, /* Normal but significant conditions */ NOTICE = 5, /* Normal but significant conditions */
INFO = 6, /* Informational messages */ INFO = 6, /* Informational messages */
DEBUG = 7, /* Debug-level messages */ DEBUG = 7, /* Debug-level messages */
}; };
static const uint8_t esphome_to_syslog_log_levels[] = { static const uint8_t esphome_to_syslog_log_levels[] = {
/* ESPHOME_LOG_LEVEL_NONE */ Priority::DEBUG, /* ESPHOME_LOG_LEVEL_NONE */ Priority::DEBUG,
/* ESPHOME_LOG_LEVEL_ERROR */ Priority::ERR, /* ESPHOME_LOG_LEVEL_ERROR */ Priority::ERR,
/* ESPHOME_LOG_LEVEL_WARN */ Priority::WARNING, /* ESPHOME_LOG_LEVEL_WARN */ Priority::WARNING,
/* ESPHOME_LOG_LEVEL_INFO */ Priority::INFO, /* ESPHOME_LOG_LEVEL_INFO */ Priority::INFO,
/* ESPHOME_LOG_LEVEL_CONFIG */ Priority::NOTICE, /* ESPHOME_LOG_LEVEL_CONFIG */ Priority::NOTICE,
/* ESPHOME_LOG_LEVEL_DEBUG */ Priority::DEBUG, /* ESPHOME_LOG_LEVEL_DEBUG */ Priority::DEBUG,
/* ESPHOME_LOG_LEVEL_VERBOSE */ Priority::DEBUG, /* ESPHOME_LOG_LEVEL_VERBOSE */ Priority::DEBUG,
/* ESPHOME_LOG_LEVEL_VERY_VERBOSE */ Priority::DEBUG, /* ESPHOME_LOG_LEVEL_VERY_VERBOSE */ Priority::DEBUG,
}; };
@@ -67,33 +67,34 @@ void Syslog::setup() {
#ifdef USE_LOGGER #ifdef USE_LOGGER
if (logger::global_logger != nullptr && this->forward_logger_) { if (logger::global_logger != nullptr && this->forward_logger_) {
logger::global_logger->add_on_log_callback( logger::global_logger->add_on_log_callback(
[this](int level, const char *tag, const char *message) { [this](int level, const char *tag, const char *message) {
if (level > this->min_log_level_) if (level > this->min_log_level_) return;
return;
if (this->strip_color_codes_) { if (this->strip_color_codes_) {
std::string clean_message = remove_ansi_colors(message); std::string clean_message = remove_ansi_colors(message);
this->log(level, tag, clean_message); this->log(level, tag, clean_message);
} else { } else {
this->log(level, tag, message); this->log(level, tag, message);
} }
}); });
} }
#endif #endif
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || \
defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
this->socket_ = socket::socket(AF_INET, SOCK_DGRAM, PF_INET); this->socket_ = socket::socket(AF_INET, SOCK_DGRAM, PF_INET);
// We don't expect any reply from syslog server, so there's no need to block. // We don't expect any reply from syslog server, so there's no need to block.
this->socket_->setblocking(false); this->socket_->setblocking(false);
this->destination_len_ = socket::set_sockaddr( this->destination_len_ = socket::set_sockaddr(
reinterpret_cast<sockaddr *>(&this->destination_), sizeof(this->destination_), reinterpret_cast<sockaddr *>(&this->destination_),
this->server_ip_address_, this->server_port_); sizeof(this->destination_), this->server_ip_address_, this->server_port_);
if (this->destination_len_ == 0) { if (this->destination_len_ == 0) {
std::string error_message(strerror(errno)); std::string error_message(strerror(errno));
ESP_LOGE(TAG, "Cannot use IP address '%s' or port %d for server connection: %s", ESP_LOGE(TAG,
this->server_ip_address_.c_str(), this->server_port_, "Cannot use IP address '%s' or port %d for server connection: %s",
error_message.c_str()); this->server_ip_address_.c_str(), this->server_port_,
error_message.c_str());
} }
#endif #endif
} }
@@ -108,13 +109,17 @@ void Syslog::loop() {
void Syslog::dump_config() { void Syslog::dump_config() {
ESP_LOGCONFIG(TAG, "Syslog:"); ESP_LOGCONFIG(TAG, "Syslog:");
ESP_LOGCONFIG(TAG, " Server IP address: %s", this->server_ip_address_.c_str()); ESP_LOGCONFIG(TAG, " Server IP address: %s",
this->server_ip_address_.c_str());
ESP_LOGCONFIG(TAG, " Server port: %d", this->server_port_); ESP_LOGCONFIG(TAG, " Server port: %d", this->server_port_);
ESP_LOGCONFIG(TAG, " Client ID (syslog hostname): %s", this->hostname_.c_str()); ESP_LOGCONFIG(TAG, " Client ID (syslog hostname): %s",
this->hostname_.c_str());
#ifdef USE_LOGGER #ifdef USE_LOGGER
ESP_LOGCONFIG(TAG, " Min log level: %d", this->min_log_level_); ESP_LOGCONFIG(TAG, " Min log level: %d", this->min_log_level_);
ESP_LOGCONFIG(TAG, " Forward logger messages: %s", this->forward_logger_ ? "yes" : "no"); ESP_LOGCONFIG(TAG, " Forward logger messages: %s",
ESP_LOGCONFIG(TAG, " Strip color codes: %s", this->strip_color_codes_ ? "yes" : "no"); this->forward_logger_ ? "yes" : "no");
ESP_LOGCONFIG(TAG, " Strip color codes: %s",
this->strip_color_codes_ ? "yes" : "no");
#endif #endif
} }
@@ -127,23 +132,25 @@ void Syslog::log(int level, const std::string &tag, const std::string &msg) {
std::stringstream payload_stream; std::stringstream payload_stream;
// rsyslog compatible protocol: // rsyslog compatible protocol:
// <PRI>VERSION SP TIMESTAMP SP HOSTNAME SP APP-NAME SP PROCID SP MSGID SP [SD-ID]s SP MSG // <PRI>VERSION SP TIMESTAMP SP HOSTNAME SP APP-NAME SP PROCID SP MSGID SP
payload_stream // [SD-ID]s SP MSG
<< "<" << static_cast<int>(esphome_to_syslog_log_levels[level]) << ">1 - " payload_stream << "<" << static_cast<int>(esphome_to_syslog_log_levels[level])
<< this->hostname_ << " " << ">1 - " << this->hostname_ << " " << App.get_name()
<< App.get_name() << " - - - " << msg; << " - - - " << msg;
std::string payload = payload_stream.str(); std::string payload = payload_stream.str();
ssize_t bytes_sent = 0; ssize_t bytes_sent = 0;
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || \
defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
if (this->destination_len_ > 0) { if (this->destination_len_ > 0) {
bytes_sent = this->socket_->sendto( bytes_sent =
payload.c_str(), payload.length(), this->socket_->sendto(payload.c_str(), payload.length(), 0,
0, reinterpret_cast<sockaddr *>(&this->destination_),
reinterpret_cast<sockaddr *>(&this->destination_), this->destination_len_); this->destination_len_);
} }
#else #else
if (this->udp_client_.beginPacket(this->server_ip_address_.c_str(), this->server_port_)) { if (this->udp_client_.beginPacket(this->server_ip_address_.c_str(),
this->server_port_)) {
bytes_sent = this->udp_client_.write(payload.c_str(), payload.length()); bytes_sent = this->udp_client_.write(payload.c_str(), payload.length());
if (!this->udp_client_.endPacket()) { if (!this->udp_client_.endPacket()) {
bytes_sent = 0; bytes_sent = 0;
+15 -11
View File
@@ -13,11 +13,12 @@
- autoretries (fixes esp8266 skipping logs at startup due to dump_config flood) - autoretries (fixes esp8266 skipping logs at startup due to dump_config flood)
*/ */
#include "esphome/core/component.h"
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || \
defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
#include "esphome/components/socket/socket.h" #include "esphome/components/socket/socket.h"
#else #else
#include "WiFiUdp.h" #include "WiFiUdp.h"
@@ -31,13 +32,17 @@ class Syslog : public Component {
public: public:
explicit Syslog(); explicit Syslog();
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override {
return setup_priority::AFTER_WIFI;
}
void setup() override; void setup() override;
void loop() override; void loop() override;
void dump_config() override; void dump_config() override;
void set_server_ip_address(const std::string &address) { this->server_ip_address_ = address; } void set_server_ip_address(const std::string &address) {
this->server_ip_address_ = address;
}
void set_server_port(uint16_t port) { this->server_port_ = port; } void set_server_port(uint16_t port) { this->server_port_ = port; }
void set_hostname(const std::string &hostname) { this->hostname_ = hostname; } void set_hostname(const std::string &hostname) { this->hostname_ = hostname; }
void set_min_log_level(int log_level) { this->min_log_level_ = log_level; } void set_min_log_level(int log_level) { this->min_log_level_ = log_level; }
@@ -54,7 +59,8 @@ class Syslog : public Component {
bool forward_logger_; bool forward_logger_;
bool strip_color_codes_; bool strip_color_codes_;
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || \
defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
std::unique_ptr<esphome::socket::Socket> socket_{}; std::unique_ptr<esphome::socket::Socket> socket_{};
struct sockaddr_storage destination_; struct sockaddr_storage destination_;
socklen_t destination_len_; socklen_t destination_len_;
@@ -75,10 +81,8 @@ class Syslog : public Component {
TEMPLATABLE_VALUE(std::string, payload) TEMPLATABLE_VALUE(std::string, payload)
void play(Ts... x) override { void play(Ts... x) override {
this->parent_->log( this->parent_->log(this->level_.value(x...), this->tag_.value(x...),
this->level_.value(x...), this->payload_.value(x...));
this->tag_.value(x...),
this->payload_.value(x...));
} }
protected: protected:
@@ -86,7 +90,7 @@ class Syslog : public Component {
}; };
}; };
} // namespace syslog } // namespace syslog
} // namespace esphome } // namespace esphome
#endif #endif
+19 -32
View File
@@ -3,7 +3,7 @@
#include "esphome/components/remote_base/remote_base.h" #include "esphome/components/remote_base/remote_base.h"
namespace { namespace {
static const char* const TAG = __FILE__; static const char *const TAG = __FILE__;
static const uint8_t MIN_TEMPERATURE = 35; static const uint8_t MIN_TEMPERATURE = 35;
static const uint8_t MAX_TEMPERATURE = 70; static const uint8_t MAX_TEMPERATURE = 70;
static const uint8_t TEMPERATURE_LEVELS = 8; static const uint8_t TEMPERATURE_LEVELS = 8;
@@ -12,7 +12,8 @@ static void add_header_(esphome::remote_base::RemoteTransmitData *data) {
data->item(30, 1000); data->item(30, 1000);
} }
static void add_byte_(esphome::remote_base::RemoteTransmitData *data, uint8_t value) { static void add_byte_(esphome::remote_base::RemoteTransmitData *data,
uint8_t value) {
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
data->item(30, value & 0b10000000 ? 830 : 650); data->item(30, value & 0b10000000 ? 830 : 650);
value <<= 1; value <<= 1;
@@ -23,7 +24,7 @@ static void add_post_(esphome::remote_base::RemoteTransmitData *data) {
data->item(30, 460); data->item(30, 460);
data->item(30, 650); data->item(30, 650);
} }
} } // namespace
namespace esphome { namespace esphome {
namespace zehnder { namespace zehnder {
@@ -67,9 +68,8 @@ void ZehnderComponent::setup() {
} }
void ZehnderComponent::update() { void ZehnderComponent::update() {
float target_temp = this->mode == climate::CLIMATE_MODE_HEAT ? float target_temp =
this->target_temperature : this->mode == climate::CLIMATE_MODE_HEAT ? this->target_temperature : 0;
0;
this->transmit_temperature_(&target_temp); this->transmit_temperature_(&target_temp);
} }
@@ -89,17 +89,16 @@ climate::ClimateTraits ZehnderComponent::traits() {
traits.set_visual_current_temperature_step(0.1); traits.set_visual_current_temperature_step(0.1);
} }
traits.set_supported_modes({ traits.set_supported_modes(
climate::CLIMATE_MODE_HEAT, {climate::CLIMATE_MODE_HEAT, climate::CLIMATE_MODE_OFF});
climate::CLIMATE_MODE_OFF
});
return traits; return traits;
} }
void ZehnderComponent::dump_config() { void ZehnderComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Zehnder:"); ESP_LOGCONFIG(TAG, "Zehnder:");
ESP_LOGCONFIG(TAG, " has temperature sensor: %s", this->has_temperature_sensor_ ? "yes" : "no"); ESP_LOGCONFIG(TAG, " has temperature sensor: %s",
this->has_temperature_sensor_ ? "yes" : "no");
} }
bool ZehnderComponent::transmit_temperature_(float *temp) { bool ZehnderComponent::transmit_temperature_(float *temp) {
@@ -107,20 +106,16 @@ bool ZehnderComponent::transmit_temperature_(float *temp) {
if (*temp == 0) { if (*temp == 0) {
level = 0; level = 0;
} else { } else {
level = level = (*temp - MIN_TEMPERATURE) * (TEMPERATURE_LEVELS - 1) /
(*temp - MIN_TEMPERATURE) * (MAX_TEMPERATURE - MIN_TEMPERATURE) +
(TEMPERATURE_LEVELS - 1) / 1;
(MAX_TEMPERATURE - MIN_TEMPERATURE) +
1;
if (level > TEMPERATURE_LEVELS) { if (level > TEMPERATURE_LEVELS) {
level = TEMPERATURE_LEVELS; level = TEMPERATURE_LEVELS;
} }
*temp = *temp = (MAX_TEMPERATURE - MIN_TEMPERATURE) / (TEMPERATURE_LEVELS - 1) *
(MAX_TEMPERATURE - MIN_TEMPERATURE) / (level - 1) +
(TEMPERATURE_LEVELS - 1) * MIN_TEMPERATURE;
(level - 1) +
MIN_TEMPERATURE;
} }
return this->transmit_level_(level); return this->transmit_level_(level);
} }
@@ -146,17 +141,9 @@ bool ZehnderComponent::transmit_level_(uint8_t level) {
data->reset(); data->reset();
data->set_carrier_frequency(455000); data->set_carrier_frequency(455000);
static uint8_t mode_bytes[TEMPERATURE_LEVELS + 1] = { static uint8_t mode_bytes[TEMPERATURE_LEVELS + 1] = {0x0D, // OFF
0x0D, // OFF 0x86, 0x95, 0xA0, 0xB3,
0x86, 0xCA, 0xD9, 0xEC, 0xFF};
0x95,
0xA0,
0xB3,
0xCA,
0xD9,
0xEC,
0xFF
};
add_header_(data); add_header_(data);
add_byte_(data, 0xB8); add_byte_(data, 0xB8);
+2 -3
View File
@@ -8,10 +8,9 @@
namespace esphome { namespace esphome {
namespace zehnder { namespace zehnder {
class ZehnderComponent : public climate::Climate, class ZehnderComponent : public climate::Climate, public PollingComponent {
public PollingComponent {
public: public:
explicit ZehnderComponent(): Climate() {}; explicit ZehnderComponent() : Climate() {};
// Component. // Component.
void setup() override; void setup() override;