Merge remote-tracking branch 'upstream/dev' into dev

This commit is contained in:
Daniël Koek 2024-09-27 14:51:52 +01:00
commit 6974400778
16 changed files with 205 additions and 96 deletions

View file

@ -86,7 +86,7 @@ esphome/components/cap1188/* @mreditor97
esphome/components/captive_portal/* @OttoWinter esphome/components/captive_portal/* @OttoWinter
esphome/components/ccs811/* @habbie esphome/components/ccs811/* @habbie
esphome/components/cd74hc4067/* @asoehlke esphome/components/cd74hc4067/* @asoehlke
esphome/components/ch422g/* @jesterret esphome/components/ch422g/* @clydebarrow @jesterret
esphome/components/climate/* @esphome/core esphome/components/climate/* @esphome/core
esphome/components/climate_ir/* @glmnet esphome/components/climate_ir/* @glmnet
esphome/components/color_temperature/* @jesserockz esphome/components/color_temperature/* @jesserockz

View file

@ -1,18 +1,20 @@
from esphome import pins from esphome import pins
import esphome.codegen as cg import esphome.codegen as cg
from esphome.components import i2c from esphome.components import i2c
from esphome.components.i2c import I2CBus
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_I2C_ID,
CONF_ID, CONF_ID,
CONF_INPUT, CONF_INPUT,
CONF_INVERTED, CONF_INVERTED,
CONF_MODE, CONF_MODE,
CONF_NUMBER, CONF_NUMBER,
CONF_OPEN_DRAIN,
CONF_OUTPUT, CONF_OUTPUT,
CONF_RESTORE_VALUE,
) )
CODEOWNERS = ["@jesterret"] CODEOWNERS = ["@jesterret", "@clydebarrow"]
DEPENDENCIES = ["i2c"] DEPENDENCIES = ["i2c"]
MULTI_CONF = True MULTI_CONF = True
ch422g_ns = cg.esphome_ns.namespace("ch422g") ch422g_ns = cg.esphome_ns.namespace("ch422g")
@ -23,29 +25,36 @@ CH422GGPIOPin = ch422g_ns.class_(
) )
CONF_CH422G = "ch422g" CONF_CH422G = "ch422g"
CONFIG_SCHEMA = (
cv.Schema( # Note that no address is configurable - each register in the CH422G has a dedicated i2c address
{ CONFIG_SCHEMA = cv.Schema(
cv.Required(CONF_ID): cv.declare_id(CH422GComponent), {
cv.Optional(CONF_RESTORE_VALUE, default=False): cv.boolean, cv.GenerateID(CONF_ID): cv.declare_id(CH422GComponent),
} cv.GenerateID(CONF_I2C_ID): cv.use_id(I2CBus),
) }
.extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
.extend(i2c.i2c_device_schema(0x24))
)
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
await cg.register_component(var, config) await cg.register_component(var, config)
await i2c.register_i2c_device(var, config) # Can't use register_i2c_device because there is no CONF_ADDRESS
parent = await cg.get_variable(config[CONF_I2C_ID])
cg.add(var.set_i2c_bus(parent))
# This is used as a final validation step so that modes have been fully transformed.
def pin_mode_check(pin_config, _):
if pin_config[CONF_MODE][CONF_INPUT] and pin_config[CONF_NUMBER] >= 8:
raise cv.Invalid("CH422G only supports input on pins 0-7")
if pin_config[CONF_MODE][CONF_OPEN_DRAIN] and pin_config[CONF_NUMBER] < 8:
raise cv.Invalid("CH422G only supports open drain output on pins 8-11")
CH422G_PIN_SCHEMA = pins.gpio_base_schema( CH422G_PIN_SCHEMA = pins.gpio_base_schema(
CH422GGPIOPin, CH422GGPIOPin,
cv.int_range(min=0, max=7), cv.int_range(min=0, max=11),
modes=[CONF_INPUT, CONF_OUTPUT], modes=[CONF_INPUT, CONF_OUTPUT, CONF_OPEN_DRAIN],
).extend( ).extend(
{ {
cv.Required(CONF_CH422G): cv.use_id(CH422GComponent), cv.Required(CONF_CH422G): cv.use_id(CH422GComponent),
@ -53,7 +62,7 @@ CH422G_PIN_SCHEMA = pins.gpio_base_schema(
) )
@pins.PIN_SCHEMA_REGISTRY.register(CONF_CH422G, CH422G_PIN_SCHEMA) @pins.PIN_SCHEMA_REGISTRY.register(CONF_CH422G, CH422G_PIN_SCHEMA, pin_mode_check)
async def ch422g_pin_to_code(config): async def ch422g_pin_to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
parent = await cg.get_variable(config[CONF_CH422G]) parent = await cg.get_variable(config[CONF_CH422G])

View file

@ -4,33 +4,33 @@
namespace esphome { namespace esphome {
namespace ch422g { namespace ch422g {
const uint8_t CH422G_REG_IN = 0x26; static const uint8_t CH422G_REG_MODE = 0x24;
const uint8_t CH422G_REG_OUT = 0x38; static const uint8_t CH422G_MODE_OUTPUT = 0x01; // enables output mode on 0-7
const uint8_t OUT_REG_DEFAULT_VAL = 0xdf; static const uint8_t CH422G_MODE_OPEN_DRAIN = 0x04; // enables open drain mode on 8-11
static const uint8_t CH422G_REG_IN = 0x26; // read reg for input bits
static const uint8_t CH422G_REG_OUT = 0x38; // write reg for output bits 0-7
static const uint8_t CH422G_REG_OUT_UPPER = 0x23; // write reg for output bits 8-11
static const char *const TAG = "ch422g"; static const char *const TAG = "ch422g";
void CH422GComponent::setup() { void CH422GComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up CH422G..."); ESP_LOGCONFIG(TAG, "Setting up CH422G...");
// Test to see if device exists // set outputs before mode
if (!this->read_inputs_()) { this->write_outputs_();
// Set mode and check for errors
if (!this->set_mode_(this->mode_value_) || !this->read_inputs_()) {
ESP_LOGE(TAG, "CH422G not detected at 0x%02X", this->address_); ESP_LOGE(TAG, "CH422G not detected at 0x%02X", this->address_);
this->mark_failed(); this->mark_failed();
return; return;
} }
// restore defaults over whatever got saved on last boot ESP_LOGCONFIG(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
if (!this->restore_value_) { this->status_has_error());
this->write_output_(OUT_REG_DEFAULT_VAL);
}
ESP_LOGD(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
this->status_has_error());
} }
void CH422GComponent::loop() { void CH422GComponent::loop() {
// Clear all the previously read flags. // Clear all the previously read flags.
this->pin_read_cache_ = 0x00; this->pin_read_flags_ = 0x00;
} }
void CH422GComponent::dump_config() { void CH422GComponent::dump_config() {
@ -41,82 +41,99 @@ void CH422GComponent::dump_config() {
} }
} }
// ch422g doesn't have any flag support (needs docs?) void CH422GComponent::pin_mode(uint8_t pin, gpio::Flags flags) {
void CH422GComponent::pin_mode(uint8_t pin, gpio::Flags flags) {} if (pin < 8) {
if (flags & gpio::FLAG_OUTPUT) {
this->mode_value_ |= CH422G_MODE_OUTPUT;
}
} else {
if (flags & gpio::FLAG_OPEN_DRAIN) {
this->mode_value_ |= CH422G_MODE_OPEN_DRAIN;
}
}
}
bool CH422GComponent::digital_read(uint8_t pin) { bool CH422GComponent::digital_read(uint8_t pin) {
if (this->pin_read_cache_ == 0 || this->pin_read_cache_ & (1 << pin)) { if (this->pin_read_flags_ == 0 || this->pin_read_flags_ & (1 << pin)) {
// Read values on first access or in case it's being read again in the same loop // Read values on first access or in case it's being read again in the same loop
this->read_inputs_(); this->read_inputs_();
} }
this->pin_read_cache_ |= (1 << pin); this->pin_read_flags_ |= (1 << pin);
return this->state_mask_ & (1 << pin); return (this->input_bits_ & (1 << pin)) != 0;
} }
void CH422GComponent::digital_write(uint8_t pin, bool value) { void CH422GComponent::digital_write(uint8_t pin, bool value) {
if (value) { if (value) {
this->write_output_(this->state_mask_ | (1 << pin)); this->output_bits_ |= (1 << pin);
} else { } else {
this->write_output_(this->state_mask_ & ~(1 << pin)); this->output_bits_ &= ~(1 << pin);
} }
this->write_outputs_();
} }
bool CH422GComponent::read_inputs_() { bool CH422GComponent::read_inputs_() {
if (this->is_failed()) { if (this->is_failed()) {
return false; return false;
} }
uint8_t result;
uint8_t temp = 0; // reading inputs requires the chip to be in input mode, possibly temporarily.
if ((this->last_error_ = this->read(&temp, 1)) != esphome::i2c::ERROR_OK) { if (this->mode_value_ & CH422G_MODE_OUTPUT) {
this->status_set_warning(str_sprintf("read_inputs_(): I2C I/O error: %d", (int) this->last_error_).c_str()); this->set_mode_(this->mode_value_ & ~CH422G_MODE_OUTPUT);
return false; result = this->read_reg_(CH422G_REG_IN);
this->set_mode_(this->mode_value_);
} else {
result = this->read_reg_(CH422G_REG_IN);
} }
this->input_bits_ = result;
uint8_t output = 0;
if ((this->last_error_ = this->bus_->read(CH422G_REG_IN, &output, 1)) != esphome::i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("read_inputs_(): I2C I/O error: %d", (int) this->last_error_).c_str());
return false;
}
this->state_mask_ = output;
this->status_clear_warning(); this->status_clear_warning();
return true; return true;
} }
bool CH422GComponent::write_output_(uint8_t value) { // Write a register. Can't use the standard write_byte() method because there is no single pre-configured i2c address.
const uint8_t temp = 1; bool CH422GComponent::write_reg_(uint8_t reg, uint8_t value) {
if ((this->last_error_ = this->write(&temp, 1, false)) != esphome::i2c::ERROR_OK) { auto err = this->bus_->write(reg, &value, 1);
this->status_set_warning(str_sprintf("write_output_(): I2C I/O error: %d", (int) this->last_error_).c_str()); if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("write failed for register 0x%X, error %d", reg, err).c_str());
return false; return false;
} }
uint8_t write_mask = value;
if ((this->last_error_ = this->bus_->write(CH422G_REG_OUT, &write_mask, 1)) != esphome::i2c::ERROR_OK) {
this->status_set_warning(
str_sprintf("write_output_(): I2C I/O error: %d for write_mask: %d", (int) this->last_error_, (int) write_mask)
.c_str());
return false;
}
this->state_mask_ = value;
this->status_clear_warning(); this->status_clear_warning();
return true; return true;
} }
uint8_t CH422GComponent::read_reg_(uint8_t reg) {
uint8_t value;
auto err = this->bus_->read(reg, &value, 1);
if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("read failed for register 0x%X, error %d", reg, err).c_str());
return 0;
}
this->status_clear_warning();
return value;
}
bool CH422GComponent::set_mode_(uint8_t mode) { return this->write_reg_(CH422G_REG_MODE, mode); }
bool CH422GComponent::write_outputs_() {
return this->write_reg_(CH422G_REG_OUT, static_cast<uint8_t>(this->output_bits_)) &&
this->write_reg_(CH422G_REG_OUT_UPPER, static_cast<uint8_t>(this->output_bits_ >> 8));
}
float CH422GComponent::get_setup_priority() const { return setup_priority::IO; } float CH422GComponent::get_setup_priority() const { return setup_priority::IO; }
// Run our loop() method very early in the loop, so that we cache read values // Run our loop() method very early in the loop, so that we cache read values
// before other components call our digital_read() method. // before other components call our digital_read() method.
float CH422GComponent::get_loop_priority() const { return 9.0f; } // Just after WIFI float CH422GComponent::get_loop_priority() const { return 9.0f; } // Just after WIFI
void CH422GGPIOPin::setup() { pin_mode(flags_); }
void CH422GGPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); } void CH422GGPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
bool CH422GGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; } bool CH422GGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) ^ this->inverted_; }
void CH422GGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } void CH422GGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value ^ this->inverted_); }
std::string CH422GGPIOPin::dump_summary() const { return str_sprintf("EXIO%u via CH422G", pin_); } std::string CH422GGPIOPin::dump_summary() const { return str_sprintf("EXIO%u via CH422G", pin_); }
void CH422GGPIOPin::set_flags(gpio::Flags flags) {
flags_ = flags;
this->parent_->pin_mode(this->pin_, flags);
}
} // namespace ch422g } // namespace ch422g
} // namespace esphome } // namespace esphome

View file

@ -23,32 +23,30 @@ class CH422GComponent : public Component, public i2c::I2CDevice {
void pin_mode(uint8_t pin, gpio::Flags flags); void pin_mode(uint8_t pin, gpio::Flags flags);
float get_setup_priority() const override; float get_setup_priority() const override;
float get_loop_priority() const override; float get_loop_priority() const override;
void dump_config() override; void dump_config() override;
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
protected: protected:
bool write_reg_(uint8_t reg, uint8_t value);
uint8_t read_reg_(uint8_t reg);
bool set_mode_(uint8_t mode);
bool read_inputs_(); bool read_inputs_();
bool write_outputs_();
bool write_output_(uint8_t value);
/// The mask to write as output state - 1 means HIGH, 0 means LOW /// The mask to write as output state - 1 means HIGH, 0 means LOW
uint8_t state_mask_{0x00}; uint16_t output_bits_{0x00};
/// Flags to check if read previously during this loop /// Flags to check if read previously during this loop
uint8_t pin_read_cache_ = {0x00}; uint8_t pin_read_flags_ = {0x00};
/// Storage for last I2C error seen /// Copy of last read values
esphome::i2c::ErrorCode last_error_; uint8_t input_bits_ = {0x00};
/// Whether we want to override stored values on expander /// Copy of the mode value
bool restore_value_{false}; uint8_t mode_value_{};
}; };
/// Helper class to expose a CH422G pin as an internal input GPIO pin. /// Helper class to expose a CH422G pin as a GPIO pin.
class CH422GGPIOPin : public GPIOPin { class CH422GGPIOPin : public GPIOPin {
public: public:
void setup() override; void setup() override{};
void pin_mode(gpio::Flags flags) override; void pin_mode(gpio::Flags flags) override;
bool digital_read() override; bool digital_read() override;
void digital_write(bool value) override; void digital_write(bool value) override;
@ -57,13 +55,13 @@ class CH422GGPIOPin : public GPIOPin {
void set_parent(CH422GComponent *parent) { parent_ = parent; } void set_parent(CH422GComponent *parent) { parent_ = parent; }
void set_pin(uint8_t pin) { pin_ = pin; } void set_pin(uint8_t pin) { pin_ = pin; }
void set_inverted(bool inverted) { inverted_ = inverted; } void set_inverted(bool inverted) { inverted_ = inverted; }
void set_flags(gpio::Flags flags) { flags_ = flags; } void set_flags(gpio::Flags flags);
protected: protected:
CH422GComponent *parent_; CH422GComponent *parent_{};
uint8_t pin_; uint8_t pin_{};
bool inverted_; bool inverted_{};
gpio::Flags flags_; gpio::Flags flags_{};
}; };
} // namespace ch422g } // namespace ch422g

View file

@ -11,6 +11,7 @@ from esphome.const import (
CONF_BIRTH_MESSAGE, CONF_BIRTH_MESSAGE,
CONF_BROKER, CONF_BROKER,
CONF_CERTIFICATE_AUTHORITY, CONF_CERTIFICATE_AUTHORITY,
CONF_CLEAN_SESSION,
CONF_CLIENT_CERTIFICATE, CONF_CLIENT_CERTIFICATE,
CONF_CLIENT_CERTIFICATE_KEY, CONF_CLIENT_CERTIFICATE_KEY,
CONF_CLIENT_ID, CONF_CLIENT_ID,
@ -209,6 +210,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_PORT, default=1883): cv.port, cv.Optional(CONF_PORT, default=1883): cv.port,
cv.Optional(CONF_USERNAME, default=""): cv.string, cv.Optional(CONF_USERNAME, default=""): cv.string,
cv.Optional(CONF_PASSWORD, default=""): cv.string, cv.Optional(CONF_PASSWORD, default=""): cv.string,
cv.Optional(CONF_CLEAN_SESSION, default=False): cv.boolean,
cv.Optional(CONF_CLIENT_ID): cv.string, cv.Optional(CONF_CLIENT_ID): cv.string,
cv.SplitDefault(CONF_IDF_SEND_ASYNC, esp32_idf=False): cv.All( cv.SplitDefault(CONF_IDF_SEND_ASYNC, esp32_idf=False): cv.All(
cv.boolean, cv.only_with_esp_idf cv.boolean, cv.only_with_esp_idf
@ -325,6 +327,7 @@ async def to_code(config):
cg.add(var.set_broker_port(config[CONF_PORT])) cg.add(var.set_broker_port(config[CONF_PORT]))
cg.add(var.set_username(config[CONF_USERNAME])) cg.add(var.set_username(config[CONF_USERNAME]))
cg.add(var.set_password(config[CONF_PASSWORD])) cg.add(var.set_password(config[CONF_PASSWORD]))
cg.add(var.set_clean_session(config[CONF_CLEAN_SESSION]))
if CONF_CLIENT_ID in config: if CONF_CLIENT_ID in config:
cg.add(var.set_client_id(config[CONF_CLIENT_ID])) cg.add(var.set_client_id(config[CONF_CLIENT_ID]))

View file

@ -147,6 +147,7 @@ void MQTTClientComponent::dump_config() {
this->ip_.str().c_str()); this->ip_.str().c_str());
ESP_LOGCONFIG(TAG, " Username: " LOG_SECRET("'%s'"), this->credentials_.username.c_str()); ESP_LOGCONFIG(TAG, " Username: " LOG_SECRET("'%s'"), this->credentials_.username.c_str());
ESP_LOGCONFIG(TAG, " Client ID: " LOG_SECRET("'%s'"), this->credentials_.client_id.c_str()); ESP_LOGCONFIG(TAG, " Client ID: " LOG_SECRET("'%s'"), this->credentials_.client_id.c_str());
ESP_LOGCONFIG(TAG, " Clean Session: %s", YESNO(this->credentials_.clean_session));
if (this->is_discovery_ip_enabled()) { if (this->is_discovery_ip_enabled()) {
ESP_LOGCONFIG(TAG, " Discovery IP enabled"); ESP_LOGCONFIG(TAG, " Discovery IP enabled");
} }
@ -246,6 +247,7 @@ void MQTTClientComponent::start_connect_() {
this->mqtt_backend_.disconnect(); this->mqtt_backend_.disconnect();
this->mqtt_backend_.set_client_id(this->credentials_.client_id.c_str()); this->mqtt_backend_.set_client_id(this->credentials_.client_id.c_str());
this->mqtt_backend_.set_clean_session(this->credentials_.clean_session);
const char *username = nullptr; const char *username = nullptr;
if (!this->credentials_.username.empty()) if (!this->credentials_.username.empty())
username = this->credentials_.username.c_str(); username = this->credentials_.username.c_str();

View file

@ -51,6 +51,7 @@ struct MQTTCredentials {
std::string username; std::string username;
std::string password; std::string password;
std::string client_id; ///< The client ID. Will automatically be truncated to 23 characters. std::string client_id; ///< The client ID. Will automatically be truncated to 23 characters.
bool clean_session; ///< Whether the session will be cleaned or remembered between connects.
}; };
/// Simple data struct for Home Assistant component availability. /// Simple data struct for Home Assistant component availability.
@ -254,6 +255,7 @@ class MQTTClientComponent : public Component {
void set_username(const std::string &username) { this->credentials_.username = username; } void set_username(const std::string &username) { this->credentials_.username = username; }
void set_password(const std::string &password) { this->credentials_.password = password; } void set_password(const std::string &password) { this->credentials_.password = password; }
void set_client_id(const std::string &client_id) { this->credentials_.client_id = client_id; } void set_client_id(const std::string &client_id) { this->credentials_.client_id = client_id; }
void set_clean_session(const bool &clean_session) { this->credentials_.clean_session = clean_session; }
void set_on_connect(mqtt_on_connect_callback_t &&callback); void set_on_connect(mqtt_on_connect_callback_t &&callback);
void set_on_disconnect(mqtt_on_disconnect_callback_t &&callback); void set_on_disconnect(mqtt_on_disconnect_callback_t &&callback);

View file

@ -130,11 +130,16 @@ void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, voi
} }
void WiFiComponent::wifi_pre_setup_() { void WiFiComponent::wifi_pre_setup_() {
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
uint8_t mac[6]; uint8_t mac[6];
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
get_mac_address_raw(mac); get_mac_address_raw(mac);
set_mac_address(mac); set_mac_address(mac);
ESP_LOGV(TAG, "Use EFuse MAC without checking CRC: %s", get_mac_address_pretty().c_str()); ESP_LOGV(TAG, "Use EFuse MAC without checking CRC: %s", get_mac_address_pretty().c_str());
#else
if (has_custom_mac_address()) {
get_mac_address_raw(mac);
set_mac_address(mac);
}
#endif #endif
esp_err_t err = esp_netif_init(); esp_err_t err = esp_netif_init();
if (err != ERR_OK) { if (err != ERR_OK) {

View file

@ -120,6 +120,7 @@ CONF_CHANNELS = "channels"
CONF_CHARACTERISTIC_UUID = "characteristic_uuid" CONF_CHARACTERISTIC_UUID = "characteristic_uuid"
CONF_CHECK = "check" CONF_CHECK = "check"
CONF_CHIPSET = "chipset" CONF_CHIPSET = "chipset"
CONF_CLEAN_SESSION = "clean_session"
CONF_CLEAR_IMPEDANCE = "clear_impedance" CONF_CLEAR_IMPEDANCE = "clear_impedance"
CONF_CLIENT_CERTIFICATE = "client_certificate" CONF_CLIENT_CERTIFICATE = "client_certificate"
CONF_CLIENT_CERTIFICATE_KEY = "client_certificate_key" CONF_CLIENT_CERTIFICATE_KEY = "client_certificate_key"

View file

@ -671,9 +671,17 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
// match the CRC that goes along with it. For those devices, this // match the CRC that goes along with it. For those devices, this
// work-around reads and uses the MAC address as-is from EFuse, // work-around reads and uses the MAC address as-is from EFuse,
// without doing the CRC check. // without doing the CRC check.
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48); if (has_custom_mac_address()) {
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
} else {
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
}
#else #else
esp_efuse_mac_get_default(mac); if (has_custom_mac_address()) {
esp_efuse_mac_get_custom(mac);
} else {
esp_efuse_mac_get_default(mac);
}
#endif #endif
#elif defined(USE_ESP8266) #elif defined(USE_ESP8266)
wifi_get_macaddr(STATION_IF, mac); wifi_get_macaddr(STATION_IF, mac);
@ -685,20 +693,53 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
// this should be an error, but that messes with CI checks. #error No mac address method defined // this should be an error, but that messes with CI checks. #error No mac address method defined
#endif #endif
} }
std::string get_mac_address() { std::string get_mac_address() {
uint8_t mac[6]; uint8_t mac[6];
get_mac_address_raw(mac); get_mac_address_raw(mac);
return str_snprintf("%02x%02x%02x%02x%02x%02x", 12, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return str_snprintf("%02x%02x%02x%02x%02x%02x", 12, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
} }
std::string get_mac_address_pretty() { std::string get_mac_address_pretty() {
uint8_t mac[6]; uint8_t mac[6];
get_mac_address_raw(mac); get_mac_address_raw(mac);
return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
} }
#ifdef USE_ESP32 #ifdef USE_ESP32
void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); } void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); }
#endif #endif
bool has_custom_mac_address() {
#ifdef USE_ESP32
uint8_t mac[6];
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) || defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC)
return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac);
#else
return (esp_efuse_mac_get_custom(mac) == ESP_OK) && mac_address_is_valid(mac);
#endif
#else
return false;
#endif
}
bool mac_address_is_valid(const uint8_t *mac) {
bool is_all_zeros = true;
bool is_all_ones = true;
for (uint8_t i = 0; i < 6; i++) {
if (mac[i] != 0) {
is_all_zeros = false;
}
}
for (uint8_t i = 0; i < 6; i++) {
if (mac[i] != 0xFF) {
is_all_ones = false;
}
}
return !(is_all_zeros || is_all_ones);
}
void delay_microseconds_safe(uint32_t us) { // avoids CPU locks that could trigger WDT or affect WiFi/BT stability void delay_microseconds_safe(uint32_t us) { // avoids CPU locks that could trigger WDT or affect WiFi/BT stability
uint32_t start = micros(); uint32_t start = micros();

View file

@ -635,6 +635,14 @@ std::string get_mac_address_pretty();
void set_mac_address(uint8_t *mac); void set_mac_address(uint8_t *mac);
#endif #endif
/// Check if a custom MAC address is set (ESP32 & variants)
/// @return True if a custom MAC address is set (ESP32 & variants), else false
bool has_custom_mac_address();
/// Check if the MAC address is not all zeros or all ones
/// @return True if MAC is valid, else false
bool mac_address_is_valid(const uint8_t *mac);
/// Delay for the given amount of microseconds, possibly yielding to other processes during the wait. /// Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
void delay_microseconds_safe(uint32_t us); void delay_microseconds_safe(uint32_t us);

View file

@ -11,16 +11,26 @@ namespace esphome {
static const char *const TAG = "ring_buffer"; static const char *const TAG = "ring_buffer";
RingBuffer::~RingBuffer() {
if (this->handle_ != nullptr) {
vStreamBufferDelete(this->handle_);
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
allocator.deallocate(this->storage_, this->size_);
}
}
std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) { std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) {
std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>(); std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>();
rb->size_ = len + 1;
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
rb->storage_ = allocator.allocate(len + 1); rb->storage_ = allocator.allocate(rb->size_);
if (rb->storage_ == nullptr) { if (rb->storage_ == nullptr) {
return nullptr; return nullptr;
} }
rb->handle_ = xStreamBufferCreateStatic(len + 1, 1, rb->storage_, &rb->structure_); rb->handle_ = xStreamBufferCreateStatic(rb->size_, 1, rb->storage_, &rb->structure_);
ESP_LOGD(TAG, "Created ring buffer with size %u", len); ESP_LOGD(TAG, "Created ring buffer with size %u", len);
return rb; return rb;
} }

View file

@ -12,6 +12,8 @@ namespace esphome {
class RingBuffer { class RingBuffer {
public: public:
~RingBuffer();
/** /**
* @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary. * @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary.
* *
@ -83,6 +85,7 @@ class RingBuffer {
StreamBufferHandle_t handle_; StreamBufferHandle_t handle_;
StaticStreamBuffer_t structure_; StaticStreamBuffer_t structure_;
uint8_t *storage_; uint8_t *storage_;
size_t size_{0};
}; };
} // namespace esphome } // namespace esphome

View file

@ -226,4 +226,6 @@ class _Schema(vol.Schema):
if isinstance(schema, vol.Schema): if isinstance(schema, vol.Schema):
schema = schema.schema schema = schema.schema
ret = super().extend(schema, extra=extra) ret = super().extend(schema, extra=extra)
return _Schema(ret.schema, extra=ret.extra, extra_schemas=self._extra_schemas) return _Schema(
ret.schema, extra=ret.extra, extra_schemas=self._extra_schemas.copy()
)

View file

@ -1,6 +1,5 @@
ch422g: ch422g:
- id: ch422g_hub - id: ch422g_hub
address: 0x24
binary_sensor: binary_sensor:
- platform: gpio - platform: gpio
@ -11,10 +10,18 @@ binary_sensor:
number: 1 number: 1
mode: INPUT mode: INPUT
inverted: true inverted: true
output:
- platform: gpio - platform: gpio
id: ch422g_output id: ch422_out_0
pin: pin:
ch422g: ch422g_hub ch422g: ch422g_hub
number: 0 number: 0
mode: OUTPUT mode: OUTPUT
inverted: false inverted: false
- platform: gpio
id: ch422_out_11
pin:
ch422g: ch422g_hub
number: 11
mode: OUTPUT_OPEN_DRAIN
inverted: true

View file

@ -10,6 +10,7 @@ mqtt:
port: 1883 port: 1883
username: debug username: debug
password: debug password: debug
clean_session: True
client_id: someclient client_id: someclient
use_abbreviations: false use_abbreviations: false
discovery: true discovery: true