diff --git a/.clang-tidy b/.clang-tidy
index 946f2950d8..5878028f48 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -7,28 +7,39 @@ Checks: >-
-boost-*,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
+ -bugprone-multi-level-implicit-pointer-conversion,
-bugprone-narrowing-conversions,
-bugprone-signed-char-misuse,
+ -bugprone-switch-missing-default-case,
-cert-dcl50-cpp,
-cert-err33-c,
-cert-err58-cpp,
-cert-oop57-cpp,
-cert-str34-c,
+ -clang-analyzer-optin.core.EnumCastOutOfRange,
-clang-analyzer-optin.cplusplus.UninitializedObject,
-clang-analyzer-osx.*,
-clang-diagnostic-delete-abstract-non-virtual-dtor,
-clang-diagnostic-delete-non-abstract-non-virtual-dtor,
+ -clang-diagnostic-deprecated-declarations,
-clang-diagnostic-ignored-optimization-argument,
+ -clang-diagnostic-missing-field-initializers,
-clang-diagnostic-shadow-field,
-clang-diagnostic-unused-const-variable,
-clang-diagnostic-unused-parameter,
+ -clang-diagnostic-vla-cxx-extension,
-concurrency-*,
-cppcoreguidelines-avoid-c-arrays,
+ -cppcoreguidelines-avoid-const-or-ref-data-members,
+ -cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-init-variables,
+ -cppcoreguidelines-macro-to-enum,
-cppcoreguidelines-macro-usage,
+ -cppcoreguidelines-missing-std-forward,
-cppcoreguidelines-narrowing-conversions,
-cppcoreguidelines-non-private-member-variables-in-classes,
+ -cppcoreguidelines-owning-memory,
-cppcoreguidelines-prefer-member-initializer,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-constant-array-index,
@@ -40,7 +51,9 @@ Checks: >-
-cppcoreguidelines-pro-type-static-cast-downcast,
-cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-pro-type-vararg,
+ -cppcoreguidelines-rvalue-reference-param-not-moved,
-cppcoreguidelines-special-member-functions,
+ -cppcoreguidelines-use-default-member-init,
-cppcoreguidelines-virtual-class-destructor,
-fuchsia-multiple-inheritance,
-fuchsia-overloaded-operator,
@@ -60,20 +73,32 @@ Checks: >-
-llvm-include-order,
-llvm-qualified-auto,
-llvmlibc-*,
- -misc-non-private-member-variables-in-classes,
+ -misc-const-correctness,
+ -misc-include-cleaner,
-misc-no-recursion,
+ -misc-non-private-member-variables-in-classes,
-misc-unused-parameters,
- -modernize-avoid-c-arrays,
+ -misc-use-anonymous-namespace,
-modernize-avoid-bind,
+ -modernize-avoid-c-arrays,
-modernize-concat-nested-namespaces,
+ -modernize-macro-to-enum,
-modernize-return-braced-init-list,
+ -modernize-type-traits,
-modernize-use-auto,
+ -modernize-use-constraints,
-modernize-use-default-member-init,
-modernize-use-equals-default,
- -modernize-use-trailing-return-type,
-modernize-use-nodiscard,
+ -modernize-use-nullptr,
+ -modernize-use-nodiscard,
+ -modernize-use-nullptr,
+ -modernize-use-trailing-return-type,
-mpi-*,
-objc-*,
+ -performance-enum-size,
+ -readability-avoid-nested-conditional-operator,
+ -readability-container-contains,
-readability-container-data-pointer,
-readability-convert-member-functions-to-static,
-readability-else-after-return,
@@ -82,11 +107,14 @@ Checks: >-
-readability-isolate-declaration,
-readability-magic-numbers,
-readability-make-member-function-const,
+ -readability-named-parameter,
+ -readability-redundant-casting,
+ -readability-redundant-inline-specifier,
+ -readability-redundant-member-init,
-readability-redundant-string-init,
-readability-uppercase-literal-suffix,
-readability-use-anyofallof,
WarningsAsErrors: '*'
-AnalyzeTemporaryDtors: false
FormatStyle: google
CheckOptions:
- key: google-readability-function-size.StatementThreshold
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/build-image/action.yaml b/.github/actions/build-image/action.yaml
index 5c686605c3..cc9894a657 100644
--- a/.github/actions/build-image/action.yaml
+++ b/.github/actions/build-image/action.yaml
@@ -46,7 +46,7 @@ runs:
- name: Build and push to ghcr by digest
id: build-ghcr
- uses: docker/build-push-action@v6.9.0
+ uses: docker/build-push-action@v6.10.0
env:
DOCKER_BUILD_SUMMARY: false
DOCKER_BUILD_RECORD_UPLOAD: false
@@ -72,7 +72,7 @@ runs:
- name: Build and push to dockerhub by digest
id: build-dockerhub
- uses: docker/build-push-action@v6.9.0
+ uses: docker/build-push-action@v6.10.0
env:
DOCKER_BUILD_SUMMARY: false
DOCKER_BUILD_RECORD_UPLOAD: false
diff --git a/.github/actions/restore-python/action.yml b/.github/actions/restore-python/action.yml
index 1f5812691e..6b87cf0170 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.2.0
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..6ce4159da0 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -30,7 +30,7 @@ concurrency:
jobs:
common:
name: Create common environment
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
outputs:
cache-key: ${{ steps.cache-key.outputs.key }}
steps:
@@ -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.2.0
with:
path: venv
# yamllint disable-line rule:line-length
@@ -62,7 +62,7 @@ jobs:
black:
name: Check black
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
steps:
@@ -83,7 +83,7 @@ jobs:
flake8:
name: Check flake8
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
steps:
@@ -104,7 +104,7 @@ jobs:
pylint:
name: Check pylint
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
steps:
@@ -125,7 +125,7 @@ jobs:
pyupgrade:
name: Check pyupgrade
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
steps:
@@ -146,7 +146,7 @@ jobs:
ci-custom:
name: Run script/ci-custom
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
steps:
@@ -219,13 +219,13 @@ 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 }}
clang-format:
name: Check clang-format
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
steps:
@@ -251,7 +251,7 @@ jobs:
clang-tidy:
name: ${{ matrix.name }}
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
- black
@@ -302,23 +302,18 @@ jobs:
- name: Cache platformio
if: github.ref == 'refs/heads/dev'
- uses: actions/cache@v4.1.1
+ uses: actions/cache@v4.2.0
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.2.0
with:
path: ~/.platformio
key: platformio-${{ matrix.pio_cache_key }}
- - name: Install clang-tidy
- run: |
- sudo apt-get update
- sudo apt-get install clang-tidy-14
-
- name: Register problem matchers
run: |
echo "::add-matcher::.github/workflows/matchers/gcc.json"
@@ -345,7 +340,7 @@ jobs:
if: always()
list-components:
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
if: github.event_name == 'pull_request'
@@ -387,7 +382,7 @@ jobs:
test-build-components:
name: Component test ${{ matrix.file }}
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
- list-components
@@ -421,7 +416,7 @@ jobs:
test-build-components-splitter:
name: Split components for testing into 20 groups maximum
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
- list-components
@@ -439,7 +434,7 @@ jobs:
test-build-components-split:
name: Test split components
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
- list-components
@@ -483,7 +478,7 @@ jobs:
ci-status:
name: CI Status
- runs-on: ubuntu-latest
+ runs-on: ubuntu-24.04
needs:
- common
- black
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 26a213f170..a4e4305207 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.3
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 876965c81d..95f0758d8f 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
@@ -177,6 +179,7 @@ esphome/components/haier/text_sensor/* @paveldn
esphome/components/havells_solar/* @sourabhjaiswal
esphome/components/hbridge/fan/* @WeekendWarrior
esphome/components/hbridge/light/* @DotNetDann
+esphome/components/hbridge/switch/* @dwmw2
esphome/components/he60r/* @clydebarrow
esphome/components/heatpumpir/* @rob-deutsch
esphome/components/hitachi_ac424/* @sourabhjaiswal
@@ -198,10 +201,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
@@ -352,6 +356,8 @@ esphome/components/sdl/* @clydebarrow
esphome/components/sdm_meter/* @jesserockz @polyfaces
esphome/components/sdp3x/* @Azimath
esphome/components/seeed_mr24hpc1/* @limengdu
+esphome/components/seeed_mr60bha2/* @limengdu
+esphome/components/seeed_mr60fda2/* @limengdu
esphome/components/selec_meter/* @sourabhjaiswal
esphome/components/select/* @esphome/core
esphome/components/sen0321/* @notjj
@@ -377,7 +383,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
@@ -406,6 +412,7 @@ esphome/components/substitutions/* @esphome/core
esphome/components/sun/* @OttoWinter
esphome/components/sun_gtil2/* @Mat931
esphome/components/switch/* @esphome/core
+esphome/components/switch/binary_sensor/* @ssieb
esphome/components/t6615/* @tylermenezes
esphome/components/tc74/* @sethgirvan
esphome/components/tca9548a/* @andreashergert1984
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 52a4794f24..0bb558d35e 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,50 @@ 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 < /etc/apt/sources.list.d/llvm.sources.list \
+ && apt-get update \
# Use pinned versions so that we get updates with build caching
&& apt-get install -y --no-install-recommends \
clang-format-13=1:13.0.1-11+b2 \
- clang-tidy-14=1:14.0.6-12 \
patch=2.7.6-7 \
software-properties-common=0.99.30-4.1~deb12u1 \
nano=7.2-1+deb12u1 \
build-essential=12.9 \
python3-dev=3.11.2-1+b1 \
- && rm -rf \
+ && if [ "$TARGETARCH$TARGETVARIANT" != "armv7" ]; then \
+ # move this up after armv7 is retired
+ apt-get install -y --no-install-recommends clang-tidy-18=1:18.1.8~++20240731024826+3b5b5c1ec4a3-1~exp1~20240731144843.145 ; \
+ fi; \
+ rm -rf \
/tmp/* \
/var/{cache,log}/* \
/var/lib/apt/lists/*
diff --git a/esphome/__main__.py b/esphome/__main__.py
index cf2741dbdb..dce041e5ac 100644
--- a/esphome/__main__.py
+++ b/esphome/__main__.py
@@ -20,6 +20,8 @@ from esphome.const import (
CONF_DEASSERT_RTS_DTR,
CONF_DISABLED,
CONF_ESPHOME,
+ CONF_LEVEL,
+ CONF_LOG_TOPIC,
CONF_LOGGER,
CONF_MDNS,
CONF_MQTT,
@@ -30,6 +32,7 @@ from esphome.const import (
CONF_PLATFORMIO_OPTIONS,
CONF_PORT,
CONF_SUBSTITUTIONS,
+ CONF_TOPIC,
PLATFORM_BK72XX,
PLATFORM_ESP32,
PLATFORM_ESP8266,
@@ -38,7 +41,7 @@ from esphome.const import (
SECRETS_FILES,
)
from esphome.core import CORE, EsphomeError, coroutine
-from esphome.helpers import indent, is_ip_address, get_bool_env
+from esphome.helpers import get_bool_env, indent, is_ip_address
from esphome.log import Fore, color, setup_log
from esphome.util import (
get_serial_ports,
@@ -95,8 +98,12 @@ def choose_upload_log_host(
options.append((f"Over The Air ({CORE.address})", CORE.address))
if default == "OTA":
return CORE.address
- if show_mqtt and CONF_MQTT in CORE.config:
- options.append((f"MQTT ({CORE.config['mqtt'][CONF_BROKER]})", "MQTT"))
+ if (
+ show_mqtt
+ and (mqtt_config := CORE.config.get(CONF_MQTT))
+ and mqtt_logging_enabled(mqtt_config)
+ ):
+ options.append((f"MQTT ({mqtt_config[CONF_BROKER]})", "MQTT"))
if default == "OTA":
return "MQTT"
if default is not None:
@@ -106,6 +113,17 @@ def choose_upload_log_host(
return choose_prompt(options, purpose=purpose)
+def mqtt_logging_enabled(mqtt_config):
+ log_topic = mqtt_config[CONF_LOG_TOPIC]
+ if log_topic is None:
+ return False
+ if CONF_TOPIC not in log_topic:
+ return False
+ if log_topic.get(CONF_LEVEL, None) == "NONE":
+ return False
+ return True
+
+
def get_port_type(port):
if port.startswith("/") or port.startswith("COM"):
return "SERIAL"
@@ -345,7 +363,7 @@ def upload_program(config, args, host):
from esphome import espota2
- remote_port = ota_conf[CONF_PORT]
+ remote_port = int(ota_conf[CONF_PORT])
password = ota_conf.get(CONF_PASSWORD, "")
if (
@@ -378,7 +396,7 @@ def show_logs(config, args, port):
port = mqtt.get_esphome_device_ip(
config, args.username, args.password, args.client_id
- )
+ )[0]
from esphome.components.api.client import run_logs
diff --git a/esphome/components/adc/adc_sensor.h b/esphome/components/adc/adc_sensor.h
index b697d6dd7e..7a3e1c8da7 100644
--- a/esphome/components/adc/adc_sensor.h
+++ b/esphome/components/adc/adc_sensor.h
@@ -3,13 +3,12 @@
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
#include "esphome/core/component.h"
-#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
#ifdef USE_ESP32
#include
#include "driver/adc.h"
-#endif
+#endif // USE_ESP32
namespace esphome {
namespace adc {
@@ -43,7 +42,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
this->channel1_ = ADC1_CHANNEL_MAX;
}
void set_autorange(bool autorange) { this->autorange_ = autorange; }
-#endif
+#endif // USE_ESP32
/// Update ADC values
void update() override;
@@ -59,11 +58,11 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
#ifdef USE_ESP8266
std::string unique_id() override;
-#endif
+#endif // USE_ESP8266
#ifdef USE_RP2040
void set_is_temperature() { this->is_temperature_ = true; }
-#endif
+#endif // USE_RP2040
protected:
InternalGPIOPin *pin_;
@@ -72,7 +71,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
#ifdef USE_RP2040
bool is_temperature_{false};
-#endif
+#endif // USE_RP2040
#ifdef USE_ESP32
adc_atten_t attenuation_{ADC_ATTEN_DB_0};
@@ -83,8 +82,8 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM] = {};
#else
esp_adc_cal_characteristics_t cal_characteristics_[ADC_ATTEN_MAX] = {};
-#endif
-#endif
+#endif // ESP_IDF_VERSION_MAJOR
+#endif // USE_ESP32
};
} // namespace adc
diff --git a/esphome/components/adc/adc_sensor_common.cpp b/esphome/components/adc/adc_sensor_common.cpp
new file mode 100644
index 0000000000..2dccd55fcd
--- /dev/null
+++ b/esphome/components/adc/adc_sensor_common.cpp
@@ -0,0 +1,24 @@
+#include "adc_sensor.h"
+#include "esphome/core/log.h"
+
+namespace esphome {
+namespace adc {
+
+static const char *const TAG = "adc.common";
+
+void ADCSensor::update() {
+ float value_v = this->sample();
+ ESP_LOGV(TAG, "'%s': Got voltage=%.4fV", this->get_name().c_str(), value_v);
+ this->publish_state(value_v);
+}
+
+void ADCSensor::set_sample_count(uint8_t sample_count) {
+ if (sample_count != 0) {
+ this->sample_count_ = sample_count;
+ }
+}
+
+float ADCSensor::get_setup_priority() const { return setup_priority::DATA; }
+
+} // namespace adc
+} // namespace esphome
diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor_esp32.cpp
similarity index 53%
rename from esphome/components/adc/adc_sensor.cpp
rename to esphome/components/adc/adc_sensor_esp32.cpp
index 7257793016..24e3750091 100644
--- a/esphome/components/adc/adc_sensor.cpp
+++ b/esphome/components/adc/adc_sensor_esp32.cpp
@@ -1,30 +1,13 @@
+#ifdef USE_ESP32
+
#include "adc_sensor.h"
-#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
-#ifdef USE_ESP8266
-#ifdef USE_ADC_SENSOR_VCC
-#include
-ADC_MODE(ADC_VCC)
-#else
-#include
-#endif
-#endif
-
-#ifdef USE_RP2040
-#ifdef CYW43_USES_VSYS_PIN
-#include "pico/cyw43_arch.h"
-#endif
-#include
-#endif
-
namespace esphome {
namespace adc {
-static const char *const TAG = "adc";
+static const char *const TAG = "adc.esp32";
-// 13-bit for S2, 12-bit for all other ESP32 variants
-#ifdef USE_ESP32
static const adc_bits_width_t ADC_WIDTH_MAX_SOC_BITS = static_cast(ADC_WIDTH_MAX - 1);
#ifndef SOC_ADC_RTC_MAX_BITWIDTH
@@ -32,24 +15,15 @@ static const adc_bits_width_t ADC_WIDTH_MAX_SOC_BITS = static_cast> 1; // 2048 (12 bit) or 4096 (13 bit)
-#endif
+static const int ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1;
+static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1;
-#ifdef USE_RP2040
-extern "C"
-#endif
- void
- ADCSensor::setup() {
+void ADCSensor::setup() {
ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
-#if !defined(USE_ADC_SENSOR_VCC) && !defined(USE_RP2040)
- this->pin_->setup();
-#endif
-#ifdef USE_ESP32
if (this->channel1_ != ADC1_CHANNEL_MAX) {
adc1_config_width(ADC_WIDTH_MAX_SOC_BITS);
if (!this->autorange_) {
@@ -61,7 +35,6 @@ extern "C"
}
}
- // load characteristics for each attenuation
for (int32_t i = 0; i <= ADC_ATTEN_DB_12_COMPAT; i++) {
auto adc_unit = this->channel1_ != ADC1_CHANNEL_MAX ? ADC_UNIT_1 : ADC_UNIT_2;
auto cal_value = esp_adc_cal_characterize(adc_unit, (adc_atten_t) i, ADC_WIDTH_MAX_SOC_BITS,
@@ -79,31 +52,10 @@ extern "C"
break;
}
}
-
-#endif // USE_ESP32
-
-#ifdef USE_RP2040
- static bool initialized = false;
- if (!initialized) {
- adc_init();
- initialized = true;
- }
-#endif
-
- ESP_LOGCONFIG(TAG, "ADC '%s' setup finished!", this->get_name().c_str());
}
void ADCSensor::dump_config() {
LOG_SENSOR("", "ADC Sensor", this);
-#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
-#ifdef USE_ADC_SENSOR_VCC
- ESP_LOGCONFIG(TAG, " Pin: VCC");
-#else
- LOG_PIN(" Pin: ", this->pin_);
-#endif
-#endif // USE_ESP8266 || USE_LIBRETINY
-
-#ifdef USE_ESP32
LOG_PIN(" Pin: ", this->pin_);
if (this->autorange_) {
ESP_LOGCONFIG(TAG, " Attenuation: auto");
@@ -125,55 +77,10 @@ void ADCSensor::dump_config() {
break;
}
}
-#endif // USE_ESP32
-
-#ifdef USE_RP2040
- if (this->is_temperature_) {
- ESP_LOGCONFIG(TAG, " Pin: Temperature");
- } else {
-#ifdef USE_ADC_SENSOR_VCC
- ESP_LOGCONFIG(TAG, " Pin: VCC");
-#else
- LOG_PIN(" Pin: ", this->pin_);
-#endif // USE_ADC_SENSOR_VCC
- }
-#endif // USE_RP2040
ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
LOG_UPDATE_INTERVAL(this);
}
-float ADCSensor::get_setup_priority() const { return setup_priority::DATA; }
-void ADCSensor::update() {
- float value_v = this->sample();
- ESP_LOGV(TAG, "'%s': Got voltage=%.4fV", this->get_name().c_str(), value_v);
- this->publish_state(value_v);
-}
-
-void ADCSensor::set_sample_count(uint8_t sample_count) {
- if (sample_count != 0) {
- this->sample_count_ = sample_count;
- }
-}
-
-#ifdef USE_ESP8266
-float ADCSensor::sample() {
- uint32_t raw = 0;
- for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
-#ifdef USE_ADC_SENSOR_VCC
- raw += ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance)
-#else
- raw += analogRead(this->pin_->get_pin()); // NOLINT
-#endif
- }
- raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
- if (this->output_raw_) {
- return raw;
- }
- return raw / 1024.0f;
-}
-#endif
-
-#ifdef USE_ESP32
float ADCSensor::sample() {
if (!this->autorange_) {
uint32_t sum = 0;
@@ -240,93 +147,17 @@ float ADCSensor::sample() {
uint32_t mv2 = esp_adc_cal_raw_to_voltage(raw2, &this->cal_characteristics_[(int32_t) ADC_ATTEN_DB_2_5]);
uint32_t mv0 = esp_adc_cal_raw_to_voltage(raw0, &this->cal_characteristics_[(int32_t) ADC_ATTEN_DB_0]);
- // Contribution of each value, in range 0-2048 (12 bit ADC) or 0-4096 (13 bit ADC)
uint32_t c12 = std::min(raw12, ADC_HALF);
uint32_t c6 = ADC_HALF - std::abs(raw6 - ADC_HALF);
uint32_t c2 = ADC_HALF - std::abs(raw2 - ADC_HALF);
uint32_t c0 = std::min(ADC_MAX - raw0, ADC_HALF);
- // max theoretical csum value is 4096*4 = 16384
uint32_t csum = c12 + c6 + c2 + c0;
- // each mv is max 3900; so max value is 3900*4096*4, fits in unsigned32
uint32_t mv_scaled = (mv12 * c12) + (mv6 * c6) + (mv2 * c2) + (mv0 * c0);
return mv_scaled / (float) (csum * 1000U);
}
-#endif // USE_ESP32
-
-#ifdef USE_RP2040
-float ADCSensor::sample() {
- if (this->is_temperature_) {
- adc_set_temp_sensor_enabled(true);
- delay(1);
- adc_select_input(4);
- uint32_t raw = 0;
- for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
- raw += adc_read();
- }
- raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
- adc_set_temp_sensor_enabled(false);
- if (this->output_raw_) {
- return raw;
- }
- return raw * 3.3f / 4096.0f;
- } else {
- uint8_t pin = this->pin_->get_pin();
-#ifdef CYW43_USES_VSYS_PIN
- if (pin == PICO_VSYS_PIN) {
- // Measuring VSYS on Raspberry Pico W needs to be wrapped with
- // `cyw43_thread_enter()`/`cyw43_thread_exit()` as discussed in
- // https://github.com/raspberrypi/pico-sdk/issues/1222, since Wifi chip and
- // VSYS ADC both share GPIO29
- cyw43_thread_enter();
- }
-#endif // CYW43_USES_VSYS_PIN
-
- adc_gpio_init(pin);
- adc_select_input(pin - 26);
-
- uint32_t raw = 0;
- for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
- raw += adc_read();
- }
- raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
-
-#ifdef CYW43_USES_VSYS_PIN
- if (pin == PICO_VSYS_PIN) {
- cyw43_thread_exit();
- }
-#endif // CYW43_USES_VSYS_PIN
-
- if (this->output_raw_) {
- return raw;
- }
- float coeff = pin == PICO_VSYS_PIN ? 3.0 : 1.0;
- return raw * 3.3f / 4096.0f * coeff;
- }
-}
-#endif
-
-#ifdef USE_LIBRETINY
-float ADCSensor::sample() {
- uint32_t raw = 0;
- if (this->output_raw_) {
- for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
- raw += analogRead(this->pin_->get_pin()); // NOLINT
- }
- raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
- return raw;
- }
- for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
- raw += analogReadVoltage(this->pin_->get_pin()); // NOLINT
- }
- raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
- return raw / 1000.0f;
-}
-#endif // USE_LIBRETINY
-
-#ifdef USE_ESP8266
-std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
-#endif
} // namespace adc
} // namespace esphome
+
+#endif // USE_ESP32
diff --git a/esphome/components/adc/adc_sensor_esp8266.cpp b/esphome/components/adc/adc_sensor_esp8266.cpp
new file mode 100644
index 0000000000..c9b6f8b652
--- /dev/null
+++ b/esphome/components/adc/adc_sensor_esp8266.cpp
@@ -0,0 +1,58 @@
+#ifdef USE_ESP8266
+
+#include "adc_sensor.h"
+#include "esphome/core/helpers.h"
+#include "esphome/core/log.h"
+
+#ifdef USE_ADC_SENSOR_VCC
+#include
+ADC_MODE(ADC_VCC)
+#else
+#include
+#endif // USE_ADC_SENSOR_VCC
+
+namespace esphome {
+namespace adc {
+
+static const char *const TAG = "adc.esp8266";
+
+void ADCSensor::setup() {
+ ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
+#ifndef USE_ADC_SENSOR_VCC
+ this->pin_->setup();
+#endif
+}
+
+void ADCSensor::dump_config() {
+ LOG_SENSOR("", "ADC Sensor", this);
+#ifdef USE_ADC_SENSOR_VCC
+ ESP_LOGCONFIG(TAG, " Pin: VCC");
+#else
+ LOG_PIN(" Pin: ", this->pin_);
+#endif // USE_ADC_SENSOR_VCC
+ ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
+ LOG_UPDATE_INTERVAL(this);
+}
+
+float ADCSensor::sample() {
+ uint32_t raw = 0;
+ for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
+#ifdef USE_ADC_SENSOR_VCC
+ raw += ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance)
+#else
+ raw += analogRead(this->pin_->get_pin()); // NOLINT
+#endif // USE_ADC_SENSOR_VCC
+ }
+ raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
+ if (this->output_raw_) {
+ return raw;
+ }
+ return raw / 1024.0f;
+}
+
+std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
+
+} // namespace adc
+} // namespace esphome
+
+#endif // USE_ESP8266
diff --git a/esphome/components/adc/adc_sensor_rp2040.cpp b/esphome/components/adc/adc_sensor_rp2040.cpp
new file mode 100644
index 0000000000..520ff3bacc
--- /dev/null
+++ b/esphome/components/adc/adc_sensor_rp2040.cpp
@@ -0,0 +1,93 @@
+#ifdef USE_RP2040
+
+#include "adc_sensor.h"
+#include "esphome/core/log.h"
+
+#ifdef CYW43_USES_VSYS_PIN
+#include "pico/cyw43_arch.h"
+#endif // CYW43_USES_VSYS_PIN
+#include
+
+namespace esphome {
+namespace adc {
+
+static const char *const TAG = "adc.rp2040";
+
+void ADCSensor::setup() {
+ ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
+ static bool initialized = false;
+ if (!initialized) {
+ adc_init();
+ initialized = true;
+ }
+}
+
+void ADCSensor::dump_config() {
+ LOG_SENSOR("", "ADC Sensor", this);
+ if (this->is_temperature_) {
+ ESP_LOGCONFIG(TAG, " Pin: Temperature");
+ } else {
+#ifdef USE_ADC_SENSOR_VCC
+ ESP_LOGCONFIG(TAG, " Pin: VCC");
+#else
+ LOG_PIN(" Pin: ", this->pin_);
+#endif // USE_ADC_SENSOR_VCC
+ }
+ ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
+ LOG_UPDATE_INTERVAL(this);
+}
+
+float ADCSensor::sample() {
+ if (this->is_temperature_) {
+ adc_set_temp_sensor_enabled(true);
+ delay(1);
+ adc_select_input(4);
+ uint32_t raw = 0;
+ for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
+ raw += adc_read();
+ }
+ raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
+ adc_set_temp_sensor_enabled(false);
+ if (this->output_raw_) {
+ return raw;
+ }
+ return raw * 3.3f / 4096.0f;
+ }
+
+ uint8_t pin = this->pin_->get_pin();
+#ifdef CYW43_USES_VSYS_PIN
+ if (pin == PICO_VSYS_PIN) {
+ // Measuring VSYS on Raspberry Pico W needs to be wrapped with
+ // `cyw43_thread_enter()`/`cyw43_thread_exit()` as discussed in
+ // https://github.com/raspberrypi/pico-sdk/issues/1222, since Wifi chip and
+ // VSYS ADC both share GPIO29
+ cyw43_thread_enter();
+ }
+#endif // CYW43_USES_VSYS_PIN
+
+ adc_gpio_init(pin);
+ adc_select_input(pin - 26);
+
+ uint32_t raw = 0;
+ for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
+ raw += adc_read();
+ }
+ raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
+
+#ifdef CYW43_USES_VSYS_PIN
+ if (pin == PICO_VSYS_PIN) {
+ cyw43_thread_exit();
+ }
+#endif // CYW43_USES_VSYS_PIN
+
+ if (this->output_raw_) {
+ return raw;
+ }
+ float coeff = pin == PICO_VSYS_PIN ? 3.0f : 1.0f;
+ return raw * 3.3f / 4096.0f * coeff;
+}
+
+} // namespace adc
+} // namespace esphome
+
+#endif // USE_RP2040
diff --git a/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp b/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp
index b1d2b2a097..7bb9b9989c 100644
--- a/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp
+++ b/esphome/components/alarm_control_panel/alarm_control_panel_call.cpp
@@ -72,10 +72,9 @@ void AlarmControlPanelCall::validate_() {
this->state_.reset();
return;
}
- if (state == ACP_STATE_DISARMED &&
- !(this->parent_->is_state_armed(this->parent_->get_state()) ||
- this->parent_->get_state() == ACP_STATE_PENDING || this->parent_->get_state() == ACP_STATE_ARMING ||
- this->parent_->get_state() == ACP_STATE_TRIGGERED)) {
+ if (state == ACP_STATE_DISARMED && !this->parent_->is_state_armed(this->parent_->get_state()) &&
+ this->parent_->get_state() != ACP_STATE_PENDING && this->parent_->get_state() != ACP_STATE_ARMING &&
+ this->parent_->get_state() != ACP_STATE_TRIGGERED) {
ESP_LOGW(TAG, "Cannot disarm when not armed");
this->state_.reset();
return;
diff --git a/esphome/components/animation/__init__.py b/esphome/components/animation/__init__.py
index eb3d09ac96..21a82649f0 100644
--- a/esphome/components/animation/__init__.py
+++ b/esphome/components/animation/__init__.py
@@ -2,7 +2,6 @@ import logging
from esphome import automation, core
import esphome.codegen as cg
-from esphome.components import font
import esphome.components.image as espImage
from esphome.components.image import (
CONF_USE_TRANSPARENCY,
@@ -131,7 +130,7 @@ ANIMATION_SCHEMA = cv.Schema(
)
)
-CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, ANIMATION_SCHEMA)
+CONFIG_SCHEMA = ANIMATION_SCHEMA
NEXT_FRAME_SCHEMA = automation.maybe_simple_id(
{
@@ -271,7 +270,8 @@ async def to_code(config):
pos += 1
elif config[CONF_TYPE] in ["RGB565", "TRANSPARENT_IMAGE"]:
- data = [0 for _ in range(height * width * 2 * frames)]
+ bytes_per_pixel = 3 if transparent else 2
+ data = [0 for _ in range(height * width * bytes_per_pixel * frames)]
pos = 0
for frameIndex in range(frames):
image.seek(frameIndex)
@@ -288,17 +288,13 @@ async def to_code(config):
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"]:
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/apds9306/apds9306.cpp b/esphome/components/apds9306/apds9306.cpp
index 7b79b0964c..bbb3ba1910 100644
--- a/esphome/components/apds9306/apds9306.cpp
+++ b/esphome/components/apds9306/apds9306.cpp
@@ -122,7 +122,8 @@ void APDS9306::update() {
this->status_clear_warning();
- if (!(status &= 0b00001000)) { // No new data
+ status &= 0b00001000;
+ if (!status) { // No new data
return;
}
diff --git a/esphome/components/audio/audio.h b/esphome/components/audio/audio.h
index b0968dc8da..caf325cf54 100644
--- a/esphome/components/audio/audio.h
+++ b/esphome/components/audio/audio.h
@@ -1,7 +1,7 @@
#pragma once
+#include
#include
-#include
namespace esphome {
namespace audio {
diff --git a/esphome/components/binary_sensor/binary_sensor.h b/esphome/components/binary_sensor/binary_sensor.h
index 301a472810..57cae9e2f5 100644
--- a/esphome/components/binary_sensor/binary_sensor.h
+++ b/esphome/components/binary_sensor/binary_sensor.h
@@ -58,7 +58,7 @@ class BinarySensor : public EntityBase, public EntityBase_DeviceClass {
void publish_initial_state(bool state);
/// The current reported state of the binary sensor.
- bool state;
+ bool state{false};
void add_filter(Filter *filter);
void add_filters(const std::vector &filters);
diff --git a/esphome/components/bk72xx/__init__.py b/esphome/components/bk72xx/__init__.py
index 6737631ac7..b5122de956 100644
--- a/esphome/components/bk72xx/__init__.py
+++ b/esphome/components/bk72xx/__init__.py
@@ -15,7 +15,7 @@ from esphome.components.libretiny.const import (
)
from esphome.core import CORE
-from .boards import BK72XX_BOARDS, BK72XX_BOARD_PINS
+from .boards import BK72XX_BOARD_PINS, BK72XX_BOARDS
CODEOWNERS = ["@kuba2k2"]
AUTO_LOAD = ["libretiny"]
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/cse7766/cse7766.cpp b/esphome/components/cse7766/cse7766.cpp
index 48240464b3..3907c195d0 100644
--- a/esphome/components/cse7766/cse7766.cpp
+++ b/esphome/components/cse7766/cse7766.cpp
@@ -43,7 +43,7 @@ bool CSE7766Component::check_byte_() {
uint8_t index = this->raw_data_index_;
uint8_t byte = this->raw_data_[index];
if (index == 0) {
- return !((byte != 0x55) && ((byte & 0xF0) != 0xF0) && (byte != 0xAA));
+ return (byte == 0x55) || ((byte & 0xF0) == 0xF0) || (byte == 0xAA);
}
if (index == 1) {
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/datetime/datetime_entity.cpp b/esphome/components/datetime/datetime_entity.cpp
index f215b7acb5..3d92194efa 100644
--- a/esphome/components/datetime/datetime_entity.cpp
+++ b/esphome/components/datetime/datetime_entity.cpp
@@ -60,9 +60,7 @@ ESPTime DateTimeEntity::state_as_esptime() const {
obj.hour = this->hour_;
obj.minute = this->minute_;
obj.second = this->second_;
- obj.day_of_week = 1; // Required to be valid for recalc_timestamp_local but not used.
- obj.day_of_year = 1; // Required to be valid for recalc_timestamp_local but not used.
- obj.recalc_timestamp_local(false);
+ obj.recalc_timestamp_local();
return obj;
}
diff --git a/esphome/components/deep_sleep/deep_sleep_esp32.cpp b/esphome/components/deep_sleep/deep_sleep_esp32.cpp
index d54046bc11..d647140865 100644
--- a/esphome/components/deep_sleep/deep_sleep_esp32.cpp
+++ b/esphome/components/deep_sleep/deep_sleep_esp32.cpp
@@ -52,11 +52,11 @@ void DeepSleepComponent::dump_config_platform_() {
bool DeepSleepComponent::prepare_to_sleep_() {
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
- !this->sleep_duration_.has_value() && this->wakeup_pin_->digital_read()) {
+ this->wakeup_pin_->digital_read()) {
// Defer deep sleep until inactive
if (!this->next_enter_deep_sleep_) {
this->status_set_warning();
- ESP_LOGW(TAG, "Waiting for pin_ to switch state to enter deep sleep...");
+ ESP_LOGW(TAG, "Waiting wakeup pin state change to enter deep sleep...");
}
this->next_enter_deep_sleep_ = true;
return false;
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/dht/dht.cpp b/esphome/components/dht/dht.cpp
index db1c851d5f..3f9f9c57f4 100644
--- a/esphome/components/dht/dht.cpp
+++ b/esphome/components/dht/dht.cpp
@@ -135,7 +135,8 @@ bool HOT IRAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, bool r
// Wait for falling edge
while (this->pin_->digital_read()) {
- if ((end_time = micros()) - start_time > 90) {
+ end_time = micros();
+ if (end_time - start_time > 90) {
if (i < 0) {
error_code = 3;
} else {
diff --git a/esphome/components/display/display.cpp b/esphome/components/display/display.cpp
index 145a4f5278..f00c2936a8 100644
--- a/esphome/components/display/display.cpp
+++ b/esphome/components/display/display.cpp
@@ -1,6 +1,6 @@
#include "display.h"
-#include "display_color_utils.h"
#include
+#include "display_color_utils.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
@@ -662,20 +662,24 @@ void DisplayOnPageChangeTrigger::process(DisplayPage *from, DisplayPage *to) {
if ((this->from_ == nullptr || this->from_ == from) && (this->to_ == nullptr || this->to_ == to))
this->trigger(from, to);
}
-void Display::strftime(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, ESPTime time) {
+void Display::strftime(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
+ ESPTime time) {
char buffer[64];
size_t ret = time.strftime(buffer, sizeof(buffer), format);
if (ret > 0)
- this->print(x, y, font, color, align, buffer);
+ this->print(x, y, font, color, align, buffer, background);
+}
+void Display::strftime(int x, int y, BaseFont *font, Color color, TextAlign align, const char *format, ESPTime time) {
+ this->strftime(x, y, font, color, COLOR_OFF, align, format, time);
}
void Display::strftime(int x, int y, BaseFont *font, Color color, const char *format, ESPTime time) {
- this->strftime(x, y, font, color, TextAlign::TOP_LEFT, format, time);
+ this->strftime(x, y, font, color, COLOR_OFF, TextAlign::TOP_LEFT, format, time);
}
void Display::strftime(int x, int y, BaseFont *font, TextAlign align, const char *format, ESPTime time) {
- this->strftime(x, y, font, COLOR_ON, align, format, time);
+ this->strftime(x, y, font, COLOR_ON, COLOR_OFF, align, format, time);
}
void Display::strftime(int x, int y, BaseFont *font, const char *format, ESPTime time) {
- this->strftime(x, y, font, COLOR_ON, TextAlign::TOP_LEFT, format, time);
+ this->strftime(x, y, font, COLOR_ON, COLOR_OFF, TextAlign::TOP_LEFT, format, time);
}
void Display::start_clipping(Rect rect) {
diff --git a/esphome/components/display/display.h b/esphome/components/display/display.h
index 54e897cdec..43da08f4ac 100644
--- a/esphome/components/display/display.h
+++ b/esphome/components/display/display.h
@@ -437,6 +437,20 @@ class Display : public PollingComponent {
*/
void printf(int x, int y, BaseFont *font, const char *format, ...) __attribute__((format(printf, 5, 6)));
+ /** Evaluate the strftime-format `format` and print the result with the anchor point at [x,y] with `font`.
+ *
+ * @param x The x coordinate of the text alignment anchor point.
+ * @param y The y coordinate of the text alignment anchor point.
+ * @param font The font to draw the text with.
+ * @param color The color to draw the text with.
+ * @param background The background color to draw the text with.
+ * @param align The alignment of the text.
+ * @param format The format to use.
+ * @param ... The arguments to use for the text formatting.
+ */
+ void strftime(int x, int y, BaseFont *font, Color color, Color background, TextAlign align, const char *format,
+ ESPTime time) __attribute__((format(strftime, 8, 0)));
+
/** Evaluate the strftime-format `format` and print the result with the anchor point at [x,y] with `font`.
*
* @param x The x coordinate of the text alignment anchor point.
diff --git a/esphome/components/display_menu_base/__init__.py b/esphome/components/display_menu_base/__init__.py
index 8ae9cbc2a4..f9c0424104 100644
--- a/esphome/components/display_menu_base/__init__.py
+++ b/esphome/components/display_menu_base/__init__.py
@@ -68,8 +68,6 @@ IsActiveCondition = display_menu_base_ns.class_(
"IsActiveCondition", automation.Condition
)
-MULTI_CONF = True
-
MenuItemType = display_menu_base_ns.enum("MenuItemType")
MENU_ITEM_TYPES = {
diff --git a/esphome/components/display_menu_base/display_menu_base.cpp b/esphome/components/display_menu_base/display_menu_base.cpp
index 5502623607..2d8e6ae5fc 100644
--- a/esphome/components/display_menu_base/display_menu_base.cpp
+++ b/esphome/components/display_menu_base/display_menu_base.cpp
@@ -280,7 +280,7 @@ bool DisplayMenuComponent::cursor_down_() {
bool DisplayMenuComponent::enter_menu_() {
this->displayed_item_->on_leave();
this->displayed_item_ = static_cast(this->get_selected_item_());
- this->selection_stack_.push_front({this->top_index_, this->cursor_index_});
+ this->selection_stack_.emplace_front(this->top_index_, this->cursor_index_);
this->cursor_index_ = this->top_index_ = 0;
this->displayed_item_->on_enter();
diff --git a/esphome/components/dsmr/dsmr.cpp b/esphome/components/dsmr/dsmr.cpp
index 193ea1d4e5..c0a2883d79 100644
--- a/esphome/components/dsmr/dsmr.cpp
+++ b/esphome/components/dsmr/dsmr.cpp
@@ -296,7 +296,7 @@ void Dsmr::dump_config() {
}
void Dsmr::set_decryption_key(const std::string &decryption_key) {
- if (decryption_key.length() == 0) {
+ if (decryption_key.empty()) {
ESP_LOGI(TAG, "Disabling decryption");
this->decryption_key_.clear();
if (this->crypt_telegram_ != nullptr) {
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..b0bde75451 100644
--- a/esphome/components/esp32/__init__.py
+++ b/esphome/components/esp32/__init__.py
@@ -65,6 +65,8 @@ _LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@esphome/core"]
AUTO_LOAD = ["preferences"]
+CONF_RELEASE = "release"
+
def set_core_data(config):
CORE.data[KEY_ESP32] = {}
@@ -216,11 +218,17 @@ def _format_framework_arduino_version(ver: cv.Version) -> str:
return f"~3.{ver.major}{ver.minor:02d}{ver.patch:02d}.0"
-def _format_framework_espidf_version(ver: cv.Version) -> str:
+def _format_framework_espidf_version(
+ ver: cv.Version, release: str, for_platformio: bool
+) -> str:
# format the given arduino (https://github.com/espressif/esp-idf/releases) version to
# a PIO platformio/framework-espidf value
# List of package versions: https://api.registry.platformio.org/v3/packages/platformio/tool/framework-espidf
- return f"~3.{ver.major}{ver.minor:02d}{ver.patch:02d}.0"
+ if for_platformio:
+ return f"platformio/framework-espidf@~3.{ver.major}{ver.minor:02d}{ver.patch:02d}.0"
+ if release:
+ return f"pioarduino/framework-espidf@https://github.com/pioarduino/esp-idf/releases/download/v{str(ver)}.{release}/esp-idf-v{str(ver)}.zip"
+ return f"pioarduino/framework-espidf@https://github.com/pioarduino/esp-idf/releases/download/v{str(ver)}/esp-idf-v{str(ver)}.zip"
# NOTE: Keep this in mind when updating the recommended version:
@@ -241,11 +249,33 @@ ARDUINO_PLATFORM_VERSION = cv.Version(5, 4, 0)
# The default/recommended esp-idf framework version
# - https://github.com/espressif/esp-idf/releases
# - https://api.registry.platformio.org/v3/packages/platformio/tool/framework-espidf
-RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION = cv.Version(4, 4, 8)
+RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION = cv.Version(5, 1, 5)
# The platformio/espressif32 version to use for esp-idf frameworks
# - https://github.com/platformio/platform-espressif32/releases
# - https://api.registry.platformio.org/v3/packages/platformio/platform/espressif32
-ESP_IDF_PLATFORM_VERSION = cv.Version(5, 4, 0)
+ESP_IDF_PLATFORM_VERSION = cv.Version(51, 3, 7)
+
+# List based on https://registry.platformio.org/tools/platformio/framework-espidf/versions
+SUPPORTED_PLATFORMIO_ESP_IDF_5X = [
+ cv.Version(5, 3, 1),
+ cv.Version(5, 3, 0),
+ cv.Version(5, 2, 2),
+ cv.Version(5, 2, 1),
+ cv.Version(5, 1, 2),
+ cv.Version(5, 1, 1),
+ cv.Version(5, 1, 0),
+ cv.Version(5, 0, 2),
+ cv.Version(5, 0, 1),
+ cv.Version(5, 0, 0),
+]
+
+# pioarduino versions that don't require a release number
+# List based on https://github.com/pioarduino/esp-idf/releases
+SUPPORTED_PIOARDUINO_ESP_IDF_5X = [
+ cv.Version(5, 3, 1),
+ cv.Version(5, 3, 0),
+ cv.Version(5, 1, 5),
+]
def _arduino_check_versions(value):
@@ -286,8 +316,8 @@ def _arduino_check_versions(value):
def _esp_idf_check_versions(value):
value = value.copy()
lookups = {
- "dev": (cv.Version(5, 1, 2), "https://github.com/espressif/esp-idf.git"),
- "latest": (cv.Version(5, 1, 2), None),
+ "dev": (cv.Version(5, 1, 5), "https://github.com/espressif/esp-idf.git"),
+ "latest": (cv.Version(5, 1, 5), None),
"recommended": (RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION, None),
}
@@ -305,13 +335,51 @@ def _esp_idf_check_versions(value):
if version < cv.Version(4, 0, 0):
raise cv.Invalid("Only ESP-IDF 4.0+ is supported.")
- value[CONF_VERSION] = str(version)
- value[CONF_SOURCE] = source or _format_framework_espidf_version(version)
+ # flag this for later *before* we set value[CONF_PLATFORM_VERSION] below
+ has_platform_ver = CONF_PLATFORM_VERSION in value
value[CONF_PLATFORM_VERSION] = value.get(
CONF_PLATFORM_VERSION, _parse_platform_version(str(ESP_IDF_PLATFORM_VERSION))
)
+ if (
+ (is_platformio := _platform_is_platformio(value[CONF_PLATFORM_VERSION]))
+ and version.major >= 5
+ and version not in SUPPORTED_PLATFORMIO_ESP_IDF_5X
+ ):
+ raise cv.Invalid(
+ f"ESP-IDF {str(version)} not supported by platformio/espressif32"
+ )
+
+ if (
+ version.major < 5
+ or (
+ version in SUPPORTED_PLATFORMIO_ESP_IDF_5X
+ and version not in SUPPORTED_PIOARDUINO_ESP_IDF_5X
+ )
+ ) and not has_platform_ver:
+ raise cv.Invalid(
+ f"ESP-IDF {value[CONF_VERSION]} may be supported by platformio/espressif32; please specify '{CONF_PLATFORM_VERSION}'"
+ )
+
+ if (
+ not is_platformio
+ and CONF_RELEASE not in value
+ and version not in SUPPORTED_PIOARDUINO_ESP_IDF_5X
+ ):
+ raise cv.Invalid(
+ f"ESP-IDF {value[CONF_VERSION]} is not available with pioarduino; you may need to specify '{CONF_RELEASE}'"
+ )
+
+ value[CONF_VERSION] = str(version)
+ value[CONF_SOURCE] = source or _format_framework_espidf_version(
+ version, value.get(CONF_RELEASE, None), is_platformio
+ )
+
+ if value[CONF_SOURCE].startswith("http"):
+ # prefix is necessary or platformio will complain with a cryptic error
+ value[CONF_SOURCE] = f"framework-espidf@{value[CONF_SOURCE]}"
+
if version != RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION:
_LOGGER.warning(
"The selected ESP-IDF framework version is not the recommended one. "
@@ -323,6 +391,12 @@ def _esp_idf_check_versions(value):
def _parse_platform_version(value):
try:
+ ver = cv.Version.parse(cv.version_number(value))
+ if ver.major >= 50: # a pioarduino version
+ if "-" in value:
+ # maybe a release candidate?...definitely not our default, just use it as-is...
+ return f"https://github.com/pioarduino/platform-espressif32.git#{value}"
+ return f"https://github.com/pioarduino/platform-espressif32.git#{ver.major}.{ver.minor:02d}.{ver.patch:02d}"
# if platform version is a valid version constraint, prefix the default package
cv.platformio_version_constraint(value)
return f"platformio/espressif32@{value}"
@@ -330,6 +404,14 @@ def _parse_platform_version(value):
return value
+def _platform_is_platformio(value):
+ try:
+ ver = cv.Version.parse(cv.version_number(value))
+ return ver.major < 50
+ except cv.Invalid:
+ return "platformio" in value
+
+
def _detect_variant(value):
board = value[CONF_BOARD]
if board in BOARDS:
@@ -355,24 +437,20 @@ def _detect_variant(value):
def final_validate(config):
- if CONF_PLATFORMIO_OPTIONS not in fv.full_config.get()[CONF_ESPHOME]:
+ if not (
+ pio_options := fv.full_config.get()[CONF_ESPHOME].get(CONF_PLATFORMIO_OPTIONS)
+ ):
+ # Not specified or empty
return config
pio_flash_size_key = "board_upload.flash_size"
pio_partitions_key = "board_build.partitions"
- if (
- CONF_PARTITIONS in config
- and pio_partitions_key
- in fv.full_config.get()[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS]
- ):
+ if CONF_PARTITIONS in config and pio_partitions_key in pio_options:
raise cv.Invalid(
f"Do not specify '{pio_partitions_key}' in '{CONF_PLATFORMIO_OPTIONS}' with '{CONF_PARTITIONS}' in esp32"
)
- if (
- pio_flash_size_key
- in fv.full_config.get()[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS]
- ):
+ if pio_flash_size_key in pio_options:
raise cv.Invalid(
f"Please specify {CONF_FLASH_SIZE} within esp32 configuration only"
)
@@ -395,6 +473,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,
@@ -405,6 +490,7 @@ ESP_IDF_FRAMEWORK_SCHEMA = cv.All(
cv.Schema(
{
cv.Optional(CONF_VERSION, default="recommended"): cv.string_strict,
+ cv.Optional(CONF_RELEASE): cv.string_strict,
cv.Optional(CONF_SOURCE): cv.string_strict,
cv.Optional(CONF_PLATFORM_VERSION): _parse_platform_version,
cv.Optional(CONF_SDKCONFIG_OPTIONS, default={}): {
@@ -494,6 +580,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",
@@ -505,10 +594,9 @@ async def to_code(config):
cg.add_build_flag("-DUSE_ESP_IDF")
cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ESP_IDF")
cg.add_build_flag("-Wno-nonnull-compare")
- cg.add_platformio_option(
- "platform_packages",
- [f"platformio/framework-espidf@{conf[CONF_SOURCE]}"],
- )
+
+ cg.add_platformio_option("platform_packages", [conf[CONF_SOURCE]])
+
# platformio/toolchain-esp32ulp does not support linux_aarch64 yet and has not been updated for over 2 years
# This is espressif's own published version which is more up to date.
cg.add_platformio_option(
@@ -540,8 +628,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..81400eb9c3 100644
--- a/esphome/components/esp32/boards.py
+++ b/esphome/components/esp32/boards.py
@@ -1,4 +1,12 @@
-from .const import VARIANT_ESP32, VARIANT_ESP32C3, VARIANT_ESP32S2, VARIANT_ESP32S3
+from .const import (
+ VARIANT_ESP32,
+ VARIANT_ESP32C2,
+ VARIANT_ESP32C3,
+ VARIANT_ESP32C6,
+ VARIANT_ESP32H2,
+ VARIANT_ESP32S2,
+ VARIANT_ESP32S3,
+)
ESP32_BASE_PINS = {
"TX": 1,
@@ -103,6 +111,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 +316,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 +1263,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,
@@ -1146,6 +1352,26 @@ done | sort
"""
BOARDS = {
+ "4d_systems_esp32s3_gen4_r8n16": {
+ "name": "4D Systems GEN4-ESP32 16MB (ESP32S3-R8N16)",
+ "variant": VARIANT_ESP32S3,
+ },
+ "adafruit_camera_esp32s3": {
+ "name": "Adafruit pyCamera S3",
+ "variant": VARIANT_ESP32S3,
+ },
+ "adafruit_feather_esp32c6": {
+ "name": "Adafruit Feather ESP32-C6",
+ "variant": VARIANT_ESP32C6,
+ },
+ "adafruit_feather_esp32s2": {
+ "name": "Adafruit Feather ESP32-S2",
+ "variant": VARIANT_ESP32S2,
+ },
+ "adafruit_feather_esp32s2_reversetft": {
+ "name": "Adafruit Feather ESP32-S2 Reverse TFT",
+ "variant": VARIANT_ESP32S2,
+ },
"adafruit_feather_esp32s2_tft": {
"name": "Adafruit Feather ESP32-S2 TFT",
"variant": VARIANT_ESP32S2,
@@ -1158,6 +1384,10 @@ BOARDS = {
"name": "Adafruit Feather ESP32-S3 No PSRAM",
"variant": VARIANT_ESP32S3,
},
+ "adafruit_feather_esp32s3_reversetft": {
+ "name": "Adafruit Feather ESP32-S3 Reverse TFT",
+ "variant": VARIANT_ESP32S3,
+ },
"adafruit_feather_esp32s3_tft": {
"name": "Adafruit Feather ESP32-S3 TFT",
"variant": VARIANT_ESP32S3,
@@ -1178,10 +1408,18 @@ BOARDS = {
"name": "Adafruit MagTag 2.9",
"variant": VARIANT_ESP32S2,
},
+ "adafruit_matrixportal_esp32s3": {
+ "name": "Adafruit MatrixPortal ESP32-S3",
+ "variant": VARIANT_ESP32S3,
+ },
"adafruit_metro_esp32s2": {
"name": "Adafruit Metro ESP32-S2",
"variant": VARIANT_ESP32S2,
},
+ "adafruit_metro_esp32s3": {
+ "name": "Adafruit Metro ESP32-S3",
+ "variant": VARIANT_ESP32S3,
+ },
"adafruit_qtpy_esp32c3": {
"name": "Adafruit QT Py ESP32-C3",
"variant": VARIANT_ESP32C3,
@@ -1194,10 +1432,18 @@ BOARDS = {
"name": "Adafruit QT Py ESP32-S2",
"variant": VARIANT_ESP32S2,
},
+ "adafruit_qtpy_esp32s3_n4r2": {
+ "name": "Adafruit QT Py ESP32-S3 (4M Flash 2M PSRAM)",
+ "variant": VARIANT_ESP32S3,
+ },
"adafruit_qtpy_esp32s3_nopsram": {
"name": "Adafruit QT Py ESP32-S3 No PSRAM",
"variant": VARIANT_ESP32S3,
},
+ "adafruit_qualia_s3_rgb666": {
+ "name": "Adafruit Qualia ESP32-S3 RGB666",
+ "variant": VARIANT_ESP32S3,
+ },
"airm2m_core_esp32c3": {
"name": "AirM2M CORE ESP32C3",
"variant": VARIANT_ESP32C3,
@@ -1206,14 +1452,30 @@ BOARDS = {
"name": "ALKS ESP32",
"variant": VARIANT_ESP32,
},
+ "arduino_nano_esp32": {
+ "name": "Arduino Nano ESP32",
+ "variant": VARIANT_ESP32S3,
+ },
+ "atd147_s3": {
+ "name": "ArtronShop ATD1.47-S3",
+ "variant": VARIANT_ESP32S3,
+ },
"atmegazero_esp32s2": {
"name": "EspinalLab ATMegaZero ESP32-S2",
"variant": VARIANT_ESP32S2,
},
+ "aventen_s3_sync": {
+ "name": "Aventen S3 Sync",
+ "variant": VARIANT_ESP32S3,
+ },
"az-delivery-devkit-v4": {
"name": "AZ-Delivery ESP-32 Dev Kit C V4",
"variant": VARIANT_ESP32,
},
+ "bee_data_logger": {
+ "name": "Smart Bee Data Logger",
+ "variant": VARIANT_ESP32S3,
+ },
"bee_motion_mini": {
"name": "Smart Bee Motion Mini",
"variant": VARIANT_ESP32C3,
@@ -1238,14 +1500,6 @@ BOARDS = {
"name": "BPI-Leaf-S3",
"variant": VARIANT_ESP32S3,
},
- "briki_abc_esp32": {
- "name": "Briki ABC (MBC-WB) - ESP32",
- "variant": VARIANT_ESP32,
- },
- "briki_mbc-wb_esp32": {
- "name": "Briki MBC-WB - ESP32",
- "variant": VARIANT_ESP32,
- },
"cnrs_aw2eth": {
"name": "CNRS AW2ETH",
"variant": VARIANT_ESP32,
@@ -1298,18 +1552,38 @@ BOARDS = {
"name": "DFRobot Beetle ESP32-C3",
"variant": VARIANT_ESP32C3,
},
+ "dfrobot_firebeetle2_esp32e": {
+ "name": "DFRobot Firebeetle 2 ESP32-E",
+ "variant": VARIANT_ESP32,
+ },
"dfrobot_firebeetle2_esp32s3": {
"name": "DFRobot Firebeetle 2 ESP32-S3",
"variant": VARIANT_ESP32S3,
},
+ "dfrobot_romeo_esp32s3": {
+ "name": "DFRobot Romeo ESP32-S3",
+ "variant": VARIANT_ESP32S3,
+ },
"dpu_esp32": {
"name": "TAMC DPU ESP32",
"variant": VARIANT_ESP32,
},
+ "edgebox-esp-100": {
+ "name": "Seeed Studio Edgebox-ESP-100",
+ "variant": VARIANT_ESP32S3,
+ },
"esp320": {
"name": "Electronic SweetPeas ESP320",
"variant": VARIANT_ESP32,
},
+ "esp32-c2-devkitm-1": {
+ "name": "Espressif ESP32-C2-DevKitM-1",
+ "variant": VARIANT_ESP32C2,
+ },
+ "esp32-c3-devkitc-02": {
+ "name": "Espressif ESP32-C3-DevKitC-02",
+ "variant": VARIANT_ESP32C3,
+ },
"esp32-c3-devkitm-1": {
"name": "Espressif ESP32-C3-DevKitM-1",
"variant": VARIANT_ESP32C3,
@@ -1318,6 +1592,14 @@ BOARDS = {
"name": "Ai-Thinker ESP-C3-M1-I-Kit",
"variant": VARIANT_ESP32C3,
},
+ "esp32-c6-devkitc-1": {
+ "name": "Espressif ESP32-C6-DevKitC-1",
+ "variant": VARIANT_ESP32C6,
+ },
+ "esp32-c6-devkitm-1": {
+ "name": "Espressif ESP32-C6-DevKitM-1",
+ "variant": VARIANT_ESP32C6,
+ },
"esp32cam": {
"name": "AI Thinker ESP32-CAM",
"variant": VARIANT_ESP32,
@@ -1346,6 +1628,14 @@ BOARDS = {
"name": "OLIMEX ESP32-GATEWAY",
"variant": VARIANT_ESP32,
},
+ "esp32-h2-devkitm-1": {
+ "name": "Espressif ESP32-H2-DevKit",
+ "variant": VARIANT_ESP32H2,
+ },
+ "esp32-pico-devkitm-2": {
+ "name": "Espressif ESP32-PICO-DevKitM-2",
+ "variant": VARIANT_ESP32,
+ },
"esp32-poe-iso": {
"name": "OLIMEX ESP32-PoE-ISO",
"variant": VARIANT_ESP32,
@@ -1382,10 +1672,22 @@ BOARDS = {
"name": "Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)",
"variant": VARIANT_ESP32S3,
},
- "esp32-s3-korvo-2": {
- "name": "Espressif ESP32-S3-Korvo-2",
+ "esp32-s3-devkitm-1": {
+ "name": "Espressif ESP32-S3-DevKitM-1",
"variant": VARIANT_ESP32S3,
},
+ "esp32s3_powerfeather": {
+ "name": "ESP32-S3 PowerFeather",
+ "variant": VARIANT_ESP32S3,
+ },
+ "esp32s3usbotg": {
+ "name": "Espressif ESP32-S3-USB-OTG",
+ "variant": VARIANT_ESP32S3,
+ },
+ "esp32-solo1": {
+ "name": "Espressif Generic ESP32-solo1 4M Flash",
+ "variant": VARIANT_ESP32,
+ },
"esp32thing": {
"name": "SparkFun ESP32 Thing",
"variant": VARIANT_ESP32,
@@ -1454,9 +1756,9 @@ BOARDS = {
"name": "Heltec WiFi Kit 32",
"variant": VARIANT_ESP32,
},
- "heltec_wifi_kit_32_v2": {
- "name": "Heltec WiFi Kit 32 (V2)",
- "variant": VARIANT_ESP32,
+ "heltec_wifi_kit_32_V3": {
+ "name": "Heltec WiFi Kit 32 (V3)",
+ "variant": VARIANT_ESP32S3,
},
"heltec_wifi_lora_32": {
"name": "Heltec WiFi LoRa 32",
@@ -1466,6 +1768,10 @@ BOARDS = {
"name": "Heltec WiFi LoRa 32 (V2)",
"variant": VARIANT_ESP32,
},
+ "heltec_wifi_lora_32_V3": {
+ "name": "Heltec WiFi LoRa 32 (V3)",
+ "variant": VARIANT_ESP32S3,
+ },
"heltec_wireless_stick_lite": {
"name": "Heltec Wireless Stick Lite",
"variant": VARIANT_ESP32,
@@ -1510,6 +1816,14 @@ BOARDS = {
"name": "oddWires IoT-Bus Proteus",
"variant": VARIANT_ESP32,
},
+ "ioxesp32": {
+ "name": "ArtronShop IOXESP32",
+ "variant": VARIANT_ESP32,
+ },
+ "ioxesp32ps": {
+ "name": "ArtronShop IOXESP32PS",
+ "variant": VARIANT_ESP32,
+ },
"kb32-ft": {
"name": "MakerAsia KB32-FT",
"variant": VARIANT_ESP32,
@@ -1522,10 +1836,26 @@ BOARDS = {
"name": "Labplus mPython",
"variant": VARIANT_ESP32,
},
+ "lilka_v2": {
+ "name": "Lilka v2",
+ "variant": VARIANT_ESP32S3,
+ },
+ "lilygo-t-display": {
+ "name": "LilyGo T-Display",
+ "variant": VARIANT_ESP32,
+ },
+ "lilygo-t-display-s3": {
+ "name": "LilyGo T-Display-S3",
+ "variant": VARIANT_ESP32S3,
+ },
"lionbit": {
"name": "Lion:Bit Dev Board",
"variant": VARIANT_ESP32,
},
+ "lionbits3": {
+ "name": "Lion:Bit S3 STEM Dev Board",
+ "variant": VARIANT_ESP32S3,
+ },
"lolin32_lite": {
"name": "WEMOS LOLIN32 Lite",
"variant": VARIANT_ESP32,
@@ -1554,10 +1884,18 @@ BOARDS = {
"name": "WEMOS LOLIN S2 PICO",
"variant": VARIANT_ESP32S2,
},
+ "lolin_s3_mini": {
+ "name": "WEMOS LOLIN S3 Mini",
+ "variant": VARIANT_ESP32S3,
+ },
"lolin_s3": {
"name": "WEMOS LOLIN S3",
"variant": VARIANT_ESP32S3,
},
+ "lolin_s3_pro": {
+ "name": "WEMOS LOLIN S3 PRO",
+ "variant": VARIANT_ESP32S3,
+ },
"lopy4": {
"name": "Pycom LoPy4",
"variant": VARIANT_ESP32,
@@ -1570,10 +1908,18 @@ BOARDS = {
"name": "M5Stack-ATOM",
"variant": VARIANT_ESP32,
},
+ "m5stack-atoms3": {
+ "name": "M5Stack AtomS3",
+ "variant": VARIANT_ESP32S3,
+ },
"m5stack-core2": {
"name": "M5Stack Core2",
"variant": VARIANT_ESP32,
},
+ "m5stack-core-esp32-16M": {
+ "name": "M5Stack Core ESP32 16M",
+ "variant": VARIANT_ESP32,
+ },
"m5stack-core-esp32": {
"name": "M5Stack Core ESP32",
"variant": VARIANT_ESP32,
@@ -1582,6 +1928,10 @@ BOARDS = {
"name": "M5Stack-Core Ink",
"variant": VARIANT_ESP32,
},
+ "m5stack-cores3": {
+ "name": "M5Stack CoreS3",
+ "variant": VARIANT_ESP32S3,
+ },
"m5stack-fire": {
"name": "M5Stack FIRE",
"variant": VARIANT_ESP32,
@@ -1590,6 +1940,14 @@ BOARDS = {
"name": "M5Stack GREY ESP32",
"variant": VARIANT_ESP32,
},
+ "m5stack_paper": {
+ "name": "M5Stack Paper",
+ "variant": VARIANT_ESP32,
+ },
+ "m5stack-stamps3": {
+ "name": "M5Stack StampS3",
+ "variant": VARIANT_ESP32S3,
+ },
"m5stack-station": {
"name": "M5Stack Station",
"variant": VARIANT_ESP32,
@@ -1598,6 +1956,10 @@ BOARDS = {
"name": "M5Stack Timer CAM",
"variant": VARIANT_ESP32,
},
+ "m5stamp-pico": {
+ "name": "M5Stamp-Pico",
+ "variant": VARIANT_ESP32,
+ },
"m5stick-c": {
"name": "M5Stick-C",
"variant": VARIANT_ESP32,
@@ -1634,10 +1996,26 @@ BOARDS = {
"name": "Deparment of Alchemy MiniMain ESP32-S2",
"variant": VARIANT_ESP32S2,
},
+ "motorgo_mini_1": {
+ "name": "MotorGo Mini 1 (ESP32-S3)",
+ "variant": VARIANT_ESP32S3,
+ },
+ "namino_arancio": {
+ "name": "Namino Arancio",
+ "variant": VARIANT_ESP32S3,
+ },
+ "namino_rosso": {
+ "name": "Namino Rosso",
+ "variant": VARIANT_ESP32S3,
+ },
"nano32": {
"name": "MakerAsia Nano32",
"variant": VARIANT_ESP32,
},
+ "nebulas3": {
+ "name": "Kinetic Dynamics Nebula S3",
+ "variant": VARIANT_ESP32S3,
+ },
"nina_w10": {
"name": "u-blox NINA-W10 series",
"variant": VARIANT_ESP32,
@@ -1698,10 +2076,22 @@ BOARDS = {
"name": "Munich Labs RedPill ESP32-S3",
"variant": VARIANT_ESP32S3,
},
+ "roboheart_hercules": {
+ "name": "RoboHeart Hercules",
+ "variant": VARIANT_ESP32,
+ },
"seeed_xiao_esp32c3": {
"name": "Seeed Studio XIAO ESP32C3",
"variant": VARIANT_ESP32C3,
},
+ "seeed_xiao_esp32s3": {
+ "name": "Seeed Studio XIAO ESP32S3",
+ "variant": VARIANT_ESP32S3,
+ },
+ "sensebox_mcu_esp32s2": {
+ "name": "senseBox MCU-S2 ESP32-S2",
+ "variant": VARIANT_ESP32S2,
+ },
"sensesiot_weizen": {
"name": "LOGISENSES Senses Weizen",
"variant": VARIANT_ESP32,
@@ -1714,6 +2104,10 @@ BOARDS = {
"name": "S.ODI Ultra v1",
"variant": VARIANT_ESP32,
},
+ "sparkfun_esp32c6_thing_plus": {
+ "name": "Sparkfun ESP32-C6 Thing Plus",
+ "variant": VARIANT_ESP32C6,
+ },
"sparkfun_esp32_iot_redboard": {
"name": "SparkFun ESP32 IoT RedBoard",
"variant": VARIANT_ESP32,
@@ -1806,6 +2200,10 @@ BOARDS = {
"name": "Unexpected Maker FeatherS3",
"variant": VARIANT_ESP32S3,
},
+ "um_nanos3": {
+ "name": "Unexpected Maker NanoS3",
+ "variant": VARIANT_ESP32S3,
+ },
"um_pros3": {
"name": "Unexpected Maker PROS3",
"variant": VARIANT_ESP32S3,
@@ -1842,6 +2240,14 @@ BOARDS = {
"name": "uPesy ESP32 Wrover DevKit",
"variant": VARIANT_ESP32,
},
+ "valtrack_v4_mfw_esp32_c3": {
+ "name": "Valetron Systems VALTRACK-V4MVF",
+ "variant": VARIANT_ESP32C3,
+ },
+ "valtrack_v4_vts_esp32_c3": {
+ "name": "Valetron Systems VALTRACK-V4VTS",
+ "variant": VARIANT_ESP32C3,
+ },
"vintlabs-devkit-v1": {
"name": "VintLabs ESP32 Devkit",
"variant": VARIANT_ESP32,
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/__init__.py b/esphome/components/esp32_ble/__init__.py
index 75cf9d707d..08e30c9247 100644
--- a/esphome/components/esp32_ble/__init__.py
+++ b/esphome/components/esp32_ble/__init__.py
@@ -2,8 +2,10 @@ from esphome import automation
import esphome.codegen as cg
from esphome.components.esp32 import add_idf_sdkconfig_option, const, get_esp32_variant
import esphome.config_validation as cv
-from esphome.const import CONF_ENABLE_ON_BOOT, CONF_ID
+from esphome.const import CONF_ENABLE_ON_BOOT, CONF_ESPHOME, CONF_ID, CONF_NAME
from esphome.core import CORE
+from esphome.core.config import CONF_NAME_ADD_MAC_SUFFIX
+import esphome.final_validate as fv
DEPENDENCIES = ["esp32"]
CODEOWNERS = ["@jesserockz", "@Rapsssito"]
@@ -50,6 +52,7 @@ TX_POWER_LEVELS = {
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(ESP32BLE),
+ cv.Optional(CONF_NAME): cv.All(cv.string, cv.Length(max=20)),
cv.Optional(CONF_IO_CAPABILITY, default="none"): cv.enum(
IO_CAPABILITY, lower=True
),
@@ -67,7 +70,22 @@ def validate_variant(_):
raise cv.Invalid(f"{variant} does not support Bluetooth")
-FINAL_VALIDATE_SCHEMA = validate_variant
+def final_validation(config):
+ validate_variant(config)
+ if (name := config.get(CONF_NAME)) is not None:
+ full_config = fv.full_config.get()
+ max_length = 20
+ if full_config[CONF_ESPHOME][CONF_NAME_ADD_MAC_SUFFIX]:
+ max_length -= 7 # "-AABBCC" is appended when add mac suffix option is used
+ if len(name) > max_length:
+ raise cv.Invalid(
+ f"Name '{name}' is too long, maximum length is {max_length} characters"
+ )
+
+ return config
+
+
+FINAL_VALIDATE_SCHEMA = final_validation
async def to_code(config):
@@ -75,6 +93,8 @@ async def to_code(config):
cg.add(var.set_enable_on_boot(config[CONF_ENABLE_ON_BOOT]))
cg.add(var.set_io_capability(config[CONF_IO_CAPABILITY]))
cg.add(var.set_advertising_cycle_time(config[CONF_ADVERTISING_CYCLE_TIME]))
+ if (name := config.get(CONF_NAME)) is not None:
+ cg.add(var.set_name(name))
await cg.register_component(var, config)
if CORE.using_esp_idf:
diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp
index 5d08b6e973..d7dcf93f86 100644
--- a/esphome/components/esp32_ble/ble.cpp
+++ b/esphome/components/esp32_ble/ble.cpp
@@ -188,12 +188,20 @@ bool ESP32BLE::ble_setup_() {
}
}
- std::string name = App.get_name();
- if (name.length() > 20) {
+ std::string name;
+ if (this->name_.has_value()) {
+ name = this->name_.value();
if (App.is_name_add_mac_suffix_enabled()) {
- name.erase(name.begin() + 13, name.end() - 7); // Remove characters between 13 and the mac address
- } else {
- name = name.substr(0, 20);
+ name += "-" + get_mac_address().substr(6);
+ }
+ } else {
+ name = App.get_name();
+ if (name.length() > 20) {
+ if (App.is_name_add_mac_suffix_enabled()) {
+ name.erase(name.begin() + 13, name.end() - 7); // Remove characters between 13 and the mac address
+ } else {
+ name = name.substr(0, 20);
+ }
}
}
diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h
index 7c55583852..ed7575f128 100644
--- a/esphome/components/esp32_ble/ble.h
+++ b/esphome/components/esp32_ble/ble.h
@@ -90,6 +90,7 @@ class ESP32BLE : public Component {
void loop() override;
void dump_config() override;
float get_setup_priority() const override;
+ void set_name(const std::string &name) { this->name_ = name; }
void advertising_start();
void advertising_set_service_data(const std::vector &data);
@@ -131,6 +132,7 @@ class ESP32BLE : public Component {
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
uint32_t advertising_cycle_time_;
bool enable_on_boot_;
+ optional name_;
};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
diff --git a/esphome/components/esp32_ble/ble_advertising.cpp b/esphome/components/esp32_ble/ble_advertising.cpp
index 1d340c76d9..92b7c60368 100644
--- a/esphome/components/esp32_ble/ble_advertising.cpp
+++ b/esphome/components/esp32_ble/ble_advertising.cpp
@@ -83,7 +83,7 @@ esp_err_t BLEAdvertising::services_advertisement_() {
esp_err_t err;
this->advertising_data_.set_scan_rsp = false;
- this->advertising_data_.include_name = !this->scan_response_;
+ this->advertising_data_.include_name = true;
this->advertising_data_.include_txpower = !this->scan_response_;
err = esp_ble_gap_config_adv_data(&this->advertising_data_);
if (err != ESP_OK) {
diff --git a/esphome/components/esp32_ble/ble_uuid.cpp b/esphome/components/esp32_ble/ble_uuid.cpp
index 07ac719434..aa1edd96b2 100644
--- a/esphome/components/esp32_ble/ble_uuid.cpp
+++ b/esphome/components/esp32_ble/ble_uuid.cpp
@@ -34,7 +34,7 @@ ESPBTUUID ESPBTUUID::from_raw(const uint8_t *data) {
ESPBTUUID ESPBTUUID::from_raw_reversed(const uint8_t *data) {
ESPBTUUID ret;
ret.uuid_.len = ESP_UUID_LEN_128;
- for (int i = 0; i < ESP_UUID_LEN_128; i++)
+ for (uint8_t i = 0; i < ESP_UUID_LEN_128; i++)
ret.uuid_.uuid.uuid128[ESP_UUID_LEN_128 - 1 - i] = data[i];
return ret;
}
@@ -43,30 +43,30 @@ ESPBTUUID ESPBTUUID::from_raw(const std::string &data) {
if (data.length() == 4) {
ret.uuid_.len = ESP_UUID_LEN_16;
ret.uuid_.uuid.uuid16 = 0;
- for (int i = 0; i < data.length();) {
+ for (uint i = 0; i < data.length(); i += 2) {
uint8_t msb = data.c_str()[i];
uint8_t lsb = data.c_str()[i + 1];
+ uint8_t lsb_shift = i <= 2 ? (2 - i) * 4 : 0;
if (msb > '9')
msb -= 7;
if (lsb > '9')
lsb -= 7;
- ret.uuid_.uuid.uuid16 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << (2 - i) * 4;
- i += 2;
+ ret.uuid_.uuid.uuid16 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << lsb_shift;
}
} else if (data.length() == 8) {
ret.uuid_.len = ESP_UUID_LEN_32;
ret.uuid_.uuid.uuid32 = 0;
- for (int i = 0; i < data.length();) {
+ for (uint i = 0; i < data.length(); i += 2) {
uint8_t msb = data.c_str()[i];
uint8_t lsb = data.c_str()[i + 1];
+ uint8_t lsb_shift = i <= 6 ? (6 - i) * 4 : 0;
if (msb > '9')
msb -= 7;
if (lsb > '9')
lsb -= 7;
- ret.uuid_.uuid.uuid32 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << (6 - i) * 4;
- i += 2;
+ ret.uuid_.uuid.uuid32 += (((msb & 0x0F) << 4) | (lsb & 0x0F)) << lsb_shift;
}
} else if (data.length() == 16) { // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be
// investigated (lack of time)
@@ -77,7 +77,7 @@ ESPBTUUID ESPBTUUID::from_raw(const std::string &data) {
// UUID format.
ret.uuid_.len = ESP_UUID_LEN_128;
int n = 0;
- for (int i = 0; i < data.length();) {
+ for (uint i = 0; i < data.length(); i += 2) {
if (data.c_str()[i] == '-')
i++;
uint8_t msb = data.c_str()[i];
@@ -88,7 +88,6 @@ ESPBTUUID ESPBTUUID::from_raw(const std::string &data) {
if (lsb > '9')
lsb -= 7;
ret.uuid_.uuid.uuid128[15 - n++] = ((msb & 0x0F) << 4) | (lsb & 0x0F);
- i += 2;
}
} else {
ESP_LOGE(TAG, "ERROR: UUID value not 2, 4, 16 or 36 bytes - %s", data.c_str());
@@ -155,7 +154,7 @@ bool ESPBTUUID::operator==(const ESPBTUUID &uuid) const {
}
break;
case ESP_UUID_LEN_128:
- for (int i = 0; i < ESP_UUID_LEN_128; i++) {
+ for (uint8_t i = 0; i < ESP_UUID_LEN_128; i++) {
if (uuid.uuid_.uuid.uuid128[i] != this->uuid_.uuid.uuid128[i]) {
return false;
}
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..6d051e3d4a 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
@@ -429,7 +432,7 @@ void ESPBTDevice::parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_e
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
ESP_LOGVV(TAG, "Parse Result:");
- const char *address_type = "";
+ const char *address_type;
switch (this->address_type_) {
case BLE_ADDR_TYPE_PUBLIC:
address_type = "PUBLIC";
@@ -443,6 +446,9 @@ void ESPBTDevice::parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_e
case BLE_ADDR_TYPE_RPA_RANDOM:
address_type = "RPA_RANDOM";
break;
+ default:
+ address_type = "UNKNOWN";
+ break;
}
ESP_LOGVV(TAG, " Address: %02X:%02X:%02X:%02X:%02X:%02X (%s)", this->address_[0], this->address_[1],
this->address_[2], this->address_[3], this->address_[4], this->address_[5], address_type);
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/esp32_camera_web_server/camera_web_server.h b/esphome/components/esp32_camera_web_server/camera_web_server.h
index f65625554c..3ba8f31dd7 100644
--- a/esphome/components/esp32_camera_web_server/camera_web_server.h
+++ b/esphome/components/esp32_camera_web_server/camera_web_server.h
@@ -11,7 +11,7 @@
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
-struct httpd_req;
+struct httpd_req; // NOLINT(readability-identifier-naming)
namespace esphome {
namespace esp32_camera_web_server {
diff --git a/esphome/components/esp32_rmt_led_strip/light.py b/esphome/components/esp32_rmt_led_strip/light.py
index 79f339e248..976f70e858 100644
--- a/esphome/components/esp32_rmt_led_strip/light.py
+++ b/esphome/components/esp32_rmt_led_strip/light.py
@@ -1,9 +1,9 @@
from dataclasses import dataclass
-import esphome.codegen as cg
-import esphome.config_validation as cv
from esphome import pins
+import esphome.codegen as cg
from esphome.components import esp32_rmt, light
+import esphome.config_validation as cv
from esphome.const import (
CONF_CHIPSET,
CONF_IS_RGBW,
@@ -103,7 +103,7 @@ CONFIG_SCHEMA = cv.All(
default="0 us",
): cv.positive_time_period_nanoseconds,
}
- ),
+ ).extend(cv.COMPONENT_SCHEMA),
cv.has_exactly_one_key(CONF_CHIPSET, CONF_BIT0_HIGH),
)
diff --git a/esphome/components/esp8266/__init__.py b/esphome/components/esp8266/__init__.py
index 64b127bda3..c73027fe1b 100644
--- a/esphome/components/esp8266/__init__.py
+++ b/esphome/components/esp8266/__init__.py
@@ -1,10 +1,13 @@
import logging
import os
+import esphome.codegen as cg
+import esphome.config_validation as cv
from esphome.const import (
CONF_BOARD,
CONF_BOARD_FLASH_MODE,
CONF_FRAMEWORK,
+ CONF_PLATFORM_VERSION,
CONF_SOURCE,
CONF_VERSION,
KEY_CORE,
@@ -12,27 +15,22 @@ from esphome.const import (
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
PLATFORM_ESP8266,
- CONF_PLATFORM_VERSION,
)
from esphome.core import CORE, coroutine_with_priority
-import esphome.config_validation as cv
-import esphome.codegen as cg
from esphome.helpers import copy_file_if_changed
+from .boards import BOARDS, ESP8266_LD_SCRIPTS
from .const import (
- CONF_RESTORE_FROM_FLASH,
CONF_EARLY_PIN_INIT,
+ CONF_RESTORE_FROM_FLASH,
KEY_BOARD,
KEY_ESP8266,
KEY_FLASH_SIZE,
KEY_PIN_INITIAL_STATES,
esp8266_ns,
)
-from .boards import BOARDS, ESP8266_LD_SCRIPTS
-
from .gpio import PinInitialState, add_pin_initial_states_array
-
CODEOWNERS = ["@esphome/core"]
_LOGGER = logging.getLogger(__name__)
AUTO_LOAD = ["preferences"]
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/ezo/ezo.cpp b/esphome/components/ezo/ezo.cpp
index 8e4486dbf2..10f3d530ce 100644
--- a/esphome/components/ezo/ezo.cpp
+++ b/esphome/components/ezo/ezo.cpp
@@ -111,11 +111,11 @@ void EZOSensor::loop() {
if (buf[0] == 1) {
std::string payload = reinterpret_cast(&buf[1]);
if (!payload.empty()) {
+ auto start_location = payload.find(',');
switch (to_run->command_type) {
case EzoCommandType::EZO_READ: {
// some sensors return multiple comma-separated values, terminate string after first one
- int start_location = 0;
- if ((start_location = payload.find(',')) != std::string::npos) {
+ if (start_location != std::string::npos) {
payload.erase(start_location);
}
auto val = parse_number(payload);
@@ -126,49 +126,37 @@ void EZOSensor::loop() {
}
break;
}
- case EzoCommandType::EZO_LED: {
+ case EzoCommandType::EZO_LED:
this->led_callback_.call(payload.back() == '1');
break;
- }
- case EzoCommandType::EZO_DEVICE_INFORMATION: {
- int start_location = 0;
- if ((start_location = payload.find(',')) != std::string::npos) {
+ case EzoCommandType::EZO_DEVICE_INFORMATION:
+ if (start_location != std::string::npos) {
this->device_infomation_callback_.call(payload.substr(start_location + 1));
}
break;
- }
- case EzoCommandType::EZO_SLOPE: {
- int start_location = 0;
- if ((start_location = payload.find(',')) != std::string::npos) {
+ case EzoCommandType::EZO_SLOPE:
+ if (start_location != std::string::npos) {
this->slope_callback_.call(payload.substr(start_location + 1));
}
break;
- }
- case EzoCommandType::EZO_CALIBRATION: {
- int start_location = 0;
- if ((start_location = payload.find(',')) != std::string::npos) {
+ case EzoCommandType::EZO_CALIBRATION:
+ if (start_location != std::string::npos) {
this->calibration_callback_.call(payload.substr(start_location + 1));
}
break;
- }
- case EzoCommandType::EZO_T: {
- int start_location = 0;
- if ((start_location = payload.find(',')) != std::string::npos) {
+ case EzoCommandType::EZO_T:
+ if (start_location != std::string::npos) {
this->t_callback_.call(payload.substr(start_location + 1));
}
break;
- }
- case EzoCommandType::EZO_CUSTOM: {
+ case EzoCommandType::EZO_CUSTOM:
this->custom_callback_.call(payload);
break;
- }
- default: {
+ default:
break;
- }
}
}
}
-
this->commands_.pop_front();
}
@@ -178,7 +166,7 @@ void EZOSensor::add_command_(const std::string &command, EzoCommandType command_
ezo_command->command_type = command_type;
ezo_command->delay_ms = delay_ms;
this->commands_.push_back(std::move(ezo_command));
-};
+}
void EZOSensor::set_calibration_point_(EzoCalibrationType type, float value) {
std::string payload = str_sprintf("Cal,%s,%0.2f", EZO_CALIBRATION_TYPE_STRINGS[type], value);
diff --git a/esphome/components/font/__init__.py b/esphome/components/font/__init__.py
index dacd0779b1..c397ba8306 100644
--- a/esphome/components/font/__init__.py
+++ b/esphome/components/font/__init__.py
@@ -5,7 +5,8 @@ import os
from pathlib import Path
import re
-from packaging import version
+import freetype
+import glyphsets
import requests
from esphome import core, external_files
@@ -43,6 +44,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,57 +72,109 @@ 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, 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
-def validate_pillow_installed(value):
- try:
- import PIL
- except ImportError as err:
- raise cv.Invalid(
- "Please install the pillow python package to use this feature. "
- '(pip install "pillow==10.4.0")'
- ) from err
-
- if version.parse(PIL.__version__) != version.parse("10.4.0"):
- raise cv.Invalid(
- "Please update your pillow installation to 10.4.0. "
- '(pip install "pillow==10.4.0")'
- )
-
- return value
-
-
FONT_EXTENSIONS = (".ttf", ".woff", ".otf")
@@ -120,7 +185,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 +204,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 +236,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 +272,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 +281,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 +294,6 @@ EXTERNAL_FONT_SCHEMA = cv.Schema(
}
)
-
GFONTS_SCHEMA = cv.All(
EXTERNAL_FONT_SCHEMA.extend(
{
@@ -259,10 +327,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 +338,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 +364,37 @@ 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°'
)
+
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 +403,7 @@ FONT_SCHEMA = cv.Schema(
},
)
-CONFIG_SCHEMA = cv.All(validate_pillow_installed, FONT_SCHEMA, merge_glyphs)
+CONFIG_SCHEMA = cv.All(FONT_SCHEMA, validate_glyphs)
# PIL doesn't provide a consistent interface for both TrueType and bitmap
@@ -344,7 +419,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 +434,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 +442,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 +467,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 +477,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 +503,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 +518,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 +556,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 +571,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 +608,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/font/font.cpp b/esphome/components/font/font.cpp
index aeca0f5cc0..8c4cba34b3 100644
--- a/esphome/components/font/font.cpp
+++ b/esphome/components/font/font.cpp
@@ -133,9 +133,11 @@ void Font::print(int x_start, int y_start, display::Display *display, Color colo
auto diff_r = (float) color.r - (float) background.r;
auto diff_g = (float) color.g - (float) background.g;
auto diff_b = (float) color.b - (float) background.b;
+ auto diff_w = (float) color.w - (float) background.w;
auto b_r = (float) background.r;
auto b_g = (float) background.g;
- auto b_b = (float) background.g;
+ auto b_b = (float) background.b;
+ auto b_w = (float) background.w;
for (int glyph_y = y_start + scan_y1; glyph_y != max_y; glyph_y++) {
for (int glyph_x = x_at + scan_x1; glyph_x != max_x; glyph_x++) {
uint8_t pixel = 0;
@@ -153,8 +155,8 @@ void Font::print(int x_start, int y_start, display::Display *display, Color colo
display->draw_pixel_at(glyph_x, glyph_y, color);
} else if (pixel != 0) {
auto on = (float) pixel / (float) bpp_max;
- auto blended =
- Color((uint8_t) (diff_r * on + b_r), (uint8_t) (diff_g * on + b_g), (uint8_t) (diff_b * on + b_b));
+ auto blended = Color((uint8_t) (diff_r * on + b_r), (uint8_t) (diff_g * on + b_g),
+ (uint8_t) (diff_b * on + b_b), (uint8_t) (diff_w * on + b_w));
display->draw_pixel_at(glyph_x, glyph_y, blended);
}
}
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/graphical_display_menu/__init__.py b/esphome/components/graphical_display_menu/__init__.py
index f4d59b22b8..56b720e75c 100644
--- a/esphome/components/graphical_display_menu/__init__.py
+++ b/esphome/components/graphical_display_menu/__init__.py
@@ -36,6 +36,8 @@ CODEOWNERS = ["@MrMDavidson"]
AUTO_LOAD = ["display_menu_base"]
+MULTI_CONF = True
+
CONFIG_SCHEMA = DISPLAY_MENU_BASE_SCHEMA.extend(
cv.Schema(
{
diff --git a/esphome/components/haier/hon_climate.cpp b/esphome/components/haier/hon_climate.cpp
index e7be1fa418..c95a87223d 100644
--- a/esphome/components/haier/hon_climate.cpp
+++ b/esphome/components/haier/hon_climate.cpp
@@ -35,7 +35,9 @@ void HonClimate::set_beeper_state(bool state) {
if (state != this->settings_.beeper_state) {
this->settings_.beeper_state = state;
#ifdef USE_SWITCH
- this->beeper_switch_->publish_state(state);
+ if (this->beeper_switch_ != nullptr) {
+ this->beeper_switch_->publish_state(state);
+ }
#endif
this->hon_rtc_.save(&this->settings_);
}
@@ -45,10 +47,17 @@ bool HonClimate::get_beeper_state() const { return this->settings_.beeper_state;
void HonClimate::set_quiet_mode_state(bool state) {
if (state != this->get_quiet_mode_state()) {
- this->quiet_mode_state_ = state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF;
+ if ((this->mode != ClimateMode::CLIMATE_MODE_OFF) && (this->mode != ClimateMode::CLIMATE_MODE_FAN_ONLY)) {
+ this->quiet_mode_state_ = state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF;
+ this->force_send_control_ = true;
+ } else {
+ this->quiet_mode_state_ = state ? SwitchState::ON : SwitchState::OFF;
+ }
this->settings_.quiet_mode_state = state;
#ifdef USE_SWITCH
- this->quiet_mode_switch_->publish_state(state);
+ if (this->quiet_mode_switch_ != nullptr) {
+ this->quiet_mode_switch_->publish_state(state);
+ }
#endif
this->hon_rtc_.save(&this->settings_);
}
@@ -509,7 +518,7 @@ void HonClimate::initialization() {
}
this->current_vertical_swing_ = this->settings_.last_vertiacal_swing;
this->current_horizontal_swing_ = this->settings_.last_horizontal_swing;
- this->quiet_mode_state_ = this->settings_.quiet_mode_state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF;
+ this->quiet_mode_state_ = this->settings_.quiet_mode_state ? SwitchState::ON : SwitchState::OFF;
}
haier_protocol::HaierMessage HonClimate::get_control_message() {
@@ -932,7 +941,7 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
if (this->mode == CLIMATE_MODE_OFF) {
// AC just turned on from remote need to turn off display
this->force_send_control_ = true;
- } else if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
+ } else if ((((uint8_t) this->display_status_) & 0b10) == 0) {
this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
}
}
@@ -1004,6 +1013,11 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
if (new_quiet_mode != this->get_quiet_mode_state()) {
this->quiet_mode_state_ = new_quiet_mode ? SwitchState::ON : SwitchState::OFF;
this->settings_.quiet_mode_state = new_quiet_mode;
+#ifdef USE_SWITCH
+ if (this->quiet_mode_switch_ != nullptr) {
+ this->quiet_mode_switch_->publish_state(new_quiet_mode);
+ }
+#endif // USE_SWITCH
this->hon_rtc_.save(&this->settings_);
}
}
@@ -1069,19 +1083,17 @@ void HonClimate::fill_control_messages_queue_() {
climate_control = this->current_hvac_settings_;
// Beeper command
{
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::BEEPER_STATUS,
- this->get_beeper_state() ? ZERO_BUF : ONE_BUF, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::BEEPER_STATUS,
+ this->get_beeper_state() ? ZERO_BUF : ONE_BUF, 2);
}
// Health mode
{
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::HEALTH_MODE,
- this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::HEALTH_MODE,
+ this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2);
this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
}
// Climate mode
@@ -1099,51 +1111,46 @@ void HonClimate::fill_control_messages_queue_() {
case CLIMATE_MODE_HEAT_COOL:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::AUTO;
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::AC_MODE,
- buffer, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::AC_MODE,
+ buffer, 2);
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_HEAT:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::HEAT;
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::AC_MODE,
- buffer, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::AC_MODE,
+ buffer, 2);
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_DRY:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::DRY;
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::AC_MODE,
- buffer, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::AC_MODE,
+ buffer, 2);
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_FAN_ONLY:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::FAN;
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::AC_MODE,
- buffer, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::AC_MODE,
+ buffer, 2);
fan_mode_buf[1] = this->other_modes_fan_speed_; // Auto doesn't work in fan only mode
break;
case CLIMATE_MODE_COOL:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::COOL;
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::AC_MODE,
- buffer, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::AC_MODE,
+ buffer, 2);
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
default:
@@ -1153,11 +1160,10 @@ void HonClimate::fill_control_messages_queue_() {
}
// Climate power
{
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::AC_POWER,
- new_power ? ONE_BUF : ZERO_BUF, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::AC_POWER,
+ new_power ? ONE_BUF : ZERO_BUF, 2);
}
// CLimate preset
{
@@ -1199,36 +1205,32 @@ void HonClimate::fill_control_messages_queue_() {
}
auto presets = this->traits_.get_supported_presets();
if (quiet_mode_buf[1] != 0xFF) {
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::QUIET_MODE,
- quiet_mode_buf, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::QUIET_MODE,
+ quiet_mode_buf, 2);
}
if ((fast_mode_buf[1] != 0xFF) && ((presets.find(climate::ClimatePreset::CLIMATE_PRESET_BOOST) != presets.end()))) {
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::FAST_MODE,
- fast_mode_buf, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::FAST_MODE,
+ fast_mode_buf, 2);
}
if ((away_mode_buf[1] != 0xFF) && ((presets.find(climate::ClimatePreset::CLIMATE_PRESET_AWAY) != presets.end()))) {
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::TEN_DEGREE,
- away_mode_buf, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::TEN_DEGREE,
+ away_mode_buf, 2);
}
}
// Target temperature
if (climate_control.target_temperature.has_value() && (this->mode != ClimateMode::CLIMATE_MODE_FAN_ONLY)) {
uint8_t buffer[2] = {0x00, 0x00};
buffer[1] = ((uint8_t) climate_control.target_temperature.value()) - 16;
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::SET_POINT,
- buffer, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::SET_POINT,
+ buffer, 2);
}
// Vertical swing mode
if (climate_control.swing_mode.has_value()) {
@@ -1248,16 +1250,14 @@ void HonClimate::fill_control_messages_queue_() {
case CLIMATE_SWING_BOTH:
break;
}
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::HORIZONTAL_SWING_MODE,
- horizontal_swing_buf, 2));
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::VERTICAL_SWING_MODE,
- vertical_swing_buf, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::HORIZONTAL_SWING_MODE,
+ horizontal_swing_buf, 2);
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::VERTICAL_SWING_MODE,
+ vertical_swing_buf, 2);
}
// Fan mode
if (climate_control.fan_mode.has_value()) {
@@ -1280,11 +1280,10 @@ void HonClimate::fill_control_messages_queue_() {
break;
}
if (fan_mode_buf[1] != 0xFF) {
- this->control_messages_queue_.push(
- haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
- (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
- (uint8_t) hon_protocol::DataParameters::FAN_MODE,
- fan_mode_buf, 2));
+ this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
+ (uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
+ (uint8_t) hon_protocol::DataParameters::FAN_MODE,
+ fan_mode_buf, 2);
}
}
}
diff --git a/esphome/components/hbridge/switch/__init__.py b/esphome/components/hbridge/switch/__init__.py
new file mode 100644
index 0000000000..e26bd6b1d8
--- /dev/null
+++ b/esphome/components/hbridge/switch/__init__.py
@@ -0,0 +1,44 @@
+from esphome import pins
+import esphome.codegen as cg
+from esphome.components import switch
+import esphome.config_validation as cv
+from esphome.const import CONF_OPTIMISTIC, CONF_PULSE_LENGTH, CONF_WAIT_TIME
+
+from .. import hbridge_ns
+
+HBridgeSwitch = hbridge_ns.class_("HBridgeSwitch", switch.Switch, cg.Component)
+
+CODEOWNERS = ["@dwmw2"]
+
+CONF_OFF_PIN = "off_pin"
+CONF_ON_PIN = "on_pin"
+
+CONFIG_SCHEMA = (
+ switch.switch_schema(HBridgeSwitch)
+ .extend(
+ {
+ cv.Required(CONF_ON_PIN): pins.gpio_output_pin_schema,
+ cv.Required(CONF_OFF_PIN): pins.gpio_output_pin_schema,
+ cv.Optional(
+ CONF_PULSE_LENGTH, default="100ms"
+ ): cv.positive_time_period_milliseconds,
+ cv.Optional(CONF_WAIT_TIME): cv.positive_time_period_milliseconds,
+ cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
+ }
+ )
+ .extend(cv.COMPONENT_SCHEMA)
+)
+
+
+async def to_code(config):
+ var = await switch.new_switch(config)
+ await cg.register_component(var, config)
+
+ on_pin = await cg.gpio_pin_expression(config[CONF_ON_PIN])
+ cg.add(var.set_on_pin(on_pin))
+ off_pin = await cg.gpio_pin_expression(config[CONF_OFF_PIN])
+ cg.add(var.set_off_pin(off_pin))
+ cg.add(var.set_pulse_length(config[CONF_PULSE_LENGTH]))
+ cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
+ if wait_time := config.get(CONF_WAIT_TIME):
+ cg.add(var.set_wait_time(wait_time))
diff --git a/esphome/components/hbridge/switch/hbridge_switch.cpp b/esphome/components/hbridge/switch/hbridge_switch.cpp
new file mode 100644
index 0000000000..12d1c01bca
--- /dev/null
+++ b/esphome/components/hbridge/switch/hbridge_switch.cpp
@@ -0,0 +1,95 @@
+#include "hbridge_switch.h"
+#include "esphome/core/log.h"
+
+#include
+
+namespace esphome {
+namespace hbridge {
+
+static const char *const TAG = "switch.hbridge";
+
+float HBridgeSwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
+void HBridgeSwitch::setup() {
+ ESP_LOGCONFIG(TAG, "Setting up H-Bridge Switch '%s'...", this->name_.c_str());
+
+ optional initial_state = this->get_initial_state_with_restore_mode().value_or(false);
+
+ // Like GPIOSwitch does, set the pin state both before and after pin setup()
+ this->on_pin_->digital_write(false);
+ this->on_pin_->setup();
+ this->on_pin_->digital_write(false);
+
+ this->off_pin_->digital_write(false);
+ this->off_pin_->setup();
+ this->off_pin_->digital_write(false);
+
+ if (initial_state.has_value())
+ this->write_state(initial_state);
+}
+
+void HBridgeSwitch::dump_config() {
+ LOG_SWITCH("", "H-Bridge Switch", this);
+ LOG_PIN(" On Pin: ", this->on_pin_);
+ LOG_PIN(" Off Pin: ", this->off_pin_);
+ ESP_LOGCONFIG(TAG, " Pulse length: %" PRId32 " ms", this->pulse_length_);
+ if (this->wait_time_)
+ ESP_LOGCONFIG(TAG, " Wait time %" PRId32 " ms", this->wait_time_);
+}
+
+void HBridgeSwitch::write_state(bool state) {
+ this->desired_state_ = state;
+ if (!this->timer_running_)
+ this->timer_fn_();
+}
+
+void HBridgeSwitch::timer_fn_() {
+ uint32_t next_timeout = 0;
+
+ while ((uint8_t) this->desired_state_ != this->relay_state_) {
+ switch (this->relay_state_) {
+ case RELAY_STATE_ON:
+ case RELAY_STATE_OFF:
+ case RELAY_STATE_UNKNOWN:
+ if (this->desired_state_) {
+ this->on_pin_->digital_write(true);
+ this->relay_state_ = RELAY_STATE_SWITCHING_ON;
+ } else {
+ this->off_pin_->digital_write(true);
+ this->relay_state_ = RELAY_STATE_SWITCHING_OFF;
+ }
+ next_timeout = this->pulse_length_;
+ if (!this->optimistic_)
+ this->publish_state(this->desired_state_);
+ break;
+
+ case RELAY_STATE_SWITCHING_ON:
+ this->on_pin_->digital_write(false);
+ this->relay_state_ = RELAY_STATE_ON;
+ if (this->optimistic_)
+ this->publish_state(true);
+ next_timeout = this->wait_time_;
+ break;
+
+ case RELAY_STATE_SWITCHING_OFF:
+ this->off_pin_->digital_write(false);
+ this->relay_state_ = RELAY_STATE_OFF;
+ if (this->optimistic_)
+ this->publish_state(false);
+ next_timeout = this->wait_time_;
+ break;
+ }
+
+ if (next_timeout) {
+ this->timer_running_ = true;
+ this->set_timeout(next_timeout, [this]() { this->timer_fn_(); });
+ return;
+ }
+
+ // In the case where ON/OFF state has been reached but we need to
+ // immediately change back again to reach desired_state_, we loop.
+ }
+ this->timer_running_ = false;
+}
+
+} // namespace hbridge
+} // namespace esphome
diff --git a/esphome/components/hbridge/switch/hbridge_switch.h b/esphome/components/hbridge/switch/hbridge_switch.h
new file mode 100644
index 0000000000..ce00c6baa2
--- /dev/null
+++ b/esphome/components/hbridge/switch/hbridge_switch.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include "esphome/core/component.h"
+#include "esphome/core/hal.h"
+#include "esphome/components/switch/switch.h"
+
+#include
+
+namespace esphome {
+namespace hbridge {
+
+enum RelayState : uint8_t {
+ RELAY_STATE_OFF = 0,
+ RELAY_STATE_ON = 1,
+ RELAY_STATE_SWITCHING_ON = 2,
+ RELAY_STATE_SWITCHING_OFF = 3,
+ RELAY_STATE_UNKNOWN = 4,
+};
+
+class HBridgeSwitch : public switch_::Switch, public Component {
+ public:
+ void set_on_pin(GPIOPin *pin) { this->on_pin_ = pin; }
+ void set_off_pin(GPIOPin *pin) { this->off_pin_ = pin; }
+ void set_pulse_length(uint32_t pulse_length) { this->pulse_length_ = pulse_length; }
+ void set_wait_time(uint32_t wait_time) { this->wait_time_ = wait_time; }
+ void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
+
+ // ========== INTERNAL METHODS ==========
+ // (In most use cases you won't need these)
+ float get_setup_priority() const override;
+
+ void setup() override;
+ void dump_config() override;
+
+ protected:
+ void write_state(bool state) override;
+ void timer_fn_();
+
+ bool timer_running_{false};
+ bool desired_state_{false};
+ RelayState relay_state_{RELAY_STATE_UNKNOWN};
+ GPIOPin *on_pin_{nullptr};
+ GPIOPin *off_pin_{nullptr};
+ uint32_t pulse_length_{0};
+ uint32_t wait_time_{0};
+ bool optimistic_{false};
+};
+
+} // namespace hbridge
+} // namespace esphome
diff --git a/esphome/components/hitachi_ac344/hitachi_ac344.cpp b/esphome/components/hitachi_ac344/hitachi_ac344.cpp
index 2825e4f04c..2bcb205644 100644
--- a/esphome/components/hitachi_ac344/hitachi_ac344.cpp
+++ b/esphome/components/hitachi_ac344/hitachi_ac344.cpp
@@ -133,8 +133,10 @@ bool HitachiClimate::get_swing_v_() {
}
void HitachiClimate::set_swing_h_(uint8_t position) {
- if (position > HITACHI_AC344_SWINGH_LEFT_MAX)
- return set_swing_h_(HITACHI_AC344_SWINGH_MIDDLE);
+ if (position > HITACHI_AC344_SWINGH_LEFT_MAX) {
+ set_swing_h_(HITACHI_AC344_SWINGH_MIDDLE);
+ return;
+ }
set_bits(&remote_state_[HITACHI_AC344_SWINGH_BYTE], HITACHI_AC344_SWINGH_OFFSET, HITACHI_AC344_SWINGH_SIZE, position);
set_button_(HITACHI_AC344_BUTTON_SWINGH);
}
diff --git a/esphome/components/hitachi_ac424/hitachi_ac424.cpp b/esphome/components/hitachi_ac424/hitachi_ac424.cpp
index 0bfc3ae564..64f23dfc17 100644
--- a/esphome/components/hitachi_ac424/hitachi_ac424.cpp
+++ b/esphome/components/hitachi_ac424/hitachi_ac424.cpp
@@ -133,8 +133,10 @@ bool HitachiClimate::get_swing_v_() {
}
void HitachiClimate::set_swing_h_(uint8_t position) {
- if (position > HITACHI_AC424_SWINGH_LEFT_MAX)
- return set_swing_h_(HITACHI_AC424_SWINGH_MIDDLE);
+ if (position > HITACHI_AC424_SWINGH_LEFT_MAX) {
+ set_swing_h_(HITACHI_AC424_SWINGH_MIDDLE);
+ return;
+ }
set_bits(&remote_state_[HITACHI_AC424_SWINGH_BYTE], HITACHI_AC424_SWINGH_OFFSET, HITACHI_AC424_SWINGH_SIZE, position);
set_button_(HITACHI_AC424_BUTTON_SWINGH);
}
diff --git a/esphome/components/homeassistant/number/homeassistant_number.cpp b/esphome/components/homeassistant/number/homeassistant_number.cpp
index d3e285f4ac..a7f71c3244 100644
--- a/esphome/components/homeassistant/number/homeassistant_number.cpp
+++ b/esphome/components/homeassistant/number/homeassistant_number.cpp
@@ -27,6 +27,7 @@ void HomeassistantNumber::min_retrieved_(const std::string &min) {
auto min_value = parse_number(min);
if (!min_value.has_value()) {
ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_.c_str(), min.c_str());
+ return;
}
ESP_LOGD(TAG, "'%s': Min retrieved: %s", get_name().c_str(), min.c_str());
this->traits.set_min_value(min_value.value());
@@ -36,6 +37,7 @@ void HomeassistantNumber::max_retrieved_(const std::string &max) {
auto max_value = parse_number(max);
if (!max_value.has_value()) {
ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_.c_str(), max.c_str());
+ return;
}
ESP_LOGD(TAG, "'%s': Max retrieved: %s", get_name().c_str(), max.c_str());
this->traits.set_max_value(max_value.value());
@@ -45,6 +47,7 @@ void HomeassistantNumber::step_retrieved_(const std::string &step) {
auto step_value = parse_number(step);
if (!step_value.has_value()) {
ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_.c_str(), step.c_str());
+ return;
}
ESP_LOGD(TAG, "'%s': Step Retrieved %s", get_name().c_str(), step.c_str());
this->traits.set_step(step_value.value());
diff --git a/esphome/components/honeywellabp2_i2c/honeywellabp2.cpp b/esphome/components/honeywellabp2_i2c/honeywellabp2.cpp
index e2910032cc..d111723669 100644
--- a/esphome/components/honeywellabp2_i2c/honeywellabp2.cpp
+++ b/esphome/components/honeywellabp2_i2c/honeywellabp2.cpp
@@ -15,7 +15,7 @@ static const char *const TAG = "honeywellabp2";
void HONEYWELLABP2Sensor::read_sensor_data() {
if (this->read(raw_data_, 7) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with ABP2 failed!");
- this->mark_failed();
+ this->status_set_warning("couldn't read sensor data");
return;
}
float press_counts = encode_uint24(raw_data_[1], raw_data_[2], raw_data_[3]); // calculate digital pressure counts
@@ -25,12 +25,13 @@ void HONEYWELLABP2Sensor::read_sensor_data() {
(this->max_pressure_ - this->min_pressure_)) +
this->min_pressure_;
this->last_temperature_ = (temp_counts * 200 / 16777215) - 50;
+ this->status_clear_warning();
}
void HONEYWELLABP2Sensor::start_measurement() {
if (this->write(i2c_cmd_, 3) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with ABP2 failed!");
- this->mark_failed();
+ this->status_set_warning("couldn't start measurement");
return;
}
this->measurement_running_ = true;
@@ -39,7 +40,7 @@ void HONEYWELLABP2Sensor::start_measurement() {
bool HONEYWELLABP2Sensor::is_measurement_ready() {
if (this->read(raw_data_, 1) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with ABP2 failed!");
- this->mark_failed();
+ this->status_set_warning("couldn't check measurement");
return false;
}
if ((raw_data_[0] & (0x1 << STATUS_BIT_BUSY)) > 0) {
@@ -52,7 +53,7 @@ bool HONEYWELLABP2Sensor::is_measurement_ready() {
void HONEYWELLABP2Sensor::measurement_timeout() {
ESP_LOGE(TAG, "Timeout!");
this->measurement_running_ = false;
- this->mark_failed();
+ this->status_set_warning("measurement timed out");
}
float HONEYWELLABP2Sensor::get_pressure() { return this->last_pressure_; }
@@ -79,7 +80,7 @@ void HONEYWELLABP2Sensor::update() {
ESP_LOGV(TAG, "Update Honeywell ABP2 Sensor");
this->start_measurement();
- this->set_timeout("meas_timeout", 50, [this] { this->measurement_timeout(); });
+ this->set_timeout("meas_timeout", 100, [this] { this->measurement_timeout(); });
}
void HONEYWELLABP2Sensor::dump_config() {
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/hx711/hx711.cpp b/esphome/components/hx711/hx711.cpp
index 1a7169eed7..9643d0c411 100644
--- a/esphome/components/hx711/hx711.cpp
+++ b/esphome/components/hx711/hx711.cpp
@@ -53,7 +53,7 @@ bool HX711Sensor::read_sensor_(uint32_t *result) {
}
// Cycle clock pin for gain setting
- for (uint8_t i = 0; i < this->gain_; i++) {
+ for (uint8_t i = 0; i < static_cast(this->gain_); i++) {
this->sck_pin_->digital_write(true);
delayMicroseconds(1);
this->sck_pin_->digital_write(false);
diff --git a/esphome/components/hx711/hx711.h b/esphome/components/hx711/hx711.h
index 0cb6868ab5..a92bb9945d 100644
--- a/esphome/components/hx711/hx711.h
+++ b/esphome/components/hx711/hx711.h
@@ -9,7 +9,7 @@
namespace esphome {
namespace hx711 {
-enum HX711Gain {
+enum HX711Gain : uint8_t {
HX711_GAIN_128 = 1,
HX711_GAIN_32 = 2,
HX711_GAIN_64 = 3,
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..d2a582c2cc 100644
--- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
+++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
@@ -13,32 +13,35 @@
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_TASK_FAILED_TO_START = (1 << 15),
- ERR_ESP_INVALID_STATE = (1 << 16),
+ ERR_TASK_FAILED_TO_START = (1 << 14),
+ ERR_ESP_INVALID_STATE = (1 << 15),
+ ERR_ESP_NOT_SUPPORTED = (1 << 16),
ERR_ESP_INVALID_ARG = (1 << 17),
ERR_ESP_INVALID_SIZE = (1 << 18),
ERR_ESP_NO_MEM = (1 << 19),
ERR_ESP_FAIL = (1 << 20),
- ALL_ERR_ESP_BITS = ERR_ESP_INVALID_STATE | ERR_ESP_INVALID_ARG | ERR_ESP_INVALID_SIZE | ERR_ESP_NO_MEM | ERR_ESP_FAIL,
+ ALL_ERR_ESP_BITS = ERR_ESP_INVALID_STATE | ERR_ESP_NOT_SUPPORTED | ERR_ESP_INVALID_ARG | ERR_ESP_INVALID_SIZE |
+ ERR_ESP_NO_MEM | ERR_ESP_FAIL,
ALL_BITS = 0x00FFFFFF, // All valid FreeRTOS event group bits
};
@@ -53,6 +56,8 @@ static esp_err_t err_bit_to_esp_err(uint32_t bit) {
return ESP_ERR_INVALID_SIZE;
case SpeakerEventGroupBits::ERR_ESP_NO_MEM:
return ESP_ERR_NO_MEM;
+ case SpeakerEventGroupBits::ERR_ESP_NOT_SUPPORTED:
+ return ESP_ERR_NOT_SUPPORTED;
default:
return ESP_FAIL;
}
@@ -90,9 +95,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 +107,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 +132,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::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::ERR_ESP_NOT_SUPPORTED) {
+ 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);
+ }
}
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 +201,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 {
@@ -200,35 +239,31 @@ void I2SAudioSpeaker::speaker_task(void *params) {
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_STARTING);
audio::AudioStreamInfo audio_stream_info = this_speaker->audio_stream_info_;
- 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 uint32_t bytes_per_ms =
+ audio_stream_info.channels * audio_stream_info.get_bytes_per_sample() * audio_stream_info.sample_rate / 1000;
- if (this_speaker->send_esp_err_to_event_group_(
- this_speaker->allocate_buffers_(dma_buffers_size, RING_BUFFER_SAMPLES * bytes_per_sample))) {
+ const size_t dma_buffers_size = DMA_BUFFERS_COUNT * DMA_BUFFER_DURATION_MS * bytes_per_ms;
+
+ // Ensure ring buffer is at least as large as the total size of the DMA buffers
+ const size_t ring_buffer_size =
+ std::min((uint32_t) dma_buffers_size, this_speaker->buffer_duration_ms_ * bytes_per_ms);
+
+ 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);
}
- 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))) {
- // Successfully set the I2S stream info, ready to write audio data to the I2S port
-
+ if (!this_speaker->send_esp_err_to_event_group_(this_speaker->start_i2s_driver_(audio_stream_info))) {
xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_RUNNING);
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 +273,24 @@ void I2SAudioSpeaker::speaker_task(void *params) {
stop_gracefully = true;
}
+ if (this_speaker->audio_stream_info_ != audio_stream_info) {
+ // Audio stream info has changed, stop the speaker task so it will restart with the proper settings.
+
+ break;
+ }
+
+ 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,31 +311,28 @@ 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());
}
}
+
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_STOPPING);
+
+ i2s_driver_uninstall(this_speaker->parent_->get_port());
+
+ this_speaker->parent_->unlock();
}
- 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();
this_speaker->delete_task_(dma_buffers_size);
}
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;
@@ -339,6 +383,9 @@ bool I2SAudioSpeaker::send_esp_err_to_event_group_(esp_err_t err) {
case ESP_ERR_NO_MEM:
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_NO_MEM);
return true;
+ case ESP_ERR_NOT_SUPPORTED:
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_NOT_SUPPORTED);
+ return true;
default:
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_FAIL);
return true;
@@ -356,8 +403,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);
}
@@ -368,20 +415,44 @@ esp_err_t I2SAudioSpeaker::allocate_buffers_(size_t data_buffer_size, size_t rin
return ESP_OK;
}
-esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
+esp_err_t I2SAudioSpeaker::start_i2s_driver_(audio::AudioStreamInfo &audio_stream_info) {
+ if ((this->i2s_mode_ & I2S_MODE_SLAVE) && (this->sample_rate_ != audio_stream_info.sample_rate)) { // NOLINT
+ // Can't reconfigure I2S bus, so the sample rate must match the configured value
+ return ESP_ERR_NOT_SUPPORTED;
+ }
+
+ if ((i2s_bits_per_sample_t) audio_stream_info.bits_per_sample > this->bits_per_sample_) {
+ // Currently can't handle the case when the incoming audio has more bits per sample than the configured value
+ return ESP_ERR_NOT_SUPPORTED;
+ }
+
if (!this->parent_->try_lock()) {
return ESP_ERR_INVALID_STATE;
}
+ i2s_channel_fmt_t channel = this->channel_;
+
+ if (audio_stream_info.channels == 1) {
+ if (this->channel_ == I2S_CHANNEL_FMT_ONLY_LEFT) {
+ channel = I2S_CHANNEL_FMT_ONLY_LEFT;
+ } else {
+ channel = I2S_CHANNEL_FMT_ONLY_RIGHT;
+ }
+ } else if (audio_stream_info.channels == 2) {
+ channel = I2S_CHANNEL_FMT_RIGHT_LEFT;
+ }
+
+ 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_,
+ .sample_rate = audio_stream_info.sample_rate,
.bits_per_sample = this->bits_per_sample_,
- .channel_format = this->channel_,
+ .channel_format = channel,
.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 +473,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();
@@ -431,41 +503,8 @@ esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
return err;
}
-esp_err_t I2SAudioSpeaker::reconfigure_i2s_stream_info_(audio::AudioStreamInfo &audio_stream_info) {
- if (this->i2s_mode_ & I2S_MODE_MASTER) {
- // ESP controls for the the I2S bus, so adjust the sample rate and bits per sample to match the incoming audio
- this->sample_rate_ = audio_stream_info.sample_rate;
- this->bits_per_sample_ = (i2s_bits_per_sample_t) audio_stream_info.bits_per_sample;
- } else if (this->sample_rate_ != audio_stream_info.sample_rate) {
- // Can't reconfigure I2S bus, so the sample rate must match the configured value
- return ESP_ERR_INVALID_ARG;
- }
-
- if ((i2s_bits_per_sample_t) audio_stream_info.bits_per_sample > this->bits_per_sample_) {
- // Currently can't handle the case when the incoming audio has more bits per sample than the configured value
- return ESP_ERR_INVALID_ARG;
- }
-
- if (audio_stream_info.channels == 1) {
- return i2s_set_clk(this->parent_->get_port(), this->sample_rate_, this->bits_per_sample_, I2S_CHANNEL_MONO);
- } else if (audio_stream_info.channels == 2) {
- return i2s_set_clk(this->parent_->get_port(), this->sample_rate_, this->bits_per_sample_, I2S_CHANNEL_STEREO);
- }
-
- return ESP_ERR_INVALID_ARG;
-}
-
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..d706deb0f4 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.
@@ -83,24 +91,15 @@ class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Comp
esp_err_t allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size);
/// @brief Starts the ESP32 I2S driver.
- /// Attempts to lock the I2S port, starts the I2S driver, and sets the data out pin. If it fails, it will unlock
- /// the I2S port and uninstall the driver, if necessary.
- /// @return ESP_ERR_INVALID_STATE if the I2S port is already locked.
- /// ESP_ERR_INVALID_ARG if installing the driver or setting the data out pin fails due to a parameter error.
+ /// Attempts to lock the I2S port, starts the I2S driver using the passed in stream information, and sets the data out
+ /// pin. If it fails, it will unlock the I2S port and uninstall the driver, if necessary.
+ /// @param audio_stream_info Stream information for the I2S driver.
+ /// @return ESP_ERR_NOT_ALLOWED if the I2S port can't play the incoming audio stream.
+ /// ESP_ERR_INVALID_STATE if the I2S port is already locked.
+ /// ESP_ERR_INVALID_ARG if nstalling the driver or setting the data outpin fails due to a parameter error.
/// ESP_ERR_NO_MEM if the driver fails to install due to a memory allocation error.
- /// ESP_FAIL if setting the data out pin fails due to an IO error
- /// ESP_OK if successful
- esp_err_t start_i2s_driver_();
-
- /// @brief Adjusts the I2S driver configuration to match the incoming audio stream.
- /// Modifies I2S driver's sample rate, bits per sample, and number of channel settings. If the I2S is in secondary
- /// mode, it only modifies the number of channels.
- /// @param audio_stream_info Describes the incoming audio stream
- /// @return ESP_ERR_INVALID_ARG if there is a parameter error, if there is more than 2 channels in the stream, or if
- /// the audio settings are incompatible with the configuration.
- /// ESP_ERR_NO_MEM if the driver fails to reconfigure due to a memory allocation error.
- /// ESP_OK if successful.
- esp_err_t reconfigure_i2s_stream_info_(audio::AudioStreamInfo &audio_stream_info);
+ /// ESP_FAIL if setting the data out pin fails due to an IO error ESP_OK if successful
+ esp_err_t start_i2s_driver_(audio::AudioStreamInfo &audio_stream_info);
/// @brief Deletes the speaker's task.
/// Deallocates the data_buffer_ and audio_ring_buffer_, if necessary, and deletes the task. Should only be called by
@@ -111,10 +110,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..3c9dd2dab9 100644
--- a/esphome/components/ili9xxx/display.py
+++ b/esphome/components/ili9xxx/display.py
@@ -1,6 +1,6 @@
from esphome import core, pins
import esphome.codegen as cg
-from esphome.components import display, font, spi
+from esphome.components import display, spi
from esphome.components.display import validate_rotation
import esphome.config_validation as cv
from esphome.const import (
@@ -147,7 +147,6 @@ def _validate(config):
CONFIG_SCHEMA = cv.All(
- font.validate_pillow_installed,
display.FULL_DISPLAY_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(ILI9XXXDisplay),
@@ -196,6 +195,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/ili9xxx/ili9xxx_display.cpp b/esphome/components/ili9xxx/ili9xxx_display.cpp
index 81976dd2c9..b9664067a9 100644
--- a/esphome/components/ili9xxx/ili9xxx_display.cpp
+++ b/esphome/components/ili9xxx/ili9xxx_display.cpp
@@ -313,8 +313,9 @@ void ILI9XXXDisplay::draw_pixels_at(int x_start, int y_start, int w, int h, cons
// do color conversion pixel-by-pixel into the buffer and draw it later. If this is happening the user has not
// configured the renderer well.
if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || !big_endian) {
- return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
- x_pad);
+ display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
+ x_pad);
+ return;
}
this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
// x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
diff --git a/esphome/components/image/__init__.py b/esphome/components/image/__init__.py
index c72417bcda..4669a3418a 100644
--- a/esphome/components/image/__init__.py
+++ b/esphome/components/image/__init__.py
@@ -10,7 +10,6 @@ import puremagic
from esphome import core, external_files
import esphome.codegen as cg
-from esphome.components import font
import esphome.config_validation as cv
from esphome.const import (
CONF_DITHER,
@@ -233,7 +232,7 @@ IMAGE_SCHEMA = cv.Schema(
)
)
-CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, IMAGE_SCHEMA)
+CONFIG_SCHEMA = IMAGE_SCHEMA
def load_svg_image(file: bytes, resize: tuple[int, int]):
@@ -361,24 +360,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/logger/logger.cpp b/esphome/components/logger/logger.cpp
index dac08fbbce..36934c7459 100644
--- a/esphome/components/logger/logger.cpp
+++ b/esphome/components/logger/logger.cpp
@@ -47,7 +47,7 @@ void Logger::write_header_(int level, const char *tag, int line) {
if (current_task == main_task_) {
this->printf_to_buffer_("%s[%s][%s:%03u]: ", color, letter, tag, line);
} else {
- const char *thread_name = "";
+ const char *thread_name = ""; // NOLINT(clang-analyzer-deadcode.DeadStores)
#if defined(USE_ESP32)
thread_name = pcTaskGetName(current_task);
#elif defined(USE_LIBRETINY)
diff --git a/esphome/components/ltr501/ltr501.cpp b/esphome/components/ltr501/ltr501.cpp
index 4f4e26f44f..b30e520f2b 100644
--- a/esphome/components/ltr501/ltr501.cpp
+++ b/esphome/components/ltr501/ltr501.cpp
@@ -23,7 +23,7 @@ bool operator==(const GainTimePair &lhs, const GainTimePair &rhs) {
}
bool operator!=(const GainTimePair &lhs, const GainTimePair &rhs) {
- return !(lhs.gain == rhs.gain && lhs.time == rhs.time);
+ return lhs.gain != rhs.gain || lhs.time != rhs.time;
}
template T get_next(const T (&array)[size], const T val) {
diff --git a/esphome/components/lvgl/__init__.py b/esphome/components/lvgl/__init__.py
index beaf279a9a..8fdd03f647 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 gradients_to_code(config)
+ await styles_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..02323f9655 100644
--- a/esphome/components/lvgl/defines.py
+++ b/esphome/components/lvgl/defines.py
@@ -8,7 +8,7 @@ import logging
from esphome import codegen as cg, config_validation as cv
from esphome.const import CONF_ITEMS
-from esphome.core import Lambda
+from esphome.core import ID, Lambda
from esphome.cpp_generator import LambdaExpression, MockObj
from esphome.cpp_types import uint32
from esphome.schema_extractors import SCHEMA_EXTRACT, schema_extractor
@@ -38,7 +38,7 @@ def literal(arg):
def call_lambda(lamb: LambdaExpression):
expr = lamb.content.strip()
if expr.startswith("return") and expr.endswith(";"):
- return expr[7:][:-1]
+ return expr[6:][:-1].strip()
return f"{lamb}()"
@@ -72,6 +72,12 @@ class LValidator:
)
if self.retmapper is not None:
return self.retmapper(value)
+ if isinstance(value, ID):
+ return await cg.get_variable(value)
+ if isinstance(value, list):
+ value = [
+ await cg.get_variable(x) if isinstance(x, ID) else x for x in value
+ ]
return cg.safe_exp(value)
@@ -162,6 +168,7 @@ LV_EVENT_MAP = {
"READY": "READY",
"CANCEL": "CANCEL",
"ALL_EVENTS": "ALL",
+ "CHANGE": "VALUE_CHANGED",
}
LV_EVENT_TRIGGERS = tuple(f"on_{x.lower()}" for x in LV_EVENT_MAP)
@@ -438,6 +445,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..f91ed893f2 100644
--- a/esphome/components/lvgl/lv_validation.py
+++ b/esphome/components/lvgl/lv_validation.py
@@ -1,6 +1,7 @@
from typing import Union
import esphome.codegen as cg
+from esphome.components import image
from esphome.components.color import CONF_HEX, ColorStruct, from_rgbw
from esphome.components.font import Font
from esphome.components.image import Image_
@@ -30,8 +31,8 @@ from .defines import (
call_lambda,
literal,
)
-from .helpers import esphome_fonts_used, lv_fonts_used, requires_component
-from .types import lv_font_t, lv_gradient_t, lv_img_t
+from .helpers import add_lv_use, esphome_fonts_used, lv_fonts_used, requires_component
+from .types import lv_font_t, lv_gradient_t
opacity_consts = LvConstant("LV_OPA_", "TRANSP", "COVER")
@@ -267,6 +268,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 +278,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)
@@ -325,13 +327,18 @@ def image_validator(value):
value = requires_component("image")(value)
value = cv.use_id(Image_)(value)
lv_images_used.add(value)
+ add_lv_use("img", "label")
return value
lv_image = LValidator(
image_validator,
- lv_img_t,
- retmapper=lambda x: MockObj(x, "->").get_lv_img_dsc(),
+ image.Image_.operator("ptr"),
+ requires="image",
+)
+lv_image_list = LValidator(
+ cv.ensure_list(image_validator),
+ cg.std_vector.template(image.Image_.operator("ptr")),
requires="image",
)
lv_bool = LValidator(cv.boolean, cg.bool_, retmapper=literal)
@@ -403,6 +410,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..61bdfe9755 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_)
@@ -270,7 +279,7 @@ std::string LvSelectable::get_selected_text() {
static std::string join_string(std::vector options) {
return std::accumulate(
options.begin(), options.end(), std::string(),
- [](const std::string &a, const std::string &b) -> std::string { return a + (a.length() > 0 ? "\n" : "") + b; });
+ [](const std::string &a, const std::string &b) -> std::string { return a + (!a.empty() ? "\n" : "") + b; });
}
void LvSelectable::set_selected_text(const std::string &text, lv_anim_enable_t anim) {
@@ -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..56413ad77e 100644
--- a/esphome/components/lvgl/lvgl_esphome.h
+++ b/esphome/components/lvgl/lvgl_esphome.h
@@ -56,7 +56,26 @@ static const display::ColorBitness LV_BITNESS = display::ColorBitness::COLOR_BIT
inline void lv_img_set_src(lv_obj_t *obj, esphome::image::Image *image) {
lv_img_set_src(obj, image->get_lv_img_dsc());
}
+inline void lv_disp_set_bg_image(lv_disp_t *disp, esphome::image::Image *image) {
+ lv_disp_set_bg_image(disp, image->get_lv_img_dsc());
+}
#endif // USE_LVGL_IMAGE
+#ifdef USE_LVGL_ANIMIMG
+inline void lv_animimg_set_src(lv_obj_t *img, std::vector images) {
+ auto *dsc = static_cast *>(lv_obj_get_user_data(img));
+ if (dsc == nullptr) {
+ // object will be lazily allocated but never freed.
+ dsc = new std::vector(images.size()); // NOLINT
+ lv_obj_set_user_data(img, dsc);
+ }
+ dsc->clear();
+ for (auto &image : images) {
+ dsc->push_back(image->get_lv_img_dsc());
+ }
+ lv_animimg_set_src(img, (const void **) dsc->data(), dsc->size());
+}
+
+#endif // USE_LVGL_ANIMIMG
// Parent class for things that wrap an LVGL object
class LvCompound {
@@ -146,10 +165,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 +254,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 +275,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..3f56b3345f 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,
@@ -341,6 +341,7 @@ FLEX_OBJ_SCHEMA = {
cv.Optional(df.CONF_FLEX_GROW): cv.int_,
}
+
DISP_BG_SCHEMA = cv.Schema(
{
cv.Optional(df.CONF_DISP_BG_IMAGE): lv_image,
@@ -391,7 +392,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..b824d28fb8 100644
--- a/esphome/components/lvgl/widgets/animimg.py
+++ b/esphome/components/lvgl/widgets/animimg.py
@@ -1,20 +1,18 @@
from esphome import automation
-import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_DURATION, CONF_ID
from ..automation import action_to_code
from ..defines import CONF_AUTO_START, CONF_MAIN, CONF_REPEAT_COUNT, CONF_SRC
from ..helpers import lvgl_components_required
-from ..lv_validation import lv_image, lv_milliseconds
+from ..lv_validation import lv_image_list, lv_milliseconds
from ..lvcode import lv
-from ..types import LvType, ObjUpdateAction, void_ptr
+from ..types import LvType, ObjUpdateAction
from . import Widget, WidgetType, get_widgets
from .img import CONF_IMAGE
from .label import CONF_LABEL
CONF_ANIMIMG = "animimg"
-CONF_SRC_LIST_ID = "src_list_id"
def lv_repeat_count(value):
@@ -32,14 +30,14 @@ ANIMIMG_BASE_SCHEMA = cv.Schema(
ANIMIMG_SCHEMA = ANIMIMG_BASE_SCHEMA.extend(
{
cv.Required(CONF_DURATION): lv_milliseconds,
- cv.Required(CONF_SRC): cv.ensure_list(lv_image),
- cv.GenerateID(CONF_SRC_LIST_ID): cv.declare_id(void_ptr),
+ cv.Required(CONF_SRC): lv_image_list,
}
)
ANIMIMG_MODIFY_SCHEMA = ANIMIMG_BASE_SCHEMA.extend(
{
cv.Optional(CONF_DURATION): lv_milliseconds,
+ cv.Optional(CONF_SRC): lv_image_list,
}
)
@@ -59,16 +57,14 @@ class AnimimgType(WidgetType):
async def to_code(self, w: Widget, config):
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]]
- 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)
- lv.animimg_set_repeat_count(w.obj, config[CONF_REPEAT_COUNT])
- lv.animimg_set_duration(w.obj, config[CONF_DURATION])
- if config.get(CONF_AUTO_START):
+ if srcs := config.get(CONF_SRC):
+ srcs = await lv_image_list.process(srcs)
+ lv.animimg_set_src(w.obj, srcs)
+ if repeat_count := config.get(CONF_REPEAT_COUNT):
+ lv.animimg_set_repeat_count(w.obj, repeat_count)
+ if duration := config.get(CONF_DURATION):
+ lv.animimg_set_duration(w.obj, duration)
+ if config[CONF_AUTO_START]:
lv.animimg_start(w.obj)
def get_uses(self):
diff --git a/esphome/components/lvgl/widgets/img.py b/esphome/components/lvgl/widgets/img.py
index 59b2c97c63..46077190d0 100644
--- a/esphome/components/lvgl/widgets/img.py
+++ b/esphome/components/lvgl/widgets/img.py
@@ -79,7 +79,7 @@ class ImgType(WidgetType):
if CONF_ANTIALIAS in config:
lv.img_set_antialias(w.obj, config[CONF_ANTIALIAS])
if mode := config.get(CONF_MODE):
- lv.img_set_mode(w.obj, mode)
+ await w.set_property("size_mode", mode)
img_spec = ImgType()
diff --git a/esphome/components/lvgl/widgets/line.py b/esphome/components/lvgl/widgets/line.py
index 4c6439fde4..0156fb1780 100644
--- a/esphome/components/lvgl/widgets/line.py
+++ b/esphome/components/lvgl/widgets/line.py
@@ -35,11 +35,20 @@ LINE_SCHEMA = {
cv.GenerateID(CONF_POINT_LIST_ID): cv.declare_id(lv_point_t),
}
+LINE_MODIFY_SCHEMA = {
+ cv.Optional(CONF_POINTS): cv_point_list,
+ cv.GenerateID(CONF_POINT_LIST_ID): cv.declare_id(lv_point_t),
+}
+
class LineType(WidgetType):
def __init__(self):
super().__init__(
- CONF_LINE, LvType("lv_line_t"), (CONF_MAIN,), LINE_SCHEMA, modify_schema={}
+ CONF_LINE,
+ LvType("lv_line_t"),
+ (CONF_MAIN,),
+ LINE_SCHEMA,
+ modify_schema=LINE_MODIFY_SCHEMA,
)
async def to_code(self, w: Widget, config):
diff --git a/esphome/components/lvgl/widgets/msgbox.py b/esphome/components/lvgl/widgets/msgbox.py
index be0f2100d7..c3393940b6 100644
--- a/esphome/components/lvgl/widgets/msgbox.py
+++ b/esphome/components/lvgl/widgets/msgbox.py
@@ -29,7 +29,7 @@ from ..lvcode import (
)
from ..schemas import STYLE_SCHEMA, STYLED_TEXT_SCHEMA, container_schema, part_schema
from ..types import LV_EVENT, char_ptr, lv_obj_t
-from . import Widget, set_obj_properties
+from . import Widget, add_widgets, set_obj_properties
from .button import button_spec
from .buttonmatrix import (
BUTTONMATRIX_BUTTON_SCHEMA,
@@ -119,6 +119,7 @@ async def msgbox_to_code(top_layer, conf):
button_style = {CONF_ITEMS: button_style}
await set_obj_properties(buttonmatrix_widget, button_style)
await set_obj_properties(msgbox_widget, conf)
+ await add_widgets(msgbox_widget, conf)
async with LambdaContext(EVENT_ARG, where=messagebox_id) as close_action:
outer_widget.add_flag("LV_OBJ_FLAG_HIDDEN")
if close_button:
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/matrix_keypad/__init__.py b/esphome/components/matrix_keypad/__init__.py
index 5250a45732..b2bcde98ec 100644
--- a/esphome/components/matrix_keypad/__init__.py
+++ b/esphome/components/matrix_keypad/__init__.py
@@ -1,8 +1,8 @@
+from esphome import automation, pins
import esphome.codegen as cg
-import esphome.config_validation as cv
-from esphome import pins
from esphome.components import key_provider
-from esphome.const import CONF_ID, CONF_PIN
+import esphome.config_validation as cv
+from esphome.const import CONF_ID, CONF_ON_KEY, CONF_PIN, CONF_TRIGGER_ID
CODEOWNERS = ["@ssieb"]
@@ -14,6 +14,9 @@ matrix_keypad_ns = cg.esphome_ns.namespace("matrix_keypad")
MatrixKeypad = matrix_keypad_ns.class_(
"MatrixKeypad", key_provider.KeyProvider, cg.Component
)
+MatrixKeyTrigger = matrix_keypad_ns.class_(
+ "MatrixKeyTrigger", automation.Trigger.template(cg.uint8)
+)
CONF_KEYPAD_ID = "keypad_id"
CONF_ROWS = "rows"
@@ -47,6 +50,11 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_DEBOUNCE_TIME, default=1): cv.int_range(min=1, max=100),
cv.Optional(CONF_HAS_DIODES): cv.boolean,
cv.Optional(CONF_HAS_PULLDOWNS): cv.boolean,
+ cv.Optional(CONF_ON_KEY): automation.validate_automation(
+ {
+ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MatrixKeyTrigger),
+ }
+ ),
}
),
check_keys,
@@ -73,3 +81,7 @@ async def to_code(config):
cg.add(var.set_has_diodes(config[CONF_HAS_DIODES]))
if CONF_HAS_PULLDOWNS in config:
cg.add(var.set_has_pulldowns(config[CONF_HAS_PULLDOWNS]))
+ for conf in config.get(CONF_ON_KEY, []):
+ trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
+ cg.add(var.register_key_trigger(trigger))
+ await automation.build_automation(trigger, [(cg.uint8, "x")], conf)
diff --git a/esphome/components/matrix_keypad/binary_sensor/matrix_keypad_binary_sensor.h b/esphome/components/matrix_keypad/binary_sensor/matrix_keypad_binary_sensor.h
index d8a217f55e..2c1ce96f0a 100644
--- a/esphome/components/matrix_keypad/binary_sensor/matrix_keypad_binary_sensor.h
+++ b/esphome/components/matrix_keypad/binary_sensor/matrix_keypad_binary_sensor.h
@@ -6,7 +6,7 @@
namespace esphome {
namespace matrix_keypad {
-class MatrixKeypadBinarySensor : public MatrixKeypadListener, public binary_sensor::BinarySensor {
+class MatrixKeypadBinarySensor : public MatrixKeypadListener, public binary_sensor::BinarySensorInitiallyOff {
public:
MatrixKeypadBinarySensor(uint8_t key) : has_key_(true), key_(key){};
MatrixKeypadBinarySensor(const char *key) : has_key_(true), key_((uint8_t) key[0]){};
diff --git a/esphome/components/matrix_keypad/matrix_keypad.cpp b/esphome/components/matrix_keypad/matrix_keypad.cpp
index f62c75c869..8537997935 100644
--- a/esphome/components/matrix_keypad/matrix_keypad.cpp
+++ b/esphome/components/matrix_keypad/matrix_keypad.cpp
@@ -86,6 +86,8 @@ void MatrixKeypad::loop() {
if (!this->keys_.empty()) {
uint8_t keycode = this->keys_[key];
ESP_LOGD(TAG, "key '%c' pressed", keycode);
+ for (auto &trigger : this->key_triggers_)
+ trigger->trigger(keycode);
for (auto &listener : this->listeners_)
listener->key_pressed(keycode);
this->send_key_(keycode);
@@ -107,5 +109,7 @@ void MatrixKeypad::dump_config() {
void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); }
+void MatrixKeypad::register_key_trigger(MatrixKeyTrigger *trig) { this->key_triggers_.push_back(trig); }
+
} // namespace matrix_keypad
} // namespace esphome
diff --git a/esphome/components/matrix_keypad/matrix_keypad.h b/esphome/components/matrix_keypad/matrix_keypad.h
index d506040b7c..8b309b42c2 100644
--- a/esphome/components/matrix_keypad/matrix_keypad.h
+++ b/esphome/components/matrix_keypad/matrix_keypad.h
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/components/key_provider/key_provider.h"
+#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
@@ -18,6 +19,8 @@ class MatrixKeypadListener {
virtual void key_released(uint8_t key){};
};
+class MatrixKeyTrigger : public Trigger {};
+
class MatrixKeypad : public key_provider::KeyProvider, public Component {
public:
void setup() override;
@@ -31,6 +34,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
void set_has_pulldowns(int has_pulldowns) { has_pulldowns_ = has_pulldowns; };
void register_listener(MatrixKeypadListener *listener);
+ void register_key_trigger(MatrixKeyTrigger *trig);
protected:
std::vector rows_;
@@ -42,6 +46,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
int pressed_key_ = -1;
std::vector listeners_{};
+ std::vector key_triggers_;
};
} // namespace matrix_keypad
diff --git a/esphome/components/max31865/max31865.cpp b/esphome/components/max31865/max31865.cpp
index b48aa2fdd3..4749874ac7 100644
--- a/esphome/components/max31865/max31865.cpp
+++ b/esphome/components/max31865/max31865.cpp
@@ -106,7 +106,8 @@ void MAX31865Sensor::read_data_() {
// Check faults
const uint8_t faults = this->read_register_(FAULT_STATUS_REG);
- if ((has_fault_ = faults & 0b00111100)) {
+ has_fault_ = faults & 0b00111100;
+ if (has_fault_) {
if (faults & (1 << 2)) {
ESP_LOGE(TAG, "Overvoltage/undervoltage fault");
}
@@ -125,7 +126,8 @@ void MAX31865Sensor::read_data_() {
} else {
this->status_clear_error();
}
- if ((has_warn_ = faults & 0b11000000)) {
+ has_warn_ = faults & 0b11000000;
+ if (has_warn_) {
if (faults & (1 << 6)) {
ESP_LOGW(TAG, "RTD Low Threshold");
}
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..47deea83e6 100644
--- a/esphome/components/modbus/modbus.cpp
+++ b/esphome/components/modbus/modbus.cpp
@@ -15,23 +15,34 @@ 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 +50,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 +155,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..2a08075831 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(ModbusOfflineTrigger),
+ }
+ ),
}
)
.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..3f487abc94 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;
@@ -148,11 +152,11 @@ void ModbusController::on_modbus_read_registers(uint8_t function_code, uint16_t
}
SensorSet ModbusController::find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const {
- auto reg_it = find_if(begin(register_ranges_), end(register_ranges_), [=](RegisterRange const &r) {
- return (r.start_address == start_address && r.register_type == register_type);
- });
+ auto reg_it = std::find_if(
+ std::begin(this->register_ranges_), std::end(this->register_ranges_),
+ [=](RegisterRange const &r) { return (r.start_address == start_address && r.register_type == register_type); });
- if (reg_it == register_ranges_.end()) {
+ if (reg_it == this->register_ranges_.end()) {
ESP_LOGE(TAG, "No matching range for sensor found - start_address : 0x%X", start_address);
} else {
return reg_it->sensors;
@@ -236,18 +240,18 @@ void ModbusController::update() {
// walk through the sensors and determine the register ranges to read
size_t ModbusController::create_register_ranges_() {
- register_ranges_.clear();
- if (this->parent_->role == modbus::ModbusRole::CLIENT && sensorset_.empty()) {
+ this->register_ranges_.clear();
+ if (this->parent_->role == modbus::ModbusRole::CLIENT && this->sensorset_.empty()) {
ESP_LOGW(TAG, "No sensors registered");
return 0;
}
// iterator is sorted see SensorItemsComparator for details
- auto ix = sensorset_.begin();
+ auto ix = this->sensorset_.begin();
RegisterRange r = {};
uint8_t buffer_offset = 0;
SensorItem *prev = nullptr;
- while (ix != sensorset_.end()) {
+ while (ix != this->sensorset_.end()) {
SensorItem *curr = *ix;
ESP_LOGV(TAG, "Register: 0x%X %d %d %d offset=%u skip=%u addr=%p", curr->start_address, curr->register_count,
@@ -274,12 +278,12 @@ size_t ModbusController::create_register_ranges_() {
// this register can re-use the data from the previous register
// remove this sensore because start_address is changed (sort-order)
- ix = sensorset_.erase(ix);
+ ix = this->sensorset_.erase(ix);
curr->start_address = r.start_address;
curr->offset += prev->offset;
- sensorset_.insert(curr);
+ this->sensorset_.insert(curr);
// move iterator backwards because it will be incremented later
ix--;
@@ -289,14 +293,14 @@ size_t ModbusController::create_register_ranges_() {
// this register can extend the current range
// remove this sensore because start_address is changed (sort-order)
- ix = sensorset_.erase(ix);
+ ix = this->sensorset_.erase(ix);
curr->start_address = r.start_address;
curr->offset += buffer_offset;
buffer_offset += curr->get_register_size();
r.register_count += curr->register_count;
- sensorset_.insert(curr);
+ this->sensorset_.insert(curr);
// move iterator backwards because it will be incremented later
ix--;
@@ -323,7 +327,7 @@ size_t ModbusController::create_register_ranges_() {
ix++;
} else {
ESP_LOGV(TAG, "Add range 0x%X %d skip:%d", r.start_address, r.register_count, r.skip_updates);
- register_ranges_.push_back(r);
+ this->register_ranges_.push_back(r);
r = {};
buffer_offset = 0;
// do not increment the iterator here because the current sensor has to be re-evaluated
@@ -335,10 +339,10 @@ size_t ModbusController::create_register_ranges_() {
if (r.register_count > 0) {
// Add the last range
ESP_LOGV(TAG, "Add last range 0x%X %d skip:%d", r.start_address, r.register_count, r.skip_updates);
- register_ranges_.push_back(r);
+ this->register_ranges_.push_back(r);
}
- return register_ranges_.size();
+ return this->register_ranges_.size();
}
void ModbusController::dump_config() {
@@ -348,18 +352,18 @@ void ModbusController::dump_config() {
ESP_LOGCONFIG(TAG, " Offline Skip Updates: %d", this->offline_skip_updates_);
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
ESP_LOGCONFIG(TAG, "sensormap");
- for (auto &it : sensorset_) {
+ for (auto &it : this->sensorset_) {
ESP_LOGCONFIG(TAG, " Sensor type=%zu start=0x%X offset=0x%X count=%d size=%d",
static_cast(it->register_type), it->start_address, it->offset, it->register_count,
it->get_register_size());
}
ESP_LOGCONFIG(TAG, "ranges");
- for (auto &it : register_ranges_) {
+ for (auto &it : this->register_ranges_) {
ESP_LOGCONFIG(TAG, " Range type=%zu start=0x%X count=%d skip_updates=%d", static_cast(it.register_type),
it.start_address, it.register_count, it.skip_updates);
}
ESP_LOGCONFIG(TAG, "server registers");
- for (auto &r : server_registers_) {
+ for (auto &r : this->server_registers_) {
ESP_LOGCONFIG(TAG, " Address=0x%02X value_type=%zu register_count=%u", r->address,
static_cast(r->value_type), r->register_count);
}
@@ -368,15 +372,15 @@ void ModbusController::dump_config() {
void ModbusController::loop() {
// Incoming data to process?
- if (!incoming_queue_.empty()) {
- auto &message = incoming_queue_.front();
+ if (!this->incoming_queue_.empty()) {
+ auto &message = this->incoming_queue_.front();
if (message != nullptr)
- process_modbus_data_(message.get());
- incoming_queue_.pop();
+ this->process_modbus_data_(message.get());
+ this->incoming_queue_.pop();
} else {
// all messages processed send pending commands
- send_next_command_();
+ this->send_next_command_();
}
}
@@ -387,7 +391,7 @@ void ModbusController::on_write_register_response(ModbusRegisterType register_ty
void ModbusController::dump_sensors_() {
ESP_LOGV(TAG, "sensors");
- for (auto &it : sensorset_) {
+ for (auto &it : this->sensorset_) {
ESP_LOGV(TAG, " Sensor start=0x%X count=%d size=%d offset=%d", it->start_address, it->register_count,
it->get_register_size(), it->offset);
}
@@ -618,51 +622,87 @@ int64_t payload_to_number(const std::vector &data, SensorValueType sens
uint32_t bitmask) {
int64_t value = 0; // int64_t because it can hold signed and unsigned 32 bits
+ size_t size = data.size() - offset;
+ bool error = false;
switch (sensor_value_type) {
case SensorValueType::U_WORD:
- value = mask_and_shift_by_rightbit(get_data(data, offset), bitmask); // default is 0xFFFF ;
+ if (size >= 2) {
+ value = mask_and_shift_by_rightbit(get_data(data, offset), bitmask); // default is 0xFFFF ;
+ } else {
+ error = true;
+ }
break;
case SensorValueType::U_DWORD:
case SensorValueType::FP32:
- value = get_data(data, offset);
- value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
+ if (size >= 4) {
+ value = get_data(data, offset);
+ value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
+ } else {
+ error = true;
+ }
break;
case SensorValueType::U_DWORD_R:
case SensorValueType::FP32_R:
- value = get_data(data, offset);
- value = static_cast(value & 0xFFFF) << 16 | (value & 0xFFFF0000) >> 16;
- value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
+ if (size >= 4) {
+ value = get_data(data, offset);
+ value = static_cast(value & 0xFFFF) << 16 | (value & 0xFFFF0000) >> 16;
+ value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
+ } else {
+ error = true;
+ }
break;
case SensorValueType::S_WORD:
- value = mask_and_shift_by_rightbit(get_data(data, offset),
- bitmask); // default is 0xFFFF ;
+ if (size >= 2) {
+ value = mask_and_shift_by_rightbit(get_data(data, offset),
+ bitmask); // default is 0xFFFF ;
+ } else {
+ error = true;
+ }
break;
case SensorValueType::S_DWORD:
- value = mask_and_shift_by_rightbit(get_data(data, offset), bitmask);
+ if (size >= 4) {
+ value = mask_and_shift_by_rightbit(get_data(data, offset), bitmask);
+ } else {
+ error = true;
+ }
break;
case SensorValueType::S_DWORD_R: {
- value = get_data(data, offset);
- // Currently the high word is at the low position
- // the sign bit is therefore at low before the switch
- uint32_t sign_bit = (value & 0x8000) << 16;
- value = mask_and_shift_by_rightbit(
- static_cast(((value & 0x7FFF) << 16 | (value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
+ if (size >= 4) {
+ value = get_data(data, offset);
+ // Currently the high word is at the low position
+ // the sign bit is therefore at low before the switch
+ uint32_t sign_bit = (value & 0x8000) << 16;
+ value = mask_and_shift_by_rightbit(
+ static_cast(((value & 0x7FFF) << 16 | (value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
+ } else {
+ error = true;
+ }
} break;
case SensorValueType::U_QWORD:
case SensorValueType::S_QWORD:
// Ignore bitmask for QWORD
- value = get_data(data, offset);
+ if (size >= 8) {
+ value = get_data(data, offset);
+ } else {
+ error = true;
+ }
break;
case SensorValueType::U_QWORD_R:
case SensorValueType::S_QWORD_R: {
// Ignore bitmask for QWORD
- uint64_t tmp = get_data(data, offset);
- value = (tmp << 48) | (tmp >> 48) | ((tmp & 0xFFFF0000) << 16) | ((tmp >> 16) & 0xFFFF0000);
+ if (size >= 8) {
+ uint64_t tmp = get_data(data, offset);
+ value = (tmp << 48) | (tmp >> 48) | ((tmp & 0xFFFF0000) << 16) | ((tmp >> 16) & 0xFFFF0000);
+ } else {
+ error = true;
+ }
} break;
case SensorValueType::RAW:
default:
break;
}
+ if (error)
+ ESP_LOGE(TAG, "not enough data for value");
return value;
}
@@ -670,5 +710,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..dfd52e44bc 100644
--- a/esphome/components/modbus_controller/modbus_controller.h
+++ b/esphome/components/modbus_controller/modbus_controller.h
@@ -240,14 +240,14 @@ class SensorItem {
}
// Override register size for modbus devices not using 1 register for one dword
void set_register_size(uint8_t register_size) { response_bytes = register_size; }
- ModbusRegisterType register_type;
- SensorValueType sensor_value_type;
- uint16_t start_address;
- uint32_t bitmask;
- uint8_t offset;
- uint8_t register_count;
+ ModbusRegisterType register_type{ModbusRegisterType::CUSTOM};
+ SensorValueType sensor_value_type{SensorValueType::RAW};
+ uint16_t start_address{0};
+ uint32_t bitmask{0};
+ uint8_t offset{0};
+ uint8_t register_count{0};
uint8_t response_bytes{0};
- uint16_t skip_updates;
+ uint16_t skip_updates{0};
std::vector custom_data{};
bool force_new_range{false};
};
@@ -261,9 +261,9 @@ class ServerRegister {
this->register_count = register_count;
this->read_lambda = std::move(read_lambda);
}
- uint16_t address;
- SensorValueType value_type;
- uint8_t register_count;
+ uint16_t address{0};
+ SensorValueType value_type{SensorValueType::RAW};
+ uint8_t register_count{0};
std::function read_lambda;
};
@@ -312,11 +312,11 @@ struct RegisterRange {
class ModbusCommandItem {
public:
static const size_t MAX_PAYLOAD_BYTES = 240;
- ModbusController *modbusdevice;
- uint16_t register_address;
- uint16_t register_count;
- ModbusFunctionCode function_code;
- ModbusRegisterType register_type;
+ ModbusController *modbusdevice{nullptr};
+ uint16_t register_address{0};
+ uint16_t register_count{0};
+ ModbusFunctionCode function_code{ModbusFunctionCode::CUSTOM};
+ ModbusRegisterType register_type{ModbusRegisterType::CUSTOM};
std::function &data)>
on_data_func;
std::vector payload = {};
@@ -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
@@ -489,26 +493,31 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
/// Collection of all sensors for this component
SensorSet sensorset_;
/// Collection of all server registers for this component
- std::vector server_registers_;
+ std::vector server_registers_{};
/// Continuous range of modbus registers
- std::vector register_ranges_;
+ std::vector register_ranges_{};
/// Hold the pending requests to be sent
std::list> command_queue_;
/// modbus response data waiting to get processed
std::queue> incoming_queue_;
/// if duplicate commands can be sent
- bool allow_duplicate_commands_;
+ bool allow_duplicate_commands_{false};
/// when was the last send operation
- uint32_t last_command_timestamp_;
+ uint32_t last_command_timestamp_{0};
/// min time in ms between sending modbus commands
- uint16_t command_throttle_;
+ uint16_t command_throttle_{0};
/// if module didn't respond the last command
- bool module_offline_;
+ bool module_offline_{false};
/// how many updates to skip if module is offline
- uint16_t offline_skip_updates_;
+ uint16_t offline_skip_updates_{0};
/// 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/modbus_controller/number/modbus_number.cpp b/esphome/components/modbus_controller/number/modbus_number.cpp
index 001cfb5787..ea8467d5a3 100644
--- a/esphome/components/modbus_controller/number/modbus_number.cpp
+++ b/esphome/components/modbus_controller/number/modbus_number.cpp
@@ -8,7 +8,7 @@ namespace modbus_controller {
static const char *const TAG = "modbus.number";
void ModbusNumber::parse_and_publish(const std::vector &data) {
- float result = payload_to_float(data, *this) / multiply_by_;
+ float result = payload_to_float(data, *this) / this->multiply_by_;
// Is there a lambda registered
// call it with the pre converted value and the raw data array
@@ -43,7 +43,7 @@ void ModbusNumber::control(float value) {
return;
}
} else {
- write_value = multiply_by_ * write_value;
+ write_value = this->multiply_by_ * write_value;
}
if (!data.empty()) {
@@ -63,21 +63,21 @@ void ModbusNumber::control(float value) {
// Create and send the write command
if (this->register_count == 1 && !this->use_write_multiple_) {
// since offset is in bytes and a register is 16 bits we get the start by adding offset/2
- write_cmd =
- ModbusCommandItem::create_write_single_command(parent_, this->start_address + this->offset / 2, data[0]);
+ write_cmd = ModbusCommandItem::create_write_single_command(this->parent_, this->start_address + this->offset / 2,
+ data[0]);
} else {
- write_cmd = ModbusCommandItem::create_write_multiple_command(parent_, this->start_address + this->offset / 2,
- this->register_count, data);
+ write_cmd = ModbusCommandItem::create_write_multiple_command(
+ this->parent_, this->start_address + this->offset / 2, this->register_count, data);
}
// publish new value
write_cmd.on_data_func = [this, write_cmd, value](ModbusRegisterType register_type, uint16_t start_address,
const std::vector &data) {
// gets called when the write command is ack'd from the device
- parent_->on_write_register_response(write_cmd.register_type, start_address, data);
+ this->parent_->on_write_register_response(write_cmd.register_type, start_address, data);
this->publish_state(value);
};
}
- parent_->queue_command(write_cmd);
+ this->parent_->queue_command(write_cmd);
this->publish_state(value);
}
void ModbusNumber::dump_config() { LOG_NUMBER(TAG, "Modbus Number", this); }
diff --git a/esphome/components/modbus_controller/number/modbus_number.h b/esphome/components/modbus_controller/number/modbus_number.h
index 544d161cbc..8f77b2e014 100644
--- a/esphome/components/modbus_controller/number/modbus_number.h
+++ b/esphome/components/modbus_controller/number/modbus_number.h
@@ -29,7 +29,7 @@ class ModbusNumber : public number::Number, public Component, public SensorItem
void parse_and_publish(const std::vector &data) override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
void set_parent(ModbusController *parent) { this->parent_ = parent; }
- void set_write_multiply(float factor) { multiply_by_ = factor; }
+ void set_write_multiply(float factor) { this->multiply_by_ = factor; }
using transform_func_t = std::function(ModbusNumber *, float, const std::vector &)>;
using write_transform_func_t = std::function(ModbusNumber *, float, std::vector &)>;
@@ -39,9 +39,9 @@ class ModbusNumber : public number::Number, public Component, public SensorItem
protected:
void control(float value) override;
- optional