mirror of
https://github.com/esphome/esphome.git
synced 2025-11-03 16:41:50 +00:00
Compare commits
1 Commits
2025.6.2
...
jesserockz
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11cc5aef62 |
@@ -1,4 +1,2 @@
|
|||||||
[run]
|
[run]
|
||||||
omit =
|
omit = esphome/components/*
|
||||||
esphome/components/*
|
|
||||||
tests/integration/*
|
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
ARG BUILD_BASE_VERSION=2025.04.0
|
|
||||||
|
|
||||||
|
|
||||||
FROM ghcr.io/esphome/docker-base:debian-${BUILD_BASE_VERSION} AS base
|
|
||||||
|
|
||||||
RUN git config --system --add safe.directory "*"
|
|
||||||
|
|
||||||
RUN apt update \
|
|
||||||
&& apt install -y \
|
|
||||||
protobuf-compiler
|
|
||||||
|
|
||||||
RUN pip install uv
|
|
||||||
|
|
||||||
RUN useradd esphome -m
|
|
||||||
|
|
||||||
USER esphome
|
|
||||||
ENV VIRTUAL_ENV=/home/esphome/.local/esphome-venv
|
|
||||||
RUN uv venv $VIRTUAL_ENV
|
|
||||||
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
|
|
||||||
# Override this set to true in the docker-base image
|
|
||||||
ENV UV_SYSTEM_PYTHON=false
|
|
||||||
|
|
||||||
WORKDIR /tmp
|
|
||||||
|
|
||||||
COPY requirements.txt ./
|
|
||||||
RUN uv pip install -r requirements.txt
|
|
||||||
COPY requirements_dev.txt requirements_test.txt ./
|
|
||||||
RUN uv pip install -r requirements_dev.txt -r requirements_test.txt
|
|
||||||
|
|
||||||
RUN \
|
|
||||||
platformio settings set enable_telemetry No \
|
|
||||||
&& platformio settings set check_platformio_interval 1000000
|
|
||||||
|
|
||||||
COPY script/platformio_install_deps.py platformio.ini ./
|
|
||||||
RUN ./platformio_install_deps.py platformio.ini --libraries --platforms --tools
|
|
||||||
|
|
||||||
WORKDIR /workspaces
|
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "ESPHome Dev",
|
"name": "ESPHome Dev",
|
||||||
"context": "..",
|
"image": "ghcr.io/esphome/esphome-lint:dev",
|
||||||
"dockerFile": "Dockerfile",
|
|
||||||
"postCreateCommand": [
|
"postCreateCommand": [
|
||||||
"script/devcontainer-post-create"
|
"script/devcontainer-post-create"
|
||||||
],
|
],
|
||||||
"features": {
|
"containerEnv": {
|
||||||
"ghcr.io/devcontainers/features/github-cli:1": {}
|
"DEVCONTAINER": "1",
|
||||||
|
"PIP_BREAK_SYSTEM_PACKAGES": "1",
|
||||||
|
"PIP_ROOT_USER_ACTION": "ignore"
|
||||||
},
|
},
|
||||||
"runArgs": [
|
"runArgs": [
|
||||||
"--privileged",
|
"--privileged",
|
||||||
"-e",
|
"-e",
|
||||||
"GIT_EDITOR=code --wait"
|
"ESPHOME_DASHBOARD_USE_PING=1"
|
||||||
// uncomment and edit the path in order to pass though local USB serial to the conatiner
|
// uncomment and edit the path in order to pass though local USB serial to the conatiner
|
||||||
// , "--device=/dev/ttyACM0"
|
// , "--device=/dev/ttyACM0"
|
||||||
],
|
],
|
||||||
|
|||||||
4
.github/actions/build-image/action.yaml
vendored
4
.github/actions/build-image/action.yaml
vendored
@@ -47,7 +47,7 @@ runs:
|
|||||||
|
|
||||||
- name: Build and push to ghcr by digest
|
- name: Build and push to ghcr by digest
|
||||||
id: build-ghcr
|
id: build-ghcr
|
||||||
uses: docker/build-push-action@v6.18.0
|
uses: docker/build-push-action@v6.16.0
|
||||||
env:
|
env:
|
||||||
DOCKER_BUILD_SUMMARY: false
|
DOCKER_BUILD_SUMMARY: false
|
||||||
DOCKER_BUILD_RECORD_UPLOAD: false
|
DOCKER_BUILD_RECORD_UPLOAD: false
|
||||||
@@ -73,7 +73,7 @@ runs:
|
|||||||
|
|
||||||
- name: Build and push to dockerhub by digest
|
- name: Build and push to dockerhub by digest
|
||||||
id: build-dockerhub
|
id: build-dockerhub
|
||||||
uses: docker/build-push-action@v6.18.0
|
uses: docker/build-push-action@v6.16.0
|
||||||
env:
|
env:
|
||||||
DOCKER_BUILD_SUMMARY: false
|
DOCKER_BUILD_SUMMARY: false
|
||||||
DOCKER_BUILD_RECORD_UPLOAD: false
|
DOCKER_BUILD_RECORD_UPLOAD: false
|
||||||
|
|||||||
13
.github/workflows/ci-api-proto.yml
vendored
13
.github/workflows/ci-api-proto.yml
vendored
@@ -21,7 +21,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5.6.0
|
uses: actions/setup-python@v5.6.0
|
||||||
with:
|
with:
|
||||||
@@ -57,17 +57,6 @@ jobs:
|
|||||||
event: 'REQUEST_CHANGES',
|
event: 'REQUEST_CHANGES',
|
||||||
body: 'You have altered the generated proto files but they do not match what is expected.\nPlease run "script/api_protobuf/api_protobuf.py" and commit the changes.'
|
body: 'You have altered the generated proto files but they do not match what is expected.\nPlease run "script/api_protobuf/api_protobuf.py" and commit the changes.'
|
||||||
})
|
})
|
||||||
- if: failure()
|
|
||||||
name: Show changes
|
|
||||||
run: git diff
|
|
||||||
- if: failure()
|
|
||||||
name: Archive artifacts
|
|
||||||
uses: actions/upload-artifact@v4.6.2
|
|
||||||
with:
|
|
||||||
name: generated-proto-files
|
|
||||||
path: |
|
|
||||||
esphome/components/api/api_pb2.*
|
|
||||||
esphome/components/api/api_pb2_service.*
|
|
||||||
- if: success()
|
- if: success()
|
||||||
name: Dismiss review
|
name: Dismiss review
|
||||||
uses: actions/github-script@v7.0.1
|
uses: actions/github-script@v7.0.1
|
||||||
|
|||||||
4
.github/workflows/ci-docker.yml
vendored
4
.github/workflows/ci-docker.yml
vendored
@@ -43,11 +43,11 @@ jobs:
|
|||||||
- "docker"
|
- "docker"
|
||||||
# - "lint"
|
# - "lint"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4.2.2
|
- uses: actions/checkout@v4.1.7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5.6.0
|
uses: actions/setup-python@v5.6.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.10"
|
python-version: "3.9"
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3.10.0
|
uses: docker/setup-buildx-action@v3.10.0
|
||||||
|
|
||||||
|
|||||||
55
.github/workflows/ci.yml
vendored
55
.github/workflows/ci.yml
vendored
@@ -20,8 +20,8 @@ permissions:
|
|||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
env:
|
env:
|
||||||
DEFAULT_PYTHON: "3.10"
|
DEFAULT_PYTHON: "3.9"
|
||||||
PYUPGRADE_TARGET: "--py310-plus"
|
PYUPGRADE_TARGET: "--py39-plus"
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
# yamllint disable-line rule:line-length
|
# yamllint disable-line rule:line-length
|
||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
cache-key: ${{ steps.cache-key.outputs.key }}
|
cache-key: ${{ steps.cache-key.outputs.key }}
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Generate cache-key
|
- name: Generate cache-key
|
||||||
id: cache-key
|
id: cache-key
|
||||||
run: echo key="${{ hashFiles('requirements.txt', 'requirements_test.txt') }}" >> $GITHUB_OUTPUT
|
run: echo key="${{ hashFiles('requirements.txt', 'requirements_test.txt') }}" >> $GITHUB_OUTPUT
|
||||||
@@ -68,7 +68,7 @@ jobs:
|
|||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -89,7 +89,7 @@ jobs:
|
|||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -110,7 +110,7 @@ jobs:
|
|||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -131,7 +131,7 @@ jobs:
|
|||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -152,7 +152,7 @@ jobs:
|
|||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -173,10 +173,10 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
python-version:
|
python-version:
|
||||||
|
- "3.9"
|
||||||
- "3.10"
|
- "3.10"
|
||||||
- "3.11"
|
- "3.11"
|
||||||
- "3.12"
|
- "3.12"
|
||||||
- "3.13"
|
|
||||||
os:
|
os:
|
||||||
- ubuntu-latest
|
- ubuntu-latest
|
||||||
- macOS-latest
|
- macOS-latest
|
||||||
@@ -185,24 +185,24 @@ jobs:
|
|||||||
# Minimize CI resource usage
|
# Minimize CI resource usage
|
||||||
# by only running the Python version
|
# by only running the Python version
|
||||||
# version used for docker images on Windows and macOS
|
# version used for docker images on Windows and macOS
|
||||||
- python-version: "3.13"
|
|
||||||
os: windows-latest
|
|
||||||
- python-version: "3.12"
|
- python-version: "3.12"
|
||||||
os: windows-latest
|
os: windows-latest
|
||||||
- python-version: "3.10"
|
- python-version: "3.10"
|
||||||
os: windows-latest
|
os: windows-latest
|
||||||
- python-version: "3.13"
|
- python-version: "3.9"
|
||||||
os: macOS-latest
|
os: windows-latest
|
||||||
- python-version: "3.12"
|
- python-version: "3.12"
|
||||||
os: macOS-latest
|
os: macOS-latest
|
||||||
- python-version: "3.10"
|
- python-version: "3.10"
|
||||||
os: macOS-latest
|
os: macOS-latest
|
||||||
|
- python-version: "3.9"
|
||||||
|
os: macOS-latest
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
needs:
|
needs:
|
||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -214,14 +214,14 @@ jobs:
|
|||||||
if: matrix.os == 'windows-latest'
|
if: matrix.os == 'windows-latest'
|
||||||
run: |
|
run: |
|
||||||
./venv/Scripts/activate
|
./venv/Scripts/activate
|
||||||
pytest -vv --cov-report=xml --tb=native -n auto tests
|
pytest -vv --cov-report=xml --tb=native tests
|
||||||
- name: Run pytest
|
- name: Run pytest
|
||||||
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
|
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
|
||||||
run: |
|
run: |
|
||||||
. venv/bin/activate
|
. venv/bin/activate
|
||||||
pytest -vv --cov-report=xml --tb=native -n auto tests
|
pytest -vv --cov-report=xml --tb=native tests
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
uses: codecov/codecov-action@v5.4.3
|
uses: codecov/codecov-action@v5.4.2
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
|
||||||
@@ -232,7 +232,7 @@ jobs:
|
|||||||
- common
|
- common
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -292,15 +292,10 @@ jobs:
|
|||||||
name: Run script/clang-tidy for ESP32 IDF
|
name: Run script/clang-tidy for ESP32 IDF
|
||||||
options: --environment esp32-idf-tidy --grep USE_ESP_IDF
|
options: --environment esp32-idf-tidy --grep USE_ESP_IDF
|
||||||
pio_cache_key: tidyesp32-idf
|
pio_cache_key: tidyesp32-idf
|
||||||
- id: clang-tidy
|
|
||||||
name: Run script/clang-tidy for ZEPHYR
|
|
||||||
options: --environment nrf52-tidy --grep USE_ZEPHYR
|
|
||||||
pio_cache_key: tidy-zephyr
|
|
||||||
ignore_errors: false
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -336,13 +331,13 @@ jobs:
|
|||||||
- name: Run clang-tidy
|
- name: Run clang-tidy
|
||||||
run: |
|
run: |
|
||||||
. venv/bin/activate
|
. venv/bin/activate
|
||||||
script/clang-tidy --all-headers --fix ${{ matrix.options }} ${{ matrix.ignore_errors && '|| true' || '' }}
|
script/clang-tidy --all-headers --fix ${{ matrix.options }}
|
||||||
env:
|
env:
|
||||||
# Also cache libdeps, store them in a ~/.platformio subfolder
|
# Also cache libdeps, store them in a ~/.platformio subfolder
|
||||||
PLATFORMIO_LIBDEPS_DIR: ~/.platformio/libdeps
|
PLATFORMIO_LIBDEPS_DIR: ~/.platformio/libdeps
|
||||||
|
|
||||||
- name: Suggested changes
|
- name: Suggested changes
|
||||||
run: script/ci-suggest-changes ${{ matrix.ignore_errors && '|| true' || '' }}
|
run: script/ci-suggest-changes
|
||||||
# yamllint disable-line rule:line-length
|
# yamllint disable-line rule:line-length
|
||||||
if: always()
|
if: always()
|
||||||
|
|
||||||
@@ -356,7 +351,7 @@ jobs:
|
|||||||
count: ${{ steps.list-components.outputs.count }}
|
count: ${{ steps.list-components.outputs.count }}
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
with:
|
with:
|
||||||
# Fetch enough history so `git merge-base refs/remotes/origin/dev HEAD` works.
|
# Fetch enough history so `git merge-base refs/remotes/origin/dev HEAD` works.
|
||||||
fetch-depth: 500
|
fetch-depth: 500
|
||||||
@@ -406,7 +401,7 @@ jobs:
|
|||||||
sudo apt-get install libsdl2-dev
|
sudo apt-get install libsdl2-dev
|
||||||
|
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
@@ -432,7 +427,7 @@ jobs:
|
|||||||
matrix: ${{ steps.split.outputs.components }}
|
matrix: ${{ steps.split.outputs.components }}
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Split components into 20 groups
|
- name: Split components into 20 groups
|
||||||
id: split
|
id: split
|
||||||
run: |
|
run: |
|
||||||
@@ -462,7 +457,7 @@ jobs:
|
|||||||
sudo apt-get install libsdl2-dev
|
sudo apt-get install libsdl2-dev
|
||||||
|
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Restore Python
|
- name: Restore Python
|
||||||
uses: ./.github/actions/restore-python
|
uses: ./.github/actions/restore-python
|
||||||
with:
|
with:
|
||||||
|
|||||||
46
.github/workflows/release.yml
vendored
46
.github/workflows/release.yml
vendored
@@ -18,9 +18,8 @@ jobs:
|
|||||||
outputs:
|
outputs:
|
||||||
tag: ${{ steps.tag.outputs.tag }}
|
tag: ${{ steps.tag.outputs.tag }}
|
||||||
branch_build: ${{ steps.tag.outputs.branch_build }}
|
branch_build: ${{ steps.tag.outputs.branch_build }}
|
||||||
deploy_env: ${{ steps.tag.outputs.deploy_env }}
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4.2.2
|
- uses: actions/checkout@v4.1.7
|
||||||
- name: Get tag
|
- name: Get tag
|
||||||
id: tag
|
id: tag
|
||||||
# yamllint disable rule:line-length
|
# yamllint disable rule:line-length
|
||||||
@@ -28,11 +27,6 @@ jobs:
|
|||||||
if [[ "${{ github.event_name }}" = "release" ]]; then
|
if [[ "${{ github.event_name }}" = "release" ]]; then
|
||||||
TAG="${{ github.event.release.tag_name}}"
|
TAG="${{ github.event.release.tag_name}}"
|
||||||
BRANCH_BUILD="false"
|
BRANCH_BUILD="false"
|
||||||
if [[ "${{ github.event.release.prerelease }}" = "true" ]]; then
|
|
||||||
ENVIRONMENT="beta"
|
|
||||||
else
|
|
||||||
ENVIRONMENT="production"
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
TAG=$(cat esphome/const.py | sed -n -E "s/^__version__\s+=\s+\"(.+)\"$/\1/p")
|
TAG=$(cat esphome/const.py | sed -n -E "s/^__version__\s+=\s+\"(.+)\"$/\1/p")
|
||||||
today="$(date --utc '+%Y%m%d')"
|
today="$(date --utc '+%Y%m%d')"
|
||||||
@@ -41,15 +35,12 @@ jobs:
|
|||||||
if [[ "$BRANCH" != "dev" ]]; then
|
if [[ "$BRANCH" != "dev" ]]; then
|
||||||
TAG="${TAG}-${BRANCH}"
|
TAG="${TAG}-${BRANCH}"
|
||||||
BRANCH_BUILD="true"
|
BRANCH_BUILD="true"
|
||||||
ENVIRONMENT=""
|
|
||||||
else
|
else
|
||||||
BRANCH_BUILD="false"
|
BRANCH_BUILD="false"
|
||||||
ENVIRONMENT="dev"
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
echo "tag=${TAG}" >> $GITHUB_OUTPUT
|
echo "tag=${TAG}" >> $GITHUB_OUTPUT
|
||||||
echo "branch_build=${BRANCH_BUILD}" >> $GITHUB_OUTPUT
|
echo "branch_build=${BRANCH_BUILD}" >> $GITHUB_OUTPUT
|
||||||
echo "deploy_env=${ENVIRONMENT}" >> $GITHUB_OUTPUT
|
|
||||||
# yamllint enable rule:line-length
|
# yamllint enable rule:line-length
|
||||||
|
|
||||||
deploy-pypi:
|
deploy-pypi:
|
||||||
@@ -60,19 +51,21 @@ jobs:
|
|||||||
contents: read
|
contents: read
|
||||||
id-token: write
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4.2.2
|
- uses: actions/checkout@v4.1.7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5.6.0
|
uses: actions/setup-python@v5.6.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.x"
|
python-version: "3.x"
|
||||||
|
- name: Set up python environment
|
||||||
|
env:
|
||||||
|
ESPHOME_NO_VENV: 1
|
||||||
|
run: script/setup
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |-
|
run: |-
|
||||||
pip3 install build
|
pip3 install build
|
||||||
python3 -m build
|
python3 -m build
|
||||||
- name: Publish
|
- name: Publish
|
||||||
uses: pypa/gh-action-pypi-publish@v1.12.4
|
uses: pypa/gh-action-pypi-publish@v1.12.4
|
||||||
with:
|
|
||||||
skip-existing: true
|
|
||||||
|
|
||||||
deploy-docker:
|
deploy-docker:
|
||||||
name: Build ESPHome ${{ matrix.platform.arch }}
|
name: Build ESPHome ${{ matrix.platform.arch }}
|
||||||
@@ -92,11 +85,11 @@ jobs:
|
|||||||
os: "ubuntu-24.04-arm"
|
os: "ubuntu-24.04-arm"
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4.2.2
|
- uses: actions/checkout@v4.1.7
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5.6.0
|
uses: actions/setup-python@v5.6.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.10"
|
python-version: "3.9"
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3.10.0
|
uses: docker/setup-buildx-action@v3.10.0
|
||||||
@@ -168,7 +161,7 @@ jobs:
|
|||||||
- ghcr
|
- ghcr
|
||||||
- dockerhub
|
- dockerhub
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4.2.2
|
- uses: actions/checkout@v4.1.7
|
||||||
|
|
||||||
- name: Download digests
|
- name: Download digests
|
||||||
uses: actions/download-artifact@v4.3.0
|
uses: actions/download-artifact@v4.3.0
|
||||||
@@ -238,24 +231,3 @@ jobs:
|
|||||||
content: description
|
content: description
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
deploy-esphome-schema:
|
|
||||||
if: github.repository == 'esphome/esphome' && needs.init.outputs.branch_build == 'false'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: [init]
|
|
||||||
environment: ${{ needs.init.outputs.deploy_env }}
|
|
||||||
steps:
|
|
||||||
- name: Trigger Workflow
|
|
||||||
uses: actions/github-script@v7.0.1
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.DEPLOY_ESPHOME_SCHEMA_REPO_TOKEN }}
|
|
||||||
script: |
|
|
||||||
github.rest.actions.createWorkflowDispatch({
|
|
||||||
owner: "esphome",
|
|
||||||
repo: "esphome-schema",
|
|
||||||
workflow_id: "generate-schemas.yml",
|
|
||||||
ref: "main",
|
|
||||||
inputs: {
|
|
||||||
version: "${{ needs.init.outputs.tag }}",
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|||||||
6
.github/workflows/sync-device-classes.yml
vendored
6
.github/workflows/sync-device-classes.yml
vendored
@@ -13,10 +13,10 @@ jobs:
|
|||||||
if: github.repository == 'esphome/esphome'
|
if: github.repository == 'esphome/esphome'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
|
|
||||||
- name: Checkout Home Assistant
|
- name: Checkout Home Assistant
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
with:
|
with:
|
||||||
repository: home-assistant/core
|
repository: home-assistant/core
|
||||||
path: lib/home-assistant
|
path: lib/home-assistant
|
||||||
@@ -24,7 +24,7 @@ jobs:
|
|||||||
- name: Setup Python
|
- name: Setup Python
|
||||||
uses: actions/setup-python@v5.6.0
|
uses: actions/setup-python@v5.6.0
|
||||||
with:
|
with:
|
||||||
python-version: 3.13
|
python-version: 3.12
|
||||||
|
|
||||||
- name: Install Home Assistant
|
- name: Install Home Assistant
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
2
.github/workflows/yaml-lint.yml
vendored
2
.github/workflows/yaml-lint.yml
vendored
@@ -18,7 +18,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@v4.2.2
|
uses: actions/checkout@v4.1.7
|
||||||
- name: Run yamllint
|
- name: Run yamllint
|
||||||
uses: frenck/action-yamllint@v1.5.0
|
uses: frenck/action-yamllint@v1.5.0
|
||||||
with:
|
with:
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -143,4 +143,3 @@ sdkconfig.*
|
|||||||
/components
|
/components
|
||||||
/managed_components
|
/managed_components
|
||||||
|
|
||||||
api-docs/
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
# Ruff version.
|
# Ruff version.
|
||||||
rev: v0.11.10
|
rev: v0.11.0
|
||||||
hooks:
|
hooks:
|
||||||
# Run the linter.
|
# Run the linter.
|
||||||
- id: ruff
|
- id: ruff
|
||||||
@@ -28,12 +28,12 @@ repos:
|
|||||||
- --branch=release
|
- --branch=release
|
||||||
- --branch=beta
|
- --branch=beta
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
rev: v3.20.0
|
rev: v3.15.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyupgrade
|
- id: pyupgrade
|
||||||
args: [--py310-plus]
|
args: [--py39-plus]
|
||||||
- repo: https://github.com/adrienverge/yamllint.git
|
- repo: https://github.com/adrienverge/yamllint.git
|
||||||
rev: v1.37.1
|
rev: v1.35.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: yamllint
|
- id: yamllint
|
||||||
- repo: https://github.com/pre-commit/mirrors-clang-format
|
- repo: https://github.com/pre-commit/mirrors-clang-format
|
||||||
|
|||||||
13
CODEOWNERS
13
CODEOWNERS
@@ -96,7 +96,6 @@ esphome/components/ch422g/* @clydebarrow @jesterret
|
|||||||
esphome/components/chsc6x/* @kkosik20
|
esphome/components/chsc6x/* @kkosik20
|
||||||
esphome/components/climate/* @esphome/core
|
esphome/components/climate/* @esphome/core
|
||||||
esphome/components/climate_ir/* @glmnet
|
esphome/components/climate_ir/* @glmnet
|
||||||
esphome/components/cm1106/* @andrewjswan
|
|
||||||
esphome/components/color_temperature/* @jesserockz
|
esphome/components/color_temperature/* @jesserockz
|
||||||
esphome/components/combination/* @Cat-Ion @kahrendt
|
esphome/components/combination/* @Cat-Ion @kahrendt
|
||||||
esphome/components/const/* @esphome/core
|
esphome/components/const/* @esphome/core
|
||||||
@@ -139,7 +138,6 @@ esphome/components/es7210/* @kahrendt
|
|||||||
esphome/components/es7243e/* @kbx81
|
esphome/components/es7243e/* @kbx81
|
||||||
esphome/components/es8156/* @kbx81
|
esphome/components/es8156/* @kbx81
|
||||||
esphome/components/es8311/* @kahrendt @kroimon
|
esphome/components/es8311/* @kahrendt @kroimon
|
||||||
esphome/components/es8388/* @P4uLT
|
|
||||||
esphome/components/esp32/* @esphome/core
|
esphome/components/esp32/* @esphome/core
|
||||||
esphome/components/esp32_ble/* @Rapsssito @jesserockz
|
esphome/components/esp32_ble/* @Rapsssito @jesserockz
|
||||||
esphome/components/esp32_ble_client/* @jesserockz
|
esphome/components/esp32_ble_client/* @jesserockz
|
||||||
@@ -150,7 +148,6 @@ esphome/components/esp32_improv/* @jesserockz
|
|||||||
esphome/components/esp32_rmt/* @jesserockz
|
esphome/components/esp32_rmt/* @jesserockz
|
||||||
esphome/components/esp32_rmt_led_strip/* @jesserockz
|
esphome/components/esp32_rmt_led_strip/* @jesserockz
|
||||||
esphome/components/esp8266/* @esphome/core
|
esphome/components/esp8266/* @esphome/core
|
||||||
esphome/components/esp_ldo/* @clydebarrow
|
|
||||||
esphome/components/ethernet_info/* @gtjadsonsantos
|
esphome/components/ethernet_info/* @gtjadsonsantos
|
||||||
esphome/components/event/* @nohat
|
esphome/components/event/* @nohat
|
||||||
esphome/components/event_emitter/* @Rapsssito
|
esphome/components/event_emitter/* @Rapsssito
|
||||||
@@ -172,7 +169,7 @@ esphome/components/gp2y1010au0f/* @zry98
|
|||||||
esphome/components/gp8403/* @jesserockz
|
esphome/components/gp8403/* @jesserockz
|
||||||
esphome/components/gpio/* @esphome/core
|
esphome/components/gpio/* @esphome/core
|
||||||
esphome/components/gpio/one_wire/* @ssieb
|
esphome/components/gpio/one_wire/* @ssieb
|
||||||
esphome/components/gps/* @coogle @ximex
|
esphome/components/gps/* @coogle
|
||||||
esphome/components/graph/* @synco
|
esphome/components/graph/* @synco
|
||||||
esphome/components/graphical_display_menu/* @MrMDavidson
|
esphome/components/graphical_display_menu/* @MrMDavidson
|
||||||
esphome/components/gree/* @orestismers
|
esphome/components/gree/* @orestismers
|
||||||
@@ -236,7 +233,6 @@ esphome/components/kamstrup_kmp/* @cfeenstra1024
|
|||||||
esphome/components/key_collector/* @ssieb
|
esphome/components/key_collector/* @ssieb
|
||||||
esphome/components/key_provider/* @ssieb
|
esphome/components/key_provider/* @ssieb
|
||||||
esphome/components/kuntze/* @ssieb
|
esphome/components/kuntze/* @ssieb
|
||||||
esphome/components/lc709203f/* @ilikecake
|
|
||||||
esphome/components/lcd_menu/* @numo68
|
esphome/components/lcd_menu/* @numo68
|
||||||
esphome/components/ld2410/* @regevbr @sebcaps
|
esphome/components/ld2410/* @regevbr @sebcaps
|
||||||
esphome/components/ld2420/* @descipher
|
esphome/components/ld2420/* @descipher
|
||||||
@@ -286,7 +282,6 @@ esphome/components/microphone/* @jesserockz @kahrendt
|
|||||||
esphome/components/mics_4514/* @jesserockz
|
esphome/components/mics_4514/* @jesserockz
|
||||||
esphome/components/midea/* @dudanov
|
esphome/components/midea/* @dudanov
|
||||||
esphome/components/midea_ir/* @dudanov
|
esphome/components/midea_ir/* @dudanov
|
||||||
esphome/components/mipi_spi/* @clydebarrow
|
|
||||||
esphome/components/mitsubishi/* @RubyBailey
|
esphome/components/mitsubishi/* @RubyBailey
|
||||||
esphome/components/mixer/speaker/* @kahrendt
|
esphome/components/mixer/speaker/* @kahrendt
|
||||||
esphome/components/mlx90393/* @functionpointer
|
esphome/components/mlx90393/* @functionpointer
|
||||||
@@ -322,7 +317,6 @@ esphome/components/number/* @esphome/core
|
|||||||
esphome/components/one_wire/* @ssieb
|
esphome/components/one_wire/* @ssieb
|
||||||
esphome/components/online_image/* @clydebarrow @guillempages
|
esphome/components/online_image/* @clydebarrow @guillempages
|
||||||
esphome/components/opentherm/* @olegtarasov
|
esphome/components/opentherm/* @olegtarasov
|
||||||
esphome/components/openthread/* @mrene
|
|
||||||
esphome/components/ota/* @esphome/core
|
esphome/components/ota/* @esphome/core
|
||||||
esphome/components/output/* @esphome/core
|
esphome/components/output/* @esphome/core
|
||||||
esphome/components/packet_transport/* @clydebarrow
|
esphome/components/packet_transport/* @clydebarrow
|
||||||
@@ -404,7 +398,6 @@ esphome/components/smt100/* @piechade
|
|||||||
esphome/components/sn74hc165/* @jesserockz
|
esphome/components/sn74hc165/* @jesserockz
|
||||||
esphome/components/socket/* @esphome/core
|
esphome/components/socket/* @esphome/core
|
||||||
esphome/components/sonoff_d1/* @anatoly-savchenkov
|
esphome/components/sonoff_d1/* @anatoly-savchenkov
|
||||||
esphome/components/sound_level/* @kahrendt
|
|
||||||
esphome/components/speaker/* @jesserockz @kahrendt
|
esphome/components/speaker/* @jesserockz @kahrendt
|
||||||
esphome/components/speaker/media_player/* @kahrendt @synesthesiam
|
esphome/components/speaker/media_player/* @kahrendt @synesthesiam
|
||||||
esphome/components/spi/* @clydebarrow @esphome/core
|
esphome/components/spi/* @clydebarrow @esphome/core
|
||||||
@@ -483,14 +476,12 @@ esphome/components/ufire_ise/* @pvizeli
|
|||||||
esphome/components/ultrasonic/* @OttoWinter
|
esphome/components/ultrasonic/* @OttoWinter
|
||||||
esphome/components/update/* @jesserockz
|
esphome/components/update/* @jesserockz
|
||||||
esphome/components/uponor_smatrix/* @kroimon
|
esphome/components/uponor_smatrix/* @kroimon
|
||||||
esphome/components/usb_host/* @clydebarrow
|
|
||||||
esphome/components/usb_uart/* @clydebarrow
|
|
||||||
esphome/components/valve/* @esphome/core
|
esphome/components/valve/* @esphome/core
|
||||||
esphome/components/vbus/* @ssieb
|
esphome/components/vbus/* @ssieb
|
||||||
esphome/components/veml3235/* @kbx81
|
esphome/components/veml3235/* @kbx81
|
||||||
esphome/components/veml7700/* @latonita
|
esphome/components/veml7700/* @latonita
|
||||||
esphome/components/version/* @esphome/core
|
esphome/components/version/* @esphome/core
|
||||||
esphome/components/voice_assistant/* @jesserockz @kahrendt
|
esphome/components/voice_assistant/* @jesserockz
|
||||||
esphome/components/wake_on_lan/* @clydebarrow @willwill2will54
|
esphome/components/wake_on_lan/* @clydebarrow @willwill2will54
|
||||||
esphome/components/watchdog/* @oarcher
|
esphome/components/watchdog/* @oarcher
|
||||||
esphome/components/waveshare_epaper/* @clydebarrow
|
esphome/components/waveshare_epaper/* @clydebarrow
|
||||||
|
|||||||
@@ -11,9 +11,7 @@ FROM base-source-${BUILD_TYPE} AS base
|
|||||||
|
|
||||||
RUN git config --system --add safe.directory "*"
|
RUN git config --system --add safe.directory "*"
|
||||||
|
|
||||||
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
|
RUN pip install uv==0.6.14
|
||||||
|
|
||||||
RUN pip install --no-cache-dir -U pip uv==0.6.14
|
|
||||||
|
|
||||||
COPY requirements.txt /
|
COPY requirements.txt /
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ from esphome.const import (
|
|||||||
)
|
)
|
||||||
from esphome.core import CORE, EsphomeError, coroutine
|
from esphome.core import CORE, EsphomeError, coroutine
|
||||||
from esphome.helpers import get_bool_env, indent, is_ip_address
|
from esphome.helpers import get_bool_env, indent, is_ip_address
|
||||||
from esphome.log import AnsiFore, color, setup_log
|
from esphome.log import Fore, color, setup_log
|
||||||
from esphome.util import (
|
from esphome.util import (
|
||||||
get_serial_ports,
|
get_serial_ports,
|
||||||
list_yaml_files,
|
list_yaml_files,
|
||||||
@@ -83,7 +83,7 @@ def choose_prompt(options, purpose: str = None):
|
|||||||
raise ValueError
|
raise ValueError
|
||||||
break
|
break
|
||||||
except ValueError:
|
except ValueError:
|
||||||
safe_print(color(AnsiFore.RED, f"Invalid option: '{opt}'"))
|
safe_print(color(Fore.RED, f"Invalid option: '{opt}'"))
|
||||||
return options[opt - 1][1]
|
return options[opt - 1][1]
|
||||||
|
|
||||||
|
|
||||||
@@ -134,7 +134,6 @@ def get_port_type(port):
|
|||||||
|
|
||||||
|
|
||||||
def run_miniterm(config, port, args):
|
def run_miniterm(config, port, args):
|
||||||
from aioesphomeapi import LogParser
|
|
||||||
import serial
|
import serial
|
||||||
|
|
||||||
from esphome import platformio_api
|
from esphome import platformio_api
|
||||||
@@ -159,7 +158,6 @@ def run_miniterm(config, port, args):
|
|||||||
ser.dtr = False
|
ser.dtr = False
|
||||||
ser.rts = False
|
ser.rts = False
|
||||||
|
|
||||||
parser = LogParser()
|
|
||||||
tries = 0
|
tries = 0
|
||||||
while tries < 5:
|
while tries < 5:
|
||||||
try:
|
try:
|
||||||
@@ -176,7 +174,8 @@ def run_miniterm(config, port, args):
|
|||||||
.decode("utf8", "backslashreplace")
|
.decode("utf8", "backslashreplace")
|
||||||
)
|
)
|
||||||
time_str = datetime.now().time().strftime("[%H:%M:%S]")
|
time_str = datetime.now().time().strftime("[%H:%M:%S]")
|
||||||
safe_print(parser.parse_line(line, time_str))
|
message = time_str + line
|
||||||
|
safe_print(message)
|
||||||
|
|
||||||
backtrace_state = platformio_api.process_stacktrace(
|
backtrace_state = platformio_api.process_stacktrace(
|
||||||
config, line, backtrace_state=backtrace_state
|
config, line, backtrace_state=backtrace_state
|
||||||
@@ -594,38 +593,33 @@ def command_update_all(args):
|
|||||||
middle_text = f" {middle_text} "
|
middle_text = f" {middle_text} "
|
||||||
width = len(click.unstyle(middle_text))
|
width = len(click.unstyle(middle_text))
|
||||||
half_line = "=" * ((twidth - width) // 2)
|
half_line = "=" * ((twidth - width) // 2)
|
||||||
safe_print(f"{half_line}{middle_text}{half_line}")
|
click.echo(f"{half_line}{middle_text}{half_line}")
|
||||||
|
|
||||||
for f in files:
|
for f in files:
|
||||||
safe_print(f"Updating {color(AnsiFore.CYAN, f)}")
|
print(f"Updating {color(Fore.CYAN, f)}")
|
||||||
safe_print("-" * twidth)
|
print("-" * twidth)
|
||||||
safe_print()
|
print()
|
||||||
if CORE.dashboard:
|
rc = run_external_process(
|
||||||
rc = run_external_process(
|
"esphome", "--dashboard", "run", f, "--no-logs", "--device", "OTA"
|
||||||
"esphome", "--dashboard", "run", f, "--no-logs", "--device", "OTA"
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
rc = run_external_process(
|
|
||||||
"esphome", "run", f, "--no-logs", "--device", "OTA"
|
|
||||||
)
|
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
print_bar(f"[{color(AnsiFore.BOLD_GREEN, 'SUCCESS')}] {f}")
|
print_bar(f"[{color(Fore.BOLD_GREEN, 'SUCCESS')}] {f}")
|
||||||
success[f] = True
|
success[f] = True
|
||||||
else:
|
else:
|
||||||
print_bar(f"[{color(AnsiFore.BOLD_RED, 'ERROR')}] {f}")
|
print_bar(f"[{color(Fore.BOLD_RED, 'ERROR')}] {f}")
|
||||||
success[f] = False
|
success[f] = False
|
||||||
|
|
||||||
safe_print()
|
print()
|
||||||
safe_print()
|
print()
|
||||||
safe_print()
|
print()
|
||||||
|
|
||||||
print_bar(f"[{color(AnsiFore.BOLD_WHITE, 'SUMMARY')}]")
|
print_bar(f"[{color(Fore.BOLD_WHITE, 'SUMMARY')}]")
|
||||||
failed = 0
|
failed = 0
|
||||||
for f in files:
|
for f in files:
|
||||||
if success[f]:
|
if success[f]:
|
||||||
safe_print(f" - {f}: {color(AnsiFore.GREEN, 'SUCCESS')}")
|
print(f" - {f}: {color(Fore.GREEN, 'SUCCESS')}")
|
||||||
else:
|
else:
|
||||||
safe_print(f" - {f}: {color(AnsiFore.BOLD_RED, 'FAILED')}")
|
print(f" - {f}: {color(Fore.BOLD_RED, 'FAILED')}")
|
||||||
failed += 1
|
failed += 1
|
||||||
return failed
|
return failed
|
||||||
|
|
||||||
@@ -651,7 +645,7 @@ def command_rename(args, config):
|
|||||||
if c not in ALLOWED_NAME_CHARS:
|
if c not in ALLOWED_NAME_CHARS:
|
||||||
print(
|
print(
|
||||||
color(
|
color(
|
||||||
AnsiFore.BOLD_RED,
|
Fore.BOLD_RED,
|
||||||
f"'{c}' is an invalid character for names. Valid characters are: "
|
f"'{c}' is an invalid character for names. Valid characters are: "
|
||||||
f"{ALLOWED_NAME_CHARS} (lowercase, no spaces)",
|
f"{ALLOWED_NAME_CHARS} (lowercase, no spaces)",
|
||||||
)
|
)
|
||||||
@@ -664,9 +658,7 @@ def command_rename(args, config):
|
|||||||
yaml = yaml_util.load_yaml(CORE.config_path)
|
yaml = yaml_util.load_yaml(CORE.config_path)
|
||||||
if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]:
|
if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]:
|
||||||
print(
|
print(
|
||||||
color(
|
color(Fore.BOLD_RED, "Complex YAML files cannot be automatically renamed.")
|
||||||
AnsiFore.BOLD_RED, "Complex YAML files cannot be automatically renamed."
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
old_name = yaml[CONF_ESPHOME][CONF_NAME]
|
old_name = yaml[CONF_ESPHOME][CONF_NAME]
|
||||||
@@ -689,7 +681,7 @@ def command_rename(args, config):
|
|||||||
)
|
)
|
||||||
> 1
|
> 1
|
||||||
):
|
):
|
||||||
print(color(AnsiFore.BOLD_RED, "Too many matches in YAML to safely rename"))
|
print(color(Fore.BOLD_RED, "Too many matches in YAML to safely rename"))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
new_raw = re.sub(
|
new_raw = re.sub(
|
||||||
@@ -701,7 +693,7 @@ def command_rename(args, config):
|
|||||||
|
|
||||||
new_path = os.path.join(CORE.config_dir, args.name + ".yaml")
|
new_path = os.path.join(CORE.config_dir, args.name + ".yaml")
|
||||||
print(
|
print(
|
||||||
f"Updating {color(AnsiFore.CYAN, CORE.config_path)} to {color(AnsiFore.CYAN, new_path)}"
|
f"Updating {color(Fore.CYAN, CORE.config_path)} to {color(Fore.CYAN, new_path)}"
|
||||||
)
|
)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
@@ -710,7 +702,7 @@ def command_rename(args, config):
|
|||||||
|
|
||||||
rc = run_external_process("esphome", "config", new_path)
|
rc = run_external_process("esphome", "config", new_path)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
print(color(AnsiFore.BOLD_RED, "Rename failed. Reverting changes."))
|
print(color(Fore.BOLD_RED, "Rename failed. Reverting changes."))
|
||||||
os.remove(new_path)
|
os.remove(new_path)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -736,7 +728,7 @@ def command_rename(args, config):
|
|||||||
if CORE.config_path != new_path:
|
if CORE.config_path != new_path:
|
||||||
os.remove(CORE.config_path)
|
os.remove(CORE.config_path)
|
||||||
|
|
||||||
print(color(AnsiFore.BOLD_GREEN, "SUCCESS"))
|
print(color(Fore.BOLD_GREEN, "SUCCESS"))
|
||||||
print()
|
print()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace a4988 {
|
|||||||
static const char *const TAG = "a4988.stepper";
|
static const char *const TAG = "a4988.stepper";
|
||||||
|
|
||||||
void A4988::setup() {
|
void A4988::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up A4988...");
|
||||||
if (this->sleep_pin_ != nullptr) {
|
if (this->sleep_pin_ != nullptr) {
|
||||||
this->sleep_pin_->setup();
|
this->sleep_pin_->setup();
|
||||||
this->sleep_pin_->digital_write(false);
|
this->sleep_pin_->digital_write(false);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace absolute_humidity {
|
|||||||
static const char *const TAG = "absolute_humidity.sensor";
|
static const char *const TAG = "absolute_humidity.sensor";
|
||||||
|
|
||||||
void AbsoluteHumidityComponent::setup() {
|
void AbsoluteHumidityComponent::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->get_name().c_str());
|
ESP_LOGCONFIG(TAG, "Setting up absolute humidity '%s'...", this->get_name().c_str());
|
||||||
|
|
||||||
ESP_LOGD(TAG, " Added callback for temperature '%s'", this->temperature_sensor_->get_name().c_str());
|
ESP_LOGD(TAG, " Added callback for temperature '%s'", this->temperature_sensor_->get_name().c_str());
|
||||||
this->temperature_sensor_->add_on_state_callback([this](float state) { this->temperature_callback_(state); });
|
this->temperature_sensor_->add_on_state_callback([this](float state) { this->temperature_callback_(state); });
|
||||||
@@ -40,11 +40,9 @@ void AbsoluteHumidityComponent::dump_config() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, "Sources");
|
||||||
"Sources\n"
|
ESP_LOGCONFIG(TAG, " Temperature: '%s'", this->temperature_sensor_->get_name().c_str());
|
||||||
" Temperature: '%s'\n"
|
ESP_LOGCONFIG(TAG, " Relative Humidity: '%s'", this->humidity_sensor_->get_name().c_str());
|
||||||
" Relative Humidity: '%s'",
|
|
||||||
this->temperature_sensor_->get_name().c_str(), this->humidity_sensor_->get_name().c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float AbsoluteHumidityComponent::get_setup_priority() const { return setup_priority::DATA; }
|
float AbsoluteHumidityComponent::get_setup_priority() const { return setup_priority::DATA; }
|
||||||
|
|||||||
@@ -214,10 +214,8 @@ void AcDimmer::dump_config() {
|
|||||||
ESP_LOGCONFIG(TAG, "AcDimmer:");
|
ESP_LOGCONFIG(TAG, "AcDimmer:");
|
||||||
LOG_PIN(" Output Pin: ", this->gate_pin_);
|
LOG_PIN(" Output Pin: ", this->gate_pin_);
|
||||||
LOG_PIN(" Zero-Cross Pin: ", this->zero_cross_pin_);
|
LOG_PIN(" Zero-Cross Pin: ", this->zero_cross_pin_);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Min Power: %.1f%%", this->store_.min_power / 10.0f);
|
||||||
" Min Power: %.1f%%\n"
|
ESP_LOGCONFIG(TAG, " Init with half cycle: %s", YESNO(this->init_with_half_cycle_));
|
||||||
" Init with half cycle: %s",
|
|
||||||
this->store_.min_power / 10.0f, YESNO(this->init_with_half_cycle_));
|
|
||||||
if (method_ == DIM_METHOD_LEADING_PULSE) {
|
if (method_ == DIM_METHOD_LEADING_PULSE) {
|
||||||
ESP_LOGCONFIG(TAG, " Method: leading pulse");
|
ESP_LOGCONFIG(TAG, " Method: leading pulse");
|
||||||
} else if (method_ == DIM_METHOD_LEADING) {
|
} else if (method_ == DIM_METHOD_LEADING) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ static const int ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1;
|
|||||||
static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1;
|
static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1;
|
||||||
|
|
||||||
void ADCSensor::setup() {
|
void ADCSensor::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->get_name().c_str());
|
ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
|
||||||
|
|
||||||
if (this->channel1_ != ADC1_CHANNEL_MAX) {
|
if (this->channel1_ != ADC1_CHANNEL_MAX) {
|
||||||
adc1_config_width(ADC_WIDTH_MAX_SOC_BITS);
|
adc1_config_width(ADC_WIDTH_MAX_SOC_BITS);
|
||||||
@@ -77,10 +77,8 @@ void ADCSensor::dump_config() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
|
||||||
" Samples: %i\n"
|
ESP_LOGCONFIG(TAG, " Sampling mode: %s", LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
||||||
" Sampling mode: %s",
|
|
||||||
this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace adc {
|
|||||||
static const char *const TAG = "adc.esp8266";
|
static const char *const TAG = "adc.esp8266";
|
||||||
|
|
||||||
void ADCSensor::setup() {
|
void ADCSensor::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->get_name().c_str());
|
ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
|
||||||
#ifndef USE_ADC_SENSOR_VCC
|
#ifndef USE_ADC_SENSOR_VCC
|
||||||
this->pin_->setup();
|
this->pin_->setup();
|
||||||
#endif
|
#endif
|
||||||
@@ -30,10 +30,8 @@ void ADCSensor::dump_config() {
|
|||||||
#else
|
#else
|
||||||
LOG_PIN(" Pin: ", this->pin_);
|
LOG_PIN(" Pin: ", this->pin_);
|
||||||
#endif // USE_ADC_SENSOR_VCC
|
#endif // USE_ADC_SENSOR_VCC
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
|
||||||
" Samples: %i\n"
|
ESP_LOGCONFIG(TAG, " Sampling mode: %s", LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
||||||
" Sampling mode: %s",
|
|
||||||
this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace adc {
|
|||||||
static const char *const TAG = "adc.libretiny";
|
static const char *const TAG = "adc.libretiny";
|
||||||
|
|
||||||
void ADCSensor::setup() {
|
void ADCSensor::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->get_name().c_str());
|
ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
|
||||||
#ifndef USE_ADC_SENSOR_VCC
|
#ifndef USE_ADC_SENSOR_VCC
|
||||||
this->pin_->setup();
|
this->pin_->setup();
|
||||||
#endif // !USE_ADC_SENSOR_VCC
|
#endif // !USE_ADC_SENSOR_VCC
|
||||||
@@ -22,10 +22,8 @@ void ADCSensor::dump_config() {
|
|||||||
#else // USE_ADC_SENSOR_VCC
|
#else // USE_ADC_SENSOR_VCC
|
||||||
LOG_PIN(" Pin: ", this->pin_);
|
LOG_PIN(" Pin: ", this->pin_);
|
||||||
#endif // USE_ADC_SENSOR_VCC
|
#endif // USE_ADC_SENSOR_VCC
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
|
||||||
" Samples: %i\n"
|
ESP_LOGCONFIG(TAG, " Sampling mode: %s", LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
||||||
" Sampling mode: %s",
|
|
||||||
this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace adc {
|
|||||||
static const char *const TAG = "adc.rp2040";
|
static const char *const TAG = "adc.rp2040";
|
||||||
|
|
||||||
void ADCSensor::setup() {
|
void ADCSensor::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->get_name().c_str());
|
ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
|
||||||
static bool initialized = false;
|
static bool initialized = false;
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
adc_init();
|
adc_init();
|
||||||
@@ -33,10 +33,8 @@ void ADCSensor::dump_config() {
|
|||||||
LOG_PIN(" Pin: ", this->pin_);
|
LOG_PIN(" Pin: ", this->pin_);
|
||||||
#endif // USE_ADC_SENSOR_VCC
|
#endif // USE_ADC_SENSOR_VCC
|
||||||
}
|
}
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
|
||||||
" Samples: %i\n"
|
ESP_LOGCONFIG(TAG, " Sampling mode: %s", LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
||||||
" Sampling mode: %s",
|
|
||||||
this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ static const char *const TAG = "adc128s102";
|
|||||||
float ADC128S102::get_setup_priority() const { return setup_priority::HARDWARE; }
|
float ADC128S102::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
void ADC128S102::setup() {
|
void ADC128S102::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up adc128s102");
|
||||||
this->spi_setup();
|
this->spi_setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -177,14 +177,11 @@ void ADE7880::dump_config() {
|
|||||||
LOG_SENSOR(" ", "Power Factor", this->channel_a_->power_factor);
|
LOG_SENSOR(" ", "Power Factor", this->channel_a_->power_factor);
|
||||||
LOG_SENSOR(" ", "Forward Active Energy", this->channel_a_->forward_active_energy);
|
LOG_SENSOR(" ", "Forward Active Energy", this->channel_a_->forward_active_energy);
|
||||||
LOG_SENSOR(" ", "Reverse Active Energy", this->channel_a_->reverse_active_energy);
|
LOG_SENSOR(" ", "Reverse Active Energy", this->channel_a_->reverse_active_energy);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Calibration:");
|
||||||
" Calibration:\n"
|
ESP_LOGCONFIG(TAG, " Current: %" PRId32, this->channel_a_->current_gain_calibration);
|
||||||
" Current: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Voltage: %" PRId32, this->channel_a_->voltage_gain_calibration);
|
||||||
" Voltage: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Power: %" PRId32, this->channel_a_->power_gain_calibration);
|
||||||
" Power: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Phase Angle: %u", this->channel_a_->phase_angle_calibration);
|
||||||
" Phase Angle: %u",
|
|
||||||
this->channel_a_->current_gain_calibration, this->channel_a_->voltage_gain_calibration,
|
|
||||||
this->channel_a_->power_gain_calibration, this->channel_a_->phase_angle_calibration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->channel_b_ != nullptr) {
|
if (this->channel_b_ != nullptr) {
|
||||||
@@ -196,14 +193,11 @@ void ADE7880::dump_config() {
|
|||||||
LOG_SENSOR(" ", "Power Factor", this->channel_b_->power_factor);
|
LOG_SENSOR(" ", "Power Factor", this->channel_b_->power_factor);
|
||||||
LOG_SENSOR(" ", "Forward Active Energy", this->channel_b_->forward_active_energy);
|
LOG_SENSOR(" ", "Forward Active Energy", this->channel_b_->forward_active_energy);
|
||||||
LOG_SENSOR(" ", "Reverse Active Energy", this->channel_b_->reverse_active_energy);
|
LOG_SENSOR(" ", "Reverse Active Energy", this->channel_b_->reverse_active_energy);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Calibration:");
|
||||||
" Calibration:\n"
|
ESP_LOGCONFIG(TAG, " Current: %" PRId32, this->channel_b_->current_gain_calibration);
|
||||||
" Current: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Voltage: %" PRId32, this->channel_b_->voltage_gain_calibration);
|
||||||
" Voltage: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Power: %" PRId32, this->channel_b_->power_gain_calibration);
|
||||||
" Power: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Phase Angle: %u", this->channel_b_->phase_angle_calibration);
|
||||||
" Phase Angle: %u",
|
|
||||||
this->channel_b_->current_gain_calibration, this->channel_b_->voltage_gain_calibration,
|
|
||||||
this->channel_b_->power_gain_calibration, this->channel_b_->phase_angle_calibration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->channel_c_ != nullptr) {
|
if (this->channel_c_ != nullptr) {
|
||||||
@@ -215,23 +209,18 @@ void ADE7880::dump_config() {
|
|||||||
LOG_SENSOR(" ", "Power Factor", this->channel_c_->power_factor);
|
LOG_SENSOR(" ", "Power Factor", this->channel_c_->power_factor);
|
||||||
LOG_SENSOR(" ", "Forward Active Energy", this->channel_c_->forward_active_energy);
|
LOG_SENSOR(" ", "Forward Active Energy", this->channel_c_->forward_active_energy);
|
||||||
LOG_SENSOR(" ", "Reverse Active Energy", this->channel_c_->reverse_active_energy);
|
LOG_SENSOR(" ", "Reverse Active Energy", this->channel_c_->reverse_active_energy);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Calibration:");
|
||||||
" Calibration:\n"
|
ESP_LOGCONFIG(TAG, " Current: %" PRId32, this->channel_c_->current_gain_calibration);
|
||||||
" Current: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Voltage: %" PRId32, this->channel_c_->voltage_gain_calibration);
|
||||||
" Voltage: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Power: %" PRId32, this->channel_c_->power_gain_calibration);
|
||||||
" Power: %" PRId32 "\n"
|
ESP_LOGCONFIG(TAG, " Phase Angle: %u", this->channel_c_->phase_angle_calibration);
|
||||||
" Phase Angle: %u",
|
|
||||||
this->channel_c_->current_gain_calibration, this->channel_c_->voltage_gain_calibration,
|
|
||||||
this->channel_c_->power_gain_calibration, this->channel_c_->phase_angle_calibration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->channel_n_ != nullptr) {
|
if (this->channel_n_ != nullptr) {
|
||||||
ESP_LOGCONFIG(TAG, " Neutral:");
|
ESP_LOGCONFIG(TAG, " Neutral:");
|
||||||
LOG_SENSOR(" ", "Current", this->channel_n_->current);
|
LOG_SENSOR(" ", "Current", this->channel_n_->current);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Calibration:");
|
||||||
" Calibration:\n"
|
ESP_LOGCONFIG(TAG, " Current: %" PRId32, this->channel_n_->current_gain_calibration);
|
||||||
" Current: %" PRId32,
|
|
||||||
this->channel_n_->current_gain_calibration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
|
|||||||
@@ -58,18 +58,15 @@ void ADE7953::dump_config() {
|
|||||||
LOG_SENSOR(" ", "Active Power B Sensor", this->active_power_b_sensor_);
|
LOG_SENSOR(" ", "Active Power B Sensor", this->active_power_b_sensor_);
|
||||||
LOG_SENSOR(" ", "Rective Power A Sensor", this->reactive_power_a_sensor_);
|
LOG_SENSOR(" ", "Rective Power A Sensor", this->reactive_power_a_sensor_);
|
||||||
LOG_SENSOR(" ", "Reactive Power B Sensor", this->reactive_power_b_sensor_);
|
LOG_SENSOR(" ", "Reactive Power B Sensor", this->reactive_power_b_sensor_);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " USE_ACC_ENERGY_REGS: %d", this->use_acc_energy_regs_);
|
||||||
" USE_ACC_ENERGY_REGS: %d\n"
|
ESP_LOGCONFIG(TAG, " PGA_V_8: 0x%X", pga_v_);
|
||||||
" PGA_V_8: 0x%X\n"
|
ESP_LOGCONFIG(TAG, " PGA_IA_8: 0x%X", pga_ia_);
|
||||||
" PGA_IA_8: 0x%X\n"
|
ESP_LOGCONFIG(TAG, " PGA_IB_8: 0x%X", pga_ib_);
|
||||||
" PGA_IB_8: 0x%X\n"
|
ESP_LOGCONFIG(TAG, " VGAIN_32: 0x%08jX", (uintmax_t) vgain_);
|
||||||
" VGAIN_32: 0x%08jX\n"
|
ESP_LOGCONFIG(TAG, " AIGAIN_32: 0x%08jX", (uintmax_t) aigain_);
|
||||||
" AIGAIN_32: 0x%08jX\n"
|
ESP_LOGCONFIG(TAG, " BIGAIN_32: 0x%08jX", (uintmax_t) bigain_);
|
||||||
" BIGAIN_32: 0x%08jX\n"
|
ESP_LOGCONFIG(TAG, " AWGAIN_32: 0x%08jX", (uintmax_t) awgain_);
|
||||||
" AWGAIN_32: 0x%08jX\n"
|
ESP_LOGCONFIG(TAG, " BWGAIN_32: 0x%08jX", (uintmax_t) bwgain_);
|
||||||
" BWGAIN_32: 0x%08jX",
|
|
||||||
this->use_acc_energy_regs_, pga_v_, pga_ia_, pga_ib_, (uintmax_t) vgain_, (uintmax_t) aigain_,
|
|
||||||
(uintmax_t) bigain_, (uintmax_t) awgain_, (uintmax_t) bwgain_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ADE_PUBLISH_(name, val, factor) \
|
#define ADE_PUBLISH_(name, val, factor) \
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "ade7953_i2c.h"
|
#include "ade7953_i2c.h"
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ade7953_i2c {
|
namespace ade7953_i2c {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "ade7953_spi.h"
|
#include "ade7953_spi.h"
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ade7953_spi {
|
namespace ade7953_spi {
|
||||||
|
|||||||
@@ -10,13 +10,15 @@ static const uint8_t ADS1115_REGISTER_CONVERSION = 0x00;
|
|||||||
static const uint8_t ADS1115_REGISTER_CONFIG = 0x01;
|
static const uint8_t ADS1115_REGISTER_CONFIG = 0x01;
|
||||||
|
|
||||||
void ADS1115Component::setup() {
|
void ADS1115Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up ADS1115...");
|
||||||
uint16_t value;
|
uint16_t value;
|
||||||
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &value)) {
|
if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &value)) {
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ESP_LOGCONFIG(TAG, "Configuring ADS1115...");
|
||||||
|
|
||||||
uint16_t config = 0;
|
uint16_t config = 0;
|
||||||
// Clear single-shot bit
|
// Clear single-shot bit
|
||||||
// 0b0xxxxxxxxxxxxxxx
|
// 0b0xxxxxxxxxxxxxxx
|
||||||
@@ -66,10 +68,10 @@ void ADS1115Component::setup() {
|
|||||||
this->prev_config_ = config;
|
this->prev_config_ = config;
|
||||||
}
|
}
|
||||||
void ADS1115Component::dump_config() {
|
void ADS1115Component::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "ADS1115:");
|
ESP_LOGCONFIG(TAG, "Setting up ADS1115...");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with ADS1115 failed!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
float ADS1115Component::request_measurement(ADS1115Multiplexer multiplexer, ADS1115Gain gain,
|
float ADS1115Component::request_measurement(ADS1115Multiplexer multiplexer, ADS1115Gain gain,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "ads1118.h"
|
#include "ads1118.h"
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
@@ -9,7 +8,7 @@ static const char *const TAG = "ads1118";
|
|||||||
static const uint8_t ADS1118_DATA_RATE_860_SPS = 0b111;
|
static const uint8_t ADS1118_DATA_RATE_860_SPS = 0b111;
|
||||||
|
|
||||||
void ADS1118::setup() {
|
void ADS1118::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up ads1118");
|
||||||
this->spi_setup();
|
this->spi_setup();
|
||||||
|
|
||||||
this->config_ = 0;
|
this->config_ = 0;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "ags10.h"
|
#include "ags10.h"
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
|
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ static const uint16_t ZP_CURRENT = 0x0000;
|
|||||||
static const uint16_t ZP_DEFAULT = 0xFFFF;
|
static const uint16_t ZP_DEFAULT = 0xFFFF;
|
||||||
|
|
||||||
void AGS10Component::setup() {
|
void AGS10Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up ags10...");
|
||||||
|
|
||||||
auto version = this->read_version_();
|
auto version = this->read_version_();
|
||||||
if (version) {
|
if (version) {
|
||||||
@@ -66,7 +65,7 @@ void AGS10Component::dump_config() {
|
|||||||
case NONE:
|
case NONE:
|
||||||
break;
|
break;
|
||||||
case COMMUNICATION_FAILED:
|
case COMMUNICATION_FAILED:
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AGS10 failed!");
|
||||||
break;
|
break;
|
||||||
case CRC_CHECK_FAILED:
|
case CRC_CHECK_FAILED:
|
||||||
ESP_LOGE(TAG, "The crc check failed");
|
ESP_LOGE(TAG, "The crc check failed");
|
||||||
|
|||||||
@@ -13,9 +13,8 @@
|
|||||||
// results making successive requests; the current implementation makes 3 attempts with a delay of 30ms each time.
|
// results making successive requests; the current implementation makes 3 attempts with a delay of 30ms each time.
|
||||||
|
|
||||||
#include "aht10.h"
|
#include "aht10.h"
|
||||||
#include "esphome/core/hal.h"
|
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/hal.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace aht10 {
|
namespace aht10 {
|
||||||
@@ -35,59 +34,57 @@ static const uint8_t AHT10_INIT_ATTEMPTS = 10;
|
|||||||
|
|
||||||
static const uint8_t AHT10_STATUS_BUSY = 0x80;
|
static const uint8_t AHT10_STATUS_BUSY = 0x80;
|
||||||
|
|
||||||
static const float AHT10_DIVISOR = 1048576.0f; // 2^20, used for temperature and humidity calculations
|
|
||||||
|
|
||||||
void AHT10Component::setup() {
|
void AHT10Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
|
||||||
|
|
||||||
if (this->write(AHT10_SOFTRESET_CMD, sizeof(AHT10_SOFTRESET_CMD)) != i2c::ERROR_OK) {
|
if (this->write(AHT10_SOFTRESET_CMD, sizeof(AHT10_SOFTRESET_CMD)) != i2c::ERROR_OK) {
|
||||||
ESP_LOGE(TAG, "Reset failed");
|
ESP_LOGE(TAG, "Reset AHT10 failed!");
|
||||||
}
|
}
|
||||||
delay(AHT10_SOFTRESET_DELAY);
|
delay(AHT10_SOFTRESET_DELAY);
|
||||||
|
|
||||||
i2c::ErrorCode error_code = i2c::ERROR_INVALID_ARGUMENT;
|
i2c::ErrorCode error_code = i2c::ERROR_INVALID_ARGUMENT;
|
||||||
switch (this->variant_) {
|
switch (this->variant_) {
|
||||||
case AHT10Variant::AHT20:
|
case AHT10Variant::AHT20:
|
||||||
|
ESP_LOGCONFIG(TAG, "Setting up AHT20");
|
||||||
error_code = this->write(AHT20_INITIALIZE_CMD, sizeof(AHT20_INITIALIZE_CMD));
|
error_code = this->write(AHT20_INITIALIZE_CMD, sizeof(AHT20_INITIALIZE_CMD));
|
||||||
break;
|
break;
|
||||||
case AHT10Variant::AHT10:
|
case AHT10Variant::AHT10:
|
||||||
|
ESP_LOGCONFIG(TAG, "Setting up AHT10");
|
||||||
error_code = this->write(AHT10_INITIALIZE_CMD, sizeof(AHT10_INITIALIZE_CMD));
|
error_code = this->write(AHT10_INITIALIZE_CMD, sizeof(AHT10_INITIALIZE_CMD));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (error_code != i2c::ERROR_OK) {
|
if (error_code != i2c::ERROR_OK) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AHT10 failed!");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t cal_attempts = 0;
|
|
||||||
uint8_t data = AHT10_STATUS_BUSY;
|
uint8_t data = AHT10_STATUS_BUSY;
|
||||||
|
int cal_attempts = 0;
|
||||||
while (data & AHT10_STATUS_BUSY) {
|
while (data & AHT10_STATUS_BUSY) {
|
||||||
delay(AHT10_DEFAULT_DELAY);
|
delay(AHT10_DEFAULT_DELAY);
|
||||||
if (this->read(&data, 1) != i2c::ERROR_OK) {
|
if (this->read(&data, 1) != i2c::ERROR_OK) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AHT10 failed!");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
++cal_attempts;
|
++cal_attempts;
|
||||||
if (cal_attempts > AHT10_INIT_ATTEMPTS) {
|
if (cal_attempts > AHT10_INIT_ATTEMPTS) {
|
||||||
ESP_LOGE(TAG, "Initialization timed out");
|
ESP_LOGE(TAG, "AHT10 initialization timed out!");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((data & 0x68) != 0x08) { // Bit[6:5] = 0b00, NORMAL mode and Bit[3] = 0b1, CALIBRATED
|
if ((data & 0x68) != 0x08) { // Bit[6:5] = 0b00, NORMAL mode and Bit[3] = 0b1, CALIBRATED
|
||||||
ESP_LOGE(TAG, "Initialization failed");
|
ESP_LOGE(TAG, "AHT10 initialization failed!");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Initialization complete");
|
ESP_LOGV(TAG, "AHT10 initialization");
|
||||||
}
|
}
|
||||||
|
|
||||||
void AHT10Component::restart_read_() {
|
void AHT10Component::restart_read_() {
|
||||||
if (this->read_count_ == AHT10_ATTEMPTS) {
|
if (this->read_count_ == AHT10_ATTEMPTS) {
|
||||||
this->read_count_ = 0;
|
this->read_count_ = 0;
|
||||||
this->status_set_error("Reading timed out");
|
this->status_set_error("Measurements reading timed-out!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->read_count_++;
|
this->read_count_++;
|
||||||
@@ -100,24 +97,24 @@ void AHT10Component::read_data_() {
|
|||||||
ESP_LOGD(TAG, "Read attempt %d at %ums", this->read_count_, (unsigned) (millis() - this->start_time_));
|
ESP_LOGD(TAG, "Read attempt %d at %ums", this->read_count_, (unsigned) (millis() - this->start_time_));
|
||||||
}
|
}
|
||||||
if (this->read(data, 6) != i2c::ERROR_OK) {
|
if (this->read(data, 6) != i2c::ERROR_OK) {
|
||||||
this->status_set_warning("Read failed, will retry");
|
this->status_set_warning("AHT10 read failed, retrying soon");
|
||||||
this->restart_read_();
|
this->restart_read_();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((data[0] & 0x80) == 0x80) { // Bit[7] = 0b1, device is busy
|
if ((data[0] & 0x80) == 0x80) { // Bit[7] = 0b1, device is busy
|
||||||
ESP_LOGD(TAG, "Device busy, will retry");
|
ESP_LOGD(TAG, "AHT10 is busy, waiting...");
|
||||||
this->restart_read_();
|
this->restart_read_();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
|
if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
|
||||||
// Invalid humidity (0x0)
|
// Unrealistic humidity (0x0)
|
||||||
if (this->humidity_sensor_ == nullptr) {
|
if (this->humidity_sensor_ == nullptr) {
|
||||||
ESP_LOGV(TAG, "Invalid humidity (reading not required)");
|
ESP_LOGV(TAG, "ATH10 Unrealistic humidity (0x0), but humidity is not required");
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGD(TAG, "Invalid humidity, retrying");
|
ESP_LOGD(TAG, "ATH10 Unrealistic humidity (0x0), retrying...");
|
||||||
if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
|
if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
|
||||||
this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
|
this->status_set_warning("Communication with AHT10 failed!");
|
||||||
}
|
}
|
||||||
this->restart_read_();
|
this->restart_read_();
|
||||||
return;
|
return;
|
||||||
@@ -126,17 +123,22 @@ void AHT10Component::read_data_() {
|
|||||||
if (this->read_count_ > 1) {
|
if (this->read_count_ > 1) {
|
||||||
ESP_LOGD(TAG, "Success at %ums", (unsigned) (millis() - this->start_time_));
|
ESP_LOGD(TAG, "Success at %ums", (unsigned) (millis() - this->start_time_));
|
||||||
}
|
}
|
||||||
uint32_t raw_temperature = encode_uint24(data[3] & 0xF, data[4], data[5]);
|
uint32_t raw_temperature = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
|
||||||
uint32_t raw_humidity = encode_uint24(data[1], data[2], data[3]) >> 4;
|
uint32_t raw_humidity = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4;
|
||||||
|
|
||||||
if (this->temperature_sensor_ != nullptr) {
|
if (this->temperature_sensor_ != nullptr) {
|
||||||
float temperature = ((200.0f * static_cast<float>(raw_temperature)) / AHT10_DIVISOR) - 50.0f;
|
float temperature = ((200.0f * (float) raw_temperature) / 1048576.0f) - 50.0f;
|
||||||
this->temperature_sensor_->publish_state(temperature);
|
this->temperature_sensor_->publish_state(temperature);
|
||||||
}
|
}
|
||||||
if (this->humidity_sensor_ != nullptr) {
|
if (this->humidity_sensor_ != nullptr) {
|
||||||
float humidity = raw_humidity == 0 ? NAN : static_cast<float>(raw_humidity) * 100.0f / AHT10_DIVISOR;
|
float humidity;
|
||||||
|
if (raw_humidity == 0) { // unrealistic value
|
||||||
|
humidity = NAN;
|
||||||
|
} else {
|
||||||
|
humidity = (float) raw_humidity * 100.0f / 1048576.0f;
|
||||||
|
}
|
||||||
if (std::isnan(humidity)) {
|
if (std::isnan(humidity)) {
|
||||||
ESP_LOGW(TAG, "Invalid humidity reading (0%%), ");
|
ESP_LOGW(TAG, "Invalid humidity! Sensor reported 0%% Hum");
|
||||||
}
|
}
|
||||||
this->humidity_sensor_->publish_state(humidity);
|
this->humidity_sensor_->publish_state(humidity);
|
||||||
}
|
}
|
||||||
@@ -148,7 +150,7 @@ void AHT10Component::update() {
|
|||||||
return;
|
return;
|
||||||
this->start_time_ = millis();
|
this->start_time_ = millis();
|
||||||
if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
|
if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
|
||||||
this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
|
this->status_set_warning("Communication with AHT10 failed!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->restart_read_();
|
this->restart_read_();
|
||||||
@@ -160,7 +162,7 @@ void AHT10Component::dump_config() {
|
|||||||
ESP_LOGCONFIG(TAG, "AHT10:");
|
ESP_LOGCONFIG(TAG, "AHT10:");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AHT10 failed!");
|
||||||
}
|
}
|
||||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||||
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ static const char *const TAG = "aic3204";
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AIC3204::setup() {
|
void AIC3204::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AIC3204...");
|
||||||
|
|
||||||
// Set register page to 0
|
// Set register page to 0
|
||||||
ERROR_CHECK(this->write_byte(AIC3204_PAGE_CTRL, 0x00), "Set page 0 failed");
|
ERROR_CHECK(this->write_byte(AIC3204_PAGE_CTRL, 0x00), "Set page 0 failed");
|
||||||
@@ -113,7 +113,7 @@ void AIC3204::dump_config() {
|
|||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
|
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AIC3204 failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ AirthingsWaveBase = airthings_wave_base_ns.class_(
|
|||||||
|
|
||||||
|
|
||||||
BASE_SCHEMA = (
|
BASE_SCHEMA = (
|
||||||
cv.Schema(
|
sensor.SENSOR_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
||||||
unit_of_measurement=UNIT_PERCENT,
|
unit_of_measurement=UNIT_PERCENT,
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ from esphome.components import mqtt, web_server
|
|||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_CODE,
|
CONF_CODE,
|
||||||
CONF_ENTITY_CATEGORY,
|
|
||||||
CONF_ICON,
|
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_MQTT_ID,
|
CONF_MQTT_ID,
|
||||||
CONF_ON_STATE,
|
CONF_ON_STATE,
|
||||||
@@ -14,7 +12,6 @@ from esphome.const import (
|
|||||||
CONF_WEB_SERVER,
|
CONF_WEB_SERVER,
|
||||||
)
|
)
|
||||||
from esphome.core import CORE, coroutine_with_priority
|
from esphome.core import CORE, coroutine_with_priority
|
||||||
from esphome.cpp_generator import MockObjClass
|
|
||||||
from esphome.cpp_helpers import setup_entity
|
from esphome.cpp_helpers import setup_entity
|
||||||
|
|
||||||
CODEOWNERS = ["@grahambrown11", "@hwstar"]
|
CODEOWNERS = ["@grahambrown11", "@hwstar"]
|
||||||
@@ -81,11 +78,12 @@ AlarmControlPanelCondition = alarm_control_panel_ns.class_(
|
|||||||
"AlarmControlPanelCondition", automation.Condition
|
"AlarmControlPanelCondition", automation.Condition
|
||||||
)
|
)
|
||||||
|
|
||||||
_ALARM_CONTROL_PANEL_SCHEMA = (
|
ALARM_CONTROL_PANEL_SCHEMA = (
|
||||||
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
|
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
|
||||||
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
|
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
|
||||||
.extend(
|
.extend(
|
||||||
{
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(AlarmControlPanel),
|
||||||
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(
|
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(
|
||||||
mqtt.MQTTAlarmControlPanelComponent
|
mqtt.MQTTAlarmControlPanelComponent
|
||||||
),
|
),
|
||||||
@@ -148,33 +146,6 @@ _ALARM_CONTROL_PANEL_SCHEMA = (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def alarm_control_panel_schema(
|
|
||||||
class_: MockObjClass,
|
|
||||||
*,
|
|
||||||
entity_category: str = cv.UNDEFINED,
|
|
||||||
icon: str = cv.UNDEFINED,
|
|
||||||
) -> cv.Schema:
|
|
||||||
schema = {
|
|
||||||
cv.GenerateID(): cv.declare_id(class_),
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, default, validator in [
|
|
||||||
(CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
|
|
||||||
(CONF_ICON, icon, cv.icon),
|
|
||||||
]:
|
|
||||||
if default is not cv.UNDEFINED:
|
|
||||||
schema[cv.Optional(key, default=default)] = validator
|
|
||||||
|
|
||||||
return _ALARM_CONTROL_PANEL_SCHEMA.extend(schema)
|
|
||||||
|
|
||||||
|
|
||||||
# Remove before 2025.11.0
|
|
||||||
ALARM_CONTROL_PANEL_SCHEMA = alarm_control_panel_schema(AlarmControlPanel)
|
|
||||||
ALARM_CONTROL_PANEL_SCHEMA.add_extra(
|
|
||||||
cv.deprecated_schema_constant("alarm_control_panel")
|
|
||||||
)
|
|
||||||
|
|
||||||
ALARM_CONTROL_PANEL_ACTION_SCHEMA = maybe_simple_id(
|
ALARM_CONTROL_PANEL_ACTION_SCHEMA = maybe_simple_id(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.use_id(AlarmControlPanel),
|
cv.GenerateID(): cv.use_id(AlarmControlPanel),
|
||||||
@@ -235,16 +206,9 @@ async def register_alarm_control_panel(var, config):
|
|||||||
if not CORE.has_id(config[CONF_ID]):
|
if not CORE.has_id(config[CONF_ID]):
|
||||||
var = cg.Pvariable(config[CONF_ID], var)
|
var = cg.Pvariable(config[CONF_ID], var)
|
||||||
cg.add(cg.App.register_alarm_control_panel(var))
|
cg.add(cg.App.register_alarm_control_panel(var))
|
||||||
CORE.register_platform_component("alarm_control_panel", var)
|
|
||||||
await setup_alarm_control_panel_core_(var, config)
|
await setup_alarm_control_panel_core_(var, config)
|
||||||
|
|
||||||
|
|
||||||
async def new_alarm_control_panel(config, *args):
|
|
||||||
var = cg.new_Pvariable(config[CONF_ID], *args)
|
|
||||||
await register_alarm_control_panel(var, config)
|
|
||||||
return var
|
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
@automation.register_action(
|
||||||
"alarm_control_panel.arm_away", ArmAwayAction, ALARM_CONTROL_PANEL_ACTION_SCHEMA
|
"alarm_control_panel.arm_away", ArmAwayAction, ALARM_CONTROL_PANEL_ACTION_SCHEMA
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AM2315C::setup() {
|
void AM2315C::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AM2315C...");
|
||||||
|
|
||||||
// get status
|
// get status
|
||||||
uint8_t status = 0;
|
uint8_t status = 0;
|
||||||
@@ -188,7 +188,7 @@ void AM2315C::dump_config() {
|
|||||||
ESP_LOGCONFIG(TAG, "AM2315C:");
|
ESP_LOGCONFIG(TAG, "AM2315C:");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AM2315C failed!");
|
||||||
}
|
}
|
||||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||||
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ void AM2320Component::update() {
|
|||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
}
|
}
|
||||||
void AM2320Component::setup() {
|
void AM2320Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AM2320...");
|
||||||
uint8_t data[8];
|
uint8_t data[8];
|
||||||
data[0] = 0;
|
data[0] = 0;
|
||||||
data[1] = 4;
|
data[1] = 4;
|
||||||
@@ -47,7 +47,7 @@ void AM2320Component::dump_config() {
|
|||||||
ESP_LOGD(TAG, "AM2320:");
|
ESP_LOGD(TAG, "AM2320:");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AM2320 failed!");
|
||||||
}
|
}
|
||||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||||
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace am43 {
|
namespace am43 {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import ble_client, cover
|
from esphome.components import ble_client, cover
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_PIN
|
from esphome.const import CONF_ID, CONF_PIN
|
||||||
|
|
||||||
CODEOWNERS = ["@buxtronix"]
|
CODEOWNERS = ["@buxtronix"]
|
||||||
DEPENDENCIES = ["ble_client"]
|
DEPENDENCIES = ["ble_client"]
|
||||||
@@ -15,9 +15,9 @@ Am43Component = am43_ns.class_(
|
|||||||
)
|
)
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
cover.cover_schema(Am43Component)
|
cover.COVER_SCHEMA.extend(
|
||||||
.extend(
|
|
||||||
{
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(Am43Component),
|
||||||
cv.Optional(CONF_PIN, default=8888): cv.int_range(min=0, max=0xFFFF),
|
cv.Optional(CONF_PIN, default=8888): cv.int_range(min=0, max=0xFFFF),
|
||||||
cv.Optional(CONF_INVERT_POSITION, default=False): cv.boolean,
|
cv.Optional(CONF_INVERT_POSITION, default=False): cv.boolean,
|
||||||
}
|
}
|
||||||
@@ -28,8 +28,9 @@ CONFIG_SCHEMA = (
|
|||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = await cover.new_cover(config)
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
cg.add(var.set_pin(config[CONF_PIN]))
|
cg.add(var.set_pin(config[CONF_PIN]))
|
||||||
cg.add(var.set_invert_position(config[CONF_INVERT_POSITION]))
|
cg.add(var.set_invert_position(config[CONF_INVERT_POSITION]))
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
await cover.register_cover(var, config)
|
||||||
await ble_client.register_ble_node(var, config)
|
await ble_client.register_ble_node(var, config)
|
||||||
|
|||||||
@@ -12,10 +12,8 @@ using namespace esphome::cover;
|
|||||||
|
|
||||||
void Am43Component::dump_config() {
|
void Am43Component::dump_config() {
|
||||||
LOG_COVER("", "AM43 Cover", this);
|
LOG_COVER("", "AM43 Cover", this);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Device Pin: %d", this->pin_);
|
||||||
" Device Pin: %d\n"
|
ESP_LOGCONFIG(TAG, " Invert Position: %d", (int) this->invert_position_);
|
||||||
" Invert Position: %d",
|
|
||||||
this->pin_, (int) this->invert_position_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Am43Component::setup() {
|
void Am43Component::setup() {
|
||||||
|
|||||||
@@ -34,10 +34,8 @@ void AnalogThresholdBinarySensor::set_sensor(sensor::Sensor *analog_sensor) {
|
|||||||
void AnalogThresholdBinarySensor::dump_config() {
|
void AnalogThresholdBinarySensor::dump_config() {
|
||||||
LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this);
|
LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this);
|
||||||
LOG_SENSOR(" ", "Sensor", this->sensor_);
|
LOG_SENSOR(" ", "Sensor", this->sensor_);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Upper threshold: %.11f", this->upper_threshold_.value());
|
||||||
" Upper threshold: %.11f\n"
|
ESP_LOGCONFIG(TAG, " Lower threshold: %.11f", this->lower_threshold_.value());
|
||||||
" Lower threshold: %.11f",
|
|
||||||
this->upper_threshold_.value(), this->lower_threshold_.value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace analog_threshold
|
} // namespace analog_threshold
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import ble_client, climate
|
from esphome.components import ble_client, climate
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_UNIT_OF_MEASUREMENT
|
from esphome.const import CONF_ID, CONF_UNIT_OF_MEASUREMENT
|
||||||
|
|
||||||
UNITS = {
|
UNITS = {
|
||||||
"f": "f",
|
"f": "f",
|
||||||
@@ -17,9 +17,9 @@ Anova = anova_ns.class_(
|
|||||||
)
|
)
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
climate.climate_schema(Anova)
|
climate.CLIMATE_SCHEMA.extend(
|
||||||
.extend(
|
|
||||||
{
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(Anova),
|
||||||
cv.Required(CONF_UNIT_OF_MEASUREMENT): cv.enum(UNITS),
|
cv.Required(CONF_UNIT_OF_MEASUREMENT): cv.enum(UNITS),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -29,7 +29,8 @@ CONFIG_SCHEMA = (
|
|||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = await climate.new_climate(config)
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
await climate.register_climate(var, config)
|
||||||
await ble_client.register_ble_node(var, config)
|
await ble_client.register_ble_node(var, config)
|
||||||
cg.add(var.set_unit_of_measurement(config[CONF_UNIT_OF_MEASUREMENT]))
|
cg.add(var.set_unit_of_measurement(config[CONF_UNIT_OF_MEASUREMENT]))
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ enum { // APDS9306 registers
|
|||||||
}
|
}
|
||||||
|
|
||||||
void APDS9306::setup() {
|
void APDS9306::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up APDS9306...");
|
||||||
|
|
||||||
uint8_t id;
|
uint8_t id;
|
||||||
if (!this->read_byte(APDS9306_PART_ID, &id)) { // Part ID register
|
if (!this->read_byte(APDS9306_PART_ID, &id)) { // Part ID register
|
||||||
@@ -97,7 +97,7 @@ void APDS9306::dump_config() {
|
|||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
switch (this->error_code_) {
|
switch (this->error_code_) {
|
||||||
case COMMUNICATION_FAILED:
|
case COMMUNICATION_FAILED:
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with APDS9306 failed!");
|
||||||
break;
|
break;
|
||||||
case WRONG_ID:
|
case WRONG_ID:
|
||||||
ESP_LOGE(TAG, "APDS9306 has invalid id!");
|
ESP_LOGE(TAG, "APDS9306 has invalid id!");
|
||||||
@@ -108,12 +108,9 @@ void APDS9306::dump_config() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Gain: %u", AMBIENT_LIGHT_GAIN_VALUES[this->gain_]);
|
||||||
" Gain: %u\n"
|
ESP_LOGCONFIG(TAG, " Measurement rate: %u", MEASUREMENT_RATE_VALUES[this->measurement_rate_]);
|
||||||
" Measurement rate: %u\n"
|
ESP_LOGCONFIG(TAG, " Measurement Resolution/Bit width: %d", MEASUREMENT_BIT_WIDTH_VALUES[this->bit_width_]);
|
||||||
" Measurement Resolution/Bit width: %d",
|
|
||||||
AMBIENT_LIGHT_GAIN_VALUES[this->gain_], MEASUREMENT_RATE_VALUES[this->measurement_rate_],
|
|
||||||
MEASUREMENT_BIT_WIDTH_VALUES[this->bit_width_]);
|
|
||||||
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ static const char *const TAG = "apds9960";
|
|||||||
#define APDS9960_WRITE_BYTE(reg, value) APDS9960_ERROR_CHECK(this->write_byte(reg, value));
|
#define APDS9960_WRITE_BYTE(reg, value) APDS9960_ERROR_CHECK(this->write_byte(reg, value));
|
||||||
|
|
||||||
void APDS9960::setup() {
|
void APDS9960::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up APDS9960...");
|
||||||
uint8_t id;
|
uint8_t id;
|
||||||
if (!this->read_byte(0x92, &id)) { // ID register
|
if (!this->read_byte(0x92, &id)) { // ID register
|
||||||
this->error_code_ = COMMUNICATION_FAILED;
|
this->error_code_ = COMMUNICATION_FAILED;
|
||||||
@@ -141,7 +141,7 @@ void APDS9960::dump_config() {
|
|||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
switch (this->error_code_) {
|
switch (this->error_code_) {
|
||||||
case COMMUNICATION_FAILED:
|
case COMMUNICATION_FAILED:
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with APDS9960 failed!");
|
||||||
break;
|
break;
|
||||||
case WRONG_ID:
|
case WRONG_ID:
|
||||||
ESP_LOGE(TAG, "APDS9960 has invalid id!");
|
ESP_LOGE(TAG, "APDS9960 has invalid id!");
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ SERVICE_ARG_NATIVE_TYPES = {
|
|||||||
"string[]": cg.std_vector.template(cg.std_string),
|
"string[]": cg.std_vector.template(cg.std_string),
|
||||||
}
|
}
|
||||||
CONF_ENCRYPTION = "encryption"
|
CONF_ENCRYPTION = "encryption"
|
||||||
CONF_BATCH_DELAY = "batch_delay"
|
|
||||||
|
|
||||||
|
|
||||||
def validate_encryption_key(value):
|
def validate_encryption_key(value):
|
||||||
@@ -110,9 +109,6 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
): ACTIONS_SCHEMA,
|
): ACTIONS_SCHEMA,
|
||||||
cv.Exclusive(CONF_ACTIONS, group_of_exclusion=CONF_ACTIONS): ACTIONS_SCHEMA,
|
cv.Exclusive(CONF_ACTIONS, group_of_exclusion=CONF_ACTIONS): ACTIONS_SCHEMA,
|
||||||
cv.Optional(CONF_ENCRYPTION): _encryption_schema,
|
cv.Optional(CONF_ENCRYPTION): _encryption_schema,
|
||||||
cv.Optional(
|
|
||||||
CONF_BATCH_DELAY, default="100ms"
|
|
||||||
): cv.positive_time_period_milliseconds,
|
|
||||||
cv.Optional(CONF_ON_CLIENT_CONNECTED): automation.validate_automation(
|
cv.Optional(CONF_ON_CLIENT_CONNECTED): automation.validate_automation(
|
||||||
single=True
|
single=True
|
||||||
),
|
),
|
||||||
@@ -133,7 +129,6 @@ async def to_code(config):
|
|||||||
cg.add(var.set_port(config[CONF_PORT]))
|
cg.add(var.set_port(config[CONF_PORT]))
|
||||||
cg.add(var.set_password(config[CONF_PASSWORD]))
|
cg.add(var.set_password(config[CONF_PASSWORD]))
|
||||||
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
|
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
|
||||||
cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY]))
|
|
||||||
|
|
||||||
for conf in config.get(CONF_ACTIONS, []):
|
for conf in config.get(CONF_ACTIONS, []):
|
||||||
template_args = []
|
template_args = []
|
||||||
|
|||||||
@@ -33,24 +33,23 @@ service APIConnection {
|
|||||||
rpc execute_service (ExecuteServiceRequest) returns (void) {}
|
rpc execute_service (ExecuteServiceRequest) returns (void) {}
|
||||||
rpc noise_encryption_set_key (NoiseEncryptionSetKeyRequest) returns (NoiseEncryptionSetKeyResponse) {}
|
rpc noise_encryption_set_key (NoiseEncryptionSetKeyRequest) returns (NoiseEncryptionSetKeyResponse) {}
|
||||||
|
|
||||||
rpc button_command (ButtonCommandRequest) returns (void) {}
|
|
||||||
rpc camera_image (CameraImageRequest) returns (void) {}
|
|
||||||
rpc climate_command (ClimateCommandRequest) returns (void) {}
|
|
||||||
rpc cover_command (CoverCommandRequest) returns (void) {}
|
rpc cover_command (CoverCommandRequest) returns (void) {}
|
||||||
rpc date_command (DateCommandRequest) returns (void) {}
|
|
||||||
rpc datetime_command (DateTimeCommandRequest) returns (void) {}
|
|
||||||
rpc fan_command (FanCommandRequest) returns (void) {}
|
rpc fan_command (FanCommandRequest) returns (void) {}
|
||||||
rpc light_command (LightCommandRequest) returns (void) {}
|
rpc light_command (LightCommandRequest) returns (void) {}
|
||||||
rpc lock_command (LockCommandRequest) returns (void) {}
|
|
||||||
rpc media_player_command (MediaPlayerCommandRequest) returns (void) {}
|
|
||||||
rpc number_command (NumberCommandRequest) returns (void) {}
|
|
||||||
rpc select_command (SelectCommandRequest) returns (void) {}
|
|
||||||
rpc siren_command (SirenCommandRequest) returns (void) {}
|
|
||||||
rpc switch_command (SwitchCommandRequest) returns (void) {}
|
rpc switch_command (SwitchCommandRequest) returns (void) {}
|
||||||
|
rpc camera_image (CameraImageRequest) returns (void) {}
|
||||||
|
rpc climate_command (ClimateCommandRequest) returns (void) {}
|
||||||
|
rpc number_command (NumberCommandRequest) returns (void) {}
|
||||||
rpc text_command (TextCommandRequest) returns (void) {}
|
rpc text_command (TextCommandRequest) returns (void) {}
|
||||||
rpc time_command (TimeCommandRequest) returns (void) {}
|
rpc select_command (SelectCommandRequest) returns (void) {}
|
||||||
rpc update_command (UpdateCommandRequest) returns (void) {}
|
rpc button_command (ButtonCommandRequest) returns (void) {}
|
||||||
|
rpc lock_command (LockCommandRequest) returns (void) {}
|
||||||
rpc valve_command (ValveCommandRequest) returns (void) {}
|
rpc valve_command (ValveCommandRequest) returns (void) {}
|
||||||
|
rpc media_player_command (MediaPlayerCommandRequest) returns (void) {}
|
||||||
|
rpc date_command (DateCommandRequest) returns (void) {}
|
||||||
|
rpc time_command (TimeCommandRequest) returns (void) {}
|
||||||
|
rpc datetime_command (DateTimeCommandRequest) returns (void) {}
|
||||||
|
rpc update_command (UpdateCommandRequest) returns (void) {}
|
||||||
|
|
||||||
rpc subscribe_bluetooth_le_advertisements(SubscribeBluetoothLEAdvertisementsRequest) returns (void) {}
|
rpc subscribe_bluetooth_le_advertisements(SubscribeBluetoothLEAdvertisementsRequest) returns (void) {}
|
||||||
rpc bluetooth_device_request(BluetoothDeviceRequest) returns (void) {}
|
rpc bluetooth_device_request(BluetoothDeviceRequest) returns (void) {}
|
||||||
@@ -266,7 +265,6 @@ enum EntityCategory {
|
|||||||
// ==================== BINARY SENSOR ====================
|
// ==================== BINARY SENSOR ====================
|
||||||
message ListEntitiesBinarySensorResponse {
|
message ListEntitiesBinarySensorResponse {
|
||||||
option (id) = 12;
|
option (id) = 12;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_BINARY_SENSOR";
|
option (ifdef) = "USE_BINARY_SENSOR";
|
||||||
|
|
||||||
@@ -283,7 +281,6 @@ message ListEntitiesBinarySensorResponse {
|
|||||||
}
|
}
|
||||||
message BinarySensorStateResponse {
|
message BinarySensorStateResponse {
|
||||||
option (id) = 21;
|
option (id) = 21;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_BINARY_SENSOR";
|
option (ifdef) = "USE_BINARY_SENSOR";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -298,7 +295,6 @@ message BinarySensorStateResponse {
|
|||||||
// ==================== COVER ====================
|
// ==================== COVER ====================
|
||||||
message ListEntitiesCoverResponse {
|
message ListEntitiesCoverResponse {
|
||||||
option (id) = 13;
|
option (id) = 13;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_COVER";
|
option (ifdef) = "USE_COVER";
|
||||||
|
|
||||||
@@ -328,7 +324,6 @@ enum CoverOperation {
|
|||||||
}
|
}
|
||||||
message CoverStateResponse {
|
message CoverStateResponse {
|
||||||
option (id) = 22;
|
option (id) = 22;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_COVER";
|
option (ifdef) = "USE_COVER";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -371,7 +366,6 @@ message CoverCommandRequest {
|
|||||||
// ==================== FAN ====================
|
// ==================== FAN ====================
|
||||||
message ListEntitiesFanResponse {
|
message ListEntitiesFanResponse {
|
||||||
option (id) = 14;
|
option (id) = 14;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_FAN";
|
option (ifdef) = "USE_FAN";
|
||||||
|
|
||||||
@@ -400,7 +394,6 @@ enum FanDirection {
|
|||||||
}
|
}
|
||||||
message FanStateResponse {
|
message FanStateResponse {
|
||||||
option (id) = 23;
|
option (id) = 23;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_FAN";
|
option (ifdef) = "USE_FAN";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -438,8 +431,7 @@ message FanCommandRequest {
|
|||||||
enum ColorMode {
|
enum ColorMode {
|
||||||
COLOR_MODE_UNKNOWN = 0;
|
COLOR_MODE_UNKNOWN = 0;
|
||||||
COLOR_MODE_ON_OFF = 1;
|
COLOR_MODE_ON_OFF = 1;
|
||||||
COLOR_MODE_LEGACY_BRIGHTNESS = 2;
|
COLOR_MODE_BRIGHTNESS = 2;
|
||||||
COLOR_MODE_BRIGHTNESS = 3;
|
|
||||||
COLOR_MODE_WHITE = 7;
|
COLOR_MODE_WHITE = 7;
|
||||||
COLOR_MODE_COLOR_TEMPERATURE = 11;
|
COLOR_MODE_COLOR_TEMPERATURE = 11;
|
||||||
COLOR_MODE_COLD_WARM_WHITE = 19;
|
COLOR_MODE_COLD_WARM_WHITE = 19;
|
||||||
@@ -450,7 +442,6 @@ enum ColorMode {
|
|||||||
}
|
}
|
||||||
message ListEntitiesLightResponse {
|
message ListEntitiesLightResponse {
|
||||||
option (id) = 15;
|
option (id) = 15;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_LIGHT";
|
option (ifdef) = "USE_LIGHT";
|
||||||
|
|
||||||
@@ -474,7 +465,6 @@ message ListEntitiesLightResponse {
|
|||||||
}
|
}
|
||||||
message LightStateResponse {
|
message LightStateResponse {
|
||||||
option (id) = 24;
|
option (id) = 24;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_LIGHT";
|
option (ifdef) = "USE_LIGHT";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -544,7 +534,6 @@ enum SensorLastResetType {
|
|||||||
|
|
||||||
message ListEntitiesSensorResponse {
|
message ListEntitiesSensorResponse {
|
||||||
option (id) = 16;
|
option (id) = 16;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_SENSOR";
|
option (ifdef) = "USE_SENSOR";
|
||||||
|
|
||||||
@@ -566,7 +555,6 @@ message ListEntitiesSensorResponse {
|
|||||||
}
|
}
|
||||||
message SensorStateResponse {
|
message SensorStateResponse {
|
||||||
option (id) = 25;
|
option (id) = 25;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_SENSOR";
|
option (ifdef) = "USE_SENSOR";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -581,7 +569,6 @@ message SensorStateResponse {
|
|||||||
// ==================== SWITCH ====================
|
// ==================== SWITCH ====================
|
||||||
message ListEntitiesSwitchResponse {
|
message ListEntitiesSwitchResponse {
|
||||||
option (id) = 17;
|
option (id) = 17;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_SWITCH";
|
option (ifdef) = "USE_SWITCH";
|
||||||
|
|
||||||
@@ -598,7 +585,6 @@ message ListEntitiesSwitchResponse {
|
|||||||
}
|
}
|
||||||
message SwitchStateResponse {
|
message SwitchStateResponse {
|
||||||
option (id) = 26;
|
option (id) = 26;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_SWITCH";
|
option (ifdef) = "USE_SWITCH";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -619,7 +605,6 @@ message SwitchCommandRequest {
|
|||||||
// ==================== TEXT SENSOR ====================
|
// ==================== TEXT SENSOR ====================
|
||||||
message ListEntitiesTextSensorResponse {
|
message ListEntitiesTextSensorResponse {
|
||||||
option (id) = 18;
|
option (id) = 18;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_TEXT_SENSOR";
|
option (ifdef) = "USE_TEXT_SENSOR";
|
||||||
|
|
||||||
@@ -635,7 +620,6 @@ message ListEntitiesTextSensorResponse {
|
|||||||
}
|
}
|
||||||
message TextSensorStateResponse {
|
message TextSensorStateResponse {
|
||||||
option (id) = 27;
|
option (id) = 27;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_TEXT_SENSOR";
|
option (ifdef) = "USE_TEXT_SENSOR";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -671,7 +655,7 @@ message SubscribeLogsResponse {
|
|||||||
option (no_delay) = false;
|
option (no_delay) = false;
|
||||||
|
|
||||||
LogLevel level = 1;
|
LogLevel level = 1;
|
||||||
bytes message = 3;
|
string message = 3;
|
||||||
bool send_failed = 4;
|
bool send_failed = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -803,7 +787,6 @@ message ExecuteServiceRequest {
|
|||||||
// ==================== CAMERA ====================
|
// ==================== CAMERA ====================
|
||||||
message ListEntitiesCameraResponse {
|
message ListEntitiesCameraResponse {
|
||||||
option (id) = 43;
|
option (id) = 43;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_ESP32_CAMERA";
|
option (ifdef) = "USE_ESP32_CAMERA";
|
||||||
|
|
||||||
@@ -884,7 +867,6 @@ enum ClimatePreset {
|
|||||||
}
|
}
|
||||||
message ListEntitiesClimateResponse {
|
message ListEntitiesClimateResponse {
|
||||||
option (id) = 46;
|
option (id) = 46;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_CLIMATE";
|
option (ifdef) = "USE_CLIMATE";
|
||||||
|
|
||||||
@@ -919,7 +901,6 @@ message ListEntitiesClimateResponse {
|
|||||||
}
|
}
|
||||||
message ClimateStateResponse {
|
message ClimateStateResponse {
|
||||||
option (id) = 47;
|
option (id) = 47;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_CLIMATE";
|
option (ifdef) = "USE_CLIMATE";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -930,7 +911,6 @@ message ClimateStateResponse {
|
|||||||
float target_temperature = 4;
|
float target_temperature = 4;
|
||||||
float target_temperature_low = 5;
|
float target_temperature_low = 5;
|
||||||
float target_temperature_high = 6;
|
float target_temperature_high = 6;
|
||||||
// For older peers, equal to preset == CLIMATE_PRESET_AWAY
|
|
||||||
bool unused_legacy_away = 7;
|
bool unused_legacy_away = 7;
|
||||||
ClimateAction action = 8;
|
ClimateAction action = 8;
|
||||||
ClimateFanMode fan_mode = 9;
|
ClimateFanMode fan_mode = 9;
|
||||||
@@ -956,7 +936,6 @@ message ClimateCommandRequest {
|
|||||||
float target_temperature_low = 7;
|
float target_temperature_low = 7;
|
||||||
bool has_target_temperature_high = 8;
|
bool has_target_temperature_high = 8;
|
||||||
float target_temperature_high = 9;
|
float target_temperature_high = 9;
|
||||||
// legacy, for older peers, newer ones should use CLIMATE_PRESET_AWAY in preset
|
|
||||||
bool unused_has_legacy_away = 10;
|
bool unused_has_legacy_away = 10;
|
||||||
bool unused_legacy_away = 11;
|
bool unused_legacy_away = 11;
|
||||||
bool has_fan_mode = 12;
|
bool has_fan_mode = 12;
|
||||||
@@ -981,7 +960,6 @@ enum NumberMode {
|
|||||||
}
|
}
|
||||||
message ListEntitiesNumberResponse {
|
message ListEntitiesNumberResponse {
|
||||||
option (id) = 49;
|
option (id) = 49;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_NUMBER";
|
option (ifdef) = "USE_NUMBER";
|
||||||
|
|
||||||
@@ -1002,7 +980,6 @@ message ListEntitiesNumberResponse {
|
|||||||
}
|
}
|
||||||
message NumberStateResponse {
|
message NumberStateResponse {
|
||||||
option (id) = 50;
|
option (id) = 50;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_NUMBER";
|
option (ifdef) = "USE_NUMBER";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1026,7 +1003,6 @@ message NumberCommandRequest {
|
|||||||
// ==================== SELECT ====================
|
// ==================== SELECT ====================
|
||||||
message ListEntitiesSelectResponse {
|
message ListEntitiesSelectResponse {
|
||||||
option (id) = 52;
|
option (id) = 52;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_SELECT";
|
option (ifdef) = "USE_SELECT";
|
||||||
|
|
||||||
@@ -1042,7 +1018,6 @@ message ListEntitiesSelectResponse {
|
|||||||
}
|
}
|
||||||
message SelectStateResponse {
|
message SelectStateResponse {
|
||||||
option (id) = 53;
|
option (id) = 53;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_SELECT";
|
option (ifdef) = "USE_SELECT";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1063,51 +1038,6 @@ message SelectCommandRequest {
|
|||||||
string state = 2;
|
string state = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== SIREN ====================
|
|
||||||
message ListEntitiesSirenResponse {
|
|
||||||
option (id) = 55;
|
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
|
||||||
option (ifdef) = "USE_SIREN";
|
|
||||||
|
|
||||||
string object_id = 1;
|
|
||||||
fixed32 key = 2;
|
|
||||||
string name = 3;
|
|
||||||
string unique_id = 4;
|
|
||||||
|
|
||||||
string icon = 5;
|
|
||||||
bool disabled_by_default = 6;
|
|
||||||
repeated string tones = 7;
|
|
||||||
bool supports_duration = 8;
|
|
||||||
bool supports_volume = 9;
|
|
||||||
EntityCategory entity_category = 10;
|
|
||||||
}
|
|
||||||
message SirenStateResponse {
|
|
||||||
option (id) = 56;
|
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
|
||||||
option (ifdef) = "USE_SIREN";
|
|
||||||
option (no_delay) = true;
|
|
||||||
|
|
||||||
fixed32 key = 1;
|
|
||||||
bool state = 2;
|
|
||||||
}
|
|
||||||
message SirenCommandRequest {
|
|
||||||
option (id) = 57;
|
|
||||||
option (source) = SOURCE_CLIENT;
|
|
||||||
option (ifdef) = "USE_SIREN";
|
|
||||||
option (no_delay) = true;
|
|
||||||
|
|
||||||
fixed32 key = 1;
|
|
||||||
bool has_state = 2;
|
|
||||||
bool state = 3;
|
|
||||||
bool has_tone = 4;
|
|
||||||
string tone = 5;
|
|
||||||
bool has_duration = 6;
|
|
||||||
uint32 duration = 7;
|
|
||||||
bool has_volume = 8;
|
|
||||||
float volume = 9;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== LOCK ====================
|
// ==================== LOCK ====================
|
||||||
enum LockState {
|
enum LockState {
|
||||||
@@ -1125,7 +1055,6 @@ enum LockCommand {
|
|||||||
}
|
}
|
||||||
message ListEntitiesLockResponse {
|
message ListEntitiesLockResponse {
|
||||||
option (id) = 58;
|
option (id) = 58;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_LOCK";
|
option (ifdef) = "USE_LOCK";
|
||||||
|
|
||||||
@@ -1147,7 +1076,6 @@ message ListEntitiesLockResponse {
|
|||||||
}
|
}
|
||||||
message LockStateResponse {
|
message LockStateResponse {
|
||||||
option (id) = 59;
|
option (id) = 59;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_LOCK";
|
option (ifdef) = "USE_LOCK";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1170,7 +1098,6 @@ message LockCommandRequest {
|
|||||||
// ==================== BUTTON ====================
|
// ==================== BUTTON ====================
|
||||||
message ListEntitiesButtonResponse {
|
message ListEntitiesButtonResponse {
|
||||||
option (id) = 61;
|
option (id) = 61;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_BUTTON";
|
option (ifdef) = "USE_BUTTON";
|
||||||
|
|
||||||
@@ -1222,7 +1149,6 @@ message MediaPlayerSupportedFormat {
|
|||||||
}
|
}
|
||||||
message ListEntitiesMediaPlayerResponse {
|
message ListEntitiesMediaPlayerResponse {
|
||||||
option (id) = 63;
|
option (id) = 63;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_MEDIA_PLAYER";
|
option (ifdef) = "USE_MEDIA_PLAYER";
|
||||||
|
|
||||||
@@ -1241,7 +1167,6 @@ message ListEntitiesMediaPlayerResponse {
|
|||||||
}
|
}
|
||||||
message MediaPlayerStateResponse {
|
message MediaPlayerStateResponse {
|
||||||
option (id) = 64;
|
option (id) = 64;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_MEDIA_PLAYER";
|
option (ifdef) = "USE_MEDIA_PLAYER";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1282,8 +1207,8 @@ message SubscribeBluetoothLEAdvertisementsRequest {
|
|||||||
|
|
||||||
message BluetoothServiceData {
|
message BluetoothServiceData {
|
||||||
string uuid = 1;
|
string uuid = 1;
|
||||||
repeated uint32 legacy_data = 2 [deprecated = true]; // Removed in api version 1.7
|
repeated uint32 legacy_data = 2 [deprecated = true];
|
||||||
bytes data = 3; // Added in api version 1.7
|
bytes data = 3; // Changed in proto version 1.7
|
||||||
}
|
}
|
||||||
message BluetoothLEAdvertisementResponse {
|
message BluetoothLEAdvertisementResponse {
|
||||||
option (id) = 67;
|
option (id) = 67;
|
||||||
@@ -1292,7 +1217,7 @@ message BluetoothLEAdvertisementResponse {
|
|||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
|
|
||||||
uint64 address = 1;
|
uint64 address = 1;
|
||||||
bytes name = 2;
|
string name = 2;
|
||||||
sint32 rssi = 3;
|
sint32 rssi = 3;
|
||||||
|
|
||||||
repeated string service_uuids = 4;
|
repeated string service_uuids = 4;
|
||||||
@@ -1579,7 +1504,7 @@ message BluetoothScannerSetModeRequest {
|
|||||||
BluetoothScannerMode mode = 1;
|
BluetoothScannerMode mode = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== VOICE ASSISTANT ====================
|
// ==================== PUSH TO TALK ====================
|
||||||
enum VoiceAssistantSubscribeFlag {
|
enum VoiceAssistantSubscribeFlag {
|
||||||
VOICE_ASSISTANT_SUBSCRIBE_NONE = 0;
|
VOICE_ASSISTANT_SUBSCRIBE_NONE = 0;
|
||||||
VOICE_ASSISTANT_SUBSCRIBE_API_AUDIO = 1;
|
VOICE_ASSISTANT_SUBSCRIBE_API_AUDIO = 1;
|
||||||
@@ -1643,7 +1568,6 @@ enum VoiceAssistantEvent {
|
|||||||
VOICE_ASSISTANT_STT_VAD_END = 12;
|
VOICE_ASSISTANT_STT_VAD_END = 12;
|
||||||
VOICE_ASSISTANT_TTS_STREAM_START = 98;
|
VOICE_ASSISTANT_TTS_STREAM_START = 98;
|
||||||
VOICE_ASSISTANT_TTS_STREAM_END = 99;
|
VOICE_ASSISTANT_TTS_STREAM_END = 99;
|
||||||
VOICE_ASSISTANT_INTENT_PROGRESS = 100;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message VoiceAssistantEventData {
|
message VoiceAssistantEventData {
|
||||||
@@ -1764,7 +1688,6 @@ enum AlarmControlPanelStateCommand {
|
|||||||
|
|
||||||
message ListEntitiesAlarmControlPanelResponse {
|
message ListEntitiesAlarmControlPanelResponse {
|
||||||
option (id) = 94;
|
option (id) = 94;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_ALARM_CONTROL_PANEL";
|
option (ifdef) = "USE_ALARM_CONTROL_PANEL";
|
||||||
|
|
||||||
@@ -1782,7 +1705,6 @@ message ListEntitiesAlarmControlPanelResponse {
|
|||||||
|
|
||||||
message AlarmControlPanelStateResponse {
|
message AlarmControlPanelStateResponse {
|
||||||
option (id) = 95;
|
option (id) = 95;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_ALARM_CONTROL_PANEL";
|
option (ifdef) = "USE_ALARM_CONTROL_PANEL";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1807,7 +1729,6 @@ enum TextMode {
|
|||||||
}
|
}
|
||||||
message ListEntitiesTextResponse {
|
message ListEntitiesTextResponse {
|
||||||
option (id) = 97;
|
option (id) = 97;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_TEXT";
|
option (ifdef) = "USE_TEXT";
|
||||||
|
|
||||||
@@ -1826,7 +1747,6 @@ message ListEntitiesTextResponse {
|
|||||||
}
|
}
|
||||||
message TextStateResponse {
|
message TextStateResponse {
|
||||||
option (id) = 98;
|
option (id) = 98;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_TEXT";
|
option (ifdef) = "USE_TEXT";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1851,7 +1771,6 @@ message TextCommandRequest {
|
|||||||
// ==================== DATETIME DATE ====================
|
// ==================== DATETIME DATE ====================
|
||||||
message ListEntitiesDateResponse {
|
message ListEntitiesDateResponse {
|
||||||
option (id) = 100;
|
option (id) = 100;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_DATETIME_DATE";
|
option (ifdef) = "USE_DATETIME_DATE";
|
||||||
|
|
||||||
@@ -1866,7 +1785,6 @@ message ListEntitiesDateResponse {
|
|||||||
}
|
}
|
||||||
message DateStateResponse {
|
message DateStateResponse {
|
||||||
option (id) = 101;
|
option (id) = 101;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_DATETIME_DATE";
|
option (ifdef) = "USE_DATETIME_DATE";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1894,7 +1812,6 @@ message DateCommandRequest {
|
|||||||
// ==================== DATETIME TIME ====================
|
// ==================== DATETIME TIME ====================
|
||||||
message ListEntitiesTimeResponse {
|
message ListEntitiesTimeResponse {
|
||||||
option (id) = 103;
|
option (id) = 103;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_DATETIME_TIME";
|
option (ifdef) = "USE_DATETIME_TIME";
|
||||||
|
|
||||||
@@ -1909,7 +1826,6 @@ message ListEntitiesTimeResponse {
|
|||||||
}
|
}
|
||||||
message TimeStateResponse {
|
message TimeStateResponse {
|
||||||
option (id) = 104;
|
option (id) = 104;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_DATETIME_TIME";
|
option (ifdef) = "USE_DATETIME_TIME";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -1937,7 +1853,6 @@ message TimeCommandRequest {
|
|||||||
// ==================== EVENT ====================
|
// ==================== EVENT ====================
|
||||||
message ListEntitiesEventResponse {
|
message ListEntitiesEventResponse {
|
||||||
option (id) = 107;
|
option (id) = 107;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_EVENT";
|
option (ifdef) = "USE_EVENT";
|
||||||
|
|
||||||
@@ -1955,7 +1870,6 @@ message ListEntitiesEventResponse {
|
|||||||
}
|
}
|
||||||
message EventResponse {
|
message EventResponse {
|
||||||
option (id) = 108;
|
option (id) = 108;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_EVENT";
|
option (ifdef) = "USE_EVENT";
|
||||||
|
|
||||||
@@ -1966,7 +1880,6 @@ message EventResponse {
|
|||||||
// ==================== VALVE ====================
|
// ==================== VALVE ====================
|
||||||
message ListEntitiesValveResponse {
|
message ListEntitiesValveResponse {
|
||||||
option (id) = 109;
|
option (id) = 109;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_VALVE";
|
option (ifdef) = "USE_VALVE";
|
||||||
|
|
||||||
@@ -1992,7 +1905,6 @@ enum ValveOperation {
|
|||||||
}
|
}
|
||||||
message ValveStateResponse {
|
message ValveStateResponse {
|
||||||
option (id) = 110;
|
option (id) = 110;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_VALVE";
|
option (ifdef) = "USE_VALVE";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -2017,7 +1929,6 @@ message ValveCommandRequest {
|
|||||||
// ==================== DATETIME DATETIME ====================
|
// ==================== DATETIME DATETIME ====================
|
||||||
message ListEntitiesDateTimeResponse {
|
message ListEntitiesDateTimeResponse {
|
||||||
option (id) = 112;
|
option (id) = 112;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_DATETIME_DATETIME";
|
option (ifdef) = "USE_DATETIME_DATETIME";
|
||||||
|
|
||||||
@@ -2032,7 +1943,6 @@ message ListEntitiesDateTimeResponse {
|
|||||||
}
|
}
|
||||||
message DateTimeStateResponse {
|
message DateTimeStateResponse {
|
||||||
option (id) = 113;
|
option (id) = 113;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_DATETIME_DATETIME";
|
option (ifdef) = "USE_DATETIME_DATETIME";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
@@ -2056,7 +1966,6 @@ message DateTimeCommandRequest {
|
|||||||
// ==================== UPDATE ====================
|
// ==================== UPDATE ====================
|
||||||
message ListEntitiesUpdateResponse {
|
message ListEntitiesUpdateResponse {
|
||||||
option (id) = 116;
|
option (id) = 116;
|
||||||
option (base_class) = "InfoResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_UPDATE";
|
option (ifdef) = "USE_UPDATE";
|
||||||
|
|
||||||
@@ -2072,7 +1981,6 @@ message ListEntitiesUpdateResponse {
|
|||||||
}
|
}
|
||||||
message UpdateStateResponse {
|
message UpdateStateResponse {
|
||||||
option (id) = 117;
|
option (id) = 117;
|
||||||
option (base_class) = "StateResponseProtoMessage";
|
|
||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
option (ifdef) = "USE_UPDATE";
|
option (ifdef) = "USE_UPDATE";
|
||||||
option (no_delay) = true;
|
option (no_delay) = true;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -8,20 +8,54 @@
|
|||||||
#include "api_server.h"
|
#include "api_server.h"
|
||||||
#include "esphome/core/application.h"
|
#include "esphome/core/application.h"
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/entity_base.h"
|
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
// Keepalive timeout in milliseconds
|
using send_message_t = bool(APIConnection *, void *);
|
||||||
static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
|
|
||||||
|
/*
|
||||||
|
This class holds a pointer to the source component that wants to publish a message, and a pointer to a function that
|
||||||
|
will lazily publish that message. The two pointers allow dedup in the deferred queue if multiple publishes for the
|
||||||
|
same component are backed up, and take up only 8 bytes of memory. The entry in the deferred queue (a std::vector) is
|
||||||
|
the DeferredMessage instance itself (not a pointer to one elsewhere in heap) so still only 8 bytes per entry. Even
|
||||||
|
100 backed up messages (you'd have to have at least 100 sensors publishing because of dedup) would take up only 0.8
|
||||||
|
kB.
|
||||||
|
*/
|
||||||
|
class DeferredMessageQueue {
|
||||||
|
struct DeferredMessage {
|
||||||
|
friend class DeferredMessageQueue;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void *source_;
|
||||||
|
send_message_t *send_message_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DeferredMessage(void *source, send_message_t *send_message) : source_(source), send_message_(send_message) {}
|
||||||
|
bool operator==(const DeferredMessage &test) const {
|
||||||
|
return (source_ == test.source_ && send_message_ == test.send_message_);
|
||||||
|
}
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// vector is used very specifically for its zero memory overhead even though items are popped from the front (memory
|
||||||
|
// footprint is more important than speed here)
|
||||||
|
std::vector<DeferredMessage> deferred_queue_;
|
||||||
|
APIConnection *api_connection_;
|
||||||
|
|
||||||
|
// helper for allowing only unique entries in the queue
|
||||||
|
void dmq_push_back_with_dedup_(void *source, send_message_t *send_message);
|
||||||
|
|
||||||
|
public:
|
||||||
|
DeferredMessageQueue(APIConnection *api_connection) : api_connection_(api_connection) {}
|
||||||
|
void process_queue();
|
||||||
|
void defer(void *source, send_message_t *send_message);
|
||||||
|
};
|
||||||
|
|
||||||
class APIConnection : public APIServerConnection {
|
class APIConnection : public APIServerConnection {
|
||||||
public:
|
public:
|
||||||
friend class APIServer;
|
|
||||||
APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
|
APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
|
||||||
virtual ~APIConnection();
|
virtual ~APIConnection();
|
||||||
|
|
||||||
@@ -29,105 +63,149 @@ class APIConnection : public APIServerConnection {
|
|||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
bool send_list_info_done() {
|
bool send_list_info_done() {
|
||||||
return this->schedule_message_(nullptr, &APIConnection::try_send_list_info_done,
|
ListEntitiesDoneResponse resp;
|
||||||
ListEntitiesDoneResponse::MESSAGE_TYPE);
|
return this->send_list_entities_done_response(resp);
|
||||||
}
|
}
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor);
|
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state);
|
||||||
void send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor);
|
void send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor);
|
||||||
|
static bool try_send_binary_sensor_state(APIConnection *api, void *v_binary_sensor);
|
||||||
|
static bool try_send_binary_sensor_state(APIConnection *api, binary_sensor::BinarySensor *binary_sensor, bool state);
|
||||||
|
static bool try_send_binary_sensor_info(APIConnection *api, void *v_binary_sensor);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_COVER
|
#ifdef USE_COVER
|
||||||
bool send_cover_state(cover::Cover *cover);
|
bool send_cover_state(cover::Cover *cover);
|
||||||
void send_cover_info(cover::Cover *cover);
|
void send_cover_info(cover::Cover *cover);
|
||||||
|
static bool try_send_cover_state(APIConnection *api, void *v_cover);
|
||||||
|
static bool try_send_cover_info(APIConnection *api, void *v_cover);
|
||||||
void cover_command(const CoverCommandRequest &msg) override;
|
void cover_command(const CoverCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_FAN
|
#ifdef USE_FAN
|
||||||
bool send_fan_state(fan::Fan *fan);
|
bool send_fan_state(fan::Fan *fan);
|
||||||
void send_fan_info(fan::Fan *fan);
|
void send_fan_info(fan::Fan *fan);
|
||||||
|
static bool try_send_fan_state(APIConnection *api, void *v_fan);
|
||||||
|
static bool try_send_fan_info(APIConnection *api, void *v_fan);
|
||||||
void fan_command(const FanCommandRequest &msg) override;
|
void fan_command(const FanCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LIGHT
|
#ifdef USE_LIGHT
|
||||||
bool send_light_state(light::LightState *light);
|
bool send_light_state(light::LightState *light);
|
||||||
void send_light_info(light::LightState *light);
|
void send_light_info(light::LightState *light);
|
||||||
|
static bool try_send_light_state(APIConnection *api, void *v_light);
|
||||||
|
static bool try_send_light_info(APIConnection *api, void *v_light);
|
||||||
void light_command(const LightCommandRequest &msg) override;
|
void light_command(const LightCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
bool send_sensor_state(sensor::Sensor *sensor);
|
bool send_sensor_state(sensor::Sensor *sensor, float state);
|
||||||
void send_sensor_info(sensor::Sensor *sensor);
|
void send_sensor_info(sensor::Sensor *sensor);
|
||||||
|
static bool try_send_sensor_state(APIConnection *api, void *v_sensor);
|
||||||
|
static bool try_send_sensor_state(APIConnection *api, sensor::Sensor *sensor, float state);
|
||||||
|
static bool try_send_sensor_info(APIConnection *api, void *v_sensor);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SWITCH
|
#ifdef USE_SWITCH
|
||||||
bool send_switch_state(switch_::Switch *a_switch);
|
bool send_switch_state(switch_::Switch *a_switch, bool state);
|
||||||
void send_switch_info(switch_::Switch *a_switch);
|
void send_switch_info(switch_::Switch *a_switch);
|
||||||
|
static bool try_send_switch_state(APIConnection *api, void *v_a_switch);
|
||||||
|
static bool try_send_switch_state(APIConnection *api, switch_::Switch *a_switch, bool state);
|
||||||
|
static bool try_send_switch_info(APIConnection *api, void *v_a_switch);
|
||||||
void switch_command(const SwitchCommandRequest &msg) override;
|
void switch_command(const SwitchCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor);
|
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state);
|
||||||
void send_text_sensor_info(text_sensor::TextSensor *text_sensor);
|
void send_text_sensor_info(text_sensor::TextSensor *text_sensor);
|
||||||
|
static bool try_send_text_sensor_state(APIConnection *api, void *v_text_sensor);
|
||||||
|
static bool try_send_text_sensor_state(APIConnection *api, text_sensor::TextSensor *text_sensor, std::string state);
|
||||||
|
static bool try_send_text_sensor_info(APIConnection *api, void *v_text_sensor);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_ESP32_CAMERA
|
#ifdef USE_ESP32_CAMERA
|
||||||
void set_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
|
void set_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
|
||||||
void send_camera_info(esp32_camera::ESP32Camera *camera);
|
void send_camera_info(esp32_camera::ESP32Camera *camera);
|
||||||
|
static bool try_send_camera_info(APIConnection *api, void *v_camera);
|
||||||
void camera_image(const CameraImageRequest &msg) override;
|
void camera_image(const CameraImageRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_CLIMATE
|
#ifdef USE_CLIMATE
|
||||||
bool send_climate_state(climate::Climate *climate);
|
bool send_climate_state(climate::Climate *climate);
|
||||||
void send_climate_info(climate::Climate *climate);
|
void send_climate_info(climate::Climate *climate);
|
||||||
|
static bool try_send_climate_state(APIConnection *api, void *v_climate);
|
||||||
|
static bool try_send_climate_info(APIConnection *api, void *v_climate);
|
||||||
void climate_command(const ClimateCommandRequest &msg) override;
|
void climate_command(const ClimateCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_NUMBER
|
#ifdef USE_NUMBER
|
||||||
bool send_number_state(number::Number *number);
|
bool send_number_state(number::Number *number, float state);
|
||||||
void send_number_info(number::Number *number);
|
void send_number_info(number::Number *number);
|
||||||
|
static bool try_send_number_state(APIConnection *api, void *v_number);
|
||||||
|
static bool try_send_number_state(APIConnection *api, number::Number *number, float state);
|
||||||
|
static bool try_send_number_info(APIConnection *api, void *v_number);
|
||||||
void number_command(const NumberCommandRequest &msg) override;
|
void number_command(const NumberCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_DATE
|
#ifdef USE_DATETIME_DATE
|
||||||
bool send_date_state(datetime::DateEntity *date);
|
bool send_date_state(datetime::DateEntity *date);
|
||||||
void send_date_info(datetime::DateEntity *date);
|
void send_date_info(datetime::DateEntity *date);
|
||||||
|
static bool try_send_date_state(APIConnection *api, void *v_date);
|
||||||
|
static bool try_send_date_info(APIConnection *api, void *v_date);
|
||||||
void date_command(const DateCommandRequest &msg) override;
|
void date_command(const DateCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_TIME
|
#ifdef USE_DATETIME_TIME
|
||||||
bool send_time_state(datetime::TimeEntity *time);
|
bool send_time_state(datetime::TimeEntity *time);
|
||||||
void send_time_info(datetime::TimeEntity *time);
|
void send_time_info(datetime::TimeEntity *time);
|
||||||
|
static bool try_send_time_state(APIConnection *api, void *v_time);
|
||||||
|
static bool try_send_time_info(APIConnection *api, void *v_time);
|
||||||
void time_command(const TimeCommandRequest &msg) override;
|
void time_command(const TimeCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_DATETIME
|
#ifdef USE_DATETIME_DATETIME
|
||||||
bool send_datetime_state(datetime::DateTimeEntity *datetime);
|
bool send_datetime_state(datetime::DateTimeEntity *datetime);
|
||||||
void send_datetime_info(datetime::DateTimeEntity *datetime);
|
void send_datetime_info(datetime::DateTimeEntity *datetime);
|
||||||
|
static bool try_send_datetime_state(APIConnection *api, void *v_datetime);
|
||||||
|
static bool try_send_datetime_info(APIConnection *api, void *v_datetime);
|
||||||
void datetime_command(const DateTimeCommandRequest &msg) override;
|
void datetime_command(const DateTimeCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT
|
#ifdef USE_TEXT
|
||||||
bool send_text_state(text::Text *text);
|
bool send_text_state(text::Text *text, std::string state);
|
||||||
void send_text_info(text::Text *text);
|
void send_text_info(text::Text *text);
|
||||||
|
static bool try_send_text_state(APIConnection *api, void *v_text);
|
||||||
|
static bool try_send_text_state(APIConnection *api, text::Text *text, std::string state);
|
||||||
|
static bool try_send_text_info(APIConnection *api, void *v_text);
|
||||||
void text_command(const TextCommandRequest &msg) override;
|
void text_command(const TextCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
bool send_select_state(select::Select *select);
|
bool send_select_state(select::Select *select, std::string state);
|
||||||
void send_select_info(select::Select *select);
|
void send_select_info(select::Select *select);
|
||||||
|
static bool try_send_select_state(APIConnection *api, void *v_select);
|
||||||
|
static bool try_send_select_state(APIConnection *api, select::Select *select, std::string state);
|
||||||
|
static bool try_send_select_info(APIConnection *api, void *v_select);
|
||||||
void select_command(const SelectCommandRequest &msg) override;
|
void select_command(const SelectCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_BUTTON
|
#ifdef USE_BUTTON
|
||||||
void send_button_info(button::Button *button);
|
void send_button_info(button::Button *button);
|
||||||
|
static bool try_send_button_info(APIConnection *api, void *v_button);
|
||||||
void button_command(const ButtonCommandRequest &msg) override;
|
void button_command(const ButtonCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LOCK
|
#ifdef USE_LOCK
|
||||||
bool send_lock_state(lock::Lock *a_lock);
|
bool send_lock_state(lock::Lock *a_lock, lock::LockState state);
|
||||||
void send_lock_info(lock::Lock *a_lock);
|
void send_lock_info(lock::Lock *a_lock);
|
||||||
|
static bool try_send_lock_state(APIConnection *api, void *v_a_lock);
|
||||||
|
static bool try_send_lock_state(APIConnection *api, lock::Lock *a_lock, lock::LockState state);
|
||||||
|
static bool try_send_lock_info(APIConnection *api, void *v_a_lock);
|
||||||
void lock_command(const LockCommandRequest &msg) override;
|
void lock_command(const LockCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VALVE
|
#ifdef USE_VALVE
|
||||||
bool send_valve_state(valve::Valve *valve);
|
bool send_valve_state(valve::Valve *valve);
|
||||||
void send_valve_info(valve::Valve *valve);
|
void send_valve_info(valve::Valve *valve);
|
||||||
|
static bool try_send_valve_state(APIConnection *api, void *v_valve);
|
||||||
|
static bool try_send_valve_info(APIConnection *api, void *v_valve);
|
||||||
void valve_command(const ValveCommandRequest &msg) override;
|
void valve_command(const ValveCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_MEDIA_PLAYER
|
#ifdef USE_MEDIA_PLAYER
|
||||||
bool send_media_player_state(media_player::MediaPlayer *media_player);
|
bool send_media_player_state(media_player::MediaPlayer *media_player);
|
||||||
void send_media_player_info(media_player::MediaPlayer *media_player);
|
void send_media_player_info(media_player::MediaPlayer *media_player);
|
||||||
|
static bool try_send_media_player_state(APIConnection *api, void *v_media_player);
|
||||||
|
static bool try_send_media_player_info(APIConnection *api, void *v_media_player);
|
||||||
void media_player_command(const MediaPlayerCommandRequest &msg) override;
|
void media_player_command(const MediaPlayerCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
bool try_send_log_message(int level, const char *tag, const char *line);
|
bool try_send_log_message(int level, const char *tag, const char *line);
|
||||||
void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
|
void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
|
||||||
if (!this->service_call_subscription_)
|
if (!this->service_call_subscription_)
|
||||||
return;
|
return;
|
||||||
this->send_message(call);
|
this->send_homeassistant_service_response(call);
|
||||||
}
|
}
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override;
|
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override;
|
||||||
@@ -149,7 +227,7 @@ class APIConnection : public APIServerConnection {
|
|||||||
#ifdef USE_HOMEASSISTANT_TIME
|
#ifdef USE_HOMEASSISTANT_TIME
|
||||||
void send_time_request() {
|
void send_time_request() {
|
||||||
GetTimeRequest req;
|
GetTimeRequest req;
|
||||||
this->send_message(req);
|
this->send_get_time_request(req);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -168,17 +246,24 @@ class APIConnection : public APIServerConnection {
|
|||||||
#ifdef USE_ALARM_CONTROL_PANEL
|
#ifdef USE_ALARM_CONTROL_PANEL
|
||||||
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
|
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
|
||||||
void send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
|
void send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
|
||||||
|
static bool try_send_alarm_control_panel_state(APIConnection *api, void *v_a_alarm_control_panel);
|
||||||
|
static bool try_send_alarm_control_panel_info(APIConnection *api, void *v_a_alarm_control_panel);
|
||||||
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override;
|
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_EVENT
|
#ifdef USE_EVENT
|
||||||
void send_event(event::Event *event, const std::string &event_type);
|
void send_event(event::Event *event, std::string event_type);
|
||||||
void send_event_info(event::Event *event);
|
void send_event_info(event::Event *event);
|
||||||
|
static bool try_send_event(APIConnection *api, void *v_event);
|
||||||
|
static bool try_send_event(APIConnection *api, event::Event *event, std::string event_type);
|
||||||
|
static bool try_send_event_info(APIConnection *api, void *v_event);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_UPDATE
|
#ifdef USE_UPDATE
|
||||||
bool send_update_state(update::UpdateEntity *update);
|
bool send_update_state(update::UpdateEntity *update);
|
||||||
void send_update_info(update::UpdateEntity *update);
|
void send_update_info(update::UpdateEntity *update);
|
||||||
|
static bool try_send_update_state(APIConnection *api, void *v_update);
|
||||||
|
static bool try_send_update_info(APIConnection *api, void *v_update);
|
||||||
void update_command(const UpdateCommandRequest &msg) override;
|
void update_command(const UpdateCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -227,210 +312,19 @@ class APIConnection : public APIServerConnection {
|
|||||||
void on_fatal_error() override;
|
void on_fatal_error() override;
|
||||||
void on_unauthenticated_access() override;
|
void on_unauthenticated_access() override;
|
||||||
void on_no_setup_connection() override;
|
void on_no_setup_connection() override;
|
||||||
ProtoWriteBuffer create_buffer(uint32_t reserve_size) override {
|
ProtoWriteBuffer create_buffer() override {
|
||||||
// FIXME: ensure no recursive writes can happen
|
// FIXME: ensure no recursive writes can happen
|
||||||
|
this->proto_write_buffer_.clear();
|
||||||
// Get header padding size - used for both reserve and insert
|
return {&this->proto_write_buffer_};
|
||||||
uint8_t header_padding = this->helper_->frame_header_padding();
|
|
||||||
|
|
||||||
// Get shared buffer from parent server
|
|
||||||
std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
|
|
||||||
shared_buf.clear();
|
|
||||||
// Reserve space for header padding + message + footer
|
|
||||||
// - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
|
|
||||||
// - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
|
|
||||||
shared_buf.reserve(reserve_size + header_padding + this->helper_->frame_footer_size());
|
|
||||||
// Resize to add header padding so message encoding starts at the correct position
|
|
||||||
shared_buf.resize(header_padding);
|
|
||||||
return {&shared_buf};
|
|
||||||
}
|
}
|
||||||
|
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;
|
||||||
// Prepare buffer for next message in batch
|
|
||||||
ProtoWriteBuffer prepare_message_buffer(uint16_t message_size, bool is_first_message) {
|
|
||||||
// Get reference to shared buffer (it maintains state between batch messages)
|
|
||||||
std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
|
|
||||||
|
|
||||||
if (is_first_message) {
|
|
||||||
shared_buf.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t current_size = shared_buf.size();
|
|
||||||
|
|
||||||
// Calculate padding to add:
|
|
||||||
// - First message: just header padding
|
|
||||||
// - Subsequent messages: footer for previous message + header padding for this message
|
|
||||||
size_t padding_to_add = is_first_message
|
|
||||||
? this->helper_->frame_header_padding()
|
|
||||||
: this->helper_->frame_header_padding() + this->helper_->frame_footer_size();
|
|
||||||
|
|
||||||
// Reserve space for padding + message
|
|
||||||
shared_buf.reserve(current_size + padding_to_add + message_size);
|
|
||||||
|
|
||||||
// Resize to add the padding bytes
|
|
||||||
shared_buf.resize(current_size + padding_to_add);
|
|
||||||
|
|
||||||
return {&shared_buf};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool try_to_clear_buffer(bool log_out_of_space);
|
|
||||||
bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) override;
|
|
||||||
|
|
||||||
std::string get_client_combined_info() const { return this->client_combined_info_; }
|
std::string get_client_combined_info() const { return this->client_combined_info_; }
|
||||||
|
|
||||||
// Buffer allocator methods for batch processing
|
|
||||||
ProtoWriteBuffer allocate_single_message_buffer(uint16_t size);
|
|
||||||
ProtoWriteBuffer allocate_batch_message_buffer(uint16_t size);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Helper function to fill common entity info fields
|
friend APIServer;
|
||||||
static void fill_entity_info_base(esphome::EntityBase *entity, InfoResponseProtoMessage &response) {
|
|
||||||
// Set common fields that are shared by all entity types
|
|
||||||
response.key = entity->get_object_id_hash();
|
|
||||||
response.object_id = entity->get_object_id();
|
|
||||||
|
|
||||||
if (entity->has_own_name())
|
bool send_(const void *buf, size_t len, bool force);
|
||||||
response.name = entity->get_name();
|
|
||||||
|
|
||||||
// Set common EntityBase properties
|
|
||||||
response.icon = entity->get_icon();
|
|
||||||
response.disabled_by_default = entity->is_disabled_by_default();
|
|
||||||
response.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to fill common entity state fields
|
|
||||||
static void fill_entity_state_base(esphome::EntityBase *entity, StateResponseProtoMessage &response) {
|
|
||||||
response.key = entity->get_object_id_hash();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-template helper to encode any ProtoMessage
|
|
||||||
static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint16_t message_type, APIConnection *conn,
|
|
||||||
uint32_t remaining_size, bool is_single);
|
|
||||||
|
|
||||||
#ifdef USE_BINARY_SENSOR
|
|
||||||
static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_COVER
|
|
||||||
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_FAN
|
|
||||||
static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_LIGHT
|
|
||||||
static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_SENSOR
|
|
||||||
static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_SWITCH
|
|
||||||
static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
|
||||||
static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_CLIMATE
|
|
||||||
static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_NUMBER
|
|
||||||
static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_DATETIME_DATE
|
|
||||||
static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_DATETIME_TIME
|
|
||||||
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_DATETIME_DATETIME
|
|
||||||
static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_TEXT
|
|
||||||
static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_SELECT
|
|
||||||
static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_BUTTON
|
|
||||||
static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_LOCK
|
|
||||||
static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_VALVE
|
|
||||||
static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_MEDIA_PLAYER
|
|
||||||
static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_ALARM_CONTROL_PANEL
|
|
||||||
static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_EVENT
|
|
||||||
static uint16_t try_send_event_response(event::Event *event, const std::string &event_type, APIConnection *conn,
|
|
||||||
uint32_t remaining_size, bool is_single);
|
|
||||||
static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_UPDATE
|
|
||||||
static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
#ifdef USE_ESP32_CAMERA
|
|
||||||
static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Method for ListEntitiesDone batching
|
|
||||||
static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
|
|
||||||
// Method for DisconnectRequest batching
|
|
||||||
static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
|
||||||
bool is_single);
|
|
||||||
|
|
||||||
// Helper function to get estimated message size for buffer pre-allocation
|
|
||||||
static uint16_t get_estimated_message_size(uint16_t message_type);
|
|
||||||
|
|
||||||
enum class ConnectionState {
|
enum class ConnectionState {
|
||||||
WAITING_FOR_HELLO,
|
WAITING_FOR_HELLO,
|
||||||
@@ -440,6 +334,9 @@ class APIConnection : public APIServerConnection {
|
|||||||
|
|
||||||
bool remove_{false};
|
bool remove_{false};
|
||||||
|
|
||||||
|
// Buffer used to encode proto messages
|
||||||
|
// Re-use to prevent allocations
|
||||||
|
std::vector<uint8_t> proto_write_buffer_;
|
||||||
std::unique_ptr<APIFrameHelper> helper_;
|
std::unique_ptr<APIFrameHelper> helper_;
|
||||||
|
|
||||||
std::string client_info_;
|
std::string client_info_;
|
||||||
@@ -460,160 +357,10 @@ class APIConnection : public APIServerConnection {
|
|||||||
bool service_call_subscription_{false};
|
bool service_call_subscription_{false};
|
||||||
bool next_close_ = false;
|
bool next_close_ = false;
|
||||||
APIServer *parent_;
|
APIServer *parent_;
|
||||||
|
DeferredMessageQueue deferred_message_queue_;
|
||||||
InitialStateIterator initial_state_iterator_;
|
InitialStateIterator initial_state_iterator_;
|
||||||
ListEntitiesIterator list_entities_iterator_;
|
ListEntitiesIterator list_entities_iterator_;
|
||||||
int state_subs_at_ = -1;
|
int state_subs_at_ = -1;
|
||||||
|
|
||||||
// Function pointer type for message encoding
|
|
||||||
using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single);
|
|
||||||
|
|
||||||
// Optimized MessageCreator class using union dispatch
|
|
||||||
class MessageCreator {
|
|
||||||
public:
|
|
||||||
// Constructor for function pointer (message_type = 0)
|
|
||||||
MessageCreator(MessageCreatorPtr ptr) : message_type_(0) { data_.ptr = ptr; }
|
|
||||||
|
|
||||||
// Constructor for string state capture
|
|
||||||
MessageCreator(const std::string &value, uint16_t msg_type) : message_type_(msg_type) {
|
|
||||||
data_.string_ptr = new std::string(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructor
|
|
||||||
~MessageCreator() {
|
|
||||||
// Clean up string data for string-based message types
|
|
||||||
if (uses_string_data_()) {
|
|
||||||
delete data_.string_ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy constructor
|
|
||||||
MessageCreator(const MessageCreator &other) : message_type_(other.message_type_) {
|
|
||||||
if (message_type_ == 0) {
|
|
||||||
data_.ptr = other.data_.ptr;
|
|
||||||
} else if (uses_string_data_()) {
|
|
||||||
data_.string_ptr = new std::string(*other.data_.string_ptr);
|
|
||||||
} else {
|
|
||||||
data_ = other.data_; // For POD types
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move constructor
|
|
||||||
MessageCreator(MessageCreator &&other) noexcept : data_(other.data_), message_type_(other.message_type_) {
|
|
||||||
other.message_type_ = 0; // Reset other to function pointer type
|
|
||||||
other.data_.ptr = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assignment operators (needed for batch deduplication)
|
|
||||||
MessageCreator &operator=(const MessageCreator &other) {
|
|
||||||
if (this != &other) {
|
|
||||||
// Clean up current string data if needed
|
|
||||||
if (uses_string_data_()) {
|
|
||||||
delete data_.string_ptr;
|
|
||||||
}
|
|
||||||
// Copy new data
|
|
||||||
message_type_ = other.message_type_;
|
|
||||||
if (other.message_type_ == 0) {
|
|
||||||
data_.ptr = other.data_.ptr;
|
|
||||||
} else if (other.uses_string_data_()) {
|
|
||||||
data_.string_ptr = new std::string(*other.data_.string_ptr);
|
|
||||||
} else {
|
|
||||||
data_ = other.data_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageCreator &operator=(MessageCreator &&other) noexcept {
|
|
||||||
if (this != &other) {
|
|
||||||
// Clean up current string data if needed
|
|
||||||
if (uses_string_data_()) {
|
|
||||||
delete data_.string_ptr;
|
|
||||||
}
|
|
||||||
// Move data
|
|
||||||
message_type_ = other.message_type_;
|
|
||||||
data_ = other.data_;
|
|
||||||
// Reset other to safe state
|
|
||||||
other.message_type_ = 0;
|
|
||||||
other.data_.ptr = nullptr;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call operator
|
|
||||||
uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Helper to check if this message type uses heap-allocated strings
|
|
||||||
bool uses_string_data_() const { return message_type_ == EventResponse::MESSAGE_TYPE; }
|
|
||||||
union CreatorData {
|
|
||||||
MessageCreatorPtr ptr; // 8 bytes
|
|
||||||
std::string *string_ptr; // 8 bytes
|
|
||||||
} data_; // 8 bytes
|
|
||||||
uint16_t message_type_; // 2 bytes (0 = function ptr, >0 = state capture)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generic batching mechanism for both state updates and entity info
|
|
||||||
struct DeferredBatch {
|
|
||||||
struct BatchItem {
|
|
||||||
EntityBase *entity; // Entity pointer
|
|
||||||
MessageCreator creator; // Function that creates the message when needed
|
|
||||||
uint16_t message_type; // Message type for overhead calculation
|
|
||||||
|
|
||||||
// Constructor for creating BatchItem
|
|
||||||
BatchItem(EntityBase *entity, MessageCreator creator, uint16_t message_type)
|
|
||||||
: entity(entity), creator(std::move(creator)), message_type(message_type) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<BatchItem> items;
|
|
||||||
uint32_t batch_start_time{0};
|
|
||||||
bool batch_scheduled{false};
|
|
||||||
|
|
||||||
DeferredBatch() {
|
|
||||||
// Pre-allocate capacity for typical batch sizes to avoid reallocation
|
|
||||||
items.reserve(8);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add item to the batch
|
|
||||||
void add_item(EntityBase *entity, MessageCreator creator, uint16_t message_type);
|
|
||||||
void clear() {
|
|
||||||
items.clear();
|
|
||||||
batch_scheduled = false;
|
|
||||||
batch_start_time = 0;
|
|
||||||
}
|
|
||||||
bool empty() const { return items.empty(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
DeferredBatch deferred_batch_;
|
|
||||||
uint32_t get_batch_delay_ms_() const;
|
|
||||||
// Message will use 8 more bytes than the minimum size, and typical
|
|
||||||
// MTU is 1500. Sometimes users will see as low as 1460 MTU.
|
|
||||||
// If its IPv6 the header is 40 bytes, and if its IPv4
|
|
||||||
// the header is 20 bytes. So we have 1460 - 40 = 1420 bytes
|
|
||||||
// available for the payload. But we also need to add the size of
|
|
||||||
// the protobuf overhead, which is 8 bytes.
|
|
||||||
//
|
|
||||||
// To be safe we pick 1390 bytes as the maximum size
|
|
||||||
// to send in one go. This is the maximum size of a single packet
|
|
||||||
// that can be sent over the network.
|
|
||||||
// This is to avoid fragmentation of the packet.
|
|
||||||
static constexpr size_t MAX_PACKET_SIZE = 1390; // MTU
|
|
||||||
|
|
||||||
bool schedule_batch_();
|
|
||||||
void process_batch_();
|
|
||||||
|
|
||||||
// State for batch buffer allocation
|
|
||||||
bool batch_first_message_{false};
|
|
||||||
|
|
||||||
// Helper function to schedule a deferred message with known message type
|
|
||||||
bool schedule_message_(EntityBase *entity, MessageCreator creator, uint16_t message_type) {
|
|
||||||
this->deferred_batch_.add_item(entity, std::move(creator), message_type);
|
|
||||||
return this->schedule_batch_();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overload for function pointers (for info messages and current state reads)
|
|
||||||
bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint16_t message_type) {
|
|
||||||
return schedule_message_(entity, MessageCreator(function_ptr), message_type);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|||||||
@@ -1,19 +1,25 @@
|
|||||||
#include "api_frame_helper.h"
|
#include "api_frame_helper.h"
|
||||||
#ifdef USE_API
|
#ifdef USE_API
|
||||||
#include "esphome/core/application.h"
|
#include "esphome/core/log.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/application.h"
|
||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
#include "api_pb2_size.h"
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cinttypes>
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
static const char *const TAG = "api.socket";
|
static const char *const TAG = "api.socket";
|
||||||
|
|
||||||
|
/// Is the given return value (from write syscalls) a wouldblock error?
|
||||||
|
bool is_would_block(ssize_t ret) {
|
||||||
|
if (ret == -1) {
|
||||||
|
return errno == EWOULDBLOCK || errno == EAGAIN;
|
||||||
|
}
|
||||||
|
return ret == 0;
|
||||||
|
}
|
||||||
|
|
||||||
const char *api_error_to_str(APIError err) {
|
const char *api_error_to_str(APIError err) {
|
||||||
// not using switch to ensure compiler doesn't try to build a big table out of it
|
// not using switch to ensure compiler doesn't try to build a big table out of it
|
||||||
if (err == APIError::OK) {
|
if (err == APIError::OK) {
|
||||||
@@ -66,154 +72,7 @@ const char *api_error_to_str(APIError err) {
|
|||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to buffer data from IOVs
|
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s: " msg, info_.c_str(), ##__VA_ARGS__)
|
||||||
void APIFrameHelper::buffer_data_from_iov_(const struct iovec *iov, int iovcnt, uint16_t total_write_len) {
|
|
||||||
SendBuffer buffer;
|
|
||||||
buffer.data.reserve(total_write_len);
|
|
||||||
for (int i = 0; i < iovcnt; i++) {
|
|
||||||
const uint8_t *data = reinterpret_cast<uint8_t *>(iov[i].iov_base);
|
|
||||||
buffer.data.insert(buffer.data.end(), data, data + iov[i].iov_len);
|
|
||||||
}
|
|
||||||
this->tx_buf_.push_back(std::move(buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method writes data to socket or buffers it
|
|
||||||
APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
|
||||||
// Returns APIError::OK if successful (or would block, but data has been buffered)
|
|
||||||
// Returns APIError::SOCKET_WRITE_FAILED if socket write failed, and sets state to FAILED
|
|
||||||
|
|
||||||
if (iovcnt == 0)
|
|
||||||
return APIError::OK; // Nothing to do, success
|
|
||||||
|
|
||||||
uint16_t total_write_len = 0;
|
|
||||||
for (int i = 0; i < iovcnt; i++) {
|
|
||||||
#ifdef HELPER_LOG_PACKETS
|
|
||||||
ESP_LOGVV(TAG, "Sending raw: %s",
|
|
||||||
format_hex_pretty(reinterpret_cast<uint8_t *>(iov[i].iov_base), iov[i].iov_len).c_str());
|
|
||||||
#endif
|
|
||||||
total_write_len += static_cast<uint16_t>(iov[i].iov_len);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to send any existing buffered data first if there is any
|
|
||||||
if (!this->tx_buf_.empty()) {
|
|
||||||
APIError send_result = try_send_tx_buf_();
|
|
||||||
// If real error occurred (not just WOULD_BLOCK), return it
|
|
||||||
if (send_result != APIError::OK && send_result != APIError::WOULD_BLOCK) {
|
|
||||||
return send_result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there is still data in the buffer, we can't send, buffer
|
|
||||||
// the new data and return
|
|
||||||
if (!this->tx_buf_.empty()) {
|
|
||||||
this->buffer_data_from_iov_(iov, iovcnt, total_write_len);
|
|
||||||
return APIError::OK; // Success, data buffered
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to send directly if no buffered data
|
|
||||||
ssize_t sent = this->socket_->writev(iov, iovcnt);
|
|
||||||
|
|
||||||
if (sent == -1) {
|
|
||||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
|
||||||
// Socket would block, buffer the data
|
|
||||||
this->buffer_data_from_iov_(iov, iovcnt, total_write_len);
|
|
||||||
return APIError::OK; // Success, data buffered
|
|
||||||
}
|
|
||||||
// Socket error
|
|
||||||
ESP_LOGVV(TAG, "%s: Socket write failed with errno %d", this->info_.c_str(), errno);
|
|
||||||
this->state_ = State::FAILED;
|
|
||||||
return APIError::SOCKET_WRITE_FAILED; // Socket write failed
|
|
||||||
} else if (static_cast<uint16_t>(sent) < total_write_len) {
|
|
||||||
// Partially sent, buffer the remaining data
|
|
||||||
SendBuffer buffer;
|
|
||||||
uint16_t to_consume = static_cast<uint16_t>(sent);
|
|
||||||
uint16_t remaining = total_write_len - static_cast<uint16_t>(sent);
|
|
||||||
|
|
||||||
buffer.data.reserve(remaining);
|
|
||||||
|
|
||||||
for (int i = 0; i < iovcnt; i++) {
|
|
||||||
if (to_consume >= iov[i].iov_len) {
|
|
||||||
// This segment was fully sent
|
|
||||||
to_consume -= static_cast<uint16_t>(iov[i].iov_len);
|
|
||||||
} else {
|
|
||||||
// This segment was partially sent or not sent at all
|
|
||||||
const uint8_t *data = reinterpret_cast<uint8_t *>(iov[i].iov_base) + to_consume;
|
|
||||||
uint16_t len = static_cast<uint16_t>(iov[i].iov_len) - to_consume;
|
|
||||||
buffer.data.insert(buffer.data.end(), data, data + len);
|
|
||||||
to_consume = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this->tx_buf_.push_back(std::move(buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
return APIError::OK; // Success, all data sent or buffered
|
|
||||||
}
|
|
||||||
|
|
||||||
// Common implementation for trying to send buffered data
|
|
||||||
// IMPORTANT: Caller MUST ensure tx_buf_ is not empty before calling this method
|
|
||||||
APIError APIFrameHelper::try_send_tx_buf_() {
|
|
||||||
// Try to send from tx_buf - we assume it's not empty as it's the caller's responsibility to check
|
|
||||||
bool tx_buf_empty = false;
|
|
||||||
while (!tx_buf_empty) {
|
|
||||||
// Get the first buffer in the queue
|
|
||||||
SendBuffer &front_buffer = this->tx_buf_.front();
|
|
||||||
|
|
||||||
// Try to send the remaining data in this buffer
|
|
||||||
ssize_t sent = this->socket_->write(front_buffer.current_data(), front_buffer.remaining());
|
|
||||||
|
|
||||||
if (sent == -1) {
|
|
||||||
if (errno != EWOULDBLOCK && errno != EAGAIN) {
|
|
||||||
// Real socket error (not just would block)
|
|
||||||
ESP_LOGVV(TAG, "%s: Socket write failed with errno %d", this->info_.c_str(), errno);
|
|
||||||
this->state_ = State::FAILED;
|
|
||||||
return APIError::SOCKET_WRITE_FAILED; // Socket write failed
|
|
||||||
}
|
|
||||||
// Socket would block, we'll try again later
|
|
||||||
return APIError::WOULD_BLOCK;
|
|
||||||
} else if (sent == 0) {
|
|
||||||
// Nothing sent but not an error
|
|
||||||
return APIError::WOULD_BLOCK;
|
|
||||||
} else if (static_cast<uint16_t>(sent) < front_buffer.remaining()) {
|
|
||||||
// Partially sent, update offset
|
|
||||||
// Cast to ensure no overflow issues with uint16_t
|
|
||||||
front_buffer.offset += static_cast<uint16_t>(sent);
|
|
||||||
return APIError::WOULD_BLOCK; // Stop processing more buffers if we couldn't send a complete buffer
|
|
||||||
} else {
|
|
||||||
// Buffer completely sent, remove it from the queue
|
|
||||||
this->tx_buf_.pop_front();
|
|
||||||
// Update empty status for the loop condition
|
|
||||||
tx_buf_empty = this->tx_buf_.empty();
|
|
||||||
// Continue loop to try sending the next buffer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return APIError::OK; // All buffers sent successfully
|
|
||||||
}
|
|
||||||
|
|
||||||
APIError APIFrameHelper::init_common_() {
|
|
||||||
if (state_ != State::INITIALIZE || this->socket_ == nullptr) {
|
|
||||||
ESP_LOGVV(TAG, "%s: Bad state for init %d", this->info_.c_str(), (int) state_);
|
|
||||||
return APIError::BAD_STATE;
|
|
||||||
}
|
|
||||||
int err = this->socket_->setblocking(false);
|
|
||||||
if (err != 0) {
|
|
||||||
state_ = State::FAILED;
|
|
||||||
ESP_LOGVV(TAG, "%s: Setting nonblocking failed with errno %d", this->info_.c_str(), errno);
|
|
||||||
return APIError::TCP_NONBLOCKING_FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
int enable = 1;
|
|
||||||
err = this->socket_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
|
|
||||||
if (err != 0) {
|
|
||||||
state_ = State::FAILED;
|
|
||||||
ESP_LOGVV(TAG, "%s: Setting nodelay failed with errno %d", this->info_.c_str(), errno);
|
|
||||||
return APIError::TCP_NODELAY_FAILED;
|
|
||||||
}
|
|
||||||
return APIError::OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s: " msg, this->info_.c_str(), ##__VA_ARGS__)
|
|
||||||
// uncomment to log raw packets
|
// uncomment to log raw packets
|
||||||
//#define HELPER_LOG_PACKETS
|
//#define HELPER_LOG_PACKETS
|
||||||
|
|
||||||
@@ -261,9 +120,23 @@ std::string noise_err_to_str(int err) {
|
|||||||
|
|
||||||
/// Initialize the frame helper, returns OK if successful.
|
/// Initialize the frame helper, returns OK if successful.
|
||||||
APIError APINoiseFrameHelper::init() {
|
APIError APINoiseFrameHelper::init() {
|
||||||
APIError err = init_common_();
|
if (state_ != State::INITIALIZE || socket_ == nullptr) {
|
||||||
if (err != APIError::OK) {
|
HELPER_LOG("Bad state for init %d", (int) state_);
|
||||||
return err;
|
return APIError::BAD_STATE;
|
||||||
|
}
|
||||||
|
int err = socket_->setblocking(false);
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Setting nonblocking failed with errno %d", errno);
|
||||||
|
return APIError::TCP_NONBLOCKING_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int enable = 1;
|
||||||
|
err = socket_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Setting nodelay failed with errno %d", errno);
|
||||||
|
return APIError::TCP_NODELAY_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// init prologue
|
// init prologue
|
||||||
@@ -275,16 +148,17 @@ APIError APINoiseFrameHelper::init() {
|
|||||||
/// Run through handshake messages (if in that phase)
|
/// Run through handshake messages (if in that phase)
|
||||||
APIError APINoiseFrameHelper::loop() {
|
APIError APINoiseFrameHelper::loop() {
|
||||||
APIError err = state_action_();
|
APIError err = state_action_();
|
||||||
if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
|
if (err == APIError::WOULD_BLOCK)
|
||||||
|
return APIError::OK;
|
||||||
|
if (err != APIError::OK)
|
||||||
return err;
|
return err;
|
||||||
}
|
if (!tx_buf_.empty()) {
|
||||||
if (!this->tx_buf_.empty()) {
|
|
||||||
err = try_send_tx_buf_();
|
err = try_send_tx_buf_();
|
||||||
if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
|
if (err != APIError::OK) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return APIError::OK; // Convert WOULD_BLOCK to OK to avoid connection termination
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
||||||
@@ -310,8 +184,8 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
// read header
|
// read header
|
||||||
if (rx_header_buf_len_ < 3) {
|
if (rx_header_buf_len_ < 3) {
|
||||||
// no header information yet
|
// no header information yet
|
||||||
uint8_t to_read = 3 - rx_header_buf_len_;
|
size_t to_read = 3 - rx_header_buf_len_;
|
||||||
ssize_t received = this->socket_->read(&rx_header_buf_[rx_header_buf_len_], to_read);
|
ssize_t received = socket_->read(&rx_header_buf_[rx_header_buf_len_], to_read);
|
||||||
if (received == -1) {
|
if (received == -1) {
|
||||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
@@ -324,8 +198,8 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
HELPER_LOG("Connection closed");
|
HELPER_LOG("Connection closed");
|
||||||
return APIError::CONNECTION_CLOSED;
|
return APIError::CONNECTION_CLOSED;
|
||||||
}
|
}
|
||||||
rx_header_buf_len_ += static_cast<uint8_t>(received);
|
rx_header_buf_len_ += received;
|
||||||
if (static_cast<uint8_t>(received) != to_read) {
|
if ((size_t) received != to_read) {
|
||||||
// not a full read
|
// not a full read
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
}
|
}
|
||||||
@@ -357,8 +231,8 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
|
|
||||||
if (rx_buf_len_ < msg_size) {
|
if (rx_buf_len_ < msg_size) {
|
||||||
// more data to read
|
// more data to read
|
||||||
uint16_t to_read = msg_size - rx_buf_len_;
|
size_t to_read = msg_size - rx_buf_len_;
|
||||||
ssize_t received = this->socket_->read(&rx_buf_[rx_buf_len_], to_read);
|
ssize_t received = socket_->read(&rx_buf_[rx_buf_len_], to_read);
|
||||||
if (received == -1) {
|
if (received == -1) {
|
||||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
@@ -371,8 +245,8 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
HELPER_LOG("Connection closed");
|
HELPER_LOG("Connection closed");
|
||||||
return APIError::CONNECTION_CLOSED;
|
return APIError::CONNECTION_CLOSED;
|
||||||
}
|
}
|
||||||
rx_buf_len_ += static_cast<uint16_t>(received);
|
rx_buf_len_ += received;
|
||||||
if (static_cast<uint16_t>(received) != to_read) {
|
if ((size_t) received != to_read) {
|
||||||
// not all read
|
// not all read
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
}
|
}
|
||||||
@@ -421,8 +295,6 @@ APIError APINoiseFrameHelper::state_action_() {
|
|||||||
if (aerr != APIError::OK)
|
if (aerr != APIError::OK)
|
||||||
return aerr;
|
return aerr;
|
||||||
// ignore contents, may be used in future for flags
|
// ignore contents, may be used in future for flags
|
||||||
// Reserve space for: existing prologue + 2 size bytes + frame data
|
|
||||||
prologue_.reserve(prologue_.size() + 2 + frame.msg.size());
|
|
||||||
prologue_.push_back((uint8_t) (frame.msg.size() >> 8));
|
prologue_.push_back((uint8_t) (frame.msg.size() >> 8));
|
||||||
prologue_.push_back((uint8_t) frame.msg.size());
|
prologue_.push_back((uint8_t) frame.msg.size());
|
||||||
prologue_.insert(prologue_.end(), frame.msg.begin(), frame.msg.end());
|
prologue_.insert(prologue_.end(), frame.msg.begin(), frame.msg.end());
|
||||||
@@ -431,20 +303,16 @@ APIError APINoiseFrameHelper::state_action_() {
|
|||||||
}
|
}
|
||||||
if (state_ == State::SERVER_HELLO) {
|
if (state_ == State::SERVER_HELLO) {
|
||||||
// send server hello
|
// send server hello
|
||||||
const std::string &name = App.get_name();
|
|
||||||
const std::string &mac = get_mac_address();
|
|
||||||
|
|
||||||
std::vector<uint8_t> msg;
|
std::vector<uint8_t> msg;
|
||||||
// Reserve space for: 1 byte proto + name + null + mac + null
|
|
||||||
msg.reserve(1 + name.size() + 1 + mac.size() + 1);
|
|
||||||
|
|
||||||
// chosen proto
|
// chosen proto
|
||||||
msg.push_back(0x01);
|
msg.push_back(0x01);
|
||||||
|
|
||||||
// node name, terminated by null byte
|
// node name, terminated by null byte
|
||||||
|
const std::string &name = App.get_name();
|
||||||
const uint8_t *name_ptr = reinterpret_cast<const uint8_t *>(name.c_str());
|
const uint8_t *name_ptr = reinterpret_cast<const uint8_t *>(name.c_str());
|
||||||
msg.insert(msg.end(), name_ptr, name_ptr + name.size() + 1);
|
msg.insert(msg.end(), name_ptr, name_ptr + name.size() + 1);
|
||||||
// node mac, terminated by null byte
|
// node mac, terminated by null byte
|
||||||
|
const std::string &mac = get_mac_address();
|
||||||
const uint8_t *mac_ptr = reinterpret_cast<const uint8_t *>(mac.c_str());
|
const uint8_t *mac_ptr = reinterpret_cast<const uint8_t *>(mac.c_str());
|
||||||
msg.insert(msg.end(), mac_ptr, mac_ptr + mac.size() + 1);
|
msg.insert(msg.end(), mac_ptr, mac_ptr + mac.size() + 1);
|
||||||
|
|
||||||
@@ -539,18 +407,16 @@ void APINoiseFrameHelper::send_explicit_handshake_reject_(const std::string &rea
|
|||||||
std::vector<uint8_t> data;
|
std::vector<uint8_t> data;
|
||||||
data.resize(reason.length() + 1);
|
data.resize(reason.length() + 1);
|
||||||
data[0] = 0x01; // failure
|
data[0] = 0x01; // failure
|
||||||
|
for (size_t i = 0; i < reason.length(); i++) {
|
||||||
// Copy error message in bulk
|
data[i + 1] = (uint8_t) reason[i];
|
||||||
if (!reason.empty()) {
|
|
||||||
std::memcpy(data.data() + 1, reason.c_str(), reason.length());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// temporarily remove failed state
|
// temporarily remove failed state
|
||||||
auto orig_state = state_;
|
auto orig_state = state_;
|
||||||
state_ = State::EXPLICIT_REJECT;
|
state_ = State::EXPLICIT_REJECT;
|
||||||
write_frame_(data.data(), data.size());
|
write_frame_(data.data(), data.size());
|
||||||
state_ = orig_state;
|
state_ = orig_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||||
int err;
|
int err;
|
||||||
APIError aerr;
|
APIError aerr;
|
||||||
@@ -578,7 +444,7 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
|||||||
return APIError::CIPHERSTATE_DECRYPT_FAILED;
|
return APIError::CIPHERSTATE_DECRYPT_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t msg_size = mbuf.size;
|
size_t msg_size = mbuf.size;
|
||||||
uint8_t *msg_data = frame.msg.data();
|
uint8_t *msg_data = frame.msg.data();
|
||||||
if (msg_size < 4) {
|
if (msg_size < 4) {
|
||||||
state_ = State::FAILED;
|
state_ = State::FAILED;
|
||||||
@@ -604,22 +470,11 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
|||||||
buffer->type = type;
|
buffer->type = type;
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
APIError APINoiseFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) {
|
bool APINoiseFrameHelper::can_write_without_blocking() { return state_ == State::DATA && tx_buf_.empty(); }
|
||||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
APIError APINoiseFrameHelper::write_packet(uint16_t type, const uint8_t *payload, size_t payload_len) {
|
||||||
uint16_t payload_len = static_cast<uint16_t>(raw_buffer->size() - frame_header_padding_);
|
int err;
|
||||||
|
APIError aerr;
|
||||||
// Resize to include MAC space (required for Noise encryption)
|
aerr = state_action_();
|
||||||
raw_buffer->resize(raw_buffer->size() + frame_footer_size_);
|
|
||||||
|
|
||||||
// Use write_protobuf_packets with a single packet
|
|
||||||
std::vector<PacketInfo> packets;
|
|
||||||
packets.emplace_back(type, 0, payload_len);
|
|
||||||
|
|
||||||
return write_protobuf_packets(buffer, packets);
|
|
||||||
}
|
|
||||||
|
|
||||||
APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) {
|
|
||||||
APIError aerr = state_action_();
|
|
||||||
if (aerr != APIError::OK) {
|
if (aerr != APIError::OK) {
|
||||||
return aerr;
|
return aerr;
|
||||||
}
|
}
|
||||||
@@ -628,67 +483,135 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co
|
|||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packets.empty()) {
|
size_t padding = 0;
|
||||||
|
size_t msg_len = 4 + payload_len + padding;
|
||||||
|
size_t frame_len = 3 + msg_len + noise_cipherstate_get_mac_length(send_cipher_);
|
||||||
|
auto tmpbuf = std::unique_ptr<uint8_t[]>{new (std::nothrow) uint8_t[frame_len]};
|
||||||
|
if (tmpbuf == nullptr) {
|
||||||
|
HELPER_LOG("Could not allocate for writing packet");
|
||||||
|
return APIError::OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpbuf[0] = 0x01; // indicator
|
||||||
|
// tmpbuf[1], tmpbuf[2] to be set later
|
||||||
|
const uint8_t msg_offset = 3;
|
||||||
|
const uint8_t payload_offset = msg_offset + 4;
|
||||||
|
tmpbuf[msg_offset + 0] = (uint8_t) (type >> 8); // type
|
||||||
|
tmpbuf[msg_offset + 1] = (uint8_t) type;
|
||||||
|
tmpbuf[msg_offset + 2] = (uint8_t) (payload_len >> 8); // data_len
|
||||||
|
tmpbuf[msg_offset + 3] = (uint8_t) payload_len;
|
||||||
|
// copy data
|
||||||
|
std::copy(payload, payload + payload_len, &tmpbuf[payload_offset]);
|
||||||
|
// fill padding with zeros
|
||||||
|
std::fill(&tmpbuf[payload_offset + payload_len], &tmpbuf[frame_len], 0);
|
||||||
|
|
||||||
|
NoiseBuffer mbuf;
|
||||||
|
noise_buffer_init(mbuf);
|
||||||
|
noise_buffer_set_inout(mbuf, &tmpbuf[msg_offset], msg_len, frame_len - msg_offset);
|
||||||
|
err = noise_cipherstate_encrypt(send_cipher_, &mbuf);
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("noise_cipherstate_encrypt failed: %s", noise_err_to_str(err).c_str());
|
||||||
|
return APIError::CIPHERSTATE_ENCRYPT_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t total_len = 3 + mbuf.size;
|
||||||
|
tmpbuf[1] = (uint8_t) (mbuf.size >> 8);
|
||||||
|
tmpbuf[2] = (uint8_t) mbuf.size;
|
||||||
|
|
||||||
|
struct iovec iov;
|
||||||
|
iov.iov_base = &tmpbuf[0];
|
||||||
|
iov.iov_len = total_len;
|
||||||
|
|
||||||
|
// write raw to not have two packets sent if NAGLE disabled
|
||||||
|
return write_raw_(&iov, 1);
|
||||||
|
}
|
||||||
|
APIError APINoiseFrameHelper::try_send_tx_buf_() {
|
||||||
|
// try send from tx_buf
|
||||||
|
while (state_ != State::CLOSED && !tx_buf_.empty()) {
|
||||||
|
ssize_t sent = socket_->write(tx_buf_.data(), tx_buf_.size());
|
||||||
|
if (sent == -1) {
|
||||||
|
if (errno == EWOULDBLOCK || errno == EAGAIN)
|
||||||
|
break;
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||||
|
return APIError::SOCKET_WRITE_FAILED;
|
||||||
|
} else if (sent == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// TODO: inefficient if multiple packets in txbuf
|
||||||
|
// replace with deque of buffers
|
||||||
|
tx_buf_.erase(tx_buf_.begin(), tx_buf_.begin() + sent);
|
||||||
|
}
|
||||||
|
|
||||||
|
return APIError::OK;
|
||||||
|
}
|
||||||
|
/** Write the data to the socket, or buffer it a write would block
|
||||||
|
*
|
||||||
|
* @param data The data to write
|
||||||
|
* @param len The length of data
|
||||||
|
*/
|
||||||
|
APIError APINoiseFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||||
|
if (iovcnt == 0)
|
||||||
|
return APIError::OK;
|
||||||
|
APIError aerr;
|
||||||
|
|
||||||
|
size_t total_write_len = 0;
|
||||||
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
|
#ifdef HELPER_LOG_PACKETS
|
||||||
|
ESP_LOGVV(TAG, "Sending raw: %s",
|
||||||
|
format_hex_pretty(reinterpret_cast<uint8_t *>(iov[i].iov_base), iov[i].iov_len).c_str());
|
||||||
|
#endif
|
||||||
|
total_write_len += iov[i].iov_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tx_buf_.empty()) {
|
||||||
|
// try to empty tx_buf_ first
|
||||||
|
aerr = try_send_tx_buf_();
|
||||||
|
if (aerr != APIError::OK && aerr != APIError::WOULD_BLOCK)
|
||||||
|
return aerr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tx_buf_.empty()) {
|
||||||
|
// tx buf not empty, can't write now because then stream would be inconsistent
|
||||||
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
|
tx_buf_.insert(tx_buf_.end(), reinterpret_cast<uint8_t *>(iov[i].iov_base),
|
||||||
|
reinterpret_cast<uint8_t *>(iov[i].iov_base) + iov[i].iov_len);
|
||||||
|
}
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
ssize_t sent = socket_->writev(iov, iovcnt);
|
||||||
this->reusable_iovs_.clear();
|
if (is_would_block(sent)) {
|
||||||
this->reusable_iovs_.reserve(packets.size());
|
// operation would block, add buffer to tx_buf
|
||||||
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
// We need to encrypt each packet in place
|
tx_buf_.insert(tx_buf_.end(), reinterpret_cast<uint8_t *>(iov[i].iov_base),
|
||||||
for (const auto &packet : packets) {
|
reinterpret_cast<uint8_t *>(iov[i].iov_base) + iov[i].iov_len);
|
||||||
uint16_t type = packet.message_type;
|
|
||||||
uint16_t offset = packet.offset;
|
|
||||||
uint16_t payload_len = packet.payload_size;
|
|
||||||
uint16_t msg_len = 4 + payload_len; // type(2) + data_len(2) + payload
|
|
||||||
|
|
||||||
// The buffer already has padding at offset
|
|
||||||
uint8_t *buf_start = raw_buffer->data() + offset;
|
|
||||||
|
|
||||||
// Write noise header
|
|
||||||
buf_start[0] = 0x01; // indicator
|
|
||||||
// buf_start[1], buf_start[2] to be set after encryption
|
|
||||||
|
|
||||||
// Write message header (to be encrypted)
|
|
||||||
const uint8_t msg_offset = 3;
|
|
||||||
buf_start[msg_offset + 0] = (uint8_t) (type >> 8); // type high byte
|
|
||||||
buf_start[msg_offset + 1] = (uint8_t) type; // type low byte
|
|
||||||
buf_start[msg_offset + 2] = (uint8_t) (payload_len >> 8); // data_len high byte
|
|
||||||
buf_start[msg_offset + 3] = (uint8_t) payload_len; // data_len low byte
|
|
||||||
// payload data is already in the buffer starting at offset + 7
|
|
||||||
|
|
||||||
// Make sure we have space for MAC
|
|
||||||
// The buffer should already have been sized appropriately
|
|
||||||
|
|
||||||
// Encrypt the message in place
|
|
||||||
NoiseBuffer mbuf;
|
|
||||||
noise_buffer_init(mbuf);
|
|
||||||
noise_buffer_set_inout(mbuf, buf_start + msg_offset, msg_len, msg_len + frame_footer_size_);
|
|
||||||
|
|
||||||
int err = noise_cipherstate_encrypt(send_cipher_, &mbuf);
|
|
||||||
if (err != 0) {
|
|
||||||
state_ = State::FAILED;
|
|
||||||
HELPER_LOG("noise_cipherstate_encrypt failed: %s", noise_err_to_str(err).c_str());
|
|
||||||
return APIError::CIPHERSTATE_ENCRYPT_FAILED;
|
|
||||||
}
|
}
|
||||||
|
return APIError::OK;
|
||||||
// Fill in the encrypted size
|
} else if (sent == -1) {
|
||||||
buf_start[1] = (uint8_t) (mbuf.size >> 8);
|
// an error occurred
|
||||||
buf_start[2] = (uint8_t) mbuf.size;
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||||
// Add iovec for this encrypted packet
|
return APIError::SOCKET_WRITE_FAILED;
|
||||||
struct iovec iov;
|
} else if ((size_t) sent != total_write_len) {
|
||||||
iov.iov_base = buf_start;
|
// partially sent, add end to tx_buf
|
||||||
iov.iov_len = 3 + mbuf.size; // indicator + size + encrypted data
|
size_t to_consume = sent;
|
||||||
this->reusable_iovs_.push_back(iov);
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
|
if (to_consume >= iov[i].iov_len) {
|
||||||
|
to_consume -= iov[i].iov_len;
|
||||||
|
} else {
|
||||||
|
tx_buf_.insert(tx_buf_.end(), reinterpret_cast<uint8_t *>(iov[i].iov_base) + to_consume,
|
||||||
|
reinterpret_cast<uint8_t *>(iov[i].iov_base) + iov[i].iov_len);
|
||||||
|
to_consume = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
|
// fully sent
|
||||||
// Send all encrypted packets in one writev call
|
return APIError::OK;
|
||||||
return this->write_raw_(this->reusable_iovs_.data(), this->reusable_iovs_.size());
|
|
||||||
}
|
}
|
||||||
|
APIError APINoiseFrameHelper::write_frame_(const uint8_t *data, size_t len) {
|
||||||
APIError APINoiseFrameHelper::write_frame_(const uint8_t *data, uint16_t len) {
|
|
||||||
uint8_t header[3];
|
uint8_t header[3];
|
||||||
header[0] = 0x01; // indicator
|
header[0] = 0x01; // indicator
|
||||||
header[1] = (uint8_t) (len >> 8);
|
header[1] = (uint8_t) (len >> 8);
|
||||||
@@ -698,12 +621,12 @@ APIError APINoiseFrameHelper::write_frame_(const uint8_t *data, uint16_t len) {
|
|||||||
iov[0].iov_base = header;
|
iov[0].iov_base = header;
|
||||||
iov[0].iov_len = 3;
|
iov[0].iov_len = 3;
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
return this->write_raw_(iov, 1);
|
return write_raw_(iov, 1);
|
||||||
}
|
}
|
||||||
iov[1].iov_base = const_cast<uint8_t *>(data);
|
iov[1].iov_base = const_cast<uint8_t *>(data);
|
||||||
iov[1].iov_len = len;
|
iov[1].iov_len = len;
|
||||||
|
|
||||||
return this->write_raw_(iov, 2);
|
return write_raw_(iov, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initiate the data structures for the handshake.
|
/** Initiate the data structures for the handshake.
|
||||||
@@ -774,8 +697,6 @@ APIError APINoiseFrameHelper::check_handshake_finished_() {
|
|||||||
return APIError::HANDSHAKESTATE_SPLIT_FAILED;
|
return APIError::HANDSHAKESTATE_SPLIT_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame_footer_size_ = noise_cipherstate_get_mac_length(send_cipher_);
|
|
||||||
|
|
||||||
HELPER_LOG("Handshake complete!");
|
HELPER_LOG("Handshake complete!");
|
||||||
noise_handshakestate_free(handshake_);
|
noise_handshakestate_free(handshake_);
|
||||||
handshake_ = nullptr;
|
handshake_ = nullptr;
|
||||||
@@ -798,25 +719,53 @@ APINoiseFrameHelper::~APINoiseFrameHelper() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
APIError APINoiseFrameHelper::close() {
|
||||||
|
state_ = State::CLOSED;
|
||||||
|
int err = socket_->close();
|
||||||
|
if (err == -1)
|
||||||
|
return APIError::CLOSE_FAILED;
|
||||||
|
return APIError::OK;
|
||||||
|
}
|
||||||
|
APIError APINoiseFrameHelper::shutdown(int how) {
|
||||||
|
int err = socket_->shutdown(how);
|
||||||
|
if (err == -1)
|
||||||
|
return APIError::SHUTDOWN_FAILED;
|
||||||
|
if (how == SHUT_RDWR) {
|
||||||
|
state_ = State::CLOSED;
|
||||||
|
}
|
||||||
|
return APIError::OK;
|
||||||
|
}
|
||||||
extern "C" {
|
extern "C" {
|
||||||
// declare how noise generates random bytes (here with a good HWRNG based on the RF system)
|
// declare how noise generates random bytes (here with a good HWRNG based on the RF system)
|
||||||
void noise_rand_bytes(void *output, size_t len) {
|
void noise_rand_bytes(void *output, size_t len) {
|
||||||
if (!esphome::random_bytes(reinterpret_cast<uint8_t *>(output), len)) {
|
if (!esphome::random_bytes(reinterpret_cast<uint8_t *>(output), len)) {
|
||||||
ESP_LOGE(TAG, "Acquiring random bytes failed; rebooting");
|
ESP_LOGE(TAG, "Failed to acquire random bytes, rebooting!");
|
||||||
arch_restart();
|
arch_restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // USE_API_NOISE
|
#endif // USE_API_NOISE
|
||||||
|
|
||||||
#ifdef USE_API_PLAINTEXT
|
#ifdef USE_API_PLAINTEXT
|
||||||
|
|
||||||
/// Initialize the frame helper, returns OK if successful.
|
/// Initialize the frame helper, returns OK if successful.
|
||||||
APIError APIPlaintextFrameHelper::init() {
|
APIError APIPlaintextFrameHelper::init() {
|
||||||
APIError err = init_common_();
|
if (state_ != State::INITIALIZE || socket_ == nullptr) {
|
||||||
if (err != APIError::OK) {
|
HELPER_LOG("Bad state for init %d", (int) state_);
|
||||||
return err;
|
return APIError::BAD_STATE;
|
||||||
|
}
|
||||||
|
int err = socket_->setblocking(false);
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Setting nonblocking failed with errno %d", errno);
|
||||||
|
return APIError::TCP_NONBLOCKING_FAILED;
|
||||||
|
}
|
||||||
|
int enable = 1;
|
||||||
|
err = socket_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Setting nodelay failed with errno %d", errno);
|
||||||
|
return APIError::TCP_NODELAY_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
state_ = State::DATA;
|
state_ = State::DATA;
|
||||||
@@ -827,13 +776,14 @@ APIError APIPlaintextFrameHelper::loop() {
|
|||||||
if (state_ != State::DATA) {
|
if (state_ != State::DATA) {
|
||||||
return APIError::BAD_STATE;
|
return APIError::BAD_STATE;
|
||||||
}
|
}
|
||||||
if (!this->tx_buf_.empty()) {
|
// try send pending TX data
|
||||||
|
if (!tx_buf_.empty()) {
|
||||||
APIError err = try_send_tx_buf_();
|
APIError err = try_send_tx_buf_();
|
||||||
if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
|
if (err != APIError::OK) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return APIError::OK; // Convert WOULD_BLOCK to OK to avoid connection termination
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
||||||
@@ -853,15 +803,8 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
|
|
||||||
// read header
|
// read header
|
||||||
while (!rx_header_parsed_) {
|
while (!rx_header_parsed_) {
|
||||||
// Now that we know when the socket is ready, we can read up to 3 bytes
|
uint8_t data;
|
||||||
// into the rx_header_buf_ before we have to switch back to reading
|
ssize_t received = socket_->read(&data, 1);
|
||||||
// one byte at a time to ensure we don't read past the message and
|
|
||||||
// into the next one.
|
|
||||||
|
|
||||||
// Read directly into rx_header_buf_ at the current position
|
|
||||||
// Try to get to at least 3 bytes total (indicator + 2 varint bytes), then read one byte at a time
|
|
||||||
ssize_t received =
|
|
||||||
this->socket_->read(&rx_header_buf_[rx_header_buf_pos_], rx_header_buf_pos_ < 3 ? 3 - rx_header_buf_pos_ : 1);
|
|
||||||
if (received == -1) {
|
if (received == -1) {
|
||||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
@@ -874,75 +817,32 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
HELPER_LOG("Connection closed");
|
HELPER_LOG("Connection closed");
|
||||||
return APIError::CONNECTION_CLOSED;
|
return APIError::CONNECTION_CLOSED;
|
||||||
}
|
}
|
||||||
|
rx_header_buf_.push_back(data);
|
||||||
|
|
||||||
// If this was the first read, validate the indicator byte
|
// try parse header
|
||||||
if (rx_header_buf_pos_ == 0 && received > 0) {
|
if (rx_header_buf_[0] != 0x00) {
|
||||||
if (rx_header_buf_[0] != 0x00) {
|
|
||||||
state_ = State::FAILED;
|
|
||||||
HELPER_LOG("Bad indicator byte %u", rx_header_buf_[0]);
|
|
||||||
return APIError::BAD_INDICATOR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rx_header_buf_pos_ += received;
|
|
||||||
|
|
||||||
// Check for buffer overflow
|
|
||||||
if (rx_header_buf_pos_ >= sizeof(rx_header_buf_)) {
|
|
||||||
state_ = State::FAILED;
|
state_ = State::FAILED;
|
||||||
HELPER_LOG("Header buffer overflow");
|
HELPER_LOG("Bad indicator byte %u", rx_header_buf_[0]);
|
||||||
return APIError::BAD_DATA_PACKET;
|
return APIError::BAD_INDICATOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need at least 3 bytes total (indicator + 2 varint bytes) before trying to parse
|
size_t i = 1;
|
||||||
if (rx_header_buf_pos_ < 3) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// At this point, we have at least 3 bytes total:
|
|
||||||
// - Validated indicator byte (0x00) stored at position 0
|
|
||||||
// - At least 2 bytes in the buffer for the varints
|
|
||||||
// Buffer layout:
|
|
||||||
// [0]: indicator byte (0x00)
|
|
||||||
// [1-3]: Message size varint (variable length)
|
|
||||||
// - 2 bytes would only allow up to 16383, which is less than noise's UINT16_MAX (65535)
|
|
||||||
// - 3 bytes allows up to 2097151, ensuring we support at least as much as noise
|
|
||||||
// [2-5]: Message type varint (variable length)
|
|
||||||
// We now attempt to parse both varints. If either is incomplete,
|
|
||||||
// we'll continue reading more bytes.
|
|
||||||
|
|
||||||
// Skip indicator byte at position 0
|
|
||||||
uint8_t varint_pos = 1;
|
|
||||||
uint32_t consumed = 0;
|
uint32_t consumed = 0;
|
||||||
|
auto msg_size_varint = ProtoVarInt::parse(&rx_header_buf_[i], rx_header_buf_.size() - i, &consumed);
|
||||||
auto msg_size_varint = ProtoVarInt::parse(&rx_header_buf_[varint_pos], rx_header_buf_pos_ - varint_pos, &consumed);
|
|
||||||
if (!msg_size_varint.has_value()) {
|
if (!msg_size_varint.has_value()) {
|
||||||
// not enough data there yet
|
// not enough data there yet
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg_size_varint->as_uint32() > std::numeric_limits<uint16_t>::max()) {
|
i += consumed;
|
||||||
state_ = State::FAILED;
|
rx_header_parsed_len_ = msg_size_varint->as_uint32();
|
||||||
HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u", msg_size_varint->as_uint32(),
|
|
||||||
std::numeric_limits<uint16_t>::max());
|
|
||||||
return APIError::BAD_DATA_PACKET;
|
|
||||||
}
|
|
||||||
rx_header_parsed_len_ = msg_size_varint->as_uint16();
|
|
||||||
|
|
||||||
// Move to next varint position
|
auto msg_type_varint = ProtoVarInt::parse(&rx_header_buf_[i], rx_header_buf_.size() - i, &consumed);
|
||||||
varint_pos += consumed;
|
|
||||||
|
|
||||||
auto msg_type_varint = ProtoVarInt::parse(&rx_header_buf_[varint_pos], rx_header_buf_pos_ - varint_pos, &consumed);
|
|
||||||
if (!msg_type_varint.has_value()) {
|
if (!msg_type_varint.has_value()) {
|
||||||
// not enough data there yet
|
// not enough data there yet
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (msg_type_varint->as_uint32() > std::numeric_limits<uint16_t>::max()) {
|
rx_header_parsed_type_ = msg_type_varint->as_uint32();
|
||||||
state_ = State::FAILED;
|
|
||||||
HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u", msg_type_varint->as_uint32(),
|
|
||||||
std::numeric_limits<uint16_t>::max());
|
|
||||||
return APIError::BAD_DATA_PACKET;
|
|
||||||
}
|
|
||||||
rx_header_parsed_type_ = msg_type_varint->as_uint16();
|
|
||||||
rx_header_parsed_ = true;
|
rx_header_parsed_ = true;
|
||||||
}
|
}
|
||||||
// header reading done
|
// header reading done
|
||||||
@@ -954,8 +854,8 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
|
|
||||||
if (rx_buf_len_ < rx_header_parsed_len_) {
|
if (rx_buf_len_ < rx_header_parsed_len_) {
|
||||||
// more data to read
|
// more data to read
|
||||||
uint16_t to_read = rx_header_parsed_len_ - rx_buf_len_;
|
size_t to_read = rx_header_parsed_len_ - rx_buf_len_;
|
||||||
ssize_t received = this->socket_->read(&rx_buf_[rx_buf_len_], to_read);
|
ssize_t received = socket_->read(&rx_buf_[rx_buf_len_], to_read);
|
||||||
if (received == -1) {
|
if (received == -1) {
|
||||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
@@ -968,8 +868,8 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
HELPER_LOG("Connection closed");
|
HELPER_LOG("Connection closed");
|
||||||
return APIError::CONNECTION_CLOSED;
|
return APIError::CONNECTION_CLOSED;
|
||||||
}
|
}
|
||||||
rx_buf_len_ += static_cast<uint16_t>(received);
|
rx_buf_len_ += received;
|
||||||
if (static_cast<uint16_t>(received) != to_read) {
|
if ((size_t) received != to_read) {
|
||||||
// not all read
|
// not all read
|
||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
}
|
}
|
||||||
@@ -983,10 +883,11 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
// consume msg
|
// consume msg
|
||||||
rx_buf_ = {};
|
rx_buf_ = {};
|
||||||
rx_buf_len_ = 0;
|
rx_buf_len_ = 0;
|
||||||
rx_header_buf_pos_ = 0;
|
rx_header_buf_.clear();
|
||||||
rx_header_parsed_ = false;
|
rx_header_parsed_ = false;
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||||
APIError aerr;
|
APIError aerr;
|
||||||
|
|
||||||
@@ -1014,7 +915,7 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
|||||||
"Bad indicator byte";
|
"Bad indicator byte";
|
||||||
iov[0].iov_base = (void *) msg;
|
iov[0].iov_base = (void *) msg;
|
||||||
iov[0].iov_len = 19;
|
iov[0].iov_len = 19;
|
||||||
this->write_raw_(iov, 1);
|
write_raw_(iov, 1);
|
||||||
}
|
}
|
||||||
return aerr;
|
return aerr;
|
||||||
}
|
}
|
||||||
@@ -1025,89 +926,128 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
|||||||
buffer->type = rx_header_parsed_type_;
|
buffer->type = rx_header_parsed_type_;
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
APIError APIPlaintextFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) {
|
bool APIPlaintextFrameHelper::can_write_without_blocking() { return state_ == State::DATA && tx_buf_.empty(); }
|
||||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
APIError APIPlaintextFrameHelper::write_packet(uint16_t type, const uint8_t *payload, size_t payload_len) {
|
||||||
uint16_t payload_len = static_cast<uint16_t>(raw_buffer->size() - frame_header_padding_);
|
|
||||||
|
|
||||||
// Use write_protobuf_packets with a single packet
|
|
||||||
std::vector<PacketInfo> packets;
|
|
||||||
packets.emplace_back(type, 0, payload_len);
|
|
||||||
|
|
||||||
return write_protobuf_packets(buffer, packets);
|
|
||||||
}
|
|
||||||
|
|
||||||
APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer,
|
|
||||||
const std::vector<PacketInfo> &packets) {
|
|
||||||
if (state_ != State::DATA) {
|
if (state_ != State::DATA) {
|
||||||
return APIError::BAD_STATE;
|
return APIError::BAD_STATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packets.empty()) {
|
std::vector<uint8_t> header;
|
||||||
|
header.push_back(0x00);
|
||||||
|
ProtoVarInt(payload_len).encode(header);
|
||||||
|
ProtoVarInt(type).encode(header);
|
||||||
|
|
||||||
|
struct iovec iov[2];
|
||||||
|
iov[0].iov_base = &header[0];
|
||||||
|
iov[0].iov_len = header.size();
|
||||||
|
if (payload_len == 0) {
|
||||||
|
return write_raw_(iov, 1);
|
||||||
|
}
|
||||||
|
iov[1].iov_base = const_cast<uint8_t *>(payload);
|
||||||
|
iov[1].iov_len = payload_len;
|
||||||
|
|
||||||
|
return write_raw_(iov, 2);
|
||||||
|
}
|
||||||
|
APIError APIPlaintextFrameHelper::try_send_tx_buf_() {
|
||||||
|
// try send from tx_buf
|
||||||
|
while (state_ != State::CLOSED && !tx_buf_.empty()) {
|
||||||
|
ssize_t sent = socket_->write(tx_buf_.data(), tx_buf_.size());
|
||||||
|
if (is_would_block(sent)) {
|
||||||
|
break;
|
||||||
|
} else if (sent == -1) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||||
|
return APIError::SOCKET_WRITE_FAILED;
|
||||||
|
}
|
||||||
|
// TODO: inefficient if multiple packets in txbuf
|
||||||
|
// replace with deque of buffers
|
||||||
|
tx_buf_.erase(tx_buf_.begin(), tx_buf_.begin() + sent);
|
||||||
|
}
|
||||||
|
|
||||||
|
return APIError::OK;
|
||||||
|
}
|
||||||
|
/** Write the data to the socket, or buffer it a write would block
|
||||||
|
*
|
||||||
|
* @param data The data to write
|
||||||
|
* @param len The length of data
|
||||||
|
*/
|
||||||
|
APIError APIPlaintextFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||||
|
if (iovcnt == 0)
|
||||||
|
return APIError::OK;
|
||||||
|
APIError aerr;
|
||||||
|
|
||||||
|
size_t total_write_len = 0;
|
||||||
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
|
#ifdef HELPER_LOG_PACKETS
|
||||||
|
ESP_LOGVV(TAG, "Sending raw: %s",
|
||||||
|
format_hex_pretty(reinterpret_cast<uint8_t *>(iov[i].iov_base), iov[i].iov_len).c_str());
|
||||||
|
#endif
|
||||||
|
total_write_len += iov[i].iov_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tx_buf_.empty()) {
|
||||||
|
// try to empty tx_buf_ first
|
||||||
|
aerr = try_send_tx_buf_();
|
||||||
|
if (aerr != APIError::OK && aerr != APIError::WOULD_BLOCK)
|
||||||
|
return aerr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tx_buf_.empty()) {
|
||||||
|
// tx buf not empty, can't write now because then stream would be inconsistent
|
||||||
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
|
tx_buf_.insert(tx_buf_.end(), reinterpret_cast<uint8_t *>(iov[i].iov_base),
|
||||||
|
reinterpret_cast<uint8_t *>(iov[i].iov_base) + iov[i].iov_len);
|
||||||
|
}
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
ssize_t sent = socket_->writev(iov, iovcnt);
|
||||||
this->reusable_iovs_.clear();
|
if (is_would_block(sent)) {
|
||||||
this->reusable_iovs_.reserve(packets.size());
|
// operation would block, add buffer to tx_buf
|
||||||
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
for (const auto &packet : packets) {
|
tx_buf_.insert(tx_buf_.end(), reinterpret_cast<uint8_t *>(iov[i].iov_base),
|
||||||
uint16_t type = packet.message_type;
|
reinterpret_cast<uint8_t *>(iov[i].iov_base) + iov[i].iov_len);
|
||||||
uint16_t offset = packet.offset;
|
}
|
||||||
uint16_t payload_len = packet.payload_size;
|
return APIError::OK;
|
||||||
|
} else if (sent == -1) {
|
||||||
// Calculate varint sizes for header layout
|
// an error occurred
|
||||||
uint8_t size_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(payload_len));
|
state_ = State::FAILED;
|
||||||
uint8_t type_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(type));
|
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||||
uint8_t total_header_len = 1 + size_varint_len + type_varint_len;
|
return APIError::SOCKET_WRITE_FAILED;
|
||||||
|
} else if ((size_t) sent != total_write_len) {
|
||||||
// Calculate where to start writing the header
|
// partially sent, add end to tx_buf
|
||||||
// The header starts at the latest possible position to minimize unused padding
|
size_t to_consume = sent;
|
||||||
//
|
for (int i = 0; i < iovcnt; i++) {
|
||||||
// Example 1 (small values): total_header_len = 3, header_offset = 6 - 3 = 3
|
if (to_consume >= iov[i].iov_len) {
|
||||||
// [0-2] - Unused padding
|
to_consume -= iov[i].iov_len;
|
||||||
// [3] - 0x00 indicator byte
|
} else {
|
||||||
// [4] - Payload size varint (1 byte, for sizes 0-127)
|
tx_buf_.insert(tx_buf_.end(), reinterpret_cast<uint8_t *>(iov[i].iov_base) + to_consume,
|
||||||
// [5] - Message type varint (1 byte, for types 0-127)
|
reinterpret_cast<uint8_t *>(iov[i].iov_base) + iov[i].iov_len);
|
||||||
// [6...] - Actual payload data
|
to_consume = 0;
|
||||||
//
|
}
|
||||||
// Example 2 (medium values): total_header_len = 4, header_offset = 6 - 4 = 2
|
}
|
||||||
// [0-1] - Unused padding
|
return APIError::OK;
|
||||||
// [2] - 0x00 indicator byte
|
|
||||||
// [3-4] - Payload size varint (2 bytes, for sizes 128-16383)
|
|
||||||
// [5] - Message type varint (1 byte, for types 0-127)
|
|
||||||
// [6...] - Actual payload data
|
|
||||||
//
|
|
||||||
// Example 3 (large values): total_header_len = 6, header_offset = 6 - 6 = 0
|
|
||||||
// [0] - 0x00 indicator byte
|
|
||||||
// [1-3] - Payload size varint (3 bytes, for sizes 16384-2097151)
|
|
||||||
// [4-5] - Message type varint (2 bytes, for types 128-32767)
|
|
||||||
// [6...] - Actual payload data
|
|
||||||
//
|
|
||||||
// The message starts at offset + frame_header_padding_
|
|
||||||
// So we write the header starting at offset + frame_header_padding_ - total_header_len
|
|
||||||
uint8_t *buf_start = raw_buffer->data() + offset;
|
|
||||||
uint32_t header_offset = frame_header_padding_ - total_header_len;
|
|
||||||
|
|
||||||
// Write the plaintext header
|
|
||||||
buf_start[header_offset] = 0x00; // indicator
|
|
||||||
|
|
||||||
// Encode size varint directly into buffer
|
|
||||||
ProtoVarInt(payload_len).encode_to_buffer_unchecked(buf_start + header_offset + 1, size_varint_len);
|
|
||||||
|
|
||||||
// Encode type varint directly into buffer
|
|
||||||
ProtoVarInt(type).encode_to_buffer_unchecked(buf_start + header_offset + 1 + size_varint_len, type_varint_len);
|
|
||||||
|
|
||||||
// Add iovec for this packet (header + payload)
|
|
||||||
struct iovec iov;
|
|
||||||
iov.iov_base = buf_start + header_offset;
|
|
||||||
iov.iov_len = total_header_len + payload_len;
|
|
||||||
this->reusable_iovs_.push_back(iov);
|
|
||||||
}
|
}
|
||||||
|
// fully sent
|
||||||
// Send all packets in one writev call
|
return APIError::OK;
|
||||||
return write_raw_(this->reusable_iovs_.data(), this->reusable_iovs_.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
APIError APIPlaintextFrameHelper::close() {
|
||||||
|
state_ = State::CLOSED;
|
||||||
|
int err = socket_->close();
|
||||||
|
if (err == -1)
|
||||||
|
return APIError::CLOSE_FAILED;
|
||||||
|
return APIError::OK;
|
||||||
|
}
|
||||||
|
APIError APIPlaintextFrameHelper::shutdown(int how) {
|
||||||
|
int err = socket_->shutdown(how);
|
||||||
|
if (err == -1)
|
||||||
|
return APIError::SHUTDOWN_FAILED;
|
||||||
|
if (how == SHUT_RDWR) {
|
||||||
|
state_ = State::CLOSED;
|
||||||
|
}
|
||||||
|
return APIError::OK;
|
||||||
|
}
|
||||||
#endif // USE_API_PLAINTEXT
|
#endif // USE_API_PLAINTEXT
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <limits>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -13,29 +12,22 @@
|
|||||||
|
|
||||||
#include "api_noise_context.h"
|
#include "api_noise_context.h"
|
||||||
#include "esphome/components/socket/socket.h"
|
#include "esphome/components/socket/socket.h"
|
||||||
#include "esphome/core/application.h"
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class ProtoWriteBuffer;
|
|
||||||
|
|
||||||
struct ReadPacketBuffer {
|
struct ReadPacketBuffer {
|
||||||
std::vector<uint8_t> container;
|
std::vector<uint8_t> container;
|
||||||
uint16_t type;
|
uint16_t type;
|
||||||
uint16_t data_offset;
|
size_t data_offset;
|
||||||
uint16_t data_len;
|
size_t data_len;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Packed packet info structure to minimize memory usage
|
struct PacketBuffer {
|
||||||
struct PacketInfo {
|
const std::vector<uint8_t> container;
|
||||||
uint16_t message_type; // 2 bytes
|
uint16_t type;
|
||||||
uint16_t offset; // 2 bytes (sufficient for packet size ~1460 bytes)
|
uint8_t data_offset;
|
||||||
uint16_t payload_size; // 2 bytes (up to 65535 bytes)
|
uint8_t data_len;
|
||||||
uint16_t padding; // 2 byte (for alignment)
|
|
||||||
|
|
||||||
PacketInfo(uint16_t type, uint16_t off, uint16_t size)
|
|
||||||
: message_type(type), offset(off), payload_size(size), padding(0) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class APIError : int {
|
enum class APIError : int {
|
||||||
@@ -68,157 +60,63 @@ const char *api_error_to_str(APIError err);
|
|||||||
|
|
||||||
class APIFrameHelper {
|
class APIFrameHelper {
|
||||||
public:
|
public:
|
||||||
APIFrameHelper() = default;
|
|
||||||
explicit APIFrameHelper(std::unique_ptr<socket::Socket> socket) : socket_owned_(std::move(socket)) {
|
|
||||||
socket_ = socket_owned_.get();
|
|
||||||
}
|
|
||||||
virtual ~APIFrameHelper() = default;
|
virtual ~APIFrameHelper() = default;
|
||||||
virtual APIError init() = 0;
|
virtual APIError init() = 0;
|
||||||
virtual APIError loop() = 0;
|
virtual APIError loop() = 0;
|
||||||
virtual APIError read_packet(ReadPacketBuffer *buffer) = 0;
|
virtual APIError read_packet(ReadPacketBuffer *buffer) = 0;
|
||||||
bool can_write_without_blocking() { return state_ == State::DATA && tx_buf_.empty(); }
|
virtual bool can_write_without_blocking() = 0;
|
||||||
std::string getpeername() { return socket_->getpeername(); }
|
virtual APIError write_packet(uint16_t type, const uint8_t *data, size_t len) = 0;
|
||||||
int getpeername(struct sockaddr *addr, socklen_t *addrlen) { return socket_->getpeername(addr, addrlen); }
|
virtual std::string getpeername() = 0;
|
||||||
APIError close() {
|
virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen) = 0;
|
||||||
state_ = State::CLOSED;
|
virtual APIError close() = 0;
|
||||||
int err = this->socket_->close();
|
virtual APIError shutdown(int how) = 0;
|
||||||
if (err == -1)
|
|
||||||
return APIError::CLOSE_FAILED;
|
|
||||||
return APIError::OK;
|
|
||||||
}
|
|
||||||
APIError shutdown(int how) {
|
|
||||||
int err = this->socket_->shutdown(how);
|
|
||||||
if (err == -1)
|
|
||||||
return APIError::SHUTDOWN_FAILED;
|
|
||||||
if (how == SHUT_RDWR) {
|
|
||||||
state_ = State::CLOSED;
|
|
||||||
}
|
|
||||||
return APIError::OK;
|
|
||||||
}
|
|
||||||
// Give this helper a name for logging
|
// Give this helper a name for logging
|
||||||
void set_log_info(std::string info) { info_ = std::move(info); }
|
virtual void set_log_info(std::string info) = 0;
|
||||||
virtual APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) = 0;
|
|
||||||
// Write multiple protobuf packets in a single operation
|
|
||||||
// packets contains (message_type, offset, length) for each message in the buffer
|
|
||||||
// The buffer contains all messages with appropriate padding before each
|
|
||||||
virtual APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) = 0;
|
|
||||||
// Get the frame header padding required by this protocol
|
|
||||||
virtual uint8_t frame_header_padding() = 0;
|
|
||||||
// Get the frame footer size required by this protocol
|
|
||||||
virtual uint8_t frame_footer_size() = 0;
|
|
||||||
// Check if socket has data ready to read
|
|
||||||
bool is_socket_ready() const { return socket_ != nullptr && socket_->ready(); }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
// Struct for holding parsed frame data
|
|
||||||
struct ParsedFrame {
|
|
||||||
std::vector<uint8_t> msg;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Buffer containing data to be sent
|
|
||||||
struct SendBuffer {
|
|
||||||
std::vector<uint8_t> data;
|
|
||||||
uint16_t offset{0}; // Current offset within the buffer (uint16_t to reduce memory usage)
|
|
||||||
|
|
||||||
// Using uint16_t reduces memory usage since ESPHome API messages are limited to UINT16_MAX (65535) bytes
|
|
||||||
uint16_t remaining() const { return static_cast<uint16_t>(data.size()) - offset; }
|
|
||||||
const uint8_t *current_data() const { return data.data() + offset; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Queue of data buffers to be sent
|
|
||||||
std::deque<SendBuffer> tx_buf_;
|
|
||||||
|
|
||||||
// Common state enum for all frame helpers
|
|
||||||
// Note: Not all states are used by all implementations
|
|
||||||
// - INITIALIZE: Used by both Noise and Plaintext
|
|
||||||
// - CLIENT_HELLO, SERVER_HELLO, HANDSHAKE: Only used by Noise protocol
|
|
||||||
// - DATA: Used by both Noise and Plaintext
|
|
||||||
// - CLOSED: Used by both Noise and Plaintext
|
|
||||||
// - FAILED: Used by both Noise and Plaintext
|
|
||||||
// - EXPLICIT_REJECT: Only used by Noise protocol
|
|
||||||
enum class State {
|
|
||||||
INITIALIZE = 1,
|
|
||||||
CLIENT_HELLO = 2, // Noise only
|
|
||||||
SERVER_HELLO = 3, // Noise only
|
|
||||||
HANDSHAKE = 4, // Noise only
|
|
||||||
DATA = 5,
|
|
||||||
CLOSED = 6,
|
|
||||||
FAILED = 7,
|
|
||||||
EXPLICIT_REJECT = 8, // Noise only
|
|
||||||
};
|
|
||||||
|
|
||||||
// Current state of the frame helper
|
|
||||||
State state_{State::INITIALIZE};
|
|
||||||
|
|
||||||
// Helper name for logging
|
|
||||||
std::string info_;
|
|
||||||
|
|
||||||
// Socket for communication
|
|
||||||
socket::Socket *socket_{nullptr};
|
|
||||||
std::unique_ptr<socket::Socket> socket_owned_;
|
|
||||||
|
|
||||||
// Common implementation for writing raw data to socket
|
|
||||||
APIError write_raw_(const struct iovec *iov, int iovcnt);
|
|
||||||
|
|
||||||
// Try to send data from the tx buffer
|
|
||||||
APIError try_send_tx_buf_();
|
|
||||||
|
|
||||||
// Helper method to buffer data from IOVs
|
|
||||||
void buffer_data_from_iov_(const struct iovec *iov, int iovcnt, uint16_t total_write_len);
|
|
||||||
template<typename StateEnum>
|
|
||||||
APIError write_raw_(const struct iovec *iov, int iovcnt, socket::Socket *socket, std::vector<uint8_t> &tx_buf,
|
|
||||||
const std::string &info, StateEnum &state, StateEnum failed_state);
|
|
||||||
|
|
||||||
uint8_t frame_header_padding_{0};
|
|
||||||
uint8_t frame_footer_size_{0};
|
|
||||||
|
|
||||||
// Reusable IOV array for write_protobuf_packets to avoid repeated allocations
|
|
||||||
std::vector<struct iovec> reusable_iovs_;
|
|
||||||
|
|
||||||
// Receive buffer for reading frame data
|
|
||||||
std::vector<uint8_t> rx_buf_;
|
|
||||||
uint16_t rx_buf_len_ = 0;
|
|
||||||
|
|
||||||
// Common initialization for both plaintext and noise protocols
|
|
||||||
APIError init_common_();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
class APINoiseFrameHelper : public APIFrameHelper {
|
class APINoiseFrameHelper : public APIFrameHelper {
|
||||||
public:
|
public:
|
||||||
APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, std::shared_ptr<APINoiseContext> ctx)
|
APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, std::shared_ptr<APINoiseContext> ctx)
|
||||||
: APIFrameHelper(std::move(socket)), ctx_(std::move(ctx)) {
|
: socket_(std::move(socket)), ctx_(std::move(std::move(ctx))) {}
|
||||||
// Noise header structure:
|
|
||||||
// Pos 0: indicator (0x01)
|
|
||||||
// Pos 1-2: encrypted payload size (16-bit big-endian)
|
|
||||||
// Pos 3-6: encrypted type (16-bit) + data_len (16-bit)
|
|
||||||
// Pos 7+: actual payload data
|
|
||||||
frame_header_padding_ = 7;
|
|
||||||
}
|
|
||||||
~APINoiseFrameHelper() override;
|
~APINoiseFrameHelper() override;
|
||||||
APIError init() override;
|
APIError init() override;
|
||||||
APIError loop() override;
|
APIError loop() override;
|
||||||
APIError read_packet(ReadPacketBuffer *buffer) override;
|
APIError read_packet(ReadPacketBuffer *buffer) override;
|
||||||
APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override;
|
bool can_write_without_blocking() override;
|
||||||
APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) override;
|
APIError write_packet(uint16_t type, const uint8_t *payload, size_t len) override;
|
||||||
// Get the frame header padding required by this protocol
|
std::string getpeername() override { return this->socket_->getpeername(); }
|
||||||
uint8_t frame_header_padding() override { return frame_header_padding_; }
|
int getpeername(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||||
// Get the frame footer size required by this protocol
|
return this->socket_->getpeername(addr, addrlen);
|
||||||
uint8_t frame_footer_size() override { return frame_footer_size_; }
|
}
|
||||||
|
APIError close() override;
|
||||||
|
APIError shutdown(int how) override;
|
||||||
|
// Give this helper a name for logging
|
||||||
|
void set_log_info(std::string info) override { info_ = std::move(info); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
struct ParsedFrame {
|
||||||
|
std::vector<uint8_t> msg;
|
||||||
|
};
|
||||||
|
|
||||||
APIError state_action_();
|
APIError state_action_();
|
||||||
APIError try_read_frame_(ParsedFrame *frame);
|
APIError try_read_frame_(ParsedFrame *frame);
|
||||||
APIError write_frame_(const uint8_t *data, uint16_t len);
|
APIError try_send_tx_buf_();
|
||||||
|
APIError write_frame_(const uint8_t *data, size_t len);
|
||||||
|
APIError write_raw_(const struct iovec *iov, int iovcnt);
|
||||||
APIError init_handshake_();
|
APIError init_handshake_();
|
||||||
APIError check_handshake_finished_();
|
APIError check_handshake_finished_();
|
||||||
void send_explicit_handshake_reject_(const std::string &reason);
|
void send_explicit_handshake_reject_(const std::string &reason);
|
||||||
// Fixed-size header buffer for noise protocol:
|
|
||||||
// 1 byte for indicator + 2 bytes for message size (16-bit value, not varint)
|
|
||||||
// Note: Maximum message size is UINT16_MAX (65535), with a limit of 128 bytes during handshake phase
|
|
||||||
uint8_t rx_header_buf_[3];
|
|
||||||
uint8_t rx_header_buf_len_ = 0;
|
|
||||||
|
|
||||||
|
std::unique_ptr<socket::Socket> socket_;
|
||||||
|
|
||||||
|
std::string info_;
|
||||||
|
uint8_t rx_header_buf_[3];
|
||||||
|
size_t rx_header_buf_len_ = 0;
|
||||||
|
std::vector<uint8_t> rx_buf_;
|
||||||
|
size_t rx_buf_len_ = 0;
|
||||||
|
|
||||||
|
std::vector<uint8_t> tx_buf_;
|
||||||
std::vector<uint8_t> prologue_;
|
std::vector<uint8_t> prologue_;
|
||||||
|
|
||||||
std::shared_ptr<APINoiseContext> ctx_;
|
std::shared_ptr<APINoiseContext> ctx_;
|
||||||
@@ -226,45 +124,67 @@ class APINoiseFrameHelper : public APIFrameHelper {
|
|||||||
NoiseCipherState *send_cipher_{nullptr};
|
NoiseCipherState *send_cipher_{nullptr};
|
||||||
NoiseCipherState *recv_cipher_{nullptr};
|
NoiseCipherState *recv_cipher_{nullptr};
|
||||||
NoiseProtocolId nid_;
|
NoiseProtocolId nid_;
|
||||||
|
|
||||||
|
enum class State {
|
||||||
|
INITIALIZE = 1,
|
||||||
|
CLIENT_HELLO = 2,
|
||||||
|
SERVER_HELLO = 3,
|
||||||
|
HANDSHAKE = 4,
|
||||||
|
DATA = 5,
|
||||||
|
CLOSED = 6,
|
||||||
|
FAILED = 7,
|
||||||
|
EXPLICIT_REJECT = 8,
|
||||||
|
} state_ = State::INITIALIZE;
|
||||||
};
|
};
|
||||||
#endif // USE_API_NOISE
|
#endif // USE_API_NOISE
|
||||||
|
|
||||||
#ifdef USE_API_PLAINTEXT
|
#ifdef USE_API_PLAINTEXT
|
||||||
class APIPlaintextFrameHelper : public APIFrameHelper {
|
class APIPlaintextFrameHelper : public APIFrameHelper {
|
||||||
public:
|
public:
|
||||||
APIPlaintextFrameHelper(std::unique_ptr<socket::Socket> socket) : APIFrameHelper(std::move(socket)) {
|
APIPlaintextFrameHelper(std::unique_ptr<socket::Socket> socket) : socket_(std::move(socket)) {}
|
||||||
// Plaintext header structure (worst case):
|
|
||||||
// Pos 0: indicator (0x00)
|
|
||||||
// Pos 1-3: payload size varint (up to 3 bytes)
|
|
||||||
// Pos 4-5: message type varint (up to 2 bytes)
|
|
||||||
// Pos 6+: actual payload data
|
|
||||||
frame_header_padding_ = 6;
|
|
||||||
}
|
|
||||||
~APIPlaintextFrameHelper() override = default;
|
~APIPlaintextFrameHelper() override = default;
|
||||||
APIError init() override;
|
APIError init() override;
|
||||||
APIError loop() override;
|
APIError loop() override;
|
||||||
APIError read_packet(ReadPacketBuffer *buffer) override;
|
APIError read_packet(ReadPacketBuffer *buffer) override;
|
||||||
APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override;
|
bool can_write_without_blocking() override;
|
||||||
APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) override;
|
APIError write_packet(uint16_t type, const uint8_t *payload, size_t len) override;
|
||||||
uint8_t frame_header_padding() override { return frame_header_padding_; }
|
std::string getpeername() override { return this->socket_->getpeername(); }
|
||||||
// Get the frame footer size required by this protocol
|
int getpeername(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||||
uint8_t frame_footer_size() override { return frame_footer_size_; }
|
return this->socket_->getpeername(addr, addrlen);
|
||||||
|
}
|
||||||
|
APIError close() override;
|
||||||
|
APIError shutdown(int how) override;
|
||||||
|
// Give this helper a name for logging
|
||||||
|
void set_log_info(std::string info) override { info_ = std::move(info); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
struct ParsedFrame {
|
||||||
|
std::vector<uint8_t> msg;
|
||||||
|
};
|
||||||
|
|
||||||
APIError try_read_frame_(ParsedFrame *frame);
|
APIError try_read_frame_(ParsedFrame *frame);
|
||||||
// Fixed-size header buffer for plaintext protocol:
|
APIError try_send_tx_buf_();
|
||||||
// We now store the indicator byte + the two varints.
|
APIError write_raw_(const struct iovec *iov, int iovcnt);
|
||||||
// To match noise protocol's maximum message size (UINT16_MAX = 65535), we need:
|
|
||||||
// 1 byte for indicator + 3 bytes for message size varint (supports up to 2097151) + 2 bytes for message type varint
|
std::unique_ptr<socket::Socket> socket_;
|
||||||
//
|
|
||||||
// While varints could theoretically be up to 10 bytes each for 64-bit values,
|
std::string info_;
|
||||||
// attempting to process messages with headers that large would likely crash the
|
std::vector<uint8_t> rx_header_buf_;
|
||||||
// ESP32 due to memory constraints.
|
|
||||||
uint8_t rx_header_buf_[6]; // 1 byte indicator + 5 bytes for varints (3 for size + 2 for type)
|
|
||||||
uint8_t rx_header_buf_pos_ = 0;
|
|
||||||
bool rx_header_parsed_ = false;
|
bool rx_header_parsed_ = false;
|
||||||
uint16_t rx_header_parsed_type_ = 0;
|
uint32_t rx_header_parsed_type_ = 0;
|
||||||
uint16_t rx_header_parsed_len_ = 0;
|
uint32_t rx_header_parsed_len_ = 0;
|
||||||
|
|
||||||
|
std::vector<uint8_t> rx_buf_;
|
||||||
|
size_t rx_buf_len_ = 0;
|
||||||
|
|
||||||
|
std::vector<uint8_t> tx_buf_;
|
||||||
|
|
||||||
|
enum class State {
|
||||||
|
INITIALIZE = 1,
|
||||||
|
DATA = 2,
|
||||||
|
CLOSED = 3,
|
||||||
|
FAILED = 4,
|
||||||
|
} state_ = State::INITIALIZE;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -21,5 +21,4 @@ extend google.protobuf.MessageOptions {
|
|||||||
optional string ifdef = 1038;
|
optional string ifdef = 1038;
|
||||||
optional bool log = 1039 [default=true];
|
optional bool log = 1039 [default=true];
|
||||||
optional bool no_delay = 1040 [default=false];
|
optional bool no_delay = 1040 [default=false];
|
||||||
optional string base_class = 1041;
|
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
|||||||
// This file was automatically generated with a tool.
|
// This file was automatically generated with a tool.
|
||||||
// See script/api_protobuf/api_protobuf.py
|
// See scripts/api_protobuf/api_protobuf.py
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "api_pb2.h"
|
#include "api_pb2.h"
|
||||||
@@ -10,94 +10,153 @@ namespace api {
|
|||||||
|
|
||||||
class APIServerConnectionBase : public ProtoService {
|
class APIServerConnectionBase : public ProtoService {
|
||||||
public:
|
public:
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
|
||||||
protected:
|
|
||||||
void log_send_message_(const char *name, const std::string &dump);
|
|
||||||
|
|
||||||
public:
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template<typename T> bool send_message(const T &msg) {
|
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
|
||||||
this->log_send_message_(T::message_name(), msg.dump());
|
|
||||||
#endif
|
|
||||||
return this->send_message_(msg, T::MESSAGE_TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void on_hello_request(const HelloRequest &value){};
|
virtual void on_hello_request(const HelloRequest &value){};
|
||||||
|
bool send_hello_response(const HelloResponse &msg);
|
||||||
virtual void on_connect_request(const ConnectRequest &value){};
|
virtual void on_connect_request(const ConnectRequest &value){};
|
||||||
|
bool send_connect_response(const ConnectResponse &msg);
|
||||||
|
bool send_disconnect_request(const DisconnectRequest &msg);
|
||||||
virtual void on_disconnect_request(const DisconnectRequest &value){};
|
virtual void on_disconnect_request(const DisconnectRequest &value){};
|
||||||
|
bool send_disconnect_response(const DisconnectResponse &msg);
|
||||||
virtual void on_disconnect_response(const DisconnectResponse &value){};
|
virtual void on_disconnect_response(const DisconnectResponse &value){};
|
||||||
|
bool send_ping_request(const PingRequest &msg);
|
||||||
virtual void on_ping_request(const PingRequest &value){};
|
virtual void on_ping_request(const PingRequest &value){};
|
||||||
|
bool send_ping_response(const PingResponse &msg);
|
||||||
virtual void on_ping_response(const PingResponse &value){};
|
virtual void on_ping_response(const PingResponse &value){};
|
||||||
virtual void on_device_info_request(const DeviceInfoRequest &value){};
|
virtual void on_device_info_request(const DeviceInfoRequest &value){};
|
||||||
|
bool send_device_info_response(const DeviceInfoResponse &msg);
|
||||||
virtual void on_list_entities_request(const ListEntitiesRequest &value){};
|
virtual void on_list_entities_request(const ListEntitiesRequest &value){};
|
||||||
|
bool send_list_entities_done_response(const ListEntitiesDoneResponse &msg);
|
||||||
virtual void on_subscribe_states_request(const SubscribeStatesRequest &value){};
|
virtual void on_subscribe_states_request(const SubscribeStatesRequest &value){};
|
||||||
|
#ifdef USE_BINARY_SENSOR
|
||||||
|
bool send_list_entities_binary_sensor_response(const ListEntitiesBinarySensorResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BINARY_SENSOR
|
||||||
|
bool send_binary_sensor_state_response(const BinarySensorStateResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_COVER
|
||||||
|
bool send_list_entities_cover_response(const ListEntitiesCoverResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_COVER
|
||||||
|
bool send_cover_state_response(const CoverStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_COVER
|
#ifdef USE_COVER
|
||||||
virtual void on_cover_command_request(const CoverCommandRequest &value){};
|
virtual void on_cover_command_request(const CoverCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_FAN
|
||||||
|
bool send_list_entities_fan_response(const ListEntitiesFanResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_FAN
|
||||||
|
bool send_fan_state_response(const FanStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_FAN
|
#ifdef USE_FAN
|
||||||
virtual void on_fan_command_request(const FanCommandRequest &value){};
|
virtual void on_fan_command_request(const FanCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_LIGHT
|
||||||
|
bool send_list_entities_light_response(const ListEntitiesLightResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_LIGHT
|
||||||
|
bool send_light_state_response(const LightStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_LIGHT
|
#ifdef USE_LIGHT
|
||||||
virtual void on_light_command_request(const LightCommandRequest &value){};
|
virtual void on_light_command_request(const LightCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_SENSOR
|
||||||
|
bool send_list_entities_sensor_response(const ListEntitiesSensorResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_SENSOR
|
||||||
|
bool send_sensor_state_response(const SensorStateResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_SWITCH
|
||||||
|
bool send_list_entities_switch_response(const ListEntitiesSwitchResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_SWITCH
|
||||||
|
bool send_switch_state_response(const SwitchStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_SWITCH
|
#ifdef USE_SWITCH
|
||||||
virtual void on_switch_command_request(const SwitchCommandRequest &value){};
|
virtual void on_switch_command_request(const SwitchCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_TEXT_SENSOR
|
||||||
|
bool send_list_entities_text_sensor_response(const ListEntitiesTextSensorResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_TEXT_SENSOR
|
||||||
|
bool send_text_sensor_state_response(const TextSensorStateResponse &msg);
|
||||||
|
#endif
|
||||||
virtual void on_subscribe_logs_request(const SubscribeLogsRequest &value){};
|
virtual void on_subscribe_logs_request(const SubscribeLogsRequest &value){};
|
||||||
|
bool send_subscribe_logs_response(const SubscribeLogsResponse &msg);
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
virtual void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &value){};
|
virtual void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_API_NOISE
|
||||||
|
bool send_noise_encryption_set_key_response(const NoiseEncryptionSetKeyResponse &msg);
|
||||||
|
#endif
|
||||||
virtual void on_subscribe_homeassistant_services_request(const SubscribeHomeassistantServicesRequest &value){};
|
virtual void on_subscribe_homeassistant_services_request(const SubscribeHomeassistantServicesRequest &value){};
|
||||||
|
bool send_homeassistant_service_response(const HomeassistantServiceResponse &msg);
|
||||||
virtual void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &value){};
|
virtual void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &value){};
|
||||||
|
bool send_subscribe_home_assistant_state_response(const SubscribeHomeAssistantStateResponse &msg);
|
||||||
virtual void on_home_assistant_state_response(const HomeAssistantStateResponse &value){};
|
virtual void on_home_assistant_state_response(const HomeAssistantStateResponse &value){};
|
||||||
|
bool send_get_time_request(const GetTimeRequest &msg);
|
||||||
virtual void on_get_time_request(const GetTimeRequest &value){};
|
virtual void on_get_time_request(const GetTimeRequest &value){};
|
||||||
|
bool send_get_time_response(const GetTimeResponse &msg);
|
||||||
virtual void on_get_time_response(const GetTimeResponse &value){};
|
virtual void on_get_time_response(const GetTimeResponse &value){};
|
||||||
|
bool send_list_entities_services_response(const ListEntitiesServicesResponse &msg);
|
||||||
virtual void on_execute_service_request(const ExecuteServiceRequest &value){};
|
virtual void on_execute_service_request(const ExecuteServiceRequest &value){};
|
||||||
|
#ifdef USE_ESP32_CAMERA
|
||||||
|
bool send_list_entities_camera_response(const ListEntitiesCameraResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_ESP32_CAMERA
|
||||||
|
bool send_camera_image_response(const CameraImageResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_ESP32_CAMERA
|
#ifdef USE_ESP32_CAMERA
|
||||||
virtual void on_camera_image_request(const CameraImageRequest &value){};
|
virtual void on_camera_image_request(const CameraImageRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_CLIMATE
|
||||||
|
bool send_list_entities_climate_response(const ListEntitiesClimateResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_CLIMATE
|
||||||
|
bool send_climate_state_response(const ClimateStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_CLIMATE
|
#ifdef USE_CLIMATE
|
||||||
virtual void on_climate_command_request(const ClimateCommandRequest &value){};
|
virtual void on_climate_command_request(const ClimateCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_NUMBER
|
||||||
|
bool send_list_entities_number_response(const ListEntitiesNumberResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_NUMBER
|
||||||
|
bool send_number_state_response(const NumberStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_NUMBER
|
#ifdef USE_NUMBER
|
||||||
virtual void on_number_command_request(const NumberCommandRequest &value){};
|
virtual void on_number_command_request(const NumberCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_SELECT
|
||||||
|
bool send_list_entities_select_response(const ListEntitiesSelectResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_SELECT
|
||||||
|
bool send_select_state_response(const SelectStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
virtual void on_select_command_request(const SelectCommandRequest &value){};
|
virtual void on_select_command_request(const SelectCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_LOCK
|
||||||
#ifdef USE_SIREN
|
bool send_list_entities_lock_response(const ListEntitiesLockResponse &msg);
|
||||||
virtual void on_siren_command_request(const SirenCommandRequest &value){};
|
#endif
|
||||||
|
#ifdef USE_LOCK
|
||||||
|
bool send_lock_state_response(const LockStateResponse &msg);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_LOCK
|
#ifdef USE_LOCK
|
||||||
virtual void on_lock_command_request(const LockCommandRequest &value){};
|
virtual void on_lock_command_request(const LockCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BUTTON
|
||||||
|
bool send_list_entities_button_response(const ListEntitiesButtonResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BUTTON
|
#ifdef USE_BUTTON
|
||||||
virtual void on_button_command_request(const ButtonCommandRequest &value){};
|
virtual void on_button_command_request(const ButtonCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_MEDIA_PLAYER
|
||||||
|
bool send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_MEDIA_PLAYER
|
||||||
|
bool send_media_player_state_response(const MediaPlayerStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_MEDIA_PLAYER
|
#ifdef USE_MEDIA_PLAYER
|
||||||
virtual void on_media_player_command_request(const MediaPlayerCommandRequest &value){};
|
virtual void on_media_player_command_request(const MediaPlayerCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
@@ -105,19 +164,33 @@ class APIServerConnectionBase : public ProtoService {
|
|||||||
virtual void on_subscribe_bluetooth_le_advertisements_request(
|
virtual void on_subscribe_bluetooth_le_advertisements_request(
|
||||||
const SubscribeBluetoothLEAdvertisementsRequest &value){};
|
const SubscribeBluetoothLEAdvertisementsRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_le_advertisement_response(const BluetoothLEAdvertisementResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_le_raw_advertisements_response(const BluetoothLERawAdvertisementsResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_bluetooth_device_request(const BluetoothDeviceRequest &value){};
|
virtual void on_bluetooth_device_request(const BluetoothDeviceRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_device_connection_response(const BluetoothDeviceConnectionResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &value){};
|
virtual void on_bluetooth_gatt_get_services_request(const BluetoothGATTGetServicesRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_get_services_response(const BluetoothGATTGetServicesResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_get_services_done_response(const BluetoothGATTGetServicesDoneResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &value){};
|
virtual void on_bluetooth_gatt_read_request(const BluetoothGATTReadRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_read_response(const BluetoothGATTReadResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &value){};
|
virtual void on_bluetooth_gatt_write_request(const BluetoothGATTWriteRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
@@ -130,23 +203,49 @@ class APIServerConnectionBase : public ProtoService {
|
|||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &value){};
|
virtual void on_bluetooth_gatt_notify_request(const BluetoothGATTNotifyRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_notify_data_response(const BluetoothGATTNotifyDataResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_subscribe_bluetooth_connections_free_request(const SubscribeBluetoothConnectionsFreeRequest &value){};
|
virtual void on_subscribe_bluetooth_connections_free_request(const SubscribeBluetoothConnectionsFreeRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_connections_free_response(const BluetoothConnectionsFreeResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_error_response(const BluetoothGATTErrorResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_write_response(const BluetoothGATTWriteResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_gatt_notify_response(const BluetoothGATTNotifyResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_device_pairing_response(const BluetoothDevicePairingResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_device_unpairing_response(const BluetoothDeviceUnpairingResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_unsubscribe_bluetooth_le_advertisements_request(
|
virtual void on_unsubscribe_bluetooth_le_advertisements_request(
|
||||||
const UnsubscribeBluetoothLEAdvertisementsRequest &value){};
|
const UnsubscribeBluetoothLEAdvertisementsRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_device_clear_cache_response(const BluetoothDeviceClearCacheResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
|
bool send_bluetooth_scanner_state_response(const BluetoothScannerStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &value){};
|
virtual void on_bluetooth_scanner_set_mode_request(const BluetoothScannerSetModeRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
virtual void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &value){};
|
virtual void on_subscribe_voice_assistant_request(const SubscribeVoiceAssistantRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
|
bool send_voice_assistant_request(const VoiceAssistantRequest &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
virtual void on_voice_assistant_response(const VoiceAssistantResponse &value){};
|
virtual void on_voice_assistant_response(const VoiceAssistantResponse &value){};
|
||||||
#endif
|
#endif
|
||||||
@@ -154,6 +253,7 @@ class APIServerConnectionBase : public ProtoService {
|
|||||||
virtual void on_voice_assistant_event_response(const VoiceAssistantEventResponse &value){};
|
virtual void on_voice_assistant_event_response(const VoiceAssistantEventResponse &value){};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
|
bool send_voice_assistant_audio(const VoiceAssistantAudio &msg);
|
||||||
virtual void on_voice_assistant_audio(const VoiceAssistantAudio &value){};
|
virtual void on_voice_assistant_audio(const VoiceAssistantAudio &value){};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
@@ -162,39 +262,84 @@ class APIServerConnectionBase : public ProtoService {
|
|||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
virtual void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &value){};
|
virtual void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
|
bool send_voice_assistant_announce_finished(const VoiceAssistantAnnounceFinished &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
virtual void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &value){};
|
virtual void on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
|
bool send_voice_assistant_configuration_response(const VoiceAssistantConfigurationResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_VOICE_ASSISTANT
|
#ifdef USE_VOICE_ASSISTANT
|
||||||
virtual void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &value){};
|
virtual void on_voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_ALARM_CONTROL_PANEL
|
||||||
|
bool send_list_entities_alarm_control_panel_response(const ListEntitiesAlarmControlPanelResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_ALARM_CONTROL_PANEL
|
||||||
|
bool send_alarm_control_panel_state_response(const AlarmControlPanelStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_ALARM_CONTROL_PANEL
|
#ifdef USE_ALARM_CONTROL_PANEL
|
||||||
virtual void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &value){};
|
virtual void on_alarm_control_panel_command_request(const AlarmControlPanelCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_TEXT
|
||||||
|
bool send_list_entities_text_response(const ListEntitiesTextResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_TEXT
|
||||||
|
bool send_text_state_response(const TextStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_TEXT
|
#ifdef USE_TEXT
|
||||||
virtual void on_text_command_request(const TextCommandRequest &value){};
|
virtual void on_text_command_request(const TextCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATE
|
||||||
|
bool send_list_entities_date_response(const ListEntitiesDateResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATE
|
||||||
|
bool send_date_state_response(const DateStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_DATETIME_DATE
|
#ifdef USE_DATETIME_DATE
|
||||||
virtual void on_date_command_request(const DateCommandRequest &value){};
|
virtual void on_date_command_request(const DateCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_DATETIME_TIME
|
||||||
|
bool send_list_entities_time_response(const ListEntitiesTimeResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_DATETIME_TIME
|
||||||
|
bool send_time_state_response(const TimeStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_DATETIME_TIME
|
#ifdef USE_DATETIME_TIME
|
||||||
virtual void on_time_command_request(const TimeCommandRequest &value){};
|
virtual void on_time_command_request(const TimeCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_EVENT
|
||||||
|
bool send_list_entities_event_response(const ListEntitiesEventResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_EVENT
|
||||||
|
bool send_event_response(const EventResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_VALVE
|
||||||
|
bool send_list_entities_valve_response(const ListEntitiesValveResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_VALVE
|
||||||
|
bool send_valve_state_response(const ValveStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_VALVE
|
#ifdef USE_VALVE
|
||||||
virtual void on_valve_command_request(const ValveCommandRequest &value){};
|
virtual void on_valve_command_request(const ValveCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATETIME
|
||||||
|
bool send_list_entities_date_time_response(const ListEntitiesDateTimeResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATETIME
|
||||||
|
bool send_date_time_state_response(const DateTimeStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_DATETIME_DATETIME
|
#ifdef USE_DATETIME_DATETIME
|
||||||
virtual void on_date_time_command_request(const DateTimeCommandRequest &value){};
|
virtual void on_date_time_command_request(const DateTimeCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_UPDATE
|
||||||
|
bool send_list_entities_update_response(const ListEntitiesUpdateResponse &msg);
|
||||||
|
#endif
|
||||||
|
#ifdef USE_UPDATE
|
||||||
|
bool send_update_state_response(const UpdateStateResponse &msg);
|
||||||
|
#endif
|
||||||
#ifdef USE_UPDATE
|
#ifdef USE_UPDATE
|
||||||
virtual void on_update_command_request(const UpdateCommandRequest &value){};
|
virtual void on_update_command_request(const UpdateCommandRequest &value){};
|
||||||
#endif
|
#endif
|
||||||
@@ -219,60 +364,57 @@ class APIServerConnection : public APIServerConnectionBase {
|
|||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
virtual NoiseEncryptionSetKeyResponse noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) = 0;
|
virtual NoiseEncryptionSetKeyResponse noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_BUTTON
|
|
||||||
virtual void button_command(const ButtonCommandRequest &msg) = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_ESP32_CAMERA
|
|
||||||
virtual void camera_image(const CameraImageRequest &msg) = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_CLIMATE
|
|
||||||
virtual void climate_command(const ClimateCommandRequest &msg) = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_COVER
|
#ifdef USE_COVER
|
||||||
virtual void cover_command(const CoverCommandRequest &msg) = 0;
|
virtual void cover_command(const CoverCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_DATE
|
|
||||||
virtual void date_command(const DateCommandRequest &msg) = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_DATETIME_DATETIME
|
|
||||||
virtual void datetime_command(const DateTimeCommandRequest &msg) = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_FAN
|
#ifdef USE_FAN
|
||||||
virtual void fan_command(const FanCommandRequest &msg) = 0;
|
virtual void fan_command(const FanCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LIGHT
|
#ifdef USE_LIGHT
|
||||||
virtual void light_command(const LightCommandRequest &msg) = 0;
|
virtual void light_command(const LightCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LOCK
|
#ifdef USE_SWITCH
|
||||||
virtual void lock_command(const LockCommandRequest &msg) = 0;
|
virtual void switch_command(const SwitchCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_MEDIA_PLAYER
|
#ifdef USE_ESP32_CAMERA
|
||||||
virtual void media_player_command(const MediaPlayerCommandRequest &msg) = 0;
|
virtual void camera_image(const CameraImageRequest &msg) = 0;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_CLIMATE
|
||||||
|
virtual void climate_command(const ClimateCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_NUMBER
|
#ifdef USE_NUMBER
|
||||||
virtual void number_command(const NumberCommandRequest &msg) = 0;
|
virtual void number_command(const NumberCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_TEXT
|
||||||
|
virtual void text_command(const TextCommandRequest &msg) = 0;
|
||||||
|
#endif
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
virtual void select_command(const SelectCommandRequest &msg) = 0;
|
virtual void select_command(const SelectCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SIREN
|
#ifdef USE_BUTTON
|
||||||
virtual void siren_command(const SirenCommandRequest &msg) = 0;
|
virtual void button_command(const ButtonCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SWITCH
|
#ifdef USE_LOCK
|
||||||
virtual void switch_command(const SwitchCommandRequest &msg) = 0;
|
virtual void lock_command(const LockCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT
|
#ifdef USE_VALVE
|
||||||
virtual void text_command(const TextCommandRequest &msg) = 0;
|
virtual void valve_command(const ValveCommandRequest &msg) = 0;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_MEDIA_PLAYER
|
||||||
|
virtual void media_player_command(const MediaPlayerCommandRequest &msg) = 0;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATE
|
||||||
|
virtual void date_command(const DateCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_TIME
|
#ifdef USE_DATETIME_TIME
|
||||||
virtual void time_command(const TimeCommandRequest &msg) = 0;
|
virtual void time_command(const TimeCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATETIME
|
||||||
|
virtual void datetime_command(const DateTimeCommandRequest &msg) = 0;
|
||||||
|
#endif
|
||||||
#ifdef USE_UPDATE
|
#ifdef USE_UPDATE
|
||||||
virtual void update_command(const UpdateCommandRequest &msg) = 0;
|
virtual void update_command(const UpdateCommandRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VALVE
|
|
||||||
virtual void valve_command(const ValveCommandRequest &msg) = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
virtual void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) = 0;
|
virtual void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) = 0;
|
||||||
#endif
|
#endif
|
||||||
@@ -336,60 +478,57 @@ class APIServerConnection : public APIServerConnectionBase {
|
|||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) override;
|
void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_BUTTON
|
|
||||||
void on_button_command_request(const ButtonCommandRequest &msg) override;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_ESP32_CAMERA
|
|
||||||
void on_camera_image_request(const CameraImageRequest &msg) override;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_CLIMATE
|
|
||||||
void on_climate_command_request(const ClimateCommandRequest &msg) override;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_COVER
|
#ifdef USE_COVER
|
||||||
void on_cover_command_request(const CoverCommandRequest &msg) override;
|
void on_cover_command_request(const CoverCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_DATE
|
|
||||||
void on_date_command_request(const DateCommandRequest &msg) override;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_DATETIME_DATETIME
|
|
||||||
void on_date_time_command_request(const DateTimeCommandRequest &msg) override;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_FAN
|
#ifdef USE_FAN
|
||||||
void on_fan_command_request(const FanCommandRequest &msg) override;
|
void on_fan_command_request(const FanCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LIGHT
|
#ifdef USE_LIGHT
|
||||||
void on_light_command_request(const LightCommandRequest &msg) override;
|
void on_light_command_request(const LightCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LOCK
|
#ifdef USE_SWITCH
|
||||||
void on_lock_command_request(const LockCommandRequest &msg) override;
|
void on_switch_command_request(const SwitchCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_MEDIA_PLAYER
|
#ifdef USE_ESP32_CAMERA
|
||||||
void on_media_player_command_request(const MediaPlayerCommandRequest &msg) override;
|
void on_camera_image_request(const CameraImageRequest &msg) override;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_CLIMATE
|
||||||
|
void on_climate_command_request(const ClimateCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_NUMBER
|
#ifdef USE_NUMBER
|
||||||
void on_number_command_request(const NumberCommandRequest &msg) override;
|
void on_number_command_request(const NumberCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_TEXT
|
||||||
|
void on_text_command_request(const TextCommandRequest &msg) override;
|
||||||
|
#endif
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
void on_select_command_request(const SelectCommandRequest &msg) override;
|
void on_select_command_request(const SelectCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SIREN
|
#ifdef USE_BUTTON
|
||||||
void on_siren_command_request(const SirenCommandRequest &msg) override;
|
void on_button_command_request(const ButtonCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SWITCH
|
#ifdef USE_LOCK
|
||||||
void on_switch_command_request(const SwitchCommandRequest &msg) override;
|
void on_lock_command_request(const LockCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT
|
#ifdef USE_VALVE
|
||||||
void on_text_command_request(const TextCommandRequest &msg) override;
|
void on_valve_command_request(const ValveCommandRequest &msg) override;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_MEDIA_PLAYER
|
||||||
|
void on_media_player_command_request(const MediaPlayerCommandRequest &msg) override;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATE
|
||||||
|
void on_date_command_request(const DateCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_TIME
|
#ifdef USE_DATETIME_TIME
|
||||||
void on_time_command_request(const TimeCommandRequest &msg) override;
|
void on_time_command_request(const TimeCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_DATETIME_DATETIME
|
||||||
|
void on_date_time_command_request(const DateTimeCommandRequest &msg) override;
|
||||||
|
#endif
|
||||||
#ifdef USE_UPDATE
|
#ifdef USE_UPDATE
|
||||||
void on_update_command_request(const UpdateCommandRequest &msg) override;
|
void on_update_command_request(const UpdateCommandRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VALVE
|
|
||||||
void on_valve_command_request(const ValveCommandRequest &msg) override;
|
|
||||||
#endif
|
|
||||||
#ifdef USE_BLUETOOTH_PROXY
|
#ifdef USE_BLUETOOTH_PROXY
|
||||||
void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg) override;
|
void on_subscribe_bluetooth_le_advertisements_request(const SubscribeBluetoothLEAdvertisementsRequest &msg) override;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,361 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "proto.h"
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace esphome {
|
|
||||||
namespace api {
|
|
||||||
|
|
||||||
class ProtoSize {
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief ProtoSize class for Protocol Buffer serialization size calculation
|
|
||||||
*
|
|
||||||
* This class provides static methods to calculate the exact byte counts needed
|
|
||||||
* for encoding various Protocol Buffer field types. All methods are designed to be
|
|
||||||
* efficient for the common case where many fields have default values.
|
|
||||||
*
|
|
||||||
* Implements Protocol Buffer encoding size calculation according to:
|
|
||||||
* https://protobuf.dev/programming-guides/encoding/
|
|
||||||
*
|
|
||||||
* Key features:
|
|
||||||
* - Early-return optimization for zero/default values
|
|
||||||
* - Direct total_size updates to avoid unnecessary additions
|
|
||||||
* - Specialized handling for different field types according to protobuf spec
|
|
||||||
* - Templated helpers for repeated fields and messages
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates the size in bytes needed to encode a uint32_t value as a varint
|
|
||||||
*
|
|
||||||
* @param value The uint32_t value to calculate size for
|
|
||||||
* @return The number of bytes needed to encode the value
|
|
||||||
*/
|
|
||||||
static inline uint32_t varint(uint32_t value) {
|
|
||||||
// Optimized varint size calculation using leading zeros
|
|
||||||
// Each 7 bits requires one byte in the varint encoding
|
|
||||||
if (value < 128)
|
|
||||||
return 1; // 7 bits, common case for small values
|
|
||||||
|
|
||||||
// For larger values, count bytes needed based on the position of the highest bit set
|
|
||||||
if (value < 16384) {
|
|
||||||
return 2; // 14 bits
|
|
||||||
} else if (value < 2097152) {
|
|
||||||
return 3; // 21 bits
|
|
||||||
} else if (value < 268435456) {
|
|
||||||
return 4; // 28 bits
|
|
||||||
} else {
|
|
||||||
return 5; // 32 bits (maximum for uint32_t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates the size in bytes needed to encode a uint64_t value as a varint
|
|
||||||
*
|
|
||||||
* @param value The uint64_t value to calculate size for
|
|
||||||
* @return The number of bytes needed to encode the value
|
|
||||||
*/
|
|
||||||
static inline uint32_t varint(uint64_t value) {
|
|
||||||
// Handle common case of values fitting in uint32_t (vast majority of use cases)
|
|
||||||
if (value <= UINT32_MAX) {
|
|
||||||
return varint(static_cast<uint32_t>(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
// For larger values, determine size based on highest bit position
|
|
||||||
if (value < (1ULL << 35)) {
|
|
||||||
return 5; // 35 bits
|
|
||||||
} else if (value < (1ULL << 42)) {
|
|
||||||
return 6; // 42 bits
|
|
||||||
} else if (value < (1ULL << 49)) {
|
|
||||||
return 7; // 49 bits
|
|
||||||
} else if (value < (1ULL << 56)) {
|
|
||||||
return 8; // 56 bits
|
|
||||||
} else if (value < (1ULL << 63)) {
|
|
||||||
return 9; // 63 bits
|
|
||||||
} else {
|
|
||||||
return 10; // 64 bits (maximum for uint64_t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates the size in bytes needed to encode an int32_t value as a varint
|
|
||||||
*
|
|
||||||
* Special handling is needed for negative values, which are sign-extended to 64 bits
|
|
||||||
* in Protocol Buffers, resulting in a 10-byte varint.
|
|
||||||
*
|
|
||||||
* @param value The int32_t value to calculate size for
|
|
||||||
* @return The number of bytes needed to encode the value
|
|
||||||
*/
|
|
||||||
static inline uint32_t varint(int32_t value) {
|
|
||||||
// Negative values are sign-extended to 64 bits in protocol buffers,
|
|
||||||
// which always results in a 10-byte varint for negative int32
|
|
||||||
if (value < 0) {
|
|
||||||
return 10; // Negative int32 is always 10 bytes long
|
|
||||||
}
|
|
||||||
// For non-negative values, use the uint32_t implementation
|
|
||||||
return varint(static_cast<uint32_t>(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates the size in bytes needed to encode an int64_t value as a varint
|
|
||||||
*
|
|
||||||
* @param value The int64_t value to calculate size for
|
|
||||||
* @return The number of bytes needed to encode the value
|
|
||||||
*/
|
|
||||||
static inline uint32_t varint(int64_t value) {
|
|
||||||
// For int64_t, we convert to uint64_t and calculate the size
|
|
||||||
// This works because the bit pattern determines the encoding size,
|
|
||||||
// and we've handled negative int32 values as a special case above
|
|
||||||
return varint(static_cast<uint64_t>(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates the size in bytes needed to encode a field ID and wire type
|
|
||||||
*
|
|
||||||
* @param field_id The field identifier
|
|
||||||
* @param type The wire type value (from the WireType enum in the protobuf spec)
|
|
||||||
* @return The number of bytes needed to encode the field ID and wire type
|
|
||||||
*/
|
|
||||||
static inline uint32_t field(uint32_t field_id, uint32_t type) {
|
|
||||||
uint32_t tag = (field_id << 3) | (type & 0b111);
|
|
||||||
return varint(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Common parameters for all add_*_field methods
|
|
||||||
*
|
|
||||||
* All add_*_field methods follow these common patterns:
|
|
||||||
*
|
|
||||||
* @param total_size Reference to the total message size to update
|
|
||||||
* @param field_id_size Pre-calculated size of the field ID in bytes
|
|
||||||
* @param value The value to calculate size for (type varies)
|
|
||||||
* @param force Whether to calculate size even if the value is default/zero/empty
|
|
||||||
*
|
|
||||||
* Each method follows this implementation pattern:
|
|
||||||
* 1. Skip calculation if value is default (0, false, empty) and not forced
|
|
||||||
* 2. Calculate the size based on the field's encoding rules
|
|
||||||
* 3. Add the field_id_size + calculated value size to total_size
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of an int32 field to the total message size
|
|
||||||
*/
|
|
||||||
static inline void add_int32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate and directly add to total_size
|
|
||||||
if (value < 0) {
|
|
||||||
// Negative values are encoded as 10-byte varints in protobuf
|
|
||||||
total_size += field_id_size + 10;
|
|
||||||
} else {
|
|
||||||
// For non-negative values, use the standard varint size
|
|
||||||
total_size += field_id_size + varint(static_cast<uint32_t>(value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a uint32 field to the total message size
|
|
||||||
*/
|
|
||||||
static inline void add_uint32_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value,
|
|
||||||
bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate and directly add to total_size
|
|
||||||
total_size += field_id_size + varint(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a boolean field to the total message size
|
|
||||||
*/
|
|
||||||
static inline void add_bool_field(uint32_t &total_size, uint32_t field_id_size, bool value, bool force = false) {
|
|
||||||
// Skip calculation if value is false and not forced
|
|
||||||
if (!value && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Boolean fields always use 1 byte when true
|
|
||||||
total_size += field_id_size + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a fixed field to the total message size
|
|
||||||
*
|
|
||||||
* Fixed fields always take exactly N bytes (4 for fixed32/float, 8 for fixed64/double).
|
|
||||||
*
|
|
||||||
* @tparam NumBytes The number of bytes for this fixed field (4 or 8)
|
|
||||||
* @param is_nonzero Whether the value is non-zero
|
|
||||||
*/
|
|
||||||
template<uint32_t NumBytes>
|
|
||||||
static inline void add_fixed_field(uint32_t &total_size, uint32_t field_id_size, bool is_nonzero,
|
|
||||||
bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (!is_nonzero && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fixed fields always take exactly NumBytes
|
|
||||||
total_size += field_id_size + NumBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of an enum field to the total message size
|
|
||||||
*
|
|
||||||
* Enum fields are encoded as uint32 varints.
|
|
||||||
*/
|
|
||||||
static inline void add_enum_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value, bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enums are encoded as uint32
|
|
||||||
total_size += field_id_size + varint(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a sint32 field to the total message size
|
|
||||||
*
|
|
||||||
* Sint32 fields use ZigZag encoding, which is more efficient for negative values.
|
|
||||||
*/
|
|
||||||
static inline void add_sint32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
|
|
||||||
uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
|
|
||||||
total_size += field_id_size + varint(zigzag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of an int64 field to the total message size
|
|
||||||
*/
|
|
||||||
static inline void add_int64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate and directly add to total_size
|
|
||||||
total_size += field_id_size + varint(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a uint64 field to the total message size
|
|
||||||
*/
|
|
||||||
static inline void add_uint64_field(uint32_t &total_size, uint32_t field_id_size, uint64_t value,
|
|
||||||
bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate and directly add to total_size
|
|
||||||
total_size += field_id_size + varint(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a sint64 field to the total message size
|
|
||||||
*
|
|
||||||
* Sint64 fields use ZigZag encoding, which is more efficient for negative values.
|
|
||||||
*/
|
|
||||||
static inline void add_sint64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force = false) {
|
|
||||||
// Skip calculation if value is zero and not forced
|
|
||||||
if (value == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
|
|
||||||
uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
|
|
||||||
total_size += field_id_size + varint(zigzag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a string/bytes field to the total message size
|
|
||||||
*/
|
|
||||||
static inline void add_string_field(uint32_t &total_size, uint32_t field_id_size, const std::string &str,
|
|
||||||
bool force = false) {
|
|
||||||
// Skip calculation if string is empty and not forced
|
|
||||||
if (str.empty() && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate and directly add to total_size
|
|
||||||
const uint32_t str_size = static_cast<uint32_t>(str.size());
|
|
||||||
total_size += field_id_size + varint(str_size) + str_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a nested message field to the total message size
|
|
||||||
*
|
|
||||||
* This helper function directly updates the total_size reference if the nested size
|
|
||||||
* is greater than zero or force is true.
|
|
||||||
*
|
|
||||||
* @param nested_size The pre-calculated size of the nested message
|
|
||||||
*/
|
|
||||||
static inline void add_message_field(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size,
|
|
||||||
bool force = false) {
|
|
||||||
// Skip calculation if nested message is empty and not forced
|
|
||||||
if (nested_size == 0 && !force) {
|
|
||||||
return; // No need to update total_size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate and directly add to total_size
|
|
||||||
// Field ID + length varint + nested message content
|
|
||||||
total_size += field_id_size + varint(nested_size) + nested_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the size of a nested message field to the total message size
|
|
||||||
*
|
|
||||||
* This templated version directly takes a message object, calculates its size internally,
|
|
||||||
* and updates the total_size reference. This eliminates the need for a temporary variable
|
|
||||||
* at the call site.
|
|
||||||
*
|
|
||||||
* @tparam MessageType The type of the nested message (inferred from parameter)
|
|
||||||
* @param message The nested message object
|
|
||||||
*/
|
|
||||||
template<typename MessageType>
|
|
||||||
static inline void add_message_object(uint32_t &total_size, uint32_t field_id_size, const MessageType &message,
|
|
||||||
bool force = false) {
|
|
||||||
uint32_t nested_size = 0;
|
|
||||||
message.calculate_size(nested_size);
|
|
||||||
|
|
||||||
// Use the base implementation with the calculated nested_size
|
|
||||||
add_message_field(total_size, field_id_size, nested_size, force);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculates and adds the sizes of all messages in a repeated field to the total message size
|
|
||||||
*
|
|
||||||
* This helper processes a vector of message objects, calculating the size for each message
|
|
||||||
* and adding it to the total size.
|
|
||||||
*
|
|
||||||
* @tparam MessageType The type of the nested messages in the vector
|
|
||||||
* @param messages Vector of message objects
|
|
||||||
*/
|
|
||||||
template<typename MessageType>
|
|
||||||
static inline void add_repeated_message(uint32_t &total_size, uint32_t field_id_size,
|
|
||||||
const std::vector<MessageType> &messages) {
|
|
||||||
// Skip if the vector is empty
|
|
||||||
if (messages.empty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// For repeated fields, always use force=true
|
|
||||||
for (const auto &message : messages) {
|
|
||||||
add_message_object(total_size, field_id_size, message, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace api
|
|
||||||
} // namespace esphome
|
|
||||||
@@ -24,14 +24,10 @@ static const char *const TAG = "api";
|
|||||||
// APIServer
|
// APIServer
|
||||||
APIServer *global_api_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
APIServer *global_api_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
APIServer::APIServer() {
|
APIServer::APIServer() { global_api_server = this; }
|
||||||
global_api_server = this;
|
|
||||||
// Pre-allocate shared write buffer
|
|
||||||
shared_write_buffer_.reserve(64);
|
|
||||||
}
|
|
||||||
|
|
||||||
void APIServer::setup() {
|
void APIServer::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up Home Assistant API server...");
|
||||||
this->setup_controller();
|
this->setup_controller();
|
||||||
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
@@ -47,7 +43,7 @@ void APIServer::setup() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
|
this->socket_ = socket::socket_ip(SOCK_STREAM, 0);
|
||||||
if (this->socket_ == nullptr) {
|
if (this->socket_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not create socket");
|
ESP_LOGW(TAG, "Could not create socket");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
@@ -92,12 +88,6 @@ void APIServer::setup() {
|
|||||||
#ifdef USE_LOGGER
|
#ifdef USE_LOGGER
|
||||||
if (logger::global_logger != nullptr) {
|
if (logger::global_logger != nullptr) {
|
||||||
logger::global_logger->add_on_log_callback([this](int level, const char *tag, const char *message) {
|
logger::global_logger->add_on_log_callback([this](int level, const char *tag, const char *message) {
|
||||||
if (this->shutting_down_) {
|
|
||||||
// Don't try to send logs during shutdown
|
|
||||||
// as it could result in a recursion and
|
|
||||||
// we would be filling a buffer we are trying to clear
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (auto &c : this->clients_) {
|
for (auto &c : this->clients_) {
|
||||||
if (!c->remove_)
|
if (!c->remove_)
|
||||||
c->try_send_log_message(level, tag, message);
|
c->try_send_log_message(level, tag, message);
|
||||||
@@ -122,52 +112,40 @@ void APIServer::setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void APIServer::loop() {
|
void APIServer::loop() {
|
||||||
// Accept new clients only if the socket exists and has incoming connections
|
// Accept new clients
|
||||||
if (this->socket_ && this->socket_->ready()) {
|
while (true) {
|
||||||
while (true) {
|
struct sockaddr_storage source_addr;
|
||||||
struct sockaddr_storage source_addr;
|
socklen_t addr_len = sizeof(source_addr);
|
||||||
socklen_t addr_len = sizeof(source_addr);
|
auto sock = this->socket_->accept((struct sockaddr *) &source_addr, &addr_len);
|
||||||
auto sock = this->socket_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
|
if (!sock)
|
||||||
if (!sock)
|
break;
|
||||||
break;
|
ESP_LOGD(TAG, "Accepted %s", sock->getpeername().c_str());
|
||||||
ESP_LOGD(TAG, "Accepted %s", sock->getpeername().c_str());
|
|
||||||
|
|
||||||
auto *conn = new APIConnection(std::move(sock), this);
|
auto *conn = new APIConnection(std::move(sock), this);
|
||||||
this->clients_.emplace_back(conn);
|
this->clients_.emplace_back(conn);
|
||||||
conn->start();
|
conn->start();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process clients and remove disconnected ones in a single pass
|
// Partition clients into remove and active
|
||||||
if (!this->clients_.empty()) {
|
auto new_end = std::partition(this->clients_.begin(), this->clients_.end(),
|
||||||
size_t client_index = 0;
|
[](const std::unique_ptr<APIConnection> &conn) { return !conn->remove_; });
|
||||||
while (client_index < this->clients_.size()) {
|
// print disconnection messages
|
||||||
auto &client = this->clients_[client_index];
|
for (auto it = new_end; it != this->clients_.end(); ++it) {
|
||||||
|
this->client_disconnected_trigger_->trigger((*it)->client_info_, (*it)->client_peername_);
|
||||||
|
ESP_LOGV(TAG, "Removing connection to %s", (*it)->client_info_.c_str());
|
||||||
|
}
|
||||||
|
// resize vector
|
||||||
|
this->clients_.erase(new_end, this->clients_.end());
|
||||||
|
|
||||||
if (client->remove_) {
|
for (auto &client : this->clients_) {
|
||||||
// Handle disconnection
|
client->loop();
|
||||||
this->client_disconnected_trigger_->trigger(client->client_info_, client->client_peername_);
|
|
||||||
ESP_LOGV(TAG, "Removing connection to %s", client->client_info_.c_str());
|
|
||||||
|
|
||||||
// Swap with the last element and pop (avoids expensive vector shifts)
|
|
||||||
if (client_index < this->clients_.size() - 1) {
|
|
||||||
std::swap(this->clients_[client_index], this->clients_.back());
|
|
||||||
}
|
|
||||||
this->clients_.pop_back();
|
|
||||||
// Don't increment client_index since we need to process the swapped element
|
|
||||||
} else {
|
|
||||||
// Process active client
|
|
||||||
client->loop();
|
|
||||||
client_index++; // Move to next client
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->reboot_timeout_ != 0) {
|
if (this->reboot_timeout_ != 0) {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (!this->is_connected()) {
|
if (!this->is_connected()) {
|
||||||
if (now - this->last_connected_ > this->reboot_timeout_) {
|
if (now - this->last_connected_ > this->reboot_timeout_) {
|
||||||
ESP_LOGE(TAG, "No client connected; rebooting");
|
ESP_LOGE(TAG, "No client connected to API. Rebooting...");
|
||||||
App.reboot();
|
App.reboot();
|
||||||
}
|
}
|
||||||
this->status_set_warning();
|
this->status_set_warning();
|
||||||
@@ -179,10 +157,8 @@ void APIServer::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void APIServer::dump_config() {
|
void APIServer::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, "API Server:");
|
||||||
"API Server:\n"
|
ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_);
|
||||||
" Address: %s:%u",
|
|
||||||
network::get_use_address().c_str(), this->port_);
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
ESP_LOGCONFIG(TAG, " Using noise encryption: %s", YESNO(this->noise_ctx_->has_psk()));
|
ESP_LOGCONFIG(TAG, " Using noise encryption: %s", YESNO(this->noise_ctx_->has_psk()));
|
||||||
if (!this->noise_ctx_->has_psk()) {
|
if (!this->noise_ctx_->has_psk()) {
|
||||||
@@ -231,7 +207,7 @@ void APIServer::on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool s
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_binary_sensor_state(obj);
|
c->send_binary_sensor_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -267,7 +243,7 @@ void APIServer::on_sensor_update(sensor::Sensor *obj, float state) {
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_sensor_state(obj);
|
c->send_sensor_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -276,7 +252,7 @@ void APIServer::on_switch_update(switch_::Switch *obj, bool state) {
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_switch_state(obj);
|
c->send_switch_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -285,7 +261,7 @@ void APIServer::on_text_sensor_update(text_sensor::TextSensor *obj, const std::s
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_text_sensor_state(obj);
|
c->send_text_sensor_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -303,7 +279,7 @@ void APIServer::on_number_update(number::Number *obj, float state) {
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_number_state(obj);
|
c->send_number_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -339,7 +315,7 @@ void APIServer::on_text_update(text::Text *obj, const std::string &state) {
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_text_state(obj);
|
c->send_text_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -348,7 +324,7 @@ void APIServer::on_select_update(select::Select *obj, const std::string &state,
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_select_state(obj);
|
c->send_select_state(obj, state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -357,7 +333,7 @@ void APIServer::on_lock_update(lock::Lock *obj) {
|
|||||||
if (obj->is_internal())
|
if (obj->is_internal())
|
||||||
return;
|
return;
|
||||||
for (auto &c : this->clients_)
|
for (auto &c : this->clients_)
|
||||||
c->send_lock_state(obj);
|
c->send_lock_state(obj, obj->state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -408,8 +384,6 @@ void APIServer::set_port(uint16_t port) { this->port_ = port; }
|
|||||||
|
|
||||||
void APIServer::set_password(const std::string &password) { this->password_ = password; }
|
void APIServer::set_password(const std::string &password) { this->password_ = password; }
|
||||||
|
|
||||||
void APIServer::set_batch_delay(uint32_t batch_delay) { this->batch_delay_ = batch_delay; }
|
|
||||||
|
|
||||||
void APIServer::send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
|
void APIServer::send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
|
||||||
for (auto &client : this->clients_) {
|
for (auto &client : this->clients_) {
|
||||||
client->send_homeassistant_service_call(call);
|
client->send_homeassistant_service_call(call);
|
||||||
@@ -468,7 +442,7 @@ bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
|
|||||||
ESP_LOGW(TAG, "Disconnecting all clients to reset connections");
|
ESP_LOGW(TAG, "Disconnecting all clients to reset connections");
|
||||||
this->set_noise_psk(psk);
|
this->set_noise_psk(psk);
|
||||||
for (auto &c : this->clients_) {
|
for (auto &c : this->clients_) {
|
||||||
c->send_message(DisconnectRequest());
|
c->send_disconnect_request(DisconnectRequest());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -488,36 +462,10 @@ void APIServer::request_time() {
|
|||||||
bool APIServer::is_connected() const { return !this->clients_.empty(); }
|
bool APIServer::is_connected() const { return !this->clients_.empty(); }
|
||||||
|
|
||||||
void APIServer::on_shutdown() {
|
void APIServer::on_shutdown() {
|
||||||
this->shutting_down_ = true;
|
|
||||||
|
|
||||||
// Close the listening socket to prevent new connections
|
|
||||||
if (this->socket_) {
|
|
||||||
this->socket_->close();
|
|
||||||
this->socket_ = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change batch delay to 5ms for quick flushing during shutdown
|
|
||||||
this->batch_delay_ = 5;
|
|
||||||
|
|
||||||
// Send disconnect requests to all connected clients
|
|
||||||
for (auto &c : this->clients_) {
|
for (auto &c : this->clients_) {
|
||||||
if (!c->send_message(DisconnectRequest())) {
|
c->send_disconnect_request(DisconnectRequest());
|
||||||
// If we can't send the disconnect request directly (tx_buffer full),
|
|
||||||
// schedule it in the batch so it will be sent with the 5ms timer
|
|
||||||
c->schedule_message_(nullptr, &APIConnection::try_send_disconnect_request, DisconnectRequest::MESSAGE_TYPE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
delay(10);
|
||||||
|
|
||||||
bool APIServer::teardown() {
|
|
||||||
// If network is disconnected, no point trying to flush buffers
|
|
||||||
if (!network::is_connected()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
this->loop();
|
|
||||||
|
|
||||||
// Return true only when all clients have been torn down
|
|
||||||
return this->clients_.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|||||||
@@ -34,17 +34,11 @@ class APIServer : public Component, public Controller {
|
|||||||
void loop() override;
|
void loop() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
void on_shutdown() override;
|
void on_shutdown() override;
|
||||||
bool teardown() override;
|
|
||||||
bool check_password(const std::string &password) const;
|
bool check_password(const std::string &password) const;
|
||||||
bool uses_password() const;
|
bool uses_password() const;
|
||||||
void set_port(uint16_t port);
|
void set_port(uint16_t port);
|
||||||
void set_password(const std::string &password);
|
void set_password(const std::string &password);
|
||||||
void set_reboot_timeout(uint32_t reboot_timeout);
|
void set_reboot_timeout(uint32_t reboot_timeout);
|
||||||
void set_batch_delay(uint32_t batch_delay);
|
|
||||||
uint32_t get_batch_delay() const { return batch_delay_; }
|
|
||||||
|
|
||||||
// Get reference to shared buffer for API connections
|
|
||||||
std::vector<uint8_t> &get_shared_buffer_ref() { return shared_write_buffer_; }
|
|
||||||
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
bool save_noise_psk(psk_t psk, bool make_active = true);
|
bool save_noise_psk(psk_t psk, bool make_active = true);
|
||||||
@@ -142,15 +136,12 @@ class APIServer : public Component, public Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool shutting_down_ = false;
|
|
||||||
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
||||||
uint16_t port_{6053};
|
uint16_t port_{6053};
|
||||||
uint32_t reboot_timeout_{300000};
|
uint32_t reboot_timeout_{300000};
|
||||||
uint32_t batch_delay_{100};
|
|
||||||
uint32_t last_connected_{0};
|
uint32_t last_connected_{0};
|
||||||
std::vector<std::unique_ptr<APIConnection>> clients_;
|
std::vector<std::unique_ptr<APIConnection>> clients_;
|
||||||
std::string password_;
|
std::string password_;
|
||||||
std::vector<uint8_t> shared_write_buffer_; // Shared proto write buffer for all connections
|
|
||||||
std::vector<HomeAssistantStateSubscription> state_subs_;
|
std::vector<HomeAssistantStateSubscription> state_subs_;
|
||||||
std::vector<UserServiceDescriptor *> user_services_;
|
std::vector<UserServiceDescriptor *> user_services_;
|
||||||
Trigger<std::string, std::string> *client_connected_trigger_ = new Trigger<std::string, std::string>();
|
Trigger<std::string, std::string> *client_connected_trigger_ = new Trigger<std::string, std::string>();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from datetime import datetime
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from aioesphomeapi import APIClient, parse_log_message
|
from aioesphomeapi import APIClient
|
||||||
from aioesphomeapi.log_runner import async_run
|
from aioesphomeapi.log_runner import async_run
|
||||||
|
|
||||||
from esphome.const import CONF_KEY, CONF_PASSWORD, CONF_PORT, __version__
|
from esphome.const import CONF_KEY, CONF_PASSWORD, CONF_PORT, __version__
|
||||||
@@ -46,10 +46,9 @@ async def async_run_logs(config: dict[str, Any], address: str) -> None:
|
|||||||
time_ = datetime.now()
|
time_ = datetime.now()
|
||||||
message: bytes = msg.message
|
message: bytes = msg.message
|
||||||
text = message.decode("utf8", "backslashreplace")
|
text = message.decode("utf8", "backslashreplace")
|
||||||
for parsed_msg in parse_log_message(
|
if dashboard:
|
||||||
text, f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}]"
|
text = text.replace("\033", "\\033")
|
||||||
):
|
print(f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}]{text}")
|
||||||
print(parsed_msg.replace("\033", "\\033") if dashboard else parsed_msg)
|
|
||||||
|
|
||||||
stop = await async_run(cli, on_log, name=name)
|
stop = await async_run(cli, on_log, name=name)
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
#include "api_server.h"
|
#include "api_server.h"
|
||||||
#ifdef USE_API
|
#ifdef USE_API
|
||||||
#include "api_pb2.h"
|
#include "api_pb2.h"
|
||||||
#include "esphome/core/automation.h"
|
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/automation.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ bool ListEntitiesIterator::on_end() { return this->client_->send_list_info_done(
|
|||||||
ListEntitiesIterator::ListEntitiesIterator(APIConnection *client) : client_(client) {}
|
ListEntitiesIterator::ListEntitiesIterator(APIConnection *client) : client_(client) {}
|
||||||
bool ListEntitiesIterator::on_service(UserServiceDescriptor *service) {
|
bool ListEntitiesIterator::on_service(UserServiceDescriptor *service) {
|
||||||
auto resp = service->encode_list_service_response();
|
auto resp = service->encode_list_service_response();
|
||||||
return this->client_->send_message(resp);
|
return this->client_->send_list_entities_services_response(resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_ESP32_CAMERA
|
#ifdef USE_ESP32_CAMERA
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -20,26 +20,16 @@ class ProtoVarInt {
|
|||||||
explicit ProtoVarInt(uint64_t value) : value_(value) {}
|
explicit ProtoVarInt(uint64_t value) : value_(value) {}
|
||||||
|
|
||||||
static optional<ProtoVarInt> parse(const uint8_t *buffer, uint32_t len, uint32_t *consumed) {
|
static optional<ProtoVarInt> parse(const uint8_t *buffer, uint32_t len, uint32_t *consumed) {
|
||||||
if (len == 0) {
|
if (consumed != nullptr)
|
||||||
if (consumed != nullptr)
|
*consumed = 0;
|
||||||
*consumed = 0;
|
|
||||||
|
if (len == 0)
|
||||||
return {};
|
return {};
|
||||||
}
|
|
||||||
|
|
||||||
// Most common case: single-byte varint (values 0-127)
|
uint64_t result = 0;
|
||||||
if ((buffer[0] & 0x80) == 0) {
|
uint8_t bitpos = 0;
|
||||||
if (consumed != nullptr)
|
|
||||||
*consumed = 1;
|
|
||||||
return ProtoVarInt(buffer[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// General case for multi-byte varints
|
for (uint32_t i = 0; i < len; i++) {
|
||||||
// Since we know buffer[0]'s high bit is set, initialize with its value
|
|
||||||
uint64_t result = buffer[0] & 0x7F;
|
|
||||||
uint8_t bitpos = 7;
|
|
||||||
|
|
||||||
// Start from the second byte since we've already processed the first
|
|
||||||
for (uint32_t i = 1; i < len; i++) {
|
|
||||||
uint8_t val = buffer[i];
|
uint8_t val = buffer[i];
|
||||||
result |= uint64_t(val & 0x7F) << uint64_t(bitpos);
|
result |= uint64_t(val & 0x7F) << uint64_t(bitpos);
|
||||||
bitpos += 7;
|
bitpos += 7;
|
||||||
@@ -50,12 +40,9 @@ class ProtoVarInt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (consumed != nullptr)
|
return {};
|
||||||
*consumed = 0;
|
|
||||||
return {}; // Incomplete or invalid varint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t as_uint16() const { return this->value_; }
|
|
||||||
uint32_t as_uint32() const { return this->value_; }
|
uint32_t as_uint32() const { return this->value_; }
|
||||||
uint64_t as_uint64() const { return this->value_; }
|
uint64_t as_uint64() const { return this->value_; }
|
||||||
bool as_bool() const { return this->value_; }
|
bool as_bool() const { return this->value_; }
|
||||||
@@ -84,34 +71,6 @@ class ProtoVarInt {
|
|||||||
return static_cast<int64_t>(this->value_ >> 1);
|
return static_cast<int64_t>(this->value_ >> 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Encode the varint value to a pre-allocated buffer without bounds checking.
|
|
||||||
*
|
|
||||||
* @param buffer The pre-allocated buffer to write the encoded varint to
|
|
||||||
* @param len The size of the buffer in bytes
|
|
||||||
*
|
|
||||||
* @note The caller is responsible for ensuring the buffer is large enough
|
|
||||||
* to hold the encoded value. Use ProtoSize::varint() to calculate
|
|
||||||
* the exact size needed before calling this method.
|
|
||||||
* @note No bounds checking is performed for performance reasons.
|
|
||||||
*/
|
|
||||||
void encode_to_buffer_unchecked(uint8_t *buffer, size_t len) {
|
|
||||||
uint64_t val = this->value_;
|
|
||||||
if (val <= 0x7F) {
|
|
||||||
buffer[0] = val;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
size_t i = 0;
|
|
||||||
while (val && i < len) {
|
|
||||||
uint8_t temp = val & 0x7F;
|
|
||||||
val >>= 7;
|
|
||||||
if (val) {
|
|
||||||
buffer[i++] = temp | 0x80;
|
|
||||||
} else {
|
|
||||||
buffer[i++] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void encode(std::vector<uint8_t> &out) {
|
void encode(std::vector<uint8_t> &out) {
|
||||||
uint64_t val = this->value_;
|
uint64_t val = this->value_;
|
||||||
if (val <= 0x7F) {
|
if (val <= 0x7F) {
|
||||||
@@ -190,18 +149,6 @@ class ProtoWriteBuffer {
|
|||||||
void write(uint8_t value) { this->buffer_->push_back(value); }
|
void write(uint8_t value) { this->buffer_->push_back(value); }
|
||||||
void encode_varint_raw(ProtoVarInt value) { value.encode(*this->buffer_); }
|
void encode_varint_raw(ProtoVarInt value) { value.encode(*this->buffer_); }
|
||||||
void encode_varint_raw(uint32_t value) { this->encode_varint_raw(ProtoVarInt(value)); }
|
void encode_varint_raw(uint32_t value) { this->encode_varint_raw(ProtoVarInt(value)); }
|
||||||
/**
|
|
||||||
* Encode a field key (tag/wire type combination).
|
|
||||||
*
|
|
||||||
* @param field_id Field number (tag) in the protobuf message
|
|
||||||
* @param type Wire type value:
|
|
||||||
* - 0: Varint (int32, int64, uint32, uint64, sint32, sint64, bool, enum)
|
|
||||||
* - 1: 64-bit (fixed64, sfixed64, double)
|
|
||||||
* - 2: Length-delimited (string, bytes, embedded messages, packed repeated fields)
|
|
||||||
* - 5: 32-bit (fixed32, sfixed32, float)
|
|
||||||
*
|
|
||||||
* Following https://protobuf.dev/programming-guides/encoding/#structure
|
|
||||||
*/
|
|
||||||
void encode_field_raw(uint32_t field_id, uint32_t type) {
|
void encode_field_raw(uint32_t field_id, uint32_t type) {
|
||||||
uint32_t val = (field_id << 3) | (type & 0b111);
|
uint32_t val = (field_id << 3) | (type & 0b111);
|
||||||
this->encode_varint_raw(val);
|
this->encode_varint_raw(val);
|
||||||
@@ -210,13 +157,13 @@ class ProtoWriteBuffer {
|
|||||||
if (len == 0 && !force)
|
if (len == 0 && !force)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this->encode_field_raw(field_id, 2); // type 2: Length-delimited string
|
this->encode_field_raw(field_id, 2);
|
||||||
this->encode_varint_raw(len);
|
this->encode_varint_raw(len);
|
||||||
auto *data = reinterpret_cast<const uint8_t *>(string);
|
auto *data = reinterpret_cast<const uint8_t *>(string);
|
||||||
this->buffer_->insert(this->buffer_->end(), data, data + len);
|
this->buffer_->insert(this->buffer_->end(), data, data + len);
|
||||||
}
|
}
|
||||||
void encode_string(uint32_t field_id, const std::string &value, bool force = false) {
|
void encode_string(uint32_t field_id, const std::string &value, bool force = false) {
|
||||||
this->encode_string(field_id, value.data(), value.size(), force);
|
this->encode_string(field_id, value.data(), value.size());
|
||||||
}
|
}
|
||||||
void encode_bytes(uint32_t field_id, const uint8_t *data, size_t len, bool force = false) {
|
void encode_bytes(uint32_t field_id, const uint8_t *data, size_t len, bool force = false) {
|
||||||
this->encode_string(field_id, reinterpret_cast<const char *>(data), len, force);
|
this->encode_string(field_id, reinterpret_cast<const char *>(data), len, force);
|
||||||
@@ -224,26 +171,26 @@ class ProtoWriteBuffer {
|
|||||||
void encode_uint32(uint32_t field_id, uint32_t value, bool force = false) {
|
void encode_uint32(uint32_t field_id, uint32_t value, bool force = false) {
|
||||||
if (value == 0 && !force)
|
if (value == 0 && !force)
|
||||||
return;
|
return;
|
||||||
this->encode_field_raw(field_id, 0); // type 0: Varint - uint32
|
this->encode_field_raw(field_id, 0);
|
||||||
this->encode_varint_raw(value);
|
this->encode_varint_raw(value);
|
||||||
}
|
}
|
||||||
void encode_uint64(uint32_t field_id, uint64_t value, bool force = false) {
|
void encode_uint64(uint32_t field_id, uint64_t value, bool force = false) {
|
||||||
if (value == 0 && !force)
|
if (value == 0 && !force)
|
||||||
return;
|
return;
|
||||||
this->encode_field_raw(field_id, 0); // type 0: Varint - uint64
|
this->encode_field_raw(field_id, 0);
|
||||||
this->encode_varint_raw(ProtoVarInt(value));
|
this->encode_varint_raw(ProtoVarInt(value));
|
||||||
}
|
}
|
||||||
void encode_bool(uint32_t field_id, bool value, bool force = false) {
|
void encode_bool(uint32_t field_id, bool value, bool force = false) {
|
||||||
if (!value && !force)
|
if (!value && !force)
|
||||||
return;
|
return;
|
||||||
this->encode_field_raw(field_id, 0); // type 0: Varint - bool
|
this->encode_field_raw(field_id, 0);
|
||||||
this->write(0x01);
|
this->write(0x01);
|
||||||
}
|
}
|
||||||
void encode_fixed32(uint32_t field_id, uint32_t value, bool force = false) {
|
void encode_fixed32(uint32_t field_id, uint32_t value, bool force = false) {
|
||||||
if (value == 0 && !force)
|
if (value == 0 && !force)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this->encode_field_raw(field_id, 5); // type 5: 32-bit fixed32
|
this->encode_field_raw(field_id, 5);
|
||||||
this->write((value >> 0) & 0xFF);
|
this->write((value >> 0) & 0xFF);
|
||||||
this->write((value >> 8) & 0xFF);
|
this->write((value >> 8) & 0xFF);
|
||||||
this->write((value >> 16) & 0xFF);
|
this->write((value >> 16) & 0xFF);
|
||||||
@@ -253,7 +200,7 @@ class ProtoWriteBuffer {
|
|||||||
if (value == 0 && !force)
|
if (value == 0 && !force)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this->encode_field_raw(field_id, 1); // type 1: 64-bit fixed64
|
this->encode_field_raw(field_id, 5);
|
||||||
this->write((value >> 0) & 0xFF);
|
this->write((value >> 0) & 0xFF);
|
||||||
this->write((value >> 8) & 0xFF);
|
this->write((value >> 8) & 0xFF);
|
||||||
this->write((value >> 16) & 0xFF);
|
this->write((value >> 16) & 0xFF);
|
||||||
@@ -307,7 +254,7 @@ class ProtoWriteBuffer {
|
|||||||
this->encode_uint64(field_id, uvalue, force);
|
this->encode_uint64(field_id, uvalue, force);
|
||||||
}
|
}
|
||||||
template<class C> void encode_message(uint32_t field_id, const C &value, bool force = false) {
|
template<class C> void encode_message(uint32_t field_id, const C &value, bool force = false) {
|
||||||
this->encode_field_raw(field_id, 2); // type 2: Length-delimited message
|
this->encode_field_raw(field_id, 2);
|
||||||
size_t begin = this->buffer_->size();
|
size_t begin = this->buffer_->size();
|
||||||
|
|
||||||
value.encode(*this);
|
value.encode(*this);
|
||||||
@@ -329,7 +276,6 @@ class ProtoMessage {
|
|||||||
virtual ~ProtoMessage() = default;
|
virtual ~ProtoMessage() = default;
|
||||||
virtual void encode(ProtoWriteBuffer buffer) const = 0;
|
virtual void encode(ProtoWriteBuffer buffer) const = 0;
|
||||||
void decode(const uint8_t *buffer, size_t length);
|
void decode(const uint8_t *buffer, size_t length);
|
||||||
virtual void calculate_size(uint32_t &total_size) const = 0;
|
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
std::string dump() const;
|
std::string dump() const;
|
||||||
virtual void dump_to(std::string &out) const = 0;
|
virtual void dump_to(std::string &out) const = 0;
|
||||||
@@ -352,29 +298,13 @@ class ProtoService {
|
|||||||
virtual void on_fatal_error() = 0;
|
virtual void on_fatal_error() = 0;
|
||||||
virtual void on_unauthenticated_access() = 0;
|
virtual void on_unauthenticated_access() = 0;
|
||||||
virtual void on_no_setup_connection() = 0;
|
virtual void on_no_setup_connection() = 0;
|
||||||
/**
|
virtual ProtoWriteBuffer create_buffer() = 0;
|
||||||
* Create a buffer with a reserved size.
|
virtual bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) = 0;
|
||||||
* @param reserve_size The number of bytes to pre-allocate in the buffer. This is a hint
|
|
||||||
* to optimize memory usage and avoid reallocations during encoding.
|
|
||||||
* Implementations should aim to allocate at least this size.
|
|
||||||
* @return A ProtoWriteBuffer object with the reserved size.
|
|
||||||
*/
|
|
||||||
virtual ProtoWriteBuffer create_buffer(uint32_t reserve_size) = 0;
|
|
||||||
virtual bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) = 0;
|
|
||||||
virtual bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
|
virtual bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
|
||||||
|
|
||||||
// Optimized method that pre-allocates buffer based on message size
|
template<class C> bool send_message_(const C &msg, uint32_t message_type) {
|
||||||
bool send_message_(const ProtoMessage &msg, uint16_t message_type) {
|
auto buffer = this->create_buffer();
|
||||||
uint32_t msg_size = 0;
|
|
||||||
msg.calculate_size(msg_size);
|
|
||||||
|
|
||||||
// Create a pre-sized buffer
|
|
||||||
auto buffer = this->create_buffer(msg_size);
|
|
||||||
|
|
||||||
// Encode message into the buffer
|
|
||||||
msg.encode(buffer);
|
msg.encode(buffer);
|
||||||
|
|
||||||
// Send the buffer
|
|
||||||
return this->send_buffer(buffer, message_type);
|
return this->send_buffer(buffer, message_type);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace api {
|
|||||||
|
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
bool InitialStateIterator::on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) {
|
bool InitialStateIterator::on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) {
|
||||||
return this->client_->send_binary_sensor_state(binary_sensor);
|
return this->client_->send_binary_sensor_state(binary_sensor, binary_sensor->state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_COVER
|
#ifdef USE_COVER
|
||||||
@@ -21,21 +21,27 @@ bool InitialStateIterator::on_fan(fan::Fan *fan) { return this->client_->send_fa
|
|||||||
bool InitialStateIterator::on_light(light::LightState *light) { return this->client_->send_light_state(light); }
|
bool InitialStateIterator::on_light(light::LightState *light) { return this->client_->send_light_state(light); }
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
bool InitialStateIterator::on_sensor(sensor::Sensor *sensor) { return this->client_->send_sensor_state(sensor); }
|
bool InitialStateIterator::on_sensor(sensor::Sensor *sensor) {
|
||||||
|
return this->client_->send_sensor_state(sensor, sensor->state);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SWITCH
|
#ifdef USE_SWITCH
|
||||||
bool InitialStateIterator::on_switch(switch_::Switch *a_switch) { return this->client_->send_switch_state(a_switch); }
|
bool InitialStateIterator::on_switch(switch_::Switch *a_switch) {
|
||||||
|
return this->client_->send_switch_state(a_switch, a_switch->state);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
bool InitialStateIterator::on_text_sensor(text_sensor::TextSensor *text_sensor) {
|
bool InitialStateIterator::on_text_sensor(text_sensor::TextSensor *text_sensor) {
|
||||||
return this->client_->send_text_sensor_state(text_sensor);
|
return this->client_->send_text_sensor_state(text_sensor, text_sensor->state);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_CLIMATE
|
#ifdef USE_CLIMATE
|
||||||
bool InitialStateIterator::on_climate(climate::Climate *climate) { return this->client_->send_climate_state(climate); }
|
bool InitialStateIterator::on_climate(climate::Climate *climate) { return this->client_->send_climate_state(climate); }
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_NUMBER
|
#ifdef USE_NUMBER
|
||||||
bool InitialStateIterator::on_number(number::Number *number) { return this->client_->send_number_state(number); }
|
bool InitialStateIterator::on_number(number::Number *number) {
|
||||||
|
return this->client_->send_number_state(number, number->state);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_DATETIME_DATE
|
#ifdef USE_DATETIME_DATE
|
||||||
bool InitialStateIterator::on_date(datetime::DateEntity *date) { return this->client_->send_date_state(date); }
|
bool InitialStateIterator::on_date(datetime::DateEntity *date) { return this->client_->send_date_state(date); }
|
||||||
@@ -49,13 +55,15 @@ bool InitialStateIterator::on_datetime(datetime::DateTimeEntity *datetime) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT
|
#ifdef USE_TEXT
|
||||||
bool InitialStateIterator::on_text(text::Text *text) { return this->client_->send_text_state(text); }
|
bool InitialStateIterator::on_text(text::Text *text) { return this->client_->send_text_state(text, text->state); }
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
bool InitialStateIterator::on_select(select::Select *select) { return this->client_->send_select_state(select); }
|
bool InitialStateIterator::on_select(select::Select *select) {
|
||||||
|
return this->client_->send_select_state(select, select->state);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_LOCK
|
#ifdef USE_LOCK
|
||||||
bool InitialStateIterator::on_lock(lock::Lock *a_lock) { return this->client_->send_lock_state(a_lock); }
|
bool InitialStateIterator::on_lock(lock::Lock *a_lock) { return this->client_->send_lock_state(a_lock, a_lock->state); }
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_VALVE
|
#ifdef USE_VALVE
|
||||||
bool InitialStateIterator::on_valve(valve::Valve *valve) { return this->client_->send_valve_state(valve); }
|
bool InitialStateIterator::on_valve(valve::Valve *valve) { return this->client_->send_valve_state(valve); }
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace as3935 {
|
|||||||
static const char *const TAG = "as3935";
|
static const char *const TAG = "as3935";
|
||||||
|
|
||||||
void AS3935Component::setup() {
|
void AS3935Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AS3935...");
|
||||||
|
|
||||||
this->irq_pin_->setup();
|
this->irq_pin_->setup();
|
||||||
LOG_PIN(" IRQ Pin: ", this->irq_pin_);
|
LOG_PIN(" IRQ Pin: ", this->irq_pin_);
|
||||||
@@ -282,7 +282,7 @@ void AS3935Component::display_oscillator(bool state, uint8_t osc) {
|
|||||||
// based on the resonance frequency of the antenna and so it should be trimmed
|
// based on the resonance frequency of the antenna and so it should be trimmed
|
||||||
// before the calibration is done.
|
// before the calibration is done.
|
||||||
bool AS3935Component::calibrate_oscillator() {
|
bool AS3935Component::calibrate_oscillator() {
|
||||||
ESP_LOGI(TAG, "Starting oscillators calibration");
|
ESP_LOGI(TAG, "Starting oscillators calibration...");
|
||||||
this->write_register(CALIB_RCO, WIPE_ALL, DIRECT_COMMAND, 0); // Send command to calibrate the oscillators
|
this->write_register(CALIB_RCO, WIPE_ALL, DIRECT_COMMAND, 0); // Send command to calibrate the oscillators
|
||||||
|
|
||||||
this->display_oscillator(true, 2);
|
this->display_oscillator(true, 2);
|
||||||
@@ -307,7 +307,7 @@ bool AS3935Component::calibrate_oscillator() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AS3935Component::tune_antenna() {
|
void AS3935Component::tune_antenna() {
|
||||||
ESP_LOGI(TAG, "Starting antenna tuning");
|
ESP_LOGI(TAG, "Starting antenna tuning...");
|
||||||
uint8_t div_ratio = this->read_div_ratio();
|
uint8_t div_ratio = this->read_div_ratio();
|
||||||
uint8_t tune_val = this->read_capacitance();
|
uint8_t tune_val = this->read_capacitance();
|
||||||
ESP_LOGI(TAG, "Division Ratio is set to: %d", div_ratio);
|
ESP_LOGI(TAG, "Division Ratio is set to: %d", div_ratio);
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ static const uint8_t REGISTER_AGC = 0x1A; // 8 bytes / R
|
|||||||
static const uint8_t REGISTER_MAGNITUDE = 0x1B; // 16 bytes / R
|
static const uint8_t REGISTER_MAGNITUDE = 0x1B; // 16 bytes / R
|
||||||
|
|
||||||
void AS5600Component::setup() {
|
void AS5600Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AS5600...");
|
||||||
|
|
||||||
if (!this->read_byte(REGISTER_STATUS).has_value()) {
|
if (!this->read_byte(REGISTER_STATUS).has_value()) {
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
@@ -91,17 +91,15 @@ void AS5600Component::dump_config() {
|
|||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
|
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AS5600 failed!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Watchdog: %d", this->watchdog_);
|
||||||
" Watchdog: %d\n"
|
ESP_LOGCONFIG(TAG, " Fast Filter: %d", this->fast_filter_);
|
||||||
" Fast Filter: %d\n"
|
ESP_LOGCONFIG(TAG, " Slow Filter: %d", this->slow_filter_);
|
||||||
" Slow Filter: %d\n"
|
ESP_LOGCONFIG(TAG, " Hysteresis: %d", this->hysteresis_);
|
||||||
" Hysteresis: %d\n"
|
ESP_LOGCONFIG(TAG, " Start Position: %d", this->start_position_);
|
||||||
" Start Position: %d",
|
|
||||||
this->watchdog_, this->fast_filter_, this->slow_filter_, this->hysteresis_, this->start_position_);
|
|
||||||
if (this->end_mode_ == END_MODE_POSITION) {
|
if (this->end_mode_ == END_MODE_POSITION) {
|
||||||
ESP_LOGCONFIG(TAG, " End Position: %d", this->end_position_);
|
ESP_LOGCONFIG(TAG, " End Position: %d", this->end_position_);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace as7341 {
|
|||||||
static const char *const TAG = "as7341";
|
static const char *const TAG = "as7341";
|
||||||
|
|
||||||
void AS7341Component::setup() {
|
void AS7341Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AS7341...");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
|
|
||||||
// Verify device ID
|
// Verify device ID
|
||||||
@@ -38,14 +38,12 @@ void AS7341Component::dump_config() {
|
|||||||
ESP_LOGCONFIG(TAG, "AS7341:");
|
ESP_LOGCONFIG(TAG, "AS7341:");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with AS7341 failed!");
|
||||||
}
|
}
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Gain: %u", get_gain());
|
||||||
" Gain: %u\n"
|
ESP_LOGCONFIG(TAG, " ATIME: %u", get_atime());
|
||||||
" ATIME: %u\n"
|
ESP_LOGCONFIG(TAG, " ASTEP: %u", get_astep());
|
||||||
" ASTEP: %u",
|
|
||||||
get_gain(), get_atime(), get_astep());
|
|
||||||
|
|
||||||
LOG_SENSOR(" ", "F1", this->f1_);
|
LOG_SENSOR(" ", "F1", this->f1_);
|
||||||
LOG_SENSOR(" ", "F2", this->f2_);
|
LOG_SENSOR(" ", "F2", this->f2_);
|
||||||
|
|||||||
@@ -71,22 +71,19 @@ bool AT581XComponent::i2c_read_reg(uint8_t addr, uint8_t &data) {
|
|||||||
return this->read_register(addr, &data, 1) == esphome::i2c::NO_ERROR;
|
return this->read_register(addr, &data, 1) == esphome::i2c::NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AT581XComponent::setup() { ESP_LOGCONFIG(TAG, "Running setup"); }
|
void AT581XComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up AT581X..."); }
|
||||||
void AT581XComponent::dump_config() { LOG_I2C_DEVICE(this); }
|
void AT581XComponent::dump_config() { LOG_I2C_DEVICE(this); }
|
||||||
#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
|
#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
|
||||||
bool AT581XComponent::i2c_write_config() {
|
bool AT581XComponent::i2c_write_config() {
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, "Writing new config for AT581X...");
|
||||||
"Writing new config for AT581X\n"
|
ESP_LOGCONFIG(TAG, "Frequency: %dMHz", this->freq_);
|
||||||
"Frequency: %dMHz\n"
|
ESP_LOGCONFIG(TAG, "Sensing distance: %d", this->delta_);
|
||||||
"Sensing distance: %d\n"
|
ESP_LOGCONFIG(TAG, "Power: %dµA", this->power_);
|
||||||
"Power: %dµA\n"
|
ESP_LOGCONFIG(TAG, "Gain: %d", this->gain_);
|
||||||
"Gain: %d\n"
|
ESP_LOGCONFIG(TAG, "Trigger base time: %dms", this->trigger_base_time_ms_);
|
||||||
"Trigger base time: %dms\n"
|
ESP_LOGCONFIG(TAG, "Trigger keep time: %dms", this->trigger_keep_time_ms_);
|
||||||
"Trigger keep time: %dms\n"
|
ESP_LOGCONFIG(TAG, "Protect time: %dms", this->protect_time_ms_);
|
||||||
"Protect time: %dms\n"
|
ESP_LOGCONFIG(TAG, "Self check time: %dms", this->self_check_time_ms_);
|
||||||
"Self check time: %dms",
|
|
||||||
this->freq_, this->delta_, this->power_, this->gain_, this->trigger_base_time_ms_,
|
|
||||||
this->trigger_keep_time_ms_, this->protect_time_ms_, this->self_check_time_ms_);
|
|
||||||
|
|
||||||
// Set frequency point
|
// Set frequency point
|
||||||
if (!this->i2c_write_reg(FREQ_ADDR, GAIN61_VALUE)) {
|
if (!this->i2c_write_reg(FREQ_ADDR, GAIN61_VALUE)) {
|
||||||
|
|||||||
@@ -14,8 +14,11 @@ namespace esphome {
|
|||||||
namespace at581x {
|
namespace at581x {
|
||||||
|
|
||||||
class AT581XComponent : public Component, public i2c::I2CDevice {
|
class AT581XComponent : public Component, public i2c::I2CDevice {
|
||||||
public:
|
|
||||||
#ifdef USE_SWITCH
|
#ifdef USE_SWITCH
|
||||||
|
protected:
|
||||||
|
switch_::Switch *rf_power_switch_{nullptr};
|
||||||
|
|
||||||
|
public:
|
||||||
void set_rf_power_switch(switch_::Switch *s) {
|
void set_rf_power_switch(switch_::Switch *s) {
|
||||||
this->rf_power_switch_ = s;
|
this->rf_power_switch_ = s;
|
||||||
s->turn_on();
|
s->turn_on();
|
||||||
@@ -45,9 +48,6 @@ class AT581XComponent : public Component, public i2c::I2CDevice {
|
|||||||
bool i2c_read_reg(uint8_t addr, uint8_t &data);
|
bool i2c_read_reg(uint8_t addr, uint8_t &data);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
#ifdef USE_SWITCH
|
|
||||||
switch_::Switch *rf_power_switch_{nullptr};
|
|
||||||
#endif
|
|
||||||
int freq_;
|
int freq_;
|
||||||
int self_check_time_ms_; /*!< Power-on self-test time, range: 0 ~ 65536 ms */
|
int self_check_time_ms_; /*!< Power-on self-test time, range: 0 ~ 65536 ms */
|
||||||
int protect_time_ms_; /*!< Protection time, recommended 1000 ms */
|
int protect_time_ms_; /*!< Protection time, recommended 1000 ms */
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ void ATM90E26Component::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ATM90E26Component::setup() {
|
void ATM90E26Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up ATM90E26 Component...");
|
||||||
this->spi_setup();
|
this->spi_setup();
|
||||||
|
|
||||||
uint16_t mmode = 0x422; // default values for everything but L/N line current gains
|
uint16_t mmode = 0x422; // default values for everything but L/N line current gains
|
||||||
@@ -135,7 +135,7 @@ void ATM90E26Component::dump_config() {
|
|||||||
ESP_LOGCONFIG("", "ATM90E26:");
|
ESP_LOGCONFIG("", "ATM90E26:");
|
||||||
LOG_PIN(" CS Pin: ", this->cs_);
|
LOG_PIN(" CS Pin: ", this->cs_);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with ATM90E26 failed!");
|
||||||
}
|
}
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
LOG_SENSOR(" ", "Voltage A", this->voltage_sensor_);
|
LOG_SENSOR(" ", "Voltage A", this->voltage_sensor_);
|
||||||
|
|||||||
@@ -3,6 +3,5 @@ import esphome.codegen as cg
|
|||||||
CODEOWNERS = ["@circuitsetup", "@descipher"]
|
CODEOWNERS = ["@circuitsetup", "@descipher"]
|
||||||
|
|
||||||
atm90e32_ns = cg.esphome_ns.namespace("atm90e32")
|
atm90e32_ns = cg.esphome_ns.namespace("atm90e32")
|
||||||
ATM90E32Component = atm90e32_ns.class_("ATM90E32Component", cg.Component)
|
|
||||||
|
|
||||||
CONF_ATM90E32_ID = "atm90e32_id"
|
CONF_ATM90E32_ID = "atm90e32_id"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "atm90e32.h"
|
#include "atm90e32.h"
|
||||||
#include <cinttypes>
|
#include "atm90e32_reg.h"
|
||||||
#include <cmath>
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include <cinttypes>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace atm90e32 {
|
namespace atm90e32 {
|
||||||
@@ -11,84 +11,115 @@ void ATM90E32Component::loop() {
|
|||||||
if (this->get_publish_interval_flag_()) {
|
if (this->get_publish_interval_flag_()) {
|
||||||
this->set_publish_interval_flag_(false);
|
this->set_publish_interval_flag_(false);
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
if (this->phase_[phase].voltage_sensor_ != nullptr)
|
if (this->phase_[phase].voltage_sensor_ != nullptr) {
|
||||||
this->phase_[phase].voltage_ = this->get_phase_voltage_(phase);
|
this->phase_[phase].voltage_ = this->get_phase_voltage_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].current_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].current_sensor_ != nullptr) {
|
||||||
this->phase_[phase].current_ = this->get_phase_current_(phase);
|
this->phase_[phase].current_ = this->get_phase_current_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].power_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].power_sensor_ != nullptr) {
|
||||||
this->phase_[phase].active_power_ = this->get_phase_active_power_(phase);
|
this->phase_[phase].active_power_ = this->get_phase_active_power_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].power_factor_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].power_factor_sensor_ != nullptr) {
|
||||||
this->phase_[phase].power_factor_ = this->get_phase_power_factor_(phase);
|
this->phase_[phase].power_factor_ = this->get_phase_power_factor_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].reactive_power_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].reactive_power_sensor_ != nullptr) {
|
||||||
this->phase_[phase].reactive_power_ = this->get_phase_reactive_power_(phase);
|
this->phase_[phase].reactive_power_ = this->get_phase_reactive_power_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].apparent_power_sensor_ != nullptr)
|
}
|
||||||
this->phase_[phase].apparent_power_ = this->get_phase_apparent_power_(phase);
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].forward_active_energy_sensor_ != nullptr) {
|
||||||
if (this->phase_[phase].forward_active_energy_sensor_ != nullptr)
|
|
||||||
this->phase_[phase].forward_active_energy_ = this->get_phase_forward_active_energy_(phase);
|
this->phase_[phase].forward_active_energy_ = this->get_phase_forward_active_energy_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].reverse_active_energy_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].reverse_active_energy_sensor_ != nullptr) {
|
||||||
this->phase_[phase].reverse_active_energy_ = this->get_phase_reverse_active_energy_(phase);
|
this->phase_[phase].reverse_active_energy_ = this->get_phase_reverse_active_energy_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].phase_angle_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].phase_angle_sensor_ != nullptr) {
|
||||||
this->phase_[phase].phase_angle_ = this->get_phase_angle_(phase);
|
this->phase_[phase].phase_angle_ = this->get_phase_angle_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].harmonic_active_power_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].harmonic_active_power_sensor_ != nullptr) {
|
||||||
this->phase_[phase].harmonic_active_power_ = this->get_phase_harmonic_active_power_(phase);
|
this->phase_[phase].harmonic_active_power_ = this->get_phase_harmonic_active_power_(phase);
|
||||||
|
}
|
||||||
if (this->phase_[phase].peak_current_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].peak_current_sensor_ != nullptr) {
|
||||||
this->phase_[phase].peak_current_ = this->get_phase_peak_current_(phase);
|
this->phase_[phase].peak_current_ = this->get_phase_peak_current_(phase);
|
||||||
|
}
|
||||||
// After the local store is collected we can publish them trusting they are within +-1 hardware sampling
|
}
|
||||||
if (this->phase_[phase].voltage_sensor_ != nullptr)
|
// After the local store in collected we can publish them trusting they are withing +-1 haardware sampling
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].voltage_sensor_ != nullptr) {
|
||||||
this->phase_[phase].voltage_sensor_->publish_state(this->get_local_phase_voltage_(phase));
|
this->phase_[phase].voltage_sensor_->publish_state(this->get_local_phase_voltage_(phase));
|
||||||
|
}
|
||||||
if (this->phase_[phase].current_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].current_sensor_ != nullptr) {
|
||||||
this->phase_[phase].current_sensor_->publish_state(this->get_local_phase_current_(phase));
|
this->phase_[phase].current_sensor_->publish_state(this->get_local_phase_current_(phase));
|
||||||
|
}
|
||||||
if (this->phase_[phase].power_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].power_sensor_ != nullptr) {
|
||||||
this->phase_[phase].power_sensor_->publish_state(this->get_local_phase_active_power_(phase));
|
this->phase_[phase].power_sensor_->publish_state(this->get_local_phase_active_power_(phase));
|
||||||
|
}
|
||||||
if (this->phase_[phase].power_factor_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].power_factor_sensor_ != nullptr) {
|
||||||
this->phase_[phase].power_factor_sensor_->publish_state(this->get_local_phase_power_factor_(phase));
|
this->phase_[phase].power_factor_sensor_->publish_state(this->get_local_phase_power_factor_(phase));
|
||||||
|
}
|
||||||
if (this->phase_[phase].reactive_power_sensor_ != nullptr)
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].reactive_power_sensor_ != nullptr) {
|
||||||
this->phase_[phase].reactive_power_sensor_->publish_state(this->get_local_phase_reactive_power_(phase));
|
this->phase_[phase].reactive_power_sensor_->publish_state(this->get_local_phase_reactive_power_(phase));
|
||||||
|
}
|
||||||
if (this->phase_[phase].apparent_power_sensor_ != nullptr)
|
}
|
||||||
this->phase_[phase].apparent_power_sensor_->publish_state(this->get_local_phase_apparent_power_(phase));
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
|
||||||
if (this->phase_[phase].forward_active_energy_sensor_ != nullptr) {
|
if (this->phase_[phase].forward_active_energy_sensor_ != nullptr) {
|
||||||
this->phase_[phase].forward_active_energy_sensor_->publish_state(
|
this->phase_[phase].forward_active_energy_sensor_->publish_state(
|
||||||
this->get_local_phase_forward_active_energy_(phase));
|
this->get_local_phase_forward_active_energy_(phase));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
if (this->phase_[phase].reverse_active_energy_sensor_ != nullptr) {
|
if (this->phase_[phase].reverse_active_energy_sensor_ != nullptr) {
|
||||||
this->phase_[phase].reverse_active_energy_sensor_->publish_state(
|
this->phase_[phase].reverse_active_energy_sensor_->publish_state(
|
||||||
this->get_local_phase_reverse_active_energy_(phase));
|
this->get_local_phase_reverse_active_energy_(phase));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (this->phase_[phase].phase_angle_sensor_ != nullptr)
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].phase_angle_sensor_ != nullptr) {
|
||||||
this->phase_[phase].phase_angle_sensor_->publish_state(this->get_local_phase_angle_(phase));
|
this->phase_[phase].phase_angle_sensor_->publish_state(this->get_local_phase_angle_(phase));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
if (this->phase_[phase].harmonic_active_power_sensor_ != nullptr) {
|
if (this->phase_[phase].harmonic_active_power_sensor_ != nullptr) {
|
||||||
this->phase_[phase].harmonic_active_power_sensor_->publish_state(
|
this->phase_[phase].harmonic_active_power_sensor_->publish_state(
|
||||||
this->get_local_phase_harmonic_active_power_(phase));
|
this->get_local_phase_harmonic_active_power_(phase));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->phase_[phase].peak_current_sensor_ != nullptr)
|
|
||||||
this->phase_[phase].peak_current_sensor_->publish_state(this->get_local_phase_peak_current_(phase));
|
|
||||||
}
|
}
|
||||||
if (this->freq_sensor_ != nullptr)
|
for (uint8_t phase = 0; phase < 3; phase++) {
|
||||||
|
if (this->phase_[phase].peak_current_sensor_ != nullptr) {
|
||||||
|
this->phase_[phase].peak_current_sensor_->publish_state(this->get_local_phase_peak_current_(phase));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this->freq_sensor_ != nullptr) {
|
||||||
this->freq_sensor_->publish_state(this->get_frequency_());
|
this->freq_sensor_->publish_state(this->get_frequency_());
|
||||||
|
}
|
||||||
if (this->chip_temperature_sensor_ != nullptr)
|
if (this->chip_temperature_sensor_ != nullptr) {
|
||||||
this->chip_temperature_sensor_->publish_state(this->get_chip_temperature_());
|
this->chip_temperature_sensor_->publish_state(this->get_chip_temperature_());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,30 +130,82 @@ void ATM90E32Component::update() {
|
|||||||
}
|
}
|
||||||
this->set_publish_interval_flag_(true);
|
this->set_publish_interval_flag_(true);
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
void ATM90E32Component::restore_calibrations_() {
|
||||||
this->check_phase_status();
|
if (enable_offset_calibration_) {
|
||||||
this->check_over_current();
|
this->pref_.load(&this->offset_phase_);
|
||||||
this->check_freq_status();
|
}
|
||||||
#endif
|
};
|
||||||
|
|
||||||
|
void ATM90E32Component::run_offset_calibrations() {
|
||||||
|
// Run the calibrations and
|
||||||
|
// Setup voltage and current calibration offsets for PHASE A
|
||||||
|
this->offset_phase_[PHASEA].voltage_offset_ = calibrate_voltage_offset_phase(PHASEA);
|
||||||
|
this->phase_[PHASEA].voltage_offset_ = this->offset_phase_[PHASEA].voltage_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_UOFFSETA, this->phase_[PHASEA].voltage_offset_); // C Voltage offset
|
||||||
|
this->offset_phase_[PHASEA].current_offset_ = calibrate_current_offset_phase(PHASEA);
|
||||||
|
this->phase_[PHASEA].current_offset_ = this->offset_phase_[PHASEA].current_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_IOFFSETA, this->phase_[PHASEA].current_offset_); // C Current offset
|
||||||
|
// Setup voltage and current calibration offsets for PHASE B
|
||||||
|
this->offset_phase_[PHASEB].voltage_offset_ = calibrate_voltage_offset_phase(PHASEB);
|
||||||
|
this->phase_[PHASEB].voltage_offset_ = this->offset_phase_[PHASEB].voltage_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_UOFFSETB, this->phase_[PHASEB].voltage_offset_); // C Voltage offset
|
||||||
|
this->offset_phase_[PHASEB].current_offset_ = calibrate_current_offset_phase(PHASEB);
|
||||||
|
this->phase_[PHASEB].current_offset_ = this->offset_phase_[PHASEB].current_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_IOFFSETB, this->phase_[PHASEB].current_offset_); // C Current offset
|
||||||
|
// Setup voltage and current calibration offsets for PHASE C
|
||||||
|
this->offset_phase_[PHASEC].voltage_offset_ = calibrate_voltage_offset_phase(PHASEC);
|
||||||
|
this->phase_[PHASEC].voltage_offset_ = this->offset_phase_[PHASEC].voltage_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_UOFFSETC, this->phase_[PHASEC].voltage_offset_); // C Voltage offset
|
||||||
|
this->offset_phase_[PHASEC].current_offset_ = calibrate_current_offset_phase(PHASEC);
|
||||||
|
this->phase_[PHASEC].current_offset_ = this->offset_phase_[PHASEC].current_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_IOFFSETC, this->phase_[PHASEC].current_offset_); // C Current offset
|
||||||
|
this->pref_.save(&this->offset_phase_);
|
||||||
|
ESP_LOGI(TAG, "PhaseA Vo=%5d PhaseB Vo=%5d PhaseC Vo=%5d", this->offset_phase_[PHASEA].voltage_offset_,
|
||||||
|
this->offset_phase_[PHASEB].voltage_offset_, this->offset_phase_[PHASEC].voltage_offset_);
|
||||||
|
ESP_LOGI(TAG, "PhaseA Io=%5d PhaseB Io=%5d PhaseC Io=%5d", this->offset_phase_[PHASEA].current_offset_,
|
||||||
|
this->offset_phase_[PHASEB].current_offset_, this->offset_phase_[PHASEC].current_offset_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ATM90E32Component::clear_offset_calibrations() {
|
||||||
|
// Clear the calibrations and
|
||||||
|
this->offset_phase_[PHASEA].voltage_offset_ = 0;
|
||||||
|
this->phase_[PHASEA].voltage_offset_ = this->offset_phase_[PHASEA].voltage_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_UOFFSETA, this->phase_[PHASEA].voltage_offset_); // C Voltage offset
|
||||||
|
this->offset_phase_[PHASEA].current_offset_ = 0;
|
||||||
|
this->phase_[PHASEA].current_offset_ = this->offset_phase_[PHASEA].current_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_IOFFSETA, this->phase_[PHASEA].current_offset_); // C Current offset
|
||||||
|
this->offset_phase_[PHASEB].voltage_offset_ = 0;
|
||||||
|
this->phase_[PHASEB].voltage_offset_ = this->offset_phase_[PHASEB].voltage_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_UOFFSETB, this->phase_[PHASEB].voltage_offset_); // C Voltage offset
|
||||||
|
this->offset_phase_[PHASEB].current_offset_ = 0;
|
||||||
|
this->phase_[PHASEB].current_offset_ = this->offset_phase_[PHASEB].current_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_IOFFSETB, this->phase_[PHASEB].current_offset_); // C Current offset
|
||||||
|
this->offset_phase_[PHASEC].voltage_offset_ = 0;
|
||||||
|
this->phase_[PHASEC].voltage_offset_ = this->offset_phase_[PHASEC].voltage_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_UOFFSETC, this->phase_[PHASEC].voltage_offset_); // C Voltage offset
|
||||||
|
this->offset_phase_[PHASEC].current_offset_ = 0;
|
||||||
|
this->phase_[PHASEC].current_offset_ = this->offset_phase_[PHASEC].current_offset_;
|
||||||
|
this->write16_(ATM90E32_REGISTER_IOFFSETC, this->phase_[PHASEC].current_offset_); // C Current offset
|
||||||
|
this->pref_.save(&this->offset_phase_);
|
||||||
|
ESP_LOGI(TAG, "PhaseA Vo=%5d PhaseB Vo=%5d PhaseC Vo=%5d", this->offset_phase_[PHASEA].voltage_offset_,
|
||||||
|
this->offset_phase_[PHASEB].voltage_offset_, this->offset_phase_[PHASEC].voltage_offset_);
|
||||||
|
ESP_LOGI(TAG, "PhaseA Io=%5d PhaseB Io=%5d PhaseC Io=%5d", this->offset_phase_[PHASEA].current_offset_,
|
||||||
|
this->offset_phase_[PHASEB].current_offset_, this->offset_phase_[PHASEC].current_offset_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ATM90E32Component::setup() {
|
void ATM90E32Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up ATM90E32 Component...");
|
||||||
this->spi_setup();
|
this->spi_setup();
|
||||||
|
if (this->enable_offset_calibration_) {
|
||||||
|
uint32_t hash = fnv1_hash(App.get_friendly_name());
|
||||||
|
this->pref_ = global_preferences->make_preference<Calibration[3]>(hash, true);
|
||||||
|
this->restore_calibrations_();
|
||||||
|
}
|
||||||
uint16_t mmode0 = 0x87; // 3P4W 50Hz
|
uint16_t mmode0 = 0x87; // 3P4W 50Hz
|
||||||
uint16_t high_thresh = 0;
|
|
||||||
uint16_t low_thresh = 0;
|
|
||||||
|
|
||||||
if (line_freq_ == 60) {
|
if (line_freq_ == 60) {
|
||||||
mmode0 |= 1 << 12; // sets 12th bit to 1, 60Hz
|
mmode0 |= 1 << 12; // sets 12th bit to 1, 60Hz
|
||||||
// for freq threshold registers
|
|
||||||
high_thresh = 6300; // 63.00 Hz
|
|
||||||
low_thresh = 5700; // 57.00 Hz
|
|
||||||
} else {
|
|
||||||
high_thresh = 5300; // 53.00 Hz
|
|
||||||
low_thresh = 4700; // 47.00 Hz
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current_phases_ == 2) {
|
if (current_phases_ == 2) {
|
||||||
@@ -133,98 +216,47 @@ void ATM90E32Component::setup() {
|
|||||||
this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A); // Perform soft reset
|
this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A); // Perform soft reset
|
||||||
delay(6); // Wait for the minimum 5ms + 1ms
|
delay(6); // Wait for the minimum 5ms + 1ms
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA); // enable register config access
|
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA); // enable register config access
|
||||||
if (!this->validate_spi_read_(0x55AA, "setup()")) {
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != 0x55AA) {
|
||||||
ESP_LOGW(TAG, "Could not initialize ATM90E32 IC, check SPI settings");
|
ESP_LOGW(TAG, "Could not initialize ATM90E32 IC, check SPI settings");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->write16_(ATM90E32_REGISTER_METEREN, 0x0001); // Enable Metering
|
this->write16_(ATM90E32_REGISTER_METEREN, 0x0001); // Enable Metering
|
||||||
this->write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F); // Peak Detector time (15:8) 255ms, Sag Period (7:0) 63ms
|
this->write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F); // Peak Detector time ms (15:8), Sag Period ms (7:0)
|
||||||
this->write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861); // PL Constant MSB (default) = 140625000
|
this->write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861); // PL Constant MSB (default) = 140625000
|
||||||
this->write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468); // PL Constant LSB (default)
|
this->write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468); // PL Constant LSB (default)
|
||||||
this->write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654); // Zero crossing (ZX2, ZX1, ZX0) pin config
|
this->write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654); // ZX2, ZX1, ZX0 pin config
|
||||||
this->write16_(ATM90E32_REGISTER_MMODE0, mmode0); // Mode Config (frequency set in main program)
|
this->write16_(ATM90E32_REGISTER_MMODE0, mmode0); // Mode Config (frequency set in main program)
|
||||||
this->write16_(ATM90E32_REGISTER_MMODE1, pga_gain_); // PGA Gain Configuration for Current Channels
|
this->write16_(ATM90E32_REGISTER_MMODE1, pga_gain_); // PGA Gain Configuration for Current Channels
|
||||||
this->write16_(ATM90E32_REGISTER_FREQHITH, high_thresh); // Frequency high threshold
|
|
||||||
this->write16_(ATM90E32_REGISTER_FREQLOTH, low_thresh); // Frequency low threshold
|
|
||||||
this->write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C); // All Active Startup Power Threshold - 0.02A/0.00032 = 7500
|
this->write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C); // All Active Startup Power Threshold - 0.02A/0.00032 = 7500
|
||||||
this->write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C); // All Reactive Startup Power Threshold - 50%
|
this->write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C); // All Reactive Startup Power Threshold - 50%
|
||||||
this->write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C); // All Reactive Startup Power Threshold - 50%
|
this->write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C); // All Reactive Startup Power Threshold - 50%
|
||||||
this->write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE); // Each Phase Active Phase Threshold - 0.002A/0.00032 = 750
|
this->write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE); // Each Phase Active Phase Threshold - 0.002A/0.00032 = 750
|
||||||
this->write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE); // Each phase Reactive Phase Threshold - 10%
|
this->write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE); // Each phase Reactive Phase Threshold - 10%
|
||||||
|
// Setup voltage and current gain for PHASE A
|
||||||
if (this->enable_offset_calibration_) {
|
this->write16_(ATM90E32_REGISTER_UGAINA, this->phase_[PHASEA].voltage_gain_); // A Voltage rms gain
|
||||||
// Initialize flash storage for offset calibrations
|
this->write16_(ATM90E32_REGISTER_IGAINA, this->phase_[PHASEA].ct_gain_); // A line current gain
|
||||||
uint32_t o_hash = fnv1_hash(std::string("_offset_calibration_") + this->cs_->dump_summary());
|
// Setup voltage and current gain for PHASE B
|
||||||
this->offset_pref_ = global_preferences->make_preference<OffsetCalibration[3]>(o_hash, true);
|
this->write16_(ATM90E32_REGISTER_UGAINB, this->phase_[PHASEB].voltage_gain_); // B Voltage rms gain
|
||||||
this->restore_offset_calibrations_();
|
this->write16_(ATM90E32_REGISTER_IGAINB, this->phase_[PHASEB].ct_gain_); // B line current gain
|
||||||
|
// Setup voltage and current gain for PHASE C
|
||||||
// Initialize flash storage for power offset calibrations
|
this->write16_(ATM90E32_REGISTER_UGAINC, this->phase_[PHASEC].voltage_gain_); // C Voltage rms gain
|
||||||
uint32_t po_hash = fnv1_hash(std::string("_power_offset_calibration_") + this->cs_->dump_summary());
|
this->write16_(ATM90E32_REGISTER_IGAINC, this->phase_[PHASEC].ct_gain_); // C line current gain
|
||||||
this->power_offset_pref_ = global_preferences->make_preference<PowerOffsetCalibration[3]>(po_hash, true);
|
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000); // end configuration
|
||||||
this->restore_power_offset_calibrations_();
|
|
||||||
} else {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Power & Voltage/Current offset calibration is disabled. Using config file values.");
|
|
||||||
for (uint8_t phase = 0; phase < 3; ++phase) {
|
|
||||||
this->write16_(this->voltage_offset_registers[phase],
|
|
||||||
static_cast<uint16_t>(this->offset_phase_[phase].voltage_offset_));
|
|
||||||
this->write16_(this->current_offset_registers[phase],
|
|
||||||
static_cast<uint16_t>(this->offset_phase_[phase].current_offset_));
|
|
||||||
this->write16_(this->power_offset_registers[phase],
|
|
||||||
static_cast<uint16_t>(this->power_offset_phase_[phase].active_power_offset));
|
|
||||||
this->write16_(this->reactive_power_offset_registers[phase],
|
|
||||||
static_cast<uint16_t>(this->power_offset_phase_[phase].reactive_power_offset));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this->enable_gain_calibration_) {
|
|
||||||
// Initialize flash storage for gain calibration
|
|
||||||
uint32_t g_hash = fnv1_hash(std::string("_gain_calibration_") + this->cs_->dump_summary());
|
|
||||||
this->gain_calibration_pref_ = global_preferences->make_preference<GainCalibration[3]>(g_hash, true);
|
|
||||||
this->restore_gain_calibrations_();
|
|
||||||
|
|
||||||
if (this->using_saved_calibrations_) {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Successfully restored gain calibration from memory.");
|
|
||||||
} else {
|
|
||||||
for (uint8_t phase = 0; phase < 3; ++phase) {
|
|
||||||
this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
|
|
||||||
this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Gain calibration is disabled. Using config file values.");
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; ++phase) {
|
|
||||||
this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
|
|
||||||
this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sag threshold (78%)
|
|
||||||
uint16_t sagth = calculate_voltage_threshold(line_freq_, this->phase_[0].voltage_gain_, 0.78f);
|
|
||||||
// Overvoltage threshold (122%)
|
|
||||||
uint16_t ovth = calculate_voltage_threshold(line_freq_, this->phase_[0].voltage_gain_, 1.22f);
|
|
||||||
|
|
||||||
// Write to registers
|
|
||||||
this->write16_(ATM90E32_REGISTER_SAGTH, sagth);
|
|
||||||
this->write16_(ATM90E32_REGISTER_OVTH, ovth);
|
|
||||||
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000); // end configuration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ATM90E32Component::dump_config() {
|
void ATM90E32Component::dump_config() {
|
||||||
ESP_LOGCONFIG("", "ATM90E32:");
|
ESP_LOGCONFIG("", "ATM90E32:");
|
||||||
LOG_PIN(" CS Pin: ", this->cs_);
|
LOG_PIN(" CS Pin: ", this->cs_);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
|
ESP_LOGE(TAG, "Communication with ATM90E32 failed!");
|
||||||
}
|
}
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
LOG_SENSOR(" ", "Voltage A", this->phase_[PHASEA].voltage_sensor_);
|
LOG_SENSOR(" ", "Voltage A", this->phase_[PHASEA].voltage_sensor_);
|
||||||
LOG_SENSOR(" ", "Current A", this->phase_[PHASEA].current_sensor_);
|
LOG_SENSOR(" ", "Current A", this->phase_[PHASEA].current_sensor_);
|
||||||
LOG_SENSOR(" ", "Power A", this->phase_[PHASEA].power_sensor_);
|
LOG_SENSOR(" ", "Power A", this->phase_[PHASEA].power_sensor_);
|
||||||
LOG_SENSOR(" ", "Reactive Power A", this->phase_[PHASEA].reactive_power_sensor_);
|
LOG_SENSOR(" ", "Reactive Power A", this->phase_[PHASEA].reactive_power_sensor_);
|
||||||
LOG_SENSOR(" ", "Apparent Power A", this->phase_[PHASEA].apparent_power_sensor_);
|
|
||||||
LOG_SENSOR(" ", "PF A", this->phase_[PHASEA].power_factor_sensor_);
|
LOG_SENSOR(" ", "PF A", this->phase_[PHASEA].power_factor_sensor_);
|
||||||
LOG_SENSOR(" ", "Active Forward Energy A", this->phase_[PHASEA].forward_active_energy_sensor_);
|
LOG_SENSOR(" ", "Active Forward Energy A", this->phase_[PHASEA].forward_active_energy_sensor_);
|
||||||
LOG_SENSOR(" ", "Active Reverse Energy A", this->phase_[PHASEA].reverse_active_energy_sensor_);
|
LOG_SENSOR(" ", "Active Reverse Energy A", this->phase_[PHASEA].reverse_active_energy_sensor_);
|
||||||
@@ -235,24 +267,22 @@ void ATM90E32Component::dump_config() {
|
|||||||
LOG_SENSOR(" ", "Current B", this->phase_[PHASEB].current_sensor_);
|
LOG_SENSOR(" ", "Current B", this->phase_[PHASEB].current_sensor_);
|
||||||
LOG_SENSOR(" ", "Power B", this->phase_[PHASEB].power_sensor_);
|
LOG_SENSOR(" ", "Power B", this->phase_[PHASEB].power_sensor_);
|
||||||
LOG_SENSOR(" ", "Reactive Power B", this->phase_[PHASEB].reactive_power_sensor_);
|
LOG_SENSOR(" ", "Reactive Power B", this->phase_[PHASEB].reactive_power_sensor_);
|
||||||
LOG_SENSOR(" ", "Apparent Power B", this->phase_[PHASEB].apparent_power_sensor_);
|
|
||||||
LOG_SENSOR(" ", "PF B", this->phase_[PHASEB].power_factor_sensor_);
|
LOG_SENSOR(" ", "PF B", this->phase_[PHASEB].power_factor_sensor_);
|
||||||
LOG_SENSOR(" ", "Active Forward Energy B", this->phase_[PHASEB].forward_active_energy_sensor_);
|
LOG_SENSOR(" ", "Active Forward Energy B", this->phase_[PHASEB].forward_active_energy_sensor_);
|
||||||
LOG_SENSOR(" ", "Active Reverse Energy B", this->phase_[PHASEB].reverse_active_energy_sensor_);
|
LOG_SENSOR(" ", "Active Reverse Energy B", this->phase_[PHASEB].reverse_active_energy_sensor_);
|
||||||
LOG_SENSOR(" ", "Harmonic Power B", this->phase_[PHASEB].harmonic_active_power_sensor_);
|
LOG_SENSOR(" ", "Harmonic Power A", this->phase_[PHASEB].harmonic_active_power_sensor_);
|
||||||
LOG_SENSOR(" ", "Phase Angle B", this->phase_[PHASEB].phase_angle_sensor_);
|
LOG_SENSOR(" ", "Phase Angle A", this->phase_[PHASEB].phase_angle_sensor_);
|
||||||
LOG_SENSOR(" ", "Peak Current B", this->phase_[PHASEB].peak_current_sensor_);
|
LOG_SENSOR(" ", "Peak Current A", this->phase_[PHASEB].peak_current_sensor_);
|
||||||
LOG_SENSOR(" ", "Voltage C", this->phase_[PHASEC].voltage_sensor_);
|
LOG_SENSOR(" ", "Voltage C", this->phase_[PHASEC].voltage_sensor_);
|
||||||
LOG_SENSOR(" ", "Current C", this->phase_[PHASEC].current_sensor_);
|
LOG_SENSOR(" ", "Current C", this->phase_[PHASEC].current_sensor_);
|
||||||
LOG_SENSOR(" ", "Power C", this->phase_[PHASEC].power_sensor_);
|
LOG_SENSOR(" ", "Power C", this->phase_[PHASEC].power_sensor_);
|
||||||
LOG_SENSOR(" ", "Reactive Power C", this->phase_[PHASEC].reactive_power_sensor_);
|
LOG_SENSOR(" ", "Reactive Power C", this->phase_[PHASEC].reactive_power_sensor_);
|
||||||
LOG_SENSOR(" ", "Apparent Power C", this->phase_[PHASEC].apparent_power_sensor_);
|
|
||||||
LOG_SENSOR(" ", "PF C", this->phase_[PHASEC].power_factor_sensor_);
|
LOG_SENSOR(" ", "PF C", this->phase_[PHASEC].power_factor_sensor_);
|
||||||
LOG_SENSOR(" ", "Active Forward Energy C", this->phase_[PHASEC].forward_active_energy_sensor_);
|
LOG_SENSOR(" ", "Active Forward Energy C", this->phase_[PHASEC].forward_active_energy_sensor_);
|
||||||
LOG_SENSOR(" ", "Active Reverse Energy C", this->phase_[PHASEC].reverse_active_energy_sensor_);
|
LOG_SENSOR(" ", "Active Reverse Energy C", this->phase_[PHASEC].reverse_active_energy_sensor_);
|
||||||
LOG_SENSOR(" ", "Harmonic Power C", this->phase_[PHASEC].harmonic_active_power_sensor_);
|
LOG_SENSOR(" ", "Harmonic Power A", this->phase_[PHASEC].harmonic_active_power_sensor_);
|
||||||
LOG_SENSOR(" ", "Phase Angle C", this->phase_[PHASEC].phase_angle_sensor_);
|
LOG_SENSOR(" ", "Phase Angle A", this->phase_[PHASEC].phase_angle_sensor_);
|
||||||
LOG_SENSOR(" ", "Peak Current C", this->phase_[PHASEC].peak_current_sensor_);
|
LOG_SENSOR(" ", "Peak Current A", this->phase_[PHASEC].peak_current_sensor_);
|
||||||
LOG_SENSOR(" ", "Frequency", this->freq_sensor_);
|
LOG_SENSOR(" ", "Frequency", this->freq_sensor_);
|
||||||
LOG_SENSOR(" ", "Chip Temp", this->chip_temperature_sensor_);
|
LOG_SENSOR(" ", "Chip Temp", this->chip_temperature_sensor_);
|
||||||
}
|
}
|
||||||
@@ -268,7 +298,7 @@ uint16_t ATM90E32Component::read16_(uint16_t a_register) {
|
|||||||
uint8_t data[2];
|
uint8_t data[2];
|
||||||
uint16_t output;
|
uint16_t output;
|
||||||
this->enable();
|
this->enable();
|
||||||
delay_microseconds_safe(1); // min delay between CS low and first SCK is 200ns - 1ms is plenty
|
delay_microseconds_safe(10);
|
||||||
this->write_byte(addrh);
|
this->write_byte(addrh);
|
||||||
this->write_byte(addrl);
|
this->write_byte(addrl);
|
||||||
this->read_array(data, 2);
|
this->read_array(data, 2);
|
||||||
@@ -298,7 +328,8 @@ void ATM90E32Component::write16_(uint16_t a_register, uint16_t val) {
|
|||||||
this->write_byte16(a_register);
|
this->write_byte16(a_register);
|
||||||
this->write_byte16(val);
|
this->write_byte16(val);
|
||||||
this->disable();
|
this->disable();
|
||||||
this->validate_spi_read_(val, "write16()");
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != val)
|
||||||
|
ESP_LOGW(TAG, "SPI write error 0x%04X val 0x%04X", a_register, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_local_phase_voltage_(uint8_t phase) { return this->phase_[phase].voltage_; }
|
float ATM90E32Component::get_local_phase_voltage_(uint8_t phase) { return this->phase_[phase].voltage_; }
|
||||||
@@ -309,8 +340,6 @@ float ATM90E32Component::get_local_phase_active_power_(uint8_t phase) { return t
|
|||||||
|
|
||||||
float ATM90E32Component::get_local_phase_reactive_power_(uint8_t phase) { return this->phase_[phase].reactive_power_; }
|
float ATM90E32Component::get_local_phase_reactive_power_(uint8_t phase) { return this->phase_[phase].reactive_power_; }
|
||||||
|
|
||||||
float ATM90E32Component::get_local_phase_apparent_power_(uint8_t phase) { return this->phase_[phase].apparent_power_; }
|
|
||||||
|
|
||||||
float ATM90E32Component::get_local_phase_power_factor_(uint8_t phase) { return this->phase_[phase].power_factor_; }
|
float ATM90E32Component::get_local_phase_power_factor_(uint8_t phase) { return this->phase_[phase].power_factor_; }
|
||||||
|
|
||||||
float ATM90E32Component::get_local_phase_forward_active_energy_(uint8_t phase) {
|
float ATM90E32Component::get_local_phase_forward_active_energy_(uint8_t phase) {
|
||||||
@@ -331,7 +360,8 @@ float ATM90E32Component::get_local_phase_peak_current_(uint8_t phase) { return t
|
|||||||
|
|
||||||
float ATM90E32Component::get_phase_voltage_(uint8_t phase) {
|
float ATM90E32Component::get_phase_voltage_(uint8_t phase) {
|
||||||
const uint16_t voltage = this->read16_(ATM90E32_REGISTER_URMS + phase);
|
const uint16_t voltage = this->read16_(ATM90E32_REGISTER_URMS + phase);
|
||||||
this->validate_spi_read_(voltage, "get_phase_voltage()");
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != voltage)
|
||||||
|
ESP_LOGW(TAG, "SPI URMS voltage register read error.");
|
||||||
return (float) voltage / 100;
|
return (float) voltage / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,7 +371,8 @@ float ATM90E32Component::get_phase_voltage_avg_(uint8_t phase) {
|
|||||||
uint16_t voltage = 0;
|
uint16_t voltage = 0;
|
||||||
for (uint8_t i = 0; i < reads; i++) {
|
for (uint8_t i = 0; i < reads; i++) {
|
||||||
voltage = this->read16_(ATM90E32_REGISTER_URMS + phase);
|
voltage = this->read16_(ATM90E32_REGISTER_URMS + phase);
|
||||||
this->validate_spi_read_(voltage, "get_phase_voltage_avg_()");
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != voltage)
|
||||||
|
ESP_LOGW(TAG, "SPI URMS voltage register read error.");
|
||||||
accumulation += voltage;
|
accumulation += voltage;
|
||||||
}
|
}
|
||||||
voltage = accumulation / reads;
|
voltage = accumulation / reads;
|
||||||
@@ -355,7 +386,8 @@ float ATM90E32Component::get_phase_current_avg_(uint8_t phase) {
|
|||||||
uint16_t current = 0;
|
uint16_t current = 0;
|
||||||
for (uint8_t i = 0; i < reads; i++) {
|
for (uint8_t i = 0; i < reads; i++) {
|
||||||
current = this->read16_(ATM90E32_REGISTER_IRMS + phase);
|
current = this->read16_(ATM90E32_REGISTER_IRMS + phase);
|
||||||
this->validate_spi_read_(current, "get_phase_current_avg_()");
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != current)
|
||||||
|
ESP_LOGW(TAG, "SPI IRMS current register read error.");
|
||||||
accumulation += current;
|
accumulation += current;
|
||||||
}
|
}
|
||||||
current = accumulation / reads;
|
current = accumulation / reads;
|
||||||
@@ -365,7 +397,8 @@ float ATM90E32Component::get_phase_current_avg_(uint8_t phase) {
|
|||||||
|
|
||||||
float ATM90E32Component::get_phase_current_(uint8_t phase) {
|
float ATM90E32Component::get_phase_current_(uint8_t phase) {
|
||||||
const uint16_t current = this->read16_(ATM90E32_REGISTER_IRMS + phase);
|
const uint16_t current = this->read16_(ATM90E32_REGISTER_IRMS + phase);
|
||||||
this->validate_spi_read_(current, "get_phase_current_()");
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != current)
|
||||||
|
ESP_LOGW(TAG, "SPI IRMS current register read error.");
|
||||||
return (float) current / 1000;
|
return (float) current / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,15 +412,11 @@ float ATM90E32Component::get_phase_reactive_power_(uint8_t phase) {
|
|||||||
return val * 0.00032f;
|
return val * 0.00032f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_phase_apparent_power_(uint8_t phase) {
|
|
||||||
const int val = this->read32_(ATM90E32_REGISTER_SMEAN + phase, ATM90E32_REGISTER_SMEANLSB + phase);
|
|
||||||
return val * 0.00032f;
|
|
||||||
}
|
|
||||||
|
|
||||||
float ATM90E32Component::get_phase_power_factor_(uint8_t phase) {
|
float ATM90E32Component::get_phase_power_factor_(uint8_t phase) {
|
||||||
uint16_t powerfactor = this->read16_(ATM90E32_REGISTER_PFMEAN + phase); // unsigned to compare to lastspidata
|
const int16_t powerfactor = this->read16_(ATM90E32_REGISTER_PFMEAN + phase);
|
||||||
this->validate_spi_read_(powerfactor, "get_phase_power_factor_()");
|
if (this->read16_(ATM90E32_REGISTER_LASTSPIDATA) != powerfactor)
|
||||||
return (float) ((int16_t) powerfactor) / 1000; // make it signed again
|
ESP_LOGW(TAG, "SPI power factor read error.");
|
||||||
|
return (float) powerfactor / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_phase_forward_active_energy_(uint8_t phase) {
|
float ATM90E32Component::get_phase_forward_active_energy_(uint8_t phase) {
|
||||||
@@ -397,19 +426,17 @@ float ATM90E32Component::get_phase_forward_active_energy_(uint8_t phase) {
|
|||||||
} else {
|
} else {
|
||||||
this->phase_[phase].cumulative_forward_active_energy_ = val;
|
this->phase_[phase].cumulative_forward_active_energy_ = val;
|
||||||
}
|
}
|
||||||
// 0.01CF resolution = 0.003125 Wh per count
|
return ((float) this->phase_[phase].cumulative_forward_active_energy_ * 10 / 3200);
|
||||||
return ((float) this->phase_[phase].cumulative_forward_active_energy_ * (10.0f / 3200.0f));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_phase_reverse_active_energy_(uint8_t phase) {
|
float ATM90E32Component::get_phase_reverse_active_energy_(uint8_t phase) {
|
||||||
const uint16_t val = this->read16_(ATM90E32_REGISTER_ANENERGY + phase);
|
const uint16_t val = this->read16_(ATM90E32_REGISTER_ANENERGY);
|
||||||
if (UINT32_MAX - this->phase_[phase].cumulative_reverse_active_energy_ > val) {
|
if (UINT32_MAX - this->phase_[phase].cumulative_reverse_active_energy_ > val) {
|
||||||
this->phase_[phase].cumulative_reverse_active_energy_ += val;
|
this->phase_[phase].cumulative_reverse_active_energy_ += val;
|
||||||
} else {
|
} else {
|
||||||
this->phase_[phase].cumulative_reverse_active_energy_ = val;
|
this->phase_[phase].cumulative_reverse_active_energy_ = val;
|
||||||
}
|
}
|
||||||
// 0.01CF resolution = 0.003125 Wh per count
|
return ((float) this->phase_[phase].cumulative_reverse_active_energy_ * 10 / 3200);
|
||||||
return ((float) this->phase_[phase].cumulative_reverse_active_energy_ * (10.0f / 3200.0f));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_phase_harmonic_active_power_(uint8_t phase) {
|
float ATM90E32Component::get_phase_harmonic_active_power_(uint8_t phase) {
|
||||||
@@ -419,15 +446,15 @@ float ATM90E32Component::get_phase_harmonic_active_power_(uint8_t phase) {
|
|||||||
|
|
||||||
float ATM90E32Component::get_phase_angle_(uint8_t phase) {
|
float ATM90E32Component::get_phase_angle_(uint8_t phase) {
|
||||||
uint16_t val = this->read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0;
|
uint16_t val = this->read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0;
|
||||||
return (val > 180) ? (float) (val - 360.0f) : (float) val;
|
return (float) (val > 180) ? val - 360.0 : val;
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_phase_peak_current_(uint8_t phase) {
|
float ATM90E32Component::get_phase_peak_current_(uint8_t phase) {
|
||||||
int16_t val = (float) this->read16_(ATM90E32_REGISTER_IPEAK + phase);
|
int16_t val = (float) this->read16_(ATM90E32_REGISTER_IPEAK + phase);
|
||||||
if (!this->peak_current_signed_)
|
if (!this->peak_current_signed_)
|
||||||
val = std::abs(val);
|
val = abs(val);
|
||||||
// phase register * phase current gain value / 1000 * 2^13
|
// phase register * phase current gain value / 1000 * 2^13
|
||||||
return (val * this->phase_[phase].ct_gain_ / 8192000.0);
|
return (float) (val * this->phase_[phase].ct_gain_ / 8192000.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
float ATM90E32Component::get_frequency_() {
|
float ATM90E32Component::get_frequency_() {
|
||||||
@@ -440,433 +467,29 @@ float ATM90E32Component::get_chip_temperature_() {
|
|||||||
return (float) ctemp;
|
return (float) ctemp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ATM90E32Component::run_gain_calibrations() {
|
uint16_t ATM90E32Component::calibrate_voltage_offset_phase(uint8_t phase) {
|
||||||
if (!this->enable_gain_calibration_) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Gain calibration is disabled! Enable it first with enable_gain_calibration: true");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float ref_voltages[3] = {
|
|
||||||
this->get_reference_voltage(0),
|
|
||||||
this->get_reference_voltage(1),
|
|
||||||
this->get_reference_voltage(2),
|
|
||||||
};
|
|
||||||
float ref_currents[3] = {this->get_reference_current(0), this->get_reference_current(1),
|
|
||||||
this->get_reference_current(2)};
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] ");
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] ========================= Gain Calibration =========================");
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] ---------------------------------------------------------------------");
|
|
||||||
ESP_LOGI(TAG,
|
|
||||||
"[CALIBRATION] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref | V_gain (old→new) | I_gain (old→new) |");
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] ---------------------------------------------------------------------");
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
float measured_voltage = this->get_phase_voltage_avg_(phase);
|
|
||||||
float measured_current = this->get_phase_current_avg_(phase);
|
|
||||||
|
|
||||||
float ref_voltage = ref_voltages[phase];
|
|
||||||
float ref_current = ref_currents[phase];
|
|
||||||
|
|
||||||
uint16_t current_voltage_gain = this->read16_(voltage_gain_registers[phase]);
|
|
||||||
uint16_t current_current_gain = this->read16_(current_gain_registers[phase]);
|
|
||||||
|
|
||||||
bool did_voltage = false;
|
|
||||||
bool did_current = false;
|
|
||||||
|
|
||||||
// Voltage calibration
|
|
||||||
if (ref_voltage <= 0.0f) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping voltage calibration: reference voltage is 0.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
} else if (measured_voltage == 0.0f) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping voltage calibration: measured voltage is 0.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
} else {
|
|
||||||
uint32_t new_voltage_gain = static_cast<uint16_t>((ref_voltage / measured_voltage) * current_voltage_gain);
|
|
||||||
if (new_voltage_gain == 0) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Voltage gain would be 0. Check reference and measured voltage.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
} else {
|
|
||||||
if (new_voltage_gain >= 65535) {
|
|
||||||
ESP_LOGW(
|
|
||||||
TAG,
|
|
||||||
"[CALIBRATION] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage transformer.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
new_voltage_gain = 65535;
|
|
||||||
}
|
|
||||||
this->gain_phase_[phase].voltage_gain = static_cast<uint16_t>(new_voltage_gain);
|
|
||||||
did_voltage = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Current calibration
|
|
||||||
if (ref_current == 0.0f) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping current calibration: reference current is 0.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
} else if (measured_current == 0.0f) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping current calibration: measured current is 0.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
} else {
|
|
||||||
uint32_t new_current_gain = static_cast<uint16_t>((ref_current / measured_current) * current_current_gain);
|
|
||||||
if (new_current_gain == 0) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Current gain would be 0. Check reference and measured current.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
} else {
|
|
||||||
if (new_current_gain >= 65535) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
|
|
||||||
phase_labels[phase]);
|
|
||||||
new_current_gain = 65535;
|
|
||||||
}
|
|
||||||
this->gain_phase_[phase].current_gain = static_cast<uint16_t>(new_current_gain);
|
|
||||||
did_current = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final row output
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] | %c | %9.2f | %9.4f | %5.2f | %6.4f | %5u → %-5u | %5u → %-5u |",
|
|
||||||
'A' + phase, measured_voltage, measured_current, ref_voltage, ref_current, current_voltage_gain,
|
|
||||||
did_voltage ? this->gain_phase_[phase].voltage_gain : current_voltage_gain, current_current_gain,
|
|
||||||
did_current ? this->gain_phase_[phase].current_gain : current_current_gain);
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] =====================================================================\n");
|
|
||||||
|
|
||||||
this->save_gain_calibration_to_memory_();
|
|
||||||
this->write_gains_to_registers_();
|
|
||||||
this->verify_gain_writes_();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::save_gain_calibration_to_memory_() {
|
|
||||||
bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
|
|
||||||
if (success) {
|
|
||||||
this->using_saved_calibrations_ = true;
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Gain calibration saved to memory.");
|
|
||||||
} else {
|
|
||||||
this->using_saved_calibrations_ = false;
|
|
||||||
ESP_LOGE(TAG, "[CALIBRATION] Failed to save gain calibration to memory!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::run_offset_calibrations() {
|
|
||||||
if (!this->enable_offset_calibration_) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] Offset calibration is disabled! Enable it first with enable_offset_calibration: true");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
int16_t voltage_offset = calibrate_offset(phase, true);
|
|
||||||
int16_t current_offset = calibrate_offset(phase, false);
|
|
||||||
|
|
||||||
this->write_offsets_to_registers_(phase, voltage_offset, current_offset);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_voltage: %d, offset_current: %d", 'A' + phase, voltage_offset,
|
|
||||||
current_offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
this->offset_pref_.save(&this->offset_phase_); // Save to flash
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::run_power_offset_calibrations() {
|
|
||||||
if (!this->enable_offset_calibration_) {
|
|
||||||
ESP_LOGW(
|
|
||||||
TAG,
|
|
||||||
"[CALIBRATION] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; ++phase) {
|
|
||||||
int16_t active_offset = calibrate_power_offset(phase, false);
|
|
||||||
int16_t reactive_offset = calibrate_power_offset(phase, true);
|
|
||||||
|
|
||||||
this->write_power_offsets_to_registers_(phase, active_offset, reactive_offset);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_active_power: %d, offset_reactive_power: %d", 'A' + phase,
|
|
||||||
active_offset, reactive_offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
this->power_offset_pref_.save(&this->power_offset_phase_); // Save to flash
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::write_gains_to_registers_() {
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
|
|
||||||
|
|
||||||
for (int phase = 0; phase < 3; phase++) {
|
|
||||||
this->write16_(voltage_gain_registers[phase], this->gain_phase_[phase].voltage_gain);
|
|
||||||
this->write16_(current_gain_registers[phase], this->gain_phase_[phase].current_gain);
|
|
||||||
}
|
|
||||||
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset) {
|
|
||||||
// Save to runtime
|
|
||||||
this->offset_phase_[phase].voltage_offset_ = voltage_offset;
|
|
||||||
this->phase_[phase].voltage_offset_ = voltage_offset;
|
|
||||||
|
|
||||||
// Save to flash-storable struct
|
|
||||||
this->offset_phase_[phase].current_offset_ = current_offset;
|
|
||||||
this->phase_[phase].current_offset_ = current_offset;
|
|
||||||
|
|
||||||
// Write to registers
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
|
|
||||||
this->write16_(voltage_offset_registers[phase], static_cast<uint16_t>(voltage_offset));
|
|
||||||
this->write16_(current_offset_registers[phase], static_cast<uint16_t>(current_offset));
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset) {
|
|
||||||
// Save to runtime
|
|
||||||
this->phase_[phase].active_power_offset_ = p_offset;
|
|
||||||
this->phase_[phase].reactive_power_offset_ = q_offset;
|
|
||||||
|
|
||||||
// Save to flash-storable struct
|
|
||||||
this->power_offset_phase_[phase].active_power_offset = p_offset;
|
|
||||||
this->power_offset_phase_[phase].reactive_power_offset = q_offset;
|
|
||||||
|
|
||||||
// Write to registers
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
|
|
||||||
this->write16_(this->power_offset_registers[phase], static_cast<uint16_t>(p_offset));
|
|
||||||
this->write16_(this->reactive_power_offset_registers[phase], static_cast<uint16_t>(q_offset));
|
|
||||||
this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::restore_gain_calibrations_() {
|
|
||||||
if (this->gain_calibration_pref_.load(&this->gain_phase_)) {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Restoring saved gain calibrations to registers:");
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
uint16_t v_gain = this->gain_phase_[phase].voltage_gain;
|
|
||||||
uint16_t i_gain = this->gain_phase_[phase].current_gain;
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Phase %c - Voltage Gain: %u, Current Gain: %u", 'A' + phase, v_gain, i_gain);
|
|
||||||
}
|
|
||||||
|
|
||||||
this->write_gains_to_registers_();
|
|
||||||
|
|
||||||
if (this->verify_gain_writes_()) {
|
|
||||||
this->using_saved_calibrations_ = true;
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Gain calibration loaded and verified successfully.");
|
|
||||||
} else {
|
|
||||||
this->using_saved_calibrations_ = false;
|
|
||||||
ESP_LOGE(TAG, "[CALIBRATION] Gain verification failed! Calibration may not be applied correctly.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this->using_saved_calibrations_ = false;
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No stored gain calibrations found. Using config file values.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::restore_offset_calibrations_() {
|
|
||||||
if (this->offset_pref_.load(&this->offset_phase_)) {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Successfully restored offset calibration from memory.");
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
auto &offset = this->offset_phase_[phase];
|
|
||||||
write_offsets_to_registers_(phase, offset.voltage_offset_, offset.current_offset_);
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_voltage:: %d, offset_current: %d", 'A' + phase,
|
|
||||||
offset.voltage_offset_, offset.current_offset_);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No stored offset calibrations found. Using default values.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::restore_power_offset_calibrations_() {
|
|
||||||
if (this->power_offset_pref_.load(&this->power_offset_phase_)) {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Successfully restored power offset calibration from memory.");
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; ++phase) {
|
|
||||||
auto &offset = this->power_offset_phase_[phase];
|
|
||||||
write_power_offsets_to_registers_(phase, offset.active_power_offset, offset.reactive_power_offset);
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_active_power: %d, offset_reactive_power: %d", 'A' + phase,
|
|
||||||
offset.active_power_offset, offset.reactive_power_offset);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No stored power offsets found. Using default values.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::clear_gain_calibrations() {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Clearing stored gain calibrations and restoring config-defined values");
|
|
||||||
|
|
||||||
for (int phase = 0; phase < 3; phase++) {
|
|
||||||
gain_phase_[phase].voltage_gain = this->phase_[phase].voltage_gain_;
|
|
||||||
gain_phase_[phase].current_gain = this->phase_[phase].ct_gain_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
|
|
||||||
this->using_saved_calibrations_ = false;
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Gain calibrations cleared. Config values restored:");
|
|
||||||
for (int phase = 0; phase < 3; phase++) {
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Phase %c - Voltage Gain: %u, Current Gain: %u", 'A' + phase,
|
|
||||||
gain_phase_[phase].voltage_gain, gain_phase_[phase].current_gain);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ESP_LOGE(TAG, "[CALIBRATION] Failed to clear gain calibrations!");
|
|
||||||
}
|
|
||||||
|
|
||||||
this->write_gains_to_registers_(); // Apply them to the chip immediately
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::clear_offset_calibrations() {
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
this->write_offsets_to_registers_(phase, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
this->offset_pref_.save(&this->offset_phase_); // Save cleared values to flash memory
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Offsets cleared.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::clear_power_offset_calibrations() {
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
this->write_power_offsets_to_registers_(phase, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
this->power_offset_pref_.save(&this->power_offset_phase_);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Power offsets cleared.");
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t ATM90E32Component::calibrate_offset(uint8_t phase, bool voltage) {
|
|
||||||
const uint8_t num_reads = 5;
|
const uint8_t num_reads = 5;
|
||||||
uint64_t total_value = 0;
|
uint64_t total_value = 0;
|
||||||
|
for (int i = 0; i < num_reads; ++i) {
|
||||||
for (uint8_t i = 0; i < num_reads; ++i) {
|
const uint32_t measurement_value = read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase);
|
||||||
uint32_t reading = voltage ? this->read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase)
|
total_value += measurement_value;
|
||||||
: this->read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
|
|
||||||
total_value += reading;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint32_t average_value = total_value / num_reads;
|
const uint32_t average_value = total_value / num_reads;
|
||||||
const uint32_t shifted = average_value >> 7;
|
const uint32_t shifted_value = average_value >> 7;
|
||||||
const uint32_t offset = ~shifted + 1;
|
const uint32_t voltage_offset = ~shifted_value + 1;
|
||||||
return static_cast<int16_t>(offset); // Takes lower 16 bits
|
return voltage_offset & 0xFFFF; // Take the lower 16 bits
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ATM90E32Component::calibrate_power_offset(uint8_t phase, bool reactive) {
|
uint16_t ATM90E32Component::calibrate_current_offset_phase(uint8_t phase) {
|
||||||
const uint8_t num_reads = 5;
|
const uint8_t num_reads = 5;
|
||||||
uint64_t total_value = 0;
|
uint64_t total_value = 0;
|
||||||
|
for (int i = 0; i < num_reads; ++i) {
|
||||||
for (uint8_t i = 0; i < num_reads; ++i) {
|
const uint32_t measurement_value = read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
|
||||||
uint32_t reading = reactive ? this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
|
total_value += measurement_value;
|
||||||
: this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
|
|
||||||
total_value += reading;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint32_t average_value = total_value / num_reads;
|
const uint32_t average_value = total_value / num_reads;
|
||||||
const uint32_t power_offset = ~average_value + 1;
|
const uint32_t current_offset = ~average_value + 1;
|
||||||
return static_cast<int16_t>(power_offset); // Takes the lower 16 bits
|
return current_offset & 0xFFFF; // Take the lower 16 bits
|
||||||
}
|
|
||||||
|
|
||||||
bool ATM90E32Component::verify_gain_writes_() {
|
|
||||||
bool success = true;
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
uint16_t read_voltage = this->read16_(voltage_gain_registers[phase]);
|
|
||||||
uint16_t read_current = this->read16_(current_gain_registers[phase]);
|
|
||||||
|
|
||||||
if (read_voltage != this->gain_phase_[phase].voltage_gain ||
|
|
||||||
read_current != this->gain_phase_[phase].current_gain) {
|
|
||||||
ESP_LOGE(TAG, "[CALIBRATION] Mismatch detected for Phase %s!", phase_labels[phase]);
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return success; // Return true if all writes were successful, false otherwise
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
|
||||||
void ATM90E32Component::check_phase_status() {
|
|
||||||
uint16_t state0 = this->read16_(ATM90E32_REGISTER_EMMSTATE0);
|
|
||||||
uint16_t state1 = this->read16_(ATM90E32_REGISTER_EMMSTATE1);
|
|
||||||
|
|
||||||
for (int phase = 0; phase < 3; phase++) {
|
|
||||||
std::string status;
|
|
||||||
|
|
||||||
if (state0 & over_voltage_flags[phase])
|
|
||||||
status += "Over Voltage; ";
|
|
||||||
if (state1 & voltage_sag_flags[phase])
|
|
||||||
status += "Voltage Sag; ";
|
|
||||||
if (state1 & phase_loss_flags[phase])
|
|
||||||
status += "Phase Loss; ";
|
|
||||||
|
|
||||||
auto *sensor = this->phase_status_text_sensor_[phase];
|
|
||||||
const char *phase_name = sensor ? sensor->get_name().c_str() : "Unknown Phase";
|
|
||||||
if (!status.empty()) {
|
|
||||||
status.pop_back(); // remove space
|
|
||||||
status.pop_back(); // remove semicolon
|
|
||||||
ESP_LOGW(TAG, "%s: %s", phase_name, status.c_str());
|
|
||||||
if (sensor != nullptr)
|
|
||||||
sensor->publish_state(status);
|
|
||||||
} else {
|
|
||||||
if (sensor != nullptr)
|
|
||||||
sensor->publish_state("Okay");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::check_freq_status() {
|
|
||||||
uint16_t state1 = this->read16_(ATM90E32_REGISTER_EMMSTATE1);
|
|
||||||
|
|
||||||
std::string freq_status;
|
|
||||||
|
|
||||||
if (state1 & ATM90E32_STATUS_S1_FREQHIST) {
|
|
||||||
freq_status = "HIGH";
|
|
||||||
} else if (state1 & ATM90E32_STATUS_S1_FREQLOST) {
|
|
||||||
freq_status = "LOW";
|
|
||||||
} else {
|
|
||||||
freq_status = "Normal";
|
|
||||||
}
|
|
||||||
ESP_LOGW(TAG, "Frequency status: %s", freq_status.c_str());
|
|
||||||
|
|
||||||
if (this->freq_status_text_sensor_ != nullptr) {
|
|
||||||
this->freq_status_text_sensor_->publish_state(freq_status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32Component::check_over_current() {
|
|
||||||
constexpr float max_current_threshold = 65.53f;
|
|
||||||
|
|
||||||
for (uint8_t phase = 0; phase < 3; phase++) {
|
|
||||||
float current_val =
|
|
||||||
this->phase_[phase].current_sensor_ != nullptr ? this->phase_[phase].current_sensor_->state : 0.0f;
|
|
||||||
|
|
||||||
if (current_val > max_current_threshold) {
|
|
||||||
ESP_LOGW(TAG, "Over current detected on Phase %c: %.2f A", 'A' + phase, current_val);
|
|
||||||
ESP_LOGW(TAG, "You may need to half your gain_ct: value & multiply the current and power values by 2");
|
|
||||||
if (this->phase_status_text_sensor_[phase] != nullptr) {
|
|
||||||
this->phase_status_text_sensor_[phase]->publish_state("Over Current; ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint16_t ATM90E32Component::calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier) {
|
|
||||||
// this assumes that 60Hz electrical systems use 120V mains,
|
|
||||||
// which is usually, but not always the case
|
|
||||||
float nominal_voltage = (line_freq == 60) ? 120.0f : 220.0f;
|
|
||||||
float target_voltage = nominal_voltage * multiplier;
|
|
||||||
|
|
||||||
float peak_01v = target_voltage * 100.0f * std::sqrt(2.0f); // convert RMS → peak, scale to 0.01V
|
|
||||||
float divider = (2.0f * ugain) / 32768.0f;
|
|
||||||
|
|
||||||
float threshold = peak_01v / divider;
|
|
||||||
|
|
||||||
return static_cast<uint16_t>(threshold);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ATM90E32Component::validate_spi_read_(uint16_t expected, const char *context) {
|
|
||||||
uint16_t last = this->read16_(ATM90E32_REGISTER_LASTSPIDATA);
|
|
||||||
if (last != expected) {
|
|
||||||
if (context != nullptr) {
|
|
||||||
ESP_LOGW(TAG, "[%s] SPI read mismatch: expected 0x%04X, got 0x%04X", context, expected, last);
|
|
||||||
} else {
|
|
||||||
ESP_LOGW(TAG, "SPI read mismatch: expected 0x%04X, got 0x%04X", expected, last);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atm90e32
|
} // namespace atm90e32
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include "atm90e32_reg.h"
|
#include "atm90e32_reg.h"
|
||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/spi/spi.h"
|
#include "esphome/components/spi/spi.h"
|
||||||
@@ -19,26 +18,6 @@ class ATM90E32Component : public PollingComponent,
|
|||||||
static const uint8_t PHASEA = 0;
|
static const uint8_t PHASEA = 0;
|
||||||
static const uint8_t PHASEB = 1;
|
static const uint8_t PHASEB = 1;
|
||||||
static const uint8_t PHASEC = 2;
|
static const uint8_t PHASEC = 2;
|
||||||
const char *phase_labels[3] = {"A", "B", "C"};
|
|
||||||
// these registers are not sucessive, so we can't just do 'base + phase'
|
|
||||||
const uint16_t voltage_gain_registers[3] = {ATM90E32_REGISTER_UGAINA, ATM90E32_REGISTER_UGAINB,
|
|
||||||
ATM90E32_REGISTER_UGAINC};
|
|
||||||
const uint16_t current_gain_registers[3] = {ATM90E32_REGISTER_IGAINA, ATM90E32_REGISTER_IGAINB,
|
|
||||||
ATM90E32_REGISTER_IGAINC};
|
|
||||||
const uint16_t voltage_offset_registers[3] = {ATM90E32_REGISTER_UOFFSETA, ATM90E32_REGISTER_UOFFSETB,
|
|
||||||
ATM90E32_REGISTER_UOFFSETC};
|
|
||||||
const uint16_t current_offset_registers[3] = {ATM90E32_REGISTER_IOFFSETA, ATM90E32_REGISTER_IOFFSETB,
|
|
||||||
ATM90E32_REGISTER_IOFFSETC};
|
|
||||||
const uint16_t power_offset_registers[3] = {ATM90E32_REGISTER_POFFSETA, ATM90E32_REGISTER_POFFSETB,
|
|
||||||
ATM90E32_REGISTER_POFFSETC};
|
|
||||||
const uint16_t reactive_power_offset_registers[3] = {ATM90E32_REGISTER_QOFFSETA, ATM90E32_REGISTER_QOFFSETB,
|
|
||||||
ATM90E32_REGISTER_QOFFSETC};
|
|
||||||
const uint16_t over_voltage_flags[3] = {ATM90E32_STATUS_S0_OVPHASEAST, ATM90E32_STATUS_S0_OVPHASEBST,
|
|
||||||
ATM90E32_STATUS_S0_OVPHASECST};
|
|
||||||
const uint16_t voltage_sag_flags[3] = {ATM90E32_STATUS_S1_SAGPHASEAST, ATM90E32_STATUS_S1_SAGPHASEBST,
|
|
||||||
ATM90E32_STATUS_S1_SAGPHASECST};
|
|
||||||
const uint16_t phase_loss_flags[3] = {ATM90E32_STATUS_S1_PHASELOSSAST, ATM90E32_STATUS_S1_PHASELOSSBST,
|
|
||||||
ATM90E32_STATUS_S1_PHASELOSSCST};
|
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
@@ -63,14 +42,6 @@ class ATM90E32Component : public PollingComponent,
|
|||||||
void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
|
void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
|
||||||
void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
|
void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
|
||||||
void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
|
void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
|
||||||
void set_voltage_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].voltage_offset_ = offset; }
|
|
||||||
void set_current_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].current_offset_ = offset; }
|
|
||||||
void set_active_power_offset(uint8_t phase, int16_t offset) {
|
|
||||||
this->power_offset_phase_[phase].active_power_offset = offset;
|
|
||||||
}
|
|
||||||
void set_reactive_power_offset(uint8_t phase, int16_t offset) {
|
|
||||||
this->power_offset_phase_[phase].reactive_power_offset = offset;
|
|
||||||
}
|
|
||||||
void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
|
void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
|
||||||
void set_peak_current_signed(bool flag) { peak_current_signed_ = flag; }
|
void set_peak_current_signed(bool flag) { peak_current_signed_ = flag; }
|
||||||
void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor) {
|
void set_chip_temperature_sensor(sensor::Sensor *chip_temperature_sensor) {
|
||||||
@@ -80,104 +51,53 @@ class ATM90E32Component : public PollingComponent,
|
|||||||
void set_current_phases(int phases) { current_phases_ = phases; }
|
void set_current_phases(int phases) { current_phases_ = phases; }
|
||||||
void set_pga_gain(uint16_t gain) { pga_gain_ = gain; }
|
void set_pga_gain(uint16_t gain) { pga_gain_ = gain; }
|
||||||
void run_offset_calibrations();
|
void run_offset_calibrations();
|
||||||
void run_power_offset_calibrations();
|
|
||||||
void clear_offset_calibrations();
|
void clear_offset_calibrations();
|
||||||
void clear_power_offset_calibrations();
|
|
||||||
void clear_gain_calibrations();
|
|
||||||
void set_enable_offset_calibration(bool flag) { enable_offset_calibration_ = flag; }
|
void set_enable_offset_calibration(bool flag) { enable_offset_calibration_ = flag; }
|
||||||
void set_enable_gain_calibration(bool flag) { enable_gain_calibration_ = flag; }
|
uint16_t calibrate_voltage_offset_phase(uint8_t /*phase*/);
|
||||||
int16_t calibrate_offset(uint8_t phase, bool voltage);
|
uint16_t calibrate_current_offset_phase(uint8_t /*phase*/);
|
||||||
int16_t calibrate_power_offset(uint8_t phase, bool reactive);
|
|
||||||
void run_gain_calibrations();
|
|
||||||
#ifdef USE_NUMBER
|
|
||||||
void set_reference_voltage(uint8_t phase, number::Number *ref_voltage) { ref_voltages_[phase] = ref_voltage; }
|
|
||||||
void set_reference_current(uint8_t phase, number::Number *ref_current) { ref_currents_[phase] = ref_current; }
|
|
||||||
#endif
|
|
||||||
float get_reference_voltage(uint8_t phase) {
|
|
||||||
#ifdef USE_NUMBER
|
|
||||||
return (phase >= 0 && phase < 3 && ref_voltages_[phase]) ? ref_voltages_[phase]->state : 120.0; // Default voltage
|
|
||||||
#else
|
|
||||||
return 120.0; // Default voltage
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
float get_reference_current(uint8_t phase) {
|
|
||||||
#ifdef USE_NUMBER
|
|
||||||
return (phase >= 0 && phase < 3 && ref_currents_[phase]) ? ref_currents_[phase]->state : 5.0f; // Default current
|
|
||||||
#else
|
|
||||||
return 5.0f; // Default current
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
bool using_saved_calibrations_ = false; // Track if stored calibrations are being used
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
|
||||||
void check_phase_status();
|
|
||||||
void check_freq_status();
|
|
||||||
void check_over_current();
|
|
||||||
void set_phase_status_text_sensor(uint8_t phase, text_sensor::TextSensor *sensor) {
|
|
||||||
this->phase_status_text_sensor_[phase] = sensor;
|
|
||||||
}
|
|
||||||
void set_freq_status_text_sensor(text_sensor::TextSensor *sensor) { this->freq_status_text_sensor_ = sensor; }
|
|
||||||
#endif
|
|
||||||
uint16_t calculate_voltage_threshold(int line_freq, uint16_t ugain, float multiplier);
|
|
||||||
int32_t last_periodic_millis = millis();
|
int32_t last_periodic_millis = millis();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
#ifdef USE_NUMBER
|
|
||||||
number::Number *ref_voltages_[3]{nullptr, nullptr, nullptr};
|
|
||||||
number::Number *ref_currents_[3]{nullptr, nullptr, nullptr};
|
|
||||||
#endif
|
|
||||||
uint16_t read16_(uint16_t a_register);
|
uint16_t read16_(uint16_t a_register);
|
||||||
int read32_(uint16_t addr_h, uint16_t addr_l);
|
int read32_(uint16_t addr_h, uint16_t addr_l);
|
||||||
void write16_(uint16_t a_register, uint16_t val);
|
void write16_(uint16_t a_register, uint16_t val);
|
||||||
float get_local_phase_voltage_(uint8_t phase);
|
float get_local_phase_voltage_(uint8_t /*phase*/);
|
||||||
float get_local_phase_current_(uint8_t phase);
|
float get_local_phase_current_(uint8_t /*phase*/);
|
||||||
float get_local_phase_active_power_(uint8_t phase);
|
float get_local_phase_active_power_(uint8_t /*phase*/);
|
||||||
float get_local_phase_reactive_power_(uint8_t phase);
|
float get_local_phase_reactive_power_(uint8_t /*phase*/);
|
||||||
float get_local_phase_apparent_power_(uint8_t phase);
|
float get_local_phase_power_factor_(uint8_t /*phase*/);
|
||||||
float get_local_phase_power_factor_(uint8_t phase);
|
float get_local_phase_forward_active_energy_(uint8_t /*phase*/);
|
||||||
float get_local_phase_forward_active_energy_(uint8_t phase);
|
float get_local_phase_reverse_active_energy_(uint8_t /*phase*/);
|
||||||
float get_local_phase_reverse_active_energy_(uint8_t phase);
|
float get_local_phase_angle_(uint8_t /*phase*/);
|
||||||
float get_local_phase_angle_(uint8_t phase);
|
float get_local_phase_harmonic_active_power_(uint8_t /*phase*/);
|
||||||
float get_local_phase_harmonic_active_power_(uint8_t phase);
|
float get_local_phase_peak_current_(uint8_t /*phase*/);
|
||||||
float get_local_phase_peak_current_(uint8_t phase);
|
float get_phase_voltage_(uint8_t /*phase*/);
|
||||||
float get_phase_voltage_(uint8_t phase);
|
float get_phase_voltage_avg_(uint8_t /*phase*/);
|
||||||
float get_phase_voltage_avg_(uint8_t phase);
|
float get_phase_current_(uint8_t /*phase*/);
|
||||||
float get_phase_current_(uint8_t phase);
|
float get_phase_current_avg_(uint8_t /*phase*/);
|
||||||
float get_phase_current_avg_(uint8_t phase);
|
float get_phase_active_power_(uint8_t /*phase*/);
|
||||||
float get_phase_active_power_(uint8_t phase);
|
float get_phase_reactive_power_(uint8_t /*phase*/);
|
||||||
float get_phase_reactive_power_(uint8_t phase);
|
float get_phase_power_factor_(uint8_t /*phase*/);
|
||||||
float get_phase_apparent_power_(uint8_t phase);
|
float get_phase_forward_active_energy_(uint8_t /*phase*/);
|
||||||
float get_phase_power_factor_(uint8_t phase);
|
float get_phase_reverse_active_energy_(uint8_t /*phase*/);
|
||||||
float get_phase_forward_active_energy_(uint8_t phase);
|
float get_phase_angle_(uint8_t /*phase*/);
|
||||||
float get_phase_reverse_active_energy_(uint8_t phase);
|
float get_phase_harmonic_active_power_(uint8_t /*phase*/);
|
||||||
float get_phase_angle_(uint8_t phase);
|
float get_phase_peak_current_(uint8_t /*phase*/);
|
||||||
float get_phase_harmonic_active_power_(uint8_t phase);
|
|
||||||
float get_phase_peak_current_(uint8_t phase);
|
|
||||||
float get_frequency_();
|
float get_frequency_();
|
||||||
float get_chip_temperature_();
|
float get_chip_temperature_();
|
||||||
bool get_publish_interval_flag_() { return publish_interval_flag_; };
|
bool get_publish_interval_flag_() { return publish_interval_flag_; };
|
||||||
void set_publish_interval_flag_(bool flag) { publish_interval_flag_ = flag; };
|
void set_publish_interval_flag_(bool flag) { publish_interval_flag_ = flag; };
|
||||||
void restore_offset_calibrations_();
|
void restore_calibrations_();
|
||||||
void restore_power_offset_calibrations_();
|
|
||||||
void restore_gain_calibrations_();
|
|
||||||
void save_gain_calibration_to_memory_();
|
|
||||||
void write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset);
|
|
||||||
void write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset);
|
|
||||||
void write_gains_to_registers_();
|
|
||||||
bool verify_gain_writes_();
|
|
||||||
bool validate_spi_read_(uint16_t expected, const char *context = nullptr);
|
|
||||||
|
|
||||||
struct ATM90E32Phase {
|
struct ATM90E32Phase {
|
||||||
uint16_t voltage_gain_{0};
|
uint16_t voltage_gain_{0};
|
||||||
uint16_t ct_gain_{0};
|
uint16_t ct_gain_{0};
|
||||||
int16_t voltage_offset_{0};
|
uint16_t voltage_offset_{0};
|
||||||
int16_t current_offset_{0};
|
uint16_t current_offset_{0};
|
||||||
int16_t active_power_offset_{0};
|
|
||||||
int16_t reactive_power_offset_{0};
|
|
||||||
float voltage_{0};
|
float voltage_{0};
|
||||||
float current_{0};
|
float current_{0};
|
||||||
float active_power_{0};
|
float active_power_{0};
|
||||||
float reactive_power_{0};
|
float reactive_power_{0};
|
||||||
float apparent_power_{0};
|
|
||||||
float power_factor_{0};
|
float power_factor_{0};
|
||||||
float forward_active_energy_{0};
|
float forward_active_energy_{0};
|
||||||
float reverse_active_energy_{0};
|
float reverse_active_energy_{0};
|
||||||
@@ -199,30 +119,14 @@ class ATM90E32Component : public PollingComponent,
|
|||||||
uint32_t cumulative_reverse_active_energy_{0};
|
uint32_t cumulative_reverse_active_energy_{0};
|
||||||
} phase_[3];
|
} phase_[3];
|
||||||
|
|
||||||
struct OffsetCalibration {
|
struct Calibration {
|
||||||
int16_t voltage_offset_{0};
|
uint16_t voltage_offset_{0};
|
||||||
int16_t current_offset_{0};
|
uint16_t current_offset_{0};
|
||||||
} offset_phase_[3];
|
} offset_phase_[3];
|
||||||
|
|
||||||
struct PowerOffsetCalibration {
|
ESPPreferenceObject pref_;
|
||||||
int16_t active_power_offset{0};
|
|
||||||
int16_t reactive_power_offset{0};
|
|
||||||
} power_offset_phase_[3];
|
|
||||||
|
|
||||||
struct GainCalibration {
|
|
||||||
uint16_t voltage_gain{1};
|
|
||||||
uint16_t current_gain{1};
|
|
||||||
} gain_phase_[3];
|
|
||||||
|
|
||||||
ESPPreferenceObject offset_pref_;
|
|
||||||
ESPPreferenceObject power_offset_pref_;
|
|
||||||
ESPPreferenceObject gain_calibration_pref_;
|
|
||||||
|
|
||||||
sensor::Sensor *freq_sensor_{nullptr};
|
sensor::Sensor *freq_sensor_{nullptr};
|
||||||
#ifdef USE_TEXT_SENSOR
|
|
||||||
text_sensor::TextSensor *phase_status_text_sensor_[3]{nullptr};
|
|
||||||
text_sensor::TextSensor *freq_status_text_sensor_{nullptr};
|
|
||||||
#endif
|
|
||||||
sensor::Sensor *chip_temperature_sensor_{nullptr};
|
sensor::Sensor *chip_temperature_sensor_{nullptr};
|
||||||
uint16_t pga_gain_{0x15};
|
uint16_t pga_gain_{0x15};
|
||||||
int line_freq_{60};
|
int line_freq_{60};
|
||||||
@@ -230,7 +134,6 @@ class ATM90E32Component : public PollingComponent,
|
|||||||
bool publish_interval_flag_{false};
|
bool publish_interval_flag_{false};
|
||||||
bool peak_current_signed_{false};
|
bool peak_current_signed_{false};
|
||||||
bool enable_offset_calibration_{false};
|
bool enable_offset_calibration_{false};
|
||||||
bool enable_gain_calibration_{false};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace atm90e32
|
} // namespace atm90e32
|
||||||
|
|||||||
@@ -176,17 +176,16 @@ static const uint16_t ATM90E32_REGISTER_ANENERGYCH = 0xAF; // C Reverse Harm. E
|
|||||||
|
|
||||||
/* POWER & P.F. REGISTERS */
|
/* POWER & P.F. REGISTERS */
|
||||||
static const uint16_t ATM90E32_REGISTER_PMEANT = 0xB0; // Total Mean Power (P)
|
static const uint16_t ATM90E32_REGISTER_PMEANT = 0xB0; // Total Mean Power (P)
|
||||||
static const uint16_t ATM90E32_REGISTER_PMEAN = 0xB1; // Active Power Reg Base (P)
|
static const uint16_t ATM90E32_REGISTER_PMEAN = 0xB1; // Mean Power Reg Base (P)
|
||||||
static const uint16_t ATM90E32_REGISTER_PMEANA = 0xB1; // A Mean Power (P)
|
static const uint16_t ATM90E32_REGISTER_PMEANA = 0xB1; // A Mean Power (P)
|
||||||
static const uint16_t ATM90E32_REGISTER_PMEANB = 0xB2; // B Mean Power (P)
|
static const uint16_t ATM90E32_REGISTER_PMEANB = 0xB2; // B Mean Power (P)
|
||||||
static const uint16_t ATM90E32_REGISTER_PMEANC = 0xB3; // C Mean Power (P)
|
static const uint16_t ATM90E32_REGISTER_PMEANC = 0xB3; // C Mean Power (P)
|
||||||
static const uint16_t ATM90E32_REGISTER_QMEANT = 0xB4; // Total Mean Power (Q)
|
static const uint16_t ATM90E32_REGISTER_QMEANT = 0xB4; // Total Mean Power (Q)
|
||||||
static const uint16_t ATM90E32_REGISTER_QMEAN = 0xB5; // Reactive Power Reg Base (Q)
|
static const uint16_t ATM90E32_REGISTER_QMEAN = 0xB5; // Mean Power Reg Base (Q)
|
||||||
static const uint16_t ATM90E32_REGISTER_QMEANA = 0xB5; // A Mean Power (Q)
|
static const uint16_t ATM90E32_REGISTER_QMEANA = 0xB5; // A Mean Power (Q)
|
||||||
static const uint16_t ATM90E32_REGISTER_QMEANB = 0xB6; // B Mean Power (Q)
|
static const uint16_t ATM90E32_REGISTER_QMEANB = 0xB6; // B Mean Power (Q)
|
||||||
static const uint16_t ATM90E32_REGISTER_QMEANC = 0xB7; // C Mean Power (Q)
|
static const uint16_t ATM90E32_REGISTER_QMEANC = 0xB7; // C Mean Power (Q)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANT = 0xB8; // Total Mean Power (S)
|
static const uint16_t ATM90E32_REGISTER_SMEANT = 0xB8; // Total Mean Power (S)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEAN = 0xB9; // Apparent Mean Power Base (S)
|
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANA = 0xB9; // A Mean Power (S)
|
static const uint16_t ATM90E32_REGISTER_SMEANA = 0xB9; // A Mean Power (S)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANB = 0xBA; // B Mean Power (S)
|
static const uint16_t ATM90E32_REGISTER_SMEANB = 0xBA; // B Mean Power (S)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANC = 0xBB; // C Mean Power (S)
|
static const uint16_t ATM90E32_REGISTER_SMEANC = 0xBB; // C Mean Power (S)
|
||||||
@@ -207,7 +206,6 @@ static const uint16_t ATM90E32_REGISTER_QMEANALSB = 0xC5; // Lower Word (A Rea
|
|||||||
static const uint16_t ATM90E32_REGISTER_QMEANBLSB = 0xC6; // Lower Word (B React. Power)
|
static const uint16_t ATM90E32_REGISTER_QMEANBLSB = 0xC6; // Lower Word (B React. Power)
|
||||||
static const uint16_t ATM90E32_REGISTER_QMEANCLSB = 0xC7; // Lower Word (C React. Power)
|
static const uint16_t ATM90E32_REGISTER_QMEANCLSB = 0xC7; // Lower Word (C React. Power)
|
||||||
static const uint16_t ATM90E32_REGISTER_SAMEANTLSB = 0xC8; // Lower Word (Tot. App. Power)
|
static const uint16_t ATM90E32_REGISTER_SAMEANTLSB = 0xC8; // Lower Word (Tot. App. Power)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANLSB = 0xC9; // Lower Word Reg Base (Apparent Power)
|
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANALSB = 0xC9; // Lower Word (A App. Power)
|
static const uint16_t ATM90E32_REGISTER_SMEANALSB = 0xC9; // Lower Word (A App. Power)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANBLSB = 0xCA; // Lower Word (B App. Power)
|
static const uint16_t ATM90E32_REGISTER_SMEANBLSB = 0xCA; // Lower Word (B App. Power)
|
||||||
static const uint16_t ATM90E32_REGISTER_SMEANCLSB = 0xCB; // Lower Word (C App. Power)
|
static const uint16_t ATM90E32_REGISTER_SMEANCLSB = 0xCB; // Lower Word (C App. Power)
|
||||||
|
|||||||
@@ -1,95 +1,43 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import button
|
from esphome.components import button
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_ID, ENTITY_CATEGORY_CONFIG, ICON_SCALE
|
from esphome.const import CONF_ID, ENTITY_CATEGORY_CONFIG, ICON_CHIP, ICON_SCALE
|
||||||
|
|
||||||
from .. import atm90e32_ns
|
from .. import atm90e32_ns
|
||||||
from ..sensor import ATM90E32Component
|
from ..sensor import ATM90E32Component
|
||||||
|
|
||||||
CONF_RUN_GAIN_CALIBRATION = "run_gain_calibration"
|
|
||||||
CONF_CLEAR_GAIN_CALIBRATION = "clear_gain_calibration"
|
|
||||||
CONF_RUN_OFFSET_CALIBRATION = "run_offset_calibration"
|
CONF_RUN_OFFSET_CALIBRATION = "run_offset_calibration"
|
||||||
CONF_CLEAR_OFFSET_CALIBRATION = "clear_offset_calibration"
|
CONF_CLEAR_OFFSET_CALIBRATION = "clear_offset_calibration"
|
||||||
CONF_RUN_POWER_OFFSET_CALIBRATION = "run_power_offset_calibration"
|
|
||||||
CONF_CLEAR_POWER_OFFSET_CALIBRATION = "clear_power_offset_calibration"
|
|
||||||
|
|
||||||
ATM90E32GainCalibrationButton = atm90e32_ns.class_(
|
ATM90E32CalibrationButton = atm90e32_ns.class_(
|
||||||
"ATM90E32GainCalibrationButton", button.Button
|
"ATM90E32CalibrationButton",
|
||||||
|
button.Button,
|
||||||
)
|
)
|
||||||
ATM90E32ClearGainCalibrationButton = atm90e32_ns.class_(
|
ATM90E32ClearCalibrationButton = atm90e32_ns.class_(
|
||||||
"ATM90E32ClearGainCalibrationButton", button.Button
|
"ATM90E32ClearCalibrationButton",
|
||||||
)
|
button.Button,
|
||||||
ATM90E32OffsetCalibrationButton = atm90e32_ns.class_(
|
|
||||||
"ATM90E32OffsetCalibrationButton", button.Button
|
|
||||||
)
|
|
||||||
ATM90E32ClearOffsetCalibrationButton = atm90e32_ns.class_(
|
|
||||||
"ATM90E32ClearOffsetCalibrationButton", button.Button
|
|
||||||
)
|
|
||||||
ATM90E32PowerOffsetCalibrationButton = atm90e32_ns.class_(
|
|
||||||
"ATM90E32PowerOffsetCalibrationButton", button.Button
|
|
||||||
)
|
|
||||||
ATM90E32ClearPowerOffsetCalibrationButton = atm90e32_ns.class_(
|
|
||||||
"ATM90E32ClearPowerOffsetCalibrationButton", button.Button
|
|
||||||
)
|
)
|
||||||
|
|
||||||
CONFIG_SCHEMA = {
|
CONFIG_SCHEMA = {
|
||||||
cv.GenerateID(CONF_ID): cv.use_id(ATM90E32Component),
|
cv.GenerateID(CONF_ID): cv.use_id(ATM90E32Component),
|
||||||
cv.Optional(CONF_RUN_GAIN_CALIBRATION): button.button_schema(
|
|
||||||
ATM90E32GainCalibrationButton,
|
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
|
||||||
icon="mdi:scale-balance",
|
|
||||||
),
|
|
||||||
cv.Optional(CONF_CLEAR_GAIN_CALIBRATION): button.button_schema(
|
|
||||||
ATM90E32ClearGainCalibrationButton,
|
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
|
||||||
icon="mdi:delete",
|
|
||||||
),
|
|
||||||
cv.Optional(CONF_RUN_OFFSET_CALIBRATION): button.button_schema(
|
cv.Optional(CONF_RUN_OFFSET_CALIBRATION): button.button_schema(
|
||||||
ATM90E32OffsetCalibrationButton,
|
ATM90E32CalibrationButton,
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||||
icon=ICON_SCALE,
|
icon=ICON_SCALE,
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_CLEAR_OFFSET_CALIBRATION): button.button_schema(
|
cv.Optional(CONF_CLEAR_OFFSET_CALIBRATION): button.button_schema(
|
||||||
ATM90E32ClearOffsetCalibrationButton,
|
ATM90E32ClearCalibrationButton,
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||||
icon="mdi:delete",
|
icon=ICON_CHIP,
|
||||||
),
|
|
||||||
cv.Optional(CONF_RUN_POWER_OFFSET_CALIBRATION): button.button_schema(
|
|
||||||
ATM90E32PowerOffsetCalibrationButton,
|
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
|
||||||
icon=ICON_SCALE,
|
|
||||||
),
|
|
||||||
cv.Optional(CONF_CLEAR_POWER_OFFSET_CALIBRATION): button.button_schema(
|
|
||||||
ATM90E32ClearPowerOffsetCalibrationButton,
|
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
|
||||||
icon="mdi:delete",
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
parent = await cg.get_variable(config[CONF_ID])
|
parent = await cg.get_variable(config[CONF_ID])
|
||||||
|
|
||||||
if run_gain := config.get(CONF_RUN_GAIN_CALIBRATION):
|
|
||||||
b = await button.new_button(run_gain)
|
|
||||||
await cg.register_parented(b, parent)
|
|
||||||
|
|
||||||
if clear_gain := config.get(CONF_CLEAR_GAIN_CALIBRATION):
|
|
||||||
b = await button.new_button(clear_gain)
|
|
||||||
await cg.register_parented(b, parent)
|
|
||||||
|
|
||||||
if run_offset := config.get(CONF_RUN_OFFSET_CALIBRATION):
|
if run_offset := config.get(CONF_RUN_OFFSET_CALIBRATION):
|
||||||
b = await button.new_button(run_offset)
|
b = await button.new_button(run_offset)
|
||||||
await cg.register_parented(b, parent)
|
await cg.register_parented(b, parent)
|
||||||
|
|
||||||
if clear_offset := config.get(CONF_CLEAR_OFFSET_CALIBRATION):
|
if clear_offset := config.get(CONF_CLEAR_OFFSET_CALIBRATION):
|
||||||
b = await button.new_button(clear_offset)
|
b = await button.new_button(clear_offset)
|
||||||
await cg.register_parented(b, parent)
|
await cg.register_parented(b, parent)
|
||||||
|
|
||||||
if run_power := config.get(CONF_RUN_POWER_OFFSET_CALIBRATION):
|
|
||||||
b = await button.new_button(run_power)
|
|
||||||
await cg.register_parented(b, parent)
|
|
||||||
|
|
||||||
if clear_power := config.get(CONF_CLEAR_POWER_OFFSET_CALIBRATION):
|
|
||||||
b = await button.new_button(clear_power)
|
|
||||||
await cg.register_parented(b, parent)
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "atm90e32_button.h"
|
#include "atm90e32_button.h"
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
@@ -7,73 +6,15 @@ namespace atm90e32 {
|
|||||||
|
|
||||||
static const char *const TAG = "atm90e32.button";
|
static const char *const TAG = "atm90e32.button";
|
||||||
|
|
||||||
void ATM90E32GainCalibrationButton::press_action() {
|
void ATM90E32CalibrationButton::press_action() {
|
||||||
if (this->parent_ == nullptr) {
|
ESP_LOGI(TAG, "Running offset calibrations, Note: CTs and ACVs must be 0 during this process...");
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No meters assigned to Gain Calibration button [%s]", this->get_name().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%s", this->get_name().c_str());
|
|
||||||
ESP_LOGI(TAG,
|
|
||||||
"[CALIBRATION] Use gain_ct: & gain_voltage: under each phase_x: in your config file to save these values");
|
|
||||||
this->parent_->run_gain_calibrations();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32ClearGainCalibrationButton::press_action() {
|
|
||||||
if (this->parent_ == nullptr) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No meters assigned to Clear Gain button [%s]", this->get_name().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%s", this->get_name().c_str());
|
|
||||||
this->parent_->clear_gain_calibrations();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32OffsetCalibrationButton::press_action() {
|
|
||||||
if (this->parent_ == nullptr) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No meters assigned to Offset Calibration button [%s]", this->get_name().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%s", this->get_name().c_str());
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] **NOTE: CTs and ACVs must be 0 during this process. USB power only**");
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Use offset_voltage: & offset_current: under each phase_x: in your config file to save "
|
|
||||||
"these values");
|
|
||||||
this->parent_->run_offset_calibrations();
|
this->parent_->run_offset_calibrations();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ATM90E32ClearOffsetCalibrationButton::press_action() {
|
void ATM90E32ClearCalibrationButton::press_action() {
|
||||||
if (this->parent_ == nullptr) {
|
ESP_LOGI(TAG, "Offset calibrations cleared.");
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No meters assigned to Clear Offset button [%s]", this->get_name().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%s", this->get_name().c_str());
|
|
||||||
this->parent_->clear_offset_calibrations();
|
this->parent_->clear_offset_calibrations();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ATM90E32PowerOffsetCalibrationButton::press_action() {
|
|
||||||
if (this->parent_ == nullptr) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No meters assigned to Power Calibration button [%s]", this->get_name().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%s", this->get_name().c_str());
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] **NOTE: CTs must be 0 during this process. Voltage reference should be present**");
|
|
||||||
ESP_LOGI(TAG, "[CALIBRATION] Use offset_active_power: & offset_reactive_power: under each phase_x: in your config "
|
|
||||||
"file to save these values");
|
|
||||||
this->parent_->run_power_offset_calibrations();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ATM90E32ClearPowerOffsetCalibrationButton::press_action() {
|
|
||||||
if (this->parent_ == nullptr) {
|
|
||||||
ESP_LOGW(TAG, "[CALIBRATION] No meters assigned to Clear Power button [%s]", this->get_name().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%s", this->get_name().c_str());
|
|
||||||
this->parent_->clear_power_offset_calibrations();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace atm90e32
|
} // namespace atm90e32
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|||||||
@@ -7,49 +7,17 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace atm90e32 {
|
namespace atm90e32 {
|
||||||
|
|
||||||
class ATM90E32GainCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
class ATM90E32CalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
||||||
public:
|
public:
|
||||||
ATM90E32GainCalibrationButton() = default;
|
ATM90E32CalibrationButton() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void press_action() override;
|
void press_action() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ATM90E32ClearGainCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
class ATM90E32ClearCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
||||||
public:
|
public:
|
||||||
ATM90E32ClearGainCalibrationButton() = default;
|
ATM90E32ClearCalibrationButton() = default;
|
||||||
|
|
||||||
protected:
|
|
||||||
void press_action() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ATM90E32OffsetCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
|
||||||
public:
|
|
||||||
ATM90E32OffsetCalibrationButton() = default;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void press_action() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ATM90E32ClearOffsetCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
|
||||||
public:
|
|
||||||
ATM90E32ClearOffsetCalibrationButton() = default;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void press_action() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ATM90E32PowerOffsetCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
|
||||||
public:
|
|
||||||
ATM90E32PowerOffsetCalibrationButton() = default;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void press_action() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ATM90E32ClearPowerOffsetCalibrationButton : public button::Button, public Parented<ATM90E32Component> {
|
|
||||||
public:
|
|
||||||
ATM90E32ClearPowerOffsetCalibrationButton() = default;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void press_action() override;
|
void press_action() override;
|
||||||
|
|||||||
@@ -1,130 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
from esphome.components import number
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.const import (
|
|
||||||
CONF_ID,
|
|
||||||
CONF_MAX_VALUE,
|
|
||||||
CONF_MIN_VALUE,
|
|
||||||
CONF_MODE,
|
|
||||||
CONF_PHASE_A,
|
|
||||||
CONF_PHASE_B,
|
|
||||||
CONF_PHASE_C,
|
|
||||||
CONF_REFERENCE_VOLTAGE,
|
|
||||||
CONF_STEP,
|
|
||||||
ENTITY_CATEGORY_CONFIG,
|
|
||||||
UNIT_AMPERE,
|
|
||||||
UNIT_VOLT,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .. import atm90e32_ns
|
|
||||||
from ..sensor import ATM90E32Component
|
|
||||||
|
|
||||||
ATM90E32Number = atm90e32_ns.class_(
|
|
||||||
"ATM90E32Number", number.Number, cg.Parented.template(ATM90E32Component)
|
|
||||||
)
|
|
||||||
|
|
||||||
CONF_REFERENCE_CURRENT = "reference_current"
|
|
||||||
PHASE_KEYS = [CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C]
|
|
||||||
|
|
||||||
|
|
||||||
REFERENCE_VOLTAGE_PHASE_SCHEMA = cv.All(
|
|
||||||
cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Optional(CONF_MODE, default="box"): cv.string,
|
|
||||||
cv.Optional(CONF_MIN_VALUE, default=100.0): cv.float_,
|
|
||||||
cv.Optional(CONF_MAX_VALUE, default=260.0): cv.float_,
|
|
||||||
cv.Optional(CONF_STEP, default=0.1): cv.float_,
|
|
||||||
}
|
|
||||||
).extend(
|
|
||||||
number.number_schema(
|
|
||||||
class_=ATM90E32Number,
|
|
||||||
unit_of_measurement=UNIT_VOLT,
|
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
|
||||||
icon="mdi:power-plug",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
REFERENCE_CURRENT_PHASE_SCHEMA = cv.All(
|
|
||||||
cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Optional(CONF_MODE, default="box"): cv.string,
|
|
||||||
cv.Optional(CONF_MIN_VALUE, default=1.0): cv.float_,
|
|
||||||
cv.Optional(CONF_MAX_VALUE, default=200.0): cv.float_,
|
|
||||||
cv.Optional(CONF_STEP, default=0.1): cv.float_,
|
|
||||||
}
|
|
||||||
).extend(
|
|
||||||
number.number_schema(
|
|
||||||
class_=ATM90E32Number,
|
|
||||||
unit_of_measurement=UNIT_AMPERE,
|
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
|
||||||
icon="mdi:home-lightning-bolt",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
REFERENCE_VOLTAGE_SCHEMA = cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Optional(CONF_PHASE_A): REFERENCE_VOLTAGE_PHASE_SCHEMA,
|
|
||||||
cv.Optional(CONF_PHASE_B): REFERENCE_VOLTAGE_PHASE_SCHEMA,
|
|
||||||
cv.Optional(CONF_PHASE_C): REFERENCE_VOLTAGE_PHASE_SCHEMA,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
REFERENCE_CURRENT_SCHEMA = cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Optional(CONF_PHASE_A): REFERENCE_CURRENT_PHASE_SCHEMA,
|
|
||||||
cv.Optional(CONF_PHASE_B): REFERENCE_CURRENT_PHASE_SCHEMA,
|
|
||||||
cv.Optional(CONF_PHASE_C): REFERENCE_CURRENT_PHASE_SCHEMA,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.Schema(
|
|
||||||
{
|
|
||||||
cv.GenerateID(CONF_ID): cv.use_id(ATM90E32Component),
|
|
||||||
cv.Optional(CONF_REFERENCE_VOLTAGE): REFERENCE_VOLTAGE_SCHEMA,
|
|
||||||
cv.Optional(CONF_REFERENCE_CURRENT): REFERENCE_CURRENT_SCHEMA,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
|
||||||
parent = await cg.get_variable(config[CONF_ID])
|
|
||||||
|
|
||||||
if voltage_cfg := config.get(CONF_REFERENCE_VOLTAGE):
|
|
||||||
voltage_objs = [None, None, None]
|
|
||||||
|
|
||||||
for i, key in enumerate(PHASE_KEYS):
|
|
||||||
if validated := voltage_cfg.get(key):
|
|
||||||
obj = await number.new_number(
|
|
||||||
validated,
|
|
||||||
min_value=validated["min_value"],
|
|
||||||
max_value=validated["max_value"],
|
|
||||||
step=validated["step"],
|
|
||||||
)
|
|
||||||
await cg.register_parented(obj, parent)
|
|
||||||
voltage_objs[i] = obj
|
|
||||||
|
|
||||||
# Inherit from A → B/C if only A defined
|
|
||||||
if voltage_objs[0] is not None:
|
|
||||||
for i in range(3):
|
|
||||||
if voltage_objs[i] is None:
|
|
||||||
voltage_objs[i] = voltage_objs[0]
|
|
||||||
|
|
||||||
for i, obj in enumerate(voltage_objs):
|
|
||||||
if obj is not None:
|
|
||||||
cg.add(parent.set_reference_voltage(i, obj))
|
|
||||||
|
|
||||||
if current_cfg := config.get(CONF_REFERENCE_CURRENT):
|
|
||||||
for i, key in enumerate(PHASE_KEYS):
|
|
||||||
if validated := current_cfg.get(key):
|
|
||||||
obj = await number.new_number(
|
|
||||||
validated,
|
|
||||||
min_value=validated["min_value"],
|
|
||||||
max_value=validated["max_value"],
|
|
||||||
step=validated["step"],
|
|
||||||
)
|
|
||||||
await cg.register_parented(obj, parent)
|
|
||||||
cg.add(parent.set_reference_current(i, obj))
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/components/atm90e32/atm90e32.h"
|
|
||||||
#include "esphome/components/number/number.h"
|
|
||||||
|
|
||||||
namespace esphome {
|
|
||||||
namespace atm90e32 {
|
|
||||||
|
|
||||||
class ATM90E32Number : public number::Number, public Parented<ATM90E32Component> {
|
|
||||||
public:
|
|
||||||
void control(float value) override { this->publish_state(value); }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace atm90e32
|
|
||||||
} // namespace esphome
|
|
||||||
@@ -33,7 +33,6 @@ from esphome.const import (
|
|||||||
UNIT_DEGREES,
|
UNIT_DEGREES,
|
||||||
UNIT_HERTZ,
|
UNIT_HERTZ,
|
||||||
UNIT_VOLT,
|
UNIT_VOLT,
|
||||||
UNIT_VOLT_AMPS,
|
|
||||||
UNIT_VOLT_AMPS_REACTIVE,
|
UNIT_VOLT_AMPS_REACTIVE,
|
||||||
UNIT_WATT,
|
UNIT_WATT,
|
||||||
UNIT_WATT_HOURS,
|
UNIT_WATT_HOURS,
|
||||||
@@ -46,17 +45,10 @@ CONF_GAIN_PGA = "gain_pga"
|
|||||||
CONF_CURRENT_PHASES = "current_phases"
|
CONF_CURRENT_PHASES = "current_phases"
|
||||||
CONF_GAIN_VOLTAGE = "gain_voltage"
|
CONF_GAIN_VOLTAGE = "gain_voltage"
|
||||||
CONF_GAIN_CT = "gain_ct"
|
CONF_GAIN_CT = "gain_ct"
|
||||||
CONF_OFFSET_VOLTAGE = "offset_voltage"
|
|
||||||
CONF_OFFSET_CURRENT = "offset_current"
|
|
||||||
CONF_OFFSET_ACTIVE_POWER = "offset_active_power"
|
|
||||||
CONF_OFFSET_REACTIVE_POWER = "offset_reactive_power"
|
|
||||||
CONF_HARMONIC_POWER = "harmonic_power"
|
CONF_HARMONIC_POWER = "harmonic_power"
|
||||||
CONF_PEAK_CURRENT = "peak_current"
|
CONF_PEAK_CURRENT = "peak_current"
|
||||||
CONF_PEAK_CURRENT_SIGNED = "peak_current_signed"
|
CONF_PEAK_CURRENT_SIGNED = "peak_current_signed"
|
||||||
CONF_ENABLE_OFFSET_CALIBRATION = "enable_offset_calibration"
|
CONF_ENABLE_OFFSET_CALIBRATION = "enable_offset_calibration"
|
||||||
CONF_ENABLE_GAIN_CALIBRATION = "enable_gain_calibration"
|
|
||||||
CONF_PHASE_STATUS = "phase_status"
|
|
||||||
CONF_FREQUENCY_STATUS = "frequency_status"
|
|
||||||
UNIT_DEG = "degrees"
|
UNIT_DEG = "degrees"
|
||||||
LINE_FREQS = {
|
LINE_FREQS = {
|
||||||
"50HZ": 50,
|
"50HZ": 50,
|
||||||
@@ -100,11 +92,10 @@ ATM90E32_PHASE_SCHEMA = cv.Schema(
|
|||||||
unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE,
|
unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE,
|
||||||
icon=ICON_LIGHTBULB,
|
icon=ICON_LIGHTBULB,
|
||||||
accuracy_decimals=2,
|
accuracy_decimals=2,
|
||||||
device_class=DEVICE_CLASS_POWER,
|
|
||||||
state_class=STATE_CLASS_MEASUREMENT,
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_APPARENT_POWER): sensor.sensor_schema(
|
cv.Optional(CONF_APPARENT_POWER): sensor.sensor_schema(
|
||||||
unit_of_measurement=UNIT_VOLT_AMPS,
|
unit_of_measurement=UNIT_WATT,
|
||||||
accuracy_decimals=2,
|
accuracy_decimals=2,
|
||||||
device_class=DEVICE_CLASS_POWER,
|
device_class=DEVICE_CLASS_POWER,
|
||||||
state_class=STATE_CLASS_MEASUREMENT,
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
@@ -146,10 +137,6 @@ ATM90E32_PHASE_SCHEMA = cv.Schema(
|
|||||||
),
|
),
|
||||||
cv.Optional(CONF_GAIN_VOLTAGE, default=7305): cv.uint16_t,
|
cv.Optional(CONF_GAIN_VOLTAGE, default=7305): cv.uint16_t,
|
||||||
cv.Optional(CONF_GAIN_CT, default=27961): cv.uint16_t,
|
cv.Optional(CONF_GAIN_CT, default=27961): cv.uint16_t,
|
||||||
cv.Optional(CONF_OFFSET_VOLTAGE, default=0): cv.int_,
|
|
||||||
cv.Optional(CONF_OFFSET_CURRENT, default=0): cv.int_,
|
|
||||||
cv.Optional(CONF_OFFSET_ACTIVE_POWER, default=0): cv.int_,
|
|
||||||
cv.Optional(CONF_OFFSET_REACTIVE_POWER, default=0): cv.int_,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -177,10 +164,9 @@ CONFIG_SCHEMA = (
|
|||||||
cv.Optional(CONF_CURRENT_PHASES, default="3"): cv.enum(
|
cv.Optional(CONF_CURRENT_PHASES, default="3"): cv.enum(
|
||||||
CURRENT_PHASES, upper=True
|
CURRENT_PHASES, upper=True
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_GAIN_PGA, default="1X"): cv.enum(PGA_GAINS, upper=True),
|
cv.Optional(CONF_GAIN_PGA, default="2X"): cv.enum(PGA_GAINS, upper=True),
|
||||||
cv.Optional(CONF_PEAK_CURRENT_SIGNED, default=False): cv.boolean,
|
cv.Optional(CONF_PEAK_CURRENT_SIGNED, default=False): cv.boolean,
|
||||||
cv.Optional(CONF_ENABLE_OFFSET_CALIBRATION, default=False): cv.boolean,
|
cv.Optional(CONF_ENABLE_OFFSET_CALIBRATION, default=False): cv.boolean,
|
||||||
cv.Optional(CONF_ENABLE_GAIN_CALIBRATION, default=False): cv.boolean,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(cv.polling_component_schema("60s"))
|
.extend(cv.polling_component_schema("60s"))
|
||||||
@@ -199,10 +185,6 @@ async def to_code(config):
|
|||||||
conf = config[phase]
|
conf = config[phase]
|
||||||
cg.add(var.set_volt_gain(i, conf[CONF_GAIN_VOLTAGE]))
|
cg.add(var.set_volt_gain(i, conf[CONF_GAIN_VOLTAGE]))
|
||||||
cg.add(var.set_ct_gain(i, conf[CONF_GAIN_CT]))
|
cg.add(var.set_ct_gain(i, conf[CONF_GAIN_CT]))
|
||||||
cg.add(var.set_voltage_offset(i, conf[CONF_OFFSET_VOLTAGE]))
|
|
||||||
cg.add(var.set_current_offset(i, conf[CONF_OFFSET_CURRENT]))
|
|
||||||
cg.add(var.set_active_power_offset(i, conf[CONF_OFFSET_ACTIVE_POWER]))
|
|
||||||
cg.add(var.set_reactive_power_offset(i, conf[CONF_OFFSET_REACTIVE_POWER]))
|
|
||||||
if voltage_config := conf.get(CONF_VOLTAGE):
|
if voltage_config := conf.get(CONF_VOLTAGE):
|
||||||
sens = await sensor.new_sensor(voltage_config)
|
sens = await sensor.new_sensor(voltage_config)
|
||||||
cg.add(var.set_voltage_sensor(i, sens))
|
cg.add(var.set_voltage_sensor(i, sens))
|
||||||
@@ -236,15 +218,16 @@ async def to_code(config):
|
|||||||
if peak_current_config := conf.get(CONF_PEAK_CURRENT):
|
if peak_current_config := conf.get(CONF_PEAK_CURRENT):
|
||||||
sens = await sensor.new_sensor(peak_current_config)
|
sens = await sensor.new_sensor(peak_current_config)
|
||||||
cg.add(var.set_peak_current_sensor(i, sens))
|
cg.add(var.set_peak_current_sensor(i, sens))
|
||||||
|
|
||||||
if frequency_config := config.get(CONF_FREQUENCY):
|
if frequency_config := config.get(CONF_FREQUENCY):
|
||||||
sens = await sensor.new_sensor(frequency_config)
|
sens = await sensor.new_sensor(frequency_config)
|
||||||
cg.add(var.set_freq_sensor(sens))
|
cg.add(var.set_freq_sensor(sens))
|
||||||
if chip_temperature_config := config.get(CONF_CHIP_TEMPERATURE):
|
if chip_temperature_config := config.get(CONF_CHIP_TEMPERATURE):
|
||||||
sens = await sensor.new_sensor(chip_temperature_config)
|
sens = await sensor.new_sensor(chip_temperature_config)
|
||||||
cg.add(var.set_chip_temperature_sensor(sens))
|
cg.add(var.set_chip_temperature_sensor(sens))
|
||||||
|
|
||||||
cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY]))
|
cg.add(var.set_line_freq(config[CONF_LINE_FREQUENCY]))
|
||||||
cg.add(var.set_current_phases(config[CONF_CURRENT_PHASES]))
|
cg.add(var.set_current_phases(config[CONF_CURRENT_PHASES]))
|
||||||
cg.add(var.set_pga_gain(config[CONF_GAIN_PGA]))
|
cg.add(var.set_pga_gain(config[CONF_GAIN_PGA]))
|
||||||
cg.add(var.set_peak_current_signed(config[CONF_PEAK_CURRENT_SIGNED]))
|
cg.add(var.set_peak_current_signed(config[CONF_PEAK_CURRENT_SIGNED]))
|
||||||
cg.add(var.set_enable_offset_calibration(config[CONF_ENABLE_OFFSET_CALIBRATION]))
|
cg.add(var.set_enable_offset_calibration(config[CONF_ENABLE_OFFSET_CALIBRATION]))
|
||||||
cg.add(var.set_enable_gain_calibration(config[CONF_ENABLE_GAIN_CALIBRATION]))
|
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
from esphome.components import text_sensor
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.const import CONF_ID, CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C
|
|
||||||
|
|
||||||
from ..sensor import ATM90E32Component
|
|
||||||
|
|
||||||
CONF_PHASE_STATUS = "phase_status"
|
|
||||||
CONF_FREQUENCY_STATUS = "frequency_status"
|
|
||||||
PHASE_KEYS = [CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C]
|
|
||||||
|
|
||||||
PHASE_STATUS_SCHEMA = cv.Schema(
|
|
||||||
{
|
|
||||||
cv.Optional(CONF_PHASE_A): text_sensor.text_sensor_schema(
|
|
||||||
icon="mdi:flash-alert"
|
|
||||||
),
|
|
||||||
cv.Optional(CONF_PHASE_B): text_sensor.text_sensor_schema(
|
|
||||||
icon="mdi:flash-alert"
|
|
||||||
),
|
|
||||||
cv.Optional(CONF_PHASE_C): text_sensor.text_sensor_schema(
|
|
||||||
icon="mdi:flash-alert"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.Schema(
|
|
||||||
{
|
|
||||||
cv.GenerateID(): cv.use_id(ATM90E32Component),
|
|
||||||
cv.Optional(CONF_PHASE_STATUS): PHASE_STATUS_SCHEMA,
|
|
||||||
cv.Optional(CONF_FREQUENCY_STATUS): text_sensor.text_sensor_schema(
|
|
||||||
icon="mdi:lightbulb-alert"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
|
||||||
parent = await cg.get_variable(config[CONF_ID])
|
|
||||||
|
|
||||||
if phase_cfg := config.get(CONF_PHASE_STATUS):
|
|
||||||
for i, key in enumerate(PHASE_KEYS):
|
|
||||||
if sub_phase_cfg := phase_cfg.get(key):
|
|
||||||
sens = await text_sensor.new_text_sensor(sub_phase_cfg)
|
|
||||||
cg.add(parent.set_phase_status_text_sensor(i, sens))
|
|
||||||
|
|
||||||
if freq_status_config := config.get(CONF_FREQUENCY_STATUS):
|
|
||||||
sens = await text_sensor.new_text_sensor(freq_status_config)
|
|
||||||
cg.add(parent.set_freq_status_text_sensor(sens))
|
|
||||||
@@ -37,13 +37,16 @@ AUDIO_COMPONENT_SCHEMA = cv.Schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_UNDEF = object()
|
||||||
|
|
||||||
|
|
||||||
def set_stream_limits(
|
def set_stream_limits(
|
||||||
min_bits_per_sample: int = cv.UNDEFINED,
|
min_bits_per_sample: int = _UNDEF,
|
||||||
max_bits_per_sample: int = cv.UNDEFINED,
|
max_bits_per_sample: int = _UNDEF,
|
||||||
min_channels: int = cv.UNDEFINED,
|
min_channels: int = _UNDEF,
|
||||||
max_channels: int = cv.UNDEFINED,
|
max_channels: int = _UNDEF,
|
||||||
min_sample_rate: int = cv.UNDEFINED,
|
min_sample_rate: int = _UNDEF,
|
||||||
max_sample_rate: int = cv.UNDEFINED,
|
max_sample_rate: int = _UNDEF,
|
||||||
):
|
):
|
||||||
"""Sets the limits for the audio stream that audio component can handle
|
"""Sets the limits for the audio stream that audio component can handle
|
||||||
|
|
||||||
@@ -52,17 +55,17 @@ def set_stream_limits(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def set_limits_in_config(config):
|
def set_limits_in_config(config):
|
||||||
if min_bits_per_sample is not cv.UNDEFINED:
|
if min_bits_per_sample is not _UNDEF:
|
||||||
config[CONF_MIN_BITS_PER_SAMPLE] = min_bits_per_sample
|
config[CONF_MIN_BITS_PER_SAMPLE] = min_bits_per_sample
|
||||||
if max_bits_per_sample is not cv.UNDEFINED:
|
if max_bits_per_sample is not _UNDEF:
|
||||||
config[CONF_MAX_BITS_PER_SAMPLE] = max_bits_per_sample
|
config[CONF_MAX_BITS_PER_SAMPLE] = max_bits_per_sample
|
||||||
if min_channels is not cv.UNDEFINED:
|
if min_channels is not _UNDEF:
|
||||||
config[CONF_MIN_CHANNELS] = min_channels
|
config[CONF_MIN_CHANNELS] = min_channels
|
||||||
if max_channels is not cv.UNDEFINED:
|
if max_channels is not _UNDEF:
|
||||||
config[CONF_MAX_CHANNELS] = max_channels
|
config[CONF_MAX_CHANNELS] = max_channels
|
||||||
if min_sample_rate is not cv.UNDEFINED:
|
if min_sample_rate is not _UNDEF:
|
||||||
config[CONF_MIN_SAMPLE_RATE] = min_sample_rate
|
config[CONF_MIN_SAMPLE_RATE] = min_sample_rate
|
||||||
if max_sample_rate is not cv.UNDEFINED:
|
if max_sample_rate is not _UNDEF:
|
||||||
config[CONF_MAX_SAMPLE_RATE] = max_sample_rate
|
config[CONF_MAX_SAMPLE_RATE] = max_sample_rate
|
||||||
|
|
||||||
return set_limits_in_config
|
return set_limits_in_config
|
||||||
@@ -72,10 +75,10 @@ def final_validate_audio_schema(
|
|||||||
name: str,
|
name: str,
|
||||||
*,
|
*,
|
||||||
audio_device: str,
|
audio_device: str,
|
||||||
bits_per_sample: int = cv.UNDEFINED,
|
bits_per_sample: int = _UNDEF,
|
||||||
channels: int = cv.UNDEFINED,
|
channels: int = _UNDEF,
|
||||||
sample_rate: int = cv.UNDEFINED,
|
sample_rate: int = _UNDEF,
|
||||||
enabled_channels: list[int] = cv.UNDEFINED,
|
enabled_channels: list[int] = _UNDEF,
|
||||||
audio_device_issue: bool = False,
|
audio_device_issue: bool = False,
|
||||||
):
|
):
|
||||||
"""Validates audio compatibility when passed between different components.
|
"""Validates audio compatibility when passed between different components.
|
||||||
@@ -98,7 +101,7 @@ def final_validate_audio_schema(
|
|||||||
def validate_audio_compatiblity(audio_config):
|
def validate_audio_compatiblity(audio_config):
|
||||||
audio_schema = {}
|
audio_schema = {}
|
||||||
|
|
||||||
if bits_per_sample is not cv.UNDEFINED:
|
if bits_per_sample is not _UNDEF:
|
||||||
try:
|
try:
|
||||||
cv.int_range(
|
cv.int_range(
|
||||||
min=audio_config.get(CONF_MIN_BITS_PER_SAMPLE),
|
min=audio_config.get(CONF_MIN_BITS_PER_SAMPLE),
|
||||||
@@ -111,7 +114,7 @@ def final_validate_audio_schema(
|
|||||||
error_string = f"Invalid configuration for the {name} component. The {CONF_BITS_PER_SAMPLE} {str(exc)}"
|
error_string = f"Invalid configuration for the {name} component. The {CONF_BITS_PER_SAMPLE} {str(exc)}"
|
||||||
raise cv.Invalid(error_string) from exc
|
raise cv.Invalid(error_string) from exc
|
||||||
|
|
||||||
if channels is not cv.UNDEFINED:
|
if channels is not _UNDEF:
|
||||||
try:
|
try:
|
||||||
cv.int_range(
|
cv.int_range(
|
||||||
min=audio_config.get(CONF_MIN_CHANNELS),
|
min=audio_config.get(CONF_MIN_CHANNELS),
|
||||||
@@ -124,7 +127,7 @@ def final_validate_audio_schema(
|
|||||||
error_string = f"Invalid configuration for the {name} component. The {CONF_NUM_CHANNELS} {str(exc)}"
|
error_string = f"Invalid configuration for the {name} component. The {CONF_NUM_CHANNELS} {str(exc)}"
|
||||||
raise cv.Invalid(error_string) from exc
|
raise cv.Invalid(error_string) from exc
|
||||||
|
|
||||||
if sample_rate is not cv.UNDEFINED:
|
if sample_rate is not _UNDEF:
|
||||||
try:
|
try:
|
||||||
cv.int_range(
|
cv.int_range(
|
||||||
min=audio_config.get(CONF_MIN_SAMPLE_RATE),
|
min=audio_config.get(CONF_MIN_SAMPLE_RATE),
|
||||||
@@ -137,7 +140,7 @@ def final_validate_audio_schema(
|
|||||||
error_string = f"Invalid configuration for the {name} component. The {CONF_SAMPLE_RATE} {str(exc)}"
|
error_string = f"Invalid configuration for the {name} component. The {CONF_SAMPLE_RATE} {str(exc)}"
|
||||||
raise cv.Invalid(error_string) from exc
|
raise cv.Invalid(error_string) from exc
|
||||||
|
|
||||||
if enabled_channels is not cv.UNDEFINED:
|
if enabled_channels is not _UNDEF:
|
||||||
for channel in enabled_channels:
|
for channel in enabled_channels:
|
||||||
try:
|
try:
|
||||||
# Channels are 0-indexed
|
# Channels are 0-indexed
|
||||||
@@ -165,4 +168,4 @@ def final_validate_audio_schema(
|
|||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
cg.add_library("esphome/esp-audio-libs", "1.1.4")
|
cg.add_library("esphome/esp-audio-libs", "1.1.3")
|
||||||
|
|||||||
@@ -135,53 +135,5 @@ const char *audio_file_type_to_string(AudioFileType file_type);
|
|||||||
void scale_audio_samples(const int16_t *audio_samples, int16_t *output_buffer, int16_t scale_factor,
|
void scale_audio_samples(const int16_t *audio_samples, int16_t *output_buffer, int16_t scale_factor,
|
||||||
size_t samples_to_scale);
|
size_t samples_to_scale);
|
||||||
|
|
||||||
/// @brief Unpacks a quantized audio sample into a Q31 fixed-point number.
|
|
||||||
/// @param data Pointer to uint8_t array containing the audio sample
|
|
||||||
/// @param bytes_per_sample The number of bytes per sample
|
|
||||||
/// @return Q31 sample
|
|
||||||
inline int32_t unpack_audio_sample_to_q31(const uint8_t *data, size_t bytes_per_sample) {
|
|
||||||
int32_t sample = 0;
|
|
||||||
if (bytes_per_sample == 1) {
|
|
||||||
sample |= data[0] << 24;
|
|
||||||
} else if (bytes_per_sample == 2) {
|
|
||||||
sample |= data[0] << 16;
|
|
||||||
sample |= data[1] << 24;
|
|
||||||
} else if (bytes_per_sample == 3) {
|
|
||||||
sample |= data[0] << 8;
|
|
||||||
sample |= data[1] << 16;
|
|
||||||
sample |= data[2] << 24;
|
|
||||||
} else if (bytes_per_sample == 4) {
|
|
||||||
sample |= data[0];
|
|
||||||
sample |= data[1] << 8;
|
|
||||||
sample |= data[2] << 16;
|
|
||||||
sample |= data[3] << 24;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sample;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @brief Packs a Q31 fixed-point number as an audio sample with the specified number of bytes per sample.
|
|
||||||
/// Packs the most significant bits - no dithering is applied.
|
|
||||||
/// @param sample Q31 fixed-point number to pack
|
|
||||||
/// @param data Pointer to data array to store
|
|
||||||
/// @param bytes_per_sample The audio data's bytes per sample
|
|
||||||
inline void pack_q31_as_audio_sample(int32_t sample, uint8_t *data, size_t bytes_per_sample) {
|
|
||||||
if (bytes_per_sample == 1) {
|
|
||||||
data[0] = static_cast<uint8_t>(sample >> 24);
|
|
||||||
} else if (bytes_per_sample == 2) {
|
|
||||||
data[0] = static_cast<uint8_t>(sample >> 16);
|
|
||||||
data[1] = static_cast<uint8_t>(sample >> 24);
|
|
||||||
} else if (bytes_per_sample == 3) {
|
|
||||||
data[0] = static_cast<uint8_t>(sample >> 8);
|
|
||||||
data[1] = static_cast<uint8_t>(sample >> 16);
|
|
||||||
data[2] = static_cast<uint8_t>(sample >> 24);
|
|
||||||
} else if (bytes_per_sample == 4) {
|
|
||||||
data[0] = static_cast<uint8_t>(sample);
|
|
||||||
data[1] = static_cast<uint8_t>(sample >> 8);
|
|
||||||
data[2] = static_cast<uint8_t>(sample >> 16);
|
|
||||||
data[3] = static_cast<uint8_t>(sample >> 24);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace audio
|
} // namespace audio
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ AudioDecoderState AudioDecoder::decode(bool stop_gracefully) {
|
|||||||
|
|
||||||
bytes_available_before_processing = this->input_transfer_buffer_->available();
|
bytes_available_before_processing = this->input_transfer_buffer_->available();
|
||||||
|
|
||||||
if ((this->potentially_failed_count_ > 0) && (bytes_read == 0)) {
|
if ((this->potentially_failed_count_ > 10) && (bytes_read == 0)) {
|
||||||
// Failed to decode in last attempt and there is no new data
|
// Failed to decode in last attempt and there is no new data
|
||||||
|
|
||||||
if ((this->input_transfer_buffer_->free() == 0) && first_loop_iteration) {
|
if ((this->input_transfer_buffer_->free() == 0) && first_loop_iteration) {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
|
||||||
|
|
||||||
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
||||||
#include "esp_crt_bundle.h"
|
#include "esp_crt_bundle.h"
|
||||||
@@ -17,13 +16,13 @@ namespace audio {
|
|||||||
static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
|
static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
|
||||||
|
|
||||||
static const uint32_t CONNECTION_TIMEOUT_MS = 5000;
|
static const uint32_t CONNECTION_TIMEOUT_MS = 5000;
|
||||||
static const uint8_t MAX_FETCHING_HEADER_ATTEMPTS = 6;
|
|
||||||
|
// The number of times the http read times out with no data before throwing an error
|
||||||
|
static const uint32_t ERROR_COUNT_NO_DATA_READ_TIMEOUT = 100;
|
||||||
|
|
||||||
static const size_t HTTP_STREAM_BUFFER_SIZE = 2048;
|
static const size_t HTTP_STREAM_BUFFER_SIZE = 2048;
|
||||||
|
|
||||||
static const uint8_t MAX_REDIRECTIONS = 5;
|
static const uint8_t MAX_REDIRECTION = 5;
|
||||||
|
|
||||||
static const char *const TAG = "audio_reader";
|
|
||||||
|
|
||||||
// Some common HTTP status codes - borrowed from http_request component accessed 20241224
|
// Some common HTTP status codes - borrowed from http_request component accessed 20241224
|
||||||
enum HttpStatus {
|
enum HttpStatus {
|
||||||
@@ -95,7 +94,7 @@ esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
|
|||||||
client_config.url = uri.c_str();
|
client_config.url = uri.c_str();
|
||||||
client_config.cert_pem = nullptr;
|
client_config.cert_pem = nullptr;
|
||||||
client_config.disable_auto_redirect = false;
|
client_config.disable_auto_redirect = false;
|
||||||
client_config.max_redirection_count = MAX_REDIRECTIONS;
|
client_config.max_redirection_count = 10;
|
||||||
client_config.event_handler = http_event_handler;
|
client_config.event_handler = http_event_handler;
|
||||||
client_config.user_data = this;
|
client_config.user_data = this;
|
||||||
client_config.buffer_size = HTTP_STREAM_BUFFER_SIZE;
|
client_config.buffer_size = HTTP_STREAM_BUFFER_SIZE;
|
||||||
@@ -117,29 +116,12 @@ esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
|
|||||||
esp_err_t err = esp_http_client_open(this->client_, 0);
|
esp_err_t err = esp_http_client_open(this->client_, 0);
|
||||||
|
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "Failed to open URL");
|
|
||||||
this->cleanup_connection_();
|
this->cleanup_connection_();
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t header_length = esp_http_client_fetch_headers(this->client_);
|
int64_t header_length = esp_http_client_fetch_headers(this->client_);
|
||||||
uint8_t reattempt_count = 0;
|
|
||||||
while ((header_length < 0) && (reattempt_count < MAX_FETCHING_HEADER_ATTEMPTS)) {
|
|
||||||
this->cleanup_connection_();
|
|
||||||
if (header_length != -ESP_ERR_HTTP_EAGAIN) {
|
|
||||||
// Serious error, no recovery
|
|
||||||
return ESP_FAIL;
|
|
||||||
} else {
|
|
||||||
// Reconnect from a fresh state to avoid a bug where it never reads the headers even if made available
|
|
||||||
this->client_ = esp_http_client_init(&client_config);
|
|
||||||
esp_http_client_open(this->client_, 0);
|
|
||||||
header_length = esp_http_client_fetch_headers(this->client_);
|
|
||||||
++reattempt_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (header_length < 0) {
|
if (header_length < 0) {
|
||||||
ESP_LOGE(TAG, "Failed to fetch headers");
|
|
||||||
this->cleanup_connection_();
|
this->cleanup_connection_();
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
@@ -153,7 +135,7 @@ esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
|
|||||||
|
|
||||||
ssize_t redirect_count = 0;
|
ssize_t redirect_count = 0;
|
||||||
|
|
||||||
while ((esp_http_client_set_redirection(this->client_) == ESP_OK) && (redirect_count < MAX_REDIRECTIONS)) {
|
while ((esp_http_client_set_redirection(this->client_) == ESP_OK) && (redirect_count < MAX_REDIRECTION)) {
|
||||||
err = esp_http_client_open(this->client_, 0);
|
err = esp_http_client_open(this->client_, 0);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
this->cleanup_connection_();
|
this->cleanup_connection_();
|
||||||
@@ -285,29 +267,27 @@ AudioReaderState AudioReader::http_read_() {
|
|||||||
return AudioReaderState::FINISHED;
|
return AudioReaderState::FINISHED;
|
||||||
}
|
}
|
||||||
} else if (this->output_transfer_buffer_->free() > 0) {
|
} else if (this->output_transfer_buffer_->free() > 0) {
|
||||||
int received_len = esp_http_client_read(this->client_, (char *) this->output_transfer_buffer_->get_buffer_end(),
|
size_t bytes_to_read = this->output_transfer_buffer_->free();
|
||||||
this->output_transfer_buffer_->free());
|
int received_len =
|
||||||
|
esp_http_client_read(this->client_, (char *) this->output_transfer_buffer_->get_buffer_end(), bytes_to_read);
|
||||||
|
|
||||||
if (received_len > 0) {
|
if (received_len > 0) {
|
||||||
this->output_transfer_buffer_->increase_buffer_length(received_len);
|
this->output_transfer_buffer_->increase_buffer_length(received_len);
|
||||||
this->last_data_read_ms_ = millis();
|
this->last_data_read_ms_ = millis();
|
||||||
return AudioReaderState::READING;
|
} else if (received_len < 0) {
|
||||||
} else if (received_len <= 0) {
|
|
||||||
// HTTP read error
|
// HTTP read error
|
||||||
if (received_len == -1) {
|
this->cleanup_connection_();
|
||||||
// A true connection error occured, no chance at recovery
|
return AudioReaderState::FAILED;
|
||||||
this->cleanup_connection_();
|
} else {
|
||||||
return AudioReaderState::FAILED;
|
if (bytes_to_read > 0) {
|
||||||
}
|
// Read timed out
|
||||||
|
if ((millis() - this->last_data_read_ms_) > CONNECTION_TIMEOUT_MS) {
|
||||||
|
this->cleanup_connection_();
|
||||||
|
return AudioReaderState::FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
// Read timed out, manually verify if it has been too long since the last successful read
|
delay(READ_WRITE_TIMEOUT_MS);
|
||||||
if ((millis() - this->last_data_read_ms_) > MAX_FETCHING_HEADER_ATTEMPTS * CONNECTION_TIMEOUT_MS) {
|
|
||||||
ESP_LOGE(TAG, "Timed out");
|
|
||||||
this->cleanup_connection_();
|
|
||||||
return AudioReaderState::FAILED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(READ_WRITE_TIMEOUT_MS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ constexpr static const uint8_t AXS_READ_TOUCHPAD[11] = {0xb5, 0xab, 0xa5, 0x5a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AXS15231Touchscreen::setup() {
|
void AXS15231Touchscreen::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up AXS15231 Touchscreen...");
|
||||||
if (this->reset_pin_ != nullptr) {
|
if (this->reset_pin_ != nullptr) {
|
||||||
this->reset_pin_->setup();
|
this->reset_pin_->setup();
|
||||||
this->reset_pin_->digital_write(false);
|
this->reset_pin_->digital_write(false);
|
||||||
@@ -60,10 +60,8 @@ void AXS15231Touchscreen::dump_config() {
|
|||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
|
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
|
||||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Width: %d", this->x_raw_max_);
|
||||||
" Width: %d\n"
|
ESP_LOGCONFIG(TAG, " Height: %d", this->y_raw_max_);
|
||||||
" Height: %d",
|
|
||||||
this->x_raw_max_, this->y_raw_max_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace axs15231
|
} // namespace axs15231
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import climate_ir
|
from esphome.components import climate_ir
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.const import CONF_ID
|
||||||
|
|
||||||
AUTO_LOAD = ["climate_ir"]
|
AUTO_LOAD = ["climate_ir"]
|
||||||
CODEOWNERS = ["@bazuchan"]
|
CODEOWNERS = ["@bazuchan"]
|
||||||
@@ -7,8 +9,13 @@ CODEOWNERS = ["@bazuchan"]
|
|||||||
ballu_ns = cg.esphome_ns.namespace("ballu")
|
ballu_ns = cg.esphome_ns.namespace("ballu")
|
||||||
BalluClimate = ballu_ns.class_("BalluClimate", climate_ir.ClimateIR)
|
BalluClimate = ballu_ns.class_("BalluClimate", climate_ir.ClimateIR)
|
||||||
|
|
||||||
CONFIG_SCHEMA = climate_ir.climate_ir_with_receiver_schema(BalluClimate)
|
CONFIG_SCHEMA = climate_ir.CLIMATE_IR_WITH_RECEIVER_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(BalluClimate),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
await climate_ir.new_climate_ir(config)
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
|
await climate_ir.register_climate_ir(var, config)
|
||||||
|
|||||||
@@ -194,14 +194,11 @@ Trigger<> *BangBangClimate::get_heat_trigger() const { return this->heat_trigger
|
|||||||
void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
|
void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
|
||||||
void BangBangClimate::dump_config() {
|
void BangBangClimate::dump_config() {
|
||||||
LOG_CLIMATE("", "Bang Bang Climate", this);
|
LOG_CLIMATE("", "Bang Bang Climate", this);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " Supports HEAT: %s", YESNO(this->supports_heat_));
|
||||||
" Supports HEAT: %s\n"
|
ESP_LOGCONFIG(TAG, " Supports COOL: %s", YESNO(this->supports_cool_));
|
||||||
" Supports COOL: %s\n"
|
ESP_LOGCONFIG(TAG, " Supports AWAY mode: %s", YESNO(this->supports_away_));
|
||||||
" Supports AWAY mode: %s\n"
|
ESP_LOGCONFIG(TAG, " Default Target Temperature Low: %.2f°C", this->normal_config_.default_temperature_low);
|
||||||
" Default Target Temperature Low: %.2f°C\n"
|
ESP_LOGCONFIG(TAG, " Default Target Temperature High: %.2f°C", this->normal_config_.default_temperature_high);
|
||||||
" Default Target Temperature High: %.2f°C",
|
|
||||||
YESNO(this->supports_heat_), YESNO(this->supports_cool_), YESNO(this->supports_away_),
|
|
||||||
this->normal_config_.default_temperature_low, this->normal_config_.default_temperature_high);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BangBangClimateTargetTempConfig::BangBangClimateTargetTempConfig() = default;
|
BangBangClimateTargetTempConfig::BangBangClimateTargetTempConfig() = default;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from esphome.const import (
|
|||||||
CONF_DEFAULT_TARGET_TEMPERATURE_LOW,
|
CONF_DEFAULT_TARGET_TEMPERATURE_LOW,
|
||||||
CONF_HEAT_ACTION,
|
CONF_HEAT_ACTION,
|
||||||
CONF_HUMIDITY_SENSOR,
|
CONF_HUMIDITY_SENSOR,
|
||||||
|
CONF_ID,
|
||||||
CONF_IDLE_ACTION,
|
CONF_IDLE_ACTION,
|
||||||
CONF_SENSOR,
|
CONF_SENSOR,
|
||||||
)
|
)
|
||||||
@@ -18,9 +19,9 @@ BangBangClimate = bang_bang_ns.class_("BangBangClimate", climate.Climate, cg.Com
|
|||||||
BangBangClimateTargetTempConfig = bang_bang_ns.struct("BangBangClimateTargetTempConfig")
|
BangBangClimateTargetTempConfig = bang_bang_ns.struct("BangBangClimateTargetTempConfig")
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
climate.climate_schema(BangBangClimate)
|
climate.CLIMATE_SCHEMA.extend(
|
||||||
.extend(
|
|
||||||
{
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(BangBangClimate),
|
||||||
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
|
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
|
||||||
cv.Optional(CONF_HUMIDITY_SENSOR): cv.use_id(sensor.Sensor),
|
cv.Optional(CONF_HUMIDITY_SENSOR): cv.use_id(sensor.Sensor),
|
||||||
cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE_LOW): cv.temperature,
|
cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE_LOW): cv.temperature,
|
||||||
@@ -35,15 +36,15 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
).extend(cv.COMPONENT_SCHEMA),
|
||||||
.extend(cv.COMPONENT_SCHEMA),
|
|
||||||
cv.has_at_least_one_key(CONF_COOL_ACTION, CONF_HEAT_ACTION),
|
cv.has_at_least_one_key(CONF_COOL_ACTION, CONF_HEAT_ACTION),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = await climate.new_climate(config)
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
await climate.register_climate(var, config)
|
||||||
|
|
||||||
sens = await cg.get_variable(config[CONF_SENSOR])
|
sens = await cg.get_variable(config[CONF_SENSOR])
|
||||||
cg.add(var.set_sensor(sens))
|
cg.add(var.set_sensor(sens))
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include "bedjet_hub.h"
|
#include "bedjet_hub.h"
|
||||||
#include "bedjet_child.h"
|
#include "bedjet_child.h"
|
||||||
#include "bedjet_const.h"
|
#include "bedjet_const.h"
|
||||||
#include "esphome/core/application.h"
|
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
@@ -484,11 +483,9 @@ void BedJetHub::loop() {}
|
|||||||
void BedJetHub::update() { this->dispatch_status_(); }
|
void BedJetHub::update() { this->dispatch_status_(); }
|
||||||
|
|
||||||
void BedJetHub::dump_config() {
|
void BedJetHub::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, "BedJet Hub '%s'", this->get_name().c_str());
|
||||||
"BedJet Hub '%s'\n"
|
ESP_LOGCONFIG(TAG, " ble_client.app_id: %d", this->parent()->app_id);
|
||||||
" ble_client.app_id: %d\n"
|
ESP_LOGCONFIG(TAG, " ble_client.conn_id: %d", this->parent()->get_conn_id());
|
||||||
" ble_client.conn_id: %d",
|
|
||||||
this->get_name().c_str(), this->parent()->app_id, this->parent()->get_conn_id());
|
|
||||||
LOG_UPDATE_INTERVAL(this)
|
LOG_UPDATE_INTERVAL(this)
|
||||||
ESP_LOGCONFIG(TAG, " Child components (%d):", this->children_.size());
|
ESP_LOGCONFIG(TAG, " Child components (%d):", this->children_.size());
|
||||||
for (auto *child : this->children_) {
|
for (auto *child : this->children_) {
|
||||||
@@ -529,7 +526,7 @@ void BedJetHub::dispatch_status_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->timeout_ > 0 && diff > this->timeout_ && this->parent()->enabled) {
|
if (this->timeout_ > 0 && diff > this->timeout_ && this->parent()->enabled) {
|
||||||
ESP_LOGW(TAG, "[%s] Timed out after %" PRId32 " sec. Retrying", this->get_name().c_str(), this->timeout_);
|
ESP_LOGW(TAG, "[%s] Timed out after %" PRId32 " sec. Retrying...", this->get_name().c_str(), this->timeout_);
|
||||||
// set_enabled(false) will only close the connection if state != IDLE.
|
// set_enabled(false) will only close the connection if state != IDLE.
|
||||||
this->parent()->set_state(espbt::ClientState::CONNECTING);
|
this->parent()->set_state(espbt::ClientState::CONNECTING);
|
||||||
this->parent()->set_enabled(false);
|
this->parent()->set_enabled(false);
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import ble_client, climate
|
from esphome.components import ble_client, climate
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_HEAT_MODE,
|
CONF_HEAT_MODE,
|
||||||
|
CONF_ID,
|
||||||
CONF_RECEIVE_TIMEOUT,
|
CONF_RECEIVE_TIMEOUT,
|
||||||
CONF_TEMPERATURE_SOURCE,
|
CONF_TEMPERATURE_SOURCE,
|
||||||
CONF_TIME_ID,
|
CONF_TIME_ID,
|
||||||
@@ -10,6 +13,7 @@ from esphome.const import (
|
|||||||
|
|
||||||
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
|
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
CODEOWNERS = ["@jhansche"]
|
CODEOWNERS = ["@jhansche"]
|
||||||
DEPENDENCIES = ["bedjet"]
|
DEPENDENCIES = ["bedjet"]
|
||||||
|
|
||||||
@@ -26,9 +30,9 @@ BEDJET_TEMPERATURE_SOURCES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
climate.climate_schema(BedJetClimate)
|
climate.CLIMATE_SCHEMA.extend(
|
||||||
.extend(
|
|
||||||
{
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(BedJetClimate),
|
||||||
cv.Optional(CONF_HEAT_MODE, default="heat"): cv.enum(
|
cv.Optional(CONF_HEAT_MODE, default="heat"): cv.enum(
|
||||||
BEDJET_HEAT_MODES, lower=True
|
BEDJET_HEAT_MODES, lower=True
|
||||||
),
|
),
|
||||||
@@ -59,8 +63,9 @@ CONFIG_SCHEMA = (
|
|||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = await climate.new_climate(config)
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
await climate.register_climate(var, config)
|
||||||
await register_bedjet_child(var, config)
|
await register_bedjet_child(var, config)
|
||||||
|
|
||||||
cg.add(var.set_heating_mode(config[CONF_HEAT_MODE]))
|
cg.add(var.set_heating_mode(config[CONF_HEAT_MODE]))
|
||||||
|
|||||||
@@ -1,22 +1,31 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import fan
|
from esphome.components import fan
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
|
from esphome.const import CONF_ID
|
||||||
|
|
||||||
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
|
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
CODEOWNERS = ["@jhansche"]
|
CODEOWNERS = ["@jhansche"]
|
||||||
DEPENDENCIES = ["bedjet"]
|
DEPENDENCIES = ["bedjet"]
|
||||||
|
|
||||||
BedJetFan = bedjet_ns.class_("BedJetFan", fan.Fan, cg.PollingComponent)
|
BedJetFan = bedjet_ns.class_("BedJetFan", fan.Fan, cg.PollingComponent)
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
fan.fan_schema(BedJetFan)
|
fan.FAN_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(BedJetFan),
|
||||||
|
}
|
||||||
|
)
|
||||||
.extend(cv.polling_component_schema("60s"))
|
.extend(cv.polling_component_schema("60s"))
|
||||||
.extend(BEDJET_CLIENT_SCHEMA)
|
.extend(BEDJET_CLIENT_SCHEMA)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = await fan.new_fan(config)
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
await fan.register_fan(var, config)
|
||||||
await register_bedjet_child(var, config)
|
await register_bedjet_child(var, config)
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ void spi_dma_tx_finish_callback(unsigned int param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BekenSPILEDStripLightOutput::setup() {
|
void BekenSPILEDStripLightOutput::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup");
|
ESP_LOGCONFIG(TAG, "Setting up Beken SPI LED Strip...");
|
||||||
|
|
||||||
size_t buffer_size = this->get_buffer_size_();
|
size_t buffer_size = this->get_buffer_size_();
|
||||||
size_t dma_buffer_size = (buffer_size * 8) + (2 * 64);
|
size_t dma_buffer_size = (buffer_size * 8) + (2 * 64);
|
||||||
@@ -256,7 +256,7 @@ void BekenSPILEDStripLightOutput::write_state(light::LightState *state) {
|
|||||||
this->last_refresh_ = now;
|
this->last_refresh_ = now;
|
||||||
this->mark_shown_();
|
this->mark_shown_();
|
||||||
|
|
||||||
ESP_LOGVV(TAG, "Writing RGB values to bus");
|
ESP_LOGVV(TAG, "Writing RGB values to bus...");
|
||||||
|
|
||||||
if (spi_data == nullptr) {
|
if (spi_data == nullptr) {
|
||||||
ESP_LOGE(TAG, "SPI not initialized");
|
ESP_LOGE(TAG, "SPI not initialized");
|
||||||
@@ -345,10 +345,8 @@ light::ESPColorView BekenSPILEDStripLightOutput::get_view_internal(int32_t index
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BekenSPILEDStripLightOutput::dump_config() {
|
void BekenSPILEDStripLightOutput::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, "Beken SPI LED Strip:");
|
||||||
"Beken SPI LED Strip:\n"
|
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
|
||||||
" Pin: %u",
|
|
||||||
this->pin_);
|
|
||||||
const char *rgb_order;
|
const char *rgb_order;
|
||||||
switch (this->rgb_order_) {
|
switch (this->rgb_order_) {
|
||||||
case ORDER_RGB:
|
case ORDER_RGB:
|
||||||
@@ -373,11 +371,9 @@ void BekenSPILEDStripLightOutput::dump_config() {
|
|||||||
rgb_order = "UNKNOWN";
|
rgb_order = "UNKNOWN";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG, " RGB Order: %s", rgb_order);
|
||||||
" RGB Order: %s\n"
|
ESP_LOGCONFIG(TAG, " Max refresh rate: %" PRIu32, *this->max_refresh_rate_);
|
||||||
" Max refresh rate: %" PRIu32 "\n"
|
ESP_LOGCONFIG(TAG, " Number of LEDs: %u", this->num_leds_);
|
||||||
" Number of LEDs: %u",
|
|
||||||
rgb_order, *this->max_refresh_rate_, this->num_leds_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float BekenSPILEDStripLightOutput::get_setup_priority() const { return setup_priority::HARDWARE; }
|
float BekenSPILEDStripLightOutput::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ MTreg:
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
void BH1750Sensor::setup() {
|
void BH1750Sensor::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->name_.c_str());
|
ESP_LOGCONFIG(TAG, "Setting up BH1750 '%s'...", this->name_.c_str());
|
||||||
uint8_t turn_on = BH1750_COMMAND_POWER_ON;
|
uint8_t turn_on = BH1750_COMMAND_POWER_ON;
|
||||||
if (this->write(&turn_on, 1) != i2c::ERROR_OK) {
|
if (this->write(&turn_on, 1) != i2c::ERROR_OK) {
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
@@ -118,7 +118,7 @@ void BH1750Sensor::dump_config() {
|
|||||||
LOG_SENSOR("", "BH1750", this);
|
LOG_SENSOR("", "BH1750", this);
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
if (this->is_failed()) {
|
if (this->is_failed()) {
|
||||||
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL_FOR, this->get_name().c_str());
|
ESP_LOGE(TAG, "Communication with BH1750 failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
|
|||||||
@@ -1,28 +1,31 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import fan, output
|
from esphome.components import fan, output
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_DIRECTION_OUTPUT, CONF_OSCILLATION_OUTPUT, CONF_OUTPUT
|
from esphome.const import (
|
||||||
|
CONF_DIRECTION_OUTPUT,
|
||||||
|
CONF_OSCILLATION_OUTPUT,
|
||||||
|
CONF_OUTPUT,
|
||||||
|
CONF_OUTPUT_ID,
|
||||||
|
)
|
||||||
|
|
||||||
from .. import binary_ns
|
from .. import binary_ns
|
||||||
|
|
||||||
BinaryFan = binary_ns.class_("BinaryFan", fan.Fan, cg.Component)
|
BinaryFan = binary_ns.class_("BinaryFan", fan.Fan, cg.Component)
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
|
||||||
fan.fan_schema(BinaryFan)
|
{
|
||||||
.extend(
|
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(BinaryFan),
|
||||||
{
|
cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||||
cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput),
|
cv.Optional(CONF_DIRECTION_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||||
cv.Optional(CONF_DIRECTION_OUTPUT): cv.use_id(output.BinaryOutput),
|
cv.Optional(CONF_OSCILLATION_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||||
cv.Optional(CONF_OSCILLATION_OUTPUT): cv.use_id(output.BinaryOutput),
|
}
|
||||||
}
|
).extend(cv.COMPONENT_SCHEMA)
|
||||||
)
|
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = await fan.new_fan(config)
|
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
await fan.register_fan(var, config)
|
||||||
|
|
||||||
output_ = await cg.get_variable(config[CONF_OUTPUT])
|
output_ = await cg.get_variable(config[CONF_OUTPUT])
|
||||||
cg.add(var.set_output(output_))
|
cg.add(var.set_output(output_))
|
||||||
|
|||||||
@@ -386,7 +386,7 @@ def validate_click_timing(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
_BINARY_SENSOR_SCHEMA = (
|
BINARY_SENSOR_SCHEMA = (
|
||||||
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
|
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
|
||||||
.extend(cv.MQTT_COMPONENT_SCHEMA)
|
.extend(cv.MQTT_COMPONENT_SCHEMA)
|
||||||
.extend(
|
.extend(
|
||||||
@@ -458,17 +458,19 @@ _BINARY_SENSOR_SCHEMA = (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_UNDEF = object()
|
||||||
|
|
||||||
|
|
||||||
def binary_sensor_schema(
|
def binary_sensor_schema(
|
||||||
class_: MockObjClass = cv.UNDEFINED,
|
class_: MockObjClass = _UNDEF,
|
||||||
*,
|
*,
|
||||||
icon: str = cv.UNDEFINED,
|
icon: str = _UNDEF,
|
||||||
entity_category: str = cv.UNDEFINED,
|
entity_category: str = _UNDEF,
|
||||||
device_class: str = cv.UNDEFINED,
|
device_class: str = _UNDEF,
|
||||||
) -> cv.Schema:
|
) -> cv.Schema:
|
||||||
schema = {}
|
schema = {}
|
||||||
|
|
||||||
if class_ is not cv.UNDEFINED:
|
if class_ is not _UNDEF:
|
||||||
# Not cv.optional
|
# Not cv.optional
|
||||||
schema[cv.GenerateID()] = cv.declare_id(class_)
|
schema[cv.GenerateID()] = cv.declare_id(class_)
|
||||||
|
|
||||||
@@ -477,15 +479,10 @@ def binary_sensor_schema(
|
|||||||
(CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
|
(CONF_ENTITY_CATEGORY, entity_category, cv.entity_category),
|
||||||
(CONF_DEVICE_CLASS, device_class, validate_device_class),
|
(CONF_DEVICE_CLASS, device_class, validate_device_class),
|
||||||
]:
|
]:
|
||||||
if default is not cv.UNDEFINED:
|
if default is not _UNDEF:
|
||||||
schema[cv.Optional(key, default=default)] = validator
|
schema[cv.Optional(key, default=default)] = validator
|
||||||
|
|
||||||
return _BINARY_SENSOR_SCHEMA.extend(schema)
|
return BINARY_SENSOR_SCHEMA.extend(schema)
|
||||||
|
|
||||||
|
|
||||||
# Remove before 2025.11.0
|
|
||||||
BINARY_SENSOR_SCHEMA = binary_sensor_schema()
|
|
||||||
BINARY_SENSOR_SCHEMA.add_extra(cv.deprecated_schema_constant("binary_sensor"))
|
|
||||||
|
|
||||||
|
|
||||||
async def setup_binary_sensor_core_(var, config):
|
async def setup_binary_sensor_core_(var, config):
|
||||||
@@ -554,7 +551,6 @@ async def register_binary_sensor(var, config):
|
|||||||
if not CORE.has_id(config[CONF_ID]):
|
if not CORE.has_id(config[CONF_ID]):
|
||||||
var = cg.Pvariable(config[CONF_ID], var)
|
var = cg.Pvariable(config[CONF_ID], var)
|
||||||
cg.add(cg.App.register_binary_sensor(var))
|
cg.add(cg.App.register_binary_sensor(var))
|
||||||
CORE.register_platform_component("binary_sensor", var)
|
|
||||||
await setup_binary_sensor_core_(var, config)
|
await setup_binary_sensor_core_(var, config)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,8 @@ void binary_sensor::MultiClickTrigger::on_state_(bool state) {
|
|||||||
*this->at_index_ = *this->at_index_ + 1;
|
*this->at_index_ = *this->at_index_ + 1;
|
||||||
}
|
}
|
||||||
void binary_sensor::MultiClickTrigger::schedule_cooldown_() {
|
void binary_sensor::MultiClickTrigger::schedule_cooldown_() {
|
||||||
ESP_LOGV(TAG, "Multi Click: Invalid length of press, starting cooldown of %" PRIu32 " ms", this->invalid_cooldown_);
|
ESP_LOGV(TAG, "Multi Click: Invalid length of press, starting cooldown of %" PRIu32 " ms...",
|
||||||
|
this->invalid_cooldown_);
|
||||||
this->is_in_cooldown_ = true;
|
this->is_in_cooldown_ = true;
|
||||||
this->set_timeout("cooldown", this->invalid_cooldown_, [this]() {
|
this->set_timeout("cooldown", this->invalid_cooldown_, [this]() {
|
||||||
ESP_LOGV(TAG, "Multi Click: Cooldown ended, matching is now enabled again.");
|
ESP_LOGV(TAG, "Multi Click: Cooldown ended, matching is now enabled again.");
|
||||||
|
|||||||
@@ -15,21 +15,17 @@ void BinarySensor::publish_state(bool state) {
|
|||||||
if (!this->publish_dedup_.next(state))
|
if (!this->publish_dedup_.next(state))
|
||||||
return;
|
return;
|
||||||
if (this->filter_list_ == nullptr) {
|
if (this->filter_list_ == nullptr) {
|
||||||
this->send_state_internal(state, false);
|
this->send_state_internal(state);
|
||||||
} else {
|
} else {
|
||||||
this->filter_list_->input(state, false);
|
this->filter_list_->input(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void BinarySensor::publish_initial_state(bool state) {
|
void BinarySensor::publish_initial_state(bool state) {
|
||||||
if (!this->publish_dedup_.next(state))
|
this->has_state_ = false;
|
||||||
return;
|
this->publish_state(state);
|
||||||
if (this->filter_list_ == nullptr) {
|
|
||||||
this->send_state_internal(state, true);
|
|
||||||
} else {
|
|
||||||
this->filter_list_->input(state, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void BinarySensor::send_state_internal(bool state, bool is_initial) {
|
void BinarySensor::send_state_internal(bool state) {
|
||||||
|
bool is_initial = !this->has_state_;
|
||||||
if (is_initial) {
|
if (is_initial) {
|
||||||
ESP_LOGD(TAG, "'%s': Sending initial state %s", this->get_name().c_str(), ONOFF(state));
|
ESP_LOGD(TAG, "'%s': Sending initial state %s", this->get_name().c_str(), ONOFF(state));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class BinarySensor : public EntityBase, public EntityBase_DeviceClass {
|
|||||||
|
|
||||||
// ========== INTERNAL METHODS ==========
|
// ========== INTERNAL METHODS ==========
|
||||||
// (In most use cases you won't need these)
|
// (In most use cases you won't need these)
|
||||||
void send_state_internal(bool state, bool is_initial);
|
void send_state_internal(bool state);
|
||||||
|
|
||||||
/// Return whether this binary sensor has outputted a state.
|
/// Return whether this binary sensor has outputted a state.
|
||||||
virtual bool has_state() const;
|
virtual bool has_state() const;
|
||||||
|
|||||||
@@ -9,37 +9,37 @@ namespace binary_sensor {
|
|||||||
|
|
||||||
static const char *const TAG = "sensor.filter";
|
static const char *const TAG = "sensor.filter";
|
||||||
|
|
||||||
void Filter::output(bool value, bool is_initial) {
|
void Filter::output(bool value) {
|
||||||
if (!this->dedup_.next(value))
|
if (!this->dedup_.next(value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (this->next_ == nullptr) {
|
if (this->next_ == nullptr) {
|
||||||
this->parent_->send_state_internal(value, is_initial);
|
this->parent_->send_state_internal(value);
|
||||||
} else {
|
} else {
|
||||||
this->next_->input(value, is_initial);
|
this->next_->input(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Filter::input(bool value, bool is_initial) {
|
void Filter::input(bool value) {
|
||||||
auto b = this->new_value(value, is_initial);
|
auto b = this->new_value(value);
|
||||||
if (b.has_value()) {
|
if (b.has_value()) {
|
||||||
this->output(*b, is_initial);
|
this->output(*b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
optional<bool> DelayedOnOffFilter::new_value(bool value, bool is_initial) {
|
optional<bool> DelayedOnOffFilter::new_value(bool value) {
|
||||||
if (value) {
|
if (value) {
|
||||||
this->set_timeout("ON_OFF", this->on_delay_.value(), [this, is_initial]() { this->output(true, is_initial); });
|
this->set_timeout("ON_OFF", this->on_delay_.value(), [this]() { this->output(true); });
|
||||||
} else {
|
} else {
|
||||||
this->set_timeout("ON_OFF", this->off_delay_.value(), [this, is_initial]() { this->output(false, is_initial); });
|
this->set_timeout("ON_OFF", this->off_delay_.value(), [this]() { this->output(false); });
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
float DelayedOnOffFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
float DelayedOnOffFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
optional<bool> DelayedOnFilter::new_value(bool value, bool is_initial) {
|
optional<bool> DelayedOnFilter::new_value(bool value) {
|
||||||
if (value) {
|
if (value) {
|
||||||
this->set_timeout("ON", this->delay_.value(), [this, is_initial]() { this->output(true, is_initial); });
|
this->set_timeout("ON", this->delay_.value(), [this]() { this->output(true); });
|
||||||
return {};
|
return {};
|
||||||
} else {
|
} else {
|
||||||
this->cancel_timeout("ON");
|
this->cancel_timeout("ON");
|
||||||
@@ -49,9 +49,9 @@ optional<bool> DelayedOnFilter::new_value(bool value, bool is_initial) {
|
|||||||
|
|
||||||
float DelayedOnFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
float DelayedOnFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
optional<bool> DelayedOffFilter::new_value(bool value, bool is_initial) {
|
optional<bool> DelayedOffFilter::new_value(bool value) {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
this->set_timeout("OFF", this->delay_.value(), [this, is_initial]() { this->output(false, is_initial); });
|
this->set_timeout("OFF", this->delay_.value(), [this]() { this->output(false); });
|
||||||
return {};
|
return {};
|
||||||
} else {
|
} else {
|
||||||
this->cancel_timeout("OFF");
|
this->cancel_timeout("OFF");
|
||||||
@@ -61,11 +61,11 @@ optional<bool> DelayedOffFilter::new_value(bool value, bool is_initial) {
|
|||||||
|
|
||||||
float DelayedOffFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
float DelayedOffFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
optional<bool> InvertFilter::new_value(bool value, bool is_initial) { return !value; }
|
optional<bool> InvertFilter::new_value(bool value) { return !value; }
|
||||||
|
|
||||||
AutorepeatFilter::AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings) : timings_(std::move(timings)) {}
|
AutorepeatFilter::AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings) : timings_(std::move(timings)) {}
|
||||||
|
|
||||||
optional<bool> AutorepeatFilter::new_value(bool value, bool is_initial) {
|
optional<bool> AutorepeatFilter::new_value(bool value) {
|
||||||
if (value) {
|
if (value) {
|
||||||
// Ignore if already running
|
// Ignore if already running
|
||||||
if (this->active_timing_ != 0)
|
if (this->active_timing_ != 0)
|
||||||
@@ -101,7 +101,7 @@ void AutorepeatFilter::next_timing_() {
|
|||||||
|
|
||||||
void AutorepeatFilter::next_value_(bool val) {
|
void AutorepeatFilter::next_value_(bool val) {
|
||||||
const AutorepeatFilterTiming &timing = this->timings_[this->active_timing_ - 2];
|
const AutorepeatFilterTiming &timing = this->timings_[this->active_timing_ - 2];
|
||||||
this->output(val, false); // This is at least the second one so not initial
|
this->output(val);
|
||||||
this->set_timeout("ON_OFF", val ? timing.time_on : timing.time_off, [this, val]() { this->next_value_(!val); });
|
this->set_timeout("ON_OFF", val ? timing.time_on : timing.time_off, [this, val]() { this->next_value_(!val); });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,18 +109,18 @@ float AutorepeatFilter::get_setup_priority() const { return setup_priority::HARD
|
|||||||
|
|
||||||
LambdaFilter::LambdaFilter(std::function<optional<bool>(bool)> f) : f_(std::move(f)) {}
|
LambdaFilter::LambdaFilter(std::function<optional<bool>(bool)> f) : f_(std::move(f)) {}
|
||||||
|
|
||||||
optional<bool> LambdaFilter::new_value(bool value, bool is_initial) { return this->f_(value); }
|
optional<bool> LambdaFilter::new_value(bool value) { return this->f_(value); }
|
||||||
|
|
||||||
optional<bool> SettleFilter::new_value(bool value, bool is_initial) {
|
optional<bool> SettleFilter::new_value(bool value) {
|
||||||
if (!this->steady_) {
|
if (!this->steady_) {
|
||||||
this->set_timeout("SETTLE", this->delay_.value(), [this, value, is_initial]() {
|
this->set_timeout("SETTLE", this->delay_.value(), [this, value]() {
|
||||||
this->steady_ = true;
|
this->steady_ = true;
|
||||||
this->output(value, is_initial);
|
this->output(value);
|
||||||
});
|
});
|
||||||
return {};
|
return {};
|
||||||
} else {
|
} else {
|
||||||
this->steady_ = false;
|
this->steady_ = false;
|
||||||
this->output(value, is_initial);
|
this->output(value);
|
||||||
this->set_timeout("SETTLE", this->delay_.value(), [this]() { this->steady_ = true; });
|
this->set_timeout("SETTLE", this->delay_.value(), [this]() { this->steady_ = true; });
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user