diff --git a/.dockerignore b/.dockerignore
index 9f14b98059..7998ff877f 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -75,6 +75,9 @@ target/
# pyenv
.python-version
+# asdf
+.tool-versions
+
# celery beat schedule file
celerybeat-schedule
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index 3bf9c4e1f6..5703d39be1 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -7,11 +7,16 @@
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
+- [ ] Code quality improvements to existing code or addition of tests
- [ ] Other
-**Related issue or feature (if applicable):** fixes
+**Related issue or feature (if applicable):**
-**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#
+- fixes
+
+**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):**
+
+- esphome/esphome-docs#
## Test Environment
@@ -23,12 +28,6 @@
- [ ] RTL87xx
## Example entry for `config.yaml`:
-
```yaml
# Example config.yaml
diff --git a/.github/actions/restore-python/action.yml b/.github/actions/restore-python/action.yml
index 1f5812691e..06c54578f5 100644
--- a/.github/actions/restore-python/action.yml
+++ b/.github/actions/restore-python/action.yml
@@ -17,12 +17,12 @@ runs:
steps:
- name: Set up Python ${{ inputs.python-version }}
id: python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: ${{ inputs.python-version }}
- name: Restore Python virtual environment
id: cache-venv
- uses: actions/cache/restore@v4.1.1
+ uses: actions/cache/restore@v4.1.2
with:
path: venv
# yamllint disable-line rule:line-length
diff --git a/.github/workflows/ci-api-proto.yml b/.github/workflows/ci-api-proto.yml
index 8112c4e0ff..a6b2e2b2b3 100644
--- a/.github/workflows/ci-api-proto.yml
+++ b/.github/workflows/ci-api-proto.yml
@@ -23,7 +23,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4.1.7
- name: Set up Python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: "3.11"
diff --git a/.github/workflows/ci-docker.yml b/.github/workflows/ci-docker.yml
index f003e5d24c..435a58498e 100644
--- a/.github/workflows/ci-docker.yml
+++ b/.github/workflows/ci-docker.yml
@@ -42,7 +42,7 @@ jobs:
steps:
- uses: actions/checkout@v4.1.7
- name: Set up Python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: "3.9"
- name: Set up Docker Buildx
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 178b914a1c..f5af3ec9e9 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -41,12 +41,12 @@ jobs:
run: echo key="${{ hashFiles('requirements.txt', 'requirements_optional.txt', 'requirements_test.txt') }}" >> $GITHUB_OUTPUT
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
id: python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Restore Python virtual environment
id: cache-venv
- uses: actions/cache@v4.1.1
+ uses: actions/cache@v4.1.2
with:
path: venv
# yamllint disable-line rule:line-length
@@ -219,7 +219,7 @@ jobs:
. venv/bin/activate
pytest -vv --cov-report=xml --tb=native tests
- name: Upload coverage to Codecov
- uses: codecov/codecov-action@v4
+ uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
@@ -302,14 +302,14 @@ jobs:
- name: Cache platformio
if: github.ref == 'refs/heads/dev'
- uses: actions/cache@v4.1.1
+ uses: actions/cache@v4.1.2
with:
path: ~/.platformio
key: platformio-${{ matrix.pio_cache_key }}
- name: Cache platformio
if: github.ref != 'refs/heads/dev'
- uses: actions/cache/restore@v4.1.1
+ uses: actions/cache/restore@v4.1.2
with:
path: ~/.platformio
key: platformio-${{ matrix.pio_cache_key }}
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 26a213f170..096b00f0f1 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -53,7 +53,7 @@ jobs:
steps:
- uses: actions/checkout@v4.1.7
- name: Set up Python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: "3.x"
- name: Set up python environment
@@ -65,7 +65,7 @@ jobs:
pip3 install build
python3 -m build
- name: Publish
- uses: pypa/gh-action-pypi-publish@v1.10.3
+ uses: pypa/gh-action-pypi-publish@v1.12.2
deploy-docker:
name: Build ESPHome ${{ matrix.platform }}
@@ -85,7 +85,7 @@ jobs:
steps:
- uses: actions/checkout@v4.1.7
- name: Set up Python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: "3.9"
diff --git a/.github/workflows/sync-device-classes.yml b/.github/workflows/sync-device-classes.yml
index c066ae9fb4..7a46d596a1 100644
--- a/.github/workflows/sync-device-classes.yml
+++ b/.github/workflows/sync-device-classes.yml
@@ -22,7 +22,7 @@ jobs:
path: lib/home-assistant
- name: Setup Python
- uses: actions/setup-python@v5.2.0
+ uses: actions/setup-python@v5.3.0
with:
python-version: 3.12
diff --git a/.gitignore b/.gitignore
index 79820249ac..ad38e26fdd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -75,6 +75,9 @@ cov.xml
# pyenv
.python-version
+# asdf
+.tool-versions
+
# Environments
.env
.venv
diff --git a/CODEOWNERS b/CODEOWNERS
index a8849cd44a..26e4f5afae 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -85,6 +85,7 @@ esphome/components/bmp581/* @kahrendt
esphome/components/bp1658cj/* @Cossid
esphome/components/bp5758d/* @Cossid
esphome/components/button/* @esphome/core
+esphome/components/bytebuffer/* @clydebarrow
esphome/components/canbus/* @danielschramm @mvturnho
esphome/components/cap1188/* @mreditor97
esphome/components/captive_portal/* @OttoWinter
@@ -130,6 +131,7 @@ esphome/components/ens160_base/* @latonita @vincentscode
esphome/components/ens160_i2c/* @latonita
esphome/components/ens160_spi/* @latonita
esphome/components/ens210/* @itn3rd77
+esphome/components/es8311/* @kahrendt @kroimon
esphome/components/esp32/* @esphome/core
esphome/components/esp32_ble/* @Rapsssito @jesserockz
esphome/components/esp32_ble_client/* @jesserockz
@@ -198,10 +200,11 @@ esphome/components/htu31d/* @betterengineering
esphome/components/hydreon_rgxx/* @functionpointer
esphome/components/hyt271/* @Philippe12
esphome/components/i2c/* @esphome/core
+esphome/components/i2c_device/* @gabest11
esphome/components/i2s_audio/* @jesserockz
esphome/components/i2s_audio/media_player/* @jesserockz
esphome/components/i2s_audio/microphone/* @jesserockz
-esphome/components/i2s_audio/speaker/* @jesserockz
+esphome/components/i2s_audio/speaker/* @jesserockz @kahrendt
esphome/components/iaqcore/* @yozik04
esphome/components/ili9xxx/* @clydebarrow @nielsnl68
esphome/components/improv_base/* @esphome/core
@@ -377,7 +380,7 @@ esphome/components/smt100/* @piechade
esphome/components/sn74hc165/* @jesserockz
esphome/components/socket/* @esphome/core
esphome/components/sonoff_d1/* @anatoly-savchenkov
-esphome/components/speaker/* @jesserockz
+esphome/components/speaker/* @jesserockz @kahrendt
esphome/components/spi/* @clydebarrow @esphome/core
esphome/components/spi_device/* @clydebarrow
esphome/components/spi_led_strip/* @clydebarrow
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 52a4794f24..ed6ce083a8 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -32,33 +32,14 @@ RUN \
python3-setuptools=66.1.1-1 \
python3-venv=3.11.2-1+b1 \
python3-wheel=0.38.4-2 \
- iputils-ping=3:20221126-1 \
+ iputils-ping=3:20221126-1+deb12u1 \
git=1:2.39.5-0+deb12u1 \
- curl=7.88.1-10+deb12u7 \
+ curl=7.88.1-10+deb12u8 \
openssh-client=1:9.2p1-2+deb12u3 \
python3-cffi=1.15.1-5 \
libcairo2=1.16.0-7 \
libmagic1=1:5.44-3 \
patch=2.7.6-7 \
- && ( \
- ( \
- [ "$TARGETARCH$TARGETVARIANT" = "armv7" ] && \
- apt-get install -y --no-install-recommends \
- build-essential=12.9 \
- python3-dev=3.11.2-1+b1 \
- zlib1g-dev=1:1.2.13.dfsg-1 \
- libjpeg-dev=1:2.1.5-2 \
- libfreetype-dev=2.12.1+dfsg-5+deb12u3 \
- libssl-dev=3.0.14-1~deb12u2 \
- libffi-dev=3.4.4-1 \
- libopenjp2-7=2.5.0-2 \
- libtiff6=4.5.0-6+deb12u1 \
- cargo=0.66.0+ds1-1 \
- pkg-config=1.8.1-1 \
- gcc-arm-linux-gnueabihf=4:12.2.0-3 \
- ) \
- || [ "$TARGETARCH$TARGETVARIANT" != "armv7" ] \
- ) \
&& rm -rf \
/tmp/* \
/var/{cache,log}/* \
@@ -97,15 +78,48 @@ RUN \
# tmpfs is for https://github.com/rust-lang/cargo/issues/8719
COPY requirements.txt requirements_optional.txt /
-RUN --mount=type=tmpfs,target=/root/.cargo if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \
- curl -L https://www.piwheels.org/cp311/cryptography-43.0.0-cp37-abi3-linux_armv7l.whl -o /tmp/cryptography-43.0.0-cp37-abi3-linux_armv7l.whl \
- && pip3 install --break-system-packages --no-cache-dir /tmp/cryptography-43.0.0-cp37-abi3-linux_armv7l.whl \
- && rm /tmp/cryptography-43.0.0-cp37-abi3-linux_armv7l.whl \
- && export PIP_EXTRA_INDEX_URL="https://www.piwheels.org/simple"; \
- fi; \
- CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse CARGO_HOME=/root/.cargo \
- pip3 install \
- --break-system-packages --no-cache-dir -r /requirements.txt -r /requirements_optional.txt
+RUN --mount=type=tmpfs,target=/root/.cargo <> 2
B = b >> 3
rgb = (R << 11) | (G << 5) | B
-
- if transparent:
- if rgb == 0x0020:
- rgb = 0
- if a < 0x80:
- rgb = 0x0020
-
data[pos] = rgb >> 8
pos += 1
data[pos] = rgb & 0xFF
pos += 1
+ if transparent:
+ data[pos] = a
+ pos += 1
elif config[CONF_TYPE] in ["BINARY", "TRANSPARENT_BINARY"]:
width8 = ((width + 7) // 8) * 8
diff --git a/esphome/components/animation/animation.cpp b/esphome/components/animation/animation.cpp
index 7e0efa97e0..1375dfe07e 100644
--- a/esphome/components/animation/animation.cpp
+++ b/esphome/components/animation/animation.cpp
@@ -62,7 +62,7 @@ void Animation::set_frame(int frame) {
}
void Animation::update_data_start_() {
- const uint32_t image_size = image_type_to_width_stride(this->width_, this->type_) * this->height_;
+ const uint32_t image_size = this->get_width_stride() * this->height_;
this->data_start_ = this->animation_data_start_ + image_size * this->current_frame_;
}
diff --git a/esphome/components/ble_rssi/sensor.py b/esphome/components/ble_rssi/sensor.py
index e3ba1abfd7..c4e767aa21 100644
--- a/esphome/components/ble_rssi/sensor.py
+++ b/esphome/components/ble_rssi/sensor.py
@@ -45,7 +45,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_SERVICE_UUID): esp32_ble_tracker.bt_uuid,
cv.Optional(CONF_IBEACON_MAJOR): cv.uint16_t,
cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t,
- cv.Optional(CONF_IBEACON_UUID): cv.uuid,
+ cv.Optional(CONF_IBEACON_UUID): esp32_ble_tracker.bt_uuid,
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
@@ -79,7 +79,7 @@ async def to_code(config):
cg.add(var.set_service_uuid128(uuid128))
if ibeacon_uuid := config.get(CONF_IBEACON_UUID):
- ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(ibeacon_uuid))
+ ibeacon_uuid = esp32_ble_tracker.as_reversed_hex_array(ibeacon_uuid)
cg.add(var.set_ibeacon_uuid(ibeacon_uuid))
if (ibeacon_major := config.get(CONF_IBEACON_MAJOR)) is not None:
diff --git a/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp b/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp
index 5425bbd5b7..f83f20f1a5 100644
--- a/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp
+++ b/esphome/components/bme68x_bsec2/bme68x_bsec2.cpp
@@ -204,11 +204,11 @@ void BME68xBSEC2Component::update_subscription_() {
}
void BME68xBSEC2Component::run_() {
+ this->op_mode_ = this->bsec_settings_.op_mode;
int64_t curr_time_ns = this->get_time_ns_();
- if (curr_time_ns < this->next_call_ns_) {
+ if (curr_time_ns < this->bsec_settings_.next_call) {
return;
}
- this->op_mode_ = this->bsec_settings_.op_mode;
uint8_t status;
ESP_LOGV(TAG, "Performing sensor run");
@@ -219,57 +219,60 @@ void BME68xBSEC2Component::run_() {
ESP_LOGW(TAG, "Failed to fetch sensor control settings (BSEC2 error code %d)", this->bsec_status_);
return;
}
- this->next_call_ns_ = this->bsec_settings_.next_call;
- if (this->bsec_settings_.trigger_measurement) {
- bme68x_get_conf(&bme68x_conf, &this->bme68x_);
+ switch (this->bsec_settings_.op_mode) {
+ case BME68X_FORCED_MODE:
+ bme68x_get_conf(&bme68x_conf, &this->bme68x_);
- bme68x_conf.os_hum = this->bsec_settings_.humidity_oversampling;
- bme68x_conf.os_temp = this->bsec_settings_.temperature_oversampling;
- bme68x_conf.os_pres = this->bsec_settings_.pressure_oversampling;
- bme68x_set_conf(&bme68x_conf, &this->bme68x_);
+ bme68x_conf.os_hum = this->bsec_settings_.humidity_oversampling;
+ bme68x_conf.os_temp = this->bsec_settings_.temperature_oversampling;
+ bme68x_conf.os_pres = this->bsec_settings_.pressure_oversampling;
+ bme68x_set_conf(&bme68x_conf, &this->bme68x_);
+ this->bme68x_heatr_conf_.enable = BME68X_ENABLE;
+ this->bme68x_heatr_conf_.heatr_temp = this->bsec_settings_.heater_temperature;
+ this->bme68x_heatr_conf_.heatr_dur = this->bsec_settings_.heater_duration;
+
+ // status = bme68x_set_op_mode(this->bsec_settings_.op_mode, &this->bme68x_);
+ status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
+ status = bme68x_set_op_mode(BME68X_FORCED_MODE, &this->bme68x_);
+ this->op_mode_ = BME68X_FORCED_MODE;
+ ESP_LOGV(TAG, "Using forced mode");
+
+ break;
+ case BME68X_PARALLEL_MODE:
+ if (this->op_mode_ != this->bsec_settings_.op_mode) {
+ bme68x_get_conf(&bme68x_conf, &this->bme68x_);
+
+ bme68x_conf.os_hum = this->bsec_settings_.humidity_oversampling;
+ bme68x_conf.os_temp = this->bsec_settings_.temperature_oversampling;
+ bme68x_conf.os_pres = this->bsec_settings_.pressure_oversampling;
+ bme68x_set_conf(&bme68x_conf, &this->bme68x_);
- switch (this->bsec_settings_.op_mode) {
- case BME68X_FORCED_MODE:
this->bme68x_heatr_conf_.enable = BME68X_ENABLE;
- this->bme68x_heatr_conf_.heatr_temp = this->bsec_settings_.heater_temperature;
- this->bme68x_heatr_conf_.heatr_dur = this->bsec_settings_.heater_duration;
+ this->bme68x_heatr_conf_.heatr_temp_prof = this->bsec_settings_.heater_temperature_profile;
+ this->bme68x_heatr_conf_.heatr_dur_prof = this->bsec_settings_.heater_duration_profile;
+ this->bme68x_heatr_conf_.profile_len = this->bsec_settings_.heater_profile_len;
+ this->bme68x_heatr_conf_.shared_heatr_dur =
+ BSEC_TOTAL_HEAT_DUR -
+ (bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &bme68x_conf, &this->bme68x_) / INT64_C(1000));
- status = bme68x_set_op_mode(this->bsec_settings_.op_mode, &this->bme68x_);
- status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
- status = bme68x_set_op_mode(BME68X_FORCED_MODE, &this->bme68x_);
- this->op_mode_ = BME68X_FORCED_MODE;
- this->sleep_mode_ = false;
- ESP_LOGV(TAG, "Using forced mode");
+ status = bme68x_set_heatr_conf(BME68X_PARALLEL_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
- break;
- case BME68X_PARALLEL_MODE:
- if (this->op_mode_ != this->bsec_settings_.op_mode) {
- this->bme68x_heatr_conf_.enable = BME68X_ENABLE;
- this->bme68x_heatr_conf_.heatr_temp_prof = this->bsec_settings_.heater_temperature_profile;
- this->bme68x_heatr_conf_.heatr_dur_prof = this->bsec_settings_.heater_duration_profile;
- this->bme68x_heatr_conf_.profile_len = this->bsec_settings_.heater_profile_len;
- this->bme68x_heatr_conf_.shared_heatr_dur =
- BSEC_TOTAL_HEAT_DUR -
- (bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &bme68x_conf, &this->bme68x_) / INT64_C(1000));
-
- status = bme68x_set_heatr_conf(BME68X_PARALLEL_MODE, &this->bme68x_heatr_conf_, &this->bme68x_);
-
- status = bme68x_set_op_mode(BME68X_PARALLEL_MODE, &this->bme68x_);
- this->op_mode_ = BME68X_PARALLEL_MODE;
- this->sleep_mode_ = false;
- ESP_LOGV(TAG, "Using parallel mode");
- }
- break;
- case BME68X_SLEEP_MODE:
- if (!this->sleep_mode_) {
- bme68x_set_op_mode(BME68X_SLEEP_MODE, &this->bme68x_);
- this->sleep_mode_ = true;
- ESP_LOGV(TAG, "Using sleep mode");
- }
- break;
- }
+ status = bme68x_set_op_mode(BME68X_PARALLEL_MODE, &this->bme68x_);
+ this->op_mode_ = BME68X_PARALLEL_MODE;
+ ESP_LOGV(TAG, "Using parallel mode");
+ }
+ break;
+ case BME68X_SLEEP_MODE:
+ if (this->op_mode_ != this->bsec_settings_.op_mode) {
+ bme68x_set_op_mode(BME68X_SLEEP_MODE, &this->bme68x_);
+ this->op_mode_ = BME68X_SLEEP_MODE;
+ ESP_LOGV(TAG, "Using sleep mode");
+ }
+ break;
+ }
+ if (this->bsec_settings_.trigger_measurement && this->bsec_settings_.op_mode != BME68X_SLEEP_MODE) {
uint32_t meas_dur = 0;
meas_dur = bme68x_get_meas_dur(this->op_mode_, &bme68x_conf, &this->bme68x_);
ESP_LOGV(TAG, "Queueing read in %uus", meas_dur);
diff --git a/esphome/components/bme68x_bsec2/bme68x_bsec2.h b/esphome/components/bme68x_bsec2/bme68x_bsec2.h
index 7b9db2b7bf..86d3e5dfbf 100644
--- a/esphome/components/bme68x_bsec2/bme68x_bsec2.h
+++ b/esphome/components/bme68x_bsec2/bme68x_bsec2.h
@@ -113,13 +113,11 @@ class BME68xBSEC2Component : public Component {
struct bme68x_heatr_conf bme68x_heatr_conf_;
uint8_t op_mode_; // operating mode of sensor
- bool sleep_mode_;
bsec_library_return_t bsec_status_{BSEC_OK};
int8_t bme68x_status_{BME68X_OK};
int64_t last_time_ms_{0};
uint32_t millis_overflow_counter_{0};
- int64_t next_call_ns_{0};
std::queue> queue_;
diff --git a/esphome/components/bytebuffer/__init__.py b/esphome/components/bytebuffer/__init__.py
new file mode 100644
index 0000000000..3c7c695118
--- /dev/null
+++ b/esphome/components/bytebuffer/__init__.py
@@ -0,0 +1,5 @@
+CODEOWNERS = ["@clydebarrow"]
+
+# Allows bytebuffer to be configured in yaml, to allow use of the C++ api.
+
+CONFIG_SCHEMA = {}
diff --git a/esphome/components/bytebuffer/bytebuffer.h b/esphome/components/bytebuffer/bytebuffer.h
new file mode 100644
index 0000000000..030484ce32
--- /dev/null
+++ b/esphome/components/bytebuffer/bytebuffer.h
@@ -0,0 +1,421 @@
+#pragma once
+
+#include
+#include
+#include
+#include
+#include "esphome/core/helpers.h"
+
+namespace esphome {
+namespace bytebuffer {
+
+enum Endian { LITTLE, BIG };
+
+/**
+ * A class modelled on the Java ByteBuffer class. It wraps a vector of bytes and permits putting and getting
+ * items of various sizes, with an automatically incremented position.
+ *
+ * There are three variables maintained pointing into the buffer:
+ *
+ * capacity: the maximum amount of data that can be stored - set on construction and cannot be changed
+ * limit: the limit of the data currently available to get or put
+ * position: the current insert or extract position
+ *
+ * 0 <= position <= limit <= capacity
+ *
+ * In addition a mark can be set to the current position with mark(). A subsequent call to reset() will restore
+ * the position to the mark.
+ *
+ * The buffer can be marked to be little-endian (default) or big-endian. All subsequent operations will use that order.
+ *
+ * The flip() operation will reset the position to 0 and limit to the current position. This is useful for reading
+ * data from a buffer after it has been written.
+ *
+ * The code is defined here in the header file rather than in a .cpp file, so that it does not get compiled if not used.
+ * The templated functions ensure that only those typed functions actually used are compiled. The functions
+ * are implicitly inline-able which will aid performance.
+ */
+class ByteBuffer {
+ public:
+ // Default constructor (compatibility with TEMPLATABLE_VALUE)
+ // Creates a zero-length ByteBuffer which is little use to anybody.
+ ByteBuffer() : ByteBuffer(std::vector()) {}
+
+ /**
+ * Create a new Bytebuffer with the given capacity
+ */
+ ByteBuffer(size_t capacity, Endian endianness = LITTLE)
+ : data_(std::vector(capacity)), endianness_(endianness), limit_(capacity){};
+
+ // templated functions to implement putting and getting data of various types. There are two flavours of all
+ // functions - one that uses the position as the offset, and updates the position accordingly, and one that
+ // takes an explicit offset and does not update the position.
+ // Separate temnplates are provided for types that fit into 32 bits and those that are bigger. These delegate
+ // the actual put/get to common code based around those sizes.
+ // This reduces the code size and execution time for smaller types. A similar structure for e.g. 16 bits is unlikely
+ // to provide any further benefit given that all target platforms are native 32 bit.
+
+ template
+ T get(typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
+ // integral types that fit into 32 bit
+ return static_cast(this->get_uint32_(sizeof(T)));
+ }
+
+ template
+ T get(size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
+ return static_cast(this->get_uint32_(offset, sizeof(T)));
+ }
+
+ template
+ void put(const T &value, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
+ this->put_uint32_(static_cast(value), sizeof(T));
+ }
+
+ template
+ void put(const T &value, size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
+ this->put_uint32_(static_cast(value), offset, sizeof(T));
+ }
+
+ // integral types that do not fit into 32 bit (basically only 64 bit types)
+ template
+ T get(typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ return static_cast(this->get_uint64_(sizeof(T)));
+ }
+
+ template
+ T get(size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ return static_cast(this->get_uint64_(offset, sizeof(T)));
+ }
+
+ template
+ void put(const T &value, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ this->put_uint64_(value, sizeof(T));
+ }
+
+ template
+ void put(const T &value, size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ this->put_uint64_(static_cast(value), offset, sizeof(T));
+ }
+
+ // floating point types. Caters for 32 and 64 bit floating point.
+ template
+ T get(typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint32_t)), T>::type * = 0) {
+ return bit_cast(this->get_uint32_(sizeof(T)));
+ }
+
+ template
+ T get(typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ return bit_cast(this->get_uint64_(sizeof(T)));
+ }
+
+ template
+ T get(size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint32_t)), T>::type * = 0) {
+ return bit_cast(this->get_uint32_(offset, sizeof(T)));
+ }
+
+ template
+ T get(size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ return bit_cast(this->get_uint64_(offset, sizeof(T)));
+ }
+ template
+ void put(const T &value, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
+ this->put_uint32_(bit_cast(value), sizeof(T));
+ }
+
+ template
+ void put(const T &value, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ this->put_uint64_(bit_cast(value), sizeof(T));
+ }
+
+ template
+ void put(const T &value, size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
+ this->put_uint32_(bit_cast(value), offset, sizeof(T));
+ }
+
+ template
+ void put(const T &value, size_t offset, typename std::enable_if::value, T>::type * = 0,
+ typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
+ this->put_uint64_(bit_cast(value), offset, sizeof(T));
+ }
+
+ template static ByteBuffer wrap(T value, Endian endianness = LITTLE) {
+ ByteBuffer buffer = ByteBuffer(sizeof(T), endianness);
+ buffer.put(value);
+ buffer.flip();
+ return buffer;
+ }
+
+ static ByteBuffer wrap(std::vector const &data, Endian endianness = LITTLE) {
+ ByteBuffer buffer = {data};
+ buffer.endianness_ = endianness;
+ return buffer;
+ }
+
+ static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE) {
+ return wrap(std::vector(ptr, ptr + len), endianness);
+ }
+
+ // convenience functions with explicit types named..
+ void put_float(float value) { this->put(value); }
+ void put_double(double value) { this->put(value); }
+
+ uint8_t get_uint8() { return this->data_[this->position_++]; }
+ // Get a 16 bit unsigned value, increment by 2
+ uint16_t get_uint16() { return this->get(); }
+ // Get a 24 bit unsigned value, increment by 3
+ uint32_t get_uint24() { return this->get_uint32_(3); };
+ // Get a 32 bit unsigned value, increment by 4
+ uint32_t get_uint32() { return this->get(); };
+ // Get a 64 bit unsigned value, increment by 8
+ uint64_t get_uint64() { return this->get(); };
+ // Signed versions of the get functions
+ uint8_t get_int8() { return static_cast(this->get_uint8()); };
+ int16_t get_int16() { return this->get(); }
+ int32_t get_int32() { return this->get(); }
+ int64_t get_int64() { return this->get(); }
+ // Get a float value, increment by 4
+ float get_float() { return this->get(); }
+ // Get a double value, increment by 8
+ double get_double() { return this->get(); }
+
+ // Get a bool value, increment by 1
+ bool get_bool() { return static_cast(this->get_uint8()); }
+
+ uint32_t get_int24(size_t offset) {
+ auto value = this->get_uint24(offset);
+ uint32_t mask = (~static_cast(0)) << 23;
+ if ((value & mask) != 0)
+ value |= mask;
+ return value;
+ }
+
+ uint32_t get_int24() {
+ auto value = this->get_uint24();
+ uint32_t mask = (~static_cast(0)) << 23;
+ if ((value & mask) != 0)
+ value |= mask;
+ return value;
+ }
+ std::vector get_vector(size_t length, size_t offset) {
+ auto start = this->data_.begin() + offset;
+ return {start, start + length};
+ }
+
+ std::vector get_vector(size_t length) {
+ auto result = this->get_vector(length, this->position_);
+ this->position_ += length;
+ return result;
+ }
+
+ // Convenience named functions
+ void put_uint8(uint8_t value) { this->data_[this->position_++] = value; }
+ void put_uint16(uint16_t value) { this->put(value); }
+ void put_uint24(uint32_t value) { this->put_uint32_(value, 3); }
+ void put_uint32(uint32_t value) { this->put(value); }
+ void put_uint64(uint64_t value) { this->put(value); }
+ // Signed versions of the put functions
+ void put_int8(int8_t value) { this->put_uint8(static_cast(value)); }
+ void put_int16(int16_t value) { this->put(value); }
+ void put_int24(int32_t value) { this->put_uint32_(value, 3); }
+ void put_int32(int32_t value) { this->put(value); }
+ void put_int64(int64_t value) { this->put(value); }
+ // Extra put functions
+ void put_bool(bool value) { this->put_uint8(value); }
+
+ // versions of the above with an offset, these do not update the position
+
+ uint64_t get_uint64(size_t offset) { return this->get(offset); }
+ uint32_t get_uint24(size_t offset) { return this->get_uint32_(offset, 3); };
+ double get_double(size_t offset) { return get(offset); }
+
+ // Get one byte from the buffer, increment position by 1
+ uint8_t get_uint8(size_t offset) { return this->data_[offset]; }
+ // Get a 16 bit unsigned value, increment by 2
+ uint16_t get_uint16(size_t offset) { return get(offset); }
+ // Get a 24 bit unsigned value, increment by 3
+ uint32_t get_uint32(size_t offset) { return this->get(offset); };
+ // Get a 64 bit unsigned value, increment by 8
+ uint8_t get_int8(size_t offset) { return get(offset); }
+ int16_t get_int16(size_t offset) { return get(offset); }
+ int32_t get_int32(size_t offset) { return get(offset); }
+ int64_t get_int64(size_t offset) { return get(offset); }
+ // Get a float value, increment by 4
+ float get_float(size_t offset) { return get(offset); }
+ // Get a double value, increment by 8
+
+ // Get a bool value, increment by 1
+ bool get_bool(size_t offset) { return this->get_uint8(offset); }
+
+ void put_uint8(uint8_t value, size_t offset) { this->data_[offset] = value; }
+ void put_uint16(uint16_t value, size_t offset) { this->put(value, offset); }
+ void put_uint24(uint32_t value, size_t offset) { this->put(value, offset); }
+ void put_uint32(uint32_t value, size_t offset) { this->put(value, offset); }
+ void put_uint64(uint64_t value, size_t offset) { this->put(value, offset); }
+ // Signed versions of the put functions
+ void put_int8(int8_t value, size_t offset) { this->put_uint8(static_cast(value), offset); }
+ void put_int16(int16_t value, size_t offset) { this->put(value, offset); }
+ void put_int24(int32_t value, size_t offset) { this->put_uint32_(value, offset, 3); }
+ void put_int32(int32_t value, size_t offset) { this->put(value, offset); }
+ void put_int64(int64_t value, size_t offset) { this->put(value, offset); }
+ // Extra put functions
+ void put_float(float value, size_t offset) { this->put(value, offset); }
+ void put_double(double value, size_t offset) { this->put(value, offset); }
+ void put_bool(bool value, size_t offset) { this->put_uint8(value, offset); }
+ void put(const std::vector &value, size_t offset) {
+ std::copy(value.begin(), value.end(), this->data_.begin() + offset);
+ }
+ void put_vector(const std::vector &value, size_t offset) { this->put(value, offset); }
+ void put(const std::vector &value) {
+ this->put_vector(value, this->position_);
+ this->position_ += value.size();
+ }
+ void put_vector(const std::vector &value) { this->put(value); }
+
+ // Getters
+
+ inline size_t get_capacity() const { return this->data_.size(); }
+ inline size_t get_position() const { return this->position_; }
+ inline size_t get_limit() const { return this->limit_; }
+ inline size_t get_remaining() const { return this->get_limit() - this->get_position(); }
+ inline Endian get_endianness() const { return this->endianness_; }
+ inline void mark() { this->mark_ = this->position_; }
+ inline void big_endian() { this->endianness_ = BIG; }
+ inline void little_endian() { this->endianness_ = LITTLE; }
+ // retrieve a pointer to the underlying data.
+ std::vector get_data() { return this->data_; };
+
+ void get_bytes(void *dest, size_t length) {
+ std::copy(this->data_.begin() + this->position_, this->data_.begin() + this->position_ + length, (uint8_t *) dest);
+ this->position_ += length;
+ }
+
+ void get_bytes(void *dest, size_t length, size_t offset) {
+ std::copy(this->data_.begin() + offset, this->data_.begin() + offset + length, (uint8_t *) dest);
+ }
+
+ void rewind() { this->position_ = 0; }
+ void reset() { this->position_ = this->mark_; }
+
+ void set_limit(size_t limit) { this->limit_ = limit; }
+ void set_position(size_t position) { this->position_ = position; }
+ void clear() {
+ this->limit_ = this->get_capacity();
+ this->position_ = 0;
+ }
+ void flip() {
+ this->limit_ = this->position_;
+ this->position_ = 0;
+ }
+
+ protected:
+ uint64_t get_uint64_(size_t offset, size_t length) const {
+ uint64_t value = 0;
+ if (this->endianness_ == LITTLE) {
+ offset += length;
+ while (length-- != 0) {
+ value <<= 8;
+ value |= this->data_[--offset];
+ }
+ } else {
+ while (length-- != 0) {
+ value <<= 8;
+ value |= this->data_[offset++];
+ }
+ }
+ return value;
+ }
+
+ uint64_t get_uint64_(size_t length) {
+ auto result = this->get_uint64_(this->position_, length);
+ this->position_ += length;
+ return result;
+ }
+ uint32_t get_uint32_(size_t offset, size_t length) const {
+ uint32_t value = 0;
+ if (this->endianness_ == LITTLE) {
+ offset += length;
+ while (length-- != 0) {
+ value <<= 8;
+ value |= this->data_[--offset];
+ }
+ } else {
+ while (length-- != 0) {
+ value <<= 8;
+ value |= this->data_[offset++];
+ }
+ }
+ return value;
+ }
+
+ uint32_t get_uint32_(size_t length) {
+ auto result = this->get_uint32_(this->position_, length);
+ this->position_ += length;
+ return result;
+ }
+
+ /// Putters
+
+ void put_uint64_(uint64_t value, size_t length) {
+ this->put_uint64_(value, this->position_, length);
+ this->position_ += length;
+ }
+ void put_uint32_(uint32_t value, size_t length) {
+ this->put_uint32_(value, this->position_, length);
+ this->position_ += length;
+ }
+
+ void put_uint64_(uint64_t value, size_t offset, size_t length) {
+ if (this->endianness_ == LITTLE) {
+ while (length-- != 0) {
+ this->data_[offset++] = static_cast(value);
+ value >>= 8;
+ }
+ } else {
+ offset += length;
+ while (length-- != 0) {
+ this->data_[--offset] = static_cast(value);
+ value >>= 8;
+ }
+ }
+ }
+
+ void put_uint32_(uint32_t value, size_t offset, size_t length) {
+ if (this->endianness_ == LITTLE) {
+ while (length-- != 0) {
+ this->data_[offset++] = static_cast(value);
+ value >>= 8;
+ }
+ } else {
+ offset += length;
+ while (length-- != 0) {
+ this->data_[--offset] = static_cast(value);
+ value >>= 8;
+ }
+ }
+ }
+ ByteBuffer(std::vector const &data) : data_(data), limit_(data.size()) {}
+
+ std::vector data_;
+ Endian endianness_{LITTLE};
+ size_t position_{0};
+ size_t mark_{0};
+ size_t limit_{0};
+};
+
+} // namespace bytebuffer
+} // namespace esphome
diff --git a/esphome/components/climate/__init__.py b/esphome/components/climate/__init__.py
index b302e2ab4e..ec68940726 100644
--- a/esphome/components/climate/__init__.py
+++ b/esphome/components/climate/__init__.py
@@ -119,10 +119,21 @@ visual_temperature = cv.float_with_unit(
)
-def single_visual_temperature(value):
- if isinstance(value, dict):
- return value
+VISUAL_TEMPERATURE_STEP_SCHEMA = cv.Schema(
+ {
+ cv.Required(CONF_TARGET_TEMPERATURE): visual_temperature,
+ cv.Required(CONF_CURRENT_TEMPERATURE): visual_temperature,
+ }
+)
+
+def visual_temperature_step(value):
+
+ # Allow defining target/current temperature steps separately
+ if isinstance(value, dict):
+ return VISUAL_TEMPERATURE_STEP_SCHEMA(value)
+
+ # Otherwise, use the single value for both properties
value = visual_temperature(value)
return VISUAL_TEMPERATURE_STEP_SCHEMA(
{
@@ -141,16 +152,6 @@ ControlTrigger = climate_ns.class_(
"ControlTrigger", automation.Trigger.template(ClimateCall.operator("ref"))
)
-VISUAL_TEMPERATURE_STEP_SCHEMA = cv.Any(
- single_visual_temperature,
- cv.Schema(
- {
- cv.Required(CONF_TARGET_TEMPERATURE): visual_temperature,
- cv.Required(CONF_CURRENT_TEMPERATURE): visual_temperature,
- }
- ),
-)
-
CLIMATE_SCHEMA = (
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
@@ -162,7 +163,7 @@ CLIMATE_SCHEMA = (
{
cv.Optional(CONF_MIN_TEMPERATURE): cv.temperature,
cv.Optional(CONF_MAX_TEMPERATURE): cv.temperature,
- cv.Optional(CONF_TEMPERATURE_STEP): VISUAL_TEMPERATURE_STEP_SCHEMA,
+ cv.Optional(CONF_TEMPERATURE_STEP): visual_temperature_step,
cv.Optional(CONF_MIN_HUMIDITY): cv.percentage_int,
cv.Optional(CONF_MAX_HUMIDITY): cv.percentage_int,
}
diff --git a/esphome/components/datetime/__init__.py b/esphome/components/datetime/__init__.py
index 7edf527e01..630bf6962c 100644
--- a/esphome/components/datetime/__init__.py
+++ b/esphome/components/datetime/__init__.py
@@ -70,8 +70,6 @@ def _validate_time_present(config):
_DATETIME_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
- web_server.WEBSERVER_SORTING_SCHEMA,
- cv.MQTT_COMMAND_COMPONENT_SCHEMA,
cv.Schema(
{
cv.Optional(CONF_ON_VALUE): automation.validate_automation(
@@ -81,7 +79,9 @@ _DATETIME_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
),
cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
}
- ),
+ )
+ .extend(web_server.WEBSERVER_SORTING_SCHEMA)
+ .extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
).add_extra(_validate_time_present)
diff --git a/esphome/components/dfplayer/dfplayer.cpp b/esphome/components/dfplayer/dfplayer.cpp
index aa2dc260e0..98c3e91e46 100644
--- a/esphome/components/dfplayer/dfplayer.cpp
+++ b/esphome/components/dfplayer/dfplayer.cpp
@@ -6,7 +6,104 @@ namespace dfplayer {
static const char *const TAG = "dfplayer";
+void DFPlayer::next() {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing next track");
+ this->send_cmd_(0x01);
+}
+
+void DFPlayer::previous() {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing previous track");
+ this->send_cmd_(0x02);
+}
+void DFPlayer::play_mp3(uint16_t file) {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing file %d in mp3 folder", file);
+ this->send_cmd_(0x12, file);
+}
+
+void DFPlayer::play_file(uint16_t file) {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing file %d", file);
+ this->send_cmd_(0x03, file);
+}
+
+void DFPlayer::play_file_loop(uint16_t file) {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing file %d in loop", file);
+ this->send_cmd_(0x08, file);
+}
+
+void DFPlayer::play_folder_loop(uint16_t folder) {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing folder %d in loop", folder);
+ this->send_cmd_(0x17, folder);
+}
+
+void DFPlayer::volume_up() {
+ ESP_LOGD(TAG, "Increasing volume");
+ this->send_cmd_(0x04);
+}
+
+void DFPlayer::volume_down() {
+ ESP_LOGD(TAG, "Decreasing volume");
+ this->send_cmd_(0x05);
+}
+
+void DFPlayer::set_device(Device device) {
+ ESP_LOGD(TAG, "Setting device to %d", device);
+ this->send_cmd_(0x09, device);
+}
+
+void DFPlayer::set_volume(uint8_t volume) {
+ ESP_LOGD(TAG, "Setting volume to %d", volume);
+ this->send_cmd_(0x06, volume);
+}
+
+void DFPlayer::set_eq(EqPreset preset) {
+ ESP_LOGD(TAG, "Setting EQ to %d", preset);
+ this->send_cmd_(0x07, preset);
+}
+
+void DFPlayer::sleep() {
+ this->ack_reset_is_playing_ = true;
+ ESP_LOGD(TAG, "Putting DFPlayer to sleep");
+ this->send_cmd_(0x0A);
+}
+
+void DFPlayer::reset() {
+ this->ack_reset_is_playing_ = true;
+ ESP_LOGD(TAG, "Resetting DFPlayer");
+ this->send_cmd_(0x0C);
+}
+
+void DFPlayer::start() {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Starting playback");
+ this->send_cmd_(0x0D);
+}
+
+void DFPlayer::pause() {
+ this->ack_reset_is_playing_ = true;
+ ESP_LOGD(TAG, "Pausing playback");
+ this->send_cmd_(0x0E);
+}
+
+void DFPlayer::stop() {
+ this->ack_reset_is_playing_ = true;
+ ESP_LOGD(TAG, "Stopping playback");
+ this->send_cmd_(0x16);
+}
+
+void DFPlayer::random() {
+ this->ack_set_is_playing_ = true;
+ ESP_LOGD(TAG, "Playing random file");
+ this->send_cmd_(0x18);
+}
+
void DFPlayer::play_folder(uint16_t folder, uint16_t file) {
+ ESP_LOGD(TAG, "Playing file %d in folder %d", file, folder);
if (folder < 100 && file < 256) {
this->ack_set_is_playing_ = true;
this->send_cmd_(0x0F, (uint8_t) folder, (uint8_t) file);
@@ -29,7 +126,7 @@ void DFPlayer::send_cmd_(uint8_t cmd, uint16_t argument) {
this->sent_cmd_ = cmd;
- ESP_LOGD(TAG, "Send Command %#02x arg %#04x", cmd, argument);
+ ESP_LOGV(TAG, "Send Command %#02x arg %#04x", cmd, argument);
this->write_array(buffer, 10);
}
@@ -101,9 +198,37 @@ void DFPlayer::loop() {
ESP_LOGV(TAG, "Nack");
this->ack_set_is_playing_ = false;
this->ack_reset_is_playing_ = false;
- if (argument == 6) {
- ESP_LOGV(TAG, "File not found");
- this->is_playing_ = false;
+ switch (argument) {
+ case 0x01:
+ ESP_LOGE(TAG, "Module is busy or uninitialized");
+ break;
+ case 0x02:
+ ESP_LOGE(TAG, "Module is in sleep mode");
+ break;
+ case 0x03:
+ ESP_LOGE(TAG, "Serial receive error");
+ break;
+ case 0x04:
+ ESP_LOGE(TAG, "Checksum incorrect");
+ break;
+ case 0x05:
+ ESP_LOGE(TAG, "Specified track is out of current track scope");
+ this->is_playing_ = false;
+ break;
+ case 0x06:
+ ESP_LOGE(TAG, "Specified track is not found");
+ this->is_playing_ = false;
+ break;
+ case 0x07:
+ ESP_LOGE(TAG, "Insertion error (an inserting operation only can be done when a track is being played)");
+ break;
+ case 0x08:
+ ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
+ break;
+ case 0x09:
+ ESP_LOGE(TAG, "Entered into sleep mode");
+ this->is_playing_ = false;
+ break;
}
break;
case 0x41:
@@ -113,12 +238,13 @@ void DFPlayer::loop() {
this->ack_set_is_playing_ = false;
this->ack_reset_is_playing_ = false;
break;
- case 0x3D: // Playback finished
+ case 0x3D:
+ ESP_LOGV(TAG, "Playback finished");
this->is_playing_ = false;
this->on_finished_playback_callback_.call();
break;
default:
- ESP_LOGD(TAG, "Command %#02x arg %#04x", cmd, argument);
+ ESP_LOGV(TAG, "Received unknown cmd %#02x arg %#04x", cmd, argument);
}
this->sent_cmd_ = 0;
this->read_pos_ = 0;
diff --git a/esphome/components/dfplayer/dfplayer.h b/esphome/components/dfplayer/dfplayer.h
index 26e90fd410..d2ec0a2310 100644
--- a/esphome/components/dfplayer/dfplayer.h
+++ b/esphome/components/dfplayer/dfplayer.h
@@ -23,64 +23,30 @@ enum Device {
TF_CARD = 2,
};
+// See the datasheet here:
+// https://github.com/DFRobot/DFRobotDFPlayerMini/blob/master/doc/FN-M16P%2BEmbedded%2BMP3%2BAudio%2BModule%2BDatasheet.pdf
class DFPlayer : public uart::UARTDevice, public Component {
public:
void loop() override;
- void next() {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x01);
- }
- void previous() {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x02);
- }
- void play_mp3(uint16_t file) {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x12, file);
- }
- void play_file(uint16_t file) {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x03, file);
- }
- void play_file_loop(uint16_t file) {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x08, file);
- }
+ void next();
+ void previous();
+ void play_mp3(uint16_t file);
+ void play_file(uint16_t file);
+ void play_file_loop(uint16_t file);
void play_folder(uint16_t folder, uint16_t file);
- void play_folder_loop(uint16_t folder) {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x17, folder);
- }
- void volume_up() { this->send_cmd_(0x04); }
- void volume_down() { this->send_cmd_(0x05); }
- void set_device(Device device) { this->send_cmd_(0x09, device); }
- void set_volume(uint8_t volume) { this->send_cmd_(0x06, volume); }
- void set_eq(EqPreset preset) { this->send_cmd_(0x07, preset); }
- void sleep() {
- this->ack_reset_is_playing_ = true;
- this->send_cmd_(0x0A);
- }
- void reset() {
- this->ack_reset_is_playing_ = true;
- this->send_cmd_(0x0C);
- }
- void start() {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x0D);
- }
- void pause() {
- this->ack_reset_is_playing_ = true;
- this->send_cmd_(0x0E);
- }
- void stop() {
- this->ack_reset_is_playing_ = true;
- this->send_cmd_(0x16);
- }
- void random() {
- this->ack_set_is_playing_ = true;
- this->send_cmd_(0x18);
- }
+ void play_folder_loop(uint16_t folder);
+ void volume_up();
+ void volume_down();
+ void set_device(Device device);
+ void set_volume(uint8_t volume);
+ void set_eq(EqPreset preset);
+ void sleep();
+ void reset();
+ void start();
+ void pause();
+ void stop();
+ void random();
bool is_playing() { return is_playing_; }
void dump_config() override;
diff --git a/esphome/components/es8311/__init__.py b/esphome/components/es8311/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/esphome/components/es8311/audio_dac.py b/esphome/components/es8311/audio_dac.py
new file mode 100644
index 0000000000..1b450c3c11
--- /dev/null
+++ b/esphome/components/es8311/audio_dac.py
@@ -0,0 +1,70 @@
+import esphome.codegen as cg
+from esphome.components import i2c
+from esphome.components.audio_dac import AudioDac
+import esphome.config_validation as cv
+from esphome.const import CONF_BITS_PER_SAMPLE, CONF_ID, CONF_SAMPLE_RATE
+
+CODEOWNERS = ["@kroimon", "@kahrendt"]
+DEPENDENCIES = ["i2c"]
+
+es8311_ns = cg.esphome_ns.namespace("es8311")
+ES8311 = es8311_ns.class_("ES8311", AudioDac, cg.Component, i2c.I2CDevice)
+
+CONF_MIC_GAIN = "mic_gain"
+CONF_USE_MCLK = "use_mclk"
+CONF_USE_MICROPHONE = "use_microphone"
+
+es8311_resolution = es8311_ns.enum("ES8311Resolution")
+ES8311_BITS_PER_SAMPLE_ENUM = {
+ 16: es8311_resolution.ES8311_RESOLUTION_16,
+ 24: es8311_resolution.ES8311_RESOLUTION_24,
+ 32: es8311_resolution.ES8311_RESOLUTION_32,
+}
+
+es8311_mic_gain = es8311_ns.enum("ES8311MicGain")
+ES8311_MIC_GAIN_ENUM = {
+ "MIN": es8311_mic_gain.ES8311_MIC_GAIN_MIN,
+ "0DB": es8311_mic_gain.ES8311_MIC_GAIN_0DB,
+ "6DB": es8311_mic_gain.ES8311_MIC_GAIN_6DB,
+ "12DB": es8311_mic_gain.ES8311_MIC_GAIN_12DB,
+ "18DB": es8311_mic_gain.ES8311_MIC_GAIN_18DB,
+ "24DB": es8311_mic_gain.ES8311_MIC_GAIN_24DB,
+ "30DB": es8311_mic_gain.ES8311_MIC_GAIN_30DB,
+ "36DB": es8311_mic_gain.ES8311_MIC_GAIN_36DB,
+ "42DB": es8311_mic_gain.ES8311_MIC_GAIN_42DB,
+ "MAX": es8311_mic_gain.ES8311_MIC_GAIN_MAX,
+}
+
+
+_validate_bits = cv.float_with_unit("bits", "bit")
+
+CONFIG_SCHEMA = (
+ cv.Schema(
+ {
+ cv.GenerateID(): cv.declare_id(ES8311),
+ cv.Optional(CONF_BITS_PER_SAMPLE, default="16bit"): cv.All(
+ _validate_bits, cv.enum(ES8311_BITS_PER_SAMPLE_ENUM)
+ ),
+ cv.Optional(CONF_MIC_GAIN, default="42DB"): cv.enum(
+ ES8311_MIC_GAIN_ENUM, upper=True
+ ),
+ cv.Optional(CONF_SAMPLE_RATE, default=16000): cv.int_range(min=1),
+ cv.Optional(CONF_USE_MCLK, default=True): cv.boolean,
+ cv.Optional(CONF_USE_MICROPHONE, default=False): cv.boolean,
+ }
+ )
+ .extend(cv.COMPONENT_SCHEMA)
+ .extend(i2c.i2c_device_schema(0x18))
+)
+
+
+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)
+
+ cg.add(var.set_bits_per_sample(config[CONF_BITS_PER_SAMPLE]))
+ cg.add(var.set_mic_gain(config[CONF_MIC_GAIN]))
+ cg.add(var.set_sample_frequency(config[CONF_SAMPLE_RATE]))
+ cg.add(var.set_use_mclk(config[CONF_USE_MCLK]))
+ cg.add(var.set_use_mic(config[CONF_USE_MICROPHONE]))
diff --git a/esphome/components/es8311/es8311.cpp b/esphome/components/es8311/es8311.cpp
new file mode 100644
index 0000000000..1cb1fbbe08
--- /dev/null
+++ b/esphome/components/es8311/es8311.cpp
@@ -0,0 +1,227 @@
+#include "es8311.h"
+#include "es8311_const.h"
+#include "esphome/core/hal.h"
+#include "esphome/core/log.h"
+#include
+
+namespace esphome {
+namespace es8311 {
+
+static const char *const TAG = "es8311";
+
+// Mark the component as failed; use only in setup
+#define ES8311_ERROR_FAILED(func) \
+ if (!(func)) { \
+ this->mark_failed(); \
+ return; \
+ }
+// Return false; use outside of setup
+#define ES8311_ERROR_CHECK(func) \
+ if (!(func)) { \
+ return false; \
+ }
+
+void ES8311::setup() {
+ ESP_LOGCONFIG(TAG, "Setting up ES8311...");
+
+ // Reset
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG00_RESET, 0x1F));
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG00_RESET, 0x00));
+
+ ES8311_ERROR_FAILED(this->configure_clock_());
+ ES8311_ERROR_FAILED(this->configure_format_());
+ ES8311_ERROR_FAILED(this->configure_mic_());
+
+ // Set initial volume
+ this->set_volume(0.75); // 0.75 = 0xBF = 0dB
+
+ // Power up analog circuitry
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG0D_SYSTEM, 0x01));
+ // Enable analog PGA, enable ADC modulator
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG0E_SYSTEM, 0x02));
+ // Power up DAC
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG12_SYSTEM, 0x00));
+ // Enable output to HP drive
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG13_SYSTEM, 0x10));
+ // ADC Equalizer bypass, cancel DC offset in digital domain
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG1C_ADC, 0x6A));
+ // Bypass DAC equalizer
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG37_DAC, 0x08));
+ // Power On
+ ES8311_ERROR_FAILED(this->write_byte(ES8311_REG00_RESET, 0x80));
+}
+
+void ES8311::dump_config() {
+ ESP_LOGCONFIG(TAG, "ES8311 Audio Codec:");
+ ESP_LOGCONFIG(TAG, " Use MCLK: %s", YESNO(this->use_mclk_));
+ ESP_LOGCONFIG(TAG, " Use Microphone: %s", YESNO(this->use_mic_));
+ ESP_LOGCONFIG(TAG, " DAC Bits per Sample: %" PRIu8, this->resolution_out_);
+ ESP_LOGCONFIG(TAG, " Sample Rate: %" PRIu32, this->sample_frequency_);
+
+ if (this->is_failed()) {
+ ESP_LOGCONFIG(TAG, " Failed to initialize!");
+ return;
+ }
+}
+
+bool ES8311::set_volume(float volume) {
+ volume = clamp(volume, 0.0f, 1.0f);
+ uint8_t reg32 = remap(volume, 0.0f, 1.0f, 0, 255);
+ return this->write_byte(ES8311_REG32_DAC, reg32);
+}
+
+float ES8311::volume() {
+ uint8_t reg32;
+ this->read_byte(ES8311_REG32_DAC, ®32);
+ return remap(reg32, 0, 255, 0.0f, 1.0f);
+}
+
+uint8_t ES8311::calculate_resolution_value(ES8311Resolution resolution) {
+ switch (resolution) {
+ case ES8311_RESOLUTION_16:
+ return (3 << 2);
+ case ES8311_RESOLUTION_18:
+ return (2 << 2);
+ case ES8311_RESOLUTION_20:
+ return (1 << 2);
+ case ES8311_RESOLUTION_24:
+ return (0 << 2);
+ case ES8311_RESOLUTION_32:
+ return (4 << 2);
+ default:
+ return 0;
+ }
+}
+
+const ES8311Coefficient *ES8311::get_coefficient(uint32_t mclk, uint32_t rate) {
+ for (const auto &coefficient : ES8311_COEFFICIENTS) {
+ if (coefficient.mclk == mclk && coefficient.rate == rate)
+ return &coefficient;
+ }
+ return nullptr;
+}
+
+bool ES8311::configure_clock_() {
+ // Register 0x01: select clock source for internal MCLK and determine its frequency
+ uint8_t reg01 = 0x3F; // Enable all clocks
+
+ uint32_t mclk_frequency = this->sample_frequency_ * this->mclk_multiple_;
+ if (!this->use_mclk_) {
+ reg01 |= BIT(7); // Use SCLK
+ mclk_frequency = this->sample_frequency_ * (int) this->resolution_out_ * 2;
+ }
+ if (this->mclk_inverted_) {
+ reg01 |= BIT(6); // Invert MCLK pin
+ }
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG01_CLK_MANAGER, reg01));
+
+ // Get clock coefficients from coefficient table
+ auto *coefficient = get_coefficient(mclk_frequency, this->sample_frequency_);
+ if (coefficient == nullptr) {
+ ESP_LOGE(TAG, "Unable to configure sample rate %" PRIu32 "Hz with %" PRIu32 "Hz MCLK", this->sample_frequency_,
+ mclk_frequency);
+ return false;
+ }
+
+ // Register 0x02
+ uint8_t reg02;
+ ES8311_ERROR_CHECK(this->read_byte(ES8311_REG02_CLK_MANAGER, ®02));
+ reg02 &= 0x07;
+ reg02 |= (coefficient->pre_div - 1) << 5;
+ reg02 |= coefficient->pre_mult << 3;
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG02_CLK_MANAGER, reg02));
+
+ // Register 0x03
+ const uint8_t reg03 = (coefficient->fs_mode << 6) | coefficient->adc_osr;
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG03_CLK_MANAGER, reg03));
+
+ // Register 0x04
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG04_CLK_MANAGER, coefficient->dac_osr));
+
+ // Register 0x05
+ const uint8_t reg05 = ((coefficient->adc_div - 1) << 4) | (coefficient->dac_div - 1);
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG05_CLK_MANAGER, reg05));
+
+ // Register 0x06
+ uint8_t reg06;
+ ES8311_ERROR_CHECK(this->read_byte(ES8311_REG06_CLK_MANAGER, ®06));
+ if (this->sclk_inverted_) {
+ reg06 |= BIT(5);
+ } else {
+ reg06 &= ~BIT(5);
+ }
+ reg06 &= 0xE0;
+ if (coefficient->bclk_div < 19) {
+ reg06 |= (coefficient->bclk_div - 1) << 0;
+ } else {
+ reg06 |= (coefficient->bclk_div) << 0;
+ }
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG06_CLK_MANAGER, reg06));
+
+ // Register 0x07
+ uint8_t reg07;
+ ES8311_ERROR_CHECK(this->read_byte(ES8311_REG07_CLK_MANAGER, ®07));
+ reg07 &= 0xC0;
+ reg07 |= coefficient->lrck_h << 0;
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG07_CLK_MANAGER, reg07));
+
+ // Register 0x08
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG08_CLK_MANAGER, coefficient->lrck_l));
+
+ // Successfully configured the clock
+ return true;
+}
+
+bool ES8311::configure_format_() {
+ // Configure I2S mode and format
+ uint8_t reg00;
+ ES8311_ERROR_CHECK(this->read_byte(ES8311_REG00_RESET, ®00));
+ reg00 &= 0xBF;
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG00_RESET, reg00));
+
+ // Configure SDP in resolution
+ uint8_t reg09 = calculate_resolution_value(this->resolution_in_);
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG09_SDPIN, reg09));
+
+ // Configure SDP out resolution
+ uint8_t reg0a = calculate_resolution_value(this->resolution_out_);
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG0A_SDPOUT, reg0a));
+
+ // Successfully configured the format
+ return true;
+}
+
+bool ES8311::configure_mic_() {
+ uint8_t reg14 = 0x1A; // Enable analog MIC and max PGA gain
+ if (this->use_mic_) {
+ reg14 |= BIT(6); // Enable PDM digital microphone
+ }
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG14_SYSTEM, reg14));
+
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG16_ADC, this->mic_gain_)); // ADC gain scale up
+ ES8311_ERROR_CHECK(this->write_byte(ES8311_REG17_ADC, 0xC8)); // Set ADC gain
+
+ // Successfully configured the microphones
+ return true;
+}
+
+bool ES8311::set_mute_state_(bool mute_state) {
+ uint8_t reg31;
+
+ this->is_muted_ = mute_state;
+
+ if (!this->read_byte(ES8311_REG31_DAC, ®31)) {
+ return false;
+ }
+
+ if (mute_state) {
+ reg31 |= BIT(6) | BIT(5);
+ } else {
+ reg31 &= ~(BIT(6) | BIT(5));
+ }
+
+ return this->write_byte(ES8311_REG31_DAC, reg31);
+}
+
+} // namespace es8311
+} // namespace esphome
diff --git a/esphome/components/es8311/es8311.h b/esphome/components/es8311/es8311.h
new file mode 100644
index 0000000000..840a07204c
--- /dev/null
+++ b/esphome/components/es8311/es8311.h
@@ -0,0 +1,135 @@
+#pragma once
+
+#include "esphome/components/audio_dac/audio_dac.h"
+#include "esphome/components/i2c/i2c.h"
+#include "esphome/core/component.h"
+
+namespace esphome {
+namespace es8311 {
+
+enum ES8311MicGain {
+ ES8311_MIC_GAIN_MIN = -1,
+ ES8311_MIC_GAIN_0DB,
+ ES8311_MIC_GAIN_6DB,
+ ES8311_MIC_GAIN_12DB,
+ ES8311_MIC_GAIN_18DB,
+ ES8311_MIC_GAIN_24DB,
+ ES8311_MIC_GAIN_30DB,
+ ES8311_MIC_GAIN_36DB,
+ ES8311_MIC_GAIN_42DB,
+ ES8311_MIC_GAIN_MAX
+};
+
+enum ES8311Resolution : uint8_t {
+ ES8311_RESOLUTION_16 = 16,
+ ES8311_RESOLUTION_18 = 18,
+ ES8311_RESOLUTION_20 = 20,
+ ES8311_RESOLUTION_24 = 24,
+ ES8311_RESOLUTION_32 = 32
+};
+
+struct ES8311Coefficient {
+ uint32_t mclk; // mclk frequency
+ uint32_t rate; // sample rate
+ uint8_t pre_div; // the pre divider with range from 1 to 8
+ uint8_t pre_mult; // the pre multiplier with x1, x2, x4 and x8 selection
+ uint8_t adc_div; // adcclk divider
+ uint8_t dac_div; // dacclk divider
+ uint8_t fs_mode; // single speed (0) or double speed (1)
+ uint8_t lrck_h; // adc lrck divider and dac lrck divider
+ uint8_t lrck_l; //
+ uint8_t bclk_div; // sclk divider
+ uint8_t adc_osr; // adc osr
+ uint8_t dac_osr; // dac osr
+};
+
+class ES8311 : public audio_dac::AudioDac, public Component, public i2c::I2CDevice {
+ public:
+ /////////////////////////
+ // Component overrides //
+ /////////////////////////
+
+ void setup() override;
+ float get_setup_priority() const override { return setup_priority::DATA; }
+ void dump_config() override;
+
+ ////////////////////////
+ // AudioDac overrides //
+ ////////////////////////
+
+ /// @brief Writes the volume out to the DAC
+ /// @param volume floating point between 0.0 and 1.0
+ /// @return True if successful and false otherwise
+ bool set_volume(float volume) override;
+
+ /// @brief Gets the current volume out from the DAC
+ /// @return floating point between 0.0 and 1.0
+ float volume() override;
+
+ /// @brief Disables mute for audio out
+ /// @return True if successful and false otherwise
+ bool set_mute_off() override { return this->set_mute_state_(false); }
+
+ /// @brief Enables mute for audio out
+ /// @return True if successful and false otherwise
+ bool set_mute_on() override { return this->set_mute_state_(true); }
+
+ bool is_muted() override { return this->is_muted_; }
+
+ //////////////////////////////////
+ // ES8311 configuration setters //
+ //////////////////////////////////
+
+ void set_use_mclk(bool use_mclk) { this->use_mclk_ = use_mclk; }
+ void set_bits_per_sample(ES8311Resolution resolution) {
+ this->resolution_in_ = resolution;
+ this->resolution_out_ = resolution;
+ }
+ void set_sample_frequency(uint32_t sample_frequency) { this->sample_frequency_ = sample_frequency; }
+ void set_use_mic(bool use_mic) { this->use_mic_ = use_mic; }
+ void set_mic_gain(ES8311MicGain mic_gain) { this->mic_gain_ = mic_gain; }
+
+ protected:
+ /// @brief Computes the register value for the configured resolution (bits per sample)
+ /// @param resolution bits per sample enum for both audio in and audio out
+ /// @return register value
+ static uint8_t calculate_resolution_value(ES8311Resolution resolution);
+
+ /// @brief Retrieves the appropriate registers values for the configured mclk and rate
+ /// @param mclk mlck frequency in Hz
+ /// @param rate sample rate frequency in Hz
+ /// @return ES8311Coeffecient containing appropriate register values to configure the ES8311 or nullptr if impossible
+ static const ES8311Coefficient *get_coefficient(uint32_t mclk, uint32_t rate);
+
+ /// @brief Configures the ES8311 registers for the chosen sample rate
+ /// @return True if successful and false otherwise
+ bool configure_clock_();
+
+ /// @brief Configures the ES8311 registers for the chosen bits per sample
+ /// @return True if successful and false otherwise
+ bool configure_format_();
+
+ /// @brief Configures the ES8311 microphone registers
+ /// @return True if successful and false otherwise
+ bool configure_mic_();
+
+ /// @brief Mutes or unmute the DAC audio out
+ /// @param mute_state True to mute, false to unmute
+ /// @return
+ bool set_mute_state_(bool mute_state);
+
+ bool use_mic_;
+ ES8311MicGain mic_gain_;
+
+ bool use_mclk_; // true = use dedicated MCLK pin, false = use SCLK
+ bool sclk_inverted_{false}; // SCLK is inverted
+ bool mclk_inverted_{false}; // MCLK is inverted (ignored if use_mclk_ == false)
+ uint32_t mclk_multiple_{256}; // MCLK frequency is sample rate * mclk_multiple_ (ignored if use_mclk_ == false)
+
+ uint32_t sample_frequency_; // in Hz
+ ES8311Resolution resolution_in_;
+ ES8311Resolution resolution_out_;
+};
+
+} // namespace es8311
+} // namespace esphome
diff --git a/esphome/components/es8311/es8311_const.h b/esphome/components/es8311/es8311_const.h
new file mode 100644
index 0000000000..7463a92ef1
--- /dev/null
+++ b/esphome/components/es8311/es8311_const.h
@@ -0,0 +1,195 @@
+#pragma once
+
+#include "es8311.h"
+
+namespace esphome {
+namespace es8311 {
+
+// ES8311 register addresses
+static const uint8_t ES8311_REG00_RESET = 0x00; // Reset
+static const uint8_t ES8311_REG01_CLK_MANAGER = 0x01; // Clock Manager: select clk src for mclk, enable clock for codec
+static const uint8_t ES8311_REG02_CLK_MANAGER = 0x02; // Clock Manager: clk divider and clk multiplier
+static const uint8_t ES8311_REG03_CLK_MANAGER = 0x03; // Clock Manager: adc fsmode and osr
+static const uint8_t ES8311_REG04_CLK_MANAGER = 0x04; // Clock Manager: dac osr
+static const uint8_t ES8311_REG05_CLK_MANAGER = 0x05; // Clock Manager: clk divider for adc and dac
+static const uint8_t ES8311_REG06_CLK_MANAGER = 0x06; // Clock Manager: bclk inverter BIT(5) and divider
+static const uint8_t ES8311_REG07_CLK_MANAGER = 0x07; // Clock Manager: tri-state, lrck divider
+static const uint8_t ES8311_REG08_CLK_MANAGER = 0x08; // Clock Manager: lrck divider
+static const uint8_t ES8311_REG09_SDPIN = 0x09; // Serial Digital Port: DAC
+static const uint8_t ES8311_REG0A_SDPOUT = 0x0A; // Serial Digital Port: ADC
+static const uint8_t ES8311_REG0B_SYSTEM = 0x0B; // System
+static const uint8_t ES8311_REG0C_SYSTEM = 0x0C; // System
+static const uint8_t ES8311_REG0D_SYSTEM = 0x0D; // System: power up/down
+static const uint8_t ES8311_REG0E_SYSTEM = 0x0E; // System: power up/down
+static const uint8_t ES8311_REG0F_SYSTEM = 0x0F; // System: low power
+static const uint8_t ES8311_REG10_SYSTEM = 0x10; // System
+static const uint8_t ES8311_REG11_SYSTEM = 0x11; // System
+static const uint8_t ES8311_REG12_SYSTEM = 0x12; // System: Enable DAC
+static const uint8_t ES8311_REG13_SYSTEM = 0x13; // System
+static const uint8_t ES8311_REG14_SYSTEM = 0x14; // System: select DMIC, select analog pga gain
+static const uint8_t ES8311_REG15_ADC = 0x15; // ADC: adc ramp rate, dmic sense
+static const uint8_t ES8311_REG16_ADC = 0x16; // ADC
+static const uint8_t ES8311_REG17_ADC = 0x17; // ADC: volume
+static const uint8_t ES8311_REG18_ADC = 0x18; // ADC: alc enable and winsize
+static const uint8_t ES8311_REG19_ADC = 0x19; // ADC: alc maxlevel
+static const uint8_t ES8311_REG1A_ADC = 0x1A; // ADC: alc automute
+static const uint8_t ES8311_REG1B_ADC = 0x1B; // ADC: alc automute, adc hpf s1
+static const uint8_t ES8311_REG1C_ADC = 0x1C; // ADC: equalizer, hpf s2
+static const uint8_t ES8311_REG1D_ADCEQ = 0x1D; // ADCEQ: equalizer B0
+static const uint8_t ES8311_REG1E_ADCEQ = 0x1E; // ADCEQ: equalizer B0
+static const uint8_t ES8311_REG1F_ADCEQ = 0x1F; // ADCEQ: equalizer B0
+static const uint8_t ES8311_REG20_ADCEQ = 0x20; // ADCEQ: equalizer B0
+static const uint8_t ES8311_REG21_ADCEQ = 0x21; // ADCEQ: equalizer A1
+static const uint8_t ES8311_REG22_ADCEQ = 0x22; // ADCEQ: equalizer A1
+static const uint8_t ES8311_REG23_ADCEQ = 0x23; // ADCEQ: equalizer A1
+static const uint8_t ES8311_REG24_ADCEQ = 0x24; // ADCEQ: equalizer A1
+static const uint8_t ES8311_REG25_ADCEQ = 0x25; // ADCEQ: equalizer A2
+static const uint8_t ES8311_REG26_ADCEQ = 0x26; // ADCEQ: equalizer A2
+static const uint8_t ES8311_REG27_ADCEQ = 0x27; // ADCEQ: equalizer A2
+static const uint8_t ES8311_REG28_ADCEQ = 0x28; // ADCEQ: equalizer A2
+static const uint8_t ES8311_REG29_ADCEQ = 0x29; // ADCEQ: equalizer B1
+static const uint8_t ES8311_REG2A_ADCEQ = 0x2A; // ADCEQ: equalizer B1
+static const uint8_t ES8311_REG2B_ADCEQ = 0x2B; // ADCEQ: equalizer B1
+static const uint8_t ES8311_REG2C_ADCEQ = 0x2C; // ADCEQ: equalizer B1
+static const uint8_t ES8311_REG2D_ADCEQ = 0x2D; // ADCEQ: equalizer B2
+static const uint8_t ES8311_REG2E_ADCEQ = 0x2E; // ADCEQ: equalizer B2
+static const uint8_t ES8311_REG2F_ADCEQ = 0x2F; // ADCEQ: equalizer B2
+static const uint8_t ES8311_REG30_ADCEQ = 0x30; // ADCEQ: equalizer B2
+static const uint8_t ES8311_REG31_DAC = 0x31; // DAC: mute
+static const uint8_t ES8311_REG32_DAC = 0x32; // DAC: volume
+static const uint8_t ES8311_REG33_DAC = 0x33; // DAC: offset
+static const uint8_t ES8311_REG34_DAC = 0x34; // DAC: drc enable, drc winsize
+static const uint8_t ES8311_REG35_DAC = 0x35; // DAC: drc maxlevel, minilevel
+static const uint8_t ES8311_REG36_DAC = 0x36; // DAC
+static const uint8_t ES8311_REG37_DAC = 0x37; // DAC: ramprate
+static const uint8_t ES8311_REG38_DACEQ = 0x38; // DACEQ: equalizer B0
+static const uint8_t ES8311_REG39_DACEQ = 0x39; // DACEQ: equalizer B0
+static const uint8_t ES8311_REG3A_DACEQ = 0x3A; // DACEQ: equalizer B0
+static const uint8_t ES8311_REG3B_DACEQ = 0x3B; // DACEQ: equalizer B0
+static const uint8_t ES8311_REG3C_DACEQ = 0x3C; // DACEQ: equalizer B1
+static const uint8_t ES8311_REG3D_DACEQ = 0x3D; // DACEQ: equalizer B1
+static const uint8_t ES8311_REG3E_DACEQ = 0x3E; // DACEQ: equalizer B1
+static const uint8_t ES8311_REG3F_DACEQ = 0x3F; // DACEQ: equalizer B1
+static const uint8_t ES8311_REG40_DACEQ = 0x40; // DACEQ: equalizer A1
+static const uint8_t ES8311_REG41_DACEQ = 0x41; // DACEQ: equalizer A1
+static const uint8_t ES8311_REG42_DACEQ = 0x42; // DACEQ: equalizer A1
+static const uint8_t ES8311_REG43_DACEQ = 0x43; // DACEQ: equalizer A1
+static const uint8_t ES8311_REG44_GPIO = 0x44; // GPIO: dac2adc for test
+static const uint8_t ES8311_REG45_GP = 0x45; // GPIO: GP control
+static const uint8_t ES8311_REGFA_I2C = 0xFA; // I2C: reset registers
+static const uint8_t ES8311_REGFC_FLAG = 0xFC; // Flag
+static const uint8_t ES8311_REGFD_CHD1 = 0xFD; // Chip: ID1
+static const uint8_t ES8311_REGFE_CHD2 = 0xFE; // Chip: ID2
+static const uint8_t ES8311_REGFF_CHVER = 0xFF; // Chip: Version
+
+// ES8311 clock divider coefficients
+static const ES8311Coefficient ES8311_COEFFICIENTS[] = {
+ // clang-format off
+
+ // mclk, rate, pre_ pre_ adc_ dac_ fs_ lrck lrck bclk_ adc_ dac_
+ // div, mult, div, div, mode, _h, _l, div, osr, osr
+
+ // 8k
+ {12288000, 8000, 0x06, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ {18432000, 8000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x05, 0xff, 0x18, 0x10, 0x20},
+ {16384000, 8000, 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 8192000, 8000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 6144000, 8000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 4096000, 8000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 3072000, 8000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 2048000, 8000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 1536000, 8000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 1024000, 8000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+
+ // 11.025k
+ {11289600, 11025, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 5644800, 11025, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 2822400, 11025, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 1411200, 11025, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+
+ // 12k
+ {12288000, 12000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 6144000, 12000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 3072000, 12000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 1536000, 12000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+
+ // 16k
+ {12288000, 16000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ {18432000, 16000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x20},
+ {16384000, 16000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 8192000, 16000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 6144000, 16000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 4096000, 16000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 3072000, 16000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 2048000, 16000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 1536000, 16000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+ { 1024000, 16000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
+
+ // 22.05k
+ {11289600, 22050, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 5644800, 22050, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 2822400, 22050, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1411200, 22050, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+
+ // 24k
+ {12288000, 24000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ {18432000, 24000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 6144000, 24000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 3072000, 24000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1536000, 24000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+
+ // 32k
+ {12288000, 32000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ {18432000, 32000, 0x03, 0x04, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x10},
+ {16384000, 32000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 8192000, 32000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 6144000, 32000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 4096000, 32000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 3072000, 32000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 2048000, 32000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1536000, 32000, 0x03, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
+ { 1024000, 32000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+
+ // 44.1k
+ {11289600, 44100, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 5644800, 44100, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 2822400, 44100, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1411200, 44100, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+
+ // 48k
+ {12288000, 48000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ {18432000, 48000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 6144000, 48000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 3072000, 48000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1536000, 48000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+
+ // 64k
+ {12288000, 64000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ {18432000, 64000, 0x03, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
+ {16384000, 64000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 8192000, 64000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 6144000, 64000, 0x01, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
+ { 4096000, 64000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 3072000, 64000, 0x01, 0x08, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
+ { 2048000, 64000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1536000, 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0xbf, 0x03, 0x18, 0x18},
+ { 1024000, 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
+
+ // 88.2k
+ {11289600, 88200, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 5644800, 88200, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 2822400, 88200, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1411200, 88200, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
+
+ // 96k
+ {12288000, 96000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ {18432000, 96000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 6144000, 96000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 3072000, 96000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
+ { 1536000, 96000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
+
+ // clang-format on
+};
+
+} // namespace es8311
+} // namespace esphome
diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py
index 8a73f2020d..61fbb53e3a 100644
--- a/esphome/components/esp32/__init__.py
+++ b/esphome/components/esp32/__init__.py
@@ -395,6 +395,13 @@ ARDUINO_FRAMEWORK_SCHEMA = cv.All(
cv.Optional(CONF_VERSION, default="recommended"): cv.string_strict,
cv.Optional(CONF_SOURCE): cv.string_strict,
cv.Optional(CONF_PLATFORM_VERSION): _parse_platform_version,
+ cv.Optional(CONF_ADVANCED, default={}): cv.Schema(
+ {
+ cv.Optional(
+ CONF_IGNORE_EFUSE_CUSTOM_MAC, default=False
+ ): cv.boolean,
+ }
+ ),
}
),
_arduino_check_versions,
@@ -494,6 +501,9 @@ async def to_code(config):
conf = config[CONF_FRAMEWORK]
cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
+ if CONF_ADVANCED in conf and conf[CONF_ADVANCED][CONF_IGNORE_EFUSE_CUSTOM_MAC]:
+ cg.add_define("USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC")
+
add_extra_script(
"post",
"post_build.py",
@@ -540,8 +550,6 @@ async def to_code(config):
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
add_idf_sdkconfig_option(name, RawSdkconfigValue(value))
- if conf[CONF_ADVANCED][CONF_IGNORE_EFUSE_CUSTOM_MAC]:
- cg.add_define("USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC")
if conf[CONF_ADVANCED].get(CONF_IGNORE_EFUSE_MAC_CRC):
add_idf_sdkconfig_option("CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR", True)
if (framework_ver.major, framework_ver.minor) >= (4, 4):
diff --git a/esphome/components/esp32/boards.py b/esphome/components/esp32/boards.py
index 60abcd447c..02744ecb6f 100644
--- a/esphome/components/esp32/boards.py
+++ b/esphome/components/esp32/boards.py
@@ -103,6 +103,173 @@ ESP32_BOARD_PINS = {
"LED": 13,
"LED_BUILTIN": 13,
},
+ "adafruit_feather_esp32s3": {
+ "BUTTON": 0,
+ "A0": 18,
+ "A1": 17,
+ "A2": 16,
+ "A3": 15,
+ "A4": 14,
+ "A5": 8,
+ "SCK": 36,
+ "MOSI": 35,
+ "MISO": 37,
+ "RX": 38,
+ "TX": 39,
+ "SCL": 4,
+ "SDA": 3,
+ "NEOPIXEL": 33,
+ "PIN_NEOPIXEL": 33,
+ "NEOPIXEL_POWER": 21,
+ "I2C_POWER": 7,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_feather_esp32s3_nopsram": {
+ "BUTTON": 0,
+ "A0": 18,
+ "A1": 17,
+ "A2": 16,
+ "A3": 15,
+ "A4": 14,
+ "A5": 8,
+ "SCK": 36,
+ "MOSI": 35,
+ "MISO": 37,
+ "RX": 38,
+ "TX": 39,
+ "SCL": 4,
+ "SDA": 3,
+ "NEOPIXEL": 33,
+ "PIN_NEOPIXEL": 33,
+ "NEOPIXEL_POWER": 21,
+ "I2C_POWER": 7,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_feather_esp32s3_tft": {
+ "BUTTON": 0,
+ "A0": 18,
+ "A1": 17,
+ "A2": 16,
+ "A3": 15,
+ "A4": 14,
+ "A5": 8,
+ "SCK": 36,
+ "MOSI": 35,
+ "MISO": 37,
+ "RX": 2,
+ "TX": 1,
+ "SCL": 41,
+ "SDA": 42,
+ "NEOPIXEL": 33,
+ "PIN_NEOPIXEL": 33,
+ "NEOPIXEL_POWER": 34,
+ "TFT_I2C_POWER": 21,
+ "TFT_CS": 7,
+ "TFT_DC": 39,
+ "TFT_RESET": 40,
+ "TFT_BACKLIGHT": 45,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_funhouse_esp32s2": {
+ "BUTTON_UP": 5,
+ "BUTTON_DOWN": 3,
+ "BUTTON_SELECT": 4,
+ "DOTSTAR_DATA": 14,
+ "DOTSTAR_CLOCK": 15,
+ "PIR_SENSE": 16,
+ "A0": 17,
+ "A1": 2,
+ "A2": 1,
+ "CAP6": 6,
+ "CAP7": 7,
+ "CAP8": 8,
+ "CAP9": 9,
+ "CAP10": 10,
+ "CAP11": 11,
+ "CAP12": 12,
+ "CAP13": 13,
+ "SPEAKER": 42,
+ "LED": 37,
+ "LIGHT": 18,
+ "TFT_MOSI": 35,
+ "TFT_SCK": 36,
+ "TFT_CS": 40,
+ "TFT_DC": 39,
+ "TFT_RESET": 41,
+ "TFT_BACKLIGHT": 21,
+ "RED_LED": 31,
+ "BUTTON": 0,
+ },
+ "adafruit_itsybitsy_esp32": {
+ "A0": 25,
+ "A1": 26,
+ "A2": 4,
+ "A3": 38,
+ "A4": 37,
+ "A5": 36,
+ "SCK": 19,
+ "MOSI": 21,
+ "MISO": 22,
+ "SCL": 27,
+ "SDA": 15,
+ "TX": 20,
+ "RX": 8,
+ "NEOPIXEL": 0,
+ "PIN_NEOPIXEL": 0,
+ "NEOPIXEL_POWER": 2,
+ "BUTTON": 35,
+ },
+ "adafruit_magtag29_esp32s2": {
+ "A1": 18,
+ "BUTTON_A": 15,
+ "BUTTON_B": 14,
+ "BUTTON_C": 12,
+ "BUTTON_D": 11,
+ "SDA": 33,
+ "SCL": 34,
+ "SPEAKER": 17,
+ "SPEAKER_ENABLE": 16,
+ "VOLTAGE_MONITOR": 4,
+ "ACCELEROMETER_INT": 9,
+ "ACCELEROMETER_INTERRUPT": 9,
+ "LIGHT": 3,
+ "NEOPIXEL": 1,
+ "PIN_NEOPIXEL": 1,
+ "NEOPIXEL_POWER": 21,
+ "EPD_BUSY": 5,
+ "EPD_RESET": 6,
+ "EPD_DC": 7,
+ "EPD_CS": 8,
+ "EPD_MOSI": 35,
+ "EPD_SCK": 36,
+ "EPD_MISO": 37,
+ "BUTTON": 0,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_metro_esp32s2": {
+ "A0": 17,
+ "A1": 18,
+ "A2": 1,
+ "A3": 2,
+ "A4": 3,
+ "A5": 4,
+ "RX": 38,
+ "TX": 37,
+ "SCL": 34,
+ "SDA": 33,
+ "MISO": 37,
+ "SCK": 36,
+ "MOSI": 35,
+ "NEOPIXEL": 45,
+ "PIN_NEOPIXEL": 45,
+ "LED": 42,
+ "LED_BUILTIN": 42,
+ "BUTTON": 0,
+ },
"adafruit_qtpy_esp32c3": {
"A0": 4,
"A1": 3,
@@ -141,6 +308,26 @@ ESP32_BOARD_PINS = {
"BUTTON": 0,
"SWITCH": 0,
},
+ "adafruit_qtpy_esp32s3_nopsram": {
+ "A0": 18,
+ "A1": 17,
+ "A2": 9,
+ "A3": 8,
+ "SDA": 7,
+ "SCL": 6,
+ "MOSI": 35,
+ "MISO": 37,
+ "SCK": 36,
+ "RX": 16,
+ "TX": 5,
+ "SDA1": 41,
+ "SCL1": 40,
+ "NEOPIXEL": 39,
+ "PIN_NEOPIXEL": 39,
+ "NEOPIXEL_POWER": 38,
+ "BUTTON": 0,
+ "SWITCH": 0,
+ },
"adafruit_qtpy_esp32": {
"A0": 26,
"A1": 25,
@@ -1068,7 +1255,18 @@ ESP32_BOARD_PINS = {
"_VBAT": 35,
},
"wemosbat": {"LED": 16},
- "wesp32": {"MISO": 32, "SCL": 4, "SDA": 15},
+ "wesp32": {
+ "MISO": 32,
+ "MOSI": 23,
+ "SCK": 18,
+ "SCL": 4,
+ "SDA": 15,
+ "MISO1": 12,
+ "MOSI1": 13,
+ "SCK1": 14,
+ "SCL1": 5,
+ "SDA1": 33,
+ },
"widora-air": {
"A1": 39,
"A2": 35,
diff --git a/esphome/components/esp32/gpio.py b/esphome/components/esp32/gpio.py
index 558ff51af8..df01769a66 100644
--- a/esphome/components/esp32/gpio.py
+++ b/esphome/components/esp32/gpio.py
@@ -67,8 +67,10 @@ def _translate_pin(value):
"This variable only supports pin numbers, not full pin schemas "
"(with inverted and mode)."
)
- if isinstance(value, int):
+ if isinstance(value, int) and not isinstance(value, bool):
return value
+ if not isinstance(value, str):
+ raise cv.Invalid(f"Invalid pin number: {value}")
try:
return int(value)
except ValueError:
diff --git a/esphome/components/esp32_ble_client/ble_client_base.h b/esphome/components/esp32_ble_client/ble_client_base.h
index fd586e59d6..fca66c0b3c 100644
--- a/esphome/components/esp32_ble_client/ble_client_base.h
+++ b/esphome/components/esp32_ble_client/ble_client_base.h
@@ -35,7 +35,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
void connect() override;
esp_err_t pair();
- void disconnect();
+ void disconnect() override;
void release_services();
bool connected() { return this->state_ == espbt::ClientState::ESTABLISHED; }
diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp
index 74b4b9aa89..b86d32ee61 100644
--- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp
+++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp
@@ -65,6 +65,9 @@ void ESP32BLETracker::setup() {
[this](ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) {
if (state == ota::OTA_STARTED) {
this->stop_scan();
+ for (auto *client : this->clients_) {
+ client->disconnect();
+ }
}
});
#endif
diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h
index d2bb6a6e6d..2fc5da829d 100644
--- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h
+++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h
@@ -11,9 +11,9 @@
#ifdef USE_ESP32
+#include
#include
#include
-#include
#include
#include
@@ -172,6 +172,7 @@ class ESPBTClient : public ESPBTDeviceListener {
esp_ble_gattc_cb_param_t *param) = 0;
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
virtual void connect() = 0;
+ virtual void disconnect() = 0;
virtual void set_state(ClientState st) { this->state_ = st; }
ClientState state() const { return state_; }
int app_id;
diff --git a/esphome/components/esp8266/gpio.py b/esphome/components/esp8266/gpio.py
index c42bc9204f..53016d2130 100644
--- a/esphome/components/esp8266/gpio.py
+++ b/esphome/components/esp8266/gpio.py
@@ -1,6 +1,9 @@
-import logging
from dataclasses import dataclass
+import logging
+from esphome import pins
+import esphome.codegen as cg
+import esphome.config_validation as cv
from esphome.const import (
CONF_ANALOG,
CONF_ID,
@@ -14,10 +17,7 @@ from esphome.const import (
CONF_PULLUP,
PLATFORM_ESP8266,
)
-from esphome import pins
from esphome.core import CORE, coroutine_with_priority
-import esphome.config_validation as cv
-import esphome.codegen as cg
from . import boards
from .const import KEY_BOARD, KEY_ESP8266, KEY_PIN_INITIAL_STATES, esp8266_ns
@@ -48,8 +48,10 @@ def _translate_pin(value):
"This variable only supports pin numbers, not full pin schemas "
"(with inverted and mode)."
)
- if isinstance(value, int):
+ if isinstance(value, int) and not isinstance(value, bool):
return value
+ if not isinstance(value, str):
+ raise cv.Invalid(f"Invalid pin number: {value}")
try:
return int(value)
except ValueError:
diff --git a/esphome/components/esphome/ota/__init__.py b/esphome/components/esphome/ota/__init__.py
index a852d8d001..86006e3e18 100644
--- a/esphome/components/esphome/ota/__init__.py
+++ b/esphome/components/esphome/ota/__init__.py
@@ -1,10 +1,9 @@
import logging
import esphome.codegen as cg
-import esphome.config_validation as cv
-import esphome.final_validate as fv
-from esphome.components.ota import BASE_OTA_SCHEMA, ota_to_code, OTAComponent
+from esphome.components.ota import BASE_OTA_SCHEMA, OTAComponent, ota_to_code
from esphome.config_helpers import merge_config
+import esphome.config_validation as cv
from esphome.const import (
CONF_ESPHOME,
CONF_ID,
@@ -18,6 +17,7 @@ from esphome.const import (
CONF_VERSION,
)
from esphome.core import coroutine_with_priority
+import esphome.final_validate as fv
_LOGGER = logging.getLogger(__name__)
@@ -124,7 +124,6 @@ FINAL_VALIDATE_SCHEMA = ota_esphome_final_validate
@coroutine_with_priority(52.0)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
- await ota_to_code(var, config)
cg.add(var.set_port(config[CONF_PORT]))
if CONF_PASSWORD in config:
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
@@ -132,3 +131,4 @@ async def to_code(config):
cg.add_define("USE_OTA_VERSION", config[CONF_VERSION])
await cg.register_component(var, config)
+ await ota_to_code(var, config)
diff --git a/esphome/components/ethernet/__init__.py b/esphome/components/ethernet/__init__.py
index 475d60df53..dca37b8dc2 100644
--- a/esphome/components/ethernet/__init__.py
+++ b/esphome/components/ethernet/__init__.py
@@ -1,3 +1,4 @@
+import logging
from esphome import pins
import esphome.codegen as cg
from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant
@@ -23,6 +24,7 @@ from esphome.const import (
CONF_MISO_PIN,
CONF_MOSI_PIN,
CONF_PAGE_ID,
+ CONF_POLLING_INTERVAL,
CONF_RESET_PIN,
CONF_SPI,
CONF_STATIC_IP,
@@ -30,13 +32,16 @@ from esphome.const import (
CONF_TYPE,
CONF_USE_ADDRESS,
CONF_VALUE,
+ KEY_CORE,
+ KEY_FRAMEWORK_VERSION,
)
-from esphome.core import CORE, coroutine_with_priority
+from esphome.core import CORE, TimePeriodMilliseconds, coroutine_with_priority
import esphome.final_validate as fv
CONFLICTS_WITH = ["wifi"]
DEPENDENCIES = ["esp32"]
AUTO_LOAD = ["network"]
+LOGGER = logging.getLogger(__name__)
ethernet_ns = cg.esphome_ns.namespace("ethernet")
PHYRegister = ethernet_ns.struct("PHYRegister")
@@ -63,6 +68,7 @@ ETHERNET_TYPES = {
}
SPI_ETHERNET_TYPES = ["W5500"]
+SPI_ETHERNET_DEFAULT_POLLING_INTERVAL = TimePeriodMilliseconds(milliseconds=10)
emac_rmii_clock_mode_t = cg.global_ns.enum("emac_rmii_clock_mode_t")
emac_rmii_clock_gpio_t = cg.global_ns.enum("emac_rmii_clock_gpio_t")
@@ -100,6 +106,24 @@ EthernetComponent = ethernet_ns.class_("EthernetComponent", cg.Component)
ManualIP = ethernet_ns.struct("ManualIP")
+def _is_framework_spi_polling_mode_supported():
+ # SPI Ethernet without IRQ feature is added in
+ # esp-idf >= (5.3+ ,5.2.1+, 5.1.4) and arduino-esp32 >= 3.0.0
+ framework_version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
+ if CORE.using_esp_idf:
+ if framework_version >= cv.Version(5, 3, 0):
+ return True
+ if cv.Version(5, 3, 0) > framework_version >= cv.Version(5, 2, 1):
+ return True
+ if cv.Version(5, 2, 0) > framework_version >= cv.Version(5, 1, 4):
+ return True
+ return False
+ if CORE.using_arduino:
+ return framework_version >= cv.Version(3, 0, 0)
+ # fail safe: Unknown framework
+ return False
+
+
def _validate(config):
if CONF_USE_ADDRESS not in config:
if CONF_MANUAL_IP in config:
@@ -107,6 +131,27 @@ def _validate(config):
else:
use_address = CORE.name + config[CONF_DOMAIN]
config[CONF_USE_ADDRESS] = use_address
+ if config[CONF_TYPE] in SPI_ETHERNET_TYPES:
+ if _is_framework_spi_polling_mode_supported():
+ if CONF_POLLING_INTERVAL in config and CONF_INTERRUPT_PIN in config:
+ raise cv.Invalid(
+ f"Cannot specify more than one of {CONF_INTERRUPT_PIN}, {CONF_POLLING_INTERVAL}"
+ )
+ if CONF_POLLING_INTERVAL not in config and CONF_INTERRUPT_PIN not in config:
+ config[CONF_POLLING_INTERVAL] = SPI_ETHERNET_DEFAULT_POLLING_INTERVAL
+ else:
+ if CONF_POLLING_INTERVAL in config:
+ raise cv.Invalid(
+ "In this version of the framework "
+ f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), "
+ f"'{CONF_POLLING_INTERVAL}' is not supported."
+ )
+ if CONF_INTERRUPT_PIN not in config:
+ raise cv.Invalid(
+ "In this version of the framework "
+ f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), "
+ f"'{CONF_INTERRUPT_PIN}' is a required option for [ethernet]."
+ )
return config
@@ -157,6 +202,11 @@ SPI_SCHEMA = BASE_SCHEMA.extend(
cv.Optional(CONF_CLOCK_SPEED, default="26.67MHz"): cv.All(
cv.frequency, cv.int_range(int(8e6), int(80e6))
),
+ # Set default value (SPI_ETHERNET_DEFAULT_POLLING_INTERVAL) at _validate()
+ cv.Optional(CONF_POLLING_INTERVAL): cv.All(
+ cv.positive_time_period_milliseconds,
+ cv.Range(min=TimePeriodMilliseconds(milliseconds=1)),
+ ),
}
),
)
@@ -234,6 +284,10 @@ async def to_code(config):
cg.add(var.set_cs_pin(config[CONF_CS_PIN]))
if CONF_INTERRUPT_PIN in config:
cg.add(var.set_interrupt_pin(config[CONF_INTERRUPT_PIN]))
+ else:
+ cg.add(var.set_polling_interval(config[CONF_POLLING_INTERVAL]))
+ if _is_framework_spi_polling_mode_supported():
+ cg.add_define("USE_ETHERNET_SPI_POLLING_SUPPORT")
if CONF_RESET_PIN in config:
cg.add(var.set_reset_pin(config[CONF_RESET_PIN]))
cg.add(var.set_clock_speed(config[CONF_CLOCK_SPEED]))
diff --git a/esphome/components/ethernet/ethernet_component.cpp b/esphome/components/ethernet/ethernet_component.cpp
index 00c7ae4ab8..08f5fa6642 100644
--- a/esphome/components/ethernet/ethernet_component.cpp
+++ b/esphome/components/ethernet/ethernet_component.cpp
@@ -116,6 +116,9 @@ void EthernetComponent::setup() {
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
#endif
w5500_config.int_gpio_num = this->interrupt_pin_;
+#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
+ w5500_config.poll_period_ms = this->polling_interval_;
+#endif
phy_config.phy_addr = this->phy_addr_spi_;
phy_config.reset_gpio_num = this->reset_pin_;
@@ -327,7 +330,14 @@ void EthernetComponent::dump_config() {
ESP_LOGCONFIG(TAG, " MISO Pin: %u", this->miso_pin_);
ESP_LOGCONFIG(TAG, " MOSI Pin: %u", this->mosi_pin_);
ESP_LOGCONFIG(TAG, " CS Pin: %u", this->cs_pin_);
- ESP_LOGCONFIG(TAG, " IRQ Pin: %u", this->interrupt_pin_);
+#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
+ if (this->polling_interval_ != 0) {
+ ESP_LOGCONFIG(TAG, " Polling Interval: %lu ms", this->polling_interval_);
+ } else
+#endif
+ {
+ ESP_LOGCONFIG(TAG, " IRQ Pin: %d", this->interrupt_pin_);
+ }
ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_);
ESP_LOGCONFIG(TAG, " Clock Speed: %d MHz", this->clock_speed_ / 1000000);
#else
@@ -536,6 +546,9 @@ void EthernetComponent::set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
void EthernetComponent::set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
void EthernetComponent::set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
void EthernetComponent::set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
+#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
+void EthernetComponent::set_polling_interval(uint32_t polling_interval) { this->polling_interval_ = polling_interval; }
+#endif
#else
void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
diff --git a/esphome/components/ethernet/ethernet_component.h b/esphome/components/ethernet/ethernet_component.h
index 5ee430c046..fb178431d5 100644
--- a/esphome/components/ethernet/ethernet_component.h
+++ b/esphome/components/ethernet/ethernet_component.h
@@ -67,6 +67,9 @@ class EthernetComponent : public Component {
void set_interrupt_pin(uint8_t interrupt_pin);
void set_reset_pin(uint8_t reset_pin);
void set_clock_speed(int clock_speed);
+#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
+ void set_polling_interval(uint32_t polling_interval);
+#endif
#else
void set_phy_addr(uint8_t phy_addr);
void set_power_pin(int power_pin);
@@ -108,10 +111,13 @@ class EthernetComponent : public Component {
uint8_t miso_pin_;
uint8_t mosi_pin_;
uint8_t cs_pin_;
- uint8_t interrupt_pin_;
+ int interrupt_pin_{-1};
int reset_pin_{-1};
int phy_addr_spi_{-1};
int clock_speed_;
+#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
+ uint32_t polling_interval_{0};
+#endif
#else
uint8_t phy_addr_{0};
int power_pin_{-1};
diff --git a/esphome/components/font/__init__.py b/esphome/components/font/__init__.py
index dacd0779b1..6fd2d7c310 100644
--- a/esphome/components/font/__init__.py
+++ b/esphome/components/font/__init__.py
@@ -1,3 +1,4 @@
+from collections.abc import Iterable
import functools
import hashlib
import logging
@@ -5,6 +6,8 @@ import os
from pathlib import Path
import re
+import freetype
+import glyphsets
from packaging import version
import requests
@@ -43,6 +46,18 @@ GlyphData = font_ns.struct("GlyphData")
CONF_BPP = "bpp"
CONF_EXTRAS = "extras"
CONF_FONTS = "fonts"
+CONF_GLYPHSETS = "glyphsets"
+CONF_IGNORE_MISSING_GLYPHS = "ignore_missing_glyphs"
+
+
+# Cache loaded freetype fonts
+class FontCache(dict):
+ def __missing__(self, key):
+ res = self[key] = freetype.Face(key)
+ return res
+
+
+FONT_CACHE = FontCache()
def glyph_comparator(x, y):
@@ -59,36 +74,106 @@ def glyph_comparator(x, y):
return -1
if len(x_) > len(y_):
return 1
- raise cv.Invalid(f"Found duplicate glyph {x}")
+ return 0
-def validate_glyphs(value):
- if isinstance(value, list):
- value = cv.Schema([cv.string])(value)
- value = cv.Schema([cv.string])(list(value))
+def flatten(lists) -> list:
+ """
+ Given a list of lists, flatten it to a single list of all elements of all lists.
+ This wraps itertools.chain.from_iterable to make it more readable, and return a list
+ rather than a single use iterable.
+ """
+ from itertools import chain
- value.sort(key=functools.cmp_to_key(glyph_comparator))
- return value
+ return list(chain.from_iterable(lists))
-font_map = {}
+def check_missing_glyphs(file, codepoints: Iterable, warning: bool = False):
+ """
+ Check that the given font file actually contains the requested glyphs
+ :param file: A Truetype font file
+ :param codepoints: A list of codepoints to check
+ :param warning: If true, log a warning instead of raising an exception
+ """
-
-def merge_glyphs(config):
- glyphs = []
- glyphs.extend(config[CONF_GLYPHS])
- font_list = [(EFont(config[CONF_FILE], config[CONF_SIZE], config[CONF_GLYPHS]))]
- if extras := config.get(CONF_EXTRAS):
- extra_fonts = list(
- map(
- lambda x: EFont(x[CONF_FILE], config[CONF_SIZE], x[CONF_GLYPHS]), extras
- )
+ font = FONT_CACHE[file]
+ missing = [chr(x) for x in codepoints if font.get_char_index(x) == 0]
+ if missing:
+ # Only list up to 10 missing glyphs
+ missing.sort(key=functools.cmp_to_key(glyph_comparator))
+ count = len(missing)
+ missing = missing[:10]
+ missing_str = "\n ".join(
+ f"{x} ({x.encode('unicode_escape')})" for x in missing
)
- font_list.extend(extra_fonts)
- for extra in extras:
- glyphs.extend(extra[CONF_GLYPHS])
- validate_glyphs(glyphs)
- font_map[config[CONF_ID]] = font_list
+ if count > 10:
+ missing_str += f"\n and {count - 10} more."
+ message = f"Font {Path(file).name} is missing {count} glyph{'s' if count != 1 else ''}:\n {missing_str}"
+ if warning:
+ _LOGGER.warning(message)
+ else:
+ raise cv.Invalid(message)
+
+
+def validate_glyphs(config):
+ """
+ Check for duplicate codepoints, then check that all requested codepoints actually
+ have glyphs defined in the appropriate font file.
+ """
+
+ # Collect all glyph codepoints and flatten to a list of chars
+ glyphspoints = flatten(
+ [x[CONF_GLYPHS] for x in config[CONF_EXTRAS]] + config[CONF_GLYPHS]
+ )
+ # Convert a list of strings to a list of chars (one char strings)
+ glyphspoints = flatten([list(x) for x in glyphspoints])
+ if len(set(glyphspoints)) != len(glyphspoints):
+ duplicates = {x for x in glyphspoints if glyphspoints.count(x) > 1}
+ dup_str = ", ".join(f"{x} ({x.encode('unicode_escape')})" for x in duplicates)
+ raise cv.Invalid(
+ f"Found duplicate glyph{'s' if len(duplicates) != 1 else ''}: {dup_str}"
+ )
+ # convert to codepoints
+ glyphspoints = {ord(x) for x in glyphspoints}
+ fileconf = config[CONF_FILE]
+ setpoints = set(
+ flatten([glyphsets.unicodes_per_glyphset(x) for x in config[CONF_GLYPHSETS]])
+ )
+ # Make setpoints and glyphspoints disjoint
+ setpoints.difference_update(glyphspoints)
+ if fileconf[CONF_TYPE] == TYPE_LOCAL_BITMAP:
+ # Pillow only allows 256 glyphs per bitmap font. Not sure if that is a Pillow limitation
+ # or a file format limitation
+ if any(x >= 256 for x in setpoints.copy().union(glyphspoints)):
+ raise cv.Invalid("Codepoints in bitmap fonts must be in the range 0-255")
+ else:
+ # for TT fonts, check that glyphs are actually present
+ # Check extras against their own font, exclude from parent font codepoints
+ for extra in config[CONF_EXTRAS]:
+ points = {ord(x) for x in flatten(extra[CONF_GLYPHS])}
+ glyphspoints.difference_update(points)
+ setpoints.difference_update(points)
+ check_missing_glyphs(extra[CONF_FILE][CONF_PATH], points)
+
+ # A named glyph that can't be provided is an error
+ check_missing_glyphs(fileconf[CONF_PATH], glyphspoints)
+ # A missing glyph from a set is a warning.
+ if not config[CONF_IGNORE_MISSING_GLYPHS]:
+ check_missing_glyphs(fileconf[CONF_PATH], setpoints, warning=True)
+
+ # Populate the default after the above checks so that use of the default doesn't trigger errors
+ if not config[CONF_GLYPHS] and not config[CONF_GLYPHSETS]:
+ if fileconf[CONF_TYPE] == TYPE_LOCAL_BITMAP:
+ config[CONF_GLYPHS] = [DEFAULT_GLYPHS]
+ else:
+ # set a default glyphset, intersected with what the font actually offers
+ font = FONT_CACHE[fileconf[CONF_PATH]]
+ config[CONF_GLYPHS] = [
+ chr(x)
+ for x in glyphsets.unicodes_per_glyphset(DEFAULT_GLYPHSET)
+ if font.get_char_index(x) != 0
+ ]
+
return config
@@ -120,7 +205,7 @@ def validate_truetype_file(value):
)
if not any(map(value.lower().endswith, FONT_EXTENSIONS)):
raise cv.Invalid(f"Only {FONT_EXTENSIONS} files are supported.")
- return cv.file_(value)
+ return CORE.relative_config_path(cv.file_(value))
TYPE_LOCAL = "local"
@@ -139,6 +224,10 @@ LOCAL_BITMAP_SCHEMA = cv.Schema(
}
)
+FULLPATH_SCHEMA = cv.maybe_simple_value(
+ {cv.Required(CONF_PATH): cv.string}, key=CONF_PATH
+)
+
CONF_ITALIC = "italic"
FONT_WEIGHTS = {
"thin": 100,
@@ -167,13 +256,13 @@ def _compute_local_font_path(value: dict) -> Path:
return base_dir / key
-def get_font_path(value, type) -> Path:
- if type == TYPE_GFONTS:
+def get_font_path(value, font_type) -> Path:
+ if font_type == TYPE_GFONTS:
name = f"{value[CONF_FAMILY]}@{value[CONF_WEIGHT]}@{value[CONF_ITALIC]}@v1"
return external_files.compute_local_file_dir(DOMAIN) / f"{name}.ttf"
- if type == TYPE_WEB:
+ if font_type == TYPE_WEB:
return _compute_local_font_path(value) / "font.ttf"
- return None
+ assert False
def download_gfont(value):
@@ -203,7 +292,7 @@ def download_gfont(value):
_LOGGER.debug("download_gfont: ttf_url=%s", ttf_url)
external_files.download_content(ttf_url, path)
- return value
+ return FULLPATH_SCHEMA(path)
def download_web_font(value):
@@ -212,7 +301,7 @@ def download_web_font(value):
external_files.download_content(url, path)
_LOGGER.debug("download_web_font: path=%s", path)
- return value
+ return FULLPATH_SCHEMA(path)
EXTERNAL_FONT_SCHEMA = cv.Schema(
@@ -225,7 +314,6 @@ EXTERNAL_FONT_SCHEMA = cv.Schema(
}
)
-
GFONTS_SCHEMA = cv.All(
EXTERNAL_FONT_SCHEMA.extend(
{
@@ -259,10 +347,10 @@ def validate_file_shorthand(value):
}
if weight is not None:
data[CONF_WEIGHT] = weight[1:]
- return FILE_SCHEMA(data)
+ return font_file_schema(data)
if value.startswith("http://") or value.startswith("https://"):
- return FILE_SCHEMA(
+ return font_file_schema(
{
CONF_TYPE: TYPE_WEB,
CONF_URL: value,
@@ -270,14 +358,15 @@ def validate_file_shorthand(value):
)
if value.endswith(".pcf") or value.endswith(".bdf"):
- return FILE_SCHEMA(
- {
- CONF_TYPE: TYPE_LOCAL_BITMAP,
- CONF_PATH: value,
- }
+ value = convert_bitmap_to_pillow_font(
+ CORE.relative_config_path(cv.file_(value))
)
+ return {
+ CONF_TYPE: TYPE_LOCAL_BITMAP,
+ CONF_PATH: value,
+ }
- return FILE_SCHEMA(
+ return font_file_schema(
{
CONF_TYPE: TYPE_LOCAL,
CONF_PATH: value,
@@ -295,31 +384,35 @@ TYPED_FILE_SCHEMA = cv.typed_schema(
)
-def _file_schema(value):
+def font_file_schema(value):
if isinstance(value, str):
return validate_file_shorthand(value)
return TYPED_FILE_SCHEMA(value)
-FILE_SCHEMA = cv.All(_file_schema)
+# Default if no glyphs or glyphsets are provided
+DEFAULT_GLYPHSET = "GF_Latin_Kernel"
+# default for bitmap fonts
+DEFAULT_GLYPHS = ' !"%()+=,-.:/?0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
-DEFAULT_GLYPHS = (
- ' !"%()+=,-.:/?0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz°'
-)
CONF_RAW_GLYPH_ID = "raw_glyph_id"
FONT_SCHEMA = cv.Schema(
{
cv.Required(CONF_ID): cv.declare_id(Font),
- cv.Required(CONF_FILE): FILE_SCHEMA,
- cv.Optional(CONF_GLYPHS, default=DEFAULT_GLYPHS): validate_glyphs,
+ cv.Required(CONF_FILE): font_file_schema,
+ cv.Optional(CONF_GLYPHS, default=[]): cv.ensure_list(cv.string_strict),
+ cv.Optional(CONF_GLYPHSETS, default=[]): cv.ensure_list(
+ cv.one_of(*glyphsets.defined_glyphsets())
+ ),
+ cv.Optional(CONF_IGNORE_MISSING_GLYPHS, default=False): cv.boolean,
cv.Optional(CONF_SIZE, default=20): cv.int_range(min=1),
cv.Optional(CONF_BPP, default=1): cv.one_of(1, 2, 4, 8),
- cv.Optional(CONF_EXTRAS): cv.ensure_list(
+ cv.Optional(CONF_EXTRAS, default=[]): cv.ensure_list(
cv.Schema(
{
- cv.Required(CONF_FILE): FILE_SCHEMA,
- cv.Required(CONF_GLYPHS): validate_glyphs,
+ cv.Required(CONF_FILE): font_file_schema,
+ cv.Required(CONF_GLYPHS): cv.ensure_list(cv.string_strict),
}
)
),
@@ -328,7 +421,7 @@ FONT_SCHEMA = cv.Schema(
},
)
-CONFIG_SCHEMA = cv.All(validate_pillow_installed, FONT_SCHEMA, merge_glyphs)
+CONFIG_SCHEMA = cv.All(validate_pillow_installed, FONT_SCHEMA, validate_glyphs)
# PIL doesn't provide a consistent interface for both TrueType and bitmap
@@ -344,7 +437,7 @@ class TrueTypeFontWrapper:
return offset_x, offset_y
def getmask(self, glyph, **kwargs):
- return self.font.getmask(glyph, **kwargs)
+ return self.font.getmask(str(glyph), **kwargs)
def getmetrics(self, glyphs):
return self.font.getmetrics()
@@ -359,7 +452,7 @@ class BitmapFontWrapper:
return 0, 0
def getmask(self, glyph, **kwargs):
- return self.font.getmask(glyph, **kwargs)
+ return self.font.getmask(str(glyph), **kwargs)
def getmetrics(self, glyphs):
max_height = 0
@@ -367,28 +460,20 @@ class BitmapFontWrapper:
mask = self.getmask(glyph, mode="1")
_, height = mask.size
max_height = max(max_height, height)
- return (max_height, 0)
+ return max_height, 0
class EFont:
- def __init__(self, file, size, glyphs):
- self.glyphs = glyphs
+ def __init__(self, file, size, codepoints):
+ self.codepoints = codepoints
+ path = file[CONF_PATH]
+ self.name = Path(path).name
ftype = file[CONF_TYPE]
if ftype == TYPE_LOCAL_BITMAP:
- font = load_bitmap_font(CORE.relative_config_path(file[CONF_PATH]))
- elif ftype == TYPE_LOCAL:
- path = CORE.relative_config_path(file[CONF_PATH])
- font = load_ttf_font(path, size)
- elif ftype in (TYPE_GFONTS, TYPE_WEB):
- path = get_font_path(file, ftype)
- font = load_ttf_font(path, size)
+ self.font = load_bitmap_font(path)
else:
- raise cv.Invalid(f"Could not load font: unknown type: {ftype}")
- self.font = font
- self.ascent, self.descent = font.getmetrics(glyphs)
-
- def has_glyph(self, glyph):
- return glyph in self.glyphs
+ self.font = load_ttf_font(path, size)
+ self.ascent, self.descent = self.font.getmetrics(codepoints)
def convert_bitmap_to_pillow_font(filepath):
@@ -400,6 +485,7 @@ def convert_bitmap_to_pillow_font(filepath):
copy_file_if_changed(filepath, local_bitmap_font_file)
+ local_pil_font_file = local_bitmap_font_file.with_suffix(".pil")
with open(local_bitmap_font_file, "rb") as fp:
try:
try:
@@ -409,28 +495,22 @@ def convert_bitmap_to_pillow_font(filepath):
p = BdfFontFile.BdfFontFile(fp)
# Convert to pillow-formatted fonts, which have a .pil and .pbm extension.
- p.save(local_bitmap_font_file)
+ p.save(local_pil_font_file)
except (SyntaxError, OSError) as err:
raise core.EsphomeError(
f"Failed to parse as bitmap font: '{filepath}': {err}"
)
- local_pil_font_file = os.path.splitext(local_bitmap_font_file)[0] + ".pil"
- return cv.file_(local_pil_font_file)
+ return str(local_pil_font_file)
def load_bitmap_font(filepath):
from PIL import ImageFont
- # Convert bpf and pcf files to pillow fonts, first.
- pil_font_path = convert_bitmap_to_pillow_font(filepath)
-
try:
- font = ImageFont.load(str(pil_font_path))
+ font = ImageFont.load(str(filepath))
except Exception as e:
- raise core.EsphomeError(
- f"Failed to load bitmap font file: {pil_font_path} : {e}"
- )
+ raise core.EsphomeError(f"Failed to load bitmap font file: {filepath}: {e}")
return BitmapFontWrapper(font)
@@ -441,7 +521,7 @@ def load_ttf_font(path, size):
try:
font = ImageFont.truetype(str(path), size)
except Exception as e:
- raise core.EsphomeError(f"Could not load truetype file {path}: {e}")
+ raise core.EsphomeError(f"Could not load TrueType file {path}: {e}")
return TrueTypeFontWrapper(font)
@@ -456,14 +536,35 @@ class GlyphInfo:
async def to_code(config):
- glyph_to_font_map = {}
- font_list = font_map[config[CONF_ID]]
- glyphs = []
- for font in font_list:
- glyphs.extend(font.glyphs)
- for glyph in font.glyphs:
- glyph_to_font_map[glyph] = font
- glyphs.sort(key=functools.cmp_to_key(glyph_comparator))
+ """
+ Collect all glyph codepoints, construct a map from a codepoint to a font file.
+ Codepoints are either explicit (glyphs key in top level or extras) or part of a glyphset.
+ Codepoints listed in extras use the extra font and override codepoints from glyphsets.
+ Achieve this by processing the base codepoints first, then the extras
+ """
+
+ # get the codepoints from glyphsets and flatten to a set of chrs.
+ point_set: set[str] = {
+ chr(x)
+ for x in flatten(
+ [glyphsets.unicodes_per_glyphset(x) for x in config[CONF_GLYPHSETS]]
+ )
+ }
+ # get the codepoints from the glyphs key, flatten to a list of chrs and combine with the points from glyphsets
+ point_set.update(flatten(config[CONF_GLYPHS]))
+ size = config[CONF_SIZE]
+ # Create the codepoint to font file map
+ base_font = EFont(config[CONF_FILE], size, point_set)
+ point_font_map: dict[str, EFont] = {c: base_font for c in point_set}
+ # process extras, updating the map and extending the codepoint list
+ for extra in config[CONF_EXTRAS]:
+ extra_points = flatten(extra[CONF_GLYPHS])
+ point_set.update(extra_points)
+ extra_font = EFont(extra[CONF_FILE], size, extra_points)
+ point_font_map.update({c: extra_font for c in extra_points})
+
+ codepoints = list(point_set)
+ codepoints.sort(key=functools.cmp_to_key(glyph_comparator))
glyph_args = {}
data = []
bpp = config[CONF_BPP]
@@ -473,10 +574,11 @@ async def to_code(config):
else:
mode = "L"
scale = 256 // (1 << bpp)
- for glyph in glyphs:
- font = glyph_to_font_map[glyph].font
- mask = font.getmask(glyph, mode=mode)
- offset_x, offset_y = font.getoffset(glyph)
+ # create the data array for all glyphs
+ for codepoint in codepoints:
+ font = point_font_map[codepoint]
+ mask = font.font.getmask(codepoint, mode=mode)
+ offset_x, offset_y = font.font.getoffset(codepoint)
width, height = mask.size
glyph_data = [0] * ((height * width * bpp + 7) // 8)
pos = 0
@@ -487,31 +589,34 @@ async def to_code(config):
if pixel & (1 << (bpp - bit_num - 1)):
glyph_data[pos // 8] |= 0x80 >> (pos % 8)
pos += 1
- glyph_args[glyph] = GlyphInfo(len(data), offset_x, offset_y, width, height)
+ glyph_args[codepoint] = GlyphInfo(len(data), offset_x, offset_y, width, height)
data += glyph_data
rhs = [HexInt(x) for x in data]
prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs)
+ # Create the glyph table that points to data in the above array.
glyph_initializer = []
- for glyph in glyphs:
+ for codepoint in codepoints:
glyph_initializer.append(
cg.StructInitializer(
GlyphData,
(
"a_char",
- cg.RawExpression(f"(const uint8_t *){cpp_string_escape(glyph)}"),
+ cg.RawExpression(
+ f"(const uint8_t *){cpp_string_escape(codepoint)}"
+ ),
),
(
"data",
cg.RawExpression(
- f"{str(prog_arr)} + {str(glyph_args[glyph].data_len)}"
+ f"{str(prog_arr)} + {str(glyph_args[codepoint].data_len)}"
),
),
- ("offset_x", glyph_args[glyph].offset_x),
- ("offset_y", glyph_args[glyph].offset_y),
- ("width", glyph_args[glyph].width),
- ("height", glyph_args[glyph].height),
+ ("offset_x", glyph_args[codepoint].offset_x),
+ ("offset_y", glyph_args[codepoint].offset_y),
+ ("width", glyph_args[codepoint].width),
+ ("height", glyph_args[codepoint].height),
)
)
@@ -521,7 +626,7 @@ async def to_code(config):
config[CONF_ID],
glyphs,
len(glyph_initializer),
- font_list[0].ascent,
- font_list[0].ascent + font_list[0].descent,
+ base_font.ascent,
+ base_font.ascent + base_font.descent,
bpp,
)
diff --git a/esphome/components/gp8403/output/__init__.py b/esphome/components/gp8403/output/__init__.py
index 1cf95ac6e5..7f17faa1b1 100644
--- a/esphome/components/gp8403/output/__init__.py
+++ b/esphome/components/gp8403/output/__init__.py
@@ -16,7 +16,7 @@ CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(GP8403Output),
cv.GenerateID(CONF_GP8403_ID): cv.use_id(GP8403),
- cv.Required(CONF_CHANNEL): cv.one_of(0, 1),
+ cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=1),
}
).extend(cv.COMPONENT_SCHEMA)
diff --git a/esphome/components/host/__init__.py b/esphome/components/host/__init__.py
index e83bf2dba8..eb8cfbd984 100644
--- a/esphome/components/host/__init__.py
+++ b/esphome/components/host/__init__.py
@@ -16,7 +16,7 @@ from .const import KEY_HOST
from .gpio import host_pin_to_code # noqa
CODEOWNERS = ["@esphome/core", "@clydebarrow"]
-AUTO_LOAD = ["network"]
+AUTO_LOAD = ["network", "preferences"]
def set_core_data(config):
diff --git a/esphome/components/host/gpio.py b/esphome/components/host/gpio.py
index 180919de4f..0f22a790bd 100644
--- a/esphome/components/host/gpio.py
+++ b/esphome/components/host/gpio.py
@@ -1,5 +1,8 @@
import logging
+from esphome import pins
+import esphome.codegen as cg
+import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
CONF_INPUT,
@@ -11,9 +14,6 @@ from esphome.const import (
CONF_PULLDOWN,
CONF_PULLUP,
)
-from esphome import pins
-import esphome.config_validation as cv
-import esphome.codegen as cg
from .const import host_ns
@@ -28,8 +28,10 @@ def _translate_pin(value):
"This variable only supports pin numbers, not full pin schemas "
"(with inverted and mode)."
)
- if isinstance(value, int):
+ if isinstance(value, int) and not isinstance(value, bool):
return value
+ if not isinstance(value, str):
+ raise cv.Invalid(f"Invalid pin number: {value}")
try:
return int(value)
except ValueError:
diff --git a/esphome/components/http_request/__init__.py b/esphome/components/http_request/__init__.py
index 0407bbd326..78064fb4b4 100644
--- a/esphome/components/http_request/__init__.py
+++ b/esphome/components/http_request/__init__.py
@@ -6,6 +6,7 @@ from esphome.const import (
CONF_ESP8266_DISABLE_SSL_SUPPORT,
CONF_ID,
CONF_METHOD,
+ CONF_ON_ERROR,
CONF_TIMEOUT,
CONF_TRIGGER_ID,
CONF_URL,
@@ -185,6 +186,13 @@ HTTP_REQUEST_ACTION_SCHEMA = cv.Schema(
cv.Optional(CONF_ON_RESPONSE): automation.validate_automation(
{cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(HttpRequestResponseTrigger)}
),
+ cv.Optional(CONF_ON_ERROR): automation.validate_automation(
+ {
+ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
+ automation.Trigger.template()
+ )
+ }
+ ),
cv.Optional(CONF_MAX_RESPONSE_BUFFER_SIZE, default="1kB"): cv.validate_bytes,
}
)
@@ -272,5 +280,9 @@ async def http_request_action_to_code(config, action_id, template_arg, args):
],
conf,
)
+ for conf in config.get(CONF_ON_ERROR, []):
+ trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
+ cg.add(var.register_error_trigger(trigger))
+ await automation.build_automation(trigger, [], conf)
return var
diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h
index c01baf8644..b2ce718ec4 100644
--- a/esphome/components/http_request/http_request.h
+++ b/esphome/components/http_request/http_request.h
@@ -22,6 +22,63 @@ struct Header {
const char *value;
};
+// Some common HTTP status codes
+enum HttpStatus {
+ HTTP_STATUS_OK = 200,
+ HTTP_STATUS_NO_CONTENT = 204,
+ HTTP_STATUS_PARTIAL_CONTENT = 206,
+
+ /* 3xx - Redirection */
+ HTTP_STATUS_MULTIPLE_CHOICES = 300,
+ HTTP_STATUS_MOVED_PERMANENTLY = 301,
+ HTTP_STATUS_FOUND = 302,
+ HTTP_STATUS_SEE_OTHER = 303,
+ HTTP_STATUS_NOT_MODIFIED = 304,
+ HTTP_STATUS_TEMPORARY_REDIRECT = 307,
+ HTTP_STATUS_PERMANENT_REDIRECT = 308,
+
+ /* 4XX - CLIENT ERROR */
+ HTTP_STATUS_BAD_REQUEST = 400,
+ HTTP_STATUS_UNAUTHORIZED = 401,
+ HTTP_STATUS_FORBIDDEN = 403,
+ HTTP_STATUS_NOT_FOUND = 404,
+ HTTP_STATUS_METHOD_NOT_ALLOWED = 405,
+ HTTP_STATUS_NOT_ACCEPTABLE = 406,
+ HTTP_STATUS_LENGTH_REQUIRED = 411,
+
+ /* 5xx - Server Error */
+ HTTP_STATUS_INTERNAL_ERROR = 500
+};
+
+/**
+ * @brief Returns true if the HTTP status code is a redirect.
+ *
+ * @param status the HTTP status code to check
+ * @return true if the status code is a redirect, false otherwise
+ */
+inline bool is_redirect(int const status) {
+ switch (status) {
+ case HTTP_STATUS_MOVED_PERMANENTLY:
+ case HTTP_STATUS_FOUND:
+ case HTTP_STATUS_SEE_OTHER:
+ case HTTP_STATUS_TEMPORARY_REDIRECT:
+ case HTTP_STATUS_PERMANENT_REDIRECT:
+ return true;
+ default:
+ return false;
+ }
+}
+
+/**
+ * @brief Checks if the given HTTP status code indicates a successful request.
+ *
+ * A successful request is one where the status code is in the range 200-299
+ *
+ * @param status the HTTP status code to check
+ * @return true if the status code indicates a successful request, false otherwise
+ */
+inline bool is_success(int const status) { return status >= HTTP_STATUS_OK && status < HTTP_STATUS_MULTIPLE_CHOICES; }
+
class HttpRequestComponent;
class HttpContainer : public Parented {
@@ -78,8 +135,8 @@ class HttpRequestComponent : public Component {
protected:
const char *useragent_{nullptr};
- bool follow_redirects_;
- uint16_t redirect_limit_;
+ bool follow_redirects_{};
+ uint16_t redirect_limit_{};
uint16_t timeout_{4500};
uint32_t watchdog_timeout_{0};
};
@@ -100,6 +157,8 @@ template class HttpRequestSendAction : public Action {
void register_response_trigger(HttpRequestResponseTrigger *trigger) { this->response_triggers_.push_back(trigger); }
+ void register_error_trigger(Trigger<> *trigger) { this->error_triggers_.push_back(trigger); }
+
void set_max_response_buffer_size(size_t max_response_buffer_size) {
this->max_response_buffer_size_ = max_response_buffer_size;
}
@@ -129,6 +188,8 @@ template class HttpRequestSendAction : public Action {
auto container = this->parent_->start(this->url_.value(x...), this->method_.value(x...), body, headers);
if (container == nullptr) {
+ for (auto *trigger : this->error_triggers_)
+ trigger->trigger();
return;
}
@@ -180,7 +241,8 @@ template class HttpRequestSendAction : public Action {
std::map> headers_{};
std::map> json_{};
std::function json_func_{nullptr};
- std::vector response_triggers_;
+ std::vector response_triggers_{};
+ std::vector *> error_triggers_{};
size_t max_response_buffer_size_{SIZE_MAX};
};
diff --git a/esphome/components/http_request/http_request_arduino.cpp b/esphome/components/http_request/http_request_arduino.cpp
index 2148d92ad2..85a1312aaa 100644
--- a/esphome/components/http_request/http_request_arduino.cpp
+++ b/esphome/components/http_request/http_request_arduino.cpp
@@ -104,7 +104,9 @@ std::shared_ptr HttpRequestArduino::start(std::string url, std::s
static const size_t HEADER_COUNT = sizeof(header_keys) / sizeof(header_keys[0]);
container->client_.collectHeaders(header_keys, HEADER_COUNT);
+ App.feed_wdt();
container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
+ App.feed_wdt();
if (container->status_code < 0) {
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", url.c_str(),
HTTPClient::errorToString(container->status_code).c_str());
@@ -113,11 +115,10 @@ std::shared_ptr HttpRequestArduino::start(std::string url, std::s
return nullptr;
}
- if (container->status_code < 200 || container->status_code >= 300) {
+ if (!is_success(container->status_code)) {
ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
this->status_momentary_error("failed", 1000);
- container->end();
- return nullptr;
+ // Still return the container, so it can be used to get the status code and error message
}
int content_length = container->client_.getSize();
diff --git a/esphome/components/http_request/http_request_idf.cpp b/esphome/components/http_request/http_request_idf.cpp
index 3819f5544e..b449f046ee 100644
--- a/esphome/components/http_request/http_request_idf.cpp
+++ b/esphome/components/http_request/http_request_idf.cpp
@@ -6,7 +6,6 @@
#include "esphome/components/watchdog/watchdog.h"
#include "esphome/core/application.h"
-#include "esphome/core/defines.h"
#include "esphome/core/log.h"
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
@@ -118,20 +117,17 @@ std::shared_ptr HttpRequestIDF::start(std::string url, std::strin
return nullptr;
}
- auto is_ok = [](int code) { return code >= HttpStatus_Ok && code < HttpStatus_MultipleChoices; };
-
+ App.feed_wdt();
container->content_length = esp_http_client_fetch_headers(client);
+ App.feed_wdt();
container->status_code = esp_http_client_get_status_code(client);
- if (is_ok(container->status_code)) {
+ App.feed_wdt();
+ if (is_success(container->status_code)) {
container->duration_ms = millis() - start;
return container;
}
if (this->follow_redirects_) {
- auto is_redirect = [](int code) {
- return code == HttpStatus_MovedPermanently || code == HttpStatus_Found || code == HttpStatus_SeeOther ||
- code == HttpStatus_TemporaryRedirect || code == HttpStatus_PermanentRedirect;
- };
auto num_redirects = this->redirect_limit_;
while (is_redirect(container->status_code) && num_redirects > 0) {
err = esp_http_client_set_redirection(client);
@@ -142,9 +138,9 @@ std::shared_ptr HttpRequestIDF::start(std::string url, std::strin
return nullptr;
}
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
- char url[256]{};
- if (esp_http_client_get_url(client, url, sizeof(url) - 1) == ESP_OK) {
- ESP_LOGV(TAG, "redirecting to url: %s", url);
+ char redirect_url[256]{};
+ if (esp_http_client_get_url(client, redirect_url, sizeof(redirect_url) - 1) == ESP_OK) {
+ ESP_LOGV(TAG, "redirecting to url: %s", redirect_url);
}
#endif
err = esp_http_client_open(client, 0);
@@ -155,9 +151,12 @@ std::shared_ptr HttpRequestIDF::start(std::string url, std::strin
return nullptr;
}
+ App.feed_wdt();
container->content_length = esp_http_client_fetch_headers(client);
+ App.feed_wdt();
container->status_code = esp_http_client_get_status_code(client);
- if (is_ok(container->status_code)) {
+ App.feed_wdt();
+ if (is_success(container->status_code)) {
container->duration_ms = millis() - start;
return container;
}
@@ -172,8 +171,7 @@ std::shared_ptr HttpRequestIDF::start(std::string url, std::strin
ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
this->status_momentary_error("failed", 1000);
- esp_http_client_cleanup(client);
- return nullptr;
+ return container;
}
int HttpContainerIDF::read(uint8_t *buf, size_t max_len) {
diff --git a/esphome/components/http_request/ota/ota_http_request.cpp b/esphome/components/http_request/ota/ota_http_request.cpp
index 1553de0bc1..cec30d72ec 100644
--- a/esphome/components/http_request/ota/ota_http_request.cpp
+++ b/esphome/components/http_request/ota/ota_http_request.cpp
@@ -106,7 +106,7 @@ uint8_t OtaHttpRequestComponent::do_ota_() {
auto container = this->parent_->get(url_with_auth);
- if (container == nullptr) {
+ if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
return OTA_CONNECTION_ERROR;
}
diff --git a/esphome/components/http_request/update/http_request_update.cpp b/esphome/components/http_request/update/http_request_update.cpp
index 059148e7e5..0e0966c22b 100644
--- a/esphome/components/http_request/update/http_request_update.cpp
+++ b/esphome/components/http_request/update/http_request_update.cpp
@@ -31,7 +31,7 @@ void HttpRequestUpdate::setup() {
void HttpRequestUpdate::update() {
auto container = this->request_parent_->get(this->source_url_);
- if (container == nullptr) {
+ if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
std::string msg = str_sprintf("Failed to fetch manifest from %s", this->source_url_.c_str());
this->status_set_error(msg.c_str());
return;
diff --git a/esphome/components/i2c_device/__init__.py b/esphome/components/i2c_device/__init__.py
new file mode 100644
index 0000000000..e145ba56f8
--- /dev/null
+++ b/esphome/components/i2c_device/__init__.py
@@ -0,0 +1,26 @@
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from esphome.components import i2c
+from esphome.const import CONF_ID
+
+DEPENDENCIES = ["i2c"]
+CODEOWNERS = ["@gabest11"]
+MULTI_CONF = True
+
+i2c_device_ns = cg.esphome_ns.namespace("i2c_device")
+
+I2CDeviceComponent = i2c_device_ns.class_(
+ "I2CDeviceComponent", cg.Component, i2c.I2CDevice
+)
+
+CONFIG_SCHEMA = cv.Schema(
+ {
+ cv.GenerateID(CONF_ID): cv.declare_id(I2CDeviceComponent),
+ }
+).extend(i2c.i2c_device_schema(None))
+
+
+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)
diff --git a/esphome/components/i2c_device/i2c_device.cpp b/esphome/components/i2c_device/i2c_device.cpp
new file mode 100644
index 0000000000..455c68fbed
--- /dev/null
+++ b/esphome/components/i2c_device/i2c_device.cpp
@@ -0,0 +1,17 @@
+#include "i2c_device.h"
+#include "esphome/core/log.h"
+#include "esphome/core/hal.h"
+#include
+
+namespace esphome {
+namespace i2c_device {
+
+static const char *const TAG = "i2c_device";
+
+void I2CDeviceComponent::dump_config() {
+ ESP_LOGCONFIG(TAG, "I2CDevice");
+ LOG_I2C_DEVICE(this);
+}
+
+} // namespace i2c_device
+} // namespace esphome
diff --git a/esphome/components/i2c_device/i2c_device.h b/esphome/components/i2c_device/i2c_device.h
new file mode 100644
index 0000000000..ab118e3e89
--- /dev/null
+++ b/esphome/components/i2c_device/i2c_device.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "esphome/core/component.h"
+#include "esphome/components/i2c/i2c.h"
+
+namespace esphome {
+namespace i2c_device {
+
+class I2CDeviceComponent : public Component, public i2c::I2CDevice {
+ public:
+ void dump_config() override;
+ float get_setup_priority() const override { return setup_priority::DATA; }
+
+ protected:
+};
+
+} // namespace i2c_device
+} // namespace esphome
diff --git a/esphome/components/i2s_audio/__init__.py b/esphome/components/i2s_audio/__init__.py
index d376907925..fa515a585f 100644
--- a/esphome/components/i2s_audio/__init__.py
+++ b/esphome/components/i2s_audio/__init__.py
@@ -8,7 +8,7 @@ from esphome.components.esp32.const import (
VARIANT_ESP32S3,
)
import esphome.config_validation as cv
-from esphome.const import CONF_CHANNEL, CONF_ID, CONF_SAMPLE_RATE
+from esphome.const import CONF_BITS_PER_SAMPLE, CONF_CHANNEL, CONF_ID, CONF_SAMPLE_RATE
from esphome.cpp_generator import MockObjClass
import esphome.final_validate as fv
@@ -25,13 +25,11 @@ CONF_I2S_LRCLK_PIN = "i2s_lrclk_pin"
CONF_I2S_AUDIO = "i2s_audio"
CONF_I2S_AUDIO_ID = "i2s_audio_id"
-CONF_BITS_PER_SAMPLE = "bits_per_sample"
CONF_I2S_MODE = "i2s_mode"
CONF_PRIMARY = "primary"
CONF_SECONDARY = "secondary"
CONF_USE_APLL = "use_apll"
-CONF_BITS_PER_SAMPLE = "bits_per_sample"
CONF_BITS_PER_CHANNEL = "bits_per_channel"
CONF_MONO = "mono"
CONF_LEFT = "left"
diff --git a/esphome/components/i2s_audio/speaker/__init__.py b/esphome/components/i2s_audio/speaker/__init__.py
index 9fdaced64c..0355c16321 100644
--- a/esphome/components/i2s_audio/speaker/__init__.py
+++ b/esphome/components/i2s_audio/speaker/__init__.py
@@ -17,16 +17,17 @@ from .. import (
)
AUTO_LOAD = ["audio"]
-CODEOWNERS = ["@jesserockz"]
+CODEOWNERS = ["@jesserockz", "@kahrendt"]
DEPENDENCIES = ["i2s_audio"]
I2SAudioSpeaker = i2s_audio_ns.class_(
"I2SAudioSpeaker", cg.Component, speaker.Speaker, I2SAudioOut
)
-
+CONF_BUFFER_DURATION = "buffer_duration"
CONF_DAC_TYPE = "dac_type"
CONF_I2S_COMM_FMT = "i2s_comm_fmt"
+CONF_NEVER = "never"
i2s_dac_mode_t = cg.global_ns.enum("i2s_dac_mode_t")
INTERNAL_DAC_OPTIONS = {
@@ -73,8 +74,12 @@ BASE_SCHEMA = (
.extend(
{
cv.Optional(
- CONF_TIMEOUT, default="500ms"
+ CONF_BUFFER_DURATION, default="500ms"
): cv.positive_time_period_milliseconds,
+ cv.Optional(CONF_TIMEOUT, default="500ms"): cv.Any(
+ cv.positive_time_period_milliseconds,
+ cv.one_of(CONF_NEVER, lower=True),
+ ),
}
)
.extend(cv.COMPONENT_SCHEMA)
@@ -116,4 +121,6 @@ async def to_code(config):
else:
cg.add(var.set_dout_pin(config[CONF_I2S_DOUT_PIN]))
cg.add(var.set_i2s_comm_fmt(config[CONF_I2S_COMM_FMT]))
- cg.add(var.set_timeout(config[CONF_TIMEOUT]))
+ if config[CONF_TIMEOUT] != CONF_NEVER:
+ cg.add(var.set_timeout(config[CONF_TIMEOUT]))
+ cg.add(var.set_buffer_duration(config[CONF_BUFFER_DURATION]))
diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
index 4fc489d0a3..53b3cc8dc0 100644
--- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
+++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
@@ -13,25 +13,27 @@
namespace esphome {
namespace i2s_audio {
-static const size_t DMA_BUFFER_SIZE = 512;
+static const uint8_t DMA_BUFFER_DURATION_MS = 15;
static const size_t DMA_BUFFERS_COUNT = 4;
-static const size_t FRAMES_IN_ALL_DMA_BUFFERS = DMA_BUFFER_SIZE * DMA_BUFFERS_COUNT;
-static const size_t RING_BUFFER_SAMPLES = 8192;
-static const size_t TASK_DELAY_MS = 10;
+
+static const size_t TASK_DELAY_MS = DMA_BUFFER_DURATION_MS * DMA_BUFFERS_COUNT / 2;
+
static const size_t TASK_STACK_SIZE = 4096;
static const ssize_t TASK_PRIORITY = 23;
+static const size_t I2S_EVENT_QUEUE_COUNT = DMA_BUFFERS_COUNT + 1;
+
static const char *const TAG = "i2s_audio.speaker";
enum SpeakerEventGroupBits : uint32_t {
- COMMAND_START = (1 << 0), // Starts the main task purpose
- COMMAND_STOP = (1 << 1), // stops the main task
- COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the task once all data has been written
- MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE = (1 << 5), // Locks the ring buffer when not set
+ COMMAND_START = (1 << 0), // starts the speaker task
+ COMMAND_STOP = (1 << 1), // stops the speaker task
+ COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the speaker task once all data has been written
STATE_STARTING = (1 << 10),
STATE_RUNNING = (1 << 11),
STATE_STOPPING = (1 << 12),
STATE_STOPPED = (1 << 13),
+ ERR_INVALID_FORMAT = (1 << 14),
ERR_TASK_FAILED_TO_START = (1 << 15),
ERR_ESP_INVALID_STATE = (1 << 16),
ERR_ESP_INVALID_ARG = (1 << 17),
@@ -90,9 +92,7 @@ static const std::vector Q15_VOLUME_SCALING_FACTORS = {
void I2SAudioSpeaker::setup() {
ESP_LOGCONFIG(TAG, "Setting up I2S Audio Speaker...");
- if (this->event_group_ == nullptr) {
- this->event_group_ = xEventGroupCreate();
- }
+ this->event_group_ = xEventGroupCreate();
if (this->event_group_ == nullptr) {
ESP_LOGE(TAG, "Failed to create event group");
@@ -104,16 +104,6 @@ void I2SAudioSpeaker::setup() {
void I2SAudioSpeaker::loop() {
uint32_t event_group_bits = xEventGroupGetBits(this->event_group_);
- if (event_group_bits & SpeakerEventGroupBits::ERR_TASK_FAILED_TO_START) {
- this->status_set_error("Failed to start speaker task");
- }
-
- if (event_group_bits & SpeakerEventGroupBits::ALL_ERR_ESP_BITS) {
- uint32_t error_bits = event_group_bits & SpeakerEventGroupBits::ALL_ERR_ESP_BITS;
- ESP_LOGW(TAG, "Error writing to I2S: %s", esp_err_to_name(err_bit_to_esp_err(error_bits)));
- this->status_set_warning();
- }
-
if (event_group_bits & SpeakerEventGroupBits::STATE_STARTING) {
ESP_LOGD(TAG, "Starting Speaker");
this->state_ = speaker::STATE_STARTING;
@@ -139,12 +129,64 @@ void I2SAudioSpeaker::loop() {
this->speaker_task_handle_ = nullptr;
}
}
+
+ if (event_group_bits & SpeakerEventGroupBits::ERR_TASK_FAILED_TO_START) {
+ this->status_set_error("Failed to start speaker task");
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::ERR_TASK_FAILED_TO_START);
+ }
+
+ if (event_group_bits & SpeakerEventGroupBits::ERR_INVALID_FORMAT) {
+ this->status_set_error("Failed to adjust I2S bus to match the incoming audio");
+ ESP_LOGE(TAG,
+ "Incompatible audio format: sample rate = %" PRIu32 ", channels = %" PRIu8 ", bits per sample = %" PRIu8,
+ this->audio_stream_info_.sample_rate, this->audio_stream_info_.channels,
+ this->audio_stream_info_.bits_per_sample);
+ }
+
+ if (event_group_bits & SpeakerEventGroupBits::ALL_ERR_ESP_BITS) {
+ uint32_t error_bits = event_group_bits & SpeakerEventGroupBits::ALL_ERR_ESP_BITS;
+ ESP_LOGW(TAG, "Error writing to I2S: %s", esp_err_to_name(err_bit_to_esp_err(error_bits)));
+ this->status_set_warning();
+ }
}
void I2SAudioSpeaker::set_volume(float volume) {
this->volume_ = volume;
- ssize_t decibel_index = remap(volume, 0.0f, 1.0f, 0, Q15_VOLUME_SCALING_FACTORS.size() - 1);
- this->q15_volume_factor_ = Q15_VOLUME_SCALING_FACTORS[decibel_index];
+#ifdef USE_AUDIO_DAC
+ if (this->audio_dac_ != nullptr) {
+ if (volume > 0.0) {
+ this->audio_dac_->set_mute_off();
+ }
+ this->audio_dac_->set_volume(volume);
+ } else
+#endif
+ {
+ // Fallback to software volume control by using a Q15 fixed point scaling factor
+ ssize_t decibel_index = remap(volume, 0.0f, 1.0f, 0, Q15_VOLUME_SCALING_FACTORS.size() - 1);
+ this->q15_volume_factor_ = Q15_VOLUME_SCALING_FACTORS[decibel_index];
+ }
+}
+
+void I2SAudioSpeaker::set_mute_state(bool mute_state) {
+ this->mute_state_ = mute_state;
+#ifdef USE_AUDIO_DAC
+ if (this->audio_dac_) {
+ if (mute_state) {
+ this->audio_dac_->set_mute_on();
+ } else {
+ this->audio_dac_->set_mute_off();
+ }
+ } else
+#endif
+ {
+ if (mute_state) {
+ // Fallback to software volume control and scale by 0
+ this->q15_volume_factor_ = 0;
+ } else {
+ // Revert to previous volume when unmuting
+ this->set_volume(this->volume_);
+ }
+ }
}
size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) {
@@ -156,23 +198,17 @@ size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length, TickType_t tick
this->start();
}
- // Wait for the ring buffer to be available
- uint32_t event_bits =
- xEventGroupWaitBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE, pdFALSE,
- pdFALSE, pdMS_TO_TICKS(TASK_DELAY_MS));
+ size_t bytes_written = 0;
+ if ((this->state_ == speaker::STATE_RUNNING) && (this->audio_ring_buffer_.use_count() == 1)) {
+ // Only one owner of the ring buffer (the speaker task), so the ring buffer is allocated and no other components are
+ // attempting to write to it.
- if (event_bits & SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE) {
- // Ring buffer is available to write
-
- // Lock the ring buffer, write to it, then unlock it
- xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
- size_t bytes_written = this->audio_ring_buffer_->write_without_replacement((void *) data, length, ticks_to_wait);
- xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
-
- return bytes_written;
+ // Temporarily share ownership of the ring buffer so it won't be deallocated while writing
+ std::shared_ptr temp_ring_buffer = this->audio_ring_buffer_;
+ bytes_written = temp_ring_buffer->write_without_replacement((void *) data, length, ticks_to_wait);
}
- return 0;
+ return bytes_written;
}
bool I2SAudioSpeaker::has_buffered_data() const {
@@ -203,10 +239,12 @@ void I2SAudioSpeaker::speaker_task(void *params) {
const ssize_t bytes_per_sample = audio_stream_info.get_bytes_per_sample();
const uint8_t number_of_channels = audio_stream_info.channels;
- const size_t dma_buffers_size = FRAMES_IN_ALL_DMA_BUFFERS * bytes_per_sample * number_of_channels;
+ const size_t dma_buffers_size = DMA_BUFFERS_COUNT * DMA_BUFFER_DURATION_MS * this_speaker->sample_rate_ / 1000 *
+ bytes_per_sample * number_of_channels;
+ const size_t ring_buffer_size =
+ this_speaker->buffer_duration_ms_ * this_speaker->sample_rate_ / 1000 * bytes_per_sample * number_of_channels;
- if (this_speaker->send_esp_err_to_event_group_(
- this_speaker->allocate_buffers_(dma_buffers_size, RING_BUFFER_SAMPLES * bytes_per_sample))) {
+ if (this_speaker->send_esp_err_to_event_group_(this_speaker->allocate_buffers_(dma_buffers_size, ring_buffer_size))) {
// Failed to allocate buffers
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_NO_MEM);
this_speaker->delete_task_(dma_buffers_size);
@@ -215,9 +253,6 @@ void I2SAudioSpeaker::speaker_task(void *params) {
if (this_speaker->send_esp_err_to_event_group_(this_speaker->start_i2s_driver_())) {
// Failed to start I2S driver
this_speaker->delete_task_(dma_buffers_size);
- } else {
- // Ring buffer is allocated, so indicate its can be written to
- xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
}
if (!this_speaker->send_esp_err_to_event_group_(this_speaker->reconfigure_i2s_stream_info_(audio_stream_info))) {
@@ -227,8 +262,10 @@ void I2SAudioSpeaker::speaker_task(void *params) {
bool stop_gracefully = false;
uint32_t last_data_received_time = millis();
+ bool tx_dma_underflow = false;
- while ((millis() - last_data_received_time) <= this_speaker->timeout_) {
+ while (!this_speaker->timeout_.has_value() ||
+ (millis() - last_data_received_time) <= this_speaker->timeout_.value()) {
event_group_bits = xEventGroupGetBits(this_speaker->event_group_);
if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP) {
@@ -238,12 +275,18 @@ void I2SAudioSpeaker::speaker_task(void *params) {
stop_gracefully = true;
}
+ i2s_event_t i2s_event;
+ while (xQueueReceive(this_speaker->i2s_event_queue_, &i2s_event, 0)) {
+ if (i2s_event.type == I2S_EVENT_TX_Q_OVF) {
+ tx_dma_underflow = true;
+ }
+ }
+
size_t bytes_to_read = dma_buffers_size;
size_t bytes_read = this_speaker->audio_ring_buffer_->read((void *) this_speaker->data_buffer_, bytes_to_read,
pdMS_TO_TICKS(TASK_DELAY_MS));
if (bytes_read > 0) {
- last_data_received_time = millis();
size_t bytes_written = 0;
if ((audio_stream_info.bits_per_sample == 16) && (this_speaker->q15_volume_factor_ < INT16_MAX)) {
@@ -264,23 +307,23 @@ void I2SAudioSpeaker::speaker_task(void *params) {
if (bytes_written != bytes_read) {
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE);
}
-
+ tx_dma_underflow = false;
+ last_data_received_time = millis();
} else {
// No data received
-
- if (stop_gracefully) {
+ if (stop_gracefully && tx_dma_underflow) {
break;
}
-
- i2s_zero_dma_buffer(this_speaker->parent_->get_port());
}
}
+ } else {
+ // Couldn't configure the I2S port to be compatible with the incoming audio
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_INVALID_FORMAT);
}
i2s_zero_dma_buffer(this_speaker->parent_->get_port());
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_STOPPING);
- i2s_stop(this_speaker->parent_->get_port());
i2s_driver_uninstall(this_speaker->parent_->get_port());
this_speaker->parent_->unlock();
@@ -288,7 +331,7 @@ void I2SAudioSpeaker::speaker_task(void *params) {
}
void I2SAudioSpeaker::start() {
- if (this->is_failed())
+ if (!this->is_ready() || this->is_failed() || this->status_has_error())
return;
if ((this->state_ == speaker::STATE_STARTING) || (this->state_ == speaker::STATE_RUNNING))
return;
@@ -356,8 +399,8 @@ esp_err_t I2SAudioSpeaker::allocate_buffers_(size_t data_buffer_size, size_t rin
return ESP_ERR_NO_MEM;
}
- if (this->audio_ring_buffer_ == nullptr) {
- // Allocate ring buffer
+ if (this->audio_ring_buffer_.use_count() == 0) {
+ // Allocate ring buffer. Uses a shared_ptr to ensure it isn't improperly deallocated.
this->audio_ring_buffer_ = RingBuffer::create(ring_buffer_size);
}
@@ -373,6 +416,8 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
return ESP_ERR_INVALID_STATE;
}
+ int dma_buffer_length = DMA_BUFFER_DURATION_MS * this->sample_rate_ / 1000;
+
i2s_driver_config_t config = {
.mode = (i2s_mode_t) (this->i2s_mode_ | I2S_MODE_TX),
.sample_rate = this->sample_rate_,
@@ -381,7 +426,7 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
.communication_format = this->i2s_comm_fmt_,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = DMA_BUFFERS_COUNT,
- .dma_buf_len = DMA_BUFFER_SIZE,
+ .dma_buf_len = dma_buffer_length,
.use_apll = this->use_apll_,
.tx_desc_auto_clear = true,
.fixed_mclk = I2S_PIN_NO_CHANGE,
@@ -402,7 +447,8 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
}
#endif
- esp_err_t err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
+ esp_err_t err =
+ i2s_driver_install(this->parent_->get_port(), &config, I2S_EVENT_QUEUE_COUNT, &this->i2s_event_queue_);
if (err != ESP_OK) {
// Failed to install the driver, so unlock the I2S port
this->parent_->unlock();
@@ -456,16 +502,7 @@ esp_err_t I2SAudioSpeaker::reconfigure_i2s_stream_info_(audio::AudioStreamInfo &
}
void I2SAudioSpeaker::delete_task_(size_t buffer_size) {
- if (this->audio_ring_buffer_ != nullptr) {
- xEventGroupWaitBits(this->event_group_,
- MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE, // Bit message to read
- pdFALSE, // Don't clear the bits on exit
- pdTRUE, // Don't wait for all the bits,
- portMAX_DELAY); // Block indefinitely until a command bit is set
-
- this->audio_ring_buffer_.reset(); // Deallocates the ring buffer stored in the unique_ptr
- this->audio_ring_buffer_ = nullptr;
- }
+ this->audio_ring_buffer_.reset(); // Releases onwership of the shared_ptr
if (this->data_buffer_ != nullptr) {
ExternalRAMAllocator allocator(ExternalRAMAllocator::ALLOW_FAILURE);
diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
index 245f97d1e7..2b90f39399 100644
--- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
+++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
@@ -7,6 +7,7 @@
#include
#include
+#include
#include
#include "esphome/components/audio/audio.h"
@@ -22,11 +23,12 @@ namespace i2s_audio {
class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Component {
public:
- float get_setup_priority() const override { return esphome::setup_priority::LATE; }
+ float get_setup_priority() const override { return esphome::setup_priority::PROCESSOR; }
void setup() override;
void loop() override;
+ void set_buffer_duration(uint32_t buffer_duration_ms) { this->buffer_duration_ms_ = buffer_duration_ms; }
void set_timeout(uint32_t ms) { this->timeout_ = ms; }
void set_dout_pin(uint8_t pin) { this->dout_pin_ = pin; }
#if SOC_I2S_SUPPORTS_DAC
@@ -49,11 +51,17 @@ class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Comp
bool has_buffered_data() const override;
- /// @brief Sets the volume of the speaker. It is implemented as a software volume control.
- /// Overrides the default setter to convert the floating point volume to a Q15 fixed-point factor.
- /// @param volume
+ /// @brief Sets the volume of the speaker. Uses the speaker's configured audio dac component. If unavailble, it is
+ /// implemented as a software volume control. Overrides the default setter to convert the floating point volume to a
+ /// Q15 fixed-point factor.
+ /// @param volume between 0.0 and 1.0
void set_volume(float volume) override;
- float get_volume() override { return this->volume_; }
+
+ /// @brief Mutes or unmute the speaker. Uses the speaker's configured audio dac component. If unavailble, it is
+ /// implemented as a software volume control. Overrides the default setter to convert the floating point volume to a
+ /// Q15 fixed-point factor.
+ /// @param mute_state true for muting, false for unmuting
+ void set_mute_state(bool mute_state) override;
protected:
/// @brief Function for the FreeRTOS task handling audio output.
@@ -111,10 +119,14 @@ class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Comp
TaskHandle_t speaker_task_handle_{nullptr};
EventGroupHandle_t event_group_{nullptr};
- uint8_t *data_buffer_;
- std::unique_ptr audio_ring_buffer_;
+ QueueHandle_t i2s_event_queue_;
- uint32_t timeout_;
+ uint8_t *data_buffer_;
+ std::shared_ptr audio_ring_buffer_;
+
+ uint32_t buffer_duration_ms_;
+
+ optional timeout_;
uint8_t dout_pin_;
bool task_created_{false};
diff --git a/esphome/components/ili9xxx/display.py b/esphome/components/ili9xxx/display.py
index 68e3aa953d..739ad07843 100644
--- a/esphome/components/ili9xxx/display.py
+++ b/esphome/components/ili9xxx/display.py
@@ -196,6 +196,10 @@ CONFIG_SCHEMA = cv.All(
_validate,
)
+FINAL_VALIDATE_SCHEMA = spi.final_validate_device_schema(
+ "ili9xxx", require_miso=False, require_mosi=True
+)
+
async def to_code(config):
rhs = MODELS[config[CONF_MODEL]].new()
diff --git a/esphome/components/image/__init__.py b/esphome/components/image/__init__.py
index c72417bcda..8742540067 100644
--- a/esphome/components/image/__init__.py
+++ b/esphome/components/image/__init__.py
@@ -361,24 +361,21 @@ async def to_code(config):
elif config[CONF_TYPE] in ["RGB565"]:
image = image.convert("RGBA")
pixels = list(image.getdata())
- data = [0 for _ in range(height * width * 2)]
+ bytes_per_pixel = 3 if transparent else 2
+ data = [0 for _ in range(height * width * bytes_per_pixel)]
pos = 0
for r, g, b, a in pixels:
R = r >> 3
G = g >> 2
B = b >> 3
rgb = (R << 11) | (G << 5) | B
-
- if transparent:
- if rgb == 0x0020:
- rgb = 0
- if a < 0x80:
- rgb = 0x0020
-
data[pos] = rgb >> 8
pos += 1
data[pos] = rgb & 0xFF
pos += 1
+ if transparent:
+ data[pos] = a
+ pos += 1
elif config[CONF_TYPE] in ["BINARY", "TRANSPARENT_BINARY"]:
if transparent:
diff --git a/esphome/components/image/image.cpp b/esphome/components/image/image.cpp
index ded4c60d25..ca2f659fb0 100644
--- a/esphome/components/image/image.cpp
+++ b/esphome/components/image/image.cpp
@@ -88,7 +88,7 @@ lv_img_dsc_t *Image::get_lv_img_dsc() {
this->dsc_.header.reserved = 0;
this->dsc_.header.w = this->width_;
this->dsc_.header.h = this->height_;
- this->dsc_.data_size = image_type_to_width_stride(this->dsc_.header.w * this->dsc_.header.h, this->get_type());
+ this->dsc_.data_size = this->get_width_stride() * this->get_height();
switch (this->get_type()) {
case IMAGE_TYPE_BINARY:
this->dsc_.header.cf = LV_IMG_CF_ALPHA_1BIT;
@@ -104,17 +104,17 @@ lv_img_dsc_t *Image::get_lv_img_dsc() {
case IMAGE_TYPE_RGB565:
#if LV_COLOR_DEPTH == 16
- this->dsc_.header.cf = this->has_transparency() ? LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED : LV_IMG_CF_TRUE_COLOR;
+ this->dsc_.header.cf = this->has_transparency() ? LV_IMG_CF_TRUE_COLOR_ALPHA : LV_IMG_CF_TRUE_COLOR;
#else
this->dsc_.header.cf = LV_IMG_CF_RGB565;
#endif
break;
- case image::IMAGE_TYPE_RGBA:
+ case IMAGE_TYPE_RGBA:
#if LV_COLOR_DEPTH == 32
this->dsc_.header.cf = LV_IMG_CF_TRUE_COLOR;
#else
- this->dsc_.header.cf = LV_IMG_CF_RGBA8888;
+ this->dsc_.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA;
#endif
break;
}
@@ -147,21 +147,21 @@ Color Image::get_rgb24_pixel_(int x, int y) const {
return color;
}
Color Image::get_rgb565_pixel_(int x, int y) const {
- const uint32_t pos = (x + y * this->width_) * 2;
- uint16_t rgb565 =
- progmem_read_byte(this->data_start_ + pos + 0) << 8 | progmem_read_byte(this->data_start_ + pos + 1);
+ const uint8_t *pos = this->data_start_;
+ if (this->transparent_) {
+ pos += (x + y * this->width_) * 3;
+ } else {
+ pos += (x + y * this->width_) * 2;
+ }
+ uint16_t rgb565 = encode_uint16(progmem_read_byte(pos), progmem_read_byte(pos + 1));
auto r = (rgb565 & 0xF800) >> 11;
auto g = (rgb565 & 0x07E0) >> 5;
auto b = rgb565 & 0x001F;
- Color color = Color((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2));
- if (rgb565 == 0x0020 && transparent_) {
- // darkest green has been defined as transparent color for transparent RGB565 images.
- color.w = 0;
- } else {
- color.w = 0xFF;
- }
+ auto a = this->transparent_ ? progmem_read_byte(pos + 2) : 0xFF;
+ Color color = Color((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2), a);
return color;
}
+
Color Image::get_grayscale_pixel_(int x, int y) const {
const uint32_t pos = (x + y * this->width_);
const uint8_t gray = progmem_read_byte(this->data_start_ + pos);
diff --git a/esphome/components/image/image.h b/esphome/components/image/image.h
index ae5a7a814d..40370d18da 100644
--- a/esphome/components/image/image.h
+++ b/esphome/components/image/image.h
@@ -3,12 +3,7 @@
#include "esphome/components/display/display.h"
#ifdef USE_LVGL
-// required for clang-tidy
-#ifndef LV_CONF_H
-#define LV_CONF_SKIP 1 // NOLINT
-#endif // LV_CONF_H
-
-#include
+#include "esphome/components/lvgl/lvgl_proxy.h"
#endif // USE_LVGL
namespace esphome {
@@ -22,24 +17,6 @@ enum ImageType {
IMAGE_TYPE_RGBA = 4,
};
-inline int image_type_to_bpp(ImageType type) {
- switch (type) {
- case IMAGE_TYPE_BINARY:
- return 1;
- case IMAGE_TYPE_GRAYSCALE:
- return 8;
- case IMAGE_TYPE_RGB565:
- return 16;
- case IMAGE_TYPE_RGB24:
- return 24;
- case IMAGE_TYPE_RGBA:
- return 32;
- }
- return 0;
-}
-
-inline int image_type_to_width_stride(int width, ImageType type) { return (width * image_type_to_bpp(type) + 7u) / 8u; }
-
class Image : public display::BaseImage {
public:
Image(const uint8_t *data_start, int width, int height, ImageType type);
@@ -49,6 +26,25 @@ class Image : public display::BaseImage {
const uint8_t *get_data_start() const { return this->data_start_; }
ImageType get_type() const;
+ int get_bpp() const {
+ switch (this->type_) {
+ case IMAGE_TYPE_BINARY:
+ return 1;
+ case IMAGE_TYPE_GRAYSCALE:
+ return 8;
+ case IMAGE_TYPE_RGB565:
+ return this->transparent_ ? 24 : 16;
+ case IMAGE_TYPE_RGB24:
+ return 24;
+ case IMAGE_TYPE_RGBA:
+ return 32;
+ }
+ return 0;
+ }
+
+ /// Return the stride of the image in bytes, that is, the distance in bytes
+ /// between two consecutive rows of pixels.
+ uint32_t get_width_stride() const { return (this->width_ * this->get_bpp() + 7u) / 8u; }
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override;
void set_transparency(bool transparent) { transparent_ = transparent; }
diff --git a/esphome/components/internal_temperature/internal_temperature.cpp b/esphome/components/internal_temperature/internal_temperature.cpp
index 9ef5cbecd5..afa5583e59 100644
--- a/esphome/components/internal_temperature/internal_temperature.cpp
+++ b/esphome/components/internal_temperature/internal_temperature.cpp
@@ -8,8 +8,13 @@ extern "C" {
uint8_t temprature_sens_read();
}
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || \
- defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
+ defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || \
+ defined(USE_ESP32_VARIANT_ESP32C2)
+#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
#include "driver/temp_sensor.h"
+#else
+#include "driver/temperature_sensor.h"
+#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
#endif // USE_ESP32_VARIANT
#endif // USE_ESP32
#ifdef USE_RP2040
@@ -25,6 +30,13 @@ namespace esphome {
namespace internal_temperature {
static const char *const TAG = "internal_temperature";
+#ifdef USE_ESP32
+#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)) && \
+ (defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S2) || \
+ defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32C2))
+static temperature_sensor_handle_t tsensNew = NULL;
+#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && USE_ESP32_VARIANT
+#endif // USE_ESP32
void InternalTemperatureSensor::update() {
float temperature = NAN;
@@ -36,7 +48,9 @@ void InternalTemperatureSensor::update() {
temperature = (raw - 32) / 1.8f;
success = (raw != 128);
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || \
- defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
+ defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || \
+ defined(USE_ESP32_VARIANT_ESP32C2)
+#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
temp_sensor_config_t tsens = TSENS_CONFIG_DEFAULT();
temp_sensor_set_config(tsens);
temp_sensor_start();
@@ -47,6 +61,13 @@ void InternalTemperatureSensor::update() {
esp_err_t result = temp_sensor_read_celsius(&temperature);
temp_sensor_stop();
success = (result == ESP_OK);
+#else
+ esp_err_t result = temperature_sensor_get_celsius(tsensNew, &temperature);
+ success = (result == ESP_OK);
+ if (!success) {
+ ESP_LOGE(TAG, "Failed to get temperature: %d", result);
+ }
+#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
#endif // USE_ESP32_VARIANT
#endif // USE_ESP32
#ifdef USE_RP2040
@@ -75,6 +96,32 @@ void InternalTemperatureSensor::update() {
}
}
+void InternalTemperatureSensor::setup() {
+#ifdef USE_ESP32
+#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)) && \
+ (defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S2) || \
+ defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32C2))
+ ESP_LOGCONFIG(TAG, "Setting up temperature sensor...");
+
+ temperature_sensor_config_t tsens_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
+
+ esp_err_t result = temperature_sensor_install(&tsens_config, &tsensNew);
+ if (result != ESP_OK) {
+ ESP_LOGE(TAG, "Failed to install temperature sensor: %d", result);
+ this->mark_failed();
+ return;
+ }
+
+ result = temperature_sensor_enable(tsensNew);
+ if (result != ESP_OK) {
+ ESP_LOGE(TAG, "Failed to enable temperature sensor: %d", result);
+ this->mark_failed();
+ return;
+ }
+#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && USE_ESP32_VARIANT
+#endif // USE_ESP32
+}
+
void InternalTemperatureSensor::dump_config() { LOG_SENSOR("", "Internal Temperature Sensor", this); }
} // namespace internal_temperature
diff --git a/esphome/components/internal_temperature/internal_temperature.h b/esphome/components/internal_temperature/internal_temperature.h
index 0e46a69769..78e3bcef7d 100644
--- a/esphome/components/internal_temperature/internal_temperature.h
+++ b/esphome/components/internal_temperature/internal_temperature.h
@@ -8,6 +8,7 @@ namespace internal_temperature {
class InternalTemperatureSensor : public sensor::Sensor, public PollingComponent {
public:
+ void setup() override;
void dump_config() override;
void update() override;
diff --git a/esphome/components/ld2420/ld2420.cpp b/esphome/components/ld2420/ld2420.cpp
index e57fdbc84e..9d628cc14f 100644
--- a/esphome/components/ld2420/ld2420.cpp
+++ b/esphome/components/ld2420/ld2420.cpp
@@ -180,7 +180,7 @@ void LD2420Component::apply_config_action() {
}
void LD2420Component::factory_reset_action() {
- ESP_LOGCONFIG(TAG, "Setiing factory defaults...");
+ ESP_LOGCONFIG(TAG, "Setting factory defaults...");
if (this->set_config_mode(true) == LD2420_ERROR_TIMEOUT) {
ESP_LOGE(TAG, "LD2420 module has failed to respond, check baud rate and serial connections.");
this->mark_failed();
diff --git a/esphome/components/libretiny/__init__.py b/esphome/components/libretiny/__init__.py
index cc7fae7e70..b29d2e309c 100644
--- a/esphome/components/libretiny/__init__.py
+++ b/esphome/components/libretiny/__init__.py
@@ -46,7 +46,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@kuba2k2"]
-AUTO_LOAD = []
+AUTO_LOAD = ["preferences"]
def _detect_variant(value):
diff --git a/esphome/components/libretiny/gpio.py b/esphome/components/libretiny/gpio.py
index 1d7b37cc9b..07eb0ce133 100644
--- a/esphome/components/libretiny/gpio.py
+++ b/esphome/components/libretiny/gpio.py
@@ -1,8 +1,8 @@
import logging
+from esphome import pins
import esphome.codegen as cg
import esphome.config_validation as cv
-from esphome import pins
from esphome.const import (
CONF_ANALOG,
CONF_ID,
@@ -103,8 +103,10 @@ def _translate_pin(value):
"This variable only supports pin numbers, not full pin schemas "
"(with inverted and mode)."
)
- if isinstance(value, int):
+ if isinstance(value, int) and not isinstance(value, bool):
return value
+ if not isinstance(value, str):
+ raise cv.Invalid(f"Invalid pin number: {value}")
try:
return int(value)
except ValueError:
diff --git a/esphome/components/lvgl/__init__.py b/esphome/components/lvgl/__init__.py
index beaf279a9a..d03adc9624 100644
--- a/esphome/components/lvgl/__init__.py
+++ b/esphome/components/lvgl/__init__.py
@@ -7,6 +7,7 @@ import esphome.config_validation as cv
from esphome.const import (
CONF_AUTO_CLEAR_ENABLED,
CONF_BUFFER_SIZE,
+ CONF_GROUP,
CONF_ID,
CONF_LAMBDA,
CONF_ON_IDLE,
@@ -23,17 +24,23 @@ from esphome.helpers import write_file_if_changed
from . import defines as df, helpers, lv_validation as lvalid
from .automation import disp_update, focused_widgets, update_to_code
from .defines import add_define
-from .encoders import ENCODERS_CONFIG, encoders_to_code, initial_focus_to_code
+from .encoders import (
+ ENCODERS_CONFIG,
+ encoders_to_code,
+ get_default_group,
+ initial_focus_to_code,
+)
from .gradient import GRADIENT_SCHEMA, gradients_to_code
from .hello_world import get_hello_world
+from .keypads import KEYPADS_CONFIG, keypads_to_code
from .lv_validation import lv_bool, lv_images_used
-from .lvcode import LvContext, LvglComponent
+from .lvcode import LvContext, LvglComponent, lvgl_static
from .schemas import (
DISP_BG_SCHEMA,
FLEX_OBJ_SCHEMA,
GRID_CELL_SCHEMA,
LAYOUT_SCHEMAS,
- STATE_SCHEMA,
+ STYLE_SCHEMA,
WIDGET_TYPES,
any_widget_schema,
container_schema,
@@ -71,6 +78,7 @@ from .widgets.meter import meter_spec
from .widgets.msgbox import MSGBOX_SCHEMA, msgboxes_to_code
from .widgets.obj import obj_spec
from .widgets.page import add_pages, generate_page_triggers, page_spec
+from .widgets.qrcode import qr_code_spec
from .widgets.roller import roller_spec
from .widgets.slider import slider_spec
from .widgets.spinbox import spinbox_spec
@@ -109,6 +117,7 @@ for w_type in (
spinbox_spec,
keyboard_spec,
tileview_spec,
+ qr_code_spec,
):
WIDGET_TYPES[w_type.name] = w_type
@@ -150,41 +159,78 @@ def generate_lv_conf_h():
return LV_CONF_H_FORMAT.format("\n".join(definitions))
-def final_validation(config):
- if pages := config.get(CONF_PAGES):
- if all(p[df.CONF_SKIP] for p in pages):
- raise cv.Invalid("At least one page must not be skipped")
+def multi_conf_validate(configs: list[dict]):
+ displays = [config[df.CONF_DISPLAYS] for config in configs]
+ # flatten the display list
+ display_list = [disp for disps in displays for disp in disps]
+ if len(display_list) != len(set(display_list)):
+ raise cv.Invalid("A display ID may be used in only one LVGL instance")
+ for config in configs:
+ for item in (df.CONF_ENCODERS, df.CONF_KEYPADS):
+ for enc in config.get(item, ()):
+ if CONF_GROUP not in enc:
+ raise cv.Invalid(
+ f"'{item}' must have an explicit group set when using multiple LVGL instances"
+ )
+ base_config = configs[0]
+ for config in configs[1:]:
+ for item in (
+ df.CONF_LOG_LEVEL,
+ df.CONF_COLOR_DEPTH,
+ df.CONF_BYTE_ORDER,
+ df.CONF_TRANSPARENCY_KEY,
+ ):
+ if base_config[item] != config[item]:
+ raise cv.Invalid(
+ f"Config item '{item}' must be the same for all LVGL instances"
+ )
+
+
+def final_validation(configs):
+ if len(configs) != 1:
+ multi_conf_validate(configs)
global_config = full_config.get()
- for display_id in config[df.CONF_DISPLAYS]:
- path = global_config.get_path_for_id(display_id)[:-1]
- display = global_config.get_config_for_path(path)
- if CONF_LAMBDA in display:
- raise cv.Invalid("Using lambda: in display config not compatible with LVGL")
- if display[CONF_AUTO_CLEAR_ENABLED]:
- raise cv.Invalid(
- "Using auto_clear_enabled: true in display config not compatible with LVGL"
- )
- buffer_frac = config[CONF_BUFFER_SIZE]
- if CORE.is_esp32 and buffer_frac > 0.5 and "psram" not in global_config:
- LOGGER.warning("buffer_size: may need to be reduced without PSRAM")
- for image_id in lv_images_used:
- path = global_config.get_path_for_id(image_id)[:-1]
- image_conf = global_config.get_config_for_path(path)
- if image_conf[CONF_TYPE] in ("RGBA", "RGB24"):
- raise cv.Invalid(
- "Using RGBA or RGB24 in image config not compatible with LVGL", path
- )
- for w in focused_widgets:
- path = global_config.get_path_for_id(w)
- widget_conf = global_config.get_config_for_path(path[:-1])
- if df.CONF_ADJUSTABLE in widget_conf and not widget_conf[df.CONF_ADJUSTABLE]:
- raise cv.Invalid(
- "A non adjustable arc may not be focused",
- path,
- )
+ for config in configs:
+ if pages := config.get(CONF_PAGES):
+ if all(p[df.CONF_SKIP] for p in pages):
+ raise cv.Invalid("At least one page must not be skipped")
+ for display_id in config[df.CONF_DISPLAYS]:
+ path = global_config.get_path_for_id(display_id)[:-1]
+ display = global_config.get_config_for_path(path)
+ if CONF_LAMBDA in display:
+ raise cv.Invalid(
+ "Using lambda: in display config not compatible with LVGL"
+ )
+ if display[CONF_AUTO_CLEAR_ENABLED]:
+ raise cv.Invalid(
+ "Using auto_clear_enabled: true in display config not compatible with LVGL"
+ )
+ buffer_frac = config[CONF_BUFFER_SIZE]
+ if CORE.is_esp32 and buffer_frac > 0.5 and "psram" not in global_config:
+ LOGGER.warning("buffer_size: may need to be reduced without PSRAM")
+ for image_id in lv_images_used:
+ path = global_config.get_path_for_id(image_id)[:-1]
+ image_conf = global_config.get_config_for_path(path)
+ if image_conf[CONF_TYPE] in ("RGBA", "RGB24"):
+ raise cv.Invalid(
+ "Using RGBA or RGB24 in image config not compatible with LVGL", path
+ )
+ for w in focused_widgets:
+ path = global_config.get_path_for_id(w)
+ widget_conf = global_config.get_config_for_path(path[:-1])
+ if (
+ df.CONF_ADJUSTABLE in widget_conf
+ and not widget_conf[df.CONF_ADJUSTABLE]
+ ):
+ raise cv.Invalid(
+ "A non adjustable arc may not be focused",
+ path,
+ )
-async def to_code(config):
+async def to_code(configs):
+ config_0 = configs[0]
+ # Global configuration
cg.add_library("lvgl/lvgl", "8.4.0")
cg.add_define("USE_LVGL")
# suppress default enabling of extra widgets
@@ -201,53 +247,33 @@ async def to_code(config):
add_define("LV_MEM_CUSTOM_INCLUDE", '"esphome/components/lvgl/lvgl_hal.h"')
add_define(
- "LV_LOG_LEVEL", f"LV_LOG_LEVEL_{df.LV_LOG_LEVELS[config[df.CONF_LOG_LEVEL]]}"
+ "LV_LOG_LEVEL",
+ f"LV_LOG_LEVEL_{df.LV_LOG_LEVELS[config_0[df.CONF_LOG_LEVEL]]}",
)
cg.add_define(
"LVGL_LOG_LEVEL",
- cg.RawExpression(f"ESPHOME_LOG_LEVEL_{config[df.CONF_LOG_LEVEL]}"),
+ cg.RawExpression(f"ESPHOME_LOG_LEVEL_{config_0[df.CONF_LOG_LEVEL]}"),
)
- add_define("LV_COLOR_DEPTH", config[df.CONF_COLOR_DEPTH])
+ add_define("LV_COLOR_DEPTH", config_0[df.CONF_COLOR_DEPTH])
for font in helpers.lv_fonts_used:
add_define(f"LV_FONT_{font.upper()}")
- if config[df.CONF_COLOR_DEPTH] == 16:
+ if config_0[df.CONF_COLOR_DEPTH] == 16:
add_define(
"LV_COLOR_16_SWAP",
- "1" if config[df.CONF_BYTE_ORDER] == "big_endian" else "0",
+ "1" if config_0[df.CONF_BYTE_ORDER] == "big_endian" else "0",
)
add_define(
"LV_COLOR_CHROMA_KEY",
- await lvalid.lv_color.process(config[df.CONF_TRANSPARENCY_KEY]),
+ await lvalid.lv_color.process(config_0[df.CONF_TRANSPARENCY_KEY]),
)
cg.add_build_flag("-Isrc")
cg.add_global(lvgl_ns.using)
- frac = config[CONF_BUFFER_SIZE]
- if frac >= 0.75:
- frac = 1
- elif frac >= 0.375:
- frac = 2
- elif frac > 0.19:
- frac = 4
- else:
- frac = 8
- displays = [await cg.get_variable(display) for display in config[df.CONF_DISPLAYS]]
- lv_component = cg.new_Pvariable(
- config[CONF_ID],
- displays,
- frac,
- config[df.CONF_FULL_REFRESH],
- config[df.CONF_DRAW_ROUNDING],
- config[df.CONF_RESUME_ON_INPUT],
- )
- await cg.register_component(lv_component, config)
- Widget.create(config[CONF_ID], lv_component, obj_spec, config)
-
for font in helpers.esphome_fonts_used:
await cg.get_variable(font)
cg.new_Pvariable(ID(f"{font}_engine", True, type=FontEngine), MockObj(font))
- default_font = config[df.CONF_DEFAULT_FONT]
+ default_font = config_0[df.CONF_DEFAULT_FONT]
if not lvalid.is_lv_font(default_font):
add_define(
"LV_FONT_CUSTOM_DECLARE", f"LV_FONT_DECLARE(*{df.DEFAULT_ESPHOME_FONT})"
@@ -263,39 +289,73 @@ async def to_code(config):
add_define("LV_FONT_DEFAULT", df.DEFAULT_ESPHOME_FONT)
else:
add_define("LV_FONT_DEFAULT", await lvalid.lv_font.process(default_font))
+ cg.add(lvgl_static.esphome_lvgl_init())
+ default_group = get_default_group(config_0)
- lv_scr_act = get_scr_act(lv_component)
- async with LvContext(lv_component):
- await touchscreens_to_code(lv_component, config)
- await encoders_to_code(lv_component, config)
- await theme_to_code(config)
- await styles_to_code(config)
- await gradients_to_code(config)
- await set_obj_properties(lv_scr_act, config)
- await add_widgets(lv_scr_act, config)
- await add_pages(lv_component, config)
- await add_top_layer(lv_component, config)
- await msgboxes_to_code(lv_component, config)
- await disp_update(lv_component.get_disp(), config)
+ for config in configs:
+ frac = config[CONF_BUFFER_SIZE]
+ if frac >= 0.75:
+ frac = 1
+ elif frac >= 0.375:
+ frac = 2
+ elif frac > 0.19:
+ frac = 4
+ else:
+ frac = 8
+ displays = [
+ await cg.get_variable(display) for display in config[df.CONF_DISPLAYS]
+ ]
+ lv_component = cg.new_Pvariable(
+ config[CONF_ID],
+ displays,
+ frac,
+ config[df.CONF_FULL_REFRESH],
+ config[df.CONF_DRAW_ROUNDING],
+ config[df.CONF_RESUME_ON_INPUT],
+ )
+ await cg.register_component(lv_component, config)
+ Widget.create(config[CONF_ID], lv_component, obj_spec, config)
+
+ lv_scr_act = get_scr_act(lv_component)
+ async with LvContext():
+ await touchscreens_to_code(lv_component, config)
+ await encoders_to_code(lv_component, config, default_group)
+ await keypads_to_code(lv_component, config, default_group)
+ await theme_to_code(config)
+ await styles_to_code(config)
+ await gradients_to_code(config)
+ await set_obj_properties(lv_scr_act, config)
+ await add_widgets(lv_scr_act, config)
+ await add_pages(lv_component, config)
+ await add_top_layer(lv_component, config)
+ await msgboxes_to_code(lv_component, config)
+ await disp_update(lv_component.get_disp(), config)
# Set this directly since we are limited in how many methods can be added to the Widget class.
Widget.widgets_completed = True
- async with LvContext(lv_component):
- await generate_triggers(lv_component)
- await generate_page_triggers(lv_component, config)
- await initial_focus_to_code(config)
- for conf in config.get(CONF_ON_IDLE, ()):
- templ = await cg.templatable(conf[CONF_TIMEOUT], [], cg.uint32)
- idle_trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], lv_component, templ)
- await build_automation(idle_trigger, [], conf)
- for conf in config.get(df.CONF_ON_PAUSE, ()):
- pause_trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], lv_component, True)
- await build_automation(pause_trigger, [], conf)
- for conf in config.get(df.CONF_ON_RESUME, ()):
- resume_trigger = cg.new_Pvariable(
- conf[CONF_TRIGGER_ID], lv_component, False
- )
- await build_automation(resume_trigger, [], conf)
+ async with LvContext():
+ await generate_triggers()
+ for config in configs:
+ lv_component = await cg.get_variable(config[CONF_ID])
+ await generate_page_triggers(config)
+ await initial_focus_to_code(config)
+ for conf in config.get(CONF_ON_IDLE, ()):
+ templ = await cg.templatable(conf[CONF_TIMEOUT], [], cg.uint32)
+ idle_trigger = cg.new_Pvariable(
+ conf[CONF_TRIGGER_ID], lv_component, templ
+ )
+ await build_automation(idle_trigger, [], conf)
+ for conf in config.get(df.CONF_ON_PAUSE, ()):
+ pause_trigger = cg.new_Pvariable(
+ conf[CONF_TRIGGER_ID], lv_component, True
+ )
+ await build_automation(pause_trigger, [], conf)
+ for conf in config.get(df.CONF_ON_RESUME, ()):
+ resume_trigger = cg.new_Pvariable(
+ conf[CONF_TRIGGER_ID], lv_component, False
+ )
+ await build_automation(resume_trigger, [], conf)
+ # This must be done after all widgets are created
for comp in helpers.lvgl_components_required:
cg.add_define(f"USE_LVGL_{comp.upper()}")
if "transform_angle" in styles_used:
@@ -310,7 +370,10 @@ async def to_code(config):
def display_schema(config):
value = cv.ensure_list(cv.use_id(Display))(config)
- return value or [cv.use_id(Display)(config)]
+ value = value or [cv.use_id(Display)(config)]
+ if len(set(value)) != len(value):
+ raise cv.Invalid("Display IDs must be unique")
+ return value
def add_hello_world(config):
@@ -322,7 +385,7 @@ def add_hello_world(config):
FINAL_VALIDATE_SCHEMA = final_validation
-CONFIG_SCHEMA = (
+LVGL_SCHEMA = (
cv.polling_component_schema("1s")
.extend(obj_schema(obj_spec))
.extend(
@@ -342,7 +405,7 @@ CONFIG_SCHEMA = (
),
cv.Optional(df.CONF_STYLE_DEFINITIONS): cv.ensure_list(
cv.Schema({cv.Required(CONF_ID): cv.declare_id(lv_style_t)})
- .extend(STATE_SCHEMA)
+ .extend(STYLE_SCHEMA)
.extend(
{
cv.Optional(df.CONF_GRID_CELL_X_ALIGN): grid_alignments,
@@ -384,6 +447,7 @@ CONFIG_SCHEMA = (
cv.Optional(df.CONF_GRADIENTS): GRADIENT_SCHEMA,
cv.Optional(df.CONF_TOUCHSCREENS, default=None): touchscreen_schema,
cv.Optional(df.CONF_ENCODERS, default=None): ENCODERS_CONFIG,
+ cv.Optional(df.CONF_KEYPADS, default=None): KEYPADS_CONFIG,
cv.GenerateID(df.CONF_DEFAULT_GROUP): cv.declare_id(lv_group_t),
cv.Optional(df.CONF_RESUME_ON_INPUT, default=True): cv.boolean,
}
@@ -391,3 +455,16 @@ CONFIG_SCHEMA = (
.extend(DISP_BG_SCHEMA)
.add_extra(add_hello_world)
)
+
+
+def lvgl_config_schema(config):
+ """
+ Can't use cv.ensure_list here because it converts an empty config to an empty list,
+ rather than a default config.
+ """
+ if not config or isinstance(config, dict):
+ return [LVGL_SCHEMA(config)]
+ return cv.Schema([LVGL_SCHEMA])(config)
+
+
+CONFIG_SCHEMA = lvgl_config_schema
diff --git a/esphome/components/lvgl/automation.py b/esphome/components/lvgl/automation.py
index 48472354f8..c26ae54892 100644
--- a/esphome/components/lvgl/automation.py
+++ b/esphome/components/lvgl/automation.py
@@ -1,5 +1,4 @@
-from collections.abc import Awaitable
-from typing import Callable
+from typing import Any, Callable
from esphome import automation
import esphome.codegen as cg
@@ -23,7 +22,6 @@ from .lvcode import (
UPDATE_EVENT,
LambdaContext,
LocalVariable,
- LvglComponent,
ReturnStatement,
add_line_marks,
lv,
@@ -58,7 +56,7 @@ focused_widgets = set()
async def action_to_code(
widgets: list[Widget],
- action: Callable[[Widget], Awaitable[None]],
+ action: Callable[[Widget], Any],
action_id,
template_arg,
args,
@@ -137,20 +135,18 @@ async def disp_update(disp, config: dict):
cv.maybe_simple_value(
{
cv.Required(CONF_ID): cv.use_id(lv_obj_t),
- cv.GenerateID(CONF_LVGL_ID): cv.use_id(LvglComponent),
},
key=CONF_ID,
),
- cv.Schema(
- {
- cv.GenerateID(CONF_LVGL_ID): cv.use_id(LvglComponent),
- }
- ),
+ LVGL_SCHEMA,
),
)
async def obj_invalidate_to_code(config, action_id, template_arg, args):
- lv_comp = await cg.get_variable(config[CONF_LVGL_ID])
- widgets = await get_widgets(config) or [get_scr_act(lv_comp)]
+ if CONF_LVGL_ID in config:
+ lv_comp = await cg.get_variable(config[CONF_LVGL_ID])
+ widgets = [get_scr_act(lv_comp)]
+ else:
+ widgets = await get_widgets(config)
async def do_invalidate(widget: Widget):
lv_obj.invalidate(widget.obj)
@@ -161,14 +157,12 @@ async def obj_invalidate_to_code(config, action_id, template_arg, args):
@automation.register_action(
"lvgl.update",
LvglAction,
- DISP_BG_SCHEMA.extend(
- {
- cv.GenerateID(): cv.use_id(LvglComponent),
- }
- ).add_extra(cv.has_at_least_one_key(CONF_DISP_BG_COLOR, CONF_DISP_BG_IMAGE)),
+ DISP_BG_SCHEMA.extend(LVGL_SCHEMA).add_extra(
+ cv.has_at_least_one_key(CONF_DISP_BG_COLOR, CONF_DISP_BG_IMAGE)
+ ),
)
async def lvgl_update_to_code(config, action_id, template_arg, args):
- widgets = await get_widgets(config)
+ widgets = await get_widgets(config, CONF_LVGL_ID)
w = widgets[0]
disp = literal(f"{w.obj}->get_disp()")
async with LambdaContext(LVGL_COMP_ARG, where=action_id) as context:
@@ -181,32 +175,33 @@ async def lvgl_update_to_code(config, action_id, template_arg, args):
@automation.register_action(
"lvgl.pause",
LvglAction,
- {
- cv.GenerateID(): cv.use_id(LvglComponent),
- cv.Optional(CONF_SHOW_SNOW, default=False): lv_bool,
- },
+ LVGL_SCHEMA.extend(
+ {
+ cv.Optional(CONF_SHOW_SNOW, default=False): lv_bool,
+ }
+ ),
)
async def pause_action_to_code(config, action_id, template_arg, args):
+ lv_comp = await cg.get_variable(config[CONF_LVGL_ID])
async with LambdaContext(LVGL_COMP_ARG) as context:
add_line_marks(where=action_id)
lv_add(lvgl_comp.set_paused(True, config[CONF_SHOW_SNOW]))
var = cg.new_Pvariable(action_id, template_arg, await context.get_lambda())
- await cg.register_parented(var, config[CONF_ID])
+ await cg.register_parented(var, lv_comp)
return var
@automation.register_action(
"lvgl.resume",
LvglAction,
- {
- cv.GenerateID(): cv.use_id(LvglComponent),
- },
+ LVGL_SCHEMA,
)
async def resume_action_to_code(config, action_id, template_arg, args):
+ lv_comp = await cg.get_variable(config[CONF_LVGL_ID])
async with LambdaContext(LVGL_COMP_ARG, where=action_id) as context:
lv_add(lvgl_comp.set_paused(False, False))
var = cg.new_Pvariable(action_id, template_arg, await context.get_lambda())
- await cg.register_parented(var, config[CONF_ID])
+ await cg.register_parented(var, lv_comp)
return var
@@ -265,14 +260,15 @@ def focused_id(value):
ObjUpdateAction,
cv.Any(
cv.maybe_simple_value(
- {
- cv.Optional(CONF_GROUP): cv.use_id(lv_group_t),
- cv.Required(CONF_ACTION): cv.one_of(
- "MARK", "RESTORE", "NEXT", "PREVIOUS", upper=True
- ),
- cv.GenerateID(CONF_LVGL_ID): cv.use_id(LvglComponent),
- cv.Optional(CONF_FREEZE, default=False): cv.boolean,
- },
+ LVGL_SCHEMA.extend(
+ {
+ cv.Optional(CONF_GROUP): cv.use_id(lv_group_t),
+ cv.Required(CONF_ACTION): cv.one_of(
+ "MARK", "RESTORE", "NEXT", "PREVIOUS", upper=True
+ ),
+ cv.Optional(CONF_FREEZE, default=False): cv.boolean,
+ }
+ ),
key=CONF_ACTION,
),
cv.maybe_simple_value(
diff --git a/esphome/components/lvgl/binary_sensor/__init__.py b/esphome/components/lvgl/binary_sensor/__init__.py
index 56984405aa..ffbdc977b2 100644
--- a/esphome/components/lvgl/binary_sensor/__init__.py
+++ b/esphome/components/lvgl/binary_sensor/__init__.py
@@ -1,4 +1,3 @@
-import esphome.codegen as cg
from esphome.components.binary_sensor import (
BinarySensor,
binary_sensor_schema,
@@ -6,36 +5,30 @@ from esphome.components.binary_sensor import (
)
import esphome.config_validation as cv
-from ..defines import CONF_LVGL_ID, CONF_WIDGET
-from ..lvcode import EVENT_ARG, LambdaContext, LvContext
-from ..schemas import LVGL_SCHEMA
+from ..defines import CONF_WIDGET
+from ..lvcode import EVENT_ARG, LambdaContext, LvContext, lvgl_static
from ..types import LV_EVENT, lv_pseudo_button_t
from ..widgets import Widget, get_widgets, wait_for_widgets
-CONFIG_SCHEMA = (
- binary_sensor_schema(BinarySensor)
- .extend(LVGL_SCHEMA)
- .extend(
- {
- cv.Required(CONF_WIDGET): cv.use_id(lv_pseudo_button_t),
- }
- )
+CONFIG_SCHEMA = binary_sensor_schema(BinarySensor).extend(
+ {
+ cv.Required(CONF_WIDGET): cv.use_id(lv_pseudo_button_t),
+ }
)
async def to_code(config):
sensor = await new_binary_sensor(config)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
assert isinstance(widget, Widget)
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as pressed_ctx:
pressed_ctx.add(sensor.publish_state(widget.is_pressed()))
- async with LvContext(paren) as ctx:
+ async with LvContext() as ctx:
ctx.add(sensor.publish_initial_state(widget.is_pressed()))
ctx.add(
- paren.add_event_cb(
+ lvgl_static.add_event_cb(
widget.obj,
await pressed_ctx.get_lambda(),
LV_EVENT.PRESSING,
diff --git a/esphome/components/lvgl/defines.py b/esphome/components/lvgl/defines.py
index 4d48028611..ea345fa55c 100644
--- a/esphome/components/lvgl/defines.py
+++ b/esphome/components/lvgl/defines.py
@@ -438,6 +438,7 @@ CONF_HEADER_MODE = "header_mode"
CONF_HOME = "home"
CONF_INITIAL_FOCUS = "initial_focus"
CONF_KEY_CODE = "key_code"
+CONF_KEYPADS = "keypads"
CONF_LAYOUT = "layout"
CONF_LEFT_BUTTON = "left_button"
CONF_LINE_WIDTH = "line_width"
diff --git a/esphome/components/lvgl/encoders.py b/esphome/components/lvgl/encoders.py
index 81bcda95b4..952572df43 100644
--- a/esphome/components/lvgl/encoders.py
+++ b/esphome/components/lvgl/encoders.py
@@ -17,7 +17,7 @@ from .defines import (
from .helpers import lvgl_components_required, requires_component
from .lvcode import lv, lv_add, lv_assign, lv_expr, lv_Pvariable
from .schemas import ENCODER_SCHEMA
-from .types import lv_group_t, lv_indev_type_t
+from .types import lv_group_t, lv_indev_type_t, lv_key_t
ENCODERS_CONFIG = cv.ensure_list(
ENCODER_SCHEMA.extend(
@@ -39,10 +39,13 @@ ENCODERS_CONFIG = cv.ensure_list(
)
-async def encoders_to_code(var, config):
- default_group = lv_Pvariable(lv_group_t, config[CONF_DEFAULT_GROUP])
- lv_assign(default_group, lv_expr.group_create())
- lv.group_set_default(default_group)
+def get_default_group(config):
+ default_group = cg.Pvariable(config[CONF_DEFAULT_GROUP], lv_expr.group_create())
+ cg.add(lv.group_set_default(default_group))
+ return default_group
+
+
+async def encoders_to_code(var, config, default_group):
for enc_conf in config[CONF_ENCODERS]:
lvgl_components_required.add("KEY_LISTENER")
lpt = enc_conf[CONF_LONG_PRESS_TIME].total_milliseconds
@@ -54,14 +57,14 @@ async def encoders_to_code(var, config):
if sensor_config := enc_conf.get(CONF_SENSOR):
if isinstance(sensor_config, dict):
b_sensor = await cg.get_variable(sensor_config[CONF_LEFT_BUTTON])
- cg.add(listener.set_left_button(b_sensor))
+ cg.add(listener.add_button(b_sensor, lv_key_t.LV_KEY_LEFT))
b_sensor = await cg.get_variable(sensor_config[CONF_RIGHT_BUTTON])
- cg.add(listener.set_right_button(b_sensor))
+ cg.add(listener.add_button(b_sensor, lv_key_t.LV_KEY_RIGHT))
else:
sensor_config = await cg.get_variable(sensor_config)
lv_add(listener.set_sensor(sensor_config))
b_sensor = await cg.get_variable(enc_conf[CONF_ENTER_BUTTON])
- cg.add(listener.set_enter_button(b_sensor))
+ cg.add(listener.add_button(b_sensor, lv_key_t.LV_KEY_ENTER))
if group := enc_conf.get(CONF_GROUP):
group = lv_Pvariable(lv_group_t, group)
lv_assign(group, lv_expr.group_create())
diff --git a/esphome/components/lvgl/keypads.py b/esphome/components/lvgl/keypads.py
new file mode 100644
index 0000000000..5e2953d57f
--- /dev/null
+++ b/esphome/components/lvgl/keypads.py
@@ -0,0 +1,77 @@
+import esphome.codegen as cg
+from esphome.components.binary_sensor import BinarySensor
+import esphome.config_validation as cv
+from esphome.const import CONF_GROUP, CONF_ID
+
+from .defines import (
+ CONF_ENCODERS,
+ CONF_INITIAL_FOCUS,
+ CONF_KEYPADS,
+ CONF_LONG_PRESS_REPEAT_TIME,
+ CONF_LONG_PRESS_TIME,
+ literal,
+)
+from .helpers import lvgl_components_required
+from .lvcode import lv, lv_assign, lv_expr, lv_Pvariable
+from .schemas import ENCODER_SCHEMA
+from .types import lv_group_t, lv_indev_type_t
+
+KEYPAD_KEYS = (
+ "up",
+ "down",
+ "right",
+ "left",
+ "esc",
+ "del",
+ "backspace",
+ "enter",
+ "next",
+ "prev",
+ "home",
+ "end",
+ "0",
+ "1",
+ "2",
+ "3",
+ "4",
+ "5",
+ "6",
+ "7",
+ "8",
+ "9",
+ "#",
+ "*",
+)
+
+KEYPADS_CONFIG = cv.ensure_list(
+ ENCODER_SCHEMA.extend(
+ {cv.Optional(key): cv.use_id(BinarySensor) for key in KEYPAD_KEYS}
+ )
+)
+
+
+async def keypads_to_code(var, config, default_group):
+ for enc_conf in config[CONF_KEYPADS]:
+ lvgl_components_required.add("KEY_LISTENER")
+ lpt = enc_conf[CONF_LONG_PRESS_TIME].total_milliseconds
+ lprt = enc_conf[CONF_LONG_PRESS_REPEAT_TIME].total_milliseconds
+ listener = cg.new_Pvariable(
+ enc_conf[CONF_ID], lv_indev_type_t.LV_INDEV_TYPE_KEYPAD, lpt, lprt
+ )
+ await cg.register_parented(listener, var)
+ for key in [x for x in enc_conf if x in KEYPAD_KEYS]:
+ b_sensor = await cg.get_variable(enc_conf[key])
+ cg.add(listener.add_button(b_sensor, literal(f"LV_KEY_{key.upper()}")))
+ if group := enc_conf.get(CONF_GROUP):
+ group = lv_Pvariable(lv_group_t, group)
+ lv_assign(group, lv_expr.group_create())
+ else:
+ group = default_group
+ lv.indev_set_group(lv_expr.indev_drv_register(listener.get_drv()), group)
+
+
+async def initial_focus_to_code(config):
+ for enc_conf in config[CONF_ENCODERS]:
+ if default_focus := enc_conf.get(CONF_INITIAL_FOCUS):
+ obj = await cg.get_variable(default_focus)
+ lv.group_focus_obj(obj)
diff --git a/esphome/components/lvgl/light/__init__.py b/esphome/components/lvgl/light/__init__.py
index a0eeded349..dcdf67a520 100644
--- a/esphome/components/lvgl/light/__init__.py
+++ b/esphome/components/lvgl/light/__init__.py
@@ -2,11 +2,10 @@ import esphome.codegen as cg
from esphome.components import light
from esphome.components.light import LightOutput
import esphome.config_validation as cv
-from esphome.const import CONF_GAMMA_CORRECT, CONF_LED, CONF_OUTPUT_ID
+from esphome.const import CONF_GAMMA_CORRECT, CONF_OUTPUT_ID
-from ..defines import CONF_LVGL_ID
+from ..defines import CONF_WIDGET
from ..lvcode import LvContext
-from ..schemas import LVGL_SCHEMA
from ..types import LvType, lvgl_ns
from ..widgets import get_widgets, wait_for_widgets
@@ -15,19 +14,18 @@ LVLight = lvgl_ns.class_("LVLight", LightOutput)
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
{
cv.Optional(CONF_GAMMA_CORRECT, default=0.0): cv.positive_float,
- cv.Required(CONF_LED): cv.use_id(lv_led_t),
+ cv.Required(CONF_WIDGET): cv.use_id(lv_led_t),
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(LVLight),
}
-).extend(LVGL_SCHEMA)
+)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
await light.register_light(var, config)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
- widget = await get_widgets(config, CONF_LED)
+ widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
- async with LvContext(paren) as ctx:
+ async with LvContext() as ctx:
ctx.add(var.set_obj(widget.obj))
diff --git a/esphome/components/lvgl/lv_validation.py b/esphome/components/lvgl/lv_validation.py
index fd840cc417..b91b0905df 100644
--- a/esphome/components/lvgl/lv_validation.py
+++ b/esphome/components/lvgl/lv_validation.py
@@ -267,6 +267,9 @@ def angle(value):
return int(cv.float_range(0.0, 360.0)(cv.angle(value)) * 10)
+lv_angle = LValidator(angle, uint32)
+
+
@schema_extractor("one_of")
def size_validator(value):
"""A size in one axis - one of "size_content", a number (pixels) or a percentage"""
@@ -274,10 +277,8 @@ def size_validator(value):
return ["SIZE_CONTENT", "number of pixels", "percentage"]
if isinstance(value, str) and value.lower().endswith("px"):
value = cv.int_(value[:-2])
- if isinstance(value, str) and not value.endswith("%"):
- if value.upper() == "SIZE_CONTENT":
- return "LV_SIZE_CONTENT"
- raise cv.Invalid("must be 'size_content', a percentage or an integer (pixels)")
+ if isinstance(value, str) and value.upper() == "SIZE_CONTENT":
+ return "LV_SIZE_CONTENT"
return pixels_or_percent_validator(value)
@@ -403,6 +404,7 @@ class TextValidator(LValidator):
lv_text = TextValidator()
lv_float = LValidator(cv.float_, cg.float_)
lv_int = LValidator(cv.int_, cg.int_)
+lv_positive_int = LValidator(cv.positive_int, cg.int_)
lv_brightness = LValidator(cv.percentage, cg.float_, retmapper=lambda x: int(x * 255))
diff --git a/esphome/components/lvgl/lvcode.py b/esphome/components/lvgl/lvcode.py
index 37d6670b84..6b98cc4251 100644
--- a/esphome/components/lvgl/lvcode.py
+++ b/esphome/components/lvgl/lvcode.py
@@ -178,10 +178,9 @@ class LvContext(LambdaContext):
added_lambda_count = 0
- def __init__(self, lv_component, args=None):
+ def __init__(self, args=None):
self.args = args or LVGL_COMP_ARG
super().__init__(parameters=self.args)
- self.lv_component = lv_component
async def __aexit__(self, exc_type, exc_val, exc_tb):
await super().__aexit__(exc_type, exc_val, exc_tb)
@@ -298,6 +297,7 @@ lv_expr = LvExpr("lv_")
lv_obj = MockLv("lv_obj_")
# Operations on the LVGL component
lvgl_comp = MockObj(LVGL_COMP, "->")
+lvgl_static = MockObj("LvglComponent", "::")
# equivalent to cg.add() for the current code context
diff --git a/esphome/components/lvgl/lvgl_esphome.cpp b/esphome/components/lvgl/lvgl_esphome.cpp
index 413b039af0..41346bc732 100644
--- a/esphome/components/lvgl/lvgl_esphome.cpp
+++ b/esphome/components/lvgl/lvgl_esphome.cpp
@@ -84,6 +84,7 @@ lv_event_code_t lv_api_event; // NOLINT
lv_event_code_t lv_update_event; // NOLINT
void LvglComponent::dump_config() {
ESP_LOGCONFIG(TAG, "LVGL:");
+ ESP_LOGCONFIG(TAG, " Display width/height: %d x %d", this->disp_drv_.hor_res, this->disp_drv_.ver_res);
ESP_LOGCONFIG(TAG, " Rotation: %d", this->rotation);
ESP_LOGCONFIG(TAG, " Draw rounding: %d", (int) this->draw_rounding);
}
@@ -97,19 +98,24 @@ void LvglComponent::set_paused(bool paused, bool show_snow) {
this->pause_callbacks_.call(paused);
}
+void LvglComponent::esphome_lvgl_init() {
+ lv_init();
+ lv_update_event = static_cast(lv_event_register_id());
+ lv_api_event = static_cast(lv_event_register_id());
+}
void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event) {
- lv_obj_add_event_cb(obj, callback, event, this);
+ lv_obj_add_event_cb(obj, callback, event, nullptr);
}
void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1,
lv_event_code_t event2) {
- this->add_event_cb(obj, callback, event1);
- this->add_event_cb(obj, callback, event2);
+ add_event_cb(obj, callback, event1);
+ add_event_cb(obj, callback, event2);
}
void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1,
lv_event_code_t event2, lv_event_code_t event3) {
- this->add_event_cb(obj, callback, event1);
- this->add_event_cb(obj, callback, event2);
- this->add_event_cb(obj, callback, event3);
+ add_event_cb(obj, callback, event1);
+ add_event_cb(obj, callback, event2);
+ add_event_cb(obj, callback, event3);
}
void LvglComponent::add_page(LvPageType *page) {
this->pages_.push_back(page);
@@ -145,7 +151,7 @@ void LvglComponent::draw_buffer_(const lv_area_t *area, lv_color_t *ptr) {
lv_color_t *dst = this->rotate_buf_;
switch (this->rotation) {
case display::DISPLAY_ROTATION_90_DEGREES:
- for (lv_coord_t x = height - 1; x-- != 0;) {
+ for (lv_coord_t x = height; x-- != 0;) {
for (lv_coord_t y = 0; y != width; y++) {
dst[y * height + x] = *ptr++;
}
@@ -217,8 +223,10 @@ PauseTrigger::PauseTrigger(LvglComponent *parent, TemplatableValue paused)
}
#ifdef USE_LVGL_TOUCHSCREEN
-LVTouchListener::LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time) {
+LVTouchListener::LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time, LvglComponent *parent) {
+ this->set_parent(parent);
lv_indev_drv_init(&this->drv_);
+ this->drv_.disp = parent->get_disp();
this->drv_.long_press_repeat_time = long_press_repeat_time;
this->drv_.long_press_time = long_press_time;
this->drv_.type = LV_INDEV_TYPE_POINTER;
@@ -234,6 +242,7 @@ LVTouchListener::LVTouchListener(uint16_t long_press_time, uint16_t long_press_r
}
};
}
+
void LVTouchListener::update(const touchscreen::TouchPoints_t &tpoints) {
this->touch_pressed_ = !this->parent_->is_paused() && !tpoints.empty();
if (this->touch_pressed_)
@@ -404,9 +413,6 @@ LvglComponent::LvglComponent(std::vector displays, float buf
buffer_frac_(buffer_frac),
full_refresh_(full_refresh),
resume_on_input_(resume_on_input) {
- lv_init();
- lv_update_event = static_cast(lv_event_register_id());
- lv_api_event = static_cast(lv_event_register_id());
auto *display = this->displays_[0];
size_t buffer_pixels = display->get_width() * display->get_height() / this->buffer_frac_;
auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8;
@@ -426,19 +432,8 @@ LvglComponent::LvglComponent(std::vector displays, float buf
this->disp_drv_.full_refresh = this->full_refresh_;
this->disp_drv_.flush_cb = static_flush_cb;
this->disp_drv_.rounder_cb = rounder_cb;
- // reset the display rotation since we will handle all rotations
- display->set_rotation(display::DISPLAY_ROTATION_0_DEGREES);
- switch (this->rotation) {
- default:
- this->disp_drv_.hor_res = (lv_coord_t) display->get_width();
- this->disp_drv_.ver_res = (lv_coord_t) display->get_height();
- break;
- case display::DISPLAY_ROTATION_90_DEGREES:
- case display::DISPLAY_ROTATION_270_DEGREES:
- this->disp_drv_.ver_res = (lv_coord_t) display->get_width();
- this->disp_drv_.hor_res = (lv_coord_t) display->get_height();
- break;
- }
+ this->disp_drv_.hor_res = (lv_coord_t) display->get_width();
+ this->disp_drv_.ver_res = (lv_coord_t) display->get_height();
this->disp_ = lv_disp_drv_register(&this->disp_drv_);
}
@@ -459,6 +454,9 @@ void LvglComponent::setup() {
esp_log_printf_(LVGL_LOG_LEVEL, TAG, 0, "%.*s", (int) strlen(buf) - 1, buf);
});
#endif
+ // Rotation will be handled by our drawing function, so reset the display rotation.
+ for (auto *display : this->displays_)
+ display->set_rotation(display::DISPLAY_ROTATION_0_DEGREES);
this->show_page(0, LV_SCR_LOAD_ANIM_NONE, 0);
lv_disp_trig_activity(this->disp_);
ESP_LOGCONFIG(TAG, "LVGL Setup complete");
diff --git a/esphome/components/lvgl/lvgl_esphome.h b/esphome/components/lvgl/lvgl_esphome.h
index f357c4950c..208cb1cbd5 100644
--- a/esphome/components/lvgl/lvgl_esphome.h
+++ b/esphome/components/lvgl/lvgl_esphome.h
@@ -146,10 +146,14 @@ class LvglComponent : public PollingComponent {
}
}
- void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event);
- void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1, lv_event_code_t event2);
- void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1, lv_event_code_t event2,
- lv_event_code_t event3);
+ /**
+ * Initialize the LVGL library and register custom events.
+ */
+ static void esphome_lvgl_init();
+ static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event);
+ static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1, lv_event_code_t event2);
+ static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1, lv_event_code_t event2,
+ lv_event_code_t event3);
void add_page(LvPageType *page);
void show_page(size_t index, lv_scr_load_anim_t anim, uint32_t time);
void show_next_page(lv_scr_load_anim_t anim, uint32_t time);
@@ -231,7 +235,7 @@ template class LvglCondition : public Condition, public P
#ifdef USE_LVGL_TOUCHSCREEN
class LVTouchListener : public touchscreen::TouchListener, public Parented {
public:
- LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time);
+ LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time, LvglComponent *parent);
void update(const touchscreen::TouchPoints_t &tpoints) override;
void release() override {
touch_pressed_ = false;
@@ -252,15 +256,8 @@ class LVEncoderListener : public Parented {
LVEncoderListener(lv_indev_type_t type, uint16_t lpt, uint16_t lprt);
#ifdef USE_BINARY_SENSOR
- void set_left_button(binary_sensor::BinarySensor *left_button) {
- left_button->add_on_state_callback([this](bool state) { this->event(LV_KEY_LEFT, state); });
- }
- void set_right_button(binary_sensor::BinarySensor *right_button) {
- right_button->add_on_state_callback([this](bool state) { this->event(LV_KEY_RIGHT, state); });
- }
-
- void set_enter_button(binary_sensor::BinarySensor *enter_button) {
- enter_button->add_on_state_callback([this](bool state) { this->event(LV_KEY_ENTER, state); });
+ void add_button(binary_sensor::BinarySensor *button, lv_key_t key) {
+ button->add_on_state_callback([this, key](bool state) { this->event(key, state); });
}
#endif
diff --git a/esphome/components/lvgl/lvgl_proxy.h b/esphome/components/lvgl/lvgl_proxy.h
new file mode 100644
index 0000000000..0ccd80e541
--- /dev/null
+++ b/esphome/components/lvgl/lvgl_proxy.h
@@ -0,0 +1,17 @@
+#pragma once
+/**
+* This header is for use in components that might or might not use LVGL. There is a platformio bug where
+the mere mention of a header file, even if ifdefed, causes the build to fail. This is a workaround, since if this
+file is included in the build, LVGL is always included.
+*/
+#ifdef USE_LVGL
+// required for clang-tidy
+#ifndef LV_CONF_H
+#define LV_CONF_SKIP 1 // NOLINT
+#endif // LV_CONF_H
+
+#include
+namespace esphome {
+namespace lvgl {} // namespace lvgl
+} // namespace esphome
+#endif // USE_LVGL
diff --git a/esphome/components/lvgl/number/__init__.py b/esphome/components/lvgl/number/__init__.py
index 07f92635b5..b41a36bc0f 100644
--- a/esphome/components/lvgl/number/__init__.py
+++ b/esphome/components/lvgl/number/__init__.py
@@ -3,7 +3,7 @@ from esphome.components import number
import esphome.config_validation as cv
from esphome.cpp_generator import MockObj
-from ..defines import CONF_ANIMATED, CONF_LVGL_ID, CONF_UPDATE_ON_RELEASE, CONF_WIDGET
+from ..defines import CONF_ANIMATED, CONF_UPDATE_ON_RELEASE, CONF_WIDGET
from ..lv_validation import animated
from ..lvcode import (
API_EVENT,
@@ -13,28 +13,23 @@ from ..lvcode import (
LvContext,
lv,
lv_add,
+ lvgl_static,
)
-from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvNumber, lvgl_ns
from ..widgets import get_widgets, wait_for_widgets
LVGLNumber = lvgl_ns.class_("LVGLNumber", number.Number)
-CONFIG_SCHEMA = (
- number.number_schema(LVGLNumber)
- .extend(LVGL_SCHEMA)
- .extend(
- {
- cv.Required(CONF_WIDGET): cv.use_id(LvNumber),
- cv.Optional(CONF_ANIMATED, default=True): animated,
- cv.Optional(CONF_UPDATE_ON_RELEASE, default=False): cv.boolean,
- }
- )
+CONFIG_SCHEMA = number.number_schema(LVGLNumber).extend(
+ {
+ cv.Required(CONF_WIDGET): cv.use_id(LvNumber),
+ cv.Optional(CONF_ANIMATED, default=True): animated,
+ cv.Optional(CONF_UPDATE_ON_RELEASE, default=False): cv.boolean,
+ }
)
async def to_code(config):
- paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
var = await number.new_number(
@@ -58,10 +53,10 @@ async def to_code(config):
if not config[CONF_UPDATE_ON_RELEASE]
else LV_EVENT.RELEASED
)
- async with LvContext(paren):
+ async with LvContext():
lv_add(var.set_control_lambda(await control.get_lambda()))
lv_add(
- paren.add_event_cb(
+ lvgl_static.add_event_cb(
widget.obj, await event.get_lambda(), UPDATE_EVENT, event_code
)
)
diff --git a/esphome/components/lvgl/schemas.py b/esphome/components/lvgl/schemas.py
index 7599d64416..516627708e 100644
--- a/esphome/components/lvgl/schemas.py
+++ b/esphome/components/lvgl/schemas.py
@@ -91,7 +91,7 @@ STYLE_PROPS = {
"arc_opa": lvalid.opacity,
"arc_color": lvalid.lv_color,
"arc_rounded": lvalid.lv_bool,
- "arc_width": cv.positive_int,
+ "arc_width": lvalid.lv_positive_int,
"anim_time": lvalid.lv_milliseconds,
"bg_color": lvalid.lv_color,
"bg_grad": lv_gradient,
@@ -111,7 +111,7 @@ STYLE_PROPS = {
"border_side": df.LvConstant(
"LV_BORDER_SIDE_", "NONE", "TOP", "BOTTOM", "LEFT", "RIGHT", "INTERNAL"
).several_of,
- "border_width": cv.positive_int,
+ "border_width": lvalid.lv_positive_int,
"clip_corner": lvalid.lv_bool,
"color_filter_opa": lvalid.opacity,
"height": lvalid.size,
@@ -134,11 +134,11 @@ STYLE_PROPS = {
"pad_right": lvalid.pixels,
"pad_top": lvalid.pixels,
"shadow_color": lvalid.lv_color,
- "shadow_ofs_x": cv.int_,
- "shadow_ofs_y": cv.int_,
+ "shadow_ofs_x": lvalid.lv_int,
+ "shadow_ofs_y": lvalid.lv_int,
"shadow_opa": lvalid.opacity,
- "shadow_spread": cv.int_,
- "shadow_width": cv.positive_int,
+ "shadow_spread": lvalid.lv_int,
+ "shadow_width": lvalid.lv_positive_int,
"text_align": df.LvConstant(
"LV_TEXT_ALIGN_", "LEFT", "CENTER", "RIGHT", "AUTO"
).one_of,
@@ -150,7 +150,7 @@ STYLE_PROPS = {
"text_letter_space": cv.positive_int,
"text_line_space": cv.positive_int,
"text_opa": lvalid.opacity,
- "transform_angle": lvalid.angle,
+ "transform_angle": lvalid.lv_angle,
"transform_height": lvalid.pixels_or_percent,
"transform_pivot_x": lvalid.pixels_or_percent,
"transform_pivot_y": lvalid.pixels_or_percent,
@@ -391,7 +391,9 @@ def container_validator(schema, widget_type: WidgetType):
add_lv_use(ltype)
if value == SCHEMA_EXTRACT:
return result
- result = result.extend(LAYOUT_SCHEMAS[ltype.lower()])
+ result = result.extend(
+ LAYOUT_SCHEMAS.get(ltype.lower(), LAYOUT_SCHEMAS[df.TYPE_NONE])
+ )
return result(value)
return validator
diff --git a/esphome/components/lvgl/select/__init__.py b/esphome/components/lvgl/select/__init__.py
index 5e50b6b385..bd5ef8f237 100644
--- a/esphome/components/lvgl/select/__init__.py
+++ b/esphome/components/lvgl/select/__init__.py
@@ -1,25 +1,19 @@
-import esphome.codegen as cg
from esphome.components import select
import esphome.config_validation as cv
from esphome.const import CONF_OPTIONS
-from ..defines import CONF_ANIMATED, CONF_LVGL_ID, CONF_WIDGET, literal
+from ..defines import CONF_ANIMATED, CONF_WIDGET, literal
from ..lvcode import LvContext
-from ..schemas import LVGL_SCHEMA
from ..types import LvSelect, lvgl_ns
from ..widgets import get_widgets, wait_for_widgets
LVGLSelect = lvgl_ns.class_("LVGLSelect", select.Select)
-CONFIG_SCHEMA = (
- select.select_schema(LVGLSelect)
- .extend(LVGL_SCHEMA)
- .extend(
- {
- cv.Required(CONF_WIDGET): cv.use_id(LvSelect),
- cv.Optional(CONF_ANIMATED, default=False): cv.boolean,
- }
- )
+CONFIG_SCHEMA = select.select_schema(LVGLSelect).extend(
+ {
+ cv.Required(CONF_WIDGET): cv.use_id(LvSelect),
+ cv.Optional(CONF_ANIMATED, default=False): cv.boolean,
+ }
)
@@ -28,9 +22,8 @@ async def to_code(config):
widget = widget[0]
options = widget.config.get(CONF_OPTIONS, [])
selector = await select.new_select(config, options=options)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
await wait_for_widgets()
- async with LvContext(paren) as ctx:
+ async with LvContext() as ctx:
ctx.add(
selector.set_widget(
widget.var,
diff --git a/esphome/components/lvgl/sensor/__init__.py b/esphome/components/lvgl/sensor/__init__.py
index a2a2298c27..03b2638ed0 100644
--- a/esphome/components/lvgl/sensor/__init__.py
+++ b/esphome/components/lvgl/sensor/__init__.py
@@ -1,8 +1,7 @@
-import esphome.codegen as cg
from esphome.components.sensor import Sensor, new_sensor, sensor_schema
import esphome.config_validation as cv
-from ..defines import CONF_LVGL_ID, CONF_WIDGET
+from ..defines import CONF_WIDGET
from ..lvcode import (
API_EVENT,
EVENT_ARG,
@@ -11,34 +10,29 @@ from ..lvcode import (
LambdaContext,
LvContext,
lv_add,
+ lvgl_static,
)
-from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvNumber
from ..widgets import Widget, get_widgets, wait_for_widgets
-CONFIG_SCHEMA = (
- sensor_schema(Sensor)
- .extend(LVGL_SCHEMA)
- .extend(
- {
- cv.Required(CONF_WIDGET): cv.use_id(LvNumber),
- }
- )
+CONFIG_SCHEMA = sensor_schema(Sensor).extend(
+ {
+ cv.Required(CONF_WIDGET): cv.use_id(LvNumber),
+ }
)
async def to_code(config):
sensor = await new_sensor(config)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
assert isinstance(widget, Widget)
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as lamb:
lv_add(sensor.publish_state(widget.get_value()))
- async with LvContext(paren, LVGL_COMP_ARG):
+ async with LvContext(LVGL_COMP_ARG):
lv_add(
- paren.add_event_cb(
+ lvgl_static.add_event_cb(
widget.obj,
await lamb.get_lambda(),
LV_EVENT.VALUE_CHANGED,
diff --git a/esphome/components/lvgl/switch/__init__.py b/esphome/components/lvgl/switch/__init__.py
index 8c090543f9..4e1e7f72e0 100644
--- a/esphome/components/lvgl/switch/__init__.py
+++ b/esphome/components/lvgl/switch/__init__.py
@@ -3,7 +3,7 @@ from esphome.components.switch import Switch, new_switch, switch_schema
import esphome.config_validation as cv
from esphome.cpp_generator import MockObj
-from ..defines import CONF_LVGL_ID, CONF_WIDGET, literal
+from ..defines import CONF_WIDGET, literal
from ..lvcode import (
API_EVENT,
EVENT_ARG,
@@ -13,26 +13,21 @@ from ..lvcode import (
LvContext,
lv,
lv_add,
+ lvgl_static,
)
-from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LV_STATE, lv_pseudo_button_t, lvgl_ns
from ..widgets import get_widgets, wait_for_widgets
LVGLSwitch = lvgl_ns.class_("LVGLSwitch", Switch)
-CONFIG_SCHEMA = (
- switch_schema(LVGLSwitch)
- .extend(LVGL_SCHEMA)
- .extend(
- {
- cv.Required(CONF_WIDGET): cv.use_id(lv_pseudo_button_t),
- }
- )
+CONFIG_SCHEMA = switch_schema(LVGLSwitch).extend(
+ {
+ cv.Required(CONF_WIDGET): cv.use_id(lv_pseudo_button_t),
+ }
)
async def to_code(config):
switch = await new_switch(config)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
@@ -45,10 +40,10 @@ async def to_code(config):
widget.clear_state(LV_STATE.CHECKED)
lv.event_send(widget.obj, API_EVENT, cg.nullptr)
control.add(switch.publish_state(literal("v")))
- async with LvContext(paren) as ctx:
+ async with LvContext() as ctx:
lv_add(switch.set_control_lambda(await control.get_lambda()))
ctx.add(
- paren.add_event_cb(
+ lvgl_static.add_event_cb(
widget.obj,
await checked_ctx.get_lambda(),
LV_EVENT.VALUE_CHANGED,
diff --git a/esphome/components/lvgl/text/__init__.py b/esphome/components/lvgl/text/__init__.py
index a59e703591..89db139a6a 100644
--- a/esphome/components/lvgl/text/__init__.py
+++ b/esphome/components/lvgl/text/__init__.py
@@ -3,7 +3,7 @@ from esphome.components import text
from esphome.components.text import new_text
import esphome.config_validation as cv
-from ..defines import CONF_LVGL_ID, CONF_WIDGET
+from ..defines import CONF_WIDGET
from ..lvcode import (
API_EVENT,
EVENT_ARG,
@@ -12,14 +12,14 @@ from ..lvcode import (
LvContext,
lv,
lv_add,
+ lvgl_static,
)
-from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvText, lvgl_ns
from ..widgets import get_widgets, wait_for_widgets
LVGLText = lvgl_ns.class_("LVGLText", text.Text)
-CONFIG_SCHEMA = text.TEXT_SCHEMA.extend(LVGL_SCHEMA).extend(
+CONFIG_SCHEMA = text.TEXT_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(LVGLText),
cv.Required(CONF_WIDGET): cv.use_id(LvText),
@@ -29,7 +29,6 @@ CONFIG_SCHEMA = text.TEXT_SCHEMA.extend(LVGL_SCHEMA).extend(
async def to_code(config):
textvar = await new_text(config)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
@@ -39,10 +38,10 @@ async def to_code(config):
control.add(textvar.publish_state(widget.get_value()))
async with LambdaContext(EVENT_ARG) as lamb:
lv_add(textvar.publish_state(widget.get_value()))
- async with LvContext(paren):
+ async with LvContext():
lv_add(textvar.set_control_lambda(await control.get_lambda()))
lv_add(
- paren.add_event_cb(
+ lvgl_static.add_event_cb(
widget.obj,
await lamb.get_lambda(),
LV_EVENT.VALUE_CHANGED,
diff --git a/esphome/components/lvgl/text_sensor/__init__.py b/esphome/components/lvgl/text_sensor/__init__.py
index ae39eec291..4728fd137a 100644
--- a/esphome/components/lvgl/text_sensor/__init__.py
+++ b/esphome/components/lvgl/text_sensor/__init__.py
@@ -1,4 +1,3 @@
-import esphome.codegen as cg
from esphome.components.text_sensor import (
TextSensor,
new_text_sensor,
@@ -6,34 +5,35 @@ from esphome.components.text_sensor import (
)
import esphome.config_validation as cv
-from ..defines import CONF_LVGL_ID, CONF_WIDGET
-from ..lvcode import API_EVENT, EVENT_ARG, UPDATE_EVENT, LambdaContext, LvContext
-from ..schemas import LVGL_SCHEMA
+from ..defines import CONF_WIDGET
+from ..lvcode import (
+ API_EVENT,
+ EVENT_ARG,
+ UPDATE_EVENT,
+ LambdaContext,
+ LvContext,
+ lvgl_static,
+)
from ..types import LV_EVENT, LvText
from ..widgets import get_widgets, wait_for_widgets
-CONFIG_SCHEMA = (
- text_sensor_schema(TextSensor)
- .extend(LVGL_SCHEMA)
- .extend(
- {
- cv.Required(CONF_WIDGET): cv.use_id(LvText),
- }
- )
+CONFIG_SCHEMA = text_sensor_schema(TextSensor).extend(
+ {
+ cv.Required(CONF_WIDGET): cv.use_id(LvText),
+ }
)
async def to_code(config):
sensor = await new_text_sensor(config)
- paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as pressed_ctx:
pressed_ctx.add(sensor.publish_state(widget.get_value()))
- async with LvContext(paren) as ctx:
+ async with LvContext() as ctx:
ctx.add(
- paren.add_event_cb(
+ lvgl_static.add_event_cb(
widget.obj,
await pressed_ctx.get_lambda(),
LV_EVENT.VALUE_CHANGED,
diff --git a/esphome/components/lvgl/touchscreens.py b/esphome/components/lvgl/touchscreens.py
index 4d430a428e..f2dd013f6d 100644
--- a/esphome/components/lvgl/touchscreens.py
+++ b/esphome/components/lvgl/touchscreens.py
@@ -33,13 +33,12 @@ def touchscreen_schema(config):
return [TOUCHSCREENS_CONFIG(config)]
-async def touchscreens_to_code(var, config):
+async def touchscreens_to_code(lv_component, config):
for tconf in config[CONF_TOUCHSCREENS]:
lvgl_components_required.add(CONF_TOUCHSCREEN)
touchscreen = await cg.get_variable(tconf[CONF_TOUCHSCREEN_ID])
lpt = tconf[CONF_LONG_PRESS_TIME].total_milliseconds
lprt = tconf[CONF_LONG_PRESS_REPEAT_TIME].total_milliseconds
- listener = cg.new_Pvariable(tconf[CONF_ID], lpt, lprt)
- await cg.register_parented(listener, var)
+ listener = cg.new_Pvariable(tconf[CONF_ID], lpt, lprt, lv_component)
lv.indev_drv_register(listener.get_drv())
cg.add(touchscreen.register_listener(listener))
diff --git a/esphome/components/lvgl/trigger.py b/esphome/components/lvgl/trigger.py
index eb6e483203..fb856df04e 100644
--- a/esphome/components/lvgl/trigger.py
+++ b/esphome/components/lvgl/trigger.py
@@ -20,17 +20,16 @@ from .lvcode import (
lv,
lv_add,
lv_event_t_ptr,
+ lvgl_static,
)
from .types import LV_EVENT
from .widgets import widget_map
-async def generate_triggers(lv_component):
+async def generate_triggers():
"""
Generate LVGL triggers for all defined widgets
Must be done after all widgets completed
- :param lv_component: The parent component
- :return:
"""
for w in widget_map.values():
@@ -43,11 +42,10 @@ async def generate_triggers(lv_component):
conf = conf[0]
w.add_flag("LV_OBJ_FLAG_CLICKABLE")
event = literal("LV_EVENT_" + LV_EVENT_MAP[event[3:].upper()])
- await add_trigger(conf, lv_component, w, event)
+ await add_trigger(conf, w, event)
for conf in w.config.get(CONF_ON_VALUE, ()):
await add_trigger(
conf,
- lv_component,
w,
LV_EVENT.VALUE_CHANGED,
API_EVENT,
@@ -63,7 +61,7 @@ async def generate_triggers(lv_component):
lv.obj_align_to(w.obj, target, align, x, y)
-async def add_trigger(conf, lv_component, w, *events):
+async def add_trigger(conf, w, *events):
tid = conf[CONF_TRIGGER_ID]
trigger = cg.new_Pvariable(tid)
args = w.get_args() + [(lv_event_t_ptr, "event")]
@@ -72,4 +70,4 @@ async def add_trigger(conf, lv_component, w, *events):
async with LambdaContext(EVENT_ARG, where=tid) as context:
with LvConditional(w.is_selected()):
lv_add(trigger.trigger(*value, literal("event")))
- lv_add(lv_component.add_event_cb(w.obj, await context.get_lambda(), *events))
+ lv_add(lvgl_static.add_event_cb(w.obj, await context.get_lambda(), *events))
diff --git a/esphome/components/lvgl/types.py b/esphome/components/lvgl/types.py
index b504f24674..40e69119f0 100644
--- a/esphome/components/lvgl/types.py
+++ b/esphome/components/lvgl/types.py
@@ -40,6 +40,7 @@ void_ptr = cg.void.operator("ptr")
lv_coord_t = cg.global_ns.namespace("lv_coord_t")
lv_event_code_t = cg.global_ns.enum("lv_event_code_t")
lv_indev_type_t = cg.global_ns.enum("lv_indev_type_t")
+lv_key_t = cg.global_ns.enum("lv_key_t")
FontEngine = lvgl_ns.class_("FontEngine")
IdleTrigger = lvgl_ns.class_("IdleTrigger", automation.Trigger.template())
PauseTrigger = lvgl_ns.class_("PauseTrigger", automation.Trigger.template())
diff --git a/esphome/components/lvgl/widgets/animimg.py b/esphome/components/lvgl/widgets/animimg.py
index 3b20008c3d..8adea72ad3 100644
--- a/esphome/components/lvgl/widgets/animimg.py
+++ b/esphome/components/lvgl/widgets/animimg.py
@@ -60,9 +60,10 @@ class AnimimgType(WidgetType):
lvgl_components_required.add(CONF_IMAGE)
lvgl_components_required.add(CONF_ANIMIMG)
if CONF_SRC in config:
- for x in config[CONF_SRC]:
- await cg.get_variable(x)
- srcs = [await lv_image.process(x) for x in config[CONF_SRC]]
+ srcs = [
+ await lv_image.process(await cg.get_variable(x))
+ for x in config[CONF_SRC]
+ ]
src_id = cg.static_const_array(config[CONF_SRC_LIST_ID], srcs)
count = len(config[CONF_SRC])
lv.animimg_set_src(w.obj, src_id, count)
diff --git a/esphome/components/lvgl/widgets/img.py b/esphome/components/lvgl/widgets/img.py
index 59b2c97c63..931d0c0b5b 100644
--- a/esphome/components/lvgl/widgets/img.py
+++ b/esphome/components/lvgl/widgets/img.py
@@ -1,3 +1,4 @@
+import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ANGLE, CONF_MODE
@@ -64,6 +65,7 @@ class ImgType(WidgetType):
async def to_code(self, w: Widget, config):
if src := config.get(CONF_SRC):
+ src = await cg.get_variable(src)
lv.img_set_src(w.obj, await lv_image.process(src))
if (cf_angle := config.get(CONF_ANGLE)) is not None:
pivot_x = config[CONF_PIVOT_X]
diff --git a/esphome/components/lvgl/widgets/page.py b/esphome/components/lvgl/widgets/page.py
index 0e84ab6791..a754a9cb9a 100644
--- a/esphome/components/lvgl/widgets/page.py
+++ b/esphome/components/lvgl/widgets/page.py
@@ -20,6 +20,7 @@ from ..lvcode import (
add_line_marks,
lv_add,
lvgl_comp,
+ lvgl_static,
)
from ..schemas import LVGL_SCHEMA
from ..types import LvglAction, lv_page_t
@@ -139,7 +140,7 @@ async def add_pages(lv_component, config):
await add_widgets(page, pconf)
-async def generate_page_triggers(lv_component, config):
+async def generate_page_triggers(config):
for pconf in config.get(CONF_PAGES, ()):
page = (await get_widgets(pconf))[0]
for ev in (CONF_ON_LOAD, CONF_ON_UNLOAD):
@@ -149,7 +150,7 @@ async def generate_page_triggers(lv_component, config):
async with LambdaContext(EVENT_ARG, where=id) as context:
lv_add(trigger.trigger())
lv_add(
- lv_component.add_event_cb(
+ lvgl_static.add_event_cb(
page.obj,
await context.get_lambda(),
literal(f"LV_EVENT_SCREEN_{ev[3:].upper()}_START"),
diff --git a/esphome/components/lvgl/widgets/qrcode.py b/esphome/components/lvgl/widgets/qrcode.py
new file mode 100644
index 0000000000..742b538938
--- /dev/null
+++ b/esphome/components/lvgl/widgets/qrcode.py
@@ -0,0 +1,54 @@
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from esphome.const import CONF_SIZE, CONF_TEXT
+from esphome.cpp_generator import MockObjClass
+
+from ..defines import CONF_MAIN, literal
+from ..lv_validation import color, color_retmapper, lv_text
+from ..lvcode import LocalVariable, lv, lv_expr
+from ..schemas import TEXT_SCHEMA
+from ..types import WidgetType, lv_obj_t
+from . import Widget
+
+CONF_QRCODE = "qrcode"
+CONF_DARK_COLOR = "dark_color"
+CONF_LIGHT_COLOR = "light_color"
+
+QRCODE_SCHEMA = TEXT_SCHEMA.extend(
+ {
+ cv.Optional(CONF_DARK_COLOR, default="black"): color,
+ cv.Optional(CONF_LIGHT_COLOR, default="white"): color,
+ cv.Required(CONF_SIZE): cv.int_,
+ }
+)
+
+
+class QrCodeType(WidgetType):
+ def __init__(self):
+ super().__init__(
+ CONF_QRCODE,
+ lv_obj_t,
+ (CONF_MAIN,),
+ QRCODE_SCHEMA,
+ modify_schema=TEXT_SCHEMA,
+ )
+
+ def get_uses(self):
+ return ("canvas", "img")
+
+ def obj_creator(self, parent: MockObjClass, config: dict):
+ dark_color = color_retmapper(config[CONF_DARK_COLOR])
+ light_color = color_retmapper(config[CONF_LIGHT_COLOR])
+ size = config[CONF_SIZE]
+ return lv_expr.call("qrcode_create", parent, size, dark_color, light_color)
+
+ async def to_code(self, w: Widget, config):
+ if (value := config.get(CONF_TEXT)) is not None:
+ value = await lv_text.process(value)
+ with LocalVariable(
+ "qr_text", cg.const_char_ptr, value, modifier=""
+ ) as str_obj:
+ lv.qrcode_update(w.obj, str_obj, literal(f"strlen({str_obj})"))
+
+
+qr_code_spec = QrCodeType()
diff --git a/esphome/components/media_player/__init__.py b/esphome/components/media_player/__init__.py
index 423cb065dc..a46b30db29 100644
--- a/esphome/components/media_player/__init__.py
+++ b/esphome/components/media_player/__init__.py
@@ -21,6 +21,7 @@ media_player_ns = cg.esphome_ns.namespace("media_player")
MediaPlayer = media_player_ns.class_("MediaPlayer")
+
PlayAction = media_player_ns.class_(
"PlayAction", automation.Action, cg.Parented.template(MediaPlayer)
)
@@ -60,7 +61,11 @@ AnnoucementTrigger = media_player_ns.class_(
"AnnouncementTrigger", automation.Trigger.template()
)
IsIdleCondition = media_player_ns.class_("IsIdleCondition", automation.Condition)
+IsPausedCondition = media_player_ns.class_("IsPausedCondition", automation.Condition)
IsPlayingCondition = media_player_ns.class_("IsPlayingCondition", automation.Condition)
+IsAnnouncingCondition = media_player_ns.class_(
+ "IsAnnouncingCondition", automation.Condition
+)
async def setup_media_player_core_(var, config):
@@ -159,9 +164,15 @@ async def media_player_play_media_action(config, action_id, template_arg, args):
@automation.register_condition(
"media_player.is_idle", IsIdleCondition, MEDIA_PLAYER_ACTION_SCHEMA
)
+@automation.register_condition(
+ "media_player.is_paused", IsPausedCondition, MEDIA_PLAYER_ACTION_SCHEMA
+)
@automation.register_condition(
"media_player.is_playing", IsPlayingCondition, MEDIA_PLAYER_ACTION_SCHEMA
)
+@automation.register_condition(
+ "media_player.is_announcing", IsAnnouncingCondition, MEDIA_PLAYER_ACTION_SCHEMA
+)
async def media_player_action(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
diff --git a/esphome/components/media_player/automation.h b/esphome/components/media_player/automation.h
index f0e0a5dd31..7b9220c4a5 100644
--- a/esphome/components/media_player/automation.h
+++ b/esphome/components/media_player/automation.h
@@ -68,5 +68,15 @@ template class IsPlayingCondition : public Condition, pub
bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
};
+template class IsPausedCondition : public Condition, public Parented {
+ public:
+ bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
+};
+
+template class IsAnnouncingCondition : public Condition, public Parented {
+ public:
+ bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; }
+};
+
} // namespace media_player
} // namespace esphome
diff --git a/esphome/components/media_player/media_player.cpp b/esphome/components/media_player/media_player.cpp
index 586345ac9f..b5190d8573 100644
--- a/esphome/components/media_player/media_player.cpp
+++ b/esphome/components/media_player/media_player.cpp
@@ -37,6 +37,10 @@ const char *media_player_command_to_string(MediaPlayerCommand command) {
return "UNMUTE";
case MEDIA_PLAYER_COMMAND_TOGGLE:
return "TOGGLE";
+ case MEDIA_PLAYER_COMMAND_VOLUME_UP:
+ return "VOLUME_UP";
+ case MEDIA_PLAYER_COMMAND_VOLUME_DOWN:
+ return "VOLUME_DOWN";
default:
return "UNKNOWN";
}
diff --git a/esphome/components/midea/air_conditioner.cpp b/esphome/components/midea/air_conditioner.cpp
index b5bf43b64f..a823680d03 100644
--- a/esphome/components/midea/air_conditioner.cpp
+++ b/esphome/components/midea/air_conditioner.cpp
@@ -3,6 +3,8 @@
#include "esphome/core/log.h"
#include "air_conditioner.h"
#include "ac_adapter.h"
+#include
+#include
namespace esphome {
namespace midea {
@@ -121,7 +123,21 @@ void AirConditioner::dump_config() {
void AirConditioner::do_follow_me(float temperature, bool beeper) {
#ifdef USE_REMOTE_TRANSMITTER
- IrFollowMeData data(static_cast(lroundf(temperature)), beeper);
+ // Check if temperature is finite (not NaN or infinite)
+ if (!std::isfinite(temperature)) {
+ ESP_LOGW(Constants::TAG, "Follow me action requires a finite temperature, got: %f", temperature);
+ return;
+ }
+
+ // Round and convert temperature to long, then clamp and convert it to uint8_t
+ uint8_t temp_uint8 =
+ static_cast(std::max(0L, std::min(static_cast(UINT8_MAX), std::lroundf(temperature))));
+
+ ESP_LOGD(Constants::TAG, "Follow me action called with temperature: %f °C, rounded to: %u °C", temperature,
+ temp_uint8);
+
+ // Create and transmit the data
+ IrFollowMeData data(temp_uint8, beeper);
this->transmitter_.transmit(data);
#else
ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
diff --git a/esphome/components/modbus/modbus.cpp b/esphome/components/modbus/modbus.cpp
index f8dd4c18b9..8544b50261 100644
--- a/esphome/components/modbus/modbus.cpp
+++ b/esphome/components/modbus/modbus.cpp
@@ -15,23 +15,33 @@ void Modbus::setup() {
void Modbus::loop() {
const uint32_t now = millis();
- if (now - this->last_modbus_byte_ > 50) {
- this->rx_buffer_.clear();
- this->last_modbus_byte_ = now;
- }
- // stop blocking new send commands after send_wait_time_ ms regardless if a response has been received since then
- if (now - this->last_send_ > send_wait_time_) {
- waiting_for_response = 0;
- }
-
while (this->available()) {
uint8_t byte;
this->read_byte(&byte);
if (this->parse_modbus_byte_(byte)) {
this->last_modbus_byte_ = now;
} else {
+ size_t at = this->rx_buffer_.size();
+ if (at > 0) {
+ ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse failed", at);
+ this->rx_buffer_.clear();
+ }
+ }
+ }
+
+ if (now - this->last_modbus_byte_ > 50) {
+ size_t at = this->rx_buffer_.size();
+ if (at > 0) {
+ ESP_LOGV(TAG, "Clearing buffer of %d bytes - timeout", at);
this->rx_buffer_.clear();
}
+
+ // stop blocking new send commands after sent_wait_time_ ms after response received
+ if (now - this->last_send_ > send_wait_time_) {
+ if (waiting_for_response > 0)
+ ESP_LOGV(TAG, "Stop waiting for response from %d", waiting_for_response);
+ waiting_for_response = 0;
+ }
}
}
@@ -39,7 +49,7 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
size_t at = this->rx_buffer_.size();
this->rx_buffer_.push_back(byte);
const uint8_t *raw = &this->rx_buffer_[0];
- ESP_LOGV(TAG, "Modbus received Byte %d (0X%x)", byte, byte);
+ ESP_LOGVV(TAG, "Modbus received Byte %d (0X%x)", byte, byte);
// Byte 0: modbus address (match all)
if (at == 0)
return true;
@@ -144,8 +154,10 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
ESP_LOGW(TAG, "Got Modbus frame from unknown address 0x%02X! ", address);
}
- // return false to reset buffer
- return false;
+ // reset buffer
+ ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse succeeded", at);
+ this->rx_buffer_.clear();
+ return true;
}
void Modbus::dump_config() {
diff --git a/esphome/components/modbus_controller/__init__.py b/esphome/components/modbus_controller/__init__.py
index 488baa245a..5c407d6fff 100644
--- a/esphome/components/modbus_controller/__init__.py
+++ b/esphome/components/modbus_controller/__init__.py
@@ -25,6 +25,8 @@ from .const import (
CONF_MODBUS_CONTROLLER_ID,
CONF_OFFLINE_SKIP_UPDATES,
CONF_ON_COMMAND_SENT,
+ CONF_ON_ONLINE,
+ CONF_ON_OFFLINE,
CONF_REGISTER_COUNT,
CONF_REGISTER_TYPE,
CONF_RESPONSE_SIZE,
@@ -114,6 +116,14 @@ ModbusCommandSentTrigger = modbus_controller_ns.class_(
"ModbusCommandSentTrigger", automation.Trigger.template(cg.int_, cg.int_)
)
+ModbusOnlineTrigger = modbus_controller_ns.class_(
+ "ModbusOnlineTrigger", automation.Trigger.template(cg.int_, cg.int_)
+)
+
+ModbusOfflineTrigger = modbus_controller_ns.class_(
+ "ModbusOfflineTrigger", automation.Trigger.template(cg.int_, cg.int_)
+)
+
_LOGGER = logging.getLogger(__name__)
ModbusServerRegisterSchema = cv.Schema(
@@ -146,6 +156,16 @@ CONFIG_SCHEMA = cv.All(
),
}
),
+ cv.Optional(CONF_ON_ONLINE): automation.validate_automation(
+ {
+ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ModbusOnlineTrigger),
+ }
+ ),
+ cv.Optional(CONF_ON_OFFLINE): automation.validate_automation(
+ {
+ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ModbusOnlineTrigger),
+ }
+ ),
}
)
.extend(cv.polling_component_schema("60s"))
@@ -284,7 +304,17 @@ async def to_code(config):
for conf in config.get(CONF_ON_COMMAND_SENT, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
- trigger, [(int, "function_code"), (int, "address")], conf
+ trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
+ )
+ for conf in config.get(CONF_ON_ONLINE, []):
+ trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
+ await automation.build_automation(
+ trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
+ )
+ for conf in config.get(CONF_ON_OFFLINE, []):
+ trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
+ await automation.build_automation(
+ trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
)
diff --git a/esphome/components/modbus_controller/automation.h b/esphome/components/modbus_controller/automation.h
index ad8de4b05d..b3338192cc 100644
--- a/esphome/components/modbus_controller/automation.h
+++ b/esphome/components/modbus_controller/automation.h
@@ -15,5 +15,21 @@ class ModbusCommandSentTrigger : public Trigger {
}
};
+class ModbusOnlineTrigger : public Trigger {
+ public:
+ ModbusOnlineTrigger(ModbusController *a_modbuscontroller) {
+ a_modbuscontroller->add_on_online_callback(
+ [this](int function_code, int address) { this->trigger(function_code, address); });
+ }
+};
+
+class ModbusOfflineTrigger : public Trigger {
+ public:
+ ModbusOfflineTrigger(ModbusController *a_modbuscontroller) {
+ a_modbuscontroller->add_on_offline_callback(
+ [this](int function_code, int address) { this->trigger(function_code, address); });
+ }
+};
+
} // namespace modbus_controller
} // namespace esphome
diff --git a/esphome/components/modbus_controller/const.py b/esphome/components/modbus_controller/const.py
index 5cf7d230f1..4d39e48dcd 100644
--- a/esphome/components/modbus_controller/const.py
+++ b/esphome/components/modbus_controller/const.py
@@ -9,6 +9,8 @@ CONF_MAX_CMD_RETRIES = "max_cmd_retries"
CONF_MODBUS_CONTROLLER_ID = "modbus_controller_id"
CONF_MODBUS_FUNCTIONCODE = "modbus_functioncode"
CONF_ON_COMMAND_SENT = "on_command_sent"
+CONF_ON_ONLINE = "on_online"
+CONF_ON_OFFLINE = "on_offline"
CONF_RAW_ENCODE = "raw_encode"
CONF_REGISTER_COUNT = "register_count"
CONF_REGISTER_TYPE = "register_type"
diff --git a/esphome/components/modbus_controller/modbus_controller.cpp b/esphome/components/modbus_controller/modbus_controller.cpp
index 1dcb533629..e1102516ca 100644
--- a/esphome/components/modbus_controller/modbus_controller.cpp
+++ b/esphome/components/modbus_controller/modbus_controller.cpp
@@ -32,8 +32,10 @@ bool ModbusController::send_next_command_() {
r.skip_updates_counter = this->offline_skip_updates_;
}
}
+
+ this->module_offline_ = true;
+ this->offline_callback_.call((int) command->function_code, command->register_address);
}
- this->module_offline_ = true;
ESP_LOGD(TAG, "Modbus command to device=%d register=0x%02X no response received - removed from send queue",
this->address_, command->register_address);
this->command_queue_.pop_front();
@@ -68,8 +70,10 @@ void ModbusController::on_modbus_data(const std::vector &data) {
r.skip_updates_counter = 0;
}
}
+ // Restore module online state
+ this->module_offline_ = false;
+ this->online_callback_.call((int) current_command->function_code, current_command->register_address);
}
- this->module_offline_ = false;
// Move the commandItem to the response queue
current_command->payload = data;
@@ -670,5 +674,13 @@ void ModbusController::add_on_command_sent_callback(std::functioncommand_sent_callback_.add(std::move(callback));
}
+void ModbusController::add_on_online_callback(std::function &&callback) {
+ this->online_callback_.add(std::move(callback));
+}
+
+void ModbusController::add_on_offline_callback(std::function &&callback) {
+ this->offline_callback_.add(std::move(callback));
+}
+
} // namespace modbus_controller
} // namespace esphome
diff --git a/esphome/components/modbus_controller/modbus_controller.h b/esphome/components/modbus_controller/modbus_controller.h
index 1fa35e1535..2a0b936bf5 100644
--- a/esphome/components/modbus_controller/modbus_controller.h
+++ b/esphome/components/modbus_controller/modbus_controller.h
@@ -468,6 +468,10 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
bool get_module_offline() { return module_offline_; }
/// Set callback for commands
void add_on_command_sent_callback(std::function &&callback);
+ /// Set callback for online changes
+ void add_on_online_callback(std::function &&callback);
+ /// Set callback for offline changes
+ void add_on_offline_callback(std::function &&callback);
/// called by esphome generated code to set the max_cmd_retries.
void set_max_cmd_retries(uint8_t max_cmd_retries) { this->max_cmd_retries_ = max_cmd_retries; }
/// get how many times a command will be (re)sent if no response is received
@@ -508,7 +512,12 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
uint16_t offline_skip_updates_;
/// How many times we will retry a command if we get no response
uint8_t max_cmd_retries_{4};
+ /// Command sent callback
CallbackManager command_sent_callback_{};
+ /// Server online callback
+ CallbackManager online_callback_{};
+ /// Server offline callback
+ CallbackManager offline_callback_{};
};
/** Convert vector response payload to float.
diff --git a/esphome/components/mopeka_pro_check/mopeka_pro_check.cpp b/esphome/components/mopeka_pro_check/mopeka_pro_check.cpp
index f79e40bb4e..9527f09f59 100644
--- a/esphome/components/mopeka_pro_check/mopeka_pro_check.cpp
+++ b/esphome/components/mopeka_pro_check/mopeka_pro_check.cpp
@@ -17,6 +17,8 @@ void MopekaProCheck::dump_config() {
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
LOG_SENSOR(" ", "Reading Distance", this->distance_);
+ LOG_SENSOR(" ", "Read Quality", this->read_quality_);
+ LOG_SENSOR(" ", "Ignored Reads", this->ignored_reads_);
}
/**
@@ -66,34 +68,49 @@ bool MopekaProCheck::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
this->battery_level_->publish_state(level);
}
+ // Get the quality value
+ SensorReadQuality quality_value = this->parse_read_quality_(manu_data.data);
+ if (this->read_quality_ != nullptr) {
+ this->read_quality_->publish_state(static_cast(quality_value));
+ }
+
+ // Determine if we have a good enough quality of read to report level and distance
+ // sensors. This sensor is reported regardless of distance or level sensors being enabled
+ if (quality_value < this->min_signal_quality_) {
+ ESP_LOGW(TAG, "Read Quality too low to report distance or level");
+ this->ignored_read_count_++;
+ } else {
+ // reset to zero since read quality was sufficient
+ this->ignored_read_count_ = 0;
+ }
+ // Report number of contiguous ignored reads if sensor defined
+ if (this->ignored_reads_ != nullptr) {
+ this->ignored_reads_->publish_state(this->ignored_read_count_);
+ }
+
// Get distance and level if either are sensors
if ((this->distance_ != nullptr) || (this->level_ != nullptr)) {
uint32_t distance_value = this->parse_distance_(manu_data.data);
- SensorReadQuality quality_value = this->parse_read_quality_(manu_data.data);
ESP_LOGD(TAG, "Distance Sensor: Quality (0x%X) Distance (%" PRId32 "mm)", quality_value, distance_value);
- if (quality_value < QUALITY_HIGH) {
- ESP_LOGW(TAG, "Poor read quality.");
- }
- if (quality_value < QUALITY_MED) {
- // if really bad reading set to 0
- ESP_LOGW(TAG, "Setting distance to 0");
- distance_value = 0;
- }
- // update distance sensor
- if (this->distance_ != nullptr) {
- this->distance_->publish_state(distance_value);
- }
-
- // update level sensor
- if (this->level_ != nullptr) {
- uint8_t tank_level = 0;
- if (distance_value >= this->full_mm_) {
- tank_level = 100; // cap at 100%
- } else if (distance_value > this->empty_mm_) {
- tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
+ // only update distance and level sensors if read quality was sufficient. This can be determined by
+ // if the ignored_read_count is zero.
+ if (this->ignored_read_count_ == 0) {
+ // update distance sensor
+ if (this->distance_ != nullptr) {
+ this->distance_->publish_state(distance_value);
+ }
+
+ // update level sensor
+ if (this->level_ != nullptr) {
+ uint8_t tank_level = 0;
+ if (distance_value >= this->full_mm_) {
+ tank_level = 100; // cap at 100%
+ } else if (distance_value > this->empty_mm_) {
+ tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
+ }
+ this->level_->publish_state(tank_level);
}
- this->level_->publish_state(tank_level);
}
}
@@ -131,6 +148,8 @@ uint32_t MopekaProCheck::parse_distance_(const std::vector &message) {
uint8_t MopekaProCheck::parse_temperature_(const std::vector &message) { return (message[2] & 0x7F) - 40; }
SensorReadQuality MopekaProCheck::parse_read_quality_(const std::vector &message) {
+ // Since a 8 bit value is being shifted and truncated to 2 bits all possible values are defined as enumeration
+ // value and the static cast is safe.
return static_cast(message[4] >> 6);
}
diff --git a/esphome/components/mopeka_pro_check/mopeka_pro_check.h b/esphome/components/mopeka_pro_check/mopeka_pro_check.h
index 8b4d47e4c6..c58406ac18 100644
--- a/esphome/components/mopeka_pro_check/mopeka_pro_check.h
+++ b/esphome/components/mopeka_pro_check/mopeka_pro_check.h
@@ -24,9 +24,9 @@ enum SensorType {
};
// Sensor read quality. If sensor is poorly placed or tank level
-// gets too low the read quality will show and the distanace
+// gets too low the read quality will show and the distance
// measurement may be inaccurate.
-enum SensorReadQuality { QUALITY_HIGH = 0x3, QUALITY_MED = 0x2, QUALITY_LOW = 0x1, QUALITY_NONE = 0x0 };
+enum SensorReadQuality { QUALITY_HIGH = 0x3, QUALITY_MED = 0x2, QUALITY_LOW = 0x1, QUALITY_ZERO = 0x0 };
class MopekaProCheck : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
@@ -35,11 +35,14 @@ class MopekaProCheck : public Component, public esp32_ble_tracker::ESPBTDeviceLi
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
+ void set_min_signal_quality(SensorReadQuality min) { this->min_signal_quality_ = min; };
void set_level(sensor::Sensor *level) { level_ = level; };
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; };
void set_battery_level(sensor::Sensor *bat) { battery_level_ = bat; };
void set_distance(sensor::Sensor *distance) { distance_ = distance; };
+ void set_signal_quality(sensor::Sensor *rq) { read_quality_ = rq; };
+ void set_ignored_reads(sensor::Sensor *ir) { ignored_reads_ = ir; };
void set_tank_full(float full) { full_mm_ = full; };
void set_tank_empty(float empty) { empty_mm_ = empty; };
@@ -49,9 +52,13 @@ class MopekaProCheck : public Component, public esp32_ble_tracker::ESPBTDeviceLi
sensor::Sensor *temperature_{nullptr};
sensor::Sensor *distance_{nullptr};
sensor::Sensor *battery_level_{nullptr};
+ sensor::Sensor *read_quality_{nullptr};
+ sensor::Sensor *ignored_reads_{nullptr};
uint32_t full_mm_;
uint32_t empty_mm_;
+ uint32_t ignored_read_count_ = 0;
+ SensorReadQuality min_signal_quality_ = QUALITY_MED;
uint8_t parse_battery_level_(const std::vector &message);
uint32_t parse_distance_(const std::vector &message);
diff --git a/esphome/components/mopeka_pro_check/sensor.py b/esphome/components/mopeka_pro_check/sensor.py
index 0ba33e94de..95ade53013 100644
--- a/esphome/components/mopeka_pro_check/sensor.py
+++ b/esphome/components/mopeka_pro_check/sensor.py
@@ -5,9 +5,12 @@ from esphome.const import (
CONF_DISTANCE,
CONF_MAC_ADDRESS,
CONF_ID,
+ ICON_COUNTER,
ICON_THERMOMETER,
ICON_RULER,
+ ICON_SIGNAL,
UNIT_PERCENT,
+ UNIT_EMPTY,
CONF_LEVEL,
CONF_TEMPERATURE,
DEVICE_CLASS_TEMPERATURE,
@@ -16,11 +19,15 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
CONF_BATTERY_LEVEL,
DEVICE_CLASS_BATTERY,
+ ENTITY_CATEGORY_DIAGNOSTIC,
)
CONF_TANK_TYPE = "tank_type"
CONF_CUSTOM_DISTANCE_FULL = "custom_distance_full"
CONF_CUSTOM_DISTANCE_EMPTY = "custom_distance_empty"
+CONF_SIGNAL_QUALITY = "signal_quality"
+CONF_MINIMUM_SIGNAL_QUALITY = "minimum_signal_quality"
+CONF_IGNORED_READS = "ignored_reads"
ICON_PROPANE_TANK = "mdi:propane-tank"
@@ -56,6 +63,14 @@ MopekaProCheck = mopeka_pro_check_ns.class_(
"MopekaProCheck", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
)
+SensorReadQuality = mopeka_pro_check_ns.enum("SensorReadQuality")
+SIGNAL_QUALITIES = {
+ "ZERO": SensorReadQuality.QUALITY_ZERO,
+ "LOW": SensorReadQuality.QUALITY_LOW,
+ "MEDIUM": SensorReadQuality.QUALITY_MED,
+ "HIGH": SensorReadQuality.QUALITY_HIGH,
+}
+
CONFIG_SCHEMA = (
cv.Schema(
{
@@ -89,6 +104,21 @@ CONFIG_SCHEMA = (
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
),
+ cv.Optional(CONF_SIGNAL_QUALITY): sensor.sensor_schema(
+ unit_of_measurement=UNIT_EMPTY,
+ icon=ICON_SIGNAL,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_MEASUREMENT,
+ ),
+ cv.Optional(CONF_IGNORED_READS): sensor.sensor_schema(
+ unit_of_measurement=UNIT_EMPTY,
+ icon=ICON_COUNTER,
+ accuracy_decimals=0,
+ entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
+ ),
+ cv.Optional(CONF_MINIMUM_SIGNAL_QUALITY, default="MEDIUM"): cv.enum(
+ SIGNAL_QUALITIES, upper=True
+ ),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
@@ -119,6 +149,11 @@ async def to_code(config):
cg.add(var.set_tank_empty(CONF_SUPPORTED_TANKS_MAP[t][0]))
cg.add(var.set_tank_full(CONF_SUPPORTED_TANKS_MAP[t][1]))
+ if (
+ minimum_signal_quality := config.get(CONF_MINIMUM_SIGNAL_QUALITY, None)
+ ) is not None:
+ cg.add(var.set_min_signal_quality(minimum_signal_quality))
+
if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
@@ -131,3 +166,9 @@ async def to_code(config):
if CONF_BATTERY_LEVEL in config:
sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))
+ if CONF_SIGNAL_QUALITY in config:
+ sens = await sensor.new_sensor(config[CONF_SIGNAL_QUALITY])
+ cg.add(var.set_signal_quality(sens))
+ if CONF_IGNORED_READS in config:
+ sens = await sensor.new_sensor(config[CONF_IGNORED_READS])
+ cg.add(var.set_ignored_reads(sens))
diff --git a/esphome/components/mqtt/__init__.py b/esphome/components/mqtt/__init__.py
index 336d928f71..86d163e61d 100644
--- a/esphome/components/mqtt/__init__.py
+++ b/esphome/components/mqtt/__init__.py
@@ -22,6 +22,7 @@ from esphome.const import (
CONF_DISCOVERY_PREFIX,
CONF_DISCOVERY_RETAIN,
CONF_DISCOVERY_UNIQUE_ID_GENERATOR,
+ CONF_ENABLE_ON_BOOT,
CONF_ID,
CONF_KEEPALIVE,
CONF_LEVEL,
@@ -41,6 +42,7 @@ from esphome.const import (
CONF_SHUTDOWN_MESSAGE,
CONF_SSL_FINGERPRINTS,
CONF_STATE_TOPIC,
+ CONF_SUBSCRIBE_QOS,
CONF_TOPIC,
CONF_TOPIC_PREFIX,
CONF_TRIGGER_ID,
@@ -98,6 +100,8 @@ MQTTMessage = mqtt_ns.struct("MQTTMessage")
MQTTClientComponent = mqtt_ns.class_("MQTTClientComponent", cg.Component)
MQTTPublishAction = mqtt_ns.class_("MQTTPublishAction", automation.Action)
MQTTPublishJsonAction = mqtt_ns.class_("MQTTPublishJsonAction", automation.Action)
+MQTTEnableAction = mqtt_ns.class_("MQTTEnableAction", automation.Action)
+MQTTDisableAction = mqtt_ns.class_("MQTTDisableAction", automation.Action)
MQTTMessageTrigger = mqtt_ns.class_(
"MQTTMessageTrigger", automation.Trigger.template(cg.std_string), cg.Component
)
@@ -207,6 +211,7 @@ CONFIG_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(MQTTClientComponent),
cv.Required(CONF_BROKER): cv.string_strict,
+ cv.Optional(CONF_ENABLE_ON_BOOT, default=True): cv.boolean,
cv.Optional(CONF_PORT, default=1883): cv.port,
cv.Optional(CONF_USERNAME, default=""): cv.string,
cv.Optional(CONF_PASSWORD, default=""): cv.string,
@@ -324,6 +329,7 @@ async def to_code(config):
cg.add_global(mqtt_ns.using)
cg.add(var.set_broker_address(config[CONF_BROKER]))
+ cg.add(var.set_enable_on_boot(config[CONF_ENABLE_ON_BOOT]))
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]))
@@ -518,6 +524,8 @@ async def register_mqtt_component(var, config):
cg.add(var.set_qos(config[CONF_QOS]))
if CONF_RETAIN in config:
cg.add(var.set_retain(config[CONF_RETAIN]))
+ if CONF_SUBSCRIBE_QOS in config:
+ cg.add(var.set_subscribe_qos(config[CONF_SUBSCRIBE_QOS]))
if not config.get(CONF_DISCOVERY, True):
cg.add(var.disable_discovery())
if CONF_STATE_TOPIC in config:
@@ -552,3 +560,31 @@ async def register_mqtt_component(var, config):
async def mqtt_connected_to_code(config, condition_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(condition_id, template_arg, paren)
+
+
+@automation.register_action(
+ "mqtt.enable",
+ MQTTEnableAction,
+ cv.Schema(
+ {
+ cv.GenerateID(): cv.use_id(MQTTClientComponent),
+ }
+ ),
+)
+async def mqtt_enable_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)
+
+
+@automation.register_action(
+ "mqtt.disable",
+ MQTTDisableAction,
+ cv.Schema(
+ {
+ cv.GenerateID(): cv.use_id(MQTTClientComponent),
+ }
+ ),
+)
+async def mqtt_disable_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)
diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp
index b5ac285026..106192c0e3 100644
--- a/esphome/components/mqtt/mqtt_client.cpp
+++ b/esphome/components/mqtt/mqtt_client.cpp
@@ -50,6 +50,8 @@ void MQTTClientComponent::setup() {
}
});
this->mqtt_backend_.set_on_disconnect([this](MQTTClientDisconnectReason reason) {
+ if (this->state_ == MQTT_CLIENT_DISABLED)
+ return;
this->state_ = MQTT_CLIENT_DISCONNECTED;
this->disconnect_reason_ = reason;
});
@@ -77,8 +79,9 @@ void MQTTClientComponent::setup() {
topic, [this](const std::string &topic, const std::string &payload) { this->send_device_info_(); }, 2);
}
- this->last_connected_ = millis();
- this->start_dnslookup_();
+ if (this->enable_on_boot_) {
+ this->enable();
+ }
}
void MQTTClientComponent::send_device_info_() {
@@ -163,7 +166,9 @@ void MQTTClientComponent::dump_config() {
ESP_LOGCONFIG(TAG, " Availability: '%s'", this->availability_.topic.c_str());
}
}
-bool MQTTClientComponent::can_proceed() { return network::is_disabled() || this->is_connected(); }
+bool MQTTClientComponent::can_proceed() {
+ return network::is_disabled() || this->state_ == MQTT_CLIENT_DISABLED || this->is_connected();
+}
void MQTTClientComponent::start_dnslookup_() {
for (auto &subscription : this->subscriptions_) {
@@ -339,6 +344,8 @@ void MQTTClientComponent::loop() {
const uint32_t now = millis();
switch (this->state_) {
+ case MQTT_CLIENT_DISABLED:
+ return; // Return to avoid a reboot when disabled
case MQTT_CLIENT_DISCONNECTED:
if (now - this->connect_begin_ > 5000) {
this->start_dnslookup_();
@@ -501,6 +508,23 @@ bool MQTTClientComponent::publish_json(const std::string &topic, const json::jso
return this->publish(topic, message, qos, retain);
}
+void MQTTClientComponent::enable() {
+ if (this->state_ != MQTT_CLIENT_DISABLED)
+ return;
+ ESP_LOGD(TAG, "Enabling MQTT...");
+ this->state_ = MQTT_CLIENT_DISCONNECTED;
+ this->last_connected_ = millis();
+ this->start_dnslookup_();
+}
+
+void MQTTClientComponent::disable() {
+ if (this->state_ == MQTT_CLIENT_DISABLED)
+ return;
+ ESP_LOGD(TAG, "Disabling MQTT...");
+ this->state_ = MQTT_CLIENT_DISABLED;
+ this->on_shutdown();
+}
+
/** Check if the message topic matches the given subscription topic
*
* INFO: MQTT spec mandates that topics must not be empty and must be valid NULL-terminated UTF-8 strings.
diff --git a/esphome/components/mqtt/mqtt_client.h b/esphome/components/mqtt/mqtt_client.h
index 887800f201..7ae3a6c5e8 100644
--- a/esphome/components/mqtt/mqtt_client.h
+++ b/esphome/components/mqtt/mqtt_client.h
@@ -87,7 +87,8 @@ struct MQTTDiscoveryInfo {
};
enum MQTTClientState {
- MQTT_CLIENT_DISCONNECTED = 0,
+ MQTT_CLIENT_DISABLED = 0,
+ MQTT_CLIENT_DISCONNECTED,
MQTT_CLIENT_RESOLVING_ADDRESS,
MQTT_CLIENT_CONNECTING,
MQTT_CLIENT_CONNECTED,
@@ -247,6 +248,9 @@ class MQTTClientComponent : public Component {
void register_mqtt_component(MQTTComponent *component);
bool is_connected();
+ void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
+ void enable();
+ void disable();
void on_shutdown() override;
@@ -314,10 +318,11 @@ class MQTTClientComponent : public Component {
MQTTBackendLibreTiny mqtt_backend_;
#endif
- MQTTClientState state_{MQTT_CLIENT_DISCONNECTED};
+ MQTTClientState state_{MQTT_CLIENT_DISABLED};
network::IPAddress ip_;
bool dns_resolved_{false};
bool dns_resolve_error_{false};
+ bool enable_on_boot_{true};
std::vector children_;
uint32_t reboot_timeout_{300000};
uint32_t connect_begin_;
@@ -414,6 +419,26 @@ template class MQTTConnectedCondition : public Condition
MQTTClientComponent *parent_;
};
+template class MQTTEnableAction : public Action {
+ public:
+ MQTTEnableAction(MQTTClientComponent *parent) : parent_(parent) {}
+
+ void play(Ts... x) override { this->parent_->enable(); }
+
+ protected:
+ MQTTClientComponent *parent_;
+};
+
+template class MQTTDisableAction : public Action {
+ public:
+ MQTTDisableAction(MQTTClientComponent *parent) : parent_(parent) {}
+
+ void play(Ts... x) override { this->parent_->disable(); }
+
+ protected:
+ MQTTClientComponent *parent_;
+};
+
} // namespace mqtt
} // namespace esphome
diff --git a/esphome/components/mqtt/mqtt_climate.cpp b/esphome/components/mqtt/mqtt_climate.cpp
index 49a8f06734..773d863835 100644
--- a/esphome/components/mqtt/mqtt_climate.cpp
+++ b/esphome/components/mqtt/mqtt_climate.cpp
@@ -71,8 +71,10 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
root[MQTT_MIN_TEMP] = traits.get_visual_min_temperature();
// max_temp
root[MQTT_MAX_TEMP] = traits.get_visual_max_temperature();
- // temp_step
- root["temp_step"] = traits.get_visual_target_temperature_step();
+ // target_temp_step
+ root[MQTT_TARGET_TEMPERATURE_STEP] = traits.get_visual_target_temperature_step();
+ // current_temp_step
+ root[MQTT_CURRENT_TEMPERATURE_STEP] = traits.get_visual_current_temperature_step();
// temperature units are always coerced to Celsius internally
root[MQTT_TEMPERATURE_UNIT] = "C";
diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp
index 295fbba5e5..3b9d367a7b 100644
--- a/esphome/components/mqtt/mqtt_component.cpp
+++ b/esphome/components/mqtt/mqtt_component.cpp
@@ -16,6 +16,8 @@ static const char *const TAG = "mqtt.component";
void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; }
+void MQTTComponent::set_subscribe_qos(uint8_t qos) { this->subscribe_qos_ = qos; }
+
void MQTTComponent::set_retain(bool retain) { this->retain_ = retain; }
std::string MQTTComponent::get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const {
@@ -76,6 +78,10 @@ bool MQTTComponent::send_discovery_() {
config.command_topic = true;
this->send_discovery(root, config);
+ // Set subscription QoS (default is 0)
+ if (this->subscribe_qos_ != 0) {
+ root[MQTT_QOS] = this->subscribe_qos_;
+ }
// Fields from EntityBase
if (this->get_entity()->has_own_name()) {
diff --git a/esphome/components/mqtt/mqtt_component.h b/esphome/components/mqtt/mqtt_component.h
index 147840d11f..01ba98ad40 100644
--- a/esphome/components/mqtt/mqtt_component.h
+++ b/esphome/components/mqtt/mqtt_component.h
@@ -89,6 +89,9 @@ class MQTTComponent : public Component {
void disable_discovery();
bool is_discovery_enabled() const;
+ /// Set the QOS for subscribe messages (used in discovery).
+ void set_subscribe_qos(uint8_t qos);
+
/// Override this method to return the component type (e.g. "light", "sensor", ...)
virtual std::string component_type() const = 0;
@@ -204,6 +207,7 @@ class MQTTComponent : public Component {
bool command_retain_{false};
bool retain_{true};
uint8_t qos_{0};
+ uint8_t subscribe_qos_{0};
bool discovery_enabled_{true};
bool resend_state_{false};
};
diff --git a/esphome/components/mqtt/mqtt_const.h b/esphome/components/mqtt/mqtt_const.h
index 71f169fbe8..445457a27f 100644
--- a/esphome/components/mqtt/mqtt_const.h
+++ b/esphome/components/mqtt/mqtt_const.h
@@ -51,6 +51,7 @@ constexpr const char *const MQTT_COMMAND_TOPIC = "cmd_t";
constexpr const char *const MQTT_CONFIGURATION_URL = "cu";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TEMPLATE = "curr_hum_tpl";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC = "curr_hum_t";
+constexpr const char *const MQTT_CURRENT_TEMPERATURE_STEP = "precision";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TEMPLATE = "curr_temp_tpl";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC = "curr_temp_t";
constexpr const char *const MQTT_DEVICE = "dev";
@@ -180,6 +181,7 @@ constexpr const char *const MQTT_PRESET_MODE_COMMAND_TOPIC = "pr_mode_cmd_t";
constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC = "pr_mode_stat_t";
constexpr const char *const MQTT_PRESET_MODE_VALUE_TEMPLATE = "pr_mode_val_tpl";
constexpr const char *const MQTT_PRESET_MODES = "pr_modes";
+constexpr const char *const MQTT_QOS = "qos";
constexpr const char *const MQTT_RED_TEMPLATE = "r_tpl";
constexpr const char *const MQTT_RETAIN = "ret";
constexpr const char *const MQTT_RGB_COMMAND_TEMPLATE = "rgb_cmd_tpl";
@@ -231,6 +233,7 @@ constexpr const char *const MQTT_TARGET_HUMIDITY_COMMAND_TEMPLATE = "hum_cmd_tpl
constexpr const char *const MQTT_TARGET_HUMIDITY_COMMAND_TOPIC = "hum_cmd_t";
constexpr const char *const MQTT_TARGET_HUMIDITY_STATE_TEMPLATE = "hum_state_tpl";
constexpr const char *const MQTT_TARGET_HUMIDITY_STATE_TOPIC = "hum_stat_t";
+constexpr const char *const MQTT_TARGET_TEMPERATURE_STEP = "temp_step";
constexpr const char *const MQTT_TEMPERATURE_COMMAND_TEMPLATE = "temp_cmd_tpl";
constexpr const char *const MQTT_TEMPERATURE_COMMAND_TOPIC = "temp_cmd_t";
constexpr const char *const MQTT_TEMPERATURE_HIGH_COMMAND_TEMPLATE = "temp_hi_cmd_tpl";
@@ -312,6 +315,7 @@ constexpr const char *const MQTT_COMMAND_TOPIC = "command_topic";
constexpr const char *const MQTT_CONFIGURATION_URL = "configuration_url";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TEMPLATE = "current_humidity_template";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC = "current_humidity_topic";
+constexpr const char *const MQTT_CURRENT_TEMPERATURE_STEP = "precision";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TEMPLATE = "current_temperature_template";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC = "current_temperature_topic";
constexpr const char *const MQTT_DEVICE = "device";
@@ -441,6 +445,7 @@ constexpr const char *const MQTT_PRESET_MODE_COMMAND_TOPIC = "preset_mode_comman
constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC = "preset_mode_state_topic";
constexpr const char *const MQTT_PRESET_MODE_VALUE_TEMPLATE = "preset_mode_value_template";
constexpr const char *const MQTT_PRESET_MODES = "preset_modes";
+constexpr const char *const MQTT_QOS = "qos";
constexpr const char *const MQTT_RED_TEMPLATE = "red_template";
constexpr const char *const MQTT_RETAIN = "retain";
constexpr const char *const MQTT_RGB_COMMAND_TEMPLATE = "rgb_command_template";
@@ -492,6 +497,7 @@ constexpr const char *const MQTT_TARGET_HUMIDITY_COMMAND_TEMPLATE = "target_humi
constexpr const char *const MQTT_TARGET_HUMIDITY_COMMAND_TOPIC = "target_humidity_command_topic";
constexpr const char *const MQTT_TARGET_HUMIDITY_STATE_TEMPLATE = "target_humidity_state_template";
constexpr const char *const MQTT_TARGET_HUMIDITY_STATE_TOPIC = "target_humidity_state_topic";
+constexpr const char *const MQTT_TARGET_TEMPERATURE_STEP = "temp_step";
constexpr const char *const MQTT_TEMPERATURE_COMMAND_TEMPLATE = "temperature_command_template";
constexpr const char *const MQTT_TEMPERATURE_COMMAND_TOPIC = "temperature_command_topic";
constexpr const char *const MQTT_TEMPERATURE_HIGH_COMMAND_TEMPLATE = "temperature_high_command_template";
diff --git a/esphome/components/nextion/automation.h b/esphome/components/nextion/automation.h
index f51fe6b4f8..5182e07229 100644
--- a/esphome/components/nextion/automation.h
+++ b/esphome/components/nextion/automation.h
@@ -42,5 +42,12 @@ class TouchTrigger : public Trigger {
}
};
+class BufferOverflowTrigger : public Trigger<> {
+ public:
+ explicit BufferOverflowTrigger(Nextion *nextion) {
+ nextion->add_buffer_overflow_event_callback([this]() { this->trigger(); });
+ }
+};
+
} // namespace nextion
} // namespace esphome
diff --git a/esphome/components/nextion/base_component.py b/esphome/components/nextion/base_component.py
index 2924f66d3c..9708379861 100644
--- a/esphome/components/nextion/base_component.py
+++ b/esphome/components/nextion/base_component.py
@@ -18,6 +18,7 @@ CONF_ON_SLEEP = "on_sleep"
CONF_ON_WAKE = "on_wake"
CONF_ON_SETUP = "on_setup"
CONF_ON_PAGE = "on_page"
+CONF_ON_BUFFER_OVERFLOW = "on_buffer_overflow"
CONF_TOUCH_SLEEP_TIMEOUT = "touch_sleep_timeout"
CONF_WAKE_UP_PAGE = "wake_up_page"
CONF_START_UP_PAGE = "start_up_page"
diff --git a/esphome/components/nextion/display.py b/esphome/components/nextion/display.py
index e403ba7ae8..6f284376af 100644
--- a/esphome/components/nextion/display.py
+++ b/esphome/components/nextion/display.py
@@ -13,6 +13,7 @@ from esphome.const import (
from esphome.core import CORE
from . import Nextion, nextion_ns, nextion_ref
from .base_component import (
+ CONF_ON_BUFFER_OVERFLOW,
CONF_ON_SLEEP,
CONF_ON_WAKE,
CONF_ON_SETUP,
@@ -36,6 +37,9 @@ SleepTrigger = nextion_ns.class_("SleepTrigger", automation.Trigger.template())
WakeTrigger = nextion_ns.class_("WakeTrigger", automation.Trigger.template())
PageTrigger = nextion_ns.class_("PageTrigger", automation.Trigger.template())
TouchTrigger = nextion_ns.class_("TouchTrigger", automation.Trigger.template())
+BufferOverflowTrigger = nextion_ns.class_(
+ "BufferOverflowTrigger", automation.Trigger.template()
+)
CONFIG_SCHEMA = (
display.BASIC_DISPLAY_SCHEMA.extend(
@@ -68,6 +72,13 @@ CONFIG_SCHEMA = (
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(TouchTrigger),
}
),
+ cv.Optional(CONF_ON_BUFFER_OVERFLOW): automation.validate_automation(
+ {
+ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
+ BufferOverflowTrigger
+ ),
+ }
+ ),
cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.int_range(min=3, max=65535),
cv.Optional(CONF_WAKE_UP_PAGE): cv.uint8_t,
cv.Optional(CONF_START_UP_PAGE): cv.uint8_t,
@@ -151,3 +162,7 @@ async def to_code(config):
],
conf,
)
+
+ for conf in config.get(CONF_ON_BUFFER_OVERFLOW, []):
+ trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
+ await automation.build_automation(trigger, [], conf)
diff --git a/esphome/components/nextion/nextion.cpp b/esphome/components/nextion/nextion.cpp
index a80f6efc91..984db09c57 100644
--- a/esphome/components/nextion/nextion.cpp
+++ b/esphome/components/nextion/nextion.cpp
@@ -190,6 +190,10 @@ void Nextion::add_touch_event_callback(std::functiontouch_callback_.add(std::move(callback));
}
+void Nextion::add_buffer_overflow_event_callback(std::function &&callback) {
+ this->buffer_overflow_callback_.add(std::move(callback));
+}
+
void Nextion::update_all_components() {
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
return;
@@ -458,7 +462,9 @@ void Nextion::process_nextion_commands_() {
this->remove_from_q_();
break;
case 0x24: // Serial Buffer overflow occurs
- ESP_LOGW(TAG, "Nextion reported Serial Buffer overflow!");
+ // Buffer will continue to receive the current instruction, all previous instructions are lost.
+ ESP_LOGE(TAG, "Nextion reported Serial Buffer overflow!");
+ this->buffer_overflow_callback_.call();
break;
case 0x65: { // touch event return data
if (to_process_length != 3) {
diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h
index 732ee9b455..f539c79718 100644
--- a/esphome/components/nextion/nextion.h
+++ b/esphome/components/nextion/nextion.h
@@ -1134,6 +1134,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*/
void add_touch_event_callback(std::function &&callback);
+ /** Add a callback to be notified when the nextion reports a buffer overflow.
+ *
+ * @param callback The void() callback.
+ */
+ void add_buffer_overflow_event_callback(std::function &&callback);
+
void update_all_components();
/**
@@ -1323,6 +1329,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
CallbackManager wake_callback_{};
CallbackManager page_callback_{};
CallbackManager touch_callback_{};
+ CallbackManager buffer_overflow_callback_{};
optional writer_;
float brightness_{1.0};
diff --git a/esphome/components/online_image/__init__.py b/esphome/components/online_image/__init__.py
index dfb10137aa..be1bfb4a00 100644
--- a/esphome/components/online_image/__init__.py
+++ b/esphome/components/online_image/__init__.py
@@ -98,6 +98,7 @@ CONFIG_SCHEMA = cv.Schema(
# esp8266_arduino=cv.Version(2, 7, 0),
esp32_arduino=cv.Version(0, 0, 0),
esp_idf=cv.Version(4, 0, 0),
+ rp2040_arduino=cv.Version(0, 0, 0),
),
)
)
diff --git a/esphome/components/online_image/online_image.cpp b/esphome/components/online_image/online_image.cpp
index 480bad6aca..1786809dfa 100644
--- a/esphome/components/online_image/online_image.cpp
+++ b/esphome/components/online_image/online_image.cpp
@@ -215,16 +215,10 @@ void OnlineImage::draw_pixel_(int x, int y, Color color) {
}
case ImageType::IMAGE_TYPE_RGB565: {
uint16_t col565 = display::ColorUtil::color_to_565(color);
- if (this->has_transparency()) {
- if (col565 == 0x0020) {
- col565 = 0;
- }
- if (color.w < 0x80) {
- col565 = 0x0020;
- }
- }
this->buffer_[pos + 0] = static_cast((col565 >> 8) & 0xFF);
this->buffer_[pos + 1] = static_cast(col565 & 0xFF);
+ if (this->has_transparency())
+ this->buffer_[pos + 2] = color.w;
break;
}
case ImageType::IMAGE_TYPE_RGBA: {
diff --git a/esphome/components/online_image/online_image.h b/esphome/components/online_image/online_image.h
index 51c11478cd..017402a088 100644
--- a/esphome/components/online_image/online_image.h
+++ b/esphome/components/online_image/online_image.h
@@ -86,13 +86,9 @@ class OnlineImage : public PollingComponent,
Allocator allocator_{Allocator::Flags::ALLOW_FAILURE};
uint32_t get_buffer_size_() const { return get_buffer_size_(this->buffer_width_, this->buffer_height_); }
- int get_buffer_size_(int width, int height) const {
- return std::ceil(image::image_type_to_bpp(this->type_) * width * height / 8.0);
- }
+ int get_buffer_size_(int width, int height) const { return (this->get_bpp() * width + 7u) / 8u * height; }
- int get_position_(int x, int y) const {
- return ((x + y * this->buffer_width_) * image::image_type_to_bpp(this->type_)) / 8;
- }
+ int get_position_(int x, int y) const { return (x + y * this->buffer_width_) * this->get_bpp() / 8; }
ESPHOME_ALWAYS_INLINE bool auto_resize_() const { return this->fixed_width_ == 0 || this->fixed_height_ == 0; }
diff --git a/esphome/components/opentherm/__init__.py b/esphome/components/opentherm/__init__.py
index 23443a4028..81cd78af08 100644
--- a/esphome/components/opentherm/__init__.py
+++ b/esphome/components/opentherm/__init__.py
@@ -1,9 +1,11 @@
from typing import Any
-from esphome import pins
import esphome.codegen as cg
import esphome.config_validation as cv
+from esphome import pins
+from esphome.components import sensor
from esphome.const import CONF_ID, PLATFORM_ESP32, PLATFORM_ESP8266
+from . import const, schema, validate, generate
CODEOWNERS = ["@olegtarasov"]
MULTI_CONF = True
@@ -15,15 +17,15 @@ CONF_DHW_ENABLE = "dhw_enable"
CONF_COOLING_ENABLE = "cooling_enable"
CONF_OTC_ACTIVE = "otc_active"
CONF_CH2_ACTIVE = "ch2_active"
+CONF_SUMMER_MODE_ACTIVE = "summer_mode_active"
+CONF_DHW_BLOCK = "dhw_block"
CONF_SYNC_MODE = "sync_mode"
-
-opentherm_ns = cg.esphome_ns.namespace("opentherm")
-OpenthermHub = opentherm_ns.class_("OpenthermHub", cg.Component)
+CONF_OPENTHERM_VERSION = "opentherm_version"
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
- cv.GenerateID(): cv.declare_id(OpenthermHub),
+ cv.GenerateID(): cv.declare_id(generate.OpenthermHub),
cv.Required(CONF_IN_PIN): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_OUT_PIN): pins.internal_gpio_output_pin_schema,
cv.Optional(CONF_CH_ENABLE, True): cv.boolean,
@@ -31,16 +33,23 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_COOLING_ENABLE, False): cv.boolean,
cv.Optional(CONF_OTC_ACTIVE, False): cv.boolean,
cv.Optional(CONF_CH2_ACTIVE, False): cv.boolean,
+ cv.Optional(CONF_SUMMER_MODE_ACTIVE, False): cv.boolean,
+ cv.Optional(CONF_DHW_BLOCK, False): cv.boolean,
cv.Optional(CONF_SYNC_MODE, False): cv.boolean,
+ cv.Optional(CONF_OPENTHERM_VERSION): cv.positive_float,
}
- ).extend(cv.COMPONENT_SCHEMA),
+ )
+ .extend(
+ validate.create_entities_schema(
+ schema.INPUTS, (lambda _: cv.use_id(sensor.Sensor))
+ )
+ )
+ .extend(cv.COMPONENT_SCHEMA),
cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266]),
)
async def to_code(config: dict[str, Any]) -> None:
- # Create the hub, passing the two callbacks defined below
- # Since the hub is used in the callbacks, we need to define it first
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
@@ -52,6 +61,23 @@ async def to_code(config: dict[str, Any]) -> None:
cg.add(var.set_out_pin(out_pin))
non_sensors = {CONF_ID, CONF_IN_PIN, CONF_OUT_PIN}
+ input_sensors = []
for key, value in config.items():
- if key not in non_sensors:
+ if key in non_sensors:
+ continue
+ if key in schema.INPUTS:
+ input_sensor = await cg.get_variable(value)
+ cg.add(
+ getattr(var, f"set_{key}_{const.INPUT_SENSOR.lower()}")(input_sensor)
+ )
+ input_sensors.append(key)
+ else:
cg.add(getattr(var, f"set_{key}")(value))
+
+ if len(input_sensors) > 0:
+ generate.define_has_component(const.INPUT_SENSOR, input_sensors)
+ generate.define_message_handler(
+ const.INPUT_SENSOR, input_sensors, schema.INPUTS
+ )
+ generate.define_readers(const.INPUT_SENSOR, input_sensors)
+ generate.add_messages(var, input_sensors, schema.INPUTS)
diff --git a/esphome/components/opentherm/binary_sensor/__init__.py b/esphome/components/opentherm/binary_sensor/__init__.py
new file mode 100644
index 0000000000..643734f90c
--- /dev/null
+++ b/esphome/components/opentherm/binary_sensor/__init__.py
@@ -0,0 +1,33 @@
+from typing import Any
+
+import esphome.config_validation as cv
+from esphome.components import binary_sensor
+from .. import const, schema, validate, generate
+
+DEPENDENCIES = [const.OPENTHERM]
+COMPONENT_TYPE = const.BINARY_SENSOR
+
+
+def get_entity_validation_schema(entity: schema.BinarySensorSchema) -> cv.Schema:
+ return binary_sensor.binary_sensor_schema(
+ device_class=(
+ entity.device_class
+ or binary_sensor._UNDEF # pylint: disable=protected-access
+ ),
+ icon=(entity.icon or binary_sensor._UNDEF), # pylint: disable=protected-access
+ )
+
+
+CONFIG_SCHEMA = validate.create_component_schema(
+ schema.BINARY_SENSORS, get_entity_validation_schema
+)
+
+
+async def to_code(config: dict[str, Any]) -> None:
+ await generate.component_to_code(
+ COMPONENT_TYPE,
+ schema.BINARY_SENSORS,
+ binary_sensor.BinarySensor,
+ generate.create_only_conf(binary_sensor.new_binary_sensor),
+ config,
+ )
diff --git a/esphome/components/opentherm/const.py b/esphome/components/opentherm/const.py
new file mode 100644
index 0000000000..a113331585
--- /dev/null
+++ b/esphome/components/opentherm/const.py
@@ -0,0 +1,11 @@
+OPENTHERM = "opentherm"
+
+CONF_OPENTHERM_ID = "opentherm_id"
+CONF_DATA_TYPE = "data_type"
+
+SENSOR = "sensor"
+BINARY_SENSOR = "binary_sensor"
+SWITCH = "switch"
+NUMBER = "number"
+OUTPUT = "output"
+INPUT_SENSOR = "input_sensor"
diff --git a/esphome/components/opentherm/generate.py b/esphome/components/opentherm/generate.py
new file mode 100644
index 0000000000..9716cab093
--- /dev/null
+++ b/esphome/components/opentherm/generate.py
@@ -0,0 +1,142 @@
+from collections.abc import Awaitable
+from typing import Any, Callable
+
+import esphome.codegen as cg
+from esphome.const import CONF_ID
+from . import const
+from .schema import TSchema
+
+opentherm_ns = cg.esphome_ns.namespace("opentherm")
+OpenthermHub = opentherm_ns.class_("OpenthermHub", cg.Component)
+
+
+def define_has_component(component_type: str, keys: list[str]) -> None:
+ cg.add_define(
+ f"OPENTHERM_{component_type.upper()}_LIST(F, sep)",
+ cg.RawExpression(
+ " sep ".join(map(lambda key: f"F({key}_{component_type.lower()})", keys))
+ ),
+ )
+ for key in keys:
+ cg.add_define(f"OPENTHERM_HAS_{component_type.upper()}_{key}")
+
+
+def define_message_handler(
+ component_type: str, keys: list[str], schemas: dict[str, TSchema]
+) -> None:
+ # The macros defined here should be able to generate things like this:
+ # // Parsing a message and publishing to sensors
+ # case MessageId::Message:
+ # // Can have multiple sensors here, for example for a Status message with multiple flags
+ # this->thing_binary_sensor->publish_state(parse_flag8_lb_0(response));
+ # this->other_binary_sensor->publish_state(parse_flag8_lb_1(response));
+ # break;
+ # // Building a message for a write request
+ # case MessageId::Message: {
+ # unsigned int data = 0;
+ # data = write_flag8_lb_0(some_input_switch->state, data); // Where input_sensor can also be a number/output/switch
+ # data = write_u8_hb(some_number->state, data);
+ # return opentherm_->build_request_(MessageType::WriteData, MessageId::Message, data);
+ # }
+
+ messages: dict[str, list[tuple[str, str]]] = {}
+ for key in keys:
+ msg = schemas[key].message
+ if msg not in messages:
+ messages[msg] = []
+ messages[msg].append((key, schemas[key].message_data))
+
+ cg.add_define(
+ f"OPENTHERM_{component_type.upper()}_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)",
+ cg.RawExpression(
+ " msg_sep ".join(
+ [
+ f"MESSAGE({msg}) "
+ + " entity_sep ".join(
+ [
+ f"ENTITY({key}_{component_type.lower()}, {msg_data})"
+ for key, msg_data in keys
+ ]
+ )
+ + " postscript"
+ for msg, keys in messages.items()
+ ]
+ )
+ ),
+ )
+
+
+def define_readers(component_type: str, keys: list[str]) -> None:
+ for key in keys:
+ cg.add_define(
+ f"OPENTHERM_READ_{key}",
+ cg.RawExpression(f"this->{key}_{component_type.lower()}->state"),
+ )
+
+
+def add_messages(hub: cg.MockObj, keys: list[str], schemas: dict[str, TSchema]):
+ messages: set[tuple[str, bool]] = set()
+ for key in keys:
+ messages.add((schemas[key].message, schemas[key].keep_updated))
+ for msg, keep_updated in messages:
+ msg_expr = cg.RawExpression(f"esphome::opentherm::MessageId::{msg}")
+ if keep_updated:
+ cg.add(hub.add_repeating_message(msg_expr))
+ else:
+ cg.add(hub.add_initial_message(msg_expr))
+
+
+def add_property_set(var: cg.MockObj, config_key: str, config: dict[str, Any]) -> None:
+ if config_key in config:
+ cg.add(getattr(var, f"set_{config_key}")(config[config_key]))
+
+
+Create = Callable[[dict[str, Any], str, cg.MockObj], Awaitable[cg.Pvariable]]
+
+
+def create_only_conf(
+ create: Callable[[dict[str, Any]], Awaitable[cg.Pvariable]]
+) -> Create:
+ return lambda conf, _key, _hub: create(conf)
+
+
+async def component_to_code(
+ component_type: str,
+ schemas: dict[str, TSchema],
+ type: cg.MockObjClass,
+ create: Create,
+ config: dict[str, Any],
+) -> list[str]:
+ """Generate the code for each configured component in the schema of a component type.
+
+ Parameters:
+ - component_type: The type of component, e.g. "sensor" or "binary_sensor"
+ - schema_: The schema for that component type, a list of available components
+ - type: The type of the component, e.g. sensor.Sensor or OpenthermOutput
+ - create: A constructor function for the component, which receives the config,
+ the key and the hub and should asynchronously return the new component
+ - config: The configuration for this component type
+
+ Returns: The list of keys for the created components
+ """
+ cg.add_define(f"OPENTHERM_USE_{component_type.upper()}")
+
+ hub = await cg.get_variable(config[const.CONF_OPENTHERM_ID])
+
+ keys: list[str] = []
+ for key, conf in config.items():
+ if not isinstance(conf, dict):
+ continue
+ id = conf[CONF_ID]
+ if id and id.type == type:
+ entity = await create(conf, key, hub)
+ if const.CONF_DATA_TYPE in conf:
+ schemas[key].message_data = conf[const.CONF_DATA_TYPE]
+ cg.add(getattr(hub, f"set_{key}_{component_type.lower()}")(entity))
+ keys.append(key)
+
+ define_has_component(component_type, keys)
+ define_message_handler(component_type, keys, schemas)
+ add_messages(hub, keys, schemas)
+
+ return keys
diff --git a/esphome/components/opentherm/hub.cpp b/esphome/components/opentherm/hub.cpp
index c26fbced32..dfa8ea95c5 100644
--- a/esphome/components/opentherm/hub.cpp
+++ b/esphome/components/opentherm/hub.cpp
@@ -7,50 +7,147 @@ namespace esphome {
namespace opentherm {
static const char *const TAG = "opentherm";
+namespace message_data {
+bool parse_flag8_lb_0(OpenthermData &data) { return read_bit(data.valueLB, 0); }
+bool parse_flag8_lb_1(OpenthermData &data) { return read_bit(data.valueLB, 1); }
+bool parse_flag8_lb_2(OpenthermData &data) { return read_bit(data.valueLB, 2); }
+bool parse_flag8_lb_3(OpenthermData &data) { return read_bit(data.valueLB, 3); }
+bool parse_flag8_lb_4(OpenthermData &data) { return read_bit(data.valueLB, 4); }
+bool parse_flag8_lb_5(OpenthermData &data) { return read_bit(data.valueLB, 5); }
+bool parse_flag8_lb_6(OpenthermData &data) { return read_bit(data.valueLB, 6); }
+bool parse_flag8_lb_7(OpenthermData &data) { return read_bit(data.valueLB, 7); }
+bool parse_flag8_hb_0(OpenthermData &data) { return read_bit(data.valueHB, 0); }
+bool parse_flag8_hb_1(OpenthermData &data) { return read_bit(data.valueHB, 1); }
+bool parse_flag8_hb_2(OpenthermData &data) { return read_bit(data.valueHB, 2); }
+bool parse_flag8_hb_3(OpenthermData &data) { return read_bit(data.valueHB, 3); }
+bool parse_flag8_hb_4(OpenthermData &data) { return read_bit(data.valueHB, 4); }
+bool parse_flag8_hb_5(OpenthermData &data) { return read_bit(data.valueHB, 5); }
+bool parse_flag8_hb_6(OpenthermData &data) { return read_bit(data.valueHB, 6); }
+bool parse_flag8_hb_7(OpenthermData &data) { return read_bit(data.valueHB, 7); }
+uint8_t parse_u8_lb(OpenthermData &data) { return data.valueLB; }
+uint8_t parse_u8_hb(OpenthermData &data) { return data.valueHB; }
+int8_t parse_s8_lb(OpenthermData &data) { return (int8_t) data.valueLB; }
+int8_t parse_s8_hb(OpenthermData &data) { return (int8_t) data.valueHB; }
+uint16_t parse_u16(OpenthermData &data) { return data.u16(); }
+uint16_t parse_u8_lb_60(OpenthermData &data) { return data.valueLB * 60; }
+uint16_t parse_u8_hb_60(OpenthermData &data) { return data.valueHB * 60; }
+int16_t parse_s16(OpenthermData &data) { return data.s16(); }
+float parse_f88(OpenthermData &data) { return data.f88(); }
-OpenthermData OpenthermHub::build_request_(MessageId request_id) {
+void write_flag8_lb_0(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 0, value); }
+void write_flag8_lb_1(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 1, value); }
+void write_flag8_lb_2(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 2, value); }
+void write_flag8_lb_3(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 3, value); }
+void write_flag8_lb_4(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 4, value); }
+void write_flag8_lb_5(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 5, value); }
+void write_flag8_lb_6(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 6, value); }
+void write_flag8_lb_7(const bool value, OpenthermData &data) { data.valueLB = write_bit(data.valueLB, 7, value); }
+void write_flag8_hb_0(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 0, value); }
+void write_flag8_hb_1(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 1, value); }
+void write_flag8_hb_2(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 2, value); }
+void write_flag8_hb_3(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 3, value); }
+void write_flag8_hb_4(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 4, value); }
+void write_flag8_hb_5(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 5, value); }
+void write_flag8_hb_6(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 6, value); }
+void write_flag8_hb_7(const bool value, OpenthermData &data) { data.valueHB = write_bit(data.valueHB, 7, value); }
+void write_u8_lb(const uint8_t value, OpenthermData &data) { data.valueLB = value; }
+void write_u8_hb(const uint8_t value, OpenthermData &data) { data.valueHB = value; }
+void write_s8_lb(const int8_t value, OpenthermData &data) { data.valueLB = (uint8_t) value; }
+void write_s8_hb(const int8_t value, OpenthermData &data) { data.valueHB = (uint8_t) value; }
+void write_u16(const uint16_t value, OpenthermData &data) { data.u16(value); }
+void write_s16(const int16_t value, OpenthermData &data) { data.s16(value); }
+void write_f88(const float value, OpenthermData &data) { data.f88(value); }
+
+} // namespace message_data
+
+OpenthermData OpenthermHub::build_request_(MessageId request_id) const {
OpenthermData data;
data.type = 0;
data.id = 0;
data.valueHB = 0;
data.valueLB = 0;
- // First, handle the status request. This requires special logic, because we
- // wouldn't want to inadvertently disable domestic hot water, for example.
- // It is also included in the macro-generated code below, but that will
- // never be executed, because we short-circuit it here.
+ // We need this special logic for STATUS message because we have two options for specifying boiler modes:
+ // with static config values in the hub, or with separate switches.
if (request_id == MessageId::STATUS) {
- bool const ch_enabled = this->ch_enable;
- bool dhw_enabled = this->dhw_enable;
- bool cooling_enabled = this->cooling_enable;
- bool otc_enabled = this->otc_active;
- bool ch2_enabled = this->ch2_active;
+ // NOLINTBEGIN
+ bool const ch_enabled = this->ch_enable && OPENTHERM_READ_ch_enable && OPENTHERM_READ_t_set > 0.0;
+ bool const dhw_enabled = this->dhw_enable && OPENTHERM_READ_dhw_enable;
+ bool const cooling_enabled =
+ this->cooling_enable && OPENTHERM_READ_cooling_enable && OPENTHERM_READ_cooling_control > 0.0;
+ bool const otc_enabled = this->otc_active && OPENTHERM_READ_otc_active;
+ bool const ch2_enabled = this->ch2_active && OPENTHERM_READ_ch2_active && OPENTHERM_READ_t_set_ch2 > 0.0;
+ bool const summer_mode_is_active = this->summer_mode_active && OPENTHERM_READ_summer_mode_active;
+ bool const dhw_blocked = this->dhw_block && OPENTHERM_READ_dhw_block;
+ // NOLINTEND
data.type = MessageType::READ_DATA;
data.id = MessageId::STATUS;
- data.valueHB = ch_enabled | (dhw_enabled << 1) | (cooling_enabled << 2) | (otc_enabled << 3) | (ch2_enabled << 4);
+ data.valueHB = ch_enabled | (dhw_enabled << 1) | (cooling_enabled << 2) | (otc_enabled << 3) | (ch2_enabled << 4) |
+ (summer_mode_is_active << 5) | (dhw_blocked << 6);
+
+ return data;
+ }
+
+ // Another special case is OpenTherm version number which is configured at hub level as a constant
+ if (request_id == MessageId::OT_VERSION_CONTROLLER) {
+ data.type = MessageType::WRITE_DATA;
+ data.id = MessageId::OT_VERSION_CONTROLLER;
+ data.f88(this->opentherm_version_);
+
+ return data;
+ }
// Disable incomplete switch statement warnings, because the cases in each
// switch are generated based on the configured sensors and inputs.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
- // TODO: This is a placeholder for an auto-generated switch statement which builds request structure based on
- // which sensors are enabled in config.
+ // Next, we start with the write requests from switches and other inputs,
+ // because we would want to write that data if it is available, rather than
+ // request a read for that type (in the case that both read and write are
+ // supported).
+ switch (request_id) {
+ OPENTHERM_SWITCH_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_WRITE_MESSAGE, OPENTHERM_MESSAGE_WRITE_ENTITY, ,
+ OPENTHERM_MESSAGE_WRITE_POSTSCRIPT, )
+ OPENTHERM_NUMBER_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_WRITE_MESSAGE, OPENTHERM_MESSAGE_WRITE_ENTITY, ,
+ OPENTHERM_MESSAGE_WRITE_POSTSCRIPT, )
+ OPENTHERM_OUTPUT_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_WRITE_MESSAGE, OPENTHERM_MESSAGE_WRITE_ENTITY, ,
+ OPENTHERM_MESSAGE_WRITE_POSTSCRIPT, )
+ OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_WRITE_MESSAGE, OPENTHERM_MESSAGE_WRITE_ENTITY, ,
+ OPENTHERM_MESSAGE_WRITE_POSTSCRIPT, )
+ }
+ // Finally, handle the simple read requests, which only change with the message id.
+ switch (request_id) { OPENTHERM_SENSOR_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_READ_MESSAGE, OPENTHERM_IGNORE, , , ) }
+ switch (request_id) {
+ OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_READ_MESSAGE, OPENTHERM_IGNORE, , , )
+ }
#pragma GCC diagnostic pop
- return data;
- }
- return OpenthermData();
+ // And if we get here, a message was requested which somehow wasn't handled.
+ // This shouldn't happen due to the way the defines are configured, so we
+ // log an error and just return a 0 message.
+ ESP_LOGE(TAG, "Tried to create a request with unknown id %d. This should never happen, so please open an issue.",
+ request_id);
+ return {};
}
-OpenthermHub::OpenthermHub() : Component() {}
+OpenthermHub::OpenthermHub() : Component(), in_pin_{}, out_pin_{} {}
void OpenthermHub::process_response(OpenthermData &data) {
ESP_LOGD(TAG, "Received OpenTherm response with id %d (%s)", data.id,
this->opentherm_->message_id_to_str((MessageId) data.id));
ESP_LOGD(TAG, "%s", this->opentherm_->debug_data(data).c_str());
+
+ switch (data.id) {
+ OPENTHERM_SENSOR_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_RESPONSE_MESSAGE, OPENTHERM_MESSAGE_RESPONSE_ENTITY, ,
+ OPENTHERM_MESSAGE_RESPONSE_POSTSCRIPT, )
+ }
+ switch (data.id) {
+ OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS(OPENTHERM_MESSAGE_RESPONSE_MESSAGE, OPENTHERM_MESSAGE_RESPONSE_ENTITY, ,
+ OPENTHERM_MESSAGE_RESPONSE_POSTSCRIPT, )
+ }
}
void OpenthermHub::setup() {
@@ -67,6 +164,13 @@ void OpenthermHub::setup() {
// good practice anyway.
this->add_repeating_message(MessageId::STATUS);
+ // Also ensure that we start communication with the STATUS message
+ this->initial_messages_.insert(this->initial_messages_.begin(), MessageId::STATUS);
+
+ if (this->opentherm_version_ > 0.0f) {
+ this->initial_messages_.insert(this->initial_messages_.begin(), MessageId::OT_VERSION_CONTROLLER);
+ }
+
this->current_message_iterator_ = this->initial_messages_.begin();
}
@@ -254,22 +358,24 @@ void OpenthermHub::handle_timeout_error_() {
this->stop_opentherm_();
}
-#define ID(x) x
-#define SHOW2(x) #x
-#define SHOW(x) SHOW2(x)
-
void OpenthermHub::dump_config() {
ESP_LOGCONFIG(TAG, "OpenTherm:");
LOG_PIN(" In: ", this->in_pin_);
LOG_PIN(" Out: ", this->out_pin_);
ESP_LOGCONFIG(TAG, " Sync mode: %d", this->sync_mode_);
+ ESP_LOGCONFIG(TAG, " Sensors: %s", SHOW(OPENTHERM_SENSOR_LIST(ID, )));
+ ESP_LOGCONFIG(TAG, " Binary sensors: %s", SHOW(OPENTHERM_BINARY_SENSOR_LIST(ID, )));
+ ESP_LOGCONFIG(TAG, " Switches: %s", SHOW(OPENTHERM_SWITCH_LIST(ID, )));
+ ESP_LOGCONFIG(TAG, " Input sensors: %s", SHOW(OPENTHERM_INPUT_SENSOR_LIST(ID, )));
+ ESP_LOGCONFIG(TAG, " Outputs: %s", SHOW(OPENTHERM_OUTPUT_LIST(ID, )));
+ ESP_LOGCONFIG(TAG, " Numbers: %s", SHOW(OPENTHERM_NUMBER_LIST(ID, )));
ESP_LOGCONFIG(TAG, " Initial requests:");
for (auto type : this->initial_messages_) {
- ESP_LOGCONFIG(TAG, " - %d", type);
+ ESP_LOGCONFIG(TAG, " - %d (%s)", type, this->opentherm_->message_id_to_str((type)));
}
ESP_LOGCONFIG(TAG, " Repeating requests:");
for (auto type : this->repeating_messages_) {
- ESP_LOGCONFIG(TAG, " - %d", type);
+ ESP_LOGCONFIG(TAG, " - %d (%s)", type, this->opentherm_->message_id_to_str((type)));
}
}
diff --git a/esphome/components/opentherm/hub.h b/esphome/components/opentherm/hub.h
index ce9f09fe33..1f536653e8 100644
--- a/esphome/components/opentherm/hub.h
+++ b/esphome/components/opentherm/hub.h
@@ -4,14 +4,37 @@
#include "esphome/core/hal.h"
#include "esphome/core/component.h"
#include "esphome/core/log.h"
+#include
#include "opentherm.h"
+#ifdef OPENTHERM_USE_SENSOR
+#include "esphome/components/sensor/sensor.h"
+#endif
+
+#ifdef OPENTHERM_USE_BINARY_SENSOR
+#include "esphome/components/binary_sensor/binary_sensor.h"
+#endif
+
+#ifdef OPENTHERM_USE_SWITCH
+#include "esphome/components/opentherm/switch/switch.h"
+#endif
+
+#ifdef OPENTHERM_USE_OUTPUT
+#include "esphome/components/opentherm/output/output.h"
+#endif
+
+#ifdef OPENTHERM_USE_NUMBER
+#include "esphome/components/opentherm/number/number.h"
+#endif
+
#include
#include
#include
#include
+#include "opentherm_macros.h"
+
namespace esphome {
namespace opentherm {
@@ -23,15 +46,27 @@ class OpenthermHub : public Component {
// The OpenTherm interface
std::unique_ptr opentherm_;
+ OPENTHERM_SENSOR_LIST(OPENTHERM_DECLARE_SENSOR, )
+
+ OPENTHERM_BINARY_SENSOR_LIST(OPENTHERM_DECLARE_BINARY_SENSOR, )
+
+ OPENTHERM_SWITCH_LIST(OPENTHERM_DECLARE_SWITCH, )
+
+ OPENTHERM_NUMBER_LIST(OPENTHERM_DECLARE_NUMBER, )
+
+ OPENTHERM_OUTPUT_LIST(OPENTHERM_DECLARE_OUTPUT, )
+
+ OPENTHERM_INPUT_SENSOR_LIST(OPENTHERM_DECLARE_INPUT_SENSOR, )
+
// The set of initial messages to send on starting communication with the boiler
- std::unordered_set initial_messages_;
+ std::vector initial_messages_;
// and the repeating messages which are sent repeatedly to update various sensors
// and boiler parameters (like the setpoint).
- std::unordered_set repeating_messages_;
+ std::vector repeating_messages_;
// Indicates if we are still working on the initial requests or not
bool sending_initial_ = true;
// Index for the current request in one of the _requests sets.
- std::unordered_set::const_iterator current_message_iterator_;
+ std::vector::const_iterator current_message_iterator_;
uint32_t last_conversation_start_ = 0;
uint32_t last_conversation_end_ = 0;
@@ -43,8 +78,10 @@ class OpenthermHub : public Component {
// Very likely to happen while using Dallas temperature sensors.
bool sync_mode_ = false;
+ float opentherm_version_ = 0.0f;
+
// Create OpenTherm messages based on the message id
- OpenthermData build_request_(MessageId request_id);
+ OpenthermData build_request_(MessageId request_id) const;
void handle_protocol_write_error_();
void handle_protocol_read_error_();
void handle_timeout_error_();
@@ -78,17 +115,30 @@ class OpenthermHub : public Component {
void set_in_pin(InternalGPIOPin *in_pin) { this->in_pin_ = in_pin; }
void set_out_pin(InternalGPIOPin *out_pin) { this->out_pin_ = out_pin; }
- // Add a request to the set of initial requests
- void add_initial_message(MessageId message_id) { this->initial_messages_.insert(message_id); }
+ OPENTHERM_SENSOR_LIST(OPENTHERM_SET_SENSOR, )
+
+ OPENTHERM_BINARY_SENSOR_LIST(OPENTHERM_SET_BINARY_SENSOR, )
+
+ OPENTHERM_SWITCH_LIST(OPENTHERM_SET_SWITCH, )
+
+ OPENTHERM_NUMBER_LIST(OPENTHERM_SET_NUMBER, )
+
+ OPENTHERM_OUTPUT_LIST(OPENTHERM_SET_OUTPUT, )
+
+ OPENTHERM_INPUT_SENSOR_LIST(OPENTHERM_SET_INPUT_SENSOR, )
+
+ // Add a request to the vector of initial requests
+ void add_initial_message(MessageId message_id) { this->initial_messages_.push_back(message_id); }
// Add a request to the set of repeating requests. Note that a large number of repeating
// requests will slow down communication with the boiler. Each request may take up to 1 second,
// so with all sensors enabled, it may take about half a minute before a change in setpoint
// will be processed.
- void add_repeating_message(MessageId message_id) { this->repeating_messages_.insert(message_id); }
+ void add_repeating_message(MessageId message_id) { this->repeating_messages_.push_back(message_id); }
- // There are five status variables, which can either be set as a simple variable,
+ // There are seven status variables, which can either be set as a simple variable,
// or using a switch. ch_enable and dhw_enable default to true, the others to false.
- bool ch_enable = true, dhw_enable = true, cooling_enable = false, otc_active = false, ch2_active = false;
+ bool ch_enable = true, dhw_enable = true, cooling_enable = false, otc_active = false, ch2_active = false,
+ summer_mode_active = false, dhw_block = false;
// Setters for the status variables
void set_ch_enable(bool value) { this->ch_enable = value; }
@@ -96,7 +146,10 @@ class OpenthermHub : public Component {
void set_cooling_enable(bool value) { this->cooling_enable = value; }
void set_otc_active(bool value) { this->otc_active = value; }
void set_ch2_active(bool value) { this->ch2_active = value; }
+ void set_summer_mode_active(bool value) { this->summer_mode_active = value; }
+ void set_dhw_block(bool value) { this->dhw_block = value; }
void set_sync_mode(bool sync_mode) { this->sync_mode_ = sync_mode; }
+ void set_opentherm_version(float value) { this->opentherm_version_ = value; }
float get_setup_priority() const override { return setup_priority::HARDWARE; }
diff --git a/esphome/components/opentherm/input.h b/esphome/components/opentherm/input.h
new file mode 100644
index 0000000000..3567138792
--- /dev/null
+++ b/esphome/components/opentherm/input.h
@@ -0,0 +1,18 @@
+#pragma once
+
+namespace esphome {
+namespace opentherm {
+
+class OpenthermInput {
+ public:
+ bool auto_min_value, auto_max_value;
+
+ virtual void set_min_value(float min_value) = 0;
+ virtual void set_max_value(float max_value) = 0;
+
+ virtual void set_auto_min_value(bool auto_min_value) { this->auto_min_value = auto_min_value; }
+ virtual void set_auto_max_value(bool auto_max_value) { this->auto_max_value = auto_max_value; }
+};
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/input.py b/esphome/components/opentherm/input.py
new file mode 100644
index 0000000000..7897747be1
--- /dev/null
+++ b/esphome/components/opentherm/input.py
@@ -0,0 +1,51 @@
+from typing import Any
+
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from . import schema, generate
+
+CONF_min_value = "min_value"
+CONF_max_value = "max_value"
+CONF_auto_min_value = "auto_min_value"
+CONF_auto_max_value = "auto_max_value"
+CONF_step = "step"
+
+OpenthermInput = generate.opentherm_ns.class_("OpenthermInput")
+
+
+def validate_min_value_less_than_max_value(conf):
+ if (
+ CONF_min_value in conf
+ and CONF_max_value in conf
+ and conf[CONF_min_value] > conf[CONF_max_value]
+ ):
+ raise cv.Invalid(f"{CONF_min_value} must be less than {CONF_max_value}")
+ return conf
+
+
+def input_schema(entity: schema.InputSchema) -> cv.Schema:
+ result = cv.Schema(
+ {
+ cv.Optional(CONF_min_value, entity.range[0]): cv.float_range(
+ entity.range[0], entity.range[1]
+ ),
+ cv.Optional(CONF_max_value, entity.range[1]): cv.float_range(
+ entity.range[0], entity.range[1]
+ ),
+ }
+ )
+ result = result.add_extra(validate_min_value_less_than_max_value)
+ result = result.extend({cv.Optional(CONF_step, False): cv.float_})
+ if entity.auto_min_value is not None:
+ result = result.extend({cv.Optional(CONF_auto_min_value, False): cv.boolean})
+ if entity.auto_max_value is not None:
+ result = result.extend({cv.Optional(CONF_auto_max_value, False): cv.boolean})
+
+ return result
+
+
+def generate_setters(entity: cg.MockObj, conf: dict[str, Any]) -> None:
+ generate.add_property_set(entity, CONF_min_value, conf)
+ generate.add_property_set(entity, CONF_max_value, conf)
+ generate.add_property_set(entity, CONF_auto_min_value, conf)
+ generate.add_property_set(entity, CONF_auto_max_value, conf)
diff --git a/esphome/components/opentherm/number/__init__.py b/esphome/components/opentherm/number/__init__.py
new file mode 100644
index 0000000000..bbf3e87586
--- /dev/null
+++ b/esphome/components/opentherm/number/__init__.py
@@ -0,0 +1,74 @@
+from typing import Any
+
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from esphome.components import number
+from esphome.const import (
+ CONF_ID,
+ CONF_UNIT_OF_MEASUREMENT,
+ CONF_STEP,
+ CONF_INITIAL_VALUE,
+ CONF_RESTORE_VALUE,
+)
+from .. import const, schema, validate, input, generate
+
+DEPENDENCIES = [const.OPENTHERM]
+COMPONENT_TYPE = const.NUMBER
+
+OpenthermNumber = generate.opentherm_ns.class_(
+ "OpenthermNumber", number.Number, cg.Component, input.OpenthermInput
+)
+
+
+async def new_openthermnumber(config: dict[str, Any]) -> cg.Pvariable:
+ var = cg.new_Pvariable(config[CONF_ID])
+ await cg.register_component(var, config)
+ await number.register_number(
+ var,
+ config,
+ min_value=config[input.CONF_min_value],
+ max_value=config[input.CONF_max_value],
+ step=config[input.CONF_step],
+ )
+ input.generate_setters(var, config)
+
+ if CONF_INITIAL_VALUE in config:
+ cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
+ if CONF_RESTORE_VALUE in config:
+ cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
+
+ return var
+
+
+def get_entity_validation_schema(entity: schema.InputSchema) -> cv.Schema:
+ return (
+ number.NUMBER_SCHEMA.extend(
+ {
+ cv.GenerateID(): cv.declare_id(OpenthermNumber),
+ cv.Optional(
+ CONF_UNIT_OF_MEASUREMENT, entity.unit_of_measurement
+ ): cv.string_strict,
+ cv.Optional(CONF_STEP, entity.step): cv.float_,
+ cv.Optional(CONF_INITIAL_VALUE): cv.float_,
+ cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
+ }
+ )
+ .extend(input.input_schema(entity))
+ .extend(cv.COMPONENT_SCHEMA)
+ )
+
+
+CONFIG_SCHEMA = validate.create_component_schema(
+ schema.INPUTS, get_entity_validation_schema
+)
+
+
+async def to_code(config: dict[str, Any]) -> None:
+ keys = await generate.component_to_code(
+ COMPONENT_TYPE,
+ schema.INPUTS,
+ OpenthermNumber,
+ generate.create_only_conf(new_openthermnumber),
+ config,
+ )
+ generate.define_readers(COMPONENT_TYPE, keys)
diff --git a/esphome/components/opentherm/number/number.cpp b/esphome/components/opentherm/number/number.cpp
new file mode 100644
index 0000000000..d02b99ee9c
--- /dev/null
+++ b/esphome/components/opentherm/number/number.cpp
@@ -0,0 +1,40 @@
+#include "number.h"
+
+namespace esphome {
+namespace opentherm {
+
+static const char *const TAG = "opentherm.number";
+
+void OpenthermNumber::control(float value) {
+ this->publish_state(value);
+
+ if (this->restore_value_)
+ this->pref_.save(&value);
+}
+
+void OpenthermNumber::setup() {
+ float value;
+ if (!this->restore_value_) {
+ value = this->initial_value_;
+ } else {
+ this->pref_ = global_preferences->make_preference(this->get_object_id_hash());
+ if (!this->pref_.load(&value)) {
+ if (!std::isnan(this->initial_value_)) {
+ value = this->initial_value_;
+ } else {
+ value = this->traits.get_min_value();
+ }
+ }
+ }
+ this->publish_state(value);
+}
+
+void OpenthermNumber::dump_config() {
+ LOG_NUMBER("", "OpenTherm Number", this);
+ ESP_LOGCONFIG(TAG, " Restore value: %d", this->restore_value_);
+ ESP_LOGCONFIG(TAG, " Initial value: %.2f", this->initial_value_);
+ ESP_LOGCONFIG(TAG, " Current value: %.2f", this->state);
+}
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/number/number.h b/esphome/components/opentherm/number/number.h
new file mode 100644
index 0000000000..6f86072754
--- /dev/null
+++ b/esphome/components/opentherm/number/number.h
@@ -0,0 +1,31 @@
+#pragma once
+
+#include "esphome/components/number/number.h"
+#include "esphome/core/preferences.h"
+#include "esphome/core/log.h"
+#include "esphome/components/opentherm/input.h"
+
+namespace esphome {
+namespace opentherm {
+
+// Just a simple number, which stores the number
+class OpenthermNumber : public number::Number, public Component, public OpenthermInput {
+ protected:
+ void control(float value) override;
+ void setup() override;
+ void dump_config() override;
+
+ float initial_value_{NAN};
+ bool restore_value_{false};
+
+ ESPPreferenceObject pref_;
+
+ public:
+ void set_min_value(float min_value) override { this->traits.set_min_value(min_value); }
+ void set_max_value(float max_value) override { this->traits.set_max_value(max_value); }
+ void set_initial_value(float initial_value) { initial_value_ = initial_value; }
+ void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
+};
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/opentherm.cpp b/esphome/components/opentherm/opentherm.cpp
index b830cc01d3..26c707f9a0 100644
--- a/esphome/components/opentherm/opentherm.cpp
+++ b/esphome/components/opentherm/opentherm.cpp
@@ -283,6 +283,9 @@ bool OpenTherm::init_esp32_timer_() {
.clk_src = TIMER_SRC_CLK_DEFAULT,
#endif
.divider = 80,
+#if defined(SOC_TIMER_GROUP_SUPPORT_XTAL) && ESP_IDF_VERSION_MAJOR < 5
+ .clk_src = TIMER_SRC_CLK_APB
+#endif
};
esp_err_t result;
@@ -480,6 +483,8 @@ const char *OpenTherm::message_id_to_str(MessageId id) {
TO_STRING_MEMBER(EXHAUST_TEMP)
TO_STRING_MEMBER(FAN_SPEED)
TO_STRING_MEMBER(FLAME_CURRENT)
+ TO_STRING_MEMBER(ROOM_TEMP_CH2)
+ TO_STRING_MEMBER(REL_HUMIDITY)
TO_STRING_MEMBER(DHW_BOUNDS)
TO_STRING_MEMBER(CH_BOUNDS)
TO_STRING_MEMBER(OTC_CURVE_BOUNDS)
@@ -489,14 +494,39 @@ const char *OpenTherm::message_id_to_str(MessageId id) {
TO_STRING_MEMBER(HVAC_STATUS)
TO_STRING_MEMBER(REL_VENT_SETPOINT)
TO_STRING_MEMBER(DEVICE_VENT)
+ TO_STRING_MEMBER(HVAC_VER_ID)
TO_STRING_MEMBER(REL_VENTILATION)
TO_STRING_MEMBER(REL_HUMID_EXHAUST)
+ TO_STRING_MEMBER(EXHAUST_CO2)
TO_STRING_MEMBER(SUPPLY_INLET_TEMP)
TO_STRING_MEMBER(SUPPLY_OUTLET_TEMP)
TO_STRING_MEMBER(EXHAUST_INLET_TEMP)
TO_STRING_MEMBER(EXHAUST_OUTLET_TEMP)
+ TO_STRING_MEMBER(EXHAUST_FAN_SPEED)
+ TO_STRING_MEMBER(SUPPLY_FAN_SPEED)
+ TO_STRING_MEMBER(REMOTE_VENTILATION_PARAM)
TO_STRING_MEMBER(NOM_REL_VENTILATION)
+ TO_STRING_MEMBER(HVAC_NUM_TSP)
+ TO_STRING_MEMBER(HVAC_IDX_TSP)
+ TO_STRING_MEMBER(HVAC_FHB_SIZE)
+ TO_STRING_MEMBER(HVAC_FHB_IDX)
+ TO_STRING_MEMBER(RF_SIGNAL)
+ TO_STRING_MEMBER(DHW_MODE)
TO_STRING_MEMBER(OVERRIDE_FUNC)
+ TO_STRING_MEMBER(SOLAR_MODE_FLAGS)
+ TO_STRING_MEMBER(SOLAR_ASF)
+ TO_STRING_MEMBER(SOLAR_VERSION_ID)
+ TO_STRING_MEMBER(SOLAR_PRODUCT_ID)
+ TO_STRING_MEMBER(SOLAR_NUM_TSP)
+ TO_STRING_MEMBER(SOLAR_IDX_TSP)
+ TO_STRING_MEMBER(SOLAR_FHB_SIZE)
+ TO_STRING_MEMBER(SOLAR_FHB_IDX)
+ TO_STRING_MEMBER(SOLAR_STARTS)
+ TO_STRING_MEMBER(SOLAR_HOURS)
+ TO_STRING_MEMBER(SOLAR_ENERGY)
+ TO_STRING_MEMBER(SOLAR_TOTAL_ENERGY)
+ TO_STRING_MEMBER(FAILED_BURNER_STARTS)
+ TO_STRING_MEMBER(BURNER_FLAME_LOW)
TO_STRING_MEMBER(OEM_DIAGNOSTIC)
TO_STRING_MEMBER(BURNER_STARTS)
TO_STRING_MEMBER(CH_PUMP_STARTS)
diff --git a/esphome/components/opentherm/opentherm.h b/esphome/components/opentherm/opentherm.h
index 609cfb6243..85f4611125 100644
--- a/esphome/components/opentherm/opentherm.h
+++ b/esphome/components/opentherm/opentherm.h
@@ -20,7 +20,6 @@
namespace esphome {
namespace opentherm {
-// TODO: Account for immutable semantics change in hub.cpp when doing later installments of OpenTherm PR
template constexpr T read_bit(T value, uint8_t bit) { return (value >> bit) & 0x01; }
template constexpr T set_bit(T value, uint8_t bit) { return value |= (1UL << bit); }
@@ -28,7 +27,7 @@ template constexpr T set_bit(T value, uint8_t bit) { return value |= (1
template constexpr T clear_bit(T value, uint8_t bit) { return value &= ~(1UL << bit); }
template constexpr T write_bit(T value, uint8_t bit, uint8_t bit_value) {
- return bit_value ? setBit(value, bit) : clearBit(value, bit);
+ return bit_value ? set_bit(value, bit) : clear_bit(value, bit);
}
enum OperationMode {
@@ -100,6 +99,8 @@ enum MessageId {
EXHAUST_TEMP = 33,
FAN_SPEED = 35,
FLAME_CURRENT = 36,
+ ROOM_TEMP_CH2 = 37,
+ REL_HUMIDITY = 38,
DHW_BOUNDS = 48,
CH_BOUNDS = 49,
OTC_CURVE_BOUNDS = 50,
@@ -111,15 +112,46 @@ enum MessageId {
HVAC_STATUS = 70,
REL_VENT_SETPOINT = 71,
DEVICE_VENT = 74,
+ HVAC_VER_ID = 75,
REL_VENTILATION = 77,
REL_HUMID_EXHAUST = 78,
+ EXHAUST_CO2 = 79,
SUPPLY_INLET_TEMP = 80,
SUPPLY_OUTLET_TEMP = 81,
EXHAUST_INLET_TEMP = 82,
EXHAUST_OUTLET_TEMP = 83,
+ EXHAUST_FAN_SPEED = 84,
+ SUPPLY_FAN_SPEED = 85,
+ REMOTE_VENTILATION_PARAM = 86,
NOM_REL_VENTILATION = 87,
+ HVAC_NUM_TSP = 88,
+ HVAC_IDX_TSP = 89,
+ HVAC_FHB_SIZE = 90,
+ HVAC_FHB_IDX = 91,
+ RF_SIGNAL = 98,
+ DHW_MODE = 99,
OVERRIDE_FUNC = 100,
+
+ // Solar Specific Message IDs
+ SOLAR_MODE_FLAGS = 101, // hb0-2 Controller storage mode
+ // lb0 Device fault
+ // lb1-3 Device mode status
+ // lb4-5 Device status
+ SOLAR_ASF = 102,
+ SOLAR_VERSION_ID = 103,
+ SOLAR_PRODUCT_ID = 104,
+ SOLAR_NUM_TSP = 105,
+ SOLAR_IDX_TSP = 106,
+ SOLAR_FHB_SIZE = 107,
+ SOLAR_FHB_IDX = 108,
+ SOLAR_STARTS = 109,
+ SOLAR_HOURS = 110,
+ SOLAR_ENERGY = 111,
+ SOLAR_TOTAL_ENERGY = 112,
+
+ FAILED_BURNER_STARTS = 113,
+ BURNER_FLAME_LOW = 114,
OEM_DIAGNOSTIC = 115,
BURNER_STARTS = 116,
CH_PUMP_STARTS = 117,
diff --git a/esphome/components/opentherm/opentherm_macros.h b/esphome/components/opentherm/opentherm_macros.h
new file mode 100644
index 0000000000..8aaec0b48a
--- /dev/null
+++ b/esphome/components/opentherm/opentherm_macros.h
@@ -0,0 +1,151 @@
+#pragma once
+namespace esphome {
+namespace opentherm {
+
+// ===== hub.h macros =====
+
+// *_LIST macros will be generated in defines.h if at least one sensor from each platform is used.
+// These lists will look like this:
+// #define OPENTHERM_BINARY_SENSOR_LIST(F, sep) F(sensor_1) sep F(sensor_2)
+// These lists will be used in hub.h to define sensor fields (passing macros like OPENTHERM_DECLARE_SENSOR as F)
+// and setters (passing macros like OPENTHERM_SET_SENSOR as F) (see below)
+// In order for things not to break, we define empty lists here in case some platforms are not used in config.
+#ifndef OPENTHERM_SENSOR_LIST
+#define OPENTHERM_SENSOR_LIST(F, sep)
+#endif
+#ifndef OPENTHERM_BINARY_SENSOR_LIST
+#define OPENTHERM_BINARY_SENSOR_LIST(F, sep)
+#endif
+#ifndef OPENTHERM_SWITCH_LIST
+#define OPENTHERM_SWITCH_LIST(F, sep)
+#endif
+#ifndef OPENTHERM_NUMBER_LIST
+#define OPENTHERM_NUMBER_LIST(F, sep)
+#endif
+#ifndef OPENTHERM_OUTPUT_LIST
+#define OPENTHERM_OUTPUT_LIST(F, sep)
+#endif
+#ifndef OPENTHERM_INPUT_SENSOR_LIST
+#define OPENTHERM_INPUT_SENSOR_LIST(F, sep)
+#endif
+
+// Use macros to create fields for every entity specified in the ESPHome configuration
+#define OPENTHERM_DECLARE_SENSOR(entity) sensor::Sensor *entity;
+#define OPENTHERM_DECLARE_BINARY_SENSOR(entity) binary_sensor::BinarySensor *entity;
+#define OPENTHERM_DECLARE_SWITCH(entity) OpenthermSwitch *entity;
+#define OPENTHERM_DECLARE_NUMBER(entity) OpenthermNumber *entity;
+#define OPENTHERM_DECLARE_OUTPUT(entity) OpenthermOutput *entity;
+#define OPENTHERM_DECLARE_INPUT_SENSOR(entity) sensor::Sensor *entity;
+
+// Setter macros
+#define OPENTHERM_SET_SENSOR(entity) \
+ void set_##entity(sensor::Sensor *sensor) { this->entity = sensor; }
+
+#define OPENTHERM_SET_BINARY_SENSOR(entity) \
+ void set_##entity(binary_sensor::BinarySensor *binary_sensor) { this->entity = binary_sensor; }
+
+#define OPENTHERM_SET_SWITCH(entity) \
+ void set_##entity(OpenthermSwitch *sw) { this->entity = sw; }
+
+#define OPENTHERM_SET_NUMBER(entity) \
+ void set_##entity(OpenthermNumber *number) { this->entity = number; }
+
+#define OPENTHERM_SET_OUTPUT(entity) \
+ void set_##entity(OpenthermOutput *output) { this->entity = output; }
+
+#define OPENTHERM_SET_INPUT_SENSOR(entity) \
+ void set_##entity(sensor::Sensor *sensor) { this->entity = sensor; }
+
+// ===== hub.cpp macros =====
+
+// *_MESSAGE_HANDLERS are generated in defines.h and look like this:
+// OPENTHERM_NUMBER_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep) MESSAGE(COOLING_CONTROL)
+// ENTITY(cooling_control_number, f88) postscript msg_sep They contain placeholders for message part and entities parts,
+// since one message can contain multiple entities. MESSAGE part is substituted with OPENTHERM_MESSAGE_WRITE_MESSAGE,
+// OPENTHERM_MESSAGE_READ_MESSAGE or OPENTHERM_MESSAGE_RESPONSE_MESSAGE. ENTITY part is substituted with
+// OPENTHERM_MESSAGE_WRITE_ENTITY or OPENTHERM_MESSAGE_RESPONSE_ENTITY. OPENTHERM_IGNORE is used for sensor read
+// requests since no data needs to be sent or processed, just the data id.
+
+// In order for things not to break, we define empty lists here in case some platforms are not used in config.
+#ifndef OPENTHERM_SENSOR_MESSAGE_HANDLERS
+#define OPENTHERM_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
+#endif
+#ifndef OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS
+#define OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
+#endif
+#ifndef OPENTHERM_SWITCH_MESSAGE_HANDLERS
+#define OPENTHERM_SWITCH_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
+#endif
+#ifndef OPENTHERM_NUMBER_MESSAGE_HANDLERS
+#define OPENTHERM_NUMBER_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
+#endif
+#ifndef OPENTHERM_OUTPUT_MESSAGE_HANDLERS
+#define OPENTHERM_OUTPUT_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
+#endif
+#ifndef OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS
+#define OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
+#endif
+
+// Write data request builders
+#define OPENTHERM_MESSAGE_WRITE_MESSAGE(msg) \
+ case MessageId::msg: { \
+ data.type = MessageType::WRITE_DATA; \
+ data.id = request_id;
+#define OPENTHERM_MESSAGE_WRITE_ENTITY(key, msg_data) message_data::write_##msg_data(this->key->state, data);
+#define OPENTHERM_MESSAGE_WRITE_POSTSCRIPT \
+ return data; \
+ }
+
+// Read data request builder
+#define OPENTHERM_MESSAGE_READ_MESSAGE(msg) \
+ case MessageId::msg: \
+ data.type = MessageType::READ_DATA; \
+ data.id = request_id; \
+ return data;
+
+// Data processing builders
+#define OPENTHERM_MESSAGE_RESPONSE_MESSAGE(msg) case MessageId::msg:
+#define OPENTHERM_MESSAGE_RESPONSE_ENTITY(key, msg_data) this->key->publish_state(message_data::parse_##msg_data(data));
+#define OPENTHERM_MESSAGE_RESPONSE_POSTSCRIPT break;
+
+#define OPENTHERM_IGNORE(x, y)
+
+// Default macros for STATUS entities
+#ifndef OPENTHERM_READ_ch_enable
+#define OPENTHERM_READ_ch_enable true
+#endif
+#ifndef OPENTHERM_READ_dhw_enable
+#define OPENTHERM_READ_dhw_enable true
+#endif
+#ifndef OPENTHERM_READ_t_set
+#define OPENTHERM_READ_t_set 0.0
+#endif
+#ifndef OPENTHERM_READ_cooling_enable
+#define OPENTHERM_READ_cooling_enable false
+#endif
+#ifndef OPENTHERM_READ_cooling_control
+#define OPENTHERM_READ_cooling_control 0.0
+#endif
+#ifndef OPENTHERM_READ_otc_active
+#define OPENTHERM_READ_otc_active false
+#endif
+#ifndef OPENTHERM_READ_ch2_active
+#define OPENTHERM_READ_ch2_active false
+#endif
+#ifndef OPENTHERM_READ_t_set_ch2
+#define OPENTHERM_READ_t_set_ch2 0.0
+#endif
+#ifndef OPENTHERM_READ_summer_mode_active
+#define OPENTHERM_READ_summer_mode_active false
+#endif
+#ifndef OPENTHERM_READ_dhw_block
+#define OPENTHERM_READ_dhw_block false
+#endif
+
+// These macros utilize the structure of *_LIST macros in order
+#define ID(x) x
+#define SHOW_INNER(x) #x
+#define SHOW(x) SHOW_INNER(x)
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/output/__init__.py b/esphome/components/opentherm/output/__init__.py
new file mode 100644
index 0000000000..3a53c9d4f4
--- /dev/null
+++ b/esphome/components/opentherm/output/__init__.py
@@ -0,0 +1,47 @@
+from typing import Any
+
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from esphome.components import output
+from esphome.const import CONF_ID
+from .. import const, schema, validate, input, generate
+
+DEPENDENCIES = [const.OPENTHERM]
+COMPONENT_TYPE = const.OUTPUT
+
+OpenthermOutput = generate.opentherm_ns.class_(
+ "OpenthermOutput", output.FloatOutput, cg.Component, input.OpenthermInput
+)
+
+
+async def new_openthermoutput(
+ config: dict[str, Any], key: str, _hub: cg.MockObj
+) -> cg.Pvariable:
+ var = cg.new_Pvariable(config[CONF_ID])
+ await cg.register_component(var, config)
+ await output.register_output(var, config)
+ cg.add(getattr(var, "set_id")(cg.RawExpression(f'"{key}_{config[CONF_ID]}"')))
+ input.generate_setters(var, config)
+ return var
+
+
+def get_entity_validation_schema(entity: schema.InputSchema) -> cv.Schema:
+ return (
+ output.FLOAT_OUTPUT_SCHEMA.extend(
+ {cv.GenerateID(): cv.declare_id(OpenthermOutput)}
+ )
+ .extend(input.input_schema(entity))
+ .extend(cv.COMPONENT_SCHEMA)
+ )
+
+
+CONFIG_SCHEMA = validate.create_component_schema(
+ schema.INPUTS, get_entity_validation_schema
+)
+
+
+async def to_code(config: dict[str, Any]) -> None:
+ keys = await generate.component_to_code(
+ COMPONENT_TYPE, schema.INPUTS, OpenthermOutput, new_openthermoutput, config
+ )
+ generate.define_readers(COMPONENT_TYPE, keys)
diff --git a/esphome/components/opentherm/output/output.cpp b/esphome/components/opentherm/output/output.cpp
new file mode 100644
index 0000000000..f820dc76f1
--- /dev/null
+++ b/esphome/components/opentherm/output/output.cpp
@@ -0,0 +1,18 @@
+#include "esphome/core/helpers.h" // for clamp() and lerp()
+#include "output.h"
+
+namespace esphome {
+namespace opentherm {
+
+static const char *const TAG = "opentherm.output";
+
+void opentherm::OpenthermOutput::write_state(float state) {
+ ESP_LOGD(TAG, "Received state: %.2f. Min value: %.2f, max value: %.2f", state, min_value_, max_value_);
+ this->state = state < 0.003 && this->zero_means_zero_
+ ? 0.0
+ : clamp(lerp(state, min_value_, max_value_), min_value_, max_value_);
+ this->has_state_ = true;
+ ESP_LOGD(TAG, "Output %s set to %.2f", this->id_, this->state);
+}
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/output/output.h b/esphome/components/opentherm/output/output.h
new file mode 100644
index 0000000000..8d6a0ee4ba
--- /dev/null
+++ b/esphome/components/opentherm/output/output.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "esphome/components/output/float_output.h"
+#include "esphome/components/opentherm/input.h"
+#include "esphome/core/log.h"
+
+namespace esphome {
+namespace opentherm {
+
+class OpenthermOutput : public output::FloatOutput, public Component, public OpenthermInput {
+ protected:
+ bool has_state_ = false;
+ const char *id_ = nullptr;
+
+ float min_value_, max_value_;
+
+ public:
+ float state;
+
+ void set_id(const char *id) { this->id_ = id; }
+
+ void write_state(float state) override;
+
+ bool has_state() { return this->has_state_; };
+
+ void set_min_value(float min_value) override { this->min_value_ = min_value; }
+ void set_max_value(float max_value) override { this->max_value_ = max_value; }
+ float get_min_value() { return this->min_value_; }
+ float get_max_value() { return this->max_value_; }
+};
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/schema.py b/esphome/components/opentherm/schema.py
new file mode 100644
index 0000000000..fe0f2a77a3
--- /dev/null
+++ b/esphome/components/opentherm/schema.py
@@ -0,0 +1,814 @@
+# This file contains a schema for all supported sensors, binary sensors and
+# inputs of the OpenTherm component.
+
+from dataclasses import dataclass
+from typing import Optional, TypeVar
+
+from esphome.const import (
+ UNIT_CELSIUS,
+ UNIT_EMPTY,
+ UNIT_KILOWATT,
+ UNIT_MICROAMP,
+ UNIT_PERCENT,
+ UNIT_REVOLUTIONS_PER_MINUTE,
+ DEVICE_CLASS_COLD,
+ DEVICE_CLASS_CURRENT,
+ DEVICE_CLASS_EMPTY,
+ DEVICE_CLASS_HEAT,
+ DEVICE_CLASS_PRESSURE,
+ DEVICE_CLASS_PROBLEM,
+ DEVICE_CLASS_TEMPERATURE,
+ STATE_CLASS_MEASUREMENT,
+ STATE_CLASS_NONE,
+ STATE_CLASS_TOTAL_INCREASING,
+)
+
+
+@dataclass
+class EntitySchema:
+ description: str
+ """Description of the item, based on the OpenTherm spec"""
+
+ message: str
+ """OpenTherm message id used to read or write the value"""
+
+ keep_updated: bool
+ """Whether the value should be read or write repeatedly (True) or only during
+ the initialization phase (False)
+ """
+
+ message_data: str
+ """Instructions on how to interpret the data in the message
+ - flag8_[hb|lb]_[0-7]: data is a byte of single bit flags,
+ this flag is set in the high (hb) or low byte (lb),
+ at position 0 to 7
+ - u8_[hb|lb]: data is an unsigned 8-bit integer,
+ in the high (hb) or low byte (lb)
+ - s8_[hb|lb]: data is an signed 8-bit integer,
+ in the high (hb) or low byte (lb)
+ - f88: data is a signed fixed point value with
+ 1 sign bit, 7 integer bits, 8 fractional bits
+ - u16: data is an unsigned 16-bit integer
+ - s16: data is a signed 16-bit integer
+ """
+
+
+TSchema = TypeVar("TSchema", bound=EntitySchema)
+
+
+@dataclass
+class SensorSchema(EntitySchema):
+ accuracy_decimals: int
+ state_class: str
+ unit_of_measurement: Optional[str] = None
+ icon: Optional[str] = None
+ device_class: Optional[str] = None
+ disabled_by_default: bool = False
+
+
+SENSORS: dict[str, SensorSchema] = {
+ "rel_mod_level": SensorSchema(
+ description="Relative modulation level",
+ unit_of_measurement=UNIT_PERCENT,
+ accuracy_decimals=2,
+ icon="mdi:percent",
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="MODULATION_LEVEL",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "ch_pressure": SensorSchema(
+ description="Water pressure in CH circuit",
+ unit_of_measurement="bar",
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_PRESSURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="CH_WATER_PRESSURE",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "dhw_flow_rate": SensorSchema(
+ description="Water flow rate in DHW circuit",
+ unit_of_measurement="l/min",
+ accuracy_decimals=2,
+ icon="mdi:waves-arrow-right",
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="DHW_FLOW_RATE",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_boiler": SensorSchema(
+ description="Boiler water temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="FEED_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_dhw": SensorSchema(
+ description="DHW temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="DHW_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_outside": SensorSchema(
+ description="Outside temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="OUTSIDE_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_ret": SensorSchema(
+ description="Return water temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="RETURN_WATER_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_storage": SensorSchema(
+ description="Solar storage temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="SOLAR_STORE_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_collector": SensorSchema(
+ description="Solar collector temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="SOLAR_COLLECT_TEMP",
+ keep_updated=True,
+ message_data="s16",
+ ),
+ "t_flow_ch2": SensorSchema(
+ description="Flow water temperature CH2 circuit",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="FEED_TEMP_CH2",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_dhw2": SensorSchema(
+ description="Domestic hot water temperature 2",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="DHW2_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "t_exhaust": SensorSchema(
+ description="Boiler exhaust temperature",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="EXHAUST_TEMP",
+ keep_updated=True,
+ message_data="s16",
+ ),
+ "fan_speed": SensorSchema(
+ description="Boiler fan speed",
+ unit_of_measurement=UNIT_REVOLUTIONS_PER_MINUTE,
+ accuracy_decimals=0,
+ icon="mdi:fan",
+ device_class=DEVICE_CLASS_EMPTY,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="FAN_SPEED",
+ keep_updated=True,
+ message_data="u8_lb_60",
+ ),
+ "fan_speed_setpoint": SensorSchema(
+ description="Boiler fan speed setpoint",
+ unit_of_measurement=UNIT_REVOLUTIONS_PER_MINUTE,
+ accuracy_decimals=0,
+ icon="mdi:fan",
+ device_class=DEVICE_CLASS_EMPTY,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="FAN_SPEED",
+ keep_updated=True,
+ message_data="u8_hb_60",
+ ),
+ "flame_current": SensorSchema(
+ description="Boiler flame current",
+ unit_of_measurement=UNIT_MICROAMP,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_CURRENT,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="FLAME_CURRENT",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "burner_starts": SensorSchema(
+ description="Number of starts burner",
+ accuracy_decimals=0,
+ icon="mdi:gas-burner",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="BURNER_STARTS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "ch_pump_starts": SensorSchema(
+ description="Number of starts CH pump",
+ accuracy_decimals=0,
+ icon="mdi:pump",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="CH_PUMP_STARTS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "dhw_pump_valve_starts": SensorSchema(
+ description="Number of starts DHW pump/valve",
+ accuracy_decimals=0,
+ icon="mdi:water-pump",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="DHW_PUMP_STARTS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "dhw_burner_starts": SensorSchema(
+ description="Number of starts burner during DHW mode",
+ accuracy_decimals=0,
+ icon="mdi:gas-burner",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="DHW_BURNER_STARTS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "burner_operation_hours": SensorSchema(
+ description="Number of hours that burner is in operation",
+ accuracy_decimals=0,
+ icon="mdi:clock-outline",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="BURNER_HOURS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "ch_pump_operation_hours": SensorSchema(
+ description="Number of hours that CH pump has been running",
+ accuracy_decimals=0,
+ icon="mdi:clock-outline",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="CH_PUMP_HOURS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "dhw_pump_valve_operation_hours": SensorSchema(
+ description="Number of hours that DHW pump has been running or DHW valve has been opened",
+ accuracy_decimals=0,
+ icon="mdi:clock-outline",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="DHW_PUMP_HOURS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "dhw_burner_operation_hours": SensorSchema(
+ description="Number of hours that burner is in operation during DHW mode",
+ accuracy_decimals=0,
+ icon="mdi:clock-outline",
+ state_class=STATE_CLASS_TOTAL_INCREASING,
+ message="DHW_BURNER_HOURS",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "t_dhw_set_ub": SensorSchema(
+ description="Upper bound for adjustment of DHW setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="DHW_BOUNDS",
+ keep_updated=False,
+ message_data="s8_hb",
+ ),
+ "t_dhw_set_lb": SensorSchema(
+ description="Lower bound for adjustment of DHW setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="DHW_BOUNDS",
+ keep_updated=False,
+ message_data="s8_lb",
+ ),
+ "max_t_set_ub": SensorSchema(
+ description="Upper bound for adjustment of max CH setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="CH_BOUNDS",
+ keep_updated=False,
+ message_data="s8_hb",
+ ),
+ "max_t_set_lb": SensorSchema(
+ description="Lower bound for adjustment of max CH setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=0,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="CH_BOUNDS",
+ keep_updated=False,
+ message_data="s8_lb",
+ ),
+ "t_dhw_set": SensorSchema(
+ description="Domestic hot water temperature setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="DHW_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "max_t_set": SensorSchema(
+ description="Maximum allowable CH water setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ accuracy_decimals=2,
+ device_class=DEVICE_CLASS_TEMPERATURE,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="MAX_CH_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ ),
+ "oem_fault_code": SensorSchema(
+ description="OEM fault code",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="u8_lb",
+ ),
+ "oem_diagnostic_code": SensorSchema(
+ description="OEM diagnostic code",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ message="OEM_DIAGNOSTIC",
+ keep_updated=True,
+ message_data="u16",
+ ),
+ "max_capacity": SensorSchema(
+ description="Maximum boiler capacity (KW)",
+ unit_of_measurement=UNIT_KILOWATT,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_MEASUREMENT,
+ disabled_by_default=True,
+ message="MAX_BOILER_CAPACITY",
+ keep_updated=False,
+ message_data="u8_hb",
+ ),
+ "min_mod_level": SensorSchema(
+ description="Minimum modulation level",
+ unit_of_measurement=UNIT_PERCENT,
+ accuracy_decimals=0,
+ icon="mdi:percent",
+ disabled_by_default=True,
+ state_class=STATE_CLASS_MEASUREMENT,
+ message="MAX_BOILER_CAPACITY",
+ keep_updated=False,
+ message_data="u8_lb",
+ ),
+ "opentherm_version_device": SensorSchema(
+ description="Version of OpenTherm implemented by device",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ disabled_by_default=True,
+ message="OT_VERSION_DEVICE",
+ keep_updated=False,
+ message_data="f88",
+ ),
+ "device_type": SensorSchema(
+ description="Device product type",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ disabled_by_default=True,
+ message="VERSION_DEVICE",
+ keep_updated=False,
+ message_data="u8_hb",
+ ),
+ "device_version": SensorSchema(
+ description="Device product version",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ disabled_by_default=True,
+ message="VERSION_DEVICE",
+ keep_updated=False,
+ message_data="u8_lb",
+ ),
+ "device_id": SensorSchema(
+ description="Device ID code",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ disabled_by_default=True,
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="u8_lb",
+ ),
+ "otc_hc_ratio_ub": SensorSchema(
+ description="OTC heat curve ratio upper bound",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ disabled_by_default=True,
+ message="OTC_CURVE_BOUNDS",
+ keep_updated=False,
+ message_data="u8_hb",
+ ),
+ "otc_hc_ratio_lb": SensorSchema(
+ description="OTC heat curve ratio lower bound",
+ unit_of_measurement=UNIT_EMPTY,
+ accuracy_decimals=0,
+ state_class=STATE_CLASS_NONE,
+ disabled_by_default=True,
+ message="OTC_CURVE_BOUNDS",
+ keep_updated=False,
+ message_data="u8_lb",
+ ),
+}
+
+
+@dataclass
+class BinarySensorSchema(EntitySchema):
+ icon: Optional[str] = None
+ device_class: Optional[str] = None
+
+
+BINARY_SENSORS: dict[str, BinarySensorSchema] = {
+ "fault_indication": BinarySensorSchema(
+ description="Status: Fault indication",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_0",
+ ),
+ "ch_active": BinarySensorSchema(
+ description="Status: Central Heating active",
+ device_class=DEVICE_CLASS_HEAT,
+ icon="mdi:radiator",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_1",
+ ),
+ "dhw_active": BinarySensorSchema(
+ description="Status: Domestic Hot Water active",
+ device_class=DEVICE_CLASS_HEAT,
+ icon="mdi:faucet",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_2",
+ ),
+ "flame_on": BinarySensorSchema(
+ description="Status: Flame on",
+ device_class=DEVICE_CLASS_HEAT,
+ icon="mdi:fire",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_3",
+ ),
+ "cooling_active": BinarySensorSchema(
+ description="Status: Cooling active",
+ device_class=DEVICE_CLASS_COLD,
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_4",
+ ),
+ "ch2_active": BinarySensorSchema(
+ description="Status: Central Heating 2 active",
+ device_class=DEVICE_CLASS_HEAT,
+ icon="mdi:radiator",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_5",
+ ),
+ "diagnostic_indication": BinarySensorSchema(
+ description="Status: Diagnostic event",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_6",
+ ),
+ "electricity_production": BinarySensorSchema(
+ description="Status: Electricity production",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_lb_7",
+ ),
+ "dhw_present": BinarySensorSchema(
+ description="Configuration: DHW present",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_0",
+ ),
+ "control_type_on_off": BinarySensorSchema(
+ description="Configuration: Control type is on/off",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_1",
+ ),
+ "cooling_supported": BinarySensorSchema(
+ description="Configuration: Cooling supported",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_2",
+ ),
+ "dhw_storage_tank": BinarySensorSchema(
+ description="Configuration: DHW storage tank",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_3",
+ ),
+ "controller_pump_control_allowed": BinarySensorSchema(
+ description="Configuration: Controller pump control allowed",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_4",
+ ),
+ "ch2_present": BinarySensorSchema(
+ description="Configuration: CH2 present",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_5",
+ ),
+ "water_filling": BinarySensorSchema(
+ description="Configuration: Remote water filling",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_6",
+ ),
+ "heat_mode": BinarySensorSchema(
+ description="Configuration: Heating or cooling",
+ message="DEVICE_CONFIG",
+ keep_updated=False,
+ message_data="flag8_hb_7",
+ ),
+ "dhw_setpoint_transfer_enabled": BinarySensorSchema(
+ description="Remote boiler parameters: DHW setpoint transfer enabled",
+ message="REMOTE",
+ keep_updated=False,
+ message_data="flag8_hb_0",
+ ),
+ "max_ch_setpoint_transfer_enabled": BinarySensorSchema(
+ description="Remote boiler parameters: CH maximum setpoint transfer enabled",
+ message="REMOTE",
+ keep_updated=False,
+ message_data="flag8_hb_1",
+ ),
+ "dhw_setpoint_rw": BinarySensorSchema(
+ description="Remote boiler parameters: DHW setpoint read/write",
+ message="REMOTE",
+ keep_updated=False,
+ message_data="flag8_lb_0",
+ ),
+ "max_ch_setpoint_rw": BinarySensorSchema(
+ description="Remote boiler parameters: CH maximum setpoint read/write",
+ message="REMOTE",
+ keep_updated=False,
+ message_data="flag8_lb_1",
+ ),
+ "service_request": BinarySensorSchema(
+ description="Service required",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="flag8_hb_0",
+ ),
+ "lockout_reset": BinarySensorSchema(
+ description="Lockout Reset",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="flag8_hb_1",
+ ),
+ "low_water_pressure": BinarySensorSchema(
+ description="Low water pressure fault",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="flag8_hb_2",
+ ),
+ "flame_fault": BinarySensorSchema(
+ description="Flame fault",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="flag8_hb_3",
+ ),
+ "air_pressure_fault": BinarySensorSchema(
+ description="Air pressure fault",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="flag8_hb_4",
+ ),
+ "water_over_temp": BinarySensorSchema(
+ description="Water overtemperature",
+ device_class=DEVICE_CLASS_PROBLEM,
+ message="FAULT_FLAGS",
+ keep_updated=True,
+ message_data="flag8_hb_5",
+ ),
+}
+
+
+@dataclass
+class SwitchSchema(EntitySchema):
+ default_mode: Optional[str] = None
+
+
+SWITCHES: dict[str, SwitchSchema] = {
+ "ch_enable": SwitchSchema(
+ description="Central Heating enabled",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_0",
+ default_mode="restore_default_off",
+ ),
+ "dhw_enable": SwitchSchema(
+ description="Domestic Hot Water enabled",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_1",
+ default_mode="restore_default_off",
+ ),
+ "cooling_enable": SwitchSchema(
+ description="Cooling enabled",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_2",
+ default_mode="restore_default_off",
+ ),
+ "otc_active": SwitchSchema(
+ description="Outside temperature compensation active",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_3",
+ default_mode="restore_default_off",
+ ),
+ "ch2_active": SwitchSchema(
+ description="Central Heating 2 active",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_4",
+ default_mode="restore_default_off",
+ ),
+ "summer_mode_active": SwitchSchema(
+ description="Summer mode active",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_5",
+ default_mode="restore_default_off",
+ ),
+ "dhw_block": SwitchSchema(
+ description="DHW blocked",
+ message="STATUS",
+ keep_updated=True,
+ message_data="flag8_hb_6",
+ default_mode="restore_default_off",
+ ),
+}
+
+
+@dataclass
+class AutoConfigure:
+ message: str
+ message_data: str
+
+
+@dataclass
+class InputSchema(EntitySchema):
+ unit_of_measurement: str
+ step: float
+ range: tuple[int, int]
+ icon: Optional[str] = None
+ auto_max_value: Optional[AutoConfigure] = None
+ auto_min_value: Optional[AutoConfigure] = None
+
+
+INPUTS: dict[str, InputSchema] = {
+ "t_set": InputSchema(
+ description="Control setpoint: temperature setpoint for the boiler's supply water",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="CH_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 100),
+ auto_max_value=AutoConfigure(message="MAX_CH_SETPOINT", message_data="f88"),
+ ),
+ "t_set_ch2": InputSchema(
+ description="Control setpoint 2: temperature setpoint for the boiler's supply water on the second heating circuit",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="CH2_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 100),
+ auto_max_value=AutoConfigure(message="MAX_CH_SETPOINT", message_data="f88"),
+ ),
+ "cooling_control": InputSchema(
+ description="Cooling control signal",
+ unit_of_measurement=UNIT_PERCENT,
+ step=1.0,
+ message="COOLING_CONTROL",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 100),
+ ),
+ "t_dhw_set": InputSchema(
+ description="Domestic hot water temperature setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="DHW_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 127),
+ auto_min_value=AutoConfigure(message="DHW_BOUNDS", message_data="s8_lb"),
+ auto_max_value=AutoConfigure(message="DHW_BOUNDS", message_data="s8_hb"),
+ ),
+ "max_t_set": InputSchema(
+ description="Maximum allowable CH water setpoint",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="MAX_CH_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 127),
+ auto_min_value=AutoConfigure(message="CH_BOUNDS", message_data="s8_lb"),
+ auto_max_value=AutoConfigure(message="CH_BOUNDS", message_data="s8_hb"),
+ ),
+ "t_room_set": InputSchema(
+ description="Current room temperature setpoint (informational)",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="ROOM_SETPOINT",
+ keep_updated=True,
+ message_data="f88",
+ range=(-40, 127),
+ ),
+ "t_room_set_ch2": InputSchema(
+ description="Current room temperature setpoint on CH2 (informational)",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="ROOM_SETPOINT_CH2",
+ keep_updated=True,
+ message_data="f88",
+ range=(-40, 127),
+ ),
+ "t_room": InputSchema(
+ description="Current sensed room temperature (informational)",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="ROOM_TEMP",
+ keep_updated=True,
+ message_data="f88",
+ range=(-40, 127),
+ ),
+ "max_rel_mod_level": InputSchema(
+ description="Maximum relative modulation level",
+ unit_of_measurement=UNIT_PERCENT,
+ step=1,
+ icon="mdi:percent",
+ message="MAX_MODULATION_LEVEL",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 100),
+ ),
+ "otc_hc_ratio": InputSchema(
+ description="OTC heat curve ratio",
+ unit_of_measurement=UNIT_CELSIUS,
+ step=0.1,
+ message="OTC_CURVE_RATIO",
+ keep_updated=True,
+ message_data="f88",
+ range=(0, 127),
+ auto_min_value=AutoConfigure(message="OTC_CURVE_BOUNDS", message_data="u8_lb"),
+ auto_max_value=AutoConfigure(message="OTC_CURVE_BOUNDS", message_data="u8_hb"),
+ ),
+}
diff --git a/esphome/components/opentherm/sensor/__init__.py b/esphome/components/opentherm/sensor/__init__.py
new file mode 100644
index 0000000000..546a79054b
--- /dev/null
+++ b/esphome/components/opentherm/sensor/__init__.py
@@ -0,0 +1,51 @@
+from typing import Any
+
+import esphome.config_validation as cv
+from esphome.components import sensor
+from .. import const, schema, validate, generate
+
+DEPENDENCIES = [const.OPENTHERM]
+COMPONENT_TYPE = const.SENSOR
+
+MSG_DATA_TYPES = {
+ "u8_lb",
+ "u8_hb",
+ "s8_lb",
+ "s8_hb",
+ "u8_lb_60",
+ "u8_hb_60",
+ "u16",
+ "s16",
+ "f88",
+}
+
+
+def get_entity_validation_schema(entity: schema.SensorSchema) -> cv.Schema:
+ return sensor.sensor_schema(
+ unit_of_measurement=entity.unit_of_measurement
+ or sensor._UNDEF, # pylint: disable=protected-access
+ accuracy_decimals=entity.accuracy_decimals,
+ device_class=entity.device_class
+ or sensor._UNDEF, # pylint: disable=protected-access
+ icon=entity.icon or sensor._UNDEF, # pylint: disable=protected-access
+ state_class=entity.state_class,
+ ).extend(
+ {
+ cv.Optional(const.CONF_DATA_TYPE): cv.one_of(*MSG_DATA_TYPES),
+ }
+ )
+
+
+CONFIG_SCHEMA = validate.create_component_schema(
+ schema.SENSORS, get_entity_validation_schema
+)
+
+
+async def to_code(config: dict[str, Any]) -> None:
+ await generate.component_to_code(
+ COMPONENT_TYPE,
+ schema.SENSORS,
+ sensor.Sensor,
+ generate.create_only_conf(sensor.new_sensor),
+ config,
+ )
diff --git a/esphome/components/opentherm/switch/__init__.py b/esphome/components/opentherm/switch/__init__.py
new file mode 100644
index 0000000000..94ec25e36c
--- /dev/null
+++ b/esphome/components/opentherm/switch/__init__.py
@@ -0,0 +1,43 @@
+from typing import Any
+
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from esphome.components import switch
+from esphome.const import CONF_ID
+from .. import const, schema, validate, generate
+
+DEPENDENCIES = [const.OPENTHERM]
+COMPONENT_TYPE = const.SWITCH
+
+OpenthermSwitch = generate.opentherm_ns.class_(
+ "OpenthermSwitch", switch.Switch, cg.Component
+)
+
+
+async def new_openthermswitch(config: dict[str, Any]) -> cg.Pvariable:
+ var = cg.new_Pvariable(config[CONF_ID])
+ await cg.register_component(var, config)
+ await switch.register_switch(var, config)
+ return var
+
+
+def get_entity_validation_schema(entity: schema.SwitchSchema) -> cv.Schema:
+ return switch.SWITCH_SCHEMA.extend(
+ {cv.GenerateID(): cv.declare_id(OpenthermSwitch)}
+ ).extend(cv.COMPONENT_SCHEMA)
+
+
+CONFIG_SCHEMA = validate.create_component_schema(
+ schema.SWITCHES, get_entity_validation_schema
+)
+
+
+async def to_code(config: dict[str, Any]) -> None:
+ keys = await generate.component_to_code(
+ COMPONENT_TYPE,
+ schema.SWITCHES,
+ OpenthermSwitch,
+ generate.create_only_conf(new_openthermswitch),
+ config,
+ )
+ generate.define_readers(COMPONENT_TYPE, keys)
diff --git a/esphome/components/opentherm/switch/switch.cpp b/esphome/components/opentherm/switch/switch.cpp
new file mode 100644
index 0000000000..228d9ac8f3
--- /dev/null
+++ b/esphome/components/opentherm/switch/switch.cpp
@@ -0,0 +1,28 @@
+#include "switch.h"
+
+namespace esphome {
+namespace opentherm {
+
+static const char *const TAG = "opentherm.switch";
+
+void OpenthermSwitch::write_state(bool state) { this->publish_state(state); }
+
+void OpenthermSwitch::setup() {
+ auto restored = this->get_initial_state_with_restore_mode();
+ bool state = false;
+ if (!restored.has_value()) {
+ ESP_LOGD(TAG, "Couldn't restore state for OpenTherm switch '%s'", this->get_name().c_str());
+ } else {
+ ESP_LOGD(TAG, "Restored state for OpenTherm switch '%s': %d", this->get_name().c_str(), restored.value());
+ state = restored.value();
+ }
+ this->write_state(state);
+}
+
+void OpenthermSwitch::dump_config() {
+ LOG_SWITCH("", "OpenTherm Switch", this);
+ ESP_LOGCONFIG(TAG, " Current state: %d", this->state);
+}
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/switch/switch.h b/esphome/components/opentherm/switch/switch.h
new file mode 100644
index 0000000000..0c20a0d9ed
--- /dev/null
+++ b/esphome/components/opentherm/switch/switch.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include "esphome/core/component.h"
+#include "esphome/components/switch/switch.h"
+#include "esphome/core/log.h"
+
+namespace esphome {
+namespace opentherm {
+
+class OpenthermSwitch : public switch_::Switch, public Component {
+ protected:
+ void write_state(bool state) override;
+
+ public:
+ void setup() override;
+ void dump_config() override;
+};
+
+} // namespace opentherm
+} // namespace esphome
diff --git a/esphome/components/opentherm/validate.py b/esphome/components/opentherm/validate.py
new file mode 100644
index 0000000000..d4507672a5
--- /dev/null
+++ b/esphome/components/opentherm/validate.py
@@ -0,0 +1,31 @@
+from typing import Callable
+
+from voluptuous import Schema
+
+import esphome.config_validation as cv
+
+from . import const, schema, generate
+from .schema import TSchema
+
+
+def create_entities_schema(
+ entities: dict[str, schema.EntitySchema],
+ get_entity_validation_schema: Callable[[TSchema], cv.Schema],
+) -> Schema:
+ entity_schema = {}
+ for key, entity in entities.items():
+ entity_schema[cv.Optional(key)] = get_entity_validation_schema(entity)
+ return cv.Schema(entity_schema)
+
+
+def create_component_schema(
+ entities: dict[str, schema.EntitySchema],
+ get_entity_validation_schema: Callable[[TSchema], cv.Schema],
+) -> Schema:
+ return (
+ cv.Schema(
+ {cv.GenerateID(const.CONF_OPENTHERM_ID): cv.use_id(generate.OpenthermHub)}
+ )
+ .extend(create_entities_schema(entities, get_entity_validation_schema))
+ .extend(cv.COMPONENT_SCHEMA)
+ )
diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py
index d9917a2aae..627c55e910 100644
--- a/esphome/components/ota/__init__.py
+++ b/esphome/components/ota/__init__.py
@@ -92,6 +92,7 @@ async def to_code(config):
async def ota_to_code(var, config):
+ await cg.past_safe_mode()
use_state_callback = False
for conf in config.get(CONF_ON_STATE_CHANGE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
diff --git a/esphome/components/pcd8544/display.py b/esphome/components/pcd8544/display.py
index d7e72d1c81..2c24b133da 100644
--- a/esphome/components/pcd8544/display.py
+++ b/esphome/components/pcd8544/display.py
@@ -1,15 +1,15 @@
-import esphome.codegen as cg
-import esphome.config_validation as cv
from esphome import pins
+import esphome.codegen as cg
from esphome.components import display, spi
+import esphome.config_validation as cv
from esphome.const import (
+ CONF_CONTRAST,
+ CONF_CS_PIN,
CONF_DC_PIN,
CONF_ID,
CONF_LAMBDA,
CONF_PAGES,
CONF_RESET_PIN,
- CONF_CS_PIN,
- CONF_CONTRAST,
)
DEPENDENCIES = ["spi"]
@@ -35,6 +35,10 @@ CONFIG_SCHEMA = cv.All(
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA),
)
+FINAL_VALIDATE_SCHEMA = spi.final_validate_device_schema(
+ "pcd8544", require_miso=False, require_mosi=True
+)
+
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
diff --git a/esphome/components/prometheus/prometheus_handler.cpp b/esphome/components/prometheus/prometheus_handler.cpp
index 3e9cf81e6e..5d1861202a 100644
--- a/esphome/components/prometheus/prometheus_handler.cpp
+++ b/esphome/components/prometheus/prometheus_handler.cpp
@@ -7,47 +7,56 @@ namespace prometheus {
void PrometheusHandler::handleRequest(AsyncWebServerRequest *req) {
AsyncResponseStream *stream = req->beginResponseStream("text/plain; version=0.0.4; charset=utf-8");
+ std::string area = App.get_area();
+ std::string node = App.get_name();
+ std::string friendly_name = App.get_friendly_name();
#ifdef USE_SENSOR
this->sensor_type_(stream);
for (auto *obj : App.get_sensors())
- this->sensor_row_(stream, obj);
+ this->sensor_row_(stream, obj, area, node, friendly_name);
#endif
#ifdef USE_BINARY_SENSOR
this->binary_sensor_type_(stream);
for (auto *obj : App.get_binary_sensors())
- this->binary_sensor_row_(stream, obj);
+ this->binary_sensor_row_(stream, obj, area, node, friendly_name);
#endif
#ifdef USE_FAN
this->fan_type_(stream);
for (auto *obj : App.get_fans())
- this->fan_row_(stream, obj);
+ this->fan_row_(stream, obj, area, node, friendly_name);
#endif
#ifdef USE_LIGHT
this->light_type_(stream);
for (auto *obj : App.get_lights())
- this->light_row_(stream, obj);
+ this->light_row_(stream, obj, area, node, friendly_name);
#endif
#ifdef USE_COVER
this->cover_type_(stream);
for (auto *obj : App.get_covers())
- this->cover_row_(stream, obj);
+ this->cover_row_(stream, obj, area, node, friendly_name);
#endif
#ifdef USE_SWITCH
this->switch_type_(stream);
for (auto *obj : App.get_switches())
- this->switch_row_(stream, obj);
+ this->switch_row_(stream, obj, area, node, friendly_name);
#endif
#ifdef USE_LOCK
this->lock_type_(stream);
for (auto *obj : App.get_locks())
- this->lock_row_(stream, obj);
+ this->lock_row_(stream, obj, area, node, friendly_name);
+#endif
+
+#ifdef USE_TEXT_SENSOR
+ this->text_sensor_type_(stream);
+ for (auto *obj : App.get_text_sensors())
+ this->text_sensor_row_(stream, obj, area, node, friendly_name);
#endif
req->send(stream);
@@ -63,25 +72,53 @@ std::string PrometheusHandler::relabel_name_(EntityBase *obj) {
return item == relabel_map_name_.end() ? obj->get_name() : item->second;
}
+void PrometheusHandler::add_area_label_(AsyncResponseStream *stream, std::string &area) {
+ if (!area.empty()) {
+ stream->print(F("\",area=\""));
+ stream->print(area.c_str());
+ }
+}
+
+void PrometheusHandler::add_node_label_(AsyncResponseStream *stream, std::string &node) {
+ if (!node.empty()) {
+ stream->print(F("\",node=\""));
+ stream->print(node.c_str());
+ }
+}
+
+void PrometheusHandler::add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name) {
+ if (!friendly_name.empty()) {
+ stream->print(F("\",friendly_name=\""));
+ stream->print(friendly_name.c_str());
+ }
+}
+
// Type-specific implementation
#ifdef USE_SENSOR
void PrometheusHandler::sensor_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_sensor_value gauge\n"));
stream->print(F("#TYPE esphome_sensor_failed gauge\n"));
}
-void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj) {
+void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area,
+ std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
if (!std::isnan(obj->state)) {
// We have a valid value, output this value
stream->print(F("esphome_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_sensor_value{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",unit=\""));
@@ -93,6 +130,9 @@ void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor
// Invalid state
stream->print(F("esphome_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n"));
@@ -106,19 +146,26 @@ void PrometheusHandler::binary_sensor_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_binary_sensor_value gauge\n"));
stream->print(F("#TYPE esphome_binary_sensor_failed gauge\n"));
}
-void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj) {
+void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj,
+ std::string &area, std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
if (obj->has_state()) {
// We have a valid value, output this value
stream->print(F("esphome_binary_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_binary_sensor_value{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -128,6 +175,9 @@ void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_s
// Invalid state
stream->print(F("esphome_binary_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n"));
@@ -142,17 +192,24 @@ void PrometheusHandler::fan_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_fan_speed gauge\n"));
stream->print(F("#TYPE esphome_fan_oscillation gauge\n"));
}
-void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) {
+void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
+ std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
stream->print(F("esphome_fan_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_fan_value{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -162,6 +219,9 @@ void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) {
if (obj->get_traits().supports_speed()) {
stream->print(F("esphome_fan_speed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -172,6 +232,9 @@ void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) {
if (obj->get_traits().supports_oscillation()) {
stream->print(F("esphome_fan_oscillation{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -187,12 +250,16 @@ void PrometheusHandler::light_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_light_color gauge\n"));
stream->print(F("#TYPE esphome_light_effect_active gauge\n"));
}
-void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightState *obj) {
+void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area,
+ std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
// State
stream->print(F("esphome_light_state{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -205,6 +272,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
color.as_rgbw(&r, &g, &b, &w);
stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"brightness\"} "));
@@ -212,6 +282,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"r\"} "));
@@ -219,6 +292,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"g\"} "));
@@ -226,6 +302,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"b\"} "));
@@ -233,6 +312,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"w\"} "));
@@ -243,12 +325,18 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
if (effect == "None") {
stream->print(F("esphome_light_effect_active{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",effect=\"None\"} 0\n"));
} else {
stream->print(F("esphome_light_effect_active{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\",effect=\""));
@@ -263,19 +351,26 @@ void PrometheusHandler::cover_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_cover_value gauge\n"));
stream->print(F("#TYPE esphome_cover_failed gauge\n"));
}
-void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *obj) {
+void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
+ std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
if (!std::isnan(obj->position)) {
// We have a valid value, output this value
stream->print(F("esphome_cover_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_cover_value{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -284,6 +379,9 @@ void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *ob
if (obj->get_traits().get_supports_tilt()) {
stream->print(F("esphome_cover_tilt{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -294,6 +392,9 @@ void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *ob
// Invalid state
stream->print(F("esphome_cover_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n"));
@@ -306,17 +407,24 @@ void PrometheusHandler::switch_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_switch_value gauge\n"));
stream->print(F("#TYPE esphome_switch_failed gauge\n"));
}
-void PrometheusHandler::switch_row_(AsyncResponseStream *stream, switch_::Switch *obj) {
+void PrometheusHandler::switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area,
+ std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
stream->print(F("esphome_switch_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_switch_value{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -330,17 +438,24 @@ void PrometheusHandler::lock_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_lock_value gauge\n"));
stream->print(F("#TYPE esphome_lock_failed gauge\n"));
}
-void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj) {
+void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
+ std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
stream->print(F("esphome_lock_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n"));
// Data itself
stream->print(F("esphome_lock_value{id=\""));
stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} "));
@@ -349,6 +464,53 @@ void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj)
}
#endif
+// Type-specific implementation
+#ifdef USE_TEXT_SENSOR
+void PrometheusHandler::text_sensor_type_(AsyncResponseStream *stream) {
+ stream->print(F("#TYPE esphome_text_sensor_value gauge\n"));
+ stream->print(F("#TYPE esphome_text_sensor_failed gauge\n"));
+}
+void PrometheusHandler::text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area,
+ std::string &node, std::string &friendly_name) {
+ if (obj->is_internal() && !this->include_internal_)
+ return;
+ if (obj->has_state()) {
+ // We have a valid value, output this value
+ stream->print(F("esphome_text_sensor_failed{id=\""));
+ stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
+ stream->print(F("\",name=\""));
+ stream->print(relabel_name_(obj).c_str());
+ stream->print(F("\"} 0\n"));
+ // Data itself
+ stream->print(F("esphome_text_sensor_value{id=\""));
+ stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
+ stream->print(F("\",name=\""));
+ stream->print(relabel_name_(obj).c_str());
+ stream->print(F("\",value=\""));
+ stream->print(obj->state.c_str());
+ stream->print(F("\"} "));
+ stream->print(F("1.0"));
+ stream->print(F("\n"));
+ } else {
+ // Invalid state
+ stream->print(F("esphome_text_sensor_failed{id=\""));
+ stream->print(relabel_id_(obj).c_str());
+ add_area_label_(stream, area);
+ add_node_label_(stream, node);
+ add_friendly_name_label_(stream, friendly_name);
+ stream->print(F("\",name=\""));
+ stream->print(relabel_name_(obj).c_str());
+ stream->print(F("\"} 1\n"));
+ }
+}
+#endif
+
} // namespace prometheus
} // namespace esphome
#endif
diff --git a/esphome/components/prometheus/prometheus_handler.h b/esphome/components/prometheus/prometheus_handler.h
index f5e49a1419..5d08aca63a 100644
--- a/esphome/components/prometheus/prometheus_handler.h
+++ b/esphome/components/prometheus/prometheus_handler.h
@@ -60,54 +60,72 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
protected:
std::string relabel_id_(EntityBase *obj);
std::string relabel_name_(EntityBase *obj);
+ void add_area_label_(AsyncResponseStream *stream, std::string &area);
+ void add_node_label_(AsyncResponseStream *stream, std::string &node);
+ void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
#ifdef USE_SENSOR
/// Return the type for prometheus
void sensor_type_(AsyncResponseStream *stream);
/// Return the sensor state as prometheus data point
- void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj);
+ void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
#endif
#ifdef USE_BINARY_SENSOR
/// Return the type for prometheus
void binary_sensor_type_(AsyncResponseStream *stream);
/// Return the sensor state as prometheus data point
- void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj);
+ void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
+ std::string &node, std::string &friendly_name);
#endif
#ifdef USE_FAN
/// Return the type for prometheus
void fan_type_(AsyncResponseStream *stream);
/// Return the sensor state as prometheus data point
- void fan_row_(AsyncResponseStream *stream, fan::Fan *obj);
+ void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
#endif
#ifdef USE_LIGHT
/// Return the type for prometheus
void light_type_(AsyncResponseStream *stream);
/// Return the Light Values state as prometheus data point
- void light_row_(AsyncResponseStream *stream, light::LightState *obj);
+ void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
#endif
#ifdef USE_COVER
/// Return the type for prometheus
void cover_type_(AsyncResponseStream *stream);
/// Return the switch Values state as prometheus data point
- void cover_row_(AsyncResponseStream *stream, cover::Cover *obj);
+ void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
#endif
#ifdef USE_SWITCH
/// Return the type for prometheus
void switch_type_(AsyncResponseStream *stream);
/// Return the switch Values state as prometheus data point
- void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj);
+ void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
#endif
#ifdef USE_LOCK
/// Return the type for prometheus
void lock_type_(AsyncResponseStream *stream);
/// Return the lock Values state as prometheus data point
- void lock_row_(AsyncResponseStream *stream, lock::Lock *obj);
+ void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
+#endif
+
+#ifdef USE_TEXT_SENSOR
+ /// Return the type for prometheus
+ void text_sensor_type_(AsyncResponseStream *stream);
+ /// Return the lock Values state as prometheus data point
+ void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
+ std::string &friendly_name);
#endif
web_server_base::WebServerBase *base_;
diff --git a/esphome/components/qspi_dbi/models.py b/esphome/components/qspi_dbi/models.py
index 071ea72d73..cbd9c4663f 100644
--- a/esphome/components/qspi_dbi/models.py
+++ b/esphome/components/qspi_dbi/models.py
@@ -55,6 +55,7 @@ chip.cmd(PAGESEL, 0x00)
chip.cmd(0xC2, 0x00)
chip.delay(10)
chip.cmd(TEON, 0x00)
+chip.cmd(PIXFMT, 0x55)
chip = DriverChip("AXS15231")
chip.cmd(0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0xA5)
diff --git a/esphome/components/remote_base/raw_protocol.cpp b/esphome/components/remote_base/raw_protocol.cpp
index bdeb935dc4..ef0cb8454e 100644
--- a/esphome/components/remote_base/raw_protocol.cpp
+++ b/esphome/components/remote_base/raw_protocol.cpp
@@ -28,7 +28,7 @@ bool RawDumper::dump(RemoteReceiveData src) {
ESP_LOGI(TAG, "%s", buffer);
buffer_offset = 0;
written = sprintf(buffer, " ");
- if (i + 1 < src.size()) {
+ if (i + 1 < src.size() - 1) {
written += sprintf(buffer + written, "%" PRId32 ", ", value);
} else {
written += sprintf(buffer + written, "%" PRId32, value);
diff --git a/esphome/components/rp2040/__init__.py b/esphome/components/rp2040/__init__.py
index 925acb629d..d612631a4c 100644
--- a/esphome/components/rp2040/__init__.py
+++ b/esphome/components/rp2040/__init__.py
@@ -17,7 +17,7 @@ from esphome.const import (
PLATFORM_RP2040,
)
from esphome.core import CORE, EsphomeError, coroutine_with_priority
-from esphome.helpers import copy_file_if_changed, mkdir_p, write_file
+from esphome.helpers import copy_file_if_changed, mkdir_p, write_file, read_file
from .const import KEY_BOARD, KEY_PIO_FILES, KEY_RP2040, rp2040_ns
@@ -26,7 +26,7 @@ from .gpio import rp2040_pin_to_code # noqa
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@jesserockz"]
-AUTO_LOAD = []
+AUTO_LOAD = ["preferences"]
def set_core_data(config):
@@ -230,11 +230,14 @@ def generate_pio_files() -> bool:
# Called by writer.py
-def copy_files() -> bool:
+def copy_files():
dir = os.path.dirname(__file__)
post_build_file = os.path.join(dir, "post_build.py.script")
copy_file_if_changed(
post_build_file,
CORE.relative_build_path("post_build.py"),
)
- return generate_pio_files()
+ if generate_pio_files():
+ path = CORE.relative_src_path("esphome.h")
+ content = read_file(path).rstrip("\n")
+ write_file(path, content + '\n#include "pio_includes.h"\n')
diff --git a/esphome/components/rp2040/gpio.py b/esphome/components/rp2040/gpio.py
index 6ba0975a2c..58514f7db5 100644
--- a/esphome/components/rp2040/gpio.py
+++ b/esphome/components/rp2040/gpio.py
@@ -1,6 +1,8 @@
+from esphome import pins
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import (
+ CONF_ANALOG,
CONF_ID,
CONF_INPUT,
CONF_INVERTED,
@@ -10,10 +12,8 @@ from esphome.const import (
CONF_OUTPUT,
CONF_PULLDOWN,
CONF_PULLUP,
- CONF_ANALOG,
)
from esphome.core import CORE
-from esphome import pins
from . import boards
from .const import KEY_BOARD, KEY_RP2040, rp2040_ns
@@ -41,8 +41,10 @@ def _translate_pin(value):
"This variable only supports pin numbers, not full pin schemas "
"(with inverted and mode)."
)
- if isinstance(value, int):
+ if isinstance(value, int) and not isinstance(value, bool):
return value
+ if not isinstance(value, str):
+ raise cv.Invalid(f"Invalid pin number: {value}")
try:
return int(value)
except ValueError:
diff --git a/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.cpp b/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.cpp
index 655b469b91..ba09171649 100644
--- a/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.cpp
+++ b/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.cpp
@@ -84,6 +84,26 @@ void RpiDpiRgb::draw_pixels_at(int x_start, int y_start, int w, int h, const uin
ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
}
+int RpiDpiRgb::get_width() {
+ switch (this->rotation_) {
+ case display::DISPLAY_ROTATION_90_DEGREES:
+ case display::DISPLAY_ROTATION_270_DEGREES:
+ return this->get_height_internal();
+ default:
+ return this->get_width_internal();
+ }
+}
+
+int RpiDpiRgb::get_height() {
+ switch (this->rotation_) {
+ case display::DISPLAY_ROTATION_90_DEGREES:
+ case display::DISPLAY_ROTATION_270_DEGREES:
+ return this->get_width_internal();
+ default:
+ return this->get_height_internal();
+ }
+}
+
void RpiDpiRgb::draw_pixel_at(int x, int y, Color color) {
if (!this->get_clipping().inside(x, y))
return; // NOLINT
diff --git a/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.h b/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.h
index 10f77a2624..7525040cd1 100644
--- a/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.h
+++ b/esphome/components/rpi_dpi_rgb/rpi_dpi_rgb.h
@@ -24,6 +24,7 @@ class RpiDpiRgb : public display::Display {
void update() override { this->do_update_(); }
void setup() override;
void loop() override;
+ float get_setup_priority() const override { return setup_priority::HARDWARE; }
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
void draw_pixel_at(int x, int y, Color color) override;
@@ -44,8 +45,8 @@ class RpiDpiRgb : public display::Display {
this->width_ = width;
this->height_ = height;
}
- int get_width() override { return this->width_; }
- int get_height() override { return this->height_; }
+ int get_width() override;
+ int get_height() override;
void set_hsync_back_porch(uint16_t hsync_back_porch) { this->hsync_back_porch_ = hsync_back_porch; }
void set_hsync_front_porch(uint16_t hsync_front_porch) { this->hsync_front_porch_ = hsync_front_porch; }
void set_hsync_pulse_width(uint16_t hsync_pulse_width) { this->hsync_pulse_width_ = hsync_pulse_width; }
diff --git a/esphome/components/rtttl/rtttl.cpp b/esphome/components/rtttl/rtttl.cpp
index 495b5c1c8a..db4cc731e4 100644
--- a/esphome/components/rtttl/rtttl.cpp
+++ b/esphome/components/rtttl/rtttl.cpp
@@ -26,7 +26,10 @@ inline double deg2rad(double degrees) {
return degrees * PI_ON_180;
}
-void Rtttl::dump_config() { ESP_LOGCONFIG(TAG, "Rtttl"); }
+void Rtttl::dump_config() {
+ ESP_LOGCONFIG(TAG, "Rtttl:");
+ ESP_LOGCONFIG(TAG, " Gain: %f", gain_);
+}
void Rtttl::play(std::string rtttl) {
if (this->state_ != State::STATE_STOPPED && this->state_ != State::STATE_STOPPING) {
diff --git a/esphome/components/rtttl/rtttl.h b/esphome/components/rtttl/rtttl.h
index 3cb6e3f5fb..420948bfbf 100644
--- a/esphome/components/rtttl/rtttl.h
+++ b/esphome/components/rtttl/rtttl.h
@@ -39,13 +39,8 @@ class Rtttl : public Component {
#ifdef USE_SPEAKER
void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
#endif
- void set_gain(float gain) {
- if (gain < 0.1f)
- gain = 0.1f;
- if (gain > 1.0f)
- gain = 1.0f;
- this->gain_ = gain;
- }
+ float get_gain() { return gain_; }
+ void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
void play(std::string rtttl);
void stop();
void dump_config() override;
diff --git a/esphome/components/sdl/sdl_esphome.cpp b/esphome/components/sdl/sdl_esphome.cpp
index 5e17ca5650..8f0821a2fa 100644
--- a/esphome/components/sdl/sdl_esphome.cpp
+++ b/esphome/components/sdl/sdl_esphome.cpp
@@ -9,8 +9,9 @@ void Sdl::setup() {
ESP_LOGD(TAG, "Starting setup");
SDL_Init(SDL_INIT_VIDEO);
this->window_ = SDL_CreateWindow(App.get_name().c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- this->width_, this->height_, 0);
+ this->width_, this->height_, SDL_WINDOW_RESIZABLE);
this->renderer_ = SDL_CreateRenderer(this->window_, -1, SDL_RENDERER_SOFTWARE);
+ SDL_RenderSetLogicalSize(this->renderer_, this->width_, this->height_);
this->texture_ =
SDL_CreateTexture(this->renderer_, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STATIC, this->width_, this->height_);
SDL_SetTextureBlendMode(this->texture_, SDL_BLENDMODE_BLEND);
@@ -25,6 +26,10 @@ void Sdl::update() {
this->y_low_ = this->height_;
this->x_high_ = 0;
this->y_high_ = 0;
+ this->redraw_(rect);
+}
+
+void Sdl::redraw_(SDL_Rect &rect) {
SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
SDL_RenderPresent(this->renderer_);
}
@@ -33,15 +38,13 @@ void Sdl::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *
display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
SDL_Rect rect{x_start, y_start, w, h};
if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || big_endian) {
- display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
- x_pad);
+ Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
} else {
auto stride = x_offset + w + x_pad;
auto data = ptr + (stride * y_offset + x_offset) * 2;
SDL_UpdateTexture(this->texture_, &rect, data, stride * 2);
}
- SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
- SDL_RenderPresent(this->renderer_);
+ this->redraw_(rect);
}
void Sdl::draw_pixel_at(int x, int y, Color color) {
@@ -84,6 +87,20 @@ void Sdl::loop() {
}
break;
+ case SDL_WINDOWEVENT:
+ switch (e.window.event) {
+ case SDL_WINDOWEVENT_SIZE_CHANGED:
+ case SDL_WINDOWEVENT_EXPOSED:
+ case SDL_WINDOWEVENT_RESIZED: {
+ SDL_Rect rect{0, 0, this->width_, this->height_};
+ this->redraw_(rect);
+ break;
+ }
+ default:
+ break;
+ }
+ break;
+
default:
ESP_LOGV(TAG, "Event %d", e.type);
break;
diff --git a/esphome/components/sdl/sdl_esphome.h b/esphome/components/sdl/sdl_esphome.h
index e4b2d9dd9f..4b0e59c9fe 100644
--- a/esphome/components/sdl/sdl_esphome.h
+++ b/esphome/components/sdl/sdl_esphome.h
@@ -38,6 +38,7 @@ class Sdl : public display::Display {
protected:
int get_width_internal() override { return this->width_; }
int get_height_internal() override { return this->height_; }
+ void redraw_(SDL_Rect &rect);
int width_{};
int height_{};
SDL_Renderer *renderer_{};
diff --git a/esphome/components/sdm_meter/sdm_meter.cpp b/esphome/components/sdm_meter/sdm_meter.cpp
index 9c35d306ad..18e06e2b04 100644
--- a/esphome/components/sdm_meter/sdm_meter.cpp
+++ b/esphome/components/sdm_meter/sdm_meter.cpp
@@ -38,7 +38,7 @@ void SDMMeter::on_modbus_data(const std::vector &data) {
ESP_LOGD(
TAG,
- "SDMMeter Phase %c: V=%.3f V, I=%.3f A, Active P=%.3f W, Apparent P=%.3f VA, Reactive P=%.3f VAR, PF=%.3f, "
+ "SDMMeter Phase %c: V=%.3f V, I=%.3f A, Active P=%.3f W, Apparent P=%.3f VA, Reactive P=%.3f var, PF=%.3f, "
"PA=%.3f °",
i + 'A', voltage, current, active_power, apparent_power, reactive_power, power_factor, phase_angle);
if (phase.voltage_sensor_ != nullptr)
diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py
index 27338b8608..9dbad27102 100644
--- a/esphome/components/sensor/__init__.py
+++ b/esphome/components/sensor/__init__.py
@@ -335,19 +335,28 @@ def sensor_schema(
return SENSOR_SCHEMA.extend(schema)
-@FILTER_REGISTRY.register("offset", OffsetFilter, cv.float_)
+@FILTER_REGISTRY.register("offset", OffsetFilter, cv.templatable(cv.float_))
async def offset_filter_to_code(config, filter_id):
- return cg.new_Pvariable(filter_id, config)
+ template_ = await cg.templatable(config, [], float)
+ return cg.new_Pvariable(filter_id, template_)
-@FILTER_REGISTRY.register("multiply", MultiplyFilter, cv.float_)
+@FILTER_REGISTRY.register("multiply", MultiplyFilter, cv.templatable(cv.float_))
async def multiply_filter_to_code(config, filter_id):
- return cg.new_Pvariable(filter_id, config)
+ template_ = await cg.templatable(config, [], float)
+ return cg.new_Pvariable(filter_id, template_)
-@FILTER_REGISTRY.register("filter_out", FilterOutValueFilter, cv.float_)
+@FILTER_REGISTRY.register(
+ "filter_out",
+ FilterOutValueFilter,
+ cv.Any(cv.templatable(cv.float_), [cv.templatable(cv.float_)]),
+)
async def filter_out_filter_to_code(config, filter_id):
- return cg.new_Pvariable(filter_id, config)
+ if not isinstance(config, list):
+ config = [config]
+ template_ = [await cg.templatable(x, [], float) for x in config]
+ return cg.new_Pvariable(filter_id, template_)
QUANTILE_SCHEMA = cv.All(
@@ -573,7 +582,7 @@ async def heartbeat_filter_to_code(config, filter_id):
TIMEOUT_SCHEMA = cv.maybe_simple_value(
{
cv.Required(CONF_TIMEOUT): cv.positive_time_period_milliseconds,
- cv.Optional(CONF_VALUE, default="nan"): cv.float_,
+ cv.Optional(CONF_VALUE, default="nan"): cv.templatable(cv.float_),
},
key=CONF_TIMEOUT,
)
@@ -581,7 +590,8 @@ TIMEOUT_SCHEMA = cv.maybe_simple_value(
@FILTER_REGISTRY.register("timeout", TimeoutFilter, TIMEOUT_SCHEMA)
async def timeout_filter_to_code(config, filter_id):
- var = cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], config[CONF_VALUE])
+ template_ = await cg.templatable(config[CONF_VALUE], [], float)
+ var = cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], template_)
await cg.register_component(var, {})
return var
diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp
index bcf1fc8269..0a8740dd5b 100644
--- a/esphome/components/sensor/filter.cpp
+++ b/esphome/components/sensor/filter.cpp
@@ -288,36 +288,36 @@ optional LambdaFilter::new_value(float value) {
}
// OffsetFilter
-OffsetFilter::OffsetFilter(float offset) : offset_(offset) {}
+OffsetFilter::OffsetFilter(TemplatableValue offset) : offset_(std::move(offset)) {}
-optional OffsetFilter::new_value(float value) { return value + this->offset_; }
+optional OffsetFilter::new_value(float value) { return value + this->offset_.value(); }
// MultiplyFilter
-MultiplyFilter::MultiplyFilter(float multiplier) : multiplier_(multiplier) {}
+MultiplyFilter::MultiplyFilter(TemplatableValue multiplier) : multiplier_(std::move(multiplier)) {}
-optional MultiplyFilter::new_value(float value) { return value * this->multiplier_; }
+optional MultiplyFilter::new_value(float value) { return value * this->multiplier_.value(); }
// FilterOutValueFilter
-FilterOutValueFilter::FilterOutValueFilter(float value_to_filter_out) : value_to_filter_out_(value_to_filter_out) {}
+FilterOutValueFilter::FilterOutValueFilter(std::vector> values_to_filter_out)
+ : values_to_filter_out_(std::move(values_to_filter_out)) {}
optional FilterOutValueFilter::new_value(float value) {
- if (std::isnan(this->value_to_filter_out_)) {
- if (std::isnan(value)) {
- return {};
- } else {
- return value;
+ int8_t accuracy = this->parent_->get_accuracy_decimals();
+ float accuracy_mult = powf(10.0f, accuracy);
+ for (auto filter_value : this->values_to_filter_out_) {
+ if (std::isnan(filter_value.value())) {
+ if (std::isnan(value)) {
+ return {};
+ }
+ continue;
}
- } else {
- int8_t accuracy = this->parent_->get_accuracy_decimals();
- float accuracy_mult = powf(10.0f, accuracy);
- float rounded_filter_out = roundf(accuracy_mult * this->value_to_filter_out_);
+ float rounded_filter_out = roundf(accuracy_mult * filter_value.value());
float rounded_value = roundf(accuracy_mult * value);
if (rounded_filter_out == rounded_value) {
return {};
- } else {
- return value;
}
}
+ return value;
}
// ThrottleFilter
@@ -383,11 +383,12 @@ void OrFilter::initialize(Sensor *parent, Filter *next) {
// TimeoutFilter
optional TimeoutFilter::new_value(float value) {
- this->set_timeout("timeout", this->time_period_, [this]() { this->output(this->value_); });
+ this->set_timeout("timeout", this->time_period_, [this]() { this->output(this->value_.value()); });
return value;
}
-TimeoutFilter::TimeoutFilter(uint32_t time_period, float new_value) : time_period_(time_period), value_(new_value) {}
+TimeoutFilter::TimeoutFilter(uint32_t time_period, TemplatableValue new_value)
+ : time_period_(time_period), value_(std::move(new_value)) {}
float TimeoutFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
// DebounceFilter
diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h
index 92b1d8d240..86586b458d 100644
--- a/esphome/components/sensor/filter.h
+++ b/esphome/components/sensor/filter.h
@@ -5,6 +5,7 @@
#include
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
+#include "esphome/core/automation.h"
namespace esphome {
namespace sensor {
@@ -273,34 +274,33 @@ class LambdaFilter : public Filter {
/// A simple filter that adds `offset` to each value it receives.
class OffsetFilter : public Filter {
public:
- explicit OffsetFilter(float offset);
+ explicit OffsetFilter(TemplatableValue offset);
optional new_value(float value) override;
protected:
- float offset_;
+ TemplatableValue offset_;
};
/// A simple filter that multiplies to each value it receives by `multiplier`.
class MultiplyFilter : public Filter {
public:
- explicit MultiplyFilter(float multiplier);
-
+ explicit MultiplyFilter(TemplatableValue multiplier);
optional new_value(float value) override;
protected:
- float multiplier_;
+ TemplatableValue multiplier_;
};
/// A simple filter that only forwards the filter chain if it doesn't receive `value_to_filter_out`.
class FilterOutValueFilter : public Filter {
public:
- explicit FilterOutValueFilter(float value_to_filter_out);
+ explicit FilterOutValueFilter(std::vector> values_to_filter_out);
optional new_value(float value) override;
protected:
- float value_to_filter_out_;
+ std::vector> values_to_filter_out_;
};
class ThrottleFilter : public Filter {
@@ -316,8 +316,7 @@ class ThrottleFilter : public Filter {
class TimeoutFilter : public Filter, public Component {
public:
- explicit TimeoutFilter(uint32_t time_period, float new_value);
- void set_value(float new_value) { this->value_ = new_value; }
+ explicit TimeoutFilter(uint32_t time_period, TemplatableValue new_value);
optional new_value(float value) override;
@@ -325,7 +324,7 @@ class TimeoutFilter : public Filter, public Component {
protected:
uint32_t time_period_;
- float value_;
+ TemplatableValue value_;
};
class DebounceFilter : public Filter, public Component {
diff --git a/esphome/components/sgp4x/sgp4x.cpp b/esphome/components/sgp4x/sgp4x.cpp
index 7e474b9371..bf91c90832 100644
--- a/esphome/components/sgp4x/sgp4x.cpp
+++ b/esphome/components/sgp4x/sgp4x.cpp
@@ -111,7 +111,7 @@ void SGP4xComponent::setup() {
number of records reported from being overwhelming.
*/
ESP_LOGD(TAG, "Component requires sampling of 1Hz, setting up background sampler");
- this->set_interval(1000, [this]() { this->update_gas_indices(); });
+ this->set_interval(1000, [this]() { this->take_sample(); });
}
void SGP4xComponent::self_test_() {
@@ -146,31 +146,15 @@ void SGP4xComponent::self_test_() {
});
}
-/**
- * @brief Combined the measured gasses, temperature, and humidity
- * to calculate the VOC Index
- *
- * @param temperature The measured temperature in degrees C
- * @param humidity The measured relative humidity in % rH
- * @return int32_t The VOC Index
- */
-bool SGP4xComponent::measure_gas_indices_(int32_t &voc, int32_t &nox) {
- uint16_t voc_sraw;
- uint16_t nox_sraw;
- if (!measure_raw_(voc_sraw, nox_sraw))
- return false;
-
- this->status_clear_warning();
-
- voc = voc_algorithm_.process(voc_sraw);
- if (nox_sensor_) {
- nox = nox_algorithm_.process(nox_sraw);
- }
- ESP_LOGV(TAG, "VOC = %" PRId32 ", NOx = %" PRId32, voc, nox);
+void SGP4xComponent::update_gas_indices_() {
+ this->voc_index_ = this->voc_algorithm_.process(this->voc_sraw_);
+ if (this->nox_sensor_ != nullptr)
+ this->nox_index_ = this->nox_algorithm_.process(this->nox_sraw_);
+ ESP_LOGV(TAG, "VOC = %" PRId32 ", NOx = %" PRId32, this->voc_index_, this->nox_index_);
// Store baselines after defined interval or if the difference between current and stored baseline becomes too
// much
if (this->store_baseline_ && this->seconds_since_last_store_ > SHORTEST_BASELINE_STORE_INTERVAL) {
- voc_algorithm_.get_states(this->voc_state0_, this->voc_state1_);
+ this->voc_algorithm_.get_states(this->voc_state0_, this->voc_state1_);
if (std::abs(this->voc_baselines_storage_.state0 - this->voc_state0_) > MAXIMUM_STORAGE_DIFF ||
std::abs(this->voc_baselines_storage_.state1 - this->voc_state1_) > MAXIMUM_STORAGE_DIFF) {
this->seconds_since_last_store_ = 0;
@@ -179,29 +163,27 @@ bool SGP4xComponent::measure_gas_indices_(int32_t &voc, int32_t &nox) {
if (this->pref_.save(&this->voc_baselines_storage_)) {
ESP_LOGI(TAG, "Stored VOC baseline state0: 0x%04" PRIX32 " ,state1: 0x%04" PRIX32,
- this->voc_baselines_storage_.state0, voc_baselines_storage_.state1);
+ this->voc_baselines_storage_.state0, this->voc_baselines_storage_.state1);
} else {
ESP_LOGW(TAG, "Could not store VOC baselines");
}
}
}
- return true;
+ if (this->samples_read_ < this->samples_to_stabilize_) {
+ this->samples_read_++;
+ ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %" PRIu32, this->samples_read_,
+ this->samples_to_stabilize_, this->voc_index_);
+ }
}
-/**
- * @brief Return the raw gas measurement
- *
- * @param temperature The measured temperature in degrees C
- * @param humidity The measured relative humidity in % rH
- * @return uint16_t The current raw gas measurement
- */
-bool SGP4xComponent::measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw) {
+
+void SGP4xComponent::measure_raw_() {
float humidity = NAN;
static uint32_t nox_conditioning_start = millis();
if (!this->self_test_complete_) {
ESP_LOGD(TAG, "Self-test not yet complete");
- return false;
+ return;
}
if (this->humidity_sensor_ != nullptr) {
humidity = this->humidity_sensor_->state;
@@ -243,61 +225,45 @@ bool SGP4xComponent::measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw) {
data[1] = tempticks;
if (!this->write_command(command, data, 2)) {
- this->status_set_warning();
ESP_LOGD(TAG, "write error (%d)", this->last_error_);
- return false;
+ this->status_set_warning("measurement request failed");
+ return;
}
- delay(measure_time_);
- uint16_t raw_data[2];
- raw_data[1] = 0;
- if (!this->read_data(raw_data, response_words)) {
- this->status_set_warning();
- ESP_LOGD(TAG, "read error (%d)", this->last_error_);
- return false;
- }
- voc_raw = raw_data[0];
- nox_raw = raw_data[1]; // either 0 or the measured NOx ticks
- return true;
+
+ this->set_timeout(this->measure_time_, [this, response_words]() {
+ uint16_t raw_data[2];
+ raw_data[1] = 0;
+ if (!this->read_data(raw_data, response_words)) {
+ ESP_LOGD(TAG, "read error (%d)", this->last_error_);
+ this->status_set_warning("measurement read failed");
+ this->voc_index_ = this->nox_index_ = UINT16_MAX;
+ return;
+ }
+ this->voc_sraw_ = raw_data[0];
+ this->nox_sraw_ = raw_data[1]; // either 0 or the measured NOx ticks
+ this->status_clear_warning();
+ this->update_gas_indices_();
+ });
}
-void SGP4xComponent::update_gas_indices() {
+void SGP4xComponent::take_sample() {
if (!this->self_test_complete_)
return;
-
this->seconds_since_last_store_ += 1;
- if (!this->measure_gas_indices_(this->voc_index_, this->nox_index_)) {
- // Set values to UINT16_MAX to indicate failure
- this->voc_index_ = this->nox_index_ = UINT16_MAX;
- ESP_LOGE(TAG, "measure gas indices failed");
- return;
- }
- if (this->samples_read_ < this->samples_to_stabilize_) {
- this->samples_read_++;
- ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %" PRIu32, this->samples_read_,
- this->samples_to_stabilize_, this->voc_index_);
- return;
- }
+ this->measure_raw_();
}
void SGP4xComponent::update() {
if (this->samples_read_ < this->samples_to_stabilize_) {
return;
}
- if (this->voc_sensor_) {
- if (this->voc_index_ != UINT16_MAX) {
- this->status_clear_warning();
+ if (this->voc_sensor_ != nullptr) {
+ if (this->voc_index_ != UINT16_MAX)
this->voc_sensor_->publish_state(this->voc_index_);
- } else {
- this->status_set_warning();
- }
}
- if (this->nox_sensor_) {
- if (this->nox_index_ != UINT16_MAX) {
- this->status_clear_warning();
+ if (this->nox_sensor_ != nullptr) {
+ if (this->nox_index_ != UINT16_MAX)
this->nox_sensor_->publish_state(this->nox_index_);
- } else {
- this->status_set_warning();
- }
}
}
@@ -329,7 +295,7 @@ void SGP4xComponent::dump_config() {
}
LOG_UPDATE_INTERVAL(this);
- if (this->humidity_sensor_ != nullptr && this->temperature_sensor_ != nullptr) {
+ if (this->humidity_sensor_ != nullptr || this->temperature_sensor_ != nullptr) {
ESP_LOGCONFIG(TAG, " Compensation:");
LOG_SENSOR(" ", "Temperature Source:", this->temperature_sensor_);
LOG_SENSOR(" ", "Humidity Source:", this->humidity_sensor_);
diff --git a/esphome/components/sgp4x/sgp4x.h b/esphome/components/sgp4x/sgp4x.h
index aa5ae4b9d2..959ff12c27 100644
--- a/esphome/components/sgp4x/sgp4x.h
+++ b/esphome/components/sgp4x/sgp4x.h
@@ -73,7 +73,7 @@ class SGP4xComponent : public PollingComponent, public sensor::Sensor, public se
void setup() override;
void update() override;
- void update_gas_indices();
+ void take_sample();
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
@@ -108,8 +108,10 @@ class SGP4xComponent : public PollingComponent, public sensor::Sensor, public se
sensor::Sensor *temperature_sensor_{nullptr};
int16_t sensirion_init_sensors_();
- bool measure_gas_indices_(int32_t &voc, int32_t &nox);
- bool measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw);
+ void update_gas_indices_();
+ void measure_raw_();
+ uint16_t voc_sraw_;
+ uint16_t nox_sraw_;
SgpType sgp_type_{SGP40};
uint64_t serial_number_;
diff --git a/esphome/components/speaker/__init__.py b/esphome/components/speaker/__init__.py
index 1bbc0b02ef..948fe4b534 100644
--- a/esphome/components/speaker/__init__.py
+++ b/esphome/components/speaker/__init__.py
@@ -1,15 +1,19 @@
from esphome import automation
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
+from esphome.components import audio_dac
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME
from esphome.core import CORE
from esphome.coroutine import coroutine_with_priority
-CODEOWNERS = ["@jesserockz"]
+AUTO_LOAD = ["audio"]
+CODEOWNERS = ["@jesserockz", "@kahrendt"]
IS_PLATFORM_COMPONENT = True
+CONF_AUDIO_DAC = "audio_dac"
+
speaker_ns = cg.esphome_ns.namespace("speaker")
Speaker = speaker_ns.class_("Speaker")
@@ -26,6 +30,12 @@ FinishAction = speaker_ns.class_(
VolumeSetAction = speaker_ns.class_(
"VolumeSetAction", automation.Action, cg.Parented.template(Speaker)
)
+MuteOnAction = speaker_ns.class_(
+ "MuteOnAction", automation.Action, cg.Parented.template(Speaker)
+)
+MuteOffAction = speaker_ns.class_(
+ "MuteOffAction", automation.Action, cg.Parented.template(Speaker)
+)
IsPlayingCondition = speaker_ns.class_("IsPlayingCondition", automation.Condition)
@@ -33,7 +43,9 @@ IsStoppedCondition = speaker_ns.class_("IsStoppedCondition", automation.Conditio
async def setup_speaker_core_(var, config):
- pass
+ if audio_dac_config := config.get(CONF_AUDIO_DAC):
+ aud_dac = await cg.get_variable(audio_dac_config)
+ cg.add(var.set_audio_dac(aud_dac))
async def register_speaker(var, config):
@@ -42,8 +54,11 @@ async def register_speaker(var, config):
await setup_speaker_core_(var, config)
-SPEAKER_SCHEMA = cv.Schema({})
-
+SPEAKER_SCHEMA = cv.Schema(
+ {
+ cv.Optional(CONF_AUDIO_DAC): cv.use_id(audio_dac.AudioDac),
+ }
+)
SPEAKER_AUTOMATION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(Speaker)})
@@ -113,6 +128,15 @@ async def speaker_volume_set_action(config, action_id, template_arg, args):
return var
+@automation.register_action(
+ "speaker.mute_off", MuteOffAction, SPEAKER_AUTOMATION_SCHEMA
+)
+@automation.register_action("speaker.mute_on", MuteOnAction, SPEAKER_AUTOMATION_SCHEMA)
+async def speaker_mute_action_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)
+
+
@coroutine_with_priority(100.0)
async def to_code(config):
cg.add_global(speaker_ns.using)
diff --git a/esphome/components/speaker/automation.h b/esphome/components/speaker/automation.h
index 9efda011f2..c083796eea 100644
--- a/esphome/components/speaker/automation.h
+++ b/esphome/components/speaker/automation.h
@@ -39,6 +39,26 @@ template class VolumeSetAction : public Action, public Pa
void play(Ts... x) override { this->parent_->set_volume(this->volume_.value(x...)); }
};
+template class MuteOnAction : public Action {
+ public:
+ explicit MuteOnAction(Speaker *speaker) : speaker_(speaker) {}
+
+ void play(Ts... x) override { this->speaker_->set_mute_state(true); }
+
+ protected:
+ Speaker *speaker_;
+};
+
+template class MuteOffAction : public Action