mirror of
https://github.com/esphome/esphome.git
synced 2025-01-12 07:33:19 +01:00
Merge branch 'dev' into nrf52_core
This commit is contained in:
commit
d872f337af
38 changed files with 514 additions and 113 deletions
2
.github/workflows/sync-device-classes.yml
vendored
2
.github/workflows/sync-device-classes.yml
vendored
|
@ -36,7 +36,7 @@ jobs:
|
|||
python ./script/sync-device_class.py
|
||||
|
||||
- name: Commit changes
|
||||
uses: peter-evans/create-pull-request@v7.0.3
|
||||
uses: peter-evans/create-pull-request@v7.0.5
|
||||
with:
|
||||
commit-message: "Synchronise Device Classes from Home Assistant"
|
||||
committer: esphomebot <esphome@nabucasa.com>
|
||||
|
|
|
@ -86,7 +86,7 @@ esphome/components/cap1188/* @mreditor97
|
|||
esphome/components/captive_portal/* @OttoWinter
|
||||
esphome/components/ccs811/* @habbie
|
||||
esphome/components/cd74hc4067/* @asoehlke
|
||||
esphome/components/ch422g/* @jesterret
|
||||
esphome/components/ch422g/* @clydebarrow @jesterret
|
||||
esphome/components/climate/* @esphome/core
|
||||
esphome/components/climate_ir/* @glmnet
|
||||
esphome/components/color_temperature/* @jesserockz
|
||||
|
|
|
@ -1243,6 +1243,9 @@ VoiceAssistantConfigurationResponse APIConnection::voice_assistant_get_configura
|
|||
}
|
||||
resp.available_wake_words.push_back(std::move(resp_wake_word));
|
||||
}
|
||||
for (auto &wake_word_id : config.active_wake_words) {
|
||||
resp.active_wake_words.push_back(wake_word_id);
|
||||
}
|
||||
resp.max_active_wake_words = config.max_active_wake_words;
|
||||
}
|
||||
return resp;
|
||||
|
|
|
@ -145,8 +145,9 @@ FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
|
|||
),
|
||||
)
|
||||
async def reset_energy_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
return var
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c
|
||||
from esphome.components.i2c import I2CBus
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_I2C_ID,
|
||||
CONF_ID,
|
||||
CONF_INPUT,
|
||||
CONF_INVERTED,
|
||||
CONF_MODE,
|
||||
CONF_NUMBER,
|
||||
CONF_OPEN_DRAIN,
|
||||
CONF_OUTPUT,
|
||||
CONF_RESTORE_VALUE,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@jesterret"]
|
||||
CODEOWNERS = ["@jesterret", "@clydebarrow"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
MULTI_CONF = True
|
||||
ch422g_ns = cg.esphome_ns.namespace("ch422g")
|
||||
|
@ -23,29 +25,36 @@ CH422GGPIOPin = ch422g_ns.class_(
|
|||
)
|
||||
|
||||
CONF_CH422G = "ch422g"
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(CH422GComponent),
|
||||
cv.Optional(CONF_RESTORE_VALUE, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(i2c.i2c_device_schema(0x24))
|
||||
)
|
||||
|
||||
# Note that no address is configurable - each register in the CH422G has a dedicated i2c address
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_ID): cv.declare_id(CH422GComponent),
|
||||
cv.GenerateID(CONF_I2C_ID): cv.use_id(I2CBus),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
|
||||
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(
|
||||
CH422GGPIOPin,
|
||||
cv.int_range(min=0, max=7),
|
||||
modes=[CONF_INPUT, CONF_OUTPUT],
|
||||
cv.int_range(min=0, max=11),
|
||||
modes=[CONF_INPUT, CONF_OUTPUT, CONF_OPEN_DRAIN],
|
||||
).extend(
|
||||
{
|
||||
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):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
parent = await cg.get_variable(config[CONF_CH422G])
|
||||
|
|
|
@ -4,33 +4,33 @@
|
|||
namespace esphome {
|
||||
namespace ch422g {
|
||||
|
||||
const uint8_t CH422G_REG_IN = 0x26;
|
||||
const uint8_t CH422G_REG_OUT = 0x38;
|
||||
const uint8_t OUT_REG_DEFAULT_VAL = 0xdf;
|
||||
static const uint8_t CH422G_REG_MODE = 0x24;
|
||||
static const uint8_t CH422G_MODE_OUTPUT = 0x01; // enables output mode on 0-7
|
||||
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";
|
||||
|
||||
void CH422GComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up CH422G...");
|
||||
// Test to see if device exists
|
||||
if (!this->read_inputs_()) {
|
||||
// set outputs before mode
|
||||
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_);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
// restore defaults over whatever got saved on last boot
|
||||
if (!this->restore_value_) {
|
||||
this->write_output_(OUT_REG_DEFAULT_VAL);
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
|
||||
this->status_has_error());
|
||||
ESP_LOGCONFIG(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
|
||||
this->status_has_error());
|
||||
}
|
||||
|
||||
void CH422GComponent::loop() {
|
||||
// Clear all the previously read flags.
|
||||
this->pin_read_cache_ = 0x00;
|
||||
this->pin_read_flags_ = 0x00;
|
||||
}
|
||||
|
||||
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) {
|
||||
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
|
||||
this->read_inputs_();
|
||||
}
|
||||
|
||||
this->pin_read_cache_ |= (1 << pin);
|
||||
return this->state_mask_ & (1 << pin);
|
||||
this->pin_read_flags_ |= (1 << pin);
|
||||
return (this->input_bits_ & (1 << pin)) != 0;
|
||||
}
|
||||
|
||||
void CH422GComponent::digital_write(uint8_t pin, bool value) {
|
||||
if (value) {
|
||||
this->write_output_(this->state_mask_ | (1 << pin));
|
||||
this->output_bits_ |= (1 << pin);
|
||||
} else {
|
||||
this->write_output_(this->state_mask_ & ~(1 << pin));
|
||||
this->output_bits_ &= ~(1 << pin);
|
||||
}
|
||||
this->write_outputs_();
|
||||
}
|
||||
|
||||
bool CH422GComponent::read_inputs_() {
|
||||
if (this->is_failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t temp = 0;
|
||||
if ((this->last_error_ = this->read(&temp, 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;
|
||||
uint8_t result;
|
||||
// reading inputs requires the chip to be in input mode, possibly temporarily.
|
||||
if (this->mode_value_ & CH422G_MODE_OUTPUT) {
|
||||
this->set_mode_(this->mode_value_ & ~CH422G_MODE_OUTPUT);
|
||||
result = this->read_reg_(CH422G_REG_IN);
|
||||
this->set_mode_(this->mode_value_);
|
||||
} else {
|
||||
result = this->read_reg_(CH422G_REG_IN);
|
||||
}
|
||||
|
||||
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->input_bits_ = result;
|
||||
this->status_clear_warning();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CH422GComponent::write_output_(uint8_t value) {
|
||||
const uint8_t temp = 1;
|
||||
if ((this->last_error_ = this->write(&temp, 1, false)) != esphome::i2c::ERROR_OK) {
|
||||
this->status_set_warning(str_sprintf("write_output_(): I2C I/O error: %d", (int) this->last_error_).c_str());
|
||||
// Write a register. Can't use the standard write_byte() method because there is no single pre-configured i2c address.
|
||||
bool CH422GComponent::write_reg_(uint8_t reg, uint8_t value) {
|
||||
auto err = this->bus_->write(reg, &value, 1);
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
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; }
|
||||
|
||||
// Run our loop() method very early in the loop, so that we cache read values
|
||||
// before other components call our digital_read() method.
|
||||
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); }
|
||||
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_); }
|
||||
void CH422GGPIOPin::set_flags(gpio::Flags flags) {
|
||||
flags_ = flags;
|
||||
this->parent_->pin_mode(this->pin_, flags);
|
||||
}
|
||||
|
||||
} // namespace ch422g
|
||||
} // namespace esphome
|
||||
|
|
|
@ -23,32 +23,30 @@ class CH422GComponent : public Component, public i2c::I2CDevice {
|
|||
void pin_mode(uint8_t pin, gpio::Flags flags);
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
float get_loop_priority() const override;
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
|
||||
|
||||
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 write_output_(uint8_t value);
|
||||
bool write_outputs_();
|
||||
|
||||
/// 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
|
||||
uint8_t pin_read_cache_ = {0x00};
|
||||
/// Storage for last I2C error seen
|
||||
esphome::i2c::ErrorCode last_error_;
|
||||
/// Whether we want to override stored values on expander
|
||||
bool restore_value_{false};
|
||||
uint8_t pin_read_flags_ = {0x00};
|
||||
/// Copy of last read values
|
||||
uint8_t input_bits_ = {0x00};
|
||||
/// Copy of the mode value
|
||||
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 {
|
||||
public:
|
||||
void setup() override;
|
||||
void setup() override{};
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
|
@ -57,13 +55,13 @@ class CH422GGPIOPin : public GPIOPin {
|
|||
void set_parent(CH422GComponent *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
void set_inverted(bool inverted) { inverted_ = inverted; }
|
||||
void set_flags(gpio::Flags flags) { flags_ = flags; }
|
||||
void set_flags(gpio::Flags flags);
|
||||
|
||||
protected:
|
||||
CH422GComponent *parent_;
|
||||
uint8_t pin_;
|
||||
bool inverted_;
|
||||
gpio::Flags flags_;
|
||||
CH422GComponent *parent_{};
|
||||
uint8_t pin_{};
|
||||
bool inverted_{};
|
||||
gpio::Flags flags_{};
|
||||
};
|
||||
|
||||
} // namespace ch422g
|
||||
|
|
|
@ -147,6 +147,7 @@ void CSE7766Component::parse_data_() {
|
|||
float power = 0.0f;
|
||||
if (power_cycle_exceeds_range) {
|
||||
// Datasheet: power cycle exceeding range means active power is 0
|
||||
have_power = true;
|
||||
if (this->power_sensor_ != nullptr) {
|
||||
this->power_sensor_->publish_state(0.0f);
|
||||
}
|
||||
|
@ -178,6 +179,15 @@ void CSE7766Component::parse_data_() {
|
|||
if (this->apparent_power_sensor_ != nullptr) {
|
||||
this->apparent_power_sensor_->publish_state(apparent_power);
|
||||
}
|
||||
if (have_power && this->reactive_power_sensor_ != nullptr) {
|
||||
const float reactive_power = apparent_power - power;
|
||||
if (reactive_power < 0.0f) {
|
||||
ESP_LOGD(TAG, "Impossible reactive power: %.4f is negative", reactive_power);
|
||||
this->reactive_power_sensor_->publish_state(0.0f);
|
||||
} else {
|
||||
this->reactive_power_sensor_->publish_state(reactive_power);
|
||||
}
|
||||
}
|
||||
if (this->power_factor_sensor_ != nullptr && (have_power || power_cycle_exceeds_range)) {
|
||||
float pf = NAN;
|
||||
if (apparent_power > 0) {
|
||||
|
@ -232,6 +242,7 @@ void CSE7766Component::dump_config() {
|
|||
LOG_SENSOR(" ", "Power", this->power_sensor_);
|
||||
LOG_SENSOR(" ", "Energy", this->energy_sensor_);
|
||||
LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
|
||||
LOG_SENSOR(" ", "Reactive Power", this->reactive_power_sensor_);
|
||||
LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
|
||||
this->check_uart_settings(4800);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@ class CSE7766Component : public Component, public uart::UARTDevice {
|
|||
void set_apparent_power_sensor(sensor::Sensor *apparent_power_sensor) {
|
||||
apparent_power_sensor_ = apparent_power_sensor;
|
||||
}
|
||||
void set_reactive_power_sensor(sensor::Sensor *reactive_power_sensor) {
|
||||
reactive_power_sensor_ = reactive_power_sensor;
|
||||
}
|
||||
void set_power_factor_sensor(sensor::Sensor *power_factor_sensor) { power_factor_sensor_ = power_factor_sensor; }
|
||||
|
||||
void loop() override;
|
||||
|
@ -35,6 +38,7 @@ class CSE7766Component : public Component, public uart::UARTDevice {
|
|||
sensor::Sensor *power_sensor_{nullptr};
|
||||
sensor::Sensor *energy_sensor_{nullptr};
|
||||
sensor::Sensor *apparent_power_sensor_{nullptr};
|
||||
sensor::Sensor *reactive_power_sensor_{nullptr};
|
||||
sensor::Sensor *power_factor_sensor_{nullptr};
|
||||
uint32_t cf_pulses_total_{0};
|
||||
uint16_t cf_pulses_last_{0};
|
||||
|
|
|
@ -8,18 +8,21 @@ from esphome.const import (
|
|||
CONF_ID,
|
||||
CONF_POWER,
|
||||
CONF_POWER_FACTOR,
|
||||
CONF_REACTIVE_POWER,
|
||||
CONF_VOLTAGE,
|
||||
DEVICE_CLASS_APPARENT_POWER,
|
||||
DEVICE_CLASS_CURRENT,
|
||||
DEVICE_CLASS_ENERGY,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_POWER_FACTOR,
|
||||
DEVICE_CLASS_REACTIVE_POWER,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
UNIT_AMPERE,
|
||||
UNIT_VOLT,
|
||||
UNIT_VOLT_AMPS,
|
||||
UNIT_VOLT_AMPS_REACTIVE,
|
||||
UNIT_WATT,
|
||||
UNIT_WATT_HOURS,
|
||||
)
|
||||
|
@ -62,6 +65,12 @@ CONFIG_SCHEMA = cv.Schema(
|
|||
device_class=DEVICE_CLASS_APPARENT_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_REACTIVE_POWER): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_REACTIVE_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_POWER_FACTOR,
|
||||
|
@ -94,6 +103,9 @@ async def to_code(config):
|
|||
if apparent_power_config := config.get(CONF_APPARENT_POWER):
|
||||
sens = await sensor.new_sensor(apparent_power_config)
|
||||
cg.add(var.set_apparent_power_sensor(sens))
|
||||
if reactive_power_config := config.get(CONF_REACTIVE_POWER):
|
||||
sens = await sensor.new_sensor(reactive_power_config)
|
||||
cg.add(var.set_reactive_power_sensor(sens))
|
||||
if power_factor_config := config.get(CONF_POWER_FACTOR):
|
||||
sens = await sensor.new_sensor(power_factor_config)
|
||||
cg.add(var.set_power_factor_sensor(sens))
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor, esp32_ble_server, output
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.const import CONF_ID, CONF_ON_STATE, CONF_TRIGGER_ID
|
||||
|
||||
AUTO_LOAD = ["esp32_ble_server"]
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
|
@ -11,13 +12,36 @@ CONF_AUTHORIZED_DURATION = "authorized_duration"
|
|||
CONF_AUTHORIZER = "authorizer"
|
||||
CONF_BLE_SERVER_ID = "ble_server_id"
|
||||
CONF_IDENTIFY_DURATION = "identify_duration"
|
||||
CONF_ON_PROVISIONED = "on_provisioned"
|
||||
CONF_ON_PROVISIONING = "on_provisioning"
|
||||
CONF_ON_START = "on_start"
|
||||
CONF_ON_STOP = "on_stop"
|
||||
CONF_STATUS_INDICATOR = "status_indicator"
|
||||
CONF_WIFI_TIMEOUT = "wifi_timeout"
|
||||
|
||||
improv_ns = cg.esphome_ns.namespace("improv")
|
||||
Error = improv_ns.enum("Error")
|
||||
State = improv_ns.enum("State")
|
||||
|
||||
esp32_improv_ns = cg.esphome_ns.namespace("esp32_improv")
|
||||
ESP32ImprovComponent = esp32_improv_ns.class_(
|
||||
"ESP32ImprovComponent", cg.Component, esp32_ble_server.BLEServiceComponent
|
||||
)
|
||||
ESP32ImprovProvisionedTrigger = esp32_improv_ns.class_(
|
||||
"ESP32ImprovProvisionedTrigger", automation.Trigger.template()
|
||||
)
|
||||
ESP32ImprovProvisioningTrigger = esp32_improv_ns.class_(
|
||||
"ESP32ImprovProvisioningTrigger", automation.Trigger.template()
|
||||
)
|
||||
ESP32ImprovStartTrigger = esp32_improv_ns.class_(
|
||||
"ESP32ImprovStartTrigger", automation.Trigger.template()
|
||||
)
|
||||
ESP32ImprovStateTrigger = esp32_improv_ns.class_(
|
||||
"ESP32ImprovStateTrigger", automation.Trigger.template()
|
||||
)
|
||||
ESP32ImprovStoppedTrigger = esp32_improv_ns.class_(
|
||||
"ESP32ImprovStoppedTrigger", automation.Trigger.template()
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
|
@ -37,6 +61,37 @@ CONFIG_SCHEMA = cv.Schema(
|
|||
cv.Optional(
|
||||
CONF_WIFI_TIMEOUT, default="1min"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_ON_PROVISIONED): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
ESP32ImprovProvisionedTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_PROVISIONING): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
ESP32ImprovProvisioningTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_START): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESP32ImprovStartTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_STATE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESP32ImprovStateTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_STOP): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
ESP32ImprovStoppedTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
@ -63,3 +118,29 @@ async def to_code(config):
|
|||
if CONF_STATUS_INDICATOR in config:
|
||||
status_indicator = await cg.get_variable(config[CONF_STATUS_INDICATOR])
|
||||
cg.add(var.set_status_indicator(status_indicator))
|
||||
|
||||
use_state_callback = False
|
||||
for conf in config.get(CONF_ON_PROVISIONED, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_PROVISIONING, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_START, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_STATE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger, [(State, "state"), (Error, "error")], conf
|
||||
)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_STOP, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
if use_state_callback:
|
||||
cg.add_define("USE_ESP32_IMPROV_STATE_CALLBACK")
|
||||
|
|
72
esphome/components/esp32_improv/automation.h
Normal file
72
esphome/components/esp32_improv/automation.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
#pragma once
|
||||
#ifdef USE_ESP32
|
||||
#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
#include "esp32_improv_component.h"
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
|
||||
#include <improv.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_improv {
|
||||
|
||||
class ESP32ImprovProvisionedTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit ESP32ImprovProvisionedTrigger(ESP32ImprovComponent *parent) {
|
||||
parent->add_on_state_callback([this, parent](improv::State state, improv::Error error) {
|
||||
if (state == improv::STATE_PROVISIONED && !parent->is_failed()) {
|
||||
trigger();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class ESP32ImprovProvisioningTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit ESP32ImprovProvisioningTrigger(ESP32ImprovComponent *parent) {
|
||||
parent->add_on_state_callback([this, parent](improv::State state, improv::Error error) {
|
||||
if (state == improv::STATE_PROVISIONING && !parent->is_failed()) {
|
||||
trigger();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class ESP32ImprovStartTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit ESP32ImprovStartTrigger(ESP32ImprovComponent *parent) {
|
||||
parent->add_on_state_callback([this, parent](improv::State state, improv::Error error) {
|
||||
if ((state == improv::STATE_AUTHORIZED || state == improv::STATE_AWAITING_AUTHORIZATION) &&
|
||||
!parent->is_failed()) {
|
||||
trigger();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class ESP32ImprovStateTrigger : public Trigger<improv::State, improv::Error> {
|
||||
public:
|
||||
explicit ESP32ImprovStateTrigger(ESP32ImprovComponent *parent) {
|
||||
parent->add_on_state_callback([this, parent](improv::State state, improv::Error error) {
|
||||
if (!parent->is_failed()) {
|
||||
trigger(state, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class ESP32ImprovStoppedTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit ESP32ImprovStoppedTrigger(ESP32ImprovComponent *parent) {
|
||||
parent->add_on_state_callback([this, parent](improv::State state, improv::Error error) {
|
||||
if (state == improv::STATE_STOPPED && !parent->is_failed()) {
|
||||
trigger();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace esp32_improv
|
||||
} // namespace esphome
|
||||
#endif
|
||||
#endif
|
|
@ -68,7 +68,12 @@ void ESP32ImprovComponent::setup_characteristics() {
|
|||
|
||||
void ESP32ImprovComponent::loop() {
|
||||
if (!global_ble_server->is_running()) {
|
||||
this->state_ = improv::STATE_STOPPED;
|
||||
if (this->state_ != improv::STATE_STOPPED) {
|
||||
this->state_ = improv::STATE_STOPPED;
|
||||
#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
this->state_callback_.call(this->state_, this->error_state_);
|
||||
#endif
|
||||
}
|
||||
this->incoming_data_.clear();
|
||||
return;
|
||||
}
|
||||
|
@ -217,6 +222,9 @@ void ESP32ImprovComponent::set_state_(improv::State state) {
|
|||
service_data[7] = 0x00; // Reserved
|
||||
|
||||
esp32_ble::global_ble->advertising_set_service_data(service_data);
|
||||
#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
this->state_callback_.call(this->state_, this->error_state_);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ESP32ImprovComponent::set_error_(improv::Error error) {
|
||||
|
@ -270,7 +278,7 @@ void ESP32ImprovComponent::dump_config() {
|
|||
void ESP32ImprovComponent::process_incoming_data_() {
|
||||
uint8_t length = this->incoming_data_[1];
|
||||
|
||||
ESP_LOGD(TAG, "Processing bytes - %s", format_hex_pretty(this->incoming_data_).c_str());
|
||||
ESP_LOGV(TAG, "Processing bytes - %s", format_hex_pretty(this->incoming_data_).c_str());
|
||||
if (this->incoming_data_.size() - 3 == length) {
|
||||
this->set_error_(improv::ERROR_NONE);
|
||||
improv::ImprovCommand command = improv::parse_improv_data(this->incoming_data_);
|
||||
|
@ -295,7 +303,7 @@ void ESP32ImprovComponent::process_incoming_data_() {
|
|||
wifi::global_wifi_component->set_sta(sta);
|
||||
wifi::global_wifi_component->start_connecting(sta, false);
|
||||
this->set_state_(improv::STATE_PROVISIONING);
|
||||
ESP_LOGD(TAG, "Received Improv wifi settings ssid=%s, password=" LOG_SECRET("%s"), command.ssid.c_str(),
|
||||
ESP_LOGD(TAG, "Received Improv Wi-Fi settings ssid=%s, password=" LOG_SECRET("%s"), command.ssid.c_str(),
|
||||
command.password.c_str());
|
||||
|
||||
auto f = std::bind(&ESP32ImprovComponent::on_wifi_connect_timeout_, this);
|
||||
|
@ -313,7 +321,7 @@ void ESP32ImprovComponent::process_incoming_data_() {
|
|||
this->incoming_data_.clear();
|
||||
}
|
||||
} else if (this->incoming_data_.size() - 2 > length) {
|
||||
ESP_LOGV(TAG, "Too much data came in, or malformed resetting buffer...");
|
||||
ESP_LOGV(TAG, "Too much data received or data malformed; resetting buffer...");
|
||||
this->incoming_data_.clear();
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Waiting for split data packets...");
|
||||
|
@ -327,7 +335,7 @@ void ESP32ImprovComponent::on_wifi_connect_timeout_() {
|
|||
if (this->authorizer_ != nullptr)
|
||||
this->authorized_start_ = millis();
|
||||
#endif
|
||||
ESP_LOGW(TAG, "Timed out trying to connect to given WiFi network");
|
||||
ESP_LOGW(TAG, "Timed out while connecting to Wi-Fi network");
|
||||
wifi::global_wifi_component->clear_sta();
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,10 @@
|
|||
#include "esphome/components/esp32_ble_server/ble_server.h"
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
|
||||
#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
#include "esphome/core/automation.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#endif
|
||||
|
@ -42,6 +46,11 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
|
|||
void stop() override;
|
||||
bool is_active() const { return this->state_ != improv::STATE_STOPPED; }
|
||||
|
||||
#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
void add_on_state_callback(std::function<void(improv::State, improv::Error)> &&callback) {
|
||||
this->state_callback_.add(std::move(callback));
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void set_authorizer(binary_sensor::BinarySensor *authorizer) { this->authorizer_ = authorizer; }
|
||||
#endif
|
||||
|
@ -54,6 +63,9 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
|
|||
void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
|
||||
uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
|
||||
|
||||
improv::State get_improv_state() const { return this->state_; }
|
||||
improv::Error get_improv_error_state() const { return this->error_state_; }
|
||||
|
||||
protected:
|
||||
bool should_start_{false};
|
||||
bool setup_complete_{false};
|
||||
|
@ -84,6 +96,9 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
|
|||
|
||||
improv::State state_{improv::STATE_STOPPED};
|
||||
improv::Error error_state_{improv::ERROR_NONE};
|
||||
#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
CallbackManager<void(improv::State, improv::Error)> state_callback_{};
|
||||
#endif
|
||||
|
||||
bool status_indicator_state_{false};
|
||||
void set_status_indicator_state_(bool state);
|
||||
|
|
|
@ -53,6 +53,8 @@ MODELS = {
|
|||
"inkplate_10": InkplateModel.INKPLATE_10,
|
||||
"inkplate_6_plus": InkplateModel.INKPLATE_6_PLUS,
|
||||
"inkplate_6_v2": InkplateModel.INKPLATE_6_V2,
|
||||
"inkplate_5": InkplateModel.INKPLATE_5,
|
||||
"inkplate_5_v2": InkplateModel.INKPLATE_5_V2,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
|
|
|
@ -15,6 +15,8 @@ enum InkplateModel : uint8_t {
|
|||
INKPLATE_10 = 1,
|
||||
INKPLATE_6_PLUS = 2,
|
||||
INKPLATE_6_V2 = 3,
|
||||
INKPLATE_5 = 4,
|
||||
INKPLATE_5_V2 = 5,
|
||||
};
|
||||
|
||||
class Inkplate6 : public display::DisplayBuffer, public i2c::I2CDevice {
|
||||
|
@ -29,7 +31,7 @@ class Inkplate6 : public display::DisplayBuffer, public i2c::I2CDevice {
|
|||
const uint8_t pixelMaskLUT[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
|
||||
const uint8_t pixelMaskGLUT[2] = {0x0F, 0xF0};
|
||||
|
||||
const uint8_t waveform3BitAll[4][8][9] = {// INKPLATE_6
|
||||
const uint8_t waveform3BitAll[6][8][9] = {// INKPLATE_6
|
||||
{{0, 1, 1, 0, 0, 1, 1, 0, 0},
|
||||
{0, 1, 2, 1, 1, 2, 1, 0, 0},
|
||||
{1, 1, 1, 2, 2, 1, 0, 0, 0},
|
||||
|
@ -64,7 +66,25 @@ class Inkplate6 : public display::DisplayBuffer, public i2c::I2CDevice {
|
|||
{1, 1, 1, 1, 2, 2, 1, 0, 0},
|
||||
{0, 1, 1, 1, 2, 2, 1, 0, 0},
|
||||
{0, 0, 0, 0, 1, 1, 2, 0, 0},
|
||||
{0, 0, 0, 0, 0, 1, 2, 0, 0}}};
|
||||
{0, 0, 0, 0, 0, 1, 2, 0, 0}},
|
||||
// INKPLATE_5
|
||||
{{0, 0, 1, 1, 0, 1, 1, 1, 0},
|
||||
{0, 1, 1, 1, 1, 2, 0, 1, 0},
|
||||
{1, 2, 2, 0, 2, 1, 1, 1, 0},
|
||||
{1, 1, 1, 2, 0, 1, 1, 2, 0},
|
||||
{0, 1, 1, 1, 2, 0, 1, 2, 0},
|
||||
{0, 0, 0, 1, 1, 2, 1, 2, 0},
|
||||
{1, 1, 1, 2, 0, 2, 1, 2, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0}},
|
||||
// INKPLATE_5_V2
|
||||
{{0, 0, 1, 1, 2, 1, 1, 1, 0},
|
||||
{1, 1, 2, 2, 1, 2, 1, 1, 0},
|
||||
{0, 1, 2, 2, 1, 1, 2, 1, 0},
|
||||
{0, 0, 1, 1, 1, 1, 1, 2, 0},
|
||||
{1, 2, 1, 2, 1, 1, 1, 2, 0},
|
||||
{0, 1, 1, 1, 2, 0, 1, 2, 0},
|
||||
{1, 1, 1, 2, 2, 2, 1, 2, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0}}};
|
||||
|
||||
void set_greyscale(bool greyscale) {
|
||||
this->greyscale_ = greyscale;
|
||||
|
@ -146,6 +166,10 @@ class Inkplate6 : public display::DisplayBuffer, public i2c::I2CDevice {
|
|||
return 800;
|
||||
} else if (this->model_ == INKPLATE_10) {
|
||||
return 1200;
|
||||
} else if (this->model_ == INKPLATE_5) {
|
||||
return 960;
|
||||
} else if (this->model_ == INKPLATE_5_V2) {
|
||||
return 1280;
|
||||
} else if (this->model_ == INKPLATE_6_PLUS) {
|
||||
return 1024;
|
||||
}
|
||||
|
@ -155,6 +179,10 @@ class Inkplate6 : public display::DisplayBuffer, public i2c::I2CDevice {
|
|||
int get_height_internal() override {
|
||||
if (this->model_ == INKPLATE_6 || this->model_ == INKPLATE_6_V2) {
|
||||
return 600;
|
||||
} else if (this->model_ == INKPLATE_5) {
|
||||
return 540;
|
||||
} else if (this->model_ == INKPLATE_5_V2) {
|
||||
return 720;
|
||||
} else if (this->model_ == INKPLATE_10) {
|
||||
return 825;
|
||||
} else if (this->model_ == INKPLATE_6_PLUS) {
|
||||
|
|
|
@ -419,6 +419,13 @@ async def to_code(config):
|
|||
repo="https://github.com/espressif/esp-tflite-micro",
|
||||
ref="v1.3.1",
|
||||
)
|
||||
# add esp-nn dependency for tflite-micro to work around https://github.com/espressif/esp-nn/issues/17
|
||||
# ...remove after switching to IDF 5.1.4+
|
||||
esp32.add_idf_component(
|
||||
name="esp-nn",
|
||||
repo="https://github.com/espressif/esp-nn",
|
||||
ref="v1.1.0",
|
||||
)
|
||||
|
||||
cg.add_build_flag("-DTF_LITE_STATIC_MEMORY")
|
||||
cg.add_build_flag("-DTF_LITE_DISABLE_X86_NEON")
|
||||
|
|
|
@ -11,6 +11,7 @@ from esphome.const import (
|
|||
CONF_BIRTH_MESSAGE,
|
||||
CONF_BROKER,
|
||||
CONF_CERTIFICATE_AUTHORITY,
|
||||
CONF_CLEAN_SESSION,
|
||||
CONF_CLIENT_CERTIFICATE,
|
||||
CONF_CLIENT_CERTIFICATE_KEY,
|
||||
CONF_CLIENT_ID,
|
||||
|
@ -209,6 +210,7 @@ CONFIG_SCHEMA = cv.All(
|
|||
cv.Optional(CONF_PORT, default=1883): cv.port,
|
||||
cv.Optional(CONF_USERNAME, 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.SplitDefault(CONF_IDF_SEND_ASYNC, esp32_idf=False): cv.All(
|
||||
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_username(config[CONF_USERNAME]))
|
||||
cg.add(var.set_password(config[CONF_PASSWORD]))
|
||||
cg.add(var.set_clean_session(config[CONF_CLEAN_SESSION]))
|
||||
if CONF_CLIENT_ID in config:
|
||||
cg.add(var.set_client_id(config[CONF_CLIENT_ID]))
|
||||
|
||||
|
|
|
@ -147,6 +147,7 @@ void MQTTClientComponent::dump_config() {
|
|||
this->ip_.str().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, " Clean Session: %s", YESNO(this->credentials_.clean_session));
|
||||
if (this->is_discovery_ip_enabled()) {
|
||||
ESP_LOGCONFIG(TAG, " Discovery IP enabled");
|
||||
}
|
||||
|
@ -246,6 +247,7 @@ void MQTTClientComponent::start_connect_() {
|
|||
this->mqtt_backend_.disconnect();
|
||||
|
||||
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;
|
||||
if (!this->credentials_.username.empty())
|
||||
username = this->credentials_.username.c_str();
|
||||
|
|
|
@ -51,6 +51,7 @@ struct MQTTCredentials {
|
|||
std::string username;
|
||||
std::string password;
|
||||
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.
|
||||
|
@ -254,6 +255,7 @@ class MQTTClientComponent : public Component {
|
|||
void set_username(const std::string &username) { this->credentials_.username = username; }
|
||||
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_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_disconnect(mqtt_on_disconnect_callback_t &&callback);
|
||||
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32_rmt, remote_base
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import remote_base, esp32_rmt
|
||||
from esphome.const import CONF_CARRIER_DUTY_PERCENT, CONF_ID, CONF_PIN, CONF_RMT_CHANNEL
|
||||
|
||||
AUTO_LOAD = ["remote_base"]
|
||||
|
||||
CONF_ON_TRANSMIT = "on_transmit"
|
||||
CONF_ON_COMPLETE = "on_complete"
|
||||
|
||||
remote_transmitter_ns = cg.esphome_ns.namespace("remote_transmitter")
|
||||
RemoteTransmitterComponent = remote_transmitter_ns.class_(
|
||||
"RemoteTransmitterComponent", remote_base.RemoteTransmitterBase, cg.Component
|
||||
|
@ -19,6 +23,8 @@ CONFIG_SCHEMA = cv.Schema(
|
|||
cv.percentage_int, cv.Range(min=1, max=100)
|
||||
),
|
||||
cv.Optional(CONF_RMT_CHANNEL): esp32_rmt.validate_rmt_channel(tx=True),
|
||||
cv.Optional(CONF_ON_TRANSMIT): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_ON_COMPLETE): automation.validate_automation(single=True),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
@ -32,3 +38,13 @@ async def to_code(config):
|
|||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_carrier_duty_percent(config[CONF_CARRIER_DUTY_PERCENT]))
|
||||
|
||||
if on_transmit_config := config.get(CONF_ON_TRANSMIT):
|
||||
await automation.build_automation(
|
||||
var.get_transmit_trigger(), [], on_transmit_config
|
||||
)
|
||||
|
||||
if on_complete_config := config.get(CONF_ON_COMPLETE):
|
||||
await automation.build_automation(
|
||||
var.get_complete_trigger(), [], on_complete_config
|
||||
)
|
||||
|
|
|
@ -33,6 +33,9 @@ class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase,
|
|||
|
||||
void set_carrier_duty_percent(uint8_t carrier_duty_percent) { this->carrier_duty_percent_ = carrier_duty_percent; }
|
||||
|
||||
Trigger<> *get_transmit_trigger() const { return this->transmit_trigger_; };
|
||||
Trigger<> *get_complete_trigger() const { return this->complete_trigger_; };
|
||||
|
||||
protected:
|
||||
void send_internal(uint32_t send_times, uint32_t send_wait) override;
|
||||
#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
|
||||
|
@ -57,6 +60,9 @@ class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase,
|
|||
bool inverted_{false};
|
||||
#endif
|
||||
uint8_t carrier_duty_percent_;
|
||||
|
||||
Trigger<> *transmit_trigger_{new Trigger<>()};
|
||||
Trigger<> *complete_trigger_{new Trigger<>()};
|
||||
};
|
||||
|
||||
} // namespace remote_transmitter
|
||||
|
|
|
@ -124,6 +124,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||
ESP_LOGE(TAG, "Empty data");
|
||||
return;
|
||||
}
|
||||
this->transmit_trigger_->trigger();
|
||||
for (uint32_t i = 0; i < send_times; i++) {
|
||||
esp_err_t error = rmt_write_items(this->channel_, this->rmt_temp_.data(), this->rmt_temp_.size(), true);
|
||||
if (error != ESP_OK) {
|
||||
|
@ -135,6 +136,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||
if (i + 1 < send_times)
|
||||
delayMicroseconds(send_wait);
|
||||
}
|
||||
this->complete_trigger_->trigger();
|
||||
}
|
||||
|
||||
} // namespace remote_transmitter
|
||||
|
|
|
@ -76,6 +76,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||
uint32_t on_time, off_time;
|
||||
this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
|
||||
this->target_time_ = 0;
|
||||
this->transmit_trigger_->trigger();
|
||||
for (uint32_t i = 0; i < send_times; i++) {
|
||||
for (int32_t item : this->temp_.get_data()) {
|
||||
if (item > 0) {
|
||||
|
@ -93,6 +94,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||
if (i + 1 < send_times)
|
||||
this->target_time_ += send_wait;
|
||||
}
|
||||
this->complete_trigger_->trigger();
|
||||
}
|
||||
|
||||
} // namespace remote_transmitter
|
||||
|
|
|
@ -78,6 +78,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||
uint32_t on_time, off_time;
|
||||
this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
|
||||
this->target_time_ = 0;
|
||||
this->transmit_trigger_->trigger();
|
||||
for (uint32_t i = 0; i < send_times; i++) {
|
||||
InterruptLock lock;
|
||||
for (int32_t item : this->temp_.get_data()) {
|
||||
|
@ -96,6 +97,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||
if (i + 1 < send_times)
|
||||
this->target_time_ += send_wait;
|
||||
}
|
||||
this->complete_trigger_->trigger();
|
||||
}
|
||||
|
||||
} // namespace remote_transmitter
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "tcs34725.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace esphome {
|
||||
namespace tcs34725 {
|
||||
|
@ -211,7 +212,7 @@ void TCS34725Component::update() {
|
|||
if (raw_c == 0) {
|
||||
channel_c = channel_r = channel_g = channel_b = 0.0f;
|
||||
} else {
|
||||
float max_count = this->integration_time_ * 1024.0f / 2.4;
|
||||
float max_count = this->integration_time_ <= 153.6f ? this->integration_time_ * 1024.0f / 2.4f : 65535.0f;
|
||||
float sum = raw_c;
|
||||
channel_r = raw_r / sum * 100.0f;
|
||||
channel_g = raw_g / sum * 100.0f;
|
||||
|
@ -254,7 +255,8 @@ void TCS34725Component::update() {
|
|||
// change integration time an gain to achieve maximum resolution an dynamic range
|
||||
// calculate optimal integration time to achieve 70% satuaration
|
||||
float integration_time_ideal;
|
||||
integration_time_ideal = 60 / ((float) raw_c / 655.35) * this->integration_time_;
|
||||
|
||||
integration_time_ideal = 60 / ((float) std::max((uint16_t) 1, raw_c) / 655.35f) * this->integration_time_;
|
||||
|
||||
uint8_t gain_reg_val_new = this->gain_reg_;
|
||||
// increase gain if less than 20% of white channel used and high integration time
|
||||
|
|
|
@ -1012,6 +1012,16 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url
|
|||
call.set_mode(mode.c_str());
|
||||
}
|
||||
|
||||
if (request->hasParam("fan_mode")) {
|
||||
auto mode = request->getParam("fan_mode")->value();
|
||||
call.set_fan_mode(mode.c_str());
|
||||
}
|
||||
|
||||
if (request->hasParam("swing_mode")) {
|
||||
auto mode = request->getParam("swing_mode")->value();
|
||||
call.set_swing_mode(mode.c_str());
|
||||
}
|
||||
|
||||
if (request->hasParam("target_temperature_high")) {
|
||||
auto target_temperature_high = parse_number<float>(request->getParam("target_temperature_high")->value().c_str());
|
||||
if (target_temperature_high.has_value())
|
||||
|
|
|
@ -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_() {
|
||||
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
|
||||
uint8_t mac[6];
|
||||
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
|
||||
get_mac_address_raw(mac);
|
||||
set_mac_address(mac);
|
||||
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
|
||||
esp_err_t err = esp_netif_init();
|
||||
if (err != ERR_OK) {
|
||||
|
|
|
@ -122,6 +122,7 @@ CONF_CHANNELS = "channels"
|
|||
CONF_CHARACTERISTIC_UUID = "characteristic_uuid"
|
||||
CONF_CHECK = "check"
|
||||
CONF_CHIPSET = "chipset"
|
||||
CONF_CLEAN_SESSION = "clean_session"
|
||||
CONF_CLEAR_IMPEDANCE = "clear_impedance"
|
||||
CONF_CLIENT_CERTIFICATE = "client_certificate"
|
||||
CONF_CLIENT_CERTIFICATE_KEY = "client_certificate_key"
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#define USE_DATETIME_TIME
|
||||
#define USE_DEEP_SLEEP
|
||||
#define USE_DISPLAY
|
||||
#define USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
#define USE_EVENT
|
||||
#define USE_FAN
|
||||
#define USE_GRAPH
|
||||
|
@ -42,10 +43,10 @@
|
|||
#define USE_LVGL_BUTTONMATRIX
|
||||
#define USE_LVGL_FONT
|
||||
#define USE_LVGL_IMAGE
|
||||
#define USE_LVGL_KEYBOARD
|
||||
#define USE_LVGL_KEY_LISTENER
|
||||
#define USE_LVGL_TOUCHSCREEN
|
||||
#define USE_LVGL_KEYBOARD
|
||||
#define USE_LVGL_ROTARY_ENCODER
|
||||
#define USE_LVGL_TOUCHSCREEN
|
||||
#define USE_MDNS
|
||||
#define USE_MEDIA_PLAYER
|
||||
#define USE_NEXTION_TFT_UPLOAD
|
||||
|
|
|
@ -686,9 +686,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
|
||||
// work-around reads and uses the MAC address as-is from EFuse,
|
||||
// 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
|
||||
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
|
||||
#elif defined(USE_ESP8266)
|
||||
wifi_get_macaddr(STATION_IF, mac);
|
||||
|
@ -707,20 +715,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
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string get_mac_address() {
|
||||
uint8_t mac[6];
|
||||
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]);
|
||||
}
|
||||
|
||||
std::string get_mac_address_pretty() {
|
||||
uint8_t mac[6];
|
||||
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]);
|
||||
}
|
||||
|
||||
#ifdef USE_ESP32
|
||||
void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); }
|
||||
#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
|
||||
uint32_t start = micros();
|
||||
|
||||
|
|
|
@ -641,6 +641,14 @@ std::string get_mac_address_pretty();
|
|||
void set_mac_address(uint8_t *mac);
|
||||
#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.
|
||||
void delay_microseconds_safe(uint32_t us);
|
||||
|
||||
|
|
|
@ -11,16 +11,26 @@ namespace esphome {
|
|||
|
||||
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> rb = make_unique<RingBuffer>();
|
||||
|
||||
rb->size_ = len + 1;
|
||||
|
||||
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) {
|
||||
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);
|
||||
return rb;
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ namespace esphome {
|
|||
|
||||
class RingBuffer {
|
||||
public:
|
||||
~RingBuffer();
|
||||
|
||||
/**
|
||||
* @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_;
|
||||
StaticStreamBuffer_t structure_;
|
||||
uint8_t *storage_;
|
||||
size_t size_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
|
|
@ -226,4 +226,6 @@ class _Schema(vol.Schema):
|
|||
if isinstance(schema, vol.Schema):
|
||||
schema = schema.schema
|
||||
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()
|
||||
)
|
||||
|
|
|
@ -8,6 +8,7 @@ uart:
|
|||
|
||||
sensor:
|
||||
- platform: bl0906
|
||||
id: bl
|
||||
frequency:
|
||||
name: 'Frequency'
|
||||
temperature:
|
||||
|
@ -60,3 +61,9 @@ sensor:
|
|||
name: 'Total_Energy'
|
||||
total_power:
|
||||
name: 'Total_Power'
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
id: reset
|
||||
on_press:
|
||||
- bl0906.reset_energy: bl
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
ch422g:
|
||||
- id: ch422g_hub
|
||||
address: 0x24
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
|
@ -11,10 +10,18 @@ binary_sensor:
|
|||
number: 1
|
||||
mode: INPUT
|
||||
inverted: true
|
||||
output:
|
||||
- platform: gpio
|
||||
id: ch422g_output
|
||||
id: ch422_out_0
|
||||
pin:
|
||||
ch422g: ch422g_hub
|
||||
number: 0
|
||||
mode: OUTPUT
|
||||
inverted: false
|
||||
- platform: gpio
|
||||
id: ch422_out_11
|
||||
pin:
|
||||
ch422g: ch422g_hub
|
||||
number: 11
|
||||
mode: OUTPUT_OPEN_DRAIN
|
||||
inverted: true
|
||||
|
|
|
@ -10,6 +10,7 @@ mqtt:
|
|||
port: 1883
|
||||
username: debug
|
||||
password: debug
|
||||
clean_session: True
|
||||
client_id: someclient
|
||||
use_abbreviations: false
|
||||
discovery: true
|
||||
|
|
Loading…
Reference in a new issue