Merge branch 'dev' into remove_dependency

This commit is contained in:
tomaszduda23 2024-09-07 12:54:44 +02:00 committed by GitHub
commit f098d3125a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
81 changed files with 1139 additions and 288 deletions

View file

@ -65,7 +65,7 @@ jobs:
pip3 install build pip3 install build
python3 -m build python3 -m build
- name: Publish - name: Publish
uses: pypa/gh-action-pypi-publish@v1.9.0 uses: pypa/gh-action-pypi-publish@v1.10.1
deploy-docker: deploy-docker:
name: Build ESPHome ${{ matrix.platform }} name: Build ESPHome ${{ matrix.platform }}

View file

@ -36,7 +36,7 @@ jobs:
python ./script/sync-device_class.py python ./script/sync-device_class.py
- name: Commit changes - name: Commit changes
uses: peter-evans/create-pull-request@v6.1.0 uses: peter-evans/create-pull-request@v7.0.0
with: with:
commit-message: "Synchronise Device Classes from Home Assistant" commit-message: "Synchronise Device Classes from Home Assistant"
committer: esphomebot <esphome@nabucasa.com> committer: esphomebot <esphome@nabucasa.com>

View file

@ -61,7 +61,7 @@ esphome/components/bk72xx/* @kuba2k2
esphome/components/bl0906/* @athom-tech @jesserockz @tarontop esphome/components/bl0906/* @athom-tech @jesserockz @tarontop
esphome/components/bl0939/* @ziceva esphome/components/bl0939/* @ziceva
esphome/components/bl0940/* @tobias- esphome/components/bl0940/* @tobias-
esphome/components/bl0942/* @dbuezas esphome/components/bl0942/* @dbuezas @dwmw2
esphome/components/ble_client/* @buxtronix @clydebarrow esphome/components/ble_client/* @buxtronix @clydebarrow
esphome/components/bluetooth_proxy/* @jesserockz esphome/components/bluetooth_proxy/* @jesserockz
esphome/components/bme280_base/* @esphome/core esphome/components/bme280_base/* @esphome/core
@ -70,6 +70,9 @@ esphome/components/bme680_bsec/* @trvrnrth
esphome/components/bme68x_bsec2/* @kbx81 @neffs esphome/components/bme68x_bsec2/* @kbx81 @neffs
esphome/components/bme68x_bsec2_i2c/* @kbx81 @neffs esphome/components/bme68x_bsec2_i2c/* @kbx81 @neffs
esphome/components/bmi160/* @flaviut esphome/components/bmi160/* @flaviut
esphome/components/bmp280_base/* @ademuri
esphome/components/bmp280_i2c/* @ademuri
esphome/components/bmp280_spi/* @ademuri
esphome/components/bmp3xx/* @latonita esphome/components/bmp3xx/* @latonita
esphome/components/bmp3xx_base/* @latonita @martgras esphome/components/bmp3xx_base/* @latonita @martgras
esphome/components/bmp3xx_i2c/* @latonita esphome/components/bmp3xx_i2c/* @latonita
@ -385,6 +388,7 @@ esphome/components/st7701s/* @clydebarrow
esphome/components/st7735/* @SenexCrenshaw esphome/components/st7735/* @SenexCrenshaw
esphome/components/st7789v/* @kbx81 esphome/components/st7789v/* @kbx81
esphome/components/st7920/* @marsjan155 esphome/components/st7920/* @marsjan155
esphome/components/statsd/* @Links2004
esphome/components/substitutions/* @esphome/core esphome/components/substitutions/* @esphome/core
esphome/components/sun/* @OttoWinter esphome/components/sun/* @OttoWinter
esphome/components/sun_gtil2/* @Mat931 esphome/components/sun_gtil2/* @Mat931

View file

@ -1112,7 +1112,6 @@ enum MediaPlayerFormatPurpose {
MEDIA_PLAYER_FORMAT_PURPOSE_ANNOUNCEMENT = 1; MEDIA_PLAYER_FORMAT_PURPOSE_ANNOUNCEMENT = 1;
} }
message MediaPlayerSupportedFormat { message MediaPlayerSupportedFormat {
option (id) = 119;
option (ifdef) = "USE_MEDIA_PLAYER"; option (ifdef) = "USE_MEDIA_PLAYER";
string format = 1; string format = 1;

View file

@ -311,14 +311,6 @@ bool APIServerConnectionBase::send_list_entities_button_response(const ListEntit
#ifdef USE_BUTTON #ifdef USE_BUTTON
#endif #endif
#ifdef USE_MEDIA_PLAYER #ifdef USE_MEDIA_PLAYER
bool APIServerConnectionBase::send_media_player_supported_format(const MediaPlayerSupportedFormat &msg) {
#ifdef HAS_PROTO_MESSAGE_DUMP
ESP_LOGVV(TAG, "send_media_player_supported_format: %s", msg.dump().c_str());
#endif
return this->send_message_<MediaPlayerSupportedFormat>(msg, 119);
}
#endif
#ifdef USE_MEDIA_PLAYER
bool APIServerConnectionBase::send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg) { bool APIServerConnectionBase::send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg) {
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
ESP_LOGVV(TAG, "send_list_entities_media_player_response: %s", msg.dump().c_str()); ESP_LOGVV(TAG, "send_list_entities_media_player_response: %s", msg.dump().c_str());
@ -1143,17 +1135,6 @@ bool APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
ESP_LOGVV(TAG, "on_update_command_request: %s", msg.dump().c_str()); ESP_LOGVV(TAG, "on_update_command_request: %s", msg.dump().c_str());
#endif #endif
this->on_update_command_request(msg); this->on_update_command_request(msg);
#endif
break;
}
case 119: {
#ifdef USE_MEDIA_PLAYER
MediaPlayerSupportedFormat msg;
msg.decode(msg_data, msg_size);
#ifdef HAS_PROTO_MESSAGE_DUMP
ESP_LOGVV(TAG, "on_media_player_supported_format: %s", msg.dump().c_str());
#endif
this->on_media_player_supported_format(msg);
#endif #endif
break; break;
} }

View file

@ -145,10 +145,6 @@ class APIServerConnectionBase : public ProtoService {
#ifdef USE_BUTTON #ifdef USE_BUTTON
virtual void on_button_command_request(const ButtonCommandRequest &value){}; virtual void on_button_command_request(const ButtonCommandRequest &value){};
#endif #endif
#ifdef USE_MEDIA_PLAYER
bool send_media_player_supported_format(const MediaPlayerSupportedFormat &msg);
virtual void on_media_player_supported_format(const MediaPlayerSupportedFormat &value){};
#endif
#ifdef USE_MEDIA_PLAYER #ifdef USE_MEDIA_PLAYER
bool send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg); bool send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg);
#endif #endif

View file

@ -1 +1 @@
CODEOWNERS = ["@dbuezas"] CODEOWNERS = ["@dbuezas", "@dwmw2"]

View file

@ -41,20 +41,33 @@ static const uint32_t BL0942_REG_MODE_DEFAULT =
static const uint32_t BL0942_REG_SOFT_RESET_MAGIC = 0x5a5a5a; static const uint32_t BL0942_REG_SOFT_RESET_MAGIC = 0x5a5a5a;
static const uint32_t BL0942_REG_USR_WRPROT_MAGIC = 0x55; static const uint32_t BL0942_REG_USR_WRPROT_MAGIC = 0x55;
// 23-byte packet, 11 bits per byte, 2400 baud: about 105ms
static const uint32_t PKT_TIMEOUT_MS = 200;
void BL0942::loop() { void BL0942::loop() {
DataPacket buffer; DataPacket buffer;
if (!this->available()) { int avail = this->available();
if (!avail) {
return; return;
} }
if (avail < sizeof(buffer)) {
if (!this->rx_start_) {
this->rx_start_ = millis();
} else if (millis() > this->rx_start_ + PKT_TIMEOUT_MS) {
ESP_LOGW(TAG, "Junk on wire. Throwing away partial message (%d bytes)", avail);
this->read_array((uint8_t *) &buffer, avail);
this->rx_start_ = 0;
}
return;
}
if (this->read_array((uint8_t *) &buffer, sizeof(buffer))) { if (this->read_array((uint8_t *) &buffer, sizeof(buffer))) {
if (this->validate_checksum_(&buffer)) { if (this->validate_checksum_(&buffer)) {
this->received_package_(&buffer); this->received_package_(&buffer);
} }
} else {
ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
while (read() >= 0)
;
} }
this->rx_start_ = 0;
} }
bool BL0942::validate_checksum_(DataPacket *data) { bool BL0942::validate_checksum_(DataPacket *data) {
@ -109,6 +122,20 @@ void BL0942::update() {
} }
void BL0942::setup() { void BL0942::setup() {
// If either current or voltage references are set explicitly by the user,
// calculate the power reference from it unless that is also explicitly set.
if ((this->current_reference_set_ || this->voltage_reference_set_) && !this->power_reference_set_) {
this->power_reference_ = (this->voltage_reference_ * this->current_reference_ * 3537.0 / 305978.0) / 73989.0;
this->power_reference_set_ = true;
}
// Similarly for energy reference, if the power reference was set by the user
// either implicitly or explicitly.
if (this->power_reference_set_ && !this->energy_reference_set_) {
this->energy_reference_ = this->power_reference_ * 3600000 / 419430.4;
this->energy_reference_set_ = true;
}
this->write_reg_(BL0942_REG_USR_WRPROT, BL0942_REG_USR_WRPROT_MAGIC); this->write_reg_(BL0942_REG_USR_WRPROT, BL0942_REG_USR_WRPROT_MAGIC);
this->write_reg_(BL0942_REG_SOFT_RESET, BL0942_REG_SOFT_RESET_MAGIC); this->write_reg_(BL0942_REG_SOFT_RESET, BL0942_REG_SOFT_RESET_MAGIC);
@ -133,10 +160,17 @@ void BL0942::received_package_(DataPacket *data) {
return; return;
} }
// cf_cnt is only 24 bits, so track overflows
uint32_t cf_cnt = (uint24_t) data->cf_cnt;
cf_cnt |= this->prev_cf_cnt_ & 0xff000000;
if (cf_cnt < this->prev_cf_cnt_) {
cf_cnt += 0x1000000;
}
this->prev_cf_cnt_ = cf_cnt;
float v_rms = (uint24_t) data->v_rms / voltage_reference_; float v_rms = (uint24_t) data->v_rms / voltage_reference_;
float i_rms = (uint24_t) data->i_rms / current_reference_; float i_rms = (uint24_t) data->i_rms / current_reference_;
float watt = (int24_t) data->watt / power_reference_; float watt = (int24_t) data->watt / power_reference_;
uint32_t cf_cnt = (uint24_t) data->cf_cnt;
float total_energy_consumption = cf_cnt / energy_reference_; float total_energy_consumption = cf_cnt / energy_reference_;
float frequency = 1000000.0f / data->frequency; float frequency = 1000000.0f / data->frequency;
@ -164,11 +198,15 @@ void BL0942::dump_config() { // NOLINT(readability-function-cognitive-complexit
ESP_LOGCONFIG(TAG, "BL0942:"); ESP_LOGCONFIG(TAG, "BL0942:");
ESP_LOGCONFIG(TAG, " Address: %d", this->address_); ESP_LOGCONFIG(TAG, " Address: %d", this->address_);
ESP_LOGCONFIG(TAG, " Nominal line frequency: %d Hz", this->line_freq_); ESP_LOGCONFIG(TAG, " Nominal line frequency: %d Hz", this->line_freq_);
ESP_LOGCONFIG(TAG, " Current reference: %f", this->current_reference_);
ESP_LOGCONFIG(TAG, " Energy reference: %f", this->energy_reference_);
ESP_LOGCONFIG(TAG, " Power reference: %f", this->power_reference_);
ESP_LOGCONFIG(TAG, " Voltage reference: %f", this->voltage_reference_);
LOG_SENSOR("", "Voltage", this->voltage_sensor_); LOG_SENSOR("", "Voltage", this->voltage_sensor_);
LOG_SENSOR("", "Current", this->current_sensor_); LOG_SENSOR("", "Current", this->current_sensor_);
LOG_SENSOR("", "Power", this->power_sensor_); LOG_SENSOR("", "Power", this->power_sensor_);
LOG_SENSOR("", "Energy", this->energy_sensor_); LOG_SENSOR("", "Energy", this->energy_sensor_);
LOG_SENSOR("", "frequency", this->frequency_sensor_); LOG_SENSOR("", "Frequency", this->frequency_sensor_);
} }
} // namespace bl0942 } // namespace bl0942

View file

@ -8,6 +8,57 @@
namespace esphome { namespace esphome {
namespace bl0942 { namespace bl0942 {
// The BL0942 IC is "calibration-free", which means that it doesn't care
// at all about calibration, and that's left to software. It measures a
// voltage differential on its IP/IN pins which linearly proportional to
// the current flow, and another on its VP pin which is proportional to
// the line voltage. It never knows the actual calibration; the values
// it reports are solely in terms of those inputs.
//
// The datasheet refers to the input voltages as I(A) and V(V), both
// in millivolts. It measures them against a reference voltage Vref,
// which is typically 1.218V (but that absolute value is meaningless
// without the actual calibration anyway).
//
// The reported I_RMS value is 305978 I(A)/Vref, and the reported V_RMS
// value is 73989 V(V)/Vref. So we can calibrate those by applying a
// simple meter with a resistive load.
//
// The chip also measures the phase difference between voltage and
// current, and uses it to calculate the power factor (cos φ). It
// reports the WATT value of 3537 * I_RMS * V_RMS * cos φ).
//
// It also integrates total energy based on the WATT value. The time for
// one CF_CNT pulse is 1638.4*256 / WATT.
//
// So... how do we calibrate that?
//
// Using a simple resistive load and an external meter, we can measure
// the true voltage and current for a given V_RMS and I_RMS reading,
// to calculate BL0942_UREF and BL0942_IREF. Those are in units of
// "305978 counts per amp" or "73989 counts per volt" respectively.
//
// We can derive BL0942_PREF from those. Let's eliminate the weird
// factors and express the calibration in plain counts per volt/amp:
// UREF1 = UREF/73989, IREF1 = IREF/305978.
//
// Next... the true power in Watts is V * I * cos φ, so that's equal
// to WATT/3537 * IREF1 * UREF1. Which means
// BL0942_PREF = BL0942_UREF * BL0942_IREF * 3537 / 305978 / 73989.
//
// Finally the accumulated energy. The period of a CF_CNT count is
// 1638.4*256 / WATT seconds, or 419230.4 / WATT seconds. Which means
// the energy represented by a CN_CNT pulse is 419230.4 WATT-seconds.
// Factoring in the calibration, that's 419230.4 / BL0942_PREF actual
// Watt-seconds (or Joules, as the physicists like to call them).
//
// But we're not being physicists today; we we're being engineers, so
// we want to convert to kWh instead. Which we do by dividing by 1000
// and then by 3600, so the energy in kWh is
// CF_CNT * 419230.4 / BL0942_PREF / 3600000
//
// Which makes BL0952_EREF = BL0942_PREF * 3600000 / 419430.4
static const float BL0942_PREF = 596; // taken from tasmota static const float BL0942_PREF = 596; // taken from tasmota
static const float BL0942_UREF = 15873.35944299; // should be 73989/1.218 static const float BL0942_UREF = 15873.35944299; // should be 73989/1.218
static const float BL0942_IREF = 251213.46469622; // 305978/1.218 static const float BL0942_IREF = 251213.46469622; // 305978/1.218
@ -42,6 +93,22 @@ class BL0942 : public PollingComponent, public uart::UARTDevice {
void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; } void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; }
void set_line_freq(LineFrequency freq) { this->line_freq_ = freq; } void set_line_freq(LineFrequency freq) { this->line_freq_ = freq; }
void set_address(uint8_t address) { this->address_ = address; } void set_address(uint8_t address) { this->address_ = address; }
void set_current_reference(float current_ref) {
this->current_reference_ = current_ref;
this->current_reference_set_ = true;
}
void set_energy_reference(float energy_ref) {
this->energy_reference_ = energy_ref;
this->energy_reference_set_ = true;
}
void set_power_reference(float power_ref) {
this->power_reference_ = power_ref;
this->power_reference_set_ = true;
}
void set_voltage_reference(float voltage_ref) {
this->voltage_reference_ = voltage_ref;
this->voltage_reference_set_ = true;
}
void loop() override; void loop() override;
void update() override; void update() override;
@ -59,14 +126,20 @@ class BL0942 : public PollingComponent, public uart::UARTDevice {
// Divide by this to turn into Watt // Divide by this to turn into Watt
float power_reference_ = BL0942_PREF; float power_reference_ = BL0942_PREF;
bool power_reference_set_ = false;
// Divide by this to turn into Volt // Divide by this to turn into Volt
float voltage_reference_ = BL0942_UREF; float voltage_reference_ = BL0942_UREF;
bool voltage_reference_set_ = false;
// Divide by this to turn into Ampere // Divide by this to turn into Ampere
float current_reference_ = BL0942_IREF; float current_reference_ = BL0942_IREF;
bool current_reference_set_ = false;
// Divide by this to turn into kWh // Divide by this to turn into kWh
float energy_reference_ = BL0942_EREF; float energy_reference_ = BL0942_EREF;
bool energy_reference_set_ = false;
uint8_t address_ = 0; uint8_t address_ = 0;
LineFrequency line_freq_ = LINE_FREQUENCY_50HZ; LineFrequency line_freq_ = LINE_FREQUENCY_50HZ;
uint32_t rx_start_ = 0;
uint32_t prev_cf_cnt_ = 0;
bool validate_checksum_(DataPacket *data); bool validate_checksum_(DataPacket *data);
int read_reg_(uint8_t reg); int read_reg_(uint8_t reg);

View file

@ -24,6 +24,11 @@ from esphome.const import (
UNIT_WATT, UNIT_WATT,
) )
CONF_CURRENT_REFERENCE = "current_reference"
CONF_ENERGY_REFERENCE = "energy_reference"
CONF_POWER_REFERENCE = "power_reference"
CONF_VOLTAGE_REFERENCE = "voltage_reference"
DEPENDENCIES = ["uart"] DEPENDENCIES = ["uart"]
bl0942_ns = cg.esphome_ns.namespace("bl0942") bl0942_ns = cg.esphome_ns.namespace("bl0942")
@ -77,6 +82,10 @@ CONFIG_SCHEMA = (
), ),
), ),
cv.Optional(CONF_ADDRESS, default=0): cv.int_range(min=0, max=3), cv.Optional(CONF_ADDRESS, default=0): cv.int_range(min=0, max=3),
cv.Optional(CONF_CURRENT_REFERENCE): cv.float_,
cv.Optional(CONF_ENERGY_REFERENCE): cv.float_,
cv.Optional(CONF_POWER_REFERENCE): cv.float_,
cv.Optional(CONF_VOLTAGE_REFERENCE): cv.float_,
} }
) )
.extend(cv.polling_component_schema("60s")) .extend(cv.polling_component_schema("60s"))
@ -106,3 +115,11 @@ async def to_code(config):
cg.add(var.set_frequency_sensor(sens)) cg.add(var.set_frequency_sensor(sens))
cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY])) cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY]))
cg.add(var.set_address(config[CONF_ADDRESS])) cg.add(var.set_address(config[CONF_ADDRESS]))
if (current_reference := config.get(CONF_CURRENT_REFERENCE, None)) is not None:
cg.add(var.set_current_reference(current_reference))
if (voltage_reference := config.get(CONF_VOLTAGE_REFERENCE, None)) is not None:
cg.add(var.set_voltage_reference(voltage_reference))
if (power_reference := config.get(CONF_POWER_REFERENCE, None)) is not None:
cg.add(var.set_power_reference(power_reference))
if (energy_reference := config.get(CONF_ENERGY_REFERENCE, None)) is not None:
cg.add(var.set_energy_reference(energy_reference))

View file

@ -65,9 +65,7 @@ CONF_ON_PASSKEY_NOTIFICATION = "on_passkey_notification"
CONF_ON_NUMERIC_COMPARISON_REQUEST = "on_numeric_comparison_request" CONF_ON_NUMERIC_COMPARISON_REQUEST = "on_numeric_comparison_request"
CONF_AUTO_CONNECT = "auto_connect" CONF_AUTO_CONNECT = "auto_connect"
# Espressif platformio framework is built with MAX_BLE_CONN to 3, so MULTI_CONF = True
# enforce this in yaml checks.
MULTI_CONF = 3
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
cv.Schema( cv.Schema(

View file

@ -1,96 +1,5 @@
import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import ( CONFIG_SCHEMA = cv.invalid(
CONF_ID, "The bmp280 sensor component has been renamed to bmp280_i2c."
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
) )
DEPENDENCIES = ["i2c"]
bmp280_ns = cg.esphome_ns.namespace("bmp280")
BMP280Oversampling = bmp280_ns.enum("BMP280Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": BMP280Oversampling.BMP280_OVERSAMPLING_NONE,
"1X": BMP280Oversampling.BMP280_OVERSAMPLING_1X,
"2X": BMP280Oversampling.BMP280_OVERSAMPLING_2X,
"4X": BMP280Oversampling.BMP280_OVERSAMPLING_4X,
"8X": BMP280Oversampling.BMP280_OVERSAMPLING_8X,
"16X": BMP280Oversampling.BMP280_OVERSAMPLING_16X,
}
BMP280IIRFilter = bmp280_ns.enum("BMP280IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": BMP280IIRFilter.BMP280_IIR_FILTER_OFF,
"2X": BMP280IIRFilter.BMP280_IIR_FILTER_2X,
"4X": BMP280IIRFilter.BMP280_IIR_FILTER_4X,
"8X": BMP280IIRFilter.BMP280_IIR_FILTER_8X,
"16X": BMP280IIRFilter.BMP280_IIR_FILTER_16X,
}
BMP280Component = bmp280_ns.class_(
"BMP280Component", cg.PollingComponent, i2c.I2CDevice
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(BMP280Component),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x77))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING]))
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING]))
cg.add(var.set_iir_filter(config[CONF_IIR_FILTER]))

View file

@ -0,0 +1,88 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_ID,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
)
CODEOWNERS = ["@ademuri"]
bmp280_ns = cg.esphome_ns.namespace("bmp280_base")
BMP280Oversampling = bmp280_ns.enum("BMP280Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": BMP280Oversampling.BMP280_OVERSAMPLING_NONE,
"1X": BMP280Oversampling.BMP280_OVERSAMPLING_1X,
"2X": BMP280Oversampling.BMP280_OVERSAMPLING_2X,
"4X": BMP280Oversampling.BMP280_OVERSAMPLING_4X,
"8X": BMP280Oversampling.BMP280_OVERSAMPLING_8X,
"16X": BMP280Oversampling.BMP280_OVERSAMPLING_16X,
}
BMP280IIRFilter = bmp280_ns.enum("BMP280IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": BMP280IIRFilter.BMP280_IIR_FILTER_OFF,
"2X": BMP280IIRFilter.BMP280_IIR_FILTER_2X,
"4X": BMP280IIRFilter.BMP280_IIR_FILTER_4X,
"8X": BMP280IIRFilter.BMP280_IIR_FILTER_8X,
"16X": BMP280IIRFilter.BMP280_IIR_FILTER_16X,
}
CONFIG_SCHEMA_BASE = cv.Schema(
{
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
).extend(cv.polling_component_schema("60s"))
async def to_code_base(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING]))
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING]))
cg.add(var.set_iir_filter(config[CONF_IIR_FILTER]))
return var

View file

@ -1,9 +1,9 @@
#include "bmp280.h" #include "bmp280_base.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome {
namespace bmp280 { namespace bmp280_base {
static const char *const TAG = "bmp280.sensor"; static const char *const TAG = "bmp280.sensor";
@ -59,6 +59,14 @@ static const char *iir_filter_to_str(BMP280IIRFilter filter) {
void BMP280Component::setup() { void BMP280Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up BMP280..."); ESP_LOGCONFIG(TAG, "Setting up BMP280...");
uint8_t chip_id = 0; uint8_t chip_id = 0;
// Read the chip id twice, to work around a bug where the first read is 0.
// https://community.st.com/t5/stm32-mcus-products/issue-with-reading-bmp280-chip-id-using-spi/td-p/691855
if (!this->read_byte(0xD0, &chip_id)) {
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
if (!this->read_byte(0xD0, &chip_id)) { if (!this->read_byte(0xD0, &chip_id)) {
this->error_code_ = COMMUNICATION_FAILED; this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed(); this->mark_failed();
@ -122,7 +130,6 @@ void BMP280Component::setup() {
} }
void BMP280Component::dump_config() { void BMP280Component::dump_config() {
ESP_LOGCONFIG(TAG, "BMP280:"); ESP_LOGCONFIG(TAG, "BMP280:");
LOG_I2C_DEVICE(this);
switch (this->error_code_) { switch (this->error_code_) {
case COMMUNICATION_FAILED: case COMMUNICATION_FAILED:
ESP_LOGE(TAG, "Communication with BMP280 failed!"); ESP_LOGE(TAG, "Communication with BMP280 failed!");
@ -262,5 +269,5 @@ uint16_t BMP280Component::read_u16_le_(uint8_t a_register) {
} }
int16_t BMP280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); } int16_t BMP280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
} // namespace bmp280 } // namespace bmp280_base
} // namespace esphome } // namespace esphome

View file

@ -2,10 +2,9 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome { namespace esphome {
namespace bmp280 { namespace bmp280_base {
/// Internal struct storing the calibration values of an BMP280. /// Internal struct storing the calibration values of an BMP280.
struct BMP280CalibrationData { struct BMP280CalibrationData {
@ -50,8 +49,8 @@ enum BMP280IIRFilter {
BMP280_IIR_FILTER_16X = 0b100, BMP280_IIR_FILTER_16X = 0b100,
}; };
/// This class implements support for the BMP280 Temperature+Pressure i2c sensor. /// This class implements support for the BMP280 Temperature+Pressure sensor.
class BMP280Component : public PollingComponent, public i2c::I2CDevice { class BMP280Component : public PollingComponent {
public: public:
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; } void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; } void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
@ -68,6 +67,11 @@ class BMP280Component : public PollingComponent, public i2c::I2CDevice {
float get_setup_priority() const override; float get_setup_priority() const override;
void update() override; void update() override;
virtual bool read_byte(uint8_t a_register, uint8_t *data) = 0;
virtual bool write_byte(uint8_t a_register, uint8_t data) = 0;
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0;
virtual bool read_byte_16(uint8_t a_register, uint16_t *data) = 0;
protected: protected:
/// Read the temperature value and store the calculated ambient temperature in t_fine. /// Read the temperature value and store the calculated ambient temperature in t_fine.
float read_temperature_(int32_t *t_fine); float read_temperature_(int32_t *t_fine);
@ -90,5 +94,5 @@ class BMP280Component : public PollingComponent, public i2c::I2CDevice {
} error_code_{NONE}; } error_code_{NONE};
}; };
} // namespace bmp280 } // namespace bmp280_base
} // namespace esphome } // namespace esphome

View file

@ -0,0 +1,27 @@
#include "bmp280_i2c.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome {
namespace bmp280_i2c {
bool BMP280I2CComponent::read_byte(uint8_t a_register, uint8_t *data) {
return I2CDevice::read_byte(a_register, data);
};
bool BMP280I2CComponent::write_byte(uint8_t a_register, uint8_t data) {
return I2CDevice::write_byte(a_register, data);
};
bool BMP280I2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
return I2CDevice::read_bytes(a_register, data, len);
};
bool BMP280I2CComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
return I2CDevice::read_byte_16(a_register, data);
};
void BMP280I2CComponent::dump_config() {
LOG_I2C_DEVICE(this);
BMP280Component::dump_config();
}
} // namespace bmp280_i2c
} // namespace esphome

View file

@ -0,0 +1,22 @@
#pragma once
#include "esphome/components/bmp280_base/bmp280_base.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace bmp280_i2c {
static const char *const TAG = "bmp280_i2c.sensor";
/// This class implements support for the BMP280 Temperature+Pressure i2c sensor.
class BMP280I2CComponent : public esphome::bmp280_base::BMP280Component, public i2c::I2CDevice {
public:
bool read_byte(uint8_t a_register, uint8_t *data) override;
bool write_byte(uint8_t a_register, uint8_t data) override;
bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
bool read_byte_16(uint8_t a_register, uint16_t *data) override;
void dump_config() override;
};
} // namespace bmp280_i2c
} // namespace esphome

View file

@ -0,0 +1,22 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c
from ..bmp280_base import to_code_base, CONFIG_SCHEMA_BASE
AUTO_LOAD = ["bmp280_base"]
CODEOWNERS = ["@ademuri"]
DEPENDENCIES = ["i2c"]
bmp280_ns = cg.esphome_ns.namespace("bmp280_i2c")
BMP280I2CComponent = bmp280_ns.class_(
"BMP280I2CComponent", cg.PollingComponent, i2c.I2CDevice
)
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(
i2c.i2c_device_schema(default_address=0x77)
).extend({cv.GenerateID(): cv.declare_id(BMP280I2CComponent)})
async def to_code(config):
var = await to_code_base(config)
await i2c.register_i2c_device(var, config)

View file

@ -0,0 +1,65 @@
#include <cstdint>
#include <cstddef>
#include "bmp280_spi.h"
#include <esphome/components/bmp280_base/bmp280_base.h>
namespace esphome {
namespace bmp280_spi {
uint8_t set_bit(uint8_t num, uint8_t position) {
uint8_t mask = 1 << position;
return num | mask;
}
uint8_t clear_bit(uint8_t num, uint8_t position) {
uint8_t mask = 1 << position;
return num & ~mask;
}
void BMP280SPIComponent::setup() {
this->spi_setup();
BMP280Component::setup();
};
// In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not used
// and replaced by a read/write bit (RW = 0 for write and RW = 1 for read).
// Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the byte
// 0x77 is transferred, for read access, the byte 0xF7 is transferred.
// https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf
bool BMP280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
this->enable();
this->transfer_byte(set_bit(a_register, 7));
*data = this->transfer_byte(0);
this->disable();
return true;
}
bool BMP280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
this->enable();
this->transfer_byte(clear_bit(a_register, 7));
this->transfer_byte(data);
this->disable();
return true;
}
bool BMP280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
this->enable();
this->transfer_byte(set_bit(a_register, 7));
this->read_array(data, len);
this->disable();
return true;
}
bool BMP280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
this->enable();
this->transfer_byte(set_bit(a_register, 7));
((uint8_t *) data)[1] = this->transfer_byte(0);
((uint8_t *) data)[0] = this->transfer_byte(0);
this->disable();
return true;
}
} // namespace bmp280_spi
} // namespace esphome

View file

@ -0,0 +1,20 @@
#pragma once
#include "esphome/components/bmp280_base/bmp280_base.h"
#include "esphome/components/spi/spi.h"
namespace esphome {
namespace bmp280_spi {
class BMP280SPIComponent : public esphome::bmp280_base::BMP280Component,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_200KHZ> {
void setup() override;
bool read_byte(uint8_t a_register, uint8_t *data) override;
bool write_byte(uint8_t a_register, uint8_t data) override;
bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
bool read_byte_16(uint8_t a_register, uint16_t *data) override;
};
} // namespace bmp280_spi
} // namespace esphome

View file

@ -0,0 +1,22 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import spi
from ..bmp280_base import to_code_base, CONFIG_SCHEMA_BASE
AUTO_LOAD = ["bmp280_base"]
CODEOWNERS = ["@ademuri"]
DEPENDENCIES = ["spi"]
bmp280_ns = cg.esphome_ns.namespace("bmp280_spi")
BMP280SPIComponent = bmp280_ns.class_(
"BMP280SPIComponent", cg.PollingComponent, spi.SPIDevice
)
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(
spi.spi_device_schema(default_mode="mode3")
).extend({cv.GenerateID(): cv.declare_id(BMP280SPIComponent)})
async def to_code(config):
var = await to_code_base(config)
await spi.register_spi_device(var, config)

View file

@ -16,6 +16,8 @@
#include <esp32s2/rom/rtc.h> #include <esp32s2/rom/rtc.h>
#elif defined(USE_ESP32_VARIANT_ESP32S3) #elif defined(USE_ESP32_VARIANT_ESP32S3)
#include <esp32s3/rom/rtc.h> #include <esp32s3/rom/rtc.h>
#elif defined(USE_ESP32_VARIANT_ESP32H2)
#include <esp32h2/rom/rtc.h>
#endif #endif
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#include <Esp.h> #include <Esp.h>
@ -61,7 +63,7 @@ std::string DebugComponent::get_reset_reason_() {
case RTCWDT_SYS_RESET: case RTCWDT_SYS_RESET:
reset_reason = "RTC Watch Dog Reset Digital Core"; reset_reason = "RTC Watch Dog Reset Digital Core";
break; break;
#if !defined(USE_ESP32_VARIANT_ESP32C6) #if !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
case INTRUSION_RESET: case INTRUSION_RESET:
reset_reason = "Intrusion Reset CPU"; reset_reason = "Intrusion Reset CPU";
break; break;

View file

@ -1,6 +1,6 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import climate_ir from esphome.components import climate_ir
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_MODEL from esphome.const import CONF_ID, CONF_MODEL
CODEOWNERS = ["@orestismers"] CODEOWNERS = ["@orestismers"]
@ -17,6 +17,7 @@ MODELS = {
"yaa": Model.GREE_YAA, "yaa": Model.GREE_YAA,
"yac": Model.GREE_YAC, "yac": Model.GREE_YAC,
"yac1fb9": Model.GREE_YAC1FB9, "yac1fb9": Model.GREE_YAC1FB9,
"yx1ff": Model.GREE_YX1FF,
} }
CONFIG_SCHEMA = climate_ir.CLIMATE_IR_WITH_RECEIVER_SCHEMA.extend( CONFIG_SCHEMA = climate_ir.CLIMATE_IR_WITH_RECEIVER_SCHEMA.extend(

View file

@ -6,7 +6,15 @@ namespace gree {
static const char *const TAG = "gree.climate"; static const char *const TAG = "gree.climate";
void GreeClimate::set_model(Model model) { this->model_ = model; } void GreeClimate::set_model(Model model) {
if (model == GREE_YX1FF) {
this->fan_modes_.insert(climate::CLIMATE_FAN_QUIET); // YX1FF 4 speed
this->presets_.insert(climate::CLIMATE_PRESET_NONE); // YX1FF sleep mode
this->presets_.insert(climate::CLIMATE_PRESET_SLEEP); // YX1FF sleep mode
}
this->model_ = model;
}
void GreeClimate::transmit_state() { void GreeClimate::transmit_state() {
uint8_t remote_state[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00}; uint8_t remote_state[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00};
@ -14,7 +22,7 @@ void GreeClimate::transmit_state() {
remote_state[0] = this->fan_speed_() | this->operation_mode_(); remote_state[0] = this->fan_speed_() | this->operation_mode_();
remote_state[1] = this->temperature_(); remote_state[1] = this->temperature_();
if (this->model_ == GREE_YAN) { if (this->model_ == GREE_YAN || this->model_ == GREE_YX1FF) {
remote_state[2] = 0x60; remote_state[2] = 0x60;
remote_state[3] = 0x50; remote_state[3] = 0x50;
remote_state[4] = this->vertical_swing_(); remote_state[4] = this->vertical_swing_();
@ -36,8 +44,18 @@ void GreeClimate::transmit_state() {
} }
} }
if (this->model_ == GREE_YX1FF) {
if (this->fan_speed_() == GREE_FAN_TURBO) {
remote_state[2] |= GREE_FAN_TURBO_BIT;
}
if (this->preset_() == GREE_PRESET_SLEEP) {
remote_state[0] |= GREE_PRESET_SLEEP_BIT;
}
}
// Calculate the checksum // Calculate the checksum
if (this->model_ == GREE_YAN) { if (this->model_ == GREE_YAN || this->model_ == GREE_YX1FF) {
remote_state[7] = ((remote_state[0] << 4) + (remote_state[1] << 4) + 0xC0); remote_state[7] = ((remote_state[0] << 4) + (remote_state[1] << 4) + 0xC0);
} else { } else {
remote_state[7] = remote_state[7] =
@ -124,6 +142,23 @@ uint8_t GreeClimate::operation_mode_() {
} }
uint8_t GreeClimate::fan_speed_() { uint8_t GreeClimate::fan_speed_() {
// YX1FF has 4 fan speeds -- we treat low as quiet and turbo as high
if (this->model_ == GREE_YX1FF) {
switch (this->fan_mode.value()) {
case climate::CLIMATE_FAN_QUIET:
return GREE_FAN_1;
case climate::CLIMATE_FAN_LOW:
return GREE_FAN_2;
case climate::CLIMATE_FAN_MEDIUM:
return GREE_FAN_3;
case climate::CLIMATE_FAN_HIGH:
return GREE_FAN_TURBO;
case climate::CLIMATE_FAN_AUTO:
default:
return GREE_FAN_AUTO;
}
}
switch (this->fan_mode.value()) { switch (this->fan_mode.value()) {
case climate::CLIMATE_FAN_LOW: case climate::CLIMATE_FAN_LOW:
return GREE_FAN_1; return GREE_FAN_1;
@ -161,5 +196,21 @@ uint8_t GreeClimate::temperature_() {
return (uint8_t) roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX)); return (uint8_t) roundf(clamp<float>(this->target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX));
} }
uint8_t GreeClimate::preset_() {
// YX1FF has sleep preset
if (this->model_ == GREE_YX1FF) {
switch (this->preset.value()) {
case climate::CLIMATE_PRESET_NONE:
return GREE_PRESET_NONE;
case climate::CLIMATE_PRESET_SLEEP:
return GREE_PRESET_SLEEP;
default:
return GREE_PRESET_NONE;
}
}
return GREE_PRESET_NONE;
}
} // namespace gree } // namespace gree
} // namespace esphome } // namespace esphome

View file

@ -25,7 +25,6 @@ const uint8_t GREE_FAN_AUTO = 0x00;
const uint8_t GREE_FAN_1 = 0x10; const uint8_t GREE_FAN_1 = 0x10;
const uint8_t GREE_FAN_2 = 0x20; const uint8_t GREE_FAN_2 = 0x20;
const uint8_t GREE_FAN_3 = 0x30; const uint8_t GREE_FAN_3 = 0x30;
const uint8_t GREE_FAN_TURBO = 0x80;
// IR Transmission // IR Transmission
const uint32_t GREE_IR_FREQUENCY = 38000; const uint32_t GREE_IR_FREQUENCY = 38000;
@ -70,8 +69,16 @@ const uint8_t GREE_HDIR_MIDDLE = 0x04;
const uint8_t GREE_HDIR_MRIGHT = 0x05; const uint8_t GREE_HDIR_MRIGHT = 0x05;
const uint8_t GREE_HDIR_RIGHT = 0x06; const uint8_t GREE_HDIR_RIGHT = 0x06;
// Only available on YX1FF
// Turbo (high) fan mode + sleep preset mode
const uint8_t GREE_FAN_TURBO = 0x80;
const uint8_t GREE_FAN_TURBO_BIT = 0x10;
const uint8_t GREE_PRESET_NONE = 0x00;
const uint8_t GREE_PRESET_SLEEP = 0x01;
const uint8_t GREE_PRESET_SLEEP_BIT = 0x80;
// Model codes // Model codes
enum Model { GREE_GENERIC, GREE_YAN, GREE_YAA, GREE_YAC, GREE_YAC1FB9 }; enum Model { GREE_GENERIC, GREE_YAN, GREE_YAA, GREE_YAC, GREE_YAC1FB9, GREE_YX1FF };
class GreeClimate : public climate_ir::ClimateIR { class GreeClimate : public climate_ir::ClimateIR {
public: public:
@ -93,6 +100,7 @@ class GreeClimate : public climate_ir::ClimateIR {
uint8_t horizontal_swing_(); uint8_t horizontal_swing_();
uint8_t vertical_swing_(); uint8_t vertical_swing_();
uint8_t temperature_(); uint8_t temperature_();
uint8_t preset_();
Model model_{}; Model model_{};
}; };

View file

@ -5,6 +5,19 @@ from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_INTERNAL
CODEOWNERS = ["@OttoWinter", "@esphome/core"] CODEOWNERS = ["@OttoWinter", "@esphome/core"]
homeassistant_ns = cg.esphome_ns.namespace("homeassistant") homeassistant_ns = cg.esphome_ns.namespace("homeassistant")
def validate_entity_domain(platform, supported_domains):
def validator(config):
domain = config[CONF_ENTITY_ID].split(".", 1)[0]
if domain not in supported_domains:
raise cv.Invalid(
f"Entity ID {config[CONF_ENTITY_ID]} is not supported by the {platform} platform."
)
return config
return validator
HOME_ASSISTANT_IMPORT_SCHEMA = cv.Schema( HOME_ASSISTANT_IMPORT_SCHEMA = cv.Schema(
{ {
cv.Required(CONF_ENTITY_ID): cv.entity_id, cv.Required(CONF_ENTITY_ID): cv.entity_id,

View file

@ -7,19 +7,32 @@ from .. import (
HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA, HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA,
homeassistant_ns, homeassistant_ns,
setup_home_assistant_entity, setup_home_assistant_entity,
validate_entity_domain,
) )
CODEOWNERS = ["@Links2004"] CODEOWNERS = ["@Links2004"]
DEPENDENCIES = ["api"] DEPENDENCIES = ["api"]
SUPPORTED_DOMAINS = [
"automation",
"fan",
"humidifier",
"input_boolean",
"light",
"remote",
"siren",
"switch",
]
HomeassistantSwitch = homeassistant_ns.class_( HomeassistantSwitch = homeassistant_ns.class_(
"HomeassistantSwitch", switch.Switch, cg.Component "HomeassistantSwitch", switch.Switch, cg.Component
) )
CONFIG_SCHEMA = ( CONFIG_SCHEMA = cv.All(
switch.switch_schema(HomeassistantSwitch) switch.switch_schema(HomeassistantSwitch)
.extend(cv.COMPONENT_SCHEMA)
.extend(HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA) .extend(HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA)
.extend(cv.COMPONENT_SCHEMA),
validate_entity_domain("switch", SUPPORTED_DOMAINS),
) )

View file

@ -42,9 +42,9 @@ void HomeassistantSwitch::write_state(bool state) {
api::HomeassistantServiceResponse resp; api::HomeassistantServiceResponse resp;
if (state) { if (state) {
resp.service = "switch.turn_on"; resp.service = "homeassistant.turn_on";
} else { } else {
resp.service = "switch.turn_off"; resp.service = "homeassistant.turn_off";
} }
api::HomeassistantServiceMap entity_id_kv; api::HomeassistantServiceMap entity_id_kv;

View file

@ -1,17 +1,17 @@
import esphome.codegen as cg
from esphome.components.esp32 import add_idf_component
import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_DISABLED,
CONF_ID, CONF_ID,
CONF_PORT, CONF_PORT,
CONF_PROTOCOL, CONF_PROTOCOL,
CONF_SERVICES,
CONF_SERVICE, CONF_SERVICE,
CONF_SERVICES,
KEY_CORE, KEY_CORE,
KEY_FRAMEWORK_VERSION, KEY_FRAMEWORK_VERSION,
CONF_DISABLED,
) )
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.core import CORE, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
from esphome.components.esp32 import add_idf_component
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
DEPENDENCIES = ["network"] DEPENDENCIES = ["network"]
@ -91,7 +91,7 @@ async def to_code(config):
add_idf_component( add_idf_component(
name="mdns", name="mdns",
repo="https://github.com/espressif/esp-protocols.git", repo="https://github.com/espressif/esp-protocols.git",
ref="mdns-v1.2.5", ref="mdns-v1.3.2",
path="components/mdns", path="components/mdns",
) )

View file

@ -4,8 +4,6 @@ import logging
from pathlib import Path from pathlib import Path
from urllib.parse import urljoin from urllib.parse import urljoin
import requests
from esphome import automation, external_files, git from esphome import automation, external_files, git
from esphome.automation import register_action, register_condition from esphome.automation import register_action, register_condition
import esphome.codegen as cg import esphome.codegen as cg
@ -26,7 +24,6 @@ from esphome.const import (
CONF_USERNAME, CONF_USERNAME,
TYPE_GIT, TYPE_GIT,
TYPE_LOCAL, TYPE_LOCAL,
__version__,
) )
from esphome.core import CORE, HexInt from esphome.core import CORE, HexInt
@ -179,26 +176,6 @@ def _convert_manifest_v1_to_v2(v1_manifest):
return v2_manifest return v2_manifest
def _download_file(url: str, path: Path) -> bytes:
if not external_files.has_remote_file_changed(url, path):
_LOGGER.debug("Remote file has not changed, skipping download")
return path.read_bytes()
try:
req = requests.get(
url,
timeout=external_files.NETWORK_TIMEOUT,
headers={"User-agent": f"ESPHome/{__version__} (https://esphome.io)"},
)
req.raise_for_status()
except requests.exceptions.RequestException as e:
raise cv.Invalid(f"Could not download file from {url}: {e}") from e
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(req.content)
return req.content
def _validate_manifest_version(manifest_data): def _validate_manifest_version(manifest_data):
if manifest_version := manifest_data.get(KEY_VERSION): if manifest_version := manifest_data.get(KEY_VERSION):
if manifest_version == 1: if manifest_version == 1:
@ -223,7 +200,7 @@ def _process_http_source(config):
json_path = path / "manifest.json" json_path = path / "manifest.json"
json_contents = _download_file(url, json_path) json_contents = external_files.download_content(url, json_path)
manifest_data = json.loads(json_contents) manifest_data = json.loads(json_contents)
if not isinstance(manifest_data, dict): if not isinstance(manifest_data, dict):
@ -234,7 +211,7 @@ def _process_http_source(config):
model_path = path / model model_path = path / model
_download_file(str(model_url), model_path) external_files.download_content(str(model_url), model_path)
return config return config

View file

@ -13,6 +13,7 @@ from esphome.const import (
) )
from esphome.cpp_helpers import logging from esphome.cpp_helpers import logging
from .const import ( from .const import (
CONF_ALLOW_DUPLICATE_COMMANDS,
CONF_BITMASK, CONF_BITMASK,
CONF_BYTE_OFFSET, CONF_BYTE_OFFSET,
CONF_COMMAND_THROTTLE, CONF_COMMAND_THROTTLE,
@ -126,6 +127,7 @@ CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
{ {
cv.GenerateID(): cv.declare_id(ModbusController), cv.GenerateID(): cv.declare_id(ModbusController),
cv.Optional(CONF_ALLOW_DUPLICATE_COMMANDS, default=False): cv.boolean,
cv.Optional( cv.Optional(
CONF_COMMAND_THROTTLE, default="0ms" CONF_COMMAND_THROTTLE, default="0ms"
): cv.positive_time_period_milliseconds, ): cv.positive_time_period_milliseconds,
@ -253,6 +255,7 @@ async def add_modbus_base_properties(
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_allow_duplicate_commands(config[CONF_ALLOW_DUPLICATE_COMMANDS]))
cg.add(var.set_command_throttle(config[CONF_COMMAND_THROTTLE])) cg.add(var.set_command_throttle(config[CONF_COMMAND_THROTTLE]))
cg.add(var.set_offline_skip_updates(config[CONF_OFFLINE_SKIP_UPDATES])) cg.add(var.set_offline_skip_updates(config[CONF_OFFLINE_SKIP_UPDATES]))
if CONF_SERVER_REGISTERS in config: if CONF_SERVER_REGISTERS in config:

View file

@ -1,3 +1,4 @@
CONF_ALLOW_DUPLICATE_COMMANDS = "allow_duplicate_commands"
CONF_BITMASK = "bitmask" CONF_BITMASK = "bitmask"
CONF_BYTE_OFFSET = "byte_offset" CONF_BYTE_OFFSET = "byte_offset"
CONF_COMMAND_THROTTLE = "command_throttle" CONF_COMMAND_THROTTLE = "command_throttle"

View file

@ -175,16 +175,18 @@ void ModbusController::on_register_data(ModbusRegisterType register_type, uint16
} }
void ModbusController::queue_command(const ModbusCommandItem &command) { void ModbusController::queue_command(const ModbusCommandItem &command) {
// check if this command is already qeued. if (!this->allow_duplicate_commands_) {
// not very effective but the queue is never really large // check if this command is already qeued.
for (auto &item : command_queue_) { // not very effective but the queue is never really large
if (item->is_equal(command)) { for (auto &item : command_queue_) {
ESP_LOGW(TAG, "Duplicate modbus command found: type=0x%x address=%u count=%u", if (item->is_equal(command)) {
static_cast<uint8_t>(command.register_type), command.register_address, command.register_count); ESP_LOGW(TAG, "Duplicate modbus command found: type=0x%x address=%u count=%u",
// update the payload of the queued command static_cast<uint8_t>(command.register_type), command.register_address, command.register_count);
// replaces a previous command // update the payload of the queued command
item->payload = command.payload; // replaces a previous command
return; item->payload = command.payload;
return;
}
} }
} }
command_queue_.push_back(make_unique<ModbusCommandItem>(command)); command_queue_.push_back(make_unique<ModbusCommandItem>(command));

View file

@ -448,6 +448,12 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
/// incoming queue /// incoming queue
void on_write_register_response(ModbusRegisterType register_type, uint16_t start_address, void on_write_register_response(ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data); const std::vector<uint8_t> &data);
/// Allow a duplicate command to be sent
void set_allow_duplicate_commands(bool allow_duplicate_commands) {
this->allow_duplicate_commands_ = allow_duplicate_commands;
}
/// get if a duplicate command can be sent
bool get_allow_duplicate_commands() { return this->allow_duplicate_commands_; }
/// called by esphome generated code to set the command_throttle period /// called by esphome generated code to set the command_throttle period
void set_command_throttle(uint16_t command_throttle) { this->command_throttle_ = command_throttle; } void set_command_throttle(uint16_t command_throttle) { this->command_throttle_ = command_throttle; }
/// called by esphome generated code to set the offline_skip_updates /// called by esphome generated code to set the offline_skip_updates
@ -482,6 +488,8 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
std::list<std::unique_ptr<ModbusCommandItem>> command_queue_; std::list<std::unique_ptr<ModbusCommandItem>> command_queue_;
/// modbus response data waiting to get processed /// modbus response data waiting to get processed
std::queue<std::unique_ptr<ModbusCommandItem>> incoming_queue_; std::queue<std::unique_ptr<ModbusCommandItem>> incoming_queue_;
/// if duplicate commands can be sent
bool allow_duplicate_commands_;
/// when was the last send operation /// when was the last send operation
uint32_t last_command_timestamp_; uint32_t last_command_timestamp_;
/// min time in ms between sending modbus commands /// min time in ms between sending modbus commands

View file

@ -1,6 +1,5 @@
import logging import logging
import os import os
from string import ascii_letters, digits from string import ascii_letters, digits
import esphome.codegen as cg import esphome.codegen as cg
@ -8,6 +7,7 @@ import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_BOARD, CONF_BOARD,
CONF_FRAMEWORK, CONF_FRAMEWORK,
CONF_PLATFORM_VERSION,
CONF_SOURCE, CONF_SOURCE,
CONF_VERSION, CONF_VERSION,
KEY_CORE, KEY_CORE,
@ -15,10 +15,9 @@ from esphome.const import (
KEY_TARGET_FRAMEWORK, KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM, KEY_TARGET_PLATFORM,
PLATFORM_RP2040, PLATFORM_RP2040,
CONF_PLATFORM_VERSION,
) )
from esphome.core import CORE, coroutine_with_priority, EsphomeError from esphome.core import CORE, EsphomeError, coroutine_with_priority
from esphome.helpers import mkdir_p, write_file, copy_file_if_changed from esphome.helpers import copy_file_if_changed, mkdir_p, write_file
from .const import KEY_BOARD, KEY_PIO_FILES, KEY_RP2040, rp2040_ns from .const import KEY_BOARD, KEY_PIO_FILES, KEY_RP2040, rp2040_ns
@ -81,19 +80,19 @@ def _format_framework_arduino_version(ver: cv.Version) -> str:
# The default/recommended arduino framework version # The default/recommended arduino framework version
# - https://github.com/earlephilhower/arduino-pico/releases # - https://github.com/earlephilhower/arduino-pico/releases
# - https://api.registry.platformio.org/v3/packages/earlephilhower/tool/framework-arduinopico # - https://api.registry.platformio.org/v3/packages/earlephilhower/tool/framework-arduinopico
RECOMMENDED_ARDUINO_FRAMEWORK_VERSION = cv.Version(3, 7, 2) RECOMMENDED_ARDUINO_FRAMEWORK_VERSION = cv.Version(3, 9, 4)
# The platformio/raspberrypi version to use for arduino frameworks # The platformio/raspberrypi version to use for arduino frameworks
# - https://github.com/platformio/platform-raspberrypi/releases # - https://github.com/platformio/platform-raspberrypi/releases
# - https://api.registry.platformio.org/v3/packages/platformio/platform/raspberrypi # - https://api.registry.platformio.org/v3/packages/platformio/platform/raspberrypi
ARDUINO_PLATFORM_VERSION = cv.Version(1, 12, 0) ARDUINO_PLATFORM_VERSION = cv.Version(1, 13, 0)
def _arduino_check_versions(value): def _arduino_check_versions(value):
value = value.copy() value = value.copy()
lookups = { lookups = {
"dev": (cv.Version(3, 4, 0), "https://github.com/earlephilhower/arduino-pico"), "dev": (cv.Version(3, 9, 4), "https://github.com/earlephilhower/arduino-pico"),
"latest": (cv.Version(3, 4, 0), None), "latest": (cv.Version(3, 9, 4), None),
"recommended": (RECOMMENDED_ARDUINO_FRAMEWORK_VERSION, None), "recommended": (RECOMMENDED_ARDUINO_FRAMEWORK_VERSION, None),
} }

View file

@ -0,0 +1,65 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, binary_sensor
from esphome.const import (
CONF_ID,
CONF_PORT,
CONF_NAME,
CONF_SENSORS,
CONF_BINARY_SENSORS,
)
AUTO_LOAD = ["socket"]
CODEOWNERS = ["@Links2004"]
DEPENDENCIES = ["network"]
CONF_HOST = "host"
CONF_PREFIX = "prefix"
statsd_component_ns = cg.esphome_ns.namespace("statsd")
StatsdComponent = statsd_component_ns.class_("StatsdComponent", cg.PollingComponent)
CONFIG_SENSORS_SCHEMA = cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(sensor.Sensor),
cv.Required(CONF_NAME): cv.string_strict,
}
)
CONFIG_BINARY_SENSORS_SCHEMA = cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(binary_sensor.BinarySensor),
cv.Required(CONF_NAME): cv.string_strict,
}
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(StatsdComponent),
cv.Required(CONF_HOST): cv.string_strict,
cv.Optional(CONF_PORT, default=8125): cv.port,
cv.Optional(CONF_PREFIX, default=""): cv.string_strict,
cv.Optional(CONF_SENSORS): cv.ensure_list(CONFIG_SENSORS_SCHEMA),
cv.Optional(CONF_BINARY_SENSORS): cv.ensure_list(CONFIG_BINARY_SENSORS_SCHEMA),
}
).extend(cv.polling_component_schema("10s"))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(
var.configure(
config.get(CONF_HOST),
config.get(CONF_PORT),
config.get(CONF_PREFIX),
)
)
for sensor_cfg in config.get(CONF_SENSORS, []):
s = await cg.get_variable(sensor_cfg[CONF_ID])
cg.add(var.register_sensor(sensor_cfg[CONF_NAME], s))
for sensor_cfg in config.get(CONF_BINARY_SENSORS, []):
s = await cg.get_variable(sensor_cfg[CONF_ID])
cg.add(var.register_binary_sensor(sensor_cfg[CONF_NAME], s))

View file

@ -0,0 +1,156 @@
#include "esphome/core/log.h"
#include "statsd.h"
namespace esphome {
namespace statsd {
// send UDP packet if we reach 1Kb packed size
// this is needed since statsD does not support fragmented UDP packets
static const uint16_t SEND_THRESHOLD = 1024;
static const char *const TAG = "statsD";
void StatsdComponent::setup() {
#ifndef USE_ESP8266
this->sock_ = esphome::socket::socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in source;
source.sin_family = AF_INET;
source.sin_addr.s_addr = htonl(INADDR_ANY);
source.sin_port = htons(this->port_);
this->sock_->bind((struct sockaddr *) &source, sizeof(source));
this->destination_.sin_family = AF_INET;
this->destination_.sin_port = htons(this->port_);
this->destination_.sin_addr.s_addr = inet_addr(this->host_);
#endif
}
StatsdComponent::~StatsdComponent() {
#ifndef USE_ESP8266
if (!this->sock_) {
return;
}
this->sock_->close();
#endif
}
void StatsdComponent::dump_config() {
ESP_LOGCONFIG(TAG, "statsD:");
ESP_LOGCONFIG(TAG, " host: %s", this->host_);
ESP_LOGCONFIG(TAG, " port: %d", this->port_);
if (this->prefix_) {
ESP_LOGCONFIG(TAG, " prefix: %s", this->prefix_);
}
ESP_LOGCONFIG(TAG, " metrics:");
for (sensors_t s : this->sensors_) {
ESP_LOGCONFIG(TAG, " - name: %s", s.name);
ESP_LOGCONFIG(TAG, " type: %d", s.type);
}
}
float StatsdComponent::get_setup_priority() const { return esphome::setup_priority::AFTER_WIFI; }
#ifdef USE_SENSOR
void StatsdComponent::register_sensor(const char *name, esphome::sensor::Sensor *sensor) {
sensors_t s;
s.name = name;
s.sensor = sensor;
s.type = TYPE_SENSOR;
this->sensors_.push_back(s);
}
#endif
#ifdef USE_BINARY_SENSOR
void StatsdComponent::register_binary_sensor(const char *name, esphome::binary_sensor::BinarySensor *binary_sensor) {
sensors_t s;
s.name = name;
s.binary_sensor = binary_sensor;
s.type = TYPE_BINARY_SENSOR;
this->sensors_.push_back(s);
}
#endif
void StatsdComponent::update() {
std::string out;
out.reserve(SEND_THRESHOLD);
for (sensors_t s : this->sensors_) {
double val = 0;
switch (s.type) {
#ifdef USE_SENSOR
case TYPE_SENSOR:
if (!s.sensor->has_state()) {
continue;
}
val = s.sensor->state;
break;
#endif
#ifdef USE_BINARY_SENSOR
case TYPE_BINARY_SENSOR:
if (!s.binary_sensor->has_state()) {
continue;
}
// map bool to double
if (s.binary_sensor->state) {
val = 1;
}
break;
#endif
default:
ESP_LOGE(TAG, "type not known, name: %s type: %d", s.name, s.type);
continue;
}
// statsD gauge:
// https://github.com/statsd/statsd/blob/master/docs/metric_types.md
// This implies you can't explicitly set a gauge to a negative number without first setting it to zero.
if (val < 0) {
if (this->prefix_) {
out.append(str_sprintf("%s.", this->prefix_));
}
out.append(str_sprintf("%s:0|g\n", s.name));
}
if (this->prefix_) {
out.append(str_sprintf("%s.", this->prefix_));
}
out.append(str_sprintf("%s:%f|g\n", s.name, val));
if (out.length() > SEND_THRESHOLD) {
this->send_(&out);
out.clear();
}
}
this->send_(&out);
}
void StatsdComponent::send_(std::string *out) {
if (out->empty()) {
return;
}
#ifdef USE_ESP8266
IPAddress ip;
ip.fromString(this->host_);
this->sock_.beginPacket(ip, this->port_);
this->sock_.write((const uint8_t *) out->c_str(), out->length());
this->sock_.endPacket();
#else
if (!this->sock_) {
return;
}
int n_bytes = this->sock_->sendto(out->c_str(), out->length(), 0, reinterpret_cast<sockaddr *>(&this->destination_),
sizeof(this->destination_));
if (n_bytes != out->length()) {
ESP_LOGE(TAG, "Failed to send UDP packed (%d of %d)", n_bytes, out->length());
}
#endif
}
} // namespace statsd
} // namespace esphome

View file

@ -0,0 +1,86 @@
#pragma once
#include <vector>
#include "esphome/core/defines.h"
#include "esphome/core/component.h"
#include "esphome/components/socket/socket.h"
#include "esphome/components/network/ip_address.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#ifdef USE_LOGGER
#include "esphome/components/logger/logger.h"
#endif
#ifdef USE_ESP8266
#include "WiFiUdp.h"
#include "IPAddress.h"
#endif
namespace esphome {
namespace statsd {
using sensor_type_t = enum { TYPE_SENSOR, TYPE_BINARY_SENSOR };
using sensors_t = struct {
const char *name;
sensor_type_t type;
union {
#ifdef USE_SENSOR
esphome::sensor::Sensor *sensor;
#endif
#ifdef USE_BINARY_SENSOR
esphome::binary_sensor::BinarySensor *binary_sensor;
#endif
};
};
class StatsdComponent : public PollingComponent {
public:
~StatsdComponent();
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override;
void configure(const char *host, uint16_t port, const char *prefix) {
this->host_ = host;
this->port_ = port;
this->prefix_ = prefix;
}
#ifdef USE_SENSOR
void register_sensor(const char *name, esphome::sensor::Sensor *sensor);
#endif
#ifdef USE_BINARY_SENSOR
void register_binary_sensor(const char *name, esphome::binary_sensor::BinarySensor *binary_sensor);
#endif
private:
const char *host_;
const char *prefix_;
uint16_t port_;
std::vector<sensors_t> sensors_;
#ifdef USE_ESP8266
WiFiUDP sock_;
#else
std::unique_ptr<esphome::socket::Socket> sock_;
struct sockaddr_in destination_;
#endif
void send_(std::string *out);
};
} // namespace statsd
} // namespace esphome

View file

@ -43,6 +43,8 @@ CONF_VOLUME_MULTIPLIER = "volume_multiplier"
CONF_WAKE_WORD = "wake_word" CONF_WAKE_WORD = "wake_word"
CONF_CONVERSATION_TIMEOUT = "conversation_timeout"
CONF_ON_TIMER_STARTED = "on_timer_started" CONF_ON_TIMER_STARTED = "on_timer_started"
CONF_ON_TIMER_UPDATED = "on_timer_updated" CONF_ON_TIMER_UPDATED = "on_timer_updated"
CONF_ON_TIMER_CANCELLED = "on_timer_cancelled" CONF_ON_TIMER_CANCELLED = "on_timer_cancelled"
@ -100,6 +102,9 @@ CONFIG_SCHEMA = cv.All(
cv.float_with_unit("decibel full scale", "(dBFS|dbfs|DBFS)"), cv.float_with_unit("decibel full scale", "(dBFS|dbfs|DBFS)"),
cv.int_range(0, 31), cv.int_range(0, 31),
), ),
cv.Optional(
CONF_CONVERSATION_TIMEOUT, default="300s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_VOLUME_MULTIPLIER, default=1.0): cv.float_range( cv.Optional(CONF_VOLUME_MULTIPLIER, default=1.0): cv.float_range(
min=0.0, min_included=False min=0.0, min_included=False
), ),
@ -182,6 +187,7 @@ async def to_code(config):
cg.add(var.set_noise_suppression_level(config[CONF_NOISE_SUPPRESSION_LEVEL])) cg.add(var.set_noise_suppression_level(config[CONF_NOISE_SUPPRESSION_LEVEL]))
cg.add(var.set_auto_gain(config[CONF_AUTO_GAIN])) cg.add(var.set_auto_gain(config[CONF_AUTO_GAIN]))
cg.add(var.set_volume_multiplier(config[CONF_VOLUME_MULTIPLIER])) cg.add(var.set_volume_multiplier(config[CONF_VOLUME_MULTIPLIER]))
cg.add(var.set_conversation_timeout(config[CONF_CONVERSATION_TIMEOUT]))
if CONF_ON_LISTENING in config: if CONF_ON_LISTENING in config:
await automation.build_automation( await automation.build_automation(

View file

@ -171,6 +171,11 @@ void VoiceAssistant::deallocate_buffers_() {
#endif #endif
} }
void VoiceAssistant::reset_conversation_id() {
this->conversation_id_ = "";
ESP_LOGD(TAG, "reset conversation ID");
}
int VoiceAssistant::read_microphone_() { int VoiceAssistant::read_microphone_() {
size_t bytes_read = 0; size_t bytes_read = 0;
if (this->mic_->is_running()) { // Read audio into input buffer if (this->mic_->is_running()) { // Read audio into input buffer
@ -299,7 +304,8 @@ void VoiceAssistant::loop() {
break; break;
} }
this->set_state_(State::STARTING_PIPELINE); this->set_state_(State::STARTING_PIPELINE);
this->set_timeout("reset-conversation_id", 5 * 60 * 1000, [this]() { this->conversation_id_ = ""; }); this->set_timeout("reset-conversation_id", this->conversation_timeout_,
[this]() { this->reset_conversation_id(); });
break; break;
} }
case State::STARTING_PIPELINE: { case State::STARTING_PIPELINE: {

View file

@ -147,6 +147,8 @@ class VoiceAssistant : public Component {
} }
void set_auto_gain(uint8_t auto_gain) { this->auto_gain_ = auto_gain; } void set_auto_gain(uint8_t auto_gain) { this->auto_gain_ = auto_gain; }
void set_volume_multiplier(float volume_multiplier) { this->volume_multiplier_ = volume_multiplier; } void set_volume_multiplier(float volume_multiplier) { this->volume_multiplier_ = volume_multiplier; }
void set_conversation_timeout(uint32_t conversation_timeout) { this->conversation_timeout_ = conversation_timeout; }
void reset_conversation_id();
Trigger<> *get_intent_end_trigger() const { return this->intent_end_trigger_; } Trigger<> *get_intent_end_trigger() const { return this->intent_end_trigger_; }
Trigger<> *get_intent_start_trigger() const { return this->intent_start_trigger_; } Trigger<> *get_intent_start_trigger() const { return this->intent_start_trigger_; }
@ -262,6 +264,7 @@ class VoiceAssistant : public Component {
uint8_t noise_suppression_level_; uint8_t noise_suppression_level_;
uint8_t auto_gain_; uint8_t auto_gain_;
float volume_multiplier_; float volume_multiplier_;
uint32_t conversation_timeout_;
uint8_t *send_buffer_; uint8_t *send_buffer_;
int16_t *input_buffer_; int16_t *input_buffer_;

View file

@ -20,13 +20,20 @@ std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) {
return nullptr; return nullptr;
} }
rb->handle_ = xStreamBufferCreateStatic(len + 1, 0, rb->storage_, &rb->structure_); rb->handle_ = xStreamBufferCreateStatic(len + 1, 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;
} }
size_t RingBuffer::read(void *data, size_t len, TickType_t ticks_to_wait) { size_t RingBuffer::read(void *data, size_t len, TickType_t ticks_to_wait) {
return xStreamBufferReceive(this->handle_, data, len, ticks_to_wait); if (ticks_to_wait > 0)
xStreamBufferSetTriggerLevel(this->handle_, len);
size_t bytes_read = xStreamBufferReceive(this->handle_, data, len, ticks_to_wait);
xStreamBufferSetTriggerLevel(this->handle_, 1);
return bytes_read;
} }
size_t RingBuffer::write(void *data, size_t len) { size_t RingBuffer::write(void *data, size_t len) {
@ -39,6 +46,10 @@ size_t RingBuffer::write(void *data, size_t len) {
return xStreamBufferSend(this->handle_, data, len, 0); return xStreamBufferSend(this->handle_, data, len, 0);
} }
size_t RingBuffer::write_without_replacement(void *data, size_t len, TickType_t ticks_to_wait) {
return xStreamBufferSend(this->handle_, data, len, ticks_to_wait);
}
size_t RingBuffer::available() const { return xStreamBufferBytesAvailable(this->handle_); } size_t RingBuffer::available() const { return xStreamBufferBytesAvailable(this->handle_); }
size_t RingBuffer::free() const { return xStreamBufferSpacesAvailable(this->handle_); } size_t RingBuffer::free() const { return xStreamBufferSpacesAvailable(this->handle_); }

View file

@ -12,13 +12,69 @@ namespace esphome {
class RingBuffer { class RingBuffer {
public: public:
/**
* @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary.
*
* Available bytes are read into the provided data pointer. If not enough bytes are available,
* the function will wait up to `ticks_to_wait` FreeRTOS ticks before reading what is available.
*
* @param data Pointer to copy read data into
* @param len Number of bytes to read
* @param ticks_to_wait Maximum number of FreeRTOS ticks to wait (default: 0)
* @return Number of bytes read
*/
size_t read(void *data, size_t len, TickType_t ticks_to_wait = 0); size_t read(void *data, size_t len, TickType_t ticks_to_wait = 0);
/**
* @brief Writes to the ring buffer, overwriting oldest data if necessary.
*
* The provided data is written to the ring buffer. If not enough space is available,
* the function will overwrite the oldest data in the ring buffer.
*
* @param data Pointer to data for writing
* @param len Number of bytes to write
* @return Number of bytes written
*/
size_t write(void *data, size_t len); size_t write(void *data, size_t len);
/**
* @brief Writes to the ring buffer without overwriting oldest data.
*
* The provided data is written to the ring buffer. If not enough space is available,
* the function will wait up to `ticks_to_wait` FreeRTOS ticks before writing as much as possible.
*
* @param data Pointer to data for writing
* @param len Number of bytes to write
* @param ticks_to_wait Maximum number of FreeRTOS ticks to wait (default: 0)
* @return Number of bytes written
*/
size_t write_without_replacement(void *data, size_t len, TickType_t ticks_to_wait = 0);
/**
* @brief Returns the number of available bytes in the ring buffer.
*
* This function provides the number of bytes that can be read from the ring buffer
* without blocking the calling FreeRTOS task.
*
* @return Number of available bytes
*/
size_t available() const; size_t available() const;
/**
* @brief Returns the number of free bytes in the ring buffer.
*
* This function provides the number of bytes that can be written to the ring buffer
* without overwriting data or blocking the calling FreeRTOS task.
*
* @return Number of free bytes
*/
size_t free() const; size_t free() const;
/**
* @brief Resets the ring buffer, discarding all stored data.
*
* @return pdPASS if successful, pdFAIL otherwise
*/
BaseType_t reset(); BaseType_t reset();
static std::unique_ptr<RingBuffer> create(size_t len); static std::unique_ptr<RingBuffer> create(size_t len);

View file

@ -80,10 +80,10 @@ def compute_local_file_dir(domain: str) -> Path:
return base_directory return base_directory
def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> None: def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> bytes:
if not has_remote_file_changed(url, path): if not has_remote_file_changed(url, path):
_LOGGER.debug("Remote file has not changed %s", url) _LOGGER.debug("Remote file has not changed %s", url)
return return path.read_bytes()
_LOGGER.debug( _LOGGER.debug(
"Remote file has changed, downloading from %s to %s", "Remote file has changed, downloading from %s to %s",
@ -102,4 +102,6 @@ def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> None:
raise cv.Invalid(f"Could not download from {url}: {e}") raise cv.Invalid(f"Could not download from {url}: {e}")
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(req.content) data = req.content
path.write_bytes(data)
return data

View file

@ -7,7 +7,7 @@ dependencies:
version: v2.0.9 version: v2.0.9
mdns: mdns:
git: https://github.com/espressif/esp-protocols.git git: https://github.com/espressif/esp-protocols.git
version: mdns-v1.2.5 version: mdns-v1.3.2
path: components/mdns path: components/mdns
rules: rules:
- if: "idf_version >=5.0" - if: "idf_version >=5.0"

View file

@ -168,7 +168,7 @@ board_build.filesystem_size = 0.5m
platform = https://github.com/maxgerhardt/platform-raspberrypi.git platform = https://github.com/maxgerhardt/platform-raspberrypi.git
platform_packages = platform_packages =
; earlephilhower/framework-arduinopico@~1.20602.0 ; Cannot use the platformio package until old releases stop getting deleted ; earlephilhower/framework-arduinopico@~1.20602.0 ; Cannot use the platformio package until old releases stop getting deleted
earlephilhower/framework-arduinopico@https://github.com/earlephilhower/arduino-pico/releases/download/3.7.2/rp2040-3.7.2.zip earlephilhower/framework-arduinopico@https://github.com/earlephilhower/arduino-pico/releases/download/3.9.4/rp2040-3.9.4.zip
framework = arduino framework = arduino
lib_deps = lib_deps =

View file

@ -20,3 +20,7 @@ sensor:
name: BL0942 Energy name: BL0942 Energy
frequency: frequency:
name: BL0942 Frequency name: BL0942 Frequency
voltage_reference: 15968
current_reference: 124180
power_reference: 309.1
energy_reference: 2653

View file

@ -18,3 +18,5 @@ sensor:
name: BL0942 Energy name: BL0942 Energy
frequency: frequency:
name: BL0942 Frequency name: BL0942 Frequency
voltage_reference: 15968
current_reference: 124180

View file

@ -1,15 +0,0 @@
i2c:
- id: i2c_bmp280
scl: 16
sda: 17
sensor:
- platform: bmp280
address: 0x77
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
iir_filter: 16x
update_interval: 15s

View file

@ -1,15 +0,0 @@
i2c:
- id: i2c_bmp280
scl: 5
sda: 4
sensor:
- platform: bmp280
address: 0x77
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
iir_filter: 16x
update_interval: 15s

View file

@ -1,15 +0,0 @@
i2c:
- id: i2c_bmp280
scl: 16
sda: 17
sensor:
- platform: bmp280
address: 0x77
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
iir_filter: 16x
update_interval: 15s

View file

@ -1,15 +0,0 @@
i2c:
- id: i2c_bmp280
scl: 5
sda: 4
sensor:
- platform: bmp280
address: 0x77
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
iir_filter: 16x
update_interval: 15s

View file

@ -1,15 +0,0 @@
i2c:
- id: i2c_bmp280
scl: 5
sda: 4
sensor:
- platform: bmp280
address: 0x77
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
iir_filter: 16x
update_interval: 15s

View file

@ -1,15 +1,17 @@
i2c: i2c:
- id: i2c_bmp280 - id: i2c_bmp280
scl: 5 scl: ${scl_pin}
sda: 4 sda: ${sda_pin}
sensor: sensor:
- platform: bmp280 - platform: bmp280_i2c
i2c_id: i2c_bmp280
address: 0x77 address: 0x77
temperature: temperature:
id: bmp280_temperature
name: Outside Temperature name: Outside Temperature
oversampling: 16x
pressure: pressure:
name: Outside Pressure name: Outside Pressure
id: bmp280_pressure
iir_filter: 16x iir_filter: 16x
update_interval: 15s update_interval: 15s

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View file

@ -0,0 +1,18 @@
spi:
- id: spi_bmp280
clk_pin: ${clk_pin}
mosi_pin: ${mosi_pin}
miso_pin: ${miso_pin}
sensor:
- platform: bmp280_spi
spi_id: spi_bmp280
cs_pin: ${cs_pin}
temperature:
id: bmp280_temperature
name: Outside Temperature
pressure:
name: Outside Pressure
id: bmp280_pressure
iir_filter: 16x
update_interval: 15s

View file

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO16
mosi_pin: GPIO17
miso_pin: GPIO15
cs_pin: GPIO5
<<: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO5
cs_pin: GPIO8
<<: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO5
cs_pin: GPIO8
<<: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO16
mosi_pin: GPIO17
miso_pin: GPIO15
cs_pin: GPIO5
<<: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO14
mosi_pin: GPIO13
miso_pin: GPIO12
cs_pin: GPIO15
<<: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO2
mosi_pin: GPIO3
miso_pin: GPIO4
cs_pin: GPIO5
<<: !include common.yaml

View file

@ -33,6 +33,27 @@ wifi:
api: api:
switch: switch:
- platform: homeassistant
entity_id: automation.my_cool_automation
id: my_cool_automation
- platform: homeassistant
entity_id: fan.my_cool_fan
id: my_cool_fan
- platform: homeassistant
entity_id: humidifier.my_cool_humidifier
id: my_cool_humidifier
- platform: homeassistant
entity_id: input_boolean.my_cool_input_boolean
id: my_cool_input_boolean
- platform: homeassistant
entity_id: light.my_cool_light
id: my_cool_light
- platform: homeassistant
entity_id: remote.my_cool_remote
id: my_cool_remote
- platform: homeassistant
entity_id: siren.my_cool_siren
id: my_cool_siren
- platform: homeassistant - platform: homeassistant
entity_id: switch.my_cool_switch entity_id: switch.my_cool_switch
id: my_cool_switch id: my_cool_switch

View file

@ -20,6 +20,7 @@ modbus_controller:
- id: modbus_controller1 - id: modbus_controller1
address: 0x2 address: 0x2
modbus_id: mod_bus1 modbus_id: mod_bus1
allow_duplicate_commands: false
- id: modbus_controller2 - id: modbus_controller2
address: 0x2 address: 0x2
modbus_id: mod_bus2 modbus_id: mod_bus2

View file

@ -12,3 +12,4 @@ modbus_controller:
- id: modbus_controller1 - id: modbus_controller1
address: 0x2 address: 0x2
modbus_id: mod_bus1 modbus_id: mod_bus1
allow_duplicate_commands: true

View file

@ -0,0 +1,29 @@
wifi:
ssid: MySSID
password: password1
statsd:
host: "192.168.1.1"
port: 8125
prefix: esphome
update_interval: 60s
sensors:
id: s
name: sensors
binary_sensors:
id: bs
name: binary_sensors
sensor:
- platform: template
id: s
name: "42.1"
lambda: |-
return 42.1f;
binary_sensor:
- platform: template
id: bs
name: "On"
lambda: |-
return true;

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -33,6 +33,7 @@ speaker:
voice_assistant: voice_assistant:
microphone: mic_id_external microphone: mic_id_external
speaker: speaker_id speaker: speaker_id
conversation_timeout: 60s
on_listening: on_listening:
- logger.log: "Voice assistant microphone listening" - logger.log: "Voice assistant microphone listening"
on_start: on_start: