Compare commits
221 Commits
s2-c3-lint
...
shutdown/b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ae8600883 | ||
|
|
048ce79235 | ||
|
|
1fb0a7109d | ||
|
|
192eb49589 | ||
|
|
5d70ff702b | ||
|
|
a7b05db2a1 | ||
|
|
45e346cf1b | ||
|
|
80e2bfada3 | ||
|
|
16e7bd0388 | ||
|
|
b3fb35783e | ||
|
|
a79c6aa9e0 | ||
|
|
4bb58b2de9 | ||
|
|
4e10881331 | ||
|
|
cec4a81e14 | ||
|
|
da45923d05 | ||
|
|
31a61b598b | ||
|
|
9c0506592b | ||
|
|
beeb0c7c5a | ||
|
|
b2f05faee0 | ||
|
|
8375e1d64d | ||
|
|
cf5193d3e5 | ||
|
|
c490388e80 | ||
|
|
24ec5a6e9d | ||
|
|
6df1d5222d | ||
|
|
58fb7a02f6 | ||
|
|
3d51ac8df0 | ||
|
|
6fe4ff7f85 | ||
|
|
2253d4bc16 | ||
|
|
e5cc19de43 | ||
|
|
5404617d43 | ||
|
|
12467a18e6 | ||
|
|
1db7043a4d | ||
|
|
49932747b3 | ||
|
|
55db190875 | ||
|
|
71fe2f7ed3 | ||
|
|
ffc112c9d0 | ||
|
|
d3e48e296f | ||
|
|
14f6ae75ea | ||
|
|
c84efe64d3 | ||
|
|
10e89a7dbb | ||
|
|
ef44acbf10 | ||
|
|
06da540ab0 | ||
|
|
40c017fd54 | ||
|
|
f0bcf81a98 | ||
|
|
6a0b343289 | ||
|
|
9ca4e8f32a | ||
|
|
1b88b7a166 | ||
|
|
caf352ff06 | ||
|
|
54106179a1 | ||
|
|
607601b3a4 | ||
|
|
f58828cb82 | ||
|
|
11330af05f | ||
|
|
fbe1bca1b9 | ||
|
|
24a5325db3 | ||
|
|
1ec3140759 | ||
|
|
ca8db7696e | ||
|
|
c9190574a9 | ||
|
|
bfeb0b3639 | ||
|
|
cbc1334b8d | ||
|
|
08cbb97ec9 | ||
|
|
5719cc1a24 | ||
|
|
d9513e5ff2 | ||
|
|
b5a0e8b2c0 | ||
|
|
b32b918936 | ||
|
|
0f47ffd908 | ||
|
|
cd018ad3a5 | ||
|
|
24dfecb6f0 | ||
|
|
ab027a6ae2 | ||
|
|
556d071e7f | ||
|
|
939fb313df | ||
|
|
b5639a6472 | ||
|
|
f50e40e0b8 | ||
|
|
6f07421911 | ||
|
|
adf48246a9 | ||
|
|
cae283dc86 | ||
|
|
7afcb0fb04 | ||
|
|
10f830c3ef | ||
|
|
7a5c3aa7ed | ||
|
|
2b50406856 | ||
|
|
10a2a7e0fc | ||
|
|
7a564b222d | ||
|
|
671d68bc2c | ||
|
|
5946c37925 | ||
|
|
17a37b1de9 | ||
|
|
e7827a6997 | ||
|
|
2347e043a9 | ||
|
|
00965fe19e | ||
|
|
9681dfb458 | ||
|
|
5e631bc6ba | ||
|
|
b5f660398c | ||
|
|
d50bdf619f | ||
|
|
4e448b21ff | ||
|
|
2a78c2970d | ||
|
|
3637be251e | ||
|
|
2aea27d272 | ||
|
|
ceb9b1d1ff | ||
|
|
ccfa1e23f0 | ||
|
|
290da8df2d | ||
|
|
4b1d73791d | ||
|
|
7e8012c1a0 | ||
|
|
15cd602e8b | ||
|
|
598f5b241f | ||
|
|
335e69e6cd | ||
|
|
05fe5db030 | ||
|
|
710096b1c6 | ||
|
|
07b882c801 | ||
|
|
3e5331a263 | ||
|
|
897277992b | ||
|
|
1424091ee5 | ||
|
|
61ec16cdfc | ||
|
|
e5cb5756aa | ||
|
|
9e1c3e8f01 | ||
|
|
448e1690aa | ||
|
|
8267f01ccd | ||
|
|
6f9439e1bc | ||
|
|
06994c0dfc | ||
|
|
dee5d639e2 | ||
|
|
df6730be55 | ||
|
|
6c1ef398bb | ||
|
|
0469e19f54 | ||
|
|
dbcfa7b599 | ||
|
|
df68403b6d | ||
|
|
57bdc2b885 | ||
|
|
f565ff5def | ||
|
|
8ece639987 | ||
|
|
b35f509784 | ||
|
|
f1954df573 | ||
|
|
9e4fa5dcf1 | ||
|
|
0809673ba9 | ||
|
|
b386284180 | ||
|
|
515519bc87 | ||
|
|
5404163be0 | ||
|
|
0b193eee43 | ||
|
|
7333123ba4 | ||
|
|
d99c5ed890 | ||
|
|
04740fbcbb | ||
|
|
6a7440f7d3 | ||
|
|
14299bb2cc | ||
|
|
66cebfc992 | ||
|
|
108b8e6705 | ||
|
|
4eaa6afa4d | ||
|
|
f643a46bbf | ||
|
|
aae63a7ff3 | ||
|
|
582567696e | ||
|
|
2e0c89409d | ||
|
|
7bb7456a8b | ||
|
|
0372e12b81 | ||
|
|
a6873c1520 | ||
|
|
f11220da3a | ||
|
|
bb9793d5b7 | ||
|
|
e99af991ec | ||
|
|
abf3708cc2 | ||
|
|
4395d6156d | ||
|
|
04ba53c870 | ||
|
|
f310cacd41 | ||
|
|
5ff7c8418c | ||
|
|
0bdb48bcac | ||
|
|
99c775d8cb | ||
|
|
4d43396835 | ||
|
|
92321e219a | ||
|
|
c422b2fb0b | ||
|
|
8aa72f4c1e | ||
|
|
d8e33c5a69 | ||
|
|
15f9677d33 | ||
|
|
219b225ac0 | ||
|
|
2ac232e634 | ||
|
|
710866ff4e | ||
|
|
662773b075 | ||
|
|
875b803483 | ||
|
|
6e5cfac927 | ||
|
|
97eaf3d4a1 | ||
|
|
366552a969 | ||
|
|
57b07441a1 | ||
|
|
fb57ab0add | ||
|
|
d6717c0032 | ||
|
|
f72389147d | ||
|
|
a509f6ccd2 | ||
|
|
add484a2ea | ||
|
|
a17a6d5346 | ||
|
|
be9439f10d | ||
|
|
96a50f5c6b | ||
|
|
3c0414c420 | ||
|
|
b450d4c734 | ||
|
|
d536509a63 | ||
|
|
11f1e28139 | ||
|
|
379c3e98f5 | ||
|
|
5ea77894b7 | ||
|
|
d54b4e7c44 | ||
|
|
d8b3af3815 | ||
|
|
2b04152482 | ||
|
|
331a3ac387 | ||
|
|
7eee3cdc7f | ||
|
|
b12c7432e0 | ||
|
|
696643d037 | ||
|
|
7e54f97003 | ||
|
|
77dbf84e55 | ||
|
|
2350c5054c | ||
|
|
73accf747f | ||
|
|
0d3e6b2c4c | ||
|
|
b3d7cc637b | ||
|
|
2147bcbc29 | ||
|
|
980c2d4cae | ||
|
|
68316cbcf9 | ||
|
|
2f4b9263c3 | ||
|
|
c2623a08e3 | ||
|
|
9f625ee7d1 | ||
|
|
2f85c27a05 | ||
|
|
c612a3bf60 | ||
|
|
a01f5f5cf1 | ||
|
|
87328686a0 | ||
|
|
81c11ba1f7 | ||
|
|
49b17c5a2d | ||
|
|
de06a781ff | ||
|
|
8e77e3c685 | ||
|
|
a687b083ae | ||
|
|
b9e5c7eb35 | ||
|
|
1a6a063e04 | ||
|
|
d85b7a6bd0 | ||
|
|
1c4700f447 | ||
|
|
83400d0417 | ||
|
|
77a6461c9d |
15
.clang-tidy
15
.clang-tidy
@@ -5,11 +5,8 @@ Checks: >-
|
||||
-altera-*,
|
||||
-android-*,
|
||||
-boost-*,
|
||||
-bugprone-branch-clone,
|
||||
-bugprone-easily-swappable-parameters,
|
||||
-bugprone-narrowing-conversions,
|
||||
-bugprone-signed-char-misuse,
|
||||
-bugprone-too-small-loop-variable,
|
||||
-cert-dcl50-cpp,
|
||||
-cert-err58-cpp,
|
||||
-cert-oop57-cpp,
|
||||
@@ -19,12 +16,10 @@ Checks: >-
|
||||
-clang-diagnostic-delete-abstract-non-virtual-dtor,
|
||||
-clang-diagnostic-delete-non-abstract-non-virtual-dtor,
|
||||
-clang-diagnostic-shadow-field,
|
||||
-clang-diagnostic-sign-compare,
|
||||
-clang-diagnostic-unused-variable,
|
||||
-clang-diagnostic-unused-const-variable,
|
||||
-clang-diagnostic-unused-parameter,
|
||||
-concurrency-*,
|
||||
-cppcoreguidelines-avoid-c-arrays,
|
||||
-cppcoreguidelines-avoid-goto,
|
||||
-cppcoreguidelines-avoid-magic-numbers,
|
||||
-cppcoreguidelines-init-variables,
|
||||
-cppcoreguidelines-macro-usage,
|
||||
@@ -41,7 +36,6 @@ Checks: >-
|
||||
-cppcoreguidelines-pro-type-union-access,
|
||||
-cppcoreguidelines-pro-type-vararg,
|
||||
-cppcoreguidelines-special-member-functions,
|
||||
-fuchsia-default-arguments,
|
||||
-fuchsia-multiple-inheritance,
|
||||
-fuchsia-overloaded-operator,
|
||||
-fuchsia-statically-constructed-objects,
|
||||
@@ -51,6 +45,7 @@ Checks: >-
|
||||
-google-explicit-constructor,
|
||||
-google-readability-braces-around-statements,
|
||||
-google-readability-casting,
|
||||
-google-readability-namespace-comments,
|
||||
-google-readability-todo,
|
||||
-google-runtime-references,
|
||||
-hicpp-*,
|
||||
@@ -97,9 +92,11 @@ CheckOptions:
|
||||
value: '1'
|
||||
- key: google-readability-function-size.StatementThreshold
|
||||
value: '800'
|
||||
- key: google-readability-namespace-comments.ShortNamespaceLines
|
||||
- key: google-runtime-int.TypeSuffix
|
||||
value: '_t'
|
||||
- key: llvm-namespace-comment.ShortNamespaceLines
|
||||
value: '10'
|
||||
- key: google-readability-namespace-comments.SpacesBeforeComments
|
||||
- key: llvm-namespace-comment.SpacesBeforeComments
|
||||
value: '2'
|
||||
- key: modernize-loop-convert.MaxCopySize
|
||||
value: '16'
|
||||
|
||||
7
.github/issue-close-app.yml
vendored
7
.github/issue-close-app.yml
vendored
@@ -1,7 +0,0 @@
|
||||
comment: >-
|
||||
https://github.com/esphome/esphome/issues/430
|
||||
issueConfigs:
|
||||
- content:
|
||||
- "OTHERWISE THE ISSUE WILL BE CLOSED AUTOMATICALLY"
|
||||
|
||||
caseInsensitive: false
|
||||
36
.github/lock.yml
vendored
36
.github/lock.yml
vendored
@@ -1,36 +0,0 @@
|
||||
# Configuration for Lock Threads - https://github.com/dessant/lock-threads
|
||||
|
||||
# Number of days of inactivity before a closed issue or pull request is locked
|
||||
daysUntilLock: 7
|
||||
|
||||
# Skip issues and pull requests created before a given timestamp. Timestamp must
|
||||
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
|
||||
skipCreatedBefore: false
|
||||
|
||||
# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
|
||||
exemptLabels:
|
||||
- keep-open
|
||||
|
||||
# Label to add before locking, such as `outdated`. Set to `false` to disable
|
||||
lockLabel: false
|
||||
|
||||
# Comment to post before locking. Set to `false` to disable
|
||||
lockComment: false
|
||||
|
||||
# Assign `resolved` as the reason for locking. Set to `false` to disable
|
||||
setLockReason: false
|
||||
|
||||
# Limit to only `issues` or `pulls`
|
||||
# only: issues
|
||||
|
||||
# Optionally, specify configuration settings just for `issues` or `pulls`
|
||||
# issues:
|
||||
# exemptLabels:
|
||||
# - help-wanted
|
||||
# lockLabel: outdated
|
||||
|
||||
# pulls:
|
||||
# daysUntilLock: 30
|
||||
|
||||
# Repository to extend settings from
|
||||
# _extends: repo
|
||||
4
.github/workflows/ci-docker.yml
vendored
4
.github/workflows/ci-docker.yml
vendored
@@ -17,6 +17,10 @@ on:
|
||||
- 'requirements*.txt'
|
||||
- 'platformio.ini'
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
|
||||
jobs:
|
||||
check-docker:
|
||||
name: Build docker containers
|
||||
|
||||
30
.github/workflows/ci.yml
vendored
30
.github/workflows/ci.yml
vendored
@@ -1,5 +1,3 @@
|
||||
# THESE JOBS ARE COPIED IN release.yml and release-dev.yml
|
||||
# PLEASE ALSO UPDATE THOSE FILES WHEN CHANGING LINES HERE
|
||||
name: CI
|
||||
|
||||
on:
|
||||
@@ -8,6 +6,13 @@ on:
|
||||
|
||||
pull_request:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
name: ${{ matrix.name }}
|
||||
@@ -31,7 +36,7 @@ jobs:
|
||||
- id: test
|
||||
file: tests/test3.yaml
|
||||
name: Test tests/test3.yaml
|
||||
pio_cache_key: test1
|
||||
pio_cache_key: test3
|
||||
- id: test
|
||||
file: tests/test4.yaml
|
||||
name: Test tests/test4.yaml
|
||||
@@ -77,18 +82,23 @@ jobs:
|
||||
with:
|
||||
python-version: '3.7'
|
||||
|
||||
- name: Cache pip modules
|
||||
- name: Cache virtualenv
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.cache/pip
|
||||
key: pip-${{ steps.python.outputs.python-version }}-${{ hashFiles('requirements*.txt') }}
|
||||
path: .venv
|
||||
key: venv-${{ steps.python.outputs.python-version }}-${{ hashFiles('requirements*.txt') }}
|
||||
restore-keys: |
|
||||
pip-${{ steps.python.outputs.python-version }}-
|
||||
venv-${{ steps.python.outputs.python-version }}-
|
||||
|
||||
- name: Set up python environment
|
||||
- name: Set up virtualenv
|
||||
run: |
|
||||
pip3 install -r requirements.txt -r requirements_optional.txt -r requirements_test.txt
|
||||
pip3 install -e .
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -U pip
|
||||
pip install -r requirements.txt -r requirements_optional.txt -r requirements_test.txt
|
||||
pip install -e .
|
||||
echo "$GITHUB_WORKSPACE/.venv/bin" >> $GITHUB_PATH
|
||||
echo "VIRTUAL_ENV=$GITHUB_WORKSPACE/.venv" >> $GITHUB_ENV
|
||||
|
||||
# Use per check platformio cache because checks use different parts
|
||||
- name: Cache platformio
|
||||
|
||||
14
.github/workflows/lock.yml
vendored
14
.github/workflows/lock.yml
vendored
@@ -9,13 +9,19 @@ permissions:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
concurrency:
|
||||
group: lock
|
||||
|
||||
jobs:
|
||||
lock:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: dessant/lock-threads@v2
|
||||
- uses: dessant/lock-threads@v3
|
||||
with:
|
||||
github-token: ${{ github.token }}
|
||||
pr-lock-inactive-days: "1"
|
||||
pr-inactive-days: "1"
|
||||
pr-lock-reason: ""
|
||||
process-only: prs
|
||||
exclude-any-pr-labels: keep-open
|
||||
|
||||
issue-inactive-days: "7"
|
||||
issue-lock-reason: ""
|
||||
exclude-any-issue-labels: keep-open
|
||||
|
||||
2
.github/workflows/matchers/ci-custom.json
vendored
2
.github/workflows/matchers/ci-custom.json
vendored
@@ -4,7 +4,7 @@
|
||||
"owner": "ci-custom",
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": "^ERROR (.*):(\\d+):(\\d+) - (.*)$",
|
||||
"regexp": "^(.*):(\\d+):(\\d+):\\s+lint:\\s+(.*)$",
|
||||
"file": 1,
|
||||
"line": 2,
|
||||
"column": 3,
|
||||
|
||||
2
.github/workflows/matchers/gcc.json
vendored
2
.github/workflows/matchers/gcc.json
vendored
@@ -5,7 +5,7 @@
|
||||
"severity": "error",
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": "^(.*):(\\d+):(\\d+):\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$",
|
||||
"regexp": "^src/(.*):(\\d+):(\\d+):\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$",
|
||||
"file": 1,
|
||||
"line": 2,
|
||||
"column": 3,
|
||||
|
||||
15
.github/workflows/matchers/lint-python.json
vendored
15
.github/workflows/matchers/lint-python.json
vendored
@@ -1,11 +1,22 @@
|
||||
{
|
||||
"problemMatcher": [
|
||||
{
|
||||
"owner": "black",
|
||||
"severity": "error",
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": "^(.*): (Please format this file with the black formatter)",
|
||||
"file": 1,
|
||||
"message": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"owner": "flake8",
|
||||
"severity": "error",
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": "^(.*):(\\d+) - ([EFCDNW]\\d{3}.*)$",
|
||||
"regexp": "^(.*):(\\d+): ([EFCDNW]\\d{3}.*)$",
|
||||
"file": 1,
|
||||
"line": 2,
|
||||
"message": 3
|
||||
@@ -17,7 +28,7 @@
|
||||
"severity": "error",
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": "^(.*):(\\d+) - (\\[[EFCRW]\\d{4}\\(.*\\),.*\\].*)$",
|
||||
"regexp": "^(.*):(\\d+): (\\[[EFCRW]\\d{4}\\(.*\\),.*\\].*)$",
|
||||
"file": 1,
|
||||
"line": 2,
|
||||
"message": 3
|
||||
|
||||
9
.github/workflows/release.yml
vendored
9
.github/workflows/release.yml
vendored
@@ -7,6 +7,9 @@ on:
|
||||
schedule:
|
||||
- cron: "0 2 * * *"
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
init:
|
||||
name: Initialize build
|
||||
@@ -52,6 +55,9 @@ jobs:
|
||||
deploy-docker:
|
||||
name: Build and publish docker containers
|
||||
if: github.repository == 'esphome/esphome'
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
runs-on: ubuntu-latest
|
||||
needs: [init]
|
||||
strategy:
|
||||
@@ -93,6 +99,9 @@ jobs:
|
||||
|
||||
deploy-docker-manifest:
|
||||
if: github.repository == 'esphome/esphome'
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
runs-on: ubuntu-latest
|
||||
needs: [init, deploy-docker]
|
||||
strategy:
|
||||
|
||||
20
.github/workflows/stale.yml
vendored
20
.github/workflows/stale.yml
vendored
@@ -9,13 +9,15 @@ permissions:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
concurrency:
|
||||
group: lock
|
||||
|
||||
jobs:
|
||||
stale:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v4
|
||||
with:
|
||||
repo-token: ${{ github.token }}
|
||||
days-before-pr-stale: 90
|
||||
days-before-pr-close: 7
|
||||
days-before-issue-stale: -1
|
||||
@@ -28,3 +30,19 @@ jobs:
|
||||
pull request has been automatically marked as stale because of that
|
||||
and will be closed if no further activity occurs within 7 days.
|
||||
Thank you for your contributions.
|
||||
|
||||
# Use stale to automatically close issues with a reference to the issue tracker
|
||||
close-issues:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v4
|
||||
with:
|
||||
days-before-pr-stale: -1
|
||||
days-before-pr-close: -1
|
||||
days-before-issue-stale: 1
|
||||
days-before-issue-close: 1
|
||||
remove-stale-when-updated: true
|
||||
stale-issue-label: "stale"
|
||||
exempt-issue-labels: "not-stale"
|
||||
stale-issue-message: >
|
||||
https://github.com/esphome/esphome/issues/430
|
||||
|
||||
@@ -30,7 +30,9 @@ esphome/components/bang_bang/* @OttoWinter
|
||||
esphome/components/binary_sensor/* @esphome/core
|
||||
esphome/components/ble_client/* @buxtronix
|
||||
esphome/components/bme680_bsec/* @trvrnrth
|
||||
esphome/components/button/* @esphome/core
|
||||
esphome/components/canbus/* @danielschramm @mvturnho
|
||||
esphome/components/cap1188/* @MrEditor97
|
||||
esphome/components/captive_portal/* @OttoWinter
|
||||
esphome/components/ccs811/* @habbie
|
||||
esphome/components/climate/* @esphome/core
|
||||
@@ -39,6 +41,7 @@ esphome/components/color_temperature/* @jesserockz
|
||||
esphome/components/coolix/* @glmnet
|
||||
esphome/components/cover/* @esphome/core
|
||||
esphome/components/cs5460a/* @balrog-kun
|
||||
esphome/components/cse7761/* @berfenger
|
||||
esphome/components/ct_clamp/* @jesserockz
|
||||
esphome/components/current_based/* @djwmarcx
|
||||
esphome/components/daly_bms/* @s1lvi0
|
||||
@@ -51,6 +54,7 @@ esphome/components/dsmr/* @glmnet @zuidwijk
|
||||
esphome/components/esp32/* @esphome/core
|
||||
esphome/components/esp32_ble/* @jesserockz
|
||||
esphome/components/esp32_ble_server/* @jesserockz
|
||||
esphome/components/esp32_camera_web_server/* @ayufan
|
||||
esphome/components/esp32_improv/* @jesserockz
|
||||
esphome/components/esp8266/* @esphome/core
|
||||
esphome/components/exposure_notifications/* @OttoWinter
|
||||
@@ -69,7 +73,7 @@ esphome/components/hitachi_ac424/* @sourabhjaiswal
|
||||
esphome/components/homeassistant/* @OttoWinter
|
||||
esphome/components/hrxl_maxsonar_wr/* @netmikey
|
||||
esphome/components/i2c/* @esphome/core
|
||||
esphome/components/improv/* @jesserockz
|
||||
esphome/components/improv_serial/* @esphome/core
|
||||
esphome/components/inkbird_ibsth1_mini/* @fkirill
|
||||
esphome/components/inkplate6/* @jesserockz
|
||||
esphome/components/integration/* @OttoWinter
|
||||
@@ -177,6 +181,7 @@ esphome/components/tuya/binary_sensor/* @jesserockz
|
||||
esphome/components/tuya/climate/* @jesserockz
|
||||
esphome/components/tuya/sensor/* @jesserockz
|
||||
esphome/components/tuya/switch/* @jesserockz
|
||||
esphome/components/tuya/text_sensor/* @dentra
|
||||
esphome/components/uart/* @esphome/core
|
||||
esphome/components/ultrasonic/* @OttoWinter
|
||||
esphome/components/version/* @esphome/core
|
||||
|
||||
@@ -147,9 +147,9 @@ RUN \
|
||||
/var/{cache,log}/* \
|
||||
/var/lib/apt/lists/*
|
||||
|
||||
COPY requirements.txt requirements_optional.txt docker/platformio_install_deps.py platformio.ini /
|
||||
COPY requirements.txt requirements_optional.txt requirements_test.txt docker/platformio_install_deps.py platformio.ini /
|
||||
RUN \
|
||||
pip3 install --no-cache-dir -r /requirements.txt -r /requirements_optional.txt \
|
||||
pip3 install --no-cache-dir -r /requirements.txt -r /requirements_optional.txt -r /requirements_test.txt \
|
||||
&& /platformio_install_deps.py /platformio.ini
|
||||
|
||||
VOLUME ["/esphome"]
|
||||
|
||||
@@ -18,6 +18,7 @@ from esphome.const import (
|
||||
CONF_PORT,
|
||||
CONF_ESPHOME,
|
||||
CONF_PLATFORMIO_OPTIONS,
|
||||
SECRETS_FILES,
|
||||
)
|
||||
from esphome.core import CORE, EsphomeError, coroutine
|
||||
from esphome.helpers import indent
|
||||
@@ -144,6 +145,8 @@ def wrap_to_code(name, comp):
|
||||
if comp.config_schema is not None:
|
||||
conf_str = yaml_util.dump(conf)
|
||||
conf_str = conf_str.replace("//", "")
|
||||
# remove tailing \ to avoid multi-line comment warning
|
||||
conf_str = conf_str.replace("\\\n", "\n")
|
||||
cg.add(cg.LineComment(indent(conf_str)))
|
||||
await coro(conf)
|
||||
|
||||
@@ -200,8 +203,7 @@ def upload_using_esptool(config, port):
|
||||
firmware_offset = "0x10000" if CORE.is_esp32 else "0x0"
|
||||
flash_images = [
|
||||
platformio_api.FlashImage(
|
||||
path=idedata.firmware_bin_path,
|
||||
offset=firmware_offset,
|
||||
path=idedata.firmware_bin_path, offset=firmware_offset
|
||||
),
|
||||
*idedata.extra_flash_images,
|
||||
]
|
||||
@@ -226,6 +228,8 @@ def upload_using_esptool(config, port):
|
||||
mcu,
|
||||
"write_flash",
|
||||
"-z",
|
||||
"--flash_size",
|
||||
"detect",
|
||||
]
|
||||
for img in flash_images:
|
||||
cmd += [img.offset, img.path]
|
||||
@@ -605,10 +609,7 @@ def parse_args(argv):
|
||||
"wizard",
|
||||
help="A helpful setup wizard that will guide you through setting up ESPHome.",
|
||||
)
|
||||
parser_wizard.add_argument(
|
||||
"configuration",
|
||||
help="Your YAML configuration file.",
|
||||
)
|
||||
parser_wizard.add_argument("configuration", help="Your YAML configuration file.")
|
||||
|
||||
parser_fingerprint = subparsers.add_parser(
|
||||
"mqtt-fingerprint", help="Get the SSL fingerprint from a MQTT broker."
|
||||
@@ -630,8 +631,7 @@ def parse_args(argv):
|
||||
"dashboard", help="Create a simple web server for a dashboard."
|
||||
)
|
||||
parser_dashboard.add_argument(
|
||||
"configuration",
|
||||
help="Your YAML configuration file directory.",
|
||||
"configuration", help="Your YAML configuration file directory."
|
||||
)
|
||||
parser_dashboard.add_argument(
|
||||
"--port",
|
||||
@@ -639,6 +639,12 @@ def parse_args(argv):
|
||||
type=int,
|
||||
default=6052,
|
||||
)
|
||||
parser_dashboard.add_argument(
|
||||
"--address",
|
||||
help="The address to bind to.",
|
||||
type=str,
|
||||
default="0.0.0.0",
|
||||
)
|
||||
parser_dashboard.add_argument(
|
||||
"--username",
|
||||
help="The optional username to require for authentication.",
|
||||
@@ -787,6 +793,10 @@ def run_esphome(argv):
|
||||
return 1
|
||||
|
||||
for conf_path in args.configuration:
|
||||
if any(os.path.basename(conf_path) == x for x in SECRETS_FILES):
|
||||
_LOGGER.warning("Skipping secrets file %s", conf_path)
|
||||
continue
|
||||
|
||||
CORE.config_path = conf_path
|
||||
CORE.dashboard = args.dashboard
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_AUTOMATION_ID,
|
||||
CONF_CONDITION,
|
||||
CONF_COUNT,
|
||||
CONF_ELSE,
|
||||
CONF_ID,
|
||||
CONF_THEN,
|
||||
@@ -66,6 +67,7 @@ DelayAction = cg.esphome_ns.class_("DelayAction", Action, cg.Component)
|
||||
LambdaAction = cg.esphome_ns.class_("LambdaAction", Action)
|
||||
IfAction = cg.esphome_ns.class_("IfAction", Action)
|
||||
WhileAction = cg.esphome_ns.class_("WhileAction", Action)
|
||||
RepeatAction = cg.esphome_ns.class_("RepeatAction", Action)
|
||||
WaitUntilAction = cg.esphome_ns.class_("WaitUntilAction", Action, cg.Component)
|
||||
UpdateComponentAction = cg.esphome_ns.class_("UpdateComponentAction", Action)
|
||||
Automation = cg.esphome_ns.class_("Automation")
|
||||
@@ -241,6 +243,25 @@ async def while_action_to_code(config, action_id, template_arg, args):
|
||||
return var
|
||||
|
||||
|
||||
@register_action(
|
||||
"repeat",
|
||||
RepeatAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_COUNT): cv.templatable(cv.positive_not_null_int),
|
||||
cv.Required(CONF_THEN): validate_action_list,
|
||||
}
|
||||
),
|
||||
)
|
||||
async def repeat_action_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
count_template = await cg.templatable(config[CONF_COUNT], args, cg.uint32)
|
||||
cg.add(var.set_count(count_template))
|
||||
actions = await build_action_list(config[CONF_THEN], template_arg, args)
|
||||
cg.add(var.add_then(actions))
|
||||
return var
|
||||
|
||||
|
||||
def validate_wait_until(value):
|
||||
schema = cv.Schema(
|
||||
{
|
||||
|
||||
@@ -81,4 +81,5 @@ from esphome.cpp_types import ( # noqa
|
||||
GPIOPin,
|
||||
InternalGPIOPin,
|
||||
gpio_Flags,
|
||||
EntityCategory,
|
||||
)
|
||||
|
||||
@@ -25,7 +25,7 @@ void AdalightLightEffect::stop() {
|
||||
AddressableLightEffect::stop();
|
||||
}
|
||||
|
||||
int AdalightLightEffect::get_frame_size_(int led_count) const {
|
||||
unsigned int AdalightLightEffect::get_frame_size_(int led_count) const {
|
||||
// 3 bytes: Ada
|
||||
// 2 bytes: LED count
|
||||
// 1 byte: checksum
|
||||
|
||||
@@ -25,7 +25,7 @@ class AdalightLightEffect : public light::AddressableLightEffect, public uart::U
|
||||
CONSUMED,
|
||||
};
|
||||
|
||||
int get_frame_size_(int led_count) const;
|
||||
unsigned int get_frame_size_(int led_count) const;
|
||||
void reset_frame_(light::AddressableLight &it);
|
||||
void blank_all_leds_(light::AddressableLight &it);
|
||||
Frame parse_frame_(light::AddressableLight &it);
|
||||
|
||||
@@ -91,17 +91,21 @@ void ADCSensor::dump_config() {
|
||||
float ADCSensor::get_setup_priority() const { return setup_priority::DATA; }
|
||||
void ADCSensor::update() {
|
||||
float value_v = this->sample();
|
||||
ESP_LOGD(TAG, "'%s': Got voltage=%.2fV", this->get_name().c_str(), value_v);
|
||||
ESP_LOGV(TAG, "'%s': Got voltage=%.4fV", this->get_name().c_str(), value_v);
|
||||
this->publish_state(value_v);
|
||||
}
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
float ADCSensor::sample() {
|
||||
#ifdef USE_ADC_SENSOR_VCC
|
||||
return ESP.getVcc() / 1024.0f; // NOLINT(readability-static-accessed-through-instance)
|
||||
int raw = ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance)
|
||||
#else
|
||||
return analogRead(this->pin_->get_pin()) / 1024.0f; // NOLINT
|
||||
int raw = analogRead(this->pin_->get_pin()); // NOLINT
|
||||
#endif
|
||||
if (output_raw_) {
|
||||
return raw;
|
||||
}
|
||||
return raw / 1024.0f;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -112,6 +116,9 @@ float ADCSensor::sample() {
|
||||
if (raw == -1) {
|
||||
return NAN;
|
||||
}
|
||||
if (output_raw_) {
|
||||
return raw;
|
||||
}
|
||||
uint32_t mv = esp_adc_cal_raw_to_voltage(raw, &cal_characteristics_[(int) attenuation_]);
|
||||
return mv / 1000.0f;
|
||||
}
|
||||
@@ -135,10 +142,6 @@ float ADCSensor::sample() {
|
||||
if (raw0 == -1 || raw2 == -1 || raw6 == -1 || raw11 == -1) {
|
||||
return NAN;
|
||||
}
|
||||
// prevent divide by zero
|
||||
if (raw0 == 0 && raw2 == 0 && raw6 == 0 && raw11 == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t mv11 = esp_adc_cal_raw_to_voltage(raw11, &cal_characteristics_[(int) ADC_ATTEN_DB_11]);
|
||||
uint32_t mv6 = esp_adc_cal_raw_to_voltage(raw6, &cal_characteristics_[(int) ADC_ATTEN_DB_6]);
|
||||
|
||||
@@ -31,6 +31,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
|
||||
/// `HARDWARE_LATE` setup priority.
|
||||
float get_setup_priority() const override;
|
||||
void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
|
||||
void set_output_raw(bool output_raw) { output_raw_ = output_raw; }
|
||||
float sample() override;
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
@@ -39,8 +40,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
|
||||
|
||||
protected:
|
||||
InternalGPIOPin *pin_;
|
||||
uint16_t read_raw_();
|
||||
uint32_t raw_to_microvolts_(uint16_t raw);
|
||||
bool output_raw_{false};
|
||||
|
||||
#ifdef USE_ESP32
|
||||
adc_atten_t attenuation_{ADC_ATTEN_DB_0};
|
||||
|
||||
@@ -4,6 +4,7 @@ from esphome import pins
|
||||
from esphome.components import sensor, voltage_sampler
|
||||
from esphome.const import (
|
||||
CONF_ATTENUATION,
|
||||
CONF_RAW,
|
||||
CONF_ID,
|
||||
CONF_INPUT,
|
||||
CONF_NUMBER,
|
||||
@@ -119,12 +120,18 @@ def validate_adc_pin(value):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def validate_config(config):
|
||||
if config[CONF_RAW] and config.get(CONF_ATTENUATION, None) == "auto":
|
||||
raise cv.Invalid("Automatic attenuation cannot be used when raw output is set.")
|
||||
return config
|
||||
|
||||
|
||||
adc_ns = cg.esphome_ns.namespace("adc")
|
||||
ADCSensor = adc_ns.class_(
|
||||
"ADCSensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_VOLT,
|
||||
accuracy_decimals=2,
|
||||
@@ -135,12 +142,14 @@ CONFIG_SCHEMA = (
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ADCSensor),
|
||||
cv.Required(CONF_PIN): validate_adc_pin,
|
||||
cv.Optional(CONF_RAW, default=False): cv.boolean,
|
||||
cv.SplitDefault(CONF_ATTENUATION, esp32="0db"): cv.All(
|
||||
cv.only_on_esp32, cv.enum(ATTENUATION_MODES, lower=True)
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(cv.polling_component_schema("60s")),
|
||||
validate_config,
|
||||
)
|
||||
|
||||
|
||||
@@ -155,6 +164,9 @@ async def to_code(config):
|
||||
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||
cg.add(var.set_pin(pin))
|
||||
|
||||
if CONF_RAW in config:
|
||||
cg.add(var.set_output_raw(config[CONF_RAW]))
|
||||
|
||||
if CONF_ATTENUATION in config:
|
||||
if config[CONF_ATTENUATION] == "auto":
|
||||
cg.add(var.set_autorange(cg.global_ns.true))
|
||||
|
||||
@@ -73,13 +73,6 @@ void AHT10Component::update() {
|
||||
bool success = false;
|
||||
for (int i = 0; i < AHT10_ATTEMPTS; ++i) {
|
||||
ESP_LOGVV(TAG, "Attempt %d at %6u", i, millis());
|
||||
delay_microseconds_accurate(4);
|
||||
|
||||
uint8_t reg = 0;
|
||||
if (this->write(®, 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGD(TAG, "Communication with AHT10 failed, waiting...");
|
||||
continue;
|
||||
}
|
||||
delay(delay_ms);
|
||||
if (this->read(data, 6) != i2c::ERROR_OK) {
|
||||
ESP_LOGD(TAG, "Communication with AHT10 failed, waiting...");
|
||||
@@ -117,12 +110,12 @@ void AHT10Component::update() {
|
||||
uint32_t raw_temperature = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
|
||||
uint32_t raw_humidity = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4;
|
||||
|
||||
float temperature = ((200.0 * (float) raw_temperature) / 1048576.0) - 50.0;
|
||||
float temperature = ((200.0f * (float) raw_temperature) / 1048576.0f) - 50.0f;
|
||||
float humidity;
|
||||
if (raw_humidity == 0) { // unrealistic value
|
||||
humidity = NAN;
|
||||
} else {
|
||||
humidity = (float) raw_humidity * 100.0 / 1048576.0;
|
||||
humidity = (float) raw_humidity * 100.0f / 1048576.0f;
|
||||
}
|
||||
|
||||
if (this->temperature_sensor_ != nullptr) {
|
||||
|
||||
@@ -38,9 +38,9 @@ void AM2320Component::update() {
|
||||
return;
|
||||
}
|
||||
|
||||
float temperature = (((data[4] & 0x7F) << 8) + data[5]) / 10.0;
|
||||
float temperature = (((data[4] & 0x7F) << 8) + data[5]) / 10.0f;
|
||||
temperature = (data[4] & 0x80) ? -temperature : temperature;
|
||||
float humidity = ((data[2] << 8) + data[3]) / 10.0;
|
||||
float humidity = ((data[2] << 8) + data[3]) / 10.0f;
|
||||
|
||||
ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temperature, humidity);
|
||||
if (this->temperature_sensor_ != nullptr)
|
||||
|
||||
@@ -4,7 +4,8 @@ from esphome.components import sensor, ble_client
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_BATTERY_LEVEL,
|
||||
ICON_BATTERY,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
CONF_ILLUMINANCE,
|
||||
ICON_BRIGHTNESS_5,
|
||||
UNIT_PERCENT,
|
||||
@@ -20,10 +21,15 @@ CONFIG_SCHEMA = (
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(Am43),
|
||||
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
|
||||
UNIT_PERCENT, ICON_BATTERY, 0
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
device_class=DEVICE_CLASS_BATTERY,
|
||||
accuracy_decimals=0,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(
|
||||
UNIT_PERCENT, ICON_BRIGHTNESS_5, 0
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
icon=ICON_BRIGHTNESS_5,
|
||||
accuracy_decimals=0,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -44,8 +44,9 @@ async def to_code(config):
|
||||
width, height = image.size
|
||||
frames = image.n_frames
|
||||
if CONF_RESIZE in config:
|
||||
image.thumbnail(config[CONF_RESIZE])
|
||||
width, height = image.size
|
||||
new_width_max, new_height_max = config[CONF_RESIZE]
|
||||
ratio = min(new_width_max / width, new_height_max / height)
|
||||
width, height = int(width * ratio), int(height * ratio)
|
||||
else:
|
||||
if width > 500 or height > 500:
|
||||
_LOGGER.warning(
|
||||
@@ -59,7 +60,13 @@ async def to_code(config):
|
||||
for frameIndex in range(frames):
|
||||
image.seek(frameIndex)
|
||||
frame = image.convert("L", dither=Image.NONE)
|
||||
if CONF_RESIZE in config:
|
||||
frame = frame.resize([width, height])
|
||||
pixels = list(frame.getdata())
|
||||
if len(pixels) != height * width:
|
||||
raise core.EsphomeError(
|
||||
f"Unexpected number of pixels in {path} frame {frameIndex}: ({len(pixels)} != {height*width})"
|
||||
)
|
||||
for pix in pixels:
|
||||
data[pos] = pix
|
||||
pos += 1
|
||||
@@ -70,7 +77,13 @@ async def to_code(config):
|
||||
for frameIndex in range(frames):
|
||||
image.seek(frameIndex)
|
||||
frame = image.convert("RGB")
|
||||
if CONF_RESIZE in config:
|
||||
frame = frame.resize([width, height])
|
||||
pixels = list(frame.getdata())
|
||||
if len(pixels) != height * width:
|
||||
raise core.EsphomeError(
|
||||
f"Unexpected number of pixels in {path} frame {frameIndex}: ({len(pixels)} != {height*width})"
|
||||
)
|
||||
for pix in pixels:
|
||||
data[pos] = pix[0]
|
||||
pos += 1
|
||||
@@ -85,6 +98,8 @@ async def to_code(config):
|
||||
for frameIndex in range(frames):
|
||||
image.seek(frameIndex)
|
||||
frame = image.convert("1", dither=Image.NONE)
|
||||
if CONF_RESIZE in config:
|
||||
frame = frame.resize([width, height])
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if frame.getpixel((x, y)):
|
||||
|
||||
@@ -30,7 +30,7 @@ class Anova : public climate::Climate, public esphome::ble_client::BLEClientNode
|
||||
climate::ClimateTraits traits() override {
|
||||
auto traits = climate::ClimateTraits();
|
||||
traits.set_supports_current_temperature(true);
|
||||
traits.set_supports_heat_mode(true);
|
||||
traits.set_supported_modes({climate::CLIMATE_MODE_OFF, climate::ClimateMode::CLIMATE_MODE_HEAT});
|
||||
traits.set_visual_min_temperature(25.0);
|
||||
traits.set_visual_max_temperature(100.0);
|
||||
traits.set_visual_temperature_step(0.1);
|
||||
|
||||
@@ -73,51 +73,46 @@ AnovaPacket *AnovaCodec::get_stop_request() {
|
||||
}
|
||||
|
||||
void AnovaCodec::decode(const uint8_t *data, uint16_t length) {
|
||||
memset(this->buf_, 0, 32);
|
||||
strncpy(this->buf_, (char *) data, length);
|
||||
char buf[32];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
strncpy(buf, (char *) data, std::min<uint16_t>(length, sizeof(buf) - 1));
|
||||
this->has_target_temp_ = this->has_current_temp_ = this->has_unit_ = this->has_running_ = false;
|
||||
switch (this->current_query_) {
|
||||
case READ_DEVICE_STATUS: {
|
||||
if (!strncmp(this->buf_, "stopped", 7)) {
|
||||
if (!strncmp(buf, "stopped", 7)) {
|
||||
this->has_running_ = true;
|
||||
this->running_ = false;
|
||||
}
|
||||
if (!strncmp(this->buf_, "running", 7)) {
|
||||
if (!strncmp(buf, "running", 7)) {
|
||||
this->has_running_ = true;
|
||||
this->running_ = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case START: {
|
||||
if (!strncmp(this->buf_, "start", 5)) {
|
||||
if (!strncmp(buf, "start", 5)) {
|
||||
this->has_running_ = true;
|
||||
this->running_ = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STOP: {
|
||||
if (!strncmp(this->buf_, "stop", 4)) {
|
||||
if (!strncmp(buf, "stop", 4)) {
|
||||
this->has_running_ = true;
|
||||
this->running_ = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case READ_TARGET_TEMPERATURE: {
|
||||
this->target_temp_ = strtof(this->buf_, nullptr);
|
||||
if (this->fahrenheit_)
|
||||
this->target_temp_ = ftoc(this->target_temp_);
|
||||
this->has_target_temp_ = true;
|
||||
break;
|
||||
}
|
||||
case READ_TARGET_TEMPERATURE:
|
||||
case SET_TARGET_TEMPERATURE: {
|
||||
this->target_temp_ = strtof(this->buf_, nullptr);
|
||||
this->target_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
|
||||
if (this->fahrenheit_)
|
||||
this->target_temp_ = ftoc(this->target_temp_);
|
||||
this->has_target_temp_ = true;
|
||||
break;
|
||||
}
|
||||
case READ_CURRENT_TEMPERATURE: {
|
||||
this->current_temp_ = strtof(this->buf_, nullptr);
|
||||
this->current_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
|
||||
if (this->fahrenheit_)
|
||||
this->current_temp_ = ftoc(this->current_temp_);
|
||||
this->has_current_temp_ = true;
|
||||
@@ -125,8 +120,8 @@ void AnovaCodec::decode(const uint8_t *data, uint16_t length) {
|
||||
}
|
||||
case SET_UNIT:
|
||||
case READ_UNIT: {
|
||||
this->unit_ = this->buf_[0];
|
||||
this->fahrenheit_ = this->buf_[0] == 'f';
|
||||
this->unit_ = buf[0];
|
||||
this->fahrenheit_ = buf[0] == 'f';
|
||||
this->has_unit_ = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -70,7 +70,6 @@ class AnovaCodec {
|
||||
bool has_current_temp_;
|
||||
bool has_unit_;
|
||||
bool has_running_;
|
||||
char buf_[32];
|
||||
bool fahrenheit_;
|
||||
|
||||
CurrentQuery current_query_;
|
||||
|
||||
@@ -40,6 +40,7 @@ service APIConnection {
|
||||
rpc climate_command (ClimateCommandRequest) returns (void) {}
|
||||
rpc number_command (NumberCommandRequest) returns (void) {}
|
||||
rpc select_command (SelectCommandRequest) returns (void) {}
|
||||
rpc button_command (ButtonCommandRequest) returns (void) {}
|
||||
}
|
||||
|
||||
|
||||
@@ -182,6 +183,8 @@ message DeviceInfoResponse {
|
||||
// The esphome project details if set
|
||||
string project_name = 8;
|
||||
string project_version = 9;
|
||||
|
||||
uint32 webserver_port = 10;
|
||||
}
|
||||
|
||||
message ListEntitiesRequest {
|
||||
@@ -201,6 +204,14 @@ message SubscribeStatesRequest {
|
||||
// Empty
|
||||
}
|
||||
|
||||
// ==================== COMMON =====================
|
||||
|
||||
enum EntityCategory {
|
||||
ENTITY_CATEGORY_NONE = 0;
|
||||
ENTITY_CATEGORY_CONFIG = 1;
|
||||
ENTITY_CATEGORY_DIAGNOSTIC = 2;
|
||||
}
|
||||
|
||||
// ==================== BINARY SENSOR ====================
|
||||
message ListEntitiesBinarySensorResponse {
|
||||
option (id) = 12;
|
||||
@@ -216,6 +227,7 @@ message ListEntitiesBinarySensorResponse {
|
||||
bool is_status_binary_sensor = 6;
|
||||
bool disabled_by_default = 7;
|
||||
string icon = 8;
|
||||
EntityCategory entity_category = 9;
|
||||
}
|
||||
message BinarySensorStateResponse {
|
||||
option (id) = 21;
|
||||
@@ -247,6 +259,7 @@ message ListEntitiesCoverResponse {
|
||||
string device_class = 8;
|
||||
bool disabled_by_default = 9;
|
||||
string icon = 10;
|
||||
EntityCategory entity_category = 11;
|
||||
}
|
||||
|
||||
enum LegacyCoverState {
|
||||
@@ -316,6 +329,7 @@ message ListEntitiesFanResponse {
|
||||
int32 supported_speed_count = 8;
|
||||
bool disabled_by_default = 9;
|
||||
string icon = 10;
|
||||
EntityCategory entity_category = 11;
|
||||
}
|
||||
enum FanSpeed {
|
||||
FAN_SPEED_LOW = 0;
|
||||
@@ -392,6 +406,7 @@ message ListEntitiesLightResponse {
|
||||
repeated string effects = 11;
|
||||
bool disabled_by_default = 13;
|
||||
string icon = 14;
|
||||
EntityCategory entity_category = 15;
|
||||
}
|
||||
message LightStateResponse {
|
||||
option (id) = 24;
|
||||
@@ -480,6 +495,7 @@ message ListEntitiesSensorResponse {
|
||||
// Last reset type removed in 2021.9.0
|
||||
SensorLastResetType legacy_last_reset_type = 11;
|
||||
bool disabled_by_default = 12;
|
||||
EntityCategory entity_category = 13;
|
||||
}
|
||||
message SensorStateResponse {
|
||||
option (id) = 25;
|
||||
@@ -508,6 +524,7 @@ message ListEntitiesSwitchResponse {
|
||||
string icon = 5;
|
||||
bool assumed_state = 6;
|
||||
bool disabled_by_default = 7;
|
||||
EntityCategory entity_category = 8;
|
||||
}
|
||||
message SwitchStateResponse {
|
||||
option (id) = 26;
|
||||
@@ -541,6 +558,7 @@ message ListEntitiesTextSensorResponse {
|
||||
|
||||
string icon = 5;
|
||||
bool disabled_by_default = 6;
|
||||
EntityCategory entity_category = 7;
|
||||
}
|
||||
message TextSensorStateResponse {
|
||||
option (id) = 27;
|
||||
@@ -702,6 +720,7 @@ message ListEntitiesCameraResponse {
|
||||
string unique_id = 4;
|
||||
bool disabled_by_default = 5;
|
||||
string icon = 6;
|
||||
EntityCategory entity_category = 7;
|
||||
}
|
||||
|
||||
message CameraImageResponse {
|
||||
@@ -796,6 +815,7 @@ message ListEntitiesClimateResponse {
|
||||
repeated string supported_custom_presets = 17;
|
||||
bool disabled_by_default = 18;
|
||||
string icon = 19;
|
||||
EntityCategory entity_category = 20;
|
||||
}
|
||||
message ClimateStateResponse {
|
||||
option (id) = 47;
|
||||
@@ -849,6 +869,11 @@ message ClimateCommandRequest {
|
||||
}
|
||||
|
||||
// ==================== NUMBER ====================
|
||||
enum NumberMode {
|
||||
NUMBER_MODE_AUTO = 0;
|
||||
NUMBER_MODE_BOX = 1;
|
||||
NUMBER_MODE_SLIDER = 2;
|
||||
}
|
||||
message ListEntitiesNumberResponse {
|
||||
option (id) = 49;
|
||||
option (source) = SOURCE_SERVER;
|
||||
@@ -864,6 +889,9 @@ message ListEntitiesNumberResponse {
|
||||
float max_value = 7;
|
||||
float step = 8;
|
||||
bool disabled_by_default = 9;
|
||||
EntityCategory entity_category = 10;
|
||||
string unit_of_measurement = 11;
|
||||
NumberMode mode = 12;
|
||||
}
|
||||
message NumberStateResponse {
|
||||
option (id) = 50;
|
||||
@@ -901,6 +929,7 @@ message ListEntitiesSelectResponse {
|
||||
string icon = 5;
|
||||
repeated string options = 6;
|
||||
bool disabled_by_default = 7;
|
||||
EntityCategory entity_category = 8;
|
||||
}
|
||||
message SelectStateResponse {
|
||||
option (id) = 53;
|
||||
@@ -923,3 +952,28 @@ message SelectCommandRequest {
|
||||
fixed32 key = 1;
|
||||
string state = 2;
|
||||
}
|
||||
|
||||
// ==================== BUTTON ====================
|
||||
message ListEntitiesButtonResponse {
|
||||
option (id) = 61;
|
||||
option (source) = SOURCE_SERVER;
|
||||
option (ifdef) = "USE_BUTTON";
|
||||
|
||||
string object_id = 1;
|
||||
fixed32 key = 2;
|
||||
string name = 3;
|
||||
string unique_id = 4;
|
||||
|
||||
string icon = 5;
|
||||
bool disabled_by_default = 6;
|
||||
EntityCategory entity_category = 7;
|
||||
string device_class = 8;
|
||||
}
|
||||
message ButtonCommandRequest {
|
||||
option (id) = 62;
|
||||
option (source) = SOURCE_CLIENT;
|
||||
option (ifdef) = "USE_BUTTON";
|
||||
option (no_delay) = true;
|
||||
|
||||
fixed32 key = 1;
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ void APIConnection::loop() {
|
||||
|
||||
if (state_subs_at_ != -1) {
|
||||
const auto &subs = this->parent_->get_state_subs();
|
||||
if (state_subs_at_ >= subs.size()) {
|
||||
if (state_subs_at_ >= (int) subs.size()) {
|
||||
state_subs_at_ = -1;
|
||||
} else {
|
||||
auto &it = subs[state_subs_at_];
|
||||
@@ -184,6 +184,7 @@ bool APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_
|
||||
msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
|
||||
msg.disabled_by_default = binary_sensor->is_disabled_by_default();
|
||||
msg.icon = binary_sensor->get_icon();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
|
||||
return this->send_list_entities_binary_sensor_response(msg);
|
||||
}
|
||||
#endif
|
||||
@@ -217,6 +218,7 @@ bool APIConnection::send_cover_info(cover::Cover *cover) {
|
||||
msg.device_class = cover->get_device_class();
|
||||
msg.disabled_by_default = cover->is_disabled_by_default();
|
||||
msg.icon = cover->get_icon();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
|
||||
return this->send_list_entities_cover_response(msg);
|
||||
}
|
||||
void APIConnection::cover_command(const CoverCommandRequest &msg) {
|
||||
@@ -283,6 +285,7 @@ bool APIConnection::send_fan_info(fan::FanState *fan) {
|
||||
msg.supported_speed_count = traits.supported_speed_count();
|
||||
msg.disabled_by_default = fan->is_disabled_by_default();
|
||||
msg.icon = fan->get_icon();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
|
||||
return this->send_list_entities_fan_response(msg);
|
||||
}
|
||||
void APIConnection::fan_command(const FanCommandRequest &msg) {
|
||||
@@ -346,6 +349,7 @@ bool APIConnection::send_light_info(light::LightState *light) {
|
||||
|
||||
msg.disabled_by_default = light->is_disabled_by_default();
|
||||
msg.icon = light->get_icon();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(light->get_entity_category());
|
||||
|
||||
for (auto mode : traits.get_supported_color_modes())
|
||||
msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
|
||||
@@ -432,7 +436,7 @@ bool APIConnection::send_sensor_info(sensor::Sensor *sensor) {
|
||||
msg.device_class = sensor->get_device_class();
|
||||
msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
|
||||
msg.disabled_by_default = sensor->is_disabled_by_default();
|
||||
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(sensor->get_entity_category());
|
||||
return this->send_list_entities_sensor_response(msg);
|
||||
}
|
||||
#endif
|
||||
@@ -456,6 +460,7 @@ bool APIConnection::send_switch_info(switch_::Switch *a_switch) {
|
||||
msg.icon = a_switch->get_icon();
|
||||
msg.assumed_state = a_switch->assumed_state();
|
||||
msg.disabled_by_default = a_switch->is_disabled_by_default();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(a_switch->get_entity_category());
|
||||
return this->send_list_entities_switch_response(msg);
|
||||
}
|
||||
void APIConnection::switch_command(const SwitchCommandRequest &msg) {
|
||||
@@ -491,6 +496,7 @@ bool APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor)
|
||||
msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
|
||||
msg.icon = text_sensor->get_icon();
|
||||
msg.disabled_by_default = text_sensor->is_disabled_by_default();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(text_sensor->get_entity_category());
|
||||
return this->send_list_entities_text_sensor_response(msg);
|
||||
}
|
||||
#endif
|
||||
@@ -537,6 +543,7 @@ bool APIConnection::send_climate_info(climate::Climate *climate) {
|
||||
|
||||
msg.disabled_by_default = climate->is_disabled_by_default();
|
||||
msg.icon = climate->get_icon();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
|
||||
|
||||
msg.supports_current_temperature = traits.get_supports_current_temperature();
|
||||
msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
|
||||
@@ -611,6 +618,9 @@ bool APIConnection::send_number_info(number::Number *number) {
|
||||
msg.unique_id = get_default_unique_id("number", number);
|
||||
msg.icon = number->get_icon();
|
||||
msg.disabled_by_default = number->is_disabled_by_default();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(number->get_entity_category());
|
||||
msg.unit_of_measurement = number->traits.get_unit_of_measurement();
|
||||
msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
|
||||
|
||||
msg.min_value = number->traits.get_min_value();
|
||||
msg.max_value = number->traits.get_max_value();
|
||||
@@ -648,6 +658,7 @@ bool APIConnection::send_select_info(select::Select *select) {
|
||||
msg.unique_id = get_default_unique_id("select", select);
|
||||
msg.icon = select->get_icon();
|
||||
msg.disabled_by_default = select->is_disabled_by_default();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(select->get_entity_category());
|
||||
|
||||
for (const auto &option : select->traits.get_options())
|
||||
msg.options.push_back(option);
|
||||
@@ -665,6 +676,28 @@ void APIConnection::select_command(const SelectCommandRequest &msg) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
bool APIConnection::send_button_info(button::Button *button) {
|
||||
ListEntitiesButtonResponse msg;
|
||||
msg.key = button->get_object_id_hash();
|
||||
msg.object_id = button->get_object_id();
|
||||
msg.name = button->get_name();
|
||||
msg.unique_id = get_default_unique_id("button", button);
|
||||
msg.icon = button->get_icon();
|
||||
msg.disabled_by_default = button->is_disabled_by_default();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(button->get_entity_category());
|
||||
msg.device_class = button->get_device_class();
|
||||
return this->send_list_entities_button_response(msg);
|
||||
}
|
||||
void APIConnection::button_command(const ButtonCommandRequest &msg) {
|
||||
button::Button *button = App.get_button_by_key(msg.key);
|
||||
if (button == nullptr)
|
||||
return;
|
||||
|
||||
button->press();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
|
||||
if (!this->state_subscription_)
|
||||
@@ -681,6 +714,7 @@ bool APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) {
|
||||
msg.unique_id = get_default_unique_id("camera", camera);
|
||||
msg.disabled_by_default = camera->is_disabled_by_default();
|
||||
msg.icon = camera->get_icon();
|
||||
msg.entity_category = static_cast<enums::EntityCategory>(camera->get_entity_category());
|
||||
return this->send_list_entities_camera_response(msg);
|
||||
}
|
||||
void APIConnection::camera_image(const CameraImageRequest &msg) {
|
||||
@@ -759,6 +793,9 @@ DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) {
|
||||
#ifdef ESPHOME_PROJECT_NAME
|
||||
resp.project_name = ESPHOME_PROJECT_NAME;
|
||||
resp.project_version = ESPHOME_PROJECT_VERSION;
|
||||
#endif
|
||||
#ifdef USE_WEBSERVER
|
||||
resp.webserver_port = WEBSERVER_PORT;
|
||||
#endif
|
||||
return resp;
|
||||
}
|
||||
|
||||
@@ -73,6 +73,10 @@ class APIConnection : public APIServerConnection {
|
||||
bool send_select_state(select::Select *select, std::string state);
|
||||
bool send_select_info(select::Select *select);
|
||||
void select_command(const SelectCommandRequest &msg) override;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool send_button_info(button::Button *button);
|
||||
void button_command(const ButtonCommandRequest &msg) override;
|
||||
#endif
|
||||
bool send_log_message(int level, const char *tag, const char *line);
|
||||
void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
|
||||
|
||||
@@ -174,9 +174,6 @@ APIError APINoiseFrameHelper::loop() {
|
||||
* errno API_ERROR_HANDSHAKE_PACKET_LEN: Packet too big for this phase.
|
||||
*/
|
||||
APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
int err;
|
||||
APIError aerr;
|
||||
|
||||
if (frame == nullptr) {
|
||||
HELPER_LOG("Bad argument for try_read_frame_");
|
||||
return APIError::BAD_ARG;
|
||||
@@ -200,7 +197,7 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
}
|
||||
rx_header_buf_len_ += received;
|
||||
if (received != to_read) {
|
||||
if ((size_t) received != to_read) {
|
||||
// not a full read
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
@@ -247,7 +244,7 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
}
|
||||
rx_buf_len_ += received;
|
||||
if (received != to_read) {
|
||||
if ((size_t) received != to_read) {
|
||||
// not all read
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
@@ -255,7 +252,7 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
|
||||
// uncomment for even more debugging
|
||||
#ifdef HELPER_LOG_PACKETS
|
||||
ESP_LOGVV(TAG, "Received frame: %s", hexencode(rx_buf_).c_str());
|
||||
ESP_LOGVV(TAG, "Received frame: %s", format_hex_pretty(rx_buf_).c_str());
|
||||
#endif
|
||||
frame->msg = std::move(rx_buf_);
|
||||
// consume msg
|
||||
@@ -544,13 +541,13 @@ APIError APINoiseFrameHelper::try_send_tx_buf_() {
|
||||
APIError APINoiseFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||
if (iovcnt == 0)
|
||||
return APIError::OK;
|
||||
int err;
|
||||
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", hexencode(reinterpret_cast<uint8_t *>(iov[i].iov_base), iov[i].iov_len).c_str());
|
||||
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;
|
||||
}
|
||||
@@ -584,7 +581,7 @@ APIError APINoiseFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
} else if (sent != total_write_len) {
|
||||
} else if ((size_t) sent != total_write_len) {
|
||||
// partially sent, add end to tx_buf
|
||||
size_t to_consume = sent;
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
@@ -778,9 +775,6 @@ APIError APIPlaintextFrameHelper::loop() {
|
||||
* error API_ERROR_BAD_INDICATOR: Bad indicator byte at start of frame.
|
||||
*/
|
||||
APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
int err;
|
||||
APIError aerr;
|
||||
|
||||
if (frame == nullptr) {
|
||||
HELPER_LOG("Bad argument for try_read_frame_");
|
||||
return APIError::BAD_ARG;
|
||||
@@ -854,7 +848,7 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
}
|
||||
rx_buf_len_ += received;
|
||||
if (received != to_read) {
|
||||
if ((size_t) received != to_read) {
|
||||
// not all read
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
@@ -862,7 +856,7 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
|
||||
// uncomment for even more debugging
|
||||
#ifdef HELPER_LOG_PACKETS
|
||||
ESP_LOGVV(TAG, "Received frame: %s", hexencode(rx_buf_).c_str());
|
||||
ESP_LOGVV(TAG, "Received frame: %s", format_hex_pretty(rx_buf_).c_str());
|
||||
#endif
|
||||
frame->msg = std::move(rx_buf_);
|
||||
// consume msg
|
||||
@@ -874,7 +868,6 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
}
|
||||
|
||||
APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||
int err;
|
||||
APIError aerr;
|
||||
|
||||
if (state_ != State::DATA) {
|
||||
@@ -894,9 +887,6 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||
}
|
||||
bool APIPlaintextFrameHelper::can_write_without_blocking() { return state_ == State::DATA && tx_buf_.empty(); }
|
||||
APIError APIPlaintextFrameHelper::write_packet(uint16_t type, const uint8_t *payload, size_t payload_len) {
|
||||
int err;
|
||||
APIError aerr;
|
||||
|
||||
if (state_ != State::DATA) {
|
||||
return APIError::BAD_STATE;
|
||||
}
|
||||
@@ -940,13 +930,13 @@ APIError APIPlaintextFrameHelper::try_send_tx_buf_() {
|
||||
APIError APIPlaintextFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||
if (iovcnt == 0)
|
||||
return APIError::OK;
|
||||
int err;
|
||||
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", hexencode(reinterpret_cast<uint8_t *>(iov[i].iov_base), iov[i].iov_len).c_str());
|
||||
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;
|
||||
}
|
||||
@@ -980,7 +970,7 @@ APIError APIPlaintextFrameHelper::write_raw_(const struct iovec *iov, int iovcnt
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
} else if (sent != total_write_len) {
|
||||
} else if ((size_t) sent != total_write_len) {
|
||||
// partially sent, add end to tx_buf
|
||||
size_t to_consume = sent;
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
|
||||
@@ -6,6 +6,18 @@
|
||||
namespace esphome {
|
||||
namespace api {
|
||||
|
||||
template<> const char *proto_enum_to_string<enums::EntityCategory>(enums::EntityCategory value) {
|
||||
switch (value) {
|
||||
case enums::ENTITY_CATEGORY_NONE:
|
||||
return "ENTITY_CATEGORY_NONE";
|
||||
case enums::ENTITY_CATEGORY_CONFIG:
|
||||
return "ENTITY_CATEGORY_CONFIG";
|
||||
case enums::ENTITY_CATEGORY_DIAGNOSTIC:
|
||||
return "ENTITY_CATEGORY_DIAGNOSTIC";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
template<> const char *proto_enum_to_string<enums::LegacyCoverState>(enums::LegacyCoverState value) {
|
||||
switch (value) {
|
||||
case enums::LEGACY_COVER_STATE_OPEN:
|
||||
@@ -254,6 +266,18 @@ template<> const char *proto_enum_to_string<enums::ClimatePreset>(enums::Climate
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
template<> const char *proto_enum_to_string<enums::NumberMode>(enums::NumberMode value) {
|
||||
switch (value) {
|
||||
case enums::NUMBER_MODE_AUTO:
|
||||
return "NUMBER_MODE_AUTO";
|
||||
case enums::NUMBER_MODE_BOX:
|
||||
return "NUMBER_MODE_BOX";
|
||||
case enums::NUMBER_MODE_SLIDER:
|
||||
return "NUMBER_MODE_SLIDER";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
bool HelloRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
|
||||
switch (field_id) {
|
||||
case 1: {
|
||||
@@ -267,7 +291,7 @@ bool HelloRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value)
|
||||
void HelloRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->client_info); }
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void HelloRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("HelloRequest {\n");
|
||||
out.append(" client_info: ");
|
||||
out.append("'").append(this->client_info).append("'");
|
||||
@@ -306,7 +330,7 @@ void HelloResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void HelloResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("HelloResponse {\n");
|
||||
out.append(" api_version_major: ");
|
||||
sprintf(buffer, "%u", this->api_version_major);
|
||||
@@ -337,7 +361,7 @@ bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value
|
||||
void ConnectRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->password); }
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ConnectRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ConnectRequest {\n");
|
||||
out.append(" password: ");
|
||||
out.append("'").append(this->password).append("'");
|
||||
@@ -358,7 +382,7 @@ bool ConnectResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
|
||||
void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->invalid_password); }
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ConnectResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ConnectResponse {\n");
|
||||
out.append(" invalid_password: ");
|
||||
out.append(YESNO(this->invalid_password));
|
||||
@@ -396,6 +420,10 @@ bool DeviceInfoResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
|
||||
this->has_deep_sleep = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 10: {
|
||||
this->webserver_port = value.as_uint32();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -444,10 +472,11 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_bool(7, this->has_deep_sleep);
|
||||
buffer.encode_string(8, this->project_name);
|
||||
buffer.encode_string(9, this->project_version);
|
||||
buffer.encode_uint32(10, this->webserver_port);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void DeviceInfoResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("DeviceInfoResponse {\n");
|
||||
out.append(" uses_password: ");
|
||||
out.append(YESNO(this->uses_password));
|
||||
@@ -484,6 +513,11 @@ void DeviceInfoResponse::dump_to(std::string &out) const {
|
||||
out.append(" project_version: ");
|
||||
out.append("'").append(this->project_version).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" webserver_port: ");
|
||||
sprintf(buffer, "%u", this->webserver_port);
|
||||
out.append(buffer);
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -509,6 +543,10 @@ bool ListEntitiesBinarySensorResponse::decode_varint(uint32_t field_id, ProtoVar
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 9: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -558,10 +596,11 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_bool(6, this->is_status_binary_sensor);
|
||||
buffer.encode_bool(7, this->disabled_by_default);
|
||||
buffer.encode_string(8, this->icon);
|
||||
buffer.encode_enum<enums::EntityCategory>(9, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesBinarySensorResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -595,6 +634,10 @@ void ListEntitiesBinarySensorResponse::dump_to(std::string &out) const {
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -629,7 +672,7 @@ void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void BinarySensorStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("BinarySensorStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -664,6 +707,10 @@ bool ListEntitiesCoverResponse::decode_varint(uint32_t field_id, ProtoVarInt val
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 11: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -715,10 +762,11 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(8, this->device_class);
|
||||
buffer.encode_bool(9, this->disabled_by_default);
|
||||
buffer.encode_string(10, this->icon);
|
||||
buffer.encode_enum<enums::EntityCategory>(11, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesCoverResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesCoverResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -760,6 +808,10 @@ void ListEntitiesCoverResponse::dump_to(std::string &out) const {
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -804,7 +856,7 @@ void CoverStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void CoverStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("CoverStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -887,7 +939,7 @@ void CoverCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void CoverCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("CoverCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -948,6 +1000,10 @@ bool ListEntitiesFanResponse::decode_varint(uint32_t field_id, ProtoVarInt value
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 11: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -995,10 +1051,11 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_int32(8, this->supported_speed_count);
|
||||
buffer.encode_bool(9, this->disabled_by_default);
|
||||
buffer.encode_string(10, this->icon);
|
||||
buffer.encode_enum<enums::EntityCategory>(11, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesFanResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesFanResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -1041,6 +1098,10 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const {
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -1090,7 +1151,7 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void FanStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("FanStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -1191,7 +1252,7 @@ void FanCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void FanCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("FanCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -1267,6 +1328,10 @@ bool ListEntitiesLightResponse::decode_varint(uint32_t field_id, ProtoVarInt val
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 15: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -1334,10 +1399,11 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
buffer.encode_bool(13, this->disabled_by_default);
|
||||
buffer.encode_string(14, this->icon);
|
||||
buffer.encode_enum<enums::EntityCategory>(15, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesLightResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesLightResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -1401,6 +1467,10 @@ void ListEntitiesLightResponse::dump_to(std::string &out) const {
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -1491,7 +1561,7 @@ void LightStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void LightStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("LightStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -1714,7 +1784,7 @@ void LightCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void LightCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("LightCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -1860,6 +1930,10 @@ bool ListEntitiesSensorResponse::decode_varint(uint32_t field_id, ProtoVarInt va
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 13: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -1917,10 +1991,11 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_enum<enums::SensorStateClass>(10, this->state_class);
|
||||
buffer.encode_enum<enums::SensorLastResetType>(11, this->legacy_last_reset_type);
|
||||
buffer.encode_bool(12, this->disabled_by_default);
|
||||
buffer.encode_enum<enums::EntityCategory>(13, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesSensorResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesSensorResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -1971,6 +2046,10 @@ void ListEntitiesSensorResponse::dump_to(std::string &out) const {
|
||||
out.append(" disabled_by_default: ");
|
||||
out.append(YESNO(this->disabled_by_default));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -2005,7 +2084,7 @@ void SensorStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SensorStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SensorStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -2033,6 +2112,10 @@ bool ListEntitiesSwitchResponse::decode_varint(uint32_t field_id, ProtoVarInt va
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 8: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -2077,10 +2160,11 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(5, this->icon);
|
||||
buffer.encode_bool(6, this->assumed_state);
|
||||
buffer.encode_bool(7, this->disabled_by_default);
|
||||
buffer.encode_enum<enums::EntityCategory>(8, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesSwitchResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesSwitchResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -2110,6 +2194,10 @@ void ListEntitiesSwitchResponse::dump_to(std::string &out) const {
|
||||
out.append(" disabled_by_default: ");
|
||||
out.append(YESNO(this->disabled_by_default));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -2139,7 +2227,7 @@ void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SwitchStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SwitchStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -2178,7 +2266,7 @@ void SwitchCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SwitchCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SwitchCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -2197,6 +2285,10 @@ bool ListEntitiesTextSensorResponse::decode_varint(uint32_t field_id, ProtoVarIn
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 7: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -2240,10 +2332,11 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(4, this->unique_id);
|
||||
buffer.encode_string(5, this->icon);
|
||||
buffer.encode_bool(6, this->disabled_by_default);
|
||||
buffer.encode_enum<enums::EntityCategory>(7, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesTextSensorResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesTextSensorResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -2269,6 +2362,10 @@ void ListEntitiesTextSensorResponse::dump_to(std::string &out) const {
|
||||
out.append(" disabled_by_default: ");
|
||||
out.append(YESNO(this->disabled_by_default));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -2309,7 +2406,7 @@ void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void TextSensorStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("TextSensorStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -2346,7 +2443,7 @@ void SubscribeLogsRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SubscribeLogsRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SubscribeLogsRequest {\n");
|
||||
out.append(" level: ");
|
||||
out.append(proto_enum_to_string<enums::LogLevel>(this->level));
|
||||
@@ -2389,7 +2486,7 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SubscribeLogsResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SubscribeLogsResponse {\n");
|
||||
out.append(" level: ");
|
||||
out.append(proto_enum_to_string<enums::LogLevel>(this->level));
|
||||
@@ -2431,7 +2528,7 @@ void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void HomeassistantServiceMap::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("HomeassistantServiceMap {\n");
|
||||
out.append(" key: ");
|
||||
out.append("'").append(this->key).append("'");
|
||||
@@ -2490,7 +2587,7 @@ void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void HomeassistantServiceResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("HomeassistantServiceResponse {\n");
|
||||
out.append(" service: ");
|
||||
out.append("'").append(this->service).append("'");
|
||||
@@ -2546,7 +2643,7 @@ void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SubscribeHomeAssistantStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SubscribeHomeAssistantStateResponse {\n");
|
||||
out.append(" entity_id: ");
|
||||
out.append("'").append(this->entity_id).append("'");
|
||||
@@ -2583,7 +2680,7 @@ void HomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void HomeAssistantStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("HomeAssistantStateResponse {\n");
|
||||
out.append(" entity_id: ");
|
||||
out.append("'").append(this->entity_id).append("'");
|
||||
@@ -2616,7 +2713,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
|
||||
void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); }
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void GetTimeResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("GetTimeResponse {\n");
|
||||
out.append(" epoch_seconds: ");
|
||||
sprintf(buffer, "%u", this->epoch_seconds);
|
||||
@@ -2651,7 +2748,7 @@ void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesServicesArgument::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesServicesArgument {\n");
|
||||
out.append(" name: ");
|
||||
out.append("'").append(this->name).append("'");
|
||||
@@ -2696,7 +2793,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesServicesResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesServicesResponse {\n");
|
||||
out.append(" name: ");
|
||||
out.append("'").append(this->name).append("'");
|
||||
@@ -2790,7 +2887,7 @@ void ExecuteServiceArgument::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ExecuteServiceArgument::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ExecuteServiceArgument {\n");
|
||||
out.append(" bool_: ");
|
||||
out.append(YESNO(this->bool_));
|
||||
@@ -2871,7 +2968,7 @@ void ExecuteServiceRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ExecuteServiceRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ExecuteServiceRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -2892,6 +2989,10 @@ bool ListEntitiesCameraResponse::decode_varint(uint32_t field_id, ProtoVarInt va
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 7: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -2935,10 +3036,11 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(4, this->unique_id);
|
||||
buffer.encode_bool(5, this->disabled_by_default);
|
||||
buffer.encode_string(6, this->icon);
|
||||
buffer.encode_enum<enums::EntityCategory>(7, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesCameraResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesCameraResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -2964,6 +3066,10 @@ void ListEntitiesCameraResponse::dump_to(std::string &out) const {
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -3004,7 +3110,7 @@ void CameraImageResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void CameraImageResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("CameraImageResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -3041,7 +3147,7 @@ void CameraImageRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void CameraImageRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("CameraImageRequest {\n");
|
||||
out.append(" single: ");
|
||||
out.append(YESNO(this->single));
|
||||
@@ -3091,6 +3197,10 @@ bool ListEntitiesClimateResponse::decode_varint(uint32_t field_id, ProtoVarInt v
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 20: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -3179,10 +3289,11 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
buffer.encode_bool(18, this->disabled_by_default);
|
||||
buffer.encode_string(19, this->icon);
|
||||
buffer.encode_enum<enums::EntityCategory>(20, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesClimateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesClimateResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -3275,6 +3386,10 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const {
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -3365,7 +3480,7 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ClimateStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ClimateStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -3553,7 +3668,7 @@ void ClimateCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ClimateCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ClimateCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -3651,6 +3766,14 @@ bool ListEntitiesNumberResponse::decode_varint(uint32_t field_id, ProtoVarInt va
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 10: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
case 12: {
|
||||
this->mode = value.as_enum<enums::NumberMode>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -3673,6 +3796,10 @@ bool ListEntitiesNumberResponse::decode_length(uint32_t field_id, ProtoLengthDel
|
||||
this->icon = value.as_string();
|
||||
return true;
|
||||
}
|
||||
case 11: {
|
||||
this->unit_of_measurement = value.as_string();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -3709,10 +3836,13 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_float(7, this->max_value);
|
||||
buffer.encode_float(8, this->step);
|
||||
buffer.encode_bool(9, this->disabled_by_default);
|
||||
buffer.encode_enum<enums::EntityCategory>(10, this->entity_category);
|
||||
buffer.encode_string(11, this->unit_of_measurement);
|
||||
buffer.encode_enum<enums::NumberMode>(12, this->mode);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesNumberResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesNumberResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -3753,6 +3883,18 @@ void ListEntitiesNumberResponse::dump_to(std::string &out) const {
|
||||
out.append(" disabled_by_default: ");
|
||||
out.append(YESNO(this->disabled_by_default));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" unit_of_measurement: ");
|
||||
out.append("'").append(this->unit_of_measurement).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" mode: ");
|
||||
out.append(proto_enum_to_string<enums::NumberMode>(this->mode));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -3787,7 +3929,7 @@ void NumberStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void NumberStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("NumberStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -3825,7 +3967,7 @@ void NumberCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void NumberCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("NumberCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -3845,6 +3987,10 @@ bool ListEntitiesSelectResponse::decode_varint(uint32_t field_id, ProtoVarInt va
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 8: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -3895,10 +4041,11 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(6, it, true);
|
||||
}
|
||||
buffer.encode_bool(7, this->disabled_by_default);
|
||||
buffer.encode_enum<enums::EntityCategory>(8, this->entity_category);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesSelectResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("ListEntitiesSelectResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
@@ -3930,6 +4077,10 @@ void ListEntitiesSelectResponse::dump_to(std::string &out) const {
|
||||
out.append(" disabled_by_default: ");
|
||||
out.append(YESNO(this->disabled_by_default));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
@@ -3970,7 +4121,7 @@ void SelectStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SelectStateResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SelectStateResponse {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -4013,7 +4164,7 @@ void SelectCommandRequest::encode(ProtoWriteBuffer buffer) const {
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void SelectCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
__attribute__((unused)) char buffer[64];
|
||||
out.append("SelectCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
@@ -4026,6 +4177,127 @@ void SelectCommandRequest::dump_to(std::string &out) const {
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
bool ListEntitiesButtonResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
|
||||
switch (field_id) {
|
||||
case 6: {
|
||||
this->disabled_by_default = value.as_bool();
|
||||
return true;
|
||||
}
|
||||
case 7: {
|
||||
this->entity_category = value.as_enum<enums::EntityCategory>();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ListEntitiesButtonResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
|
||||
switch (field_id) {
|
||||
case 1: {
|
||||
this->object_id = value.as_string();
|
||||
return true;
|
||||
}
|
||||
case 3: {
|
||||
this->name = value.as_string();
|
||||
return true;
|
||||
}
|
||||
case 4: {
|
||||
this->unique_id = value.as_string();
|
||||
return true;
|
||||
}
|
||||
case 5: {
|
||||
this->icon = value.as_string();
|
||||
return true;
|
||||
}
|
||||
case 8: {
|
||||
this->device_class = value.as_string();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ListEntitiesButtonResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
|
||||
switch (field_id) {
|
||||
case 2: {
|
||||
this->key = value.as_fixed32();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void ListEntitiesButtonResponse::encode(ProtoWriteBuffer buffer) const {
|
||||
buffer.encode_string(1, this->object_id);
|
||||
buffer.encode_fixed32(2, this->key);
|
||||
buffer.encode_string(3, this->name);
|
||||
buffer.encode_string(4, this->unique_id);
|
||||
buffer.encode_string(5, this->icon);
|
||||
buffer.encode_bool(6, this->disabled_by_default);
|
||||
buffer.encode_enum<enums::EntityCategory>(7, this->entity_category);
|
||||
buffer.encode_string(8, this->device_class);
|
||||
}
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ListEntitiesButtonResponse::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
out.append("ListEntitiesButtonResponse {\n");
|
||||
out.append(" object_id: ");
|
||||
out.append("'").append(this->object_id).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
out.append(buffer);
|
||||
out.append("\n");
|
||||
|
||||
out.append(" name: ");
|
||||
out.append("'").append(this->name).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" unique_id: ");
|
||||
out.append("'").append(this->unique_id).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" icon: ");
|
||||
out.append("'").append(this->icon).append("'");
|
||||
out.append("\n");
|
||||
|
||||
out.append(" disabled_by_default: ");
|
||||
out.append(YESNO(this->disabled_by_default));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" entity_category: ");
|
||||
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
|
||||
out.append("\n");
|
||||
|
||||
out.append(" device_class: ");
|
||||
out.append("'").append(this->device_class).append("'");
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
bool ButtonCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) {
|
||||
switch (field_id) {
|
||||
case 1: {
|
||||
this->key = value.as_fixed32();
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void ButtonCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); }
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void ButtonCommandRequest::dump_to(std::string &out) const {
|
||||
char buffer[64];
|
||||
out.append("ButtonCommandRequest {\n");
|
||||
out.append(" key: ");
|
||||
sprintf(buffer, "%u", this->key);
|
||||
out.append(buffer);
|
||||
out.append("\n");
|
||||
out.append("}");
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace api
|
||||
} // namespace esphome
|
||||
|
||||
@@ -9,6 +9,11 @@ namespace api {
|
||||
|
||||
namespace enums {
|
||||
|
||||
enum EntityCategory : uint32_t {
|
||||
ENTITY_CATEGORY_NONE = 0,
|
||||
ENTITY_CATEGORY_CONFIG = 1,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC = 2,
|
||||
};
|
||||
enum LegacyCoverState : uint32_t {
|
||||
LEGACY_COVER_STATE_OPEN = 0,
|
||||
LEGACY_COVER_STATE_CLOSED = 1,
|
||||
@@ -118,6 +123,11 @@ enum ClimatePreset : uint32_t {
|
||||
CLIMATE_PRESET_SLEEP = 6,
|
||||
CLIMATE_PRESET_ACTIVITY = 7,
|
||||
};
|
||||
enum NumberMode : uint32_t {
|
||||
NUMBER_MODE_AUTO = 0,
|
||||
NUMBER_MODE_BOX = 1,
|
||||
NUMBER_MODE_SLIDER = 2,
|
||||
};
|
||||
|
||||
} // namespace enums
|
||||
|
||||
@@ -224,6 +234,7 @@ class DeviceInfoResponse : public ProtoMessage {
|
||||
bool has_deep_sleep{false};
|
||||
std::string project_name{};
|
||||
std::string project_version{};
|
||||
uint32_t webserver_port{0};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -270,6 +281,7 @@ class ListEntitiesBinarySensorResponse : public ProtoMessage {
|
||||
bool is_status_binary_sensor{false};
|
||||
bool disabled_by_default{false};
|
||||
std::string icon{};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -306,6 +318,7 @@ class ListEntitiesCoverResponse : public ProtoMessage {
|
||||
std::string device_class{};
|
||||
bool disabled_by_default{false};
|
||||
std::string icon{};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -363,6 +376,7 @@ class ListEntitiesFanResponse : public ProtoMessage {
|
||||
int32_t supported_speed_count{0};
|
||||
bool disabled_by_default{false};
|
||||
std::string icon{};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -428,6 +442,7 @@ class ListEntitiesLightResponse : public ProtoMessage {
|
||||
std::vector<std::string> effects{};
|
||||
bool disabled_by_default{false};
|
||||
std::string icon{};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -516,6 +531,7 @@ class ListEntitiesSensorResponse : public ProtoMessage {
|
||||
enums::SensorStateClass state_class{};
|
||||
enums::SensorLastResetType legacy_last_reset_type{};
|
||||
bool disabled_by_default{false};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -549,6 +565,7 @@ class ListEntitiesSwitchResponse : public ProtoMessage {
|
||||
std::string icon{};
|
||||
bool assumed_state{false};
|
||||
bool disabled_by_default{false};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -593,6 +610,7 @@ class ListEntitiesTextSensorResponse : public ProtoMessage {
|
||||
std::string unique_id{};
|
||||
std::string icon{};
|
||||
bool disabled_by_default{false};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -804,6 +822,7 @@ class ListEntitiesCameraResponse : public ProtoMessage {
|
||||
std::string unique_id{};
|
||||
bool disabled_by_default{false};
|
||||
std::string icon{};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -862,6 +881,7 @@ class ListEntitiesClimateResponse : public ProtoMessage {
|
||||
std::vector<std::string> supported_custom_presets{};
|
||||
bool disabled_by_default{false};
|
||||
std::string icon{};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -941,6 +961,9 @@ class ListEntitiesNumberResponse : public ProtoMessage {
|
||||
float max_value{0.0f};
|
||||
float step{0.0f};
|
||||
bool disabled_by_default{false};
|
||||
enums::EntityCategory entity_category{};
|
||||
std::string unit_of_measurement{};
|
||||
enums::NumberMode mode{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -986,6 +1009,7 @@ class ListEntitiesSelectResponse : public ProtoMessage {
|
||||
std::string icon{};
|
||||
std::vector<std::string> options{};
|
||||
bool disabled_by_default{false};
|
||||
enums::EntityCategory entity_category{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
@@ -1024,6 +1048,37 @@ class SelectCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
};
|
||||
class ListEntitiesButtonResponse : public ProtoMessage {
|
||||
public:
|
||||
std::string object_id{};
|
||||
uint32_t key{0};
|
||||
std::string name{};
|
||||
std::string unique_id{};
|
||||
std::string icon{};
|
||||
bool disabled_by_default{false};
|
||||
enums::EntityCategory entity_category{};
|
||||
std::string device_class{};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
class ButtonCommandRequest : public ProtoMessage {
|
||||
public:
|
||||
uint32_t key{0};
|
||||
void encode(ProtoWriteBuffer buffer) const override;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void dump_to(std::string &out) const override;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
} // namespace esphome
|
||||
|
||||
@@ -282,6 +282,16 @@ bool APIServerConnectionBase::send_select_state_response(const SelectStateRespon
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool APIServerConnectionBase::send_list_entities_button_response(const ListEntitiesButtonResponse &msg) {
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
ESP_LOGVV(TAG, "send_list_entities_button_response: %s", msg.dump().c_str());
|
||||
#endif
|
||||
return this->send_message_<ListEntitiesButtonResponse>(msg, 61);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
#endif
|
||||
bool APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) {
|
||||
switch (msg_type) {
|
||||
case 1: {
|
||||
@@ -513,6 +523,17 @@ bool APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
||||
ESP_LOGVV(TAG, "on_select_command_request: %s", msg.dump().c_str());
|
||||
#endif
|
||||
this->on_select_command_request(msg);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case 62: {
|
||||
#ifdef USE_BUTTON
|
||||
ButtonCommandRequest msg;
|
||||
msg.decode(msg_data, msg_size);
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
ESP_LOGVV(TAG, "on_button_command_request: %s", msg.dump().c_str());
|
||||
#endif
|
||||
this->on_button_command_request(msg);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
@@ -737,6 +758,19 @@ void APIServerConnection::on_select_command_request(const SelectCommandRequest &
|
||||
this->select_command(msg);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
void APIServerConnection::on_button_command_request(const ButtonCommandRequest &msg) {
|
||||
if (!this->is_connection_setup()) {
|
||||
this->on_no_setup_connection();
|
||||
return;
|
||||
}
|
||||
if (!this->is_authenticated()) {
|
||||
this->on_unauthenticated_access();
|
||||
return;
|
||||
}
|
||||
this->button_command(msg);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace api
|
||||
} // namespace esphome
|
||||
|
||||
@@ -129,6 +129,12 @@ class APIServerConnectionBase : public ProtoService {
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
virtual void on_select_command_request(const SelectCommandRequest &value){};
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool send_list_entities_button_response(const ListEntitiesButtonResponse &msg);
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
virtual void on_button_command_request(const ButtonCommandRequest &value){};
|
||||
#endif
|
||||
protected:
|
||||
bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) override;
|
||||
@@ -171,6 +177,9 @@ class APIServerConnection : public APIServerConnectionBase {
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
virtual void select_command(const SelectCommandRequest &msg) = 0;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
virtual void button_command(const ButtonCommandRequest &msg) = 0;
|
||||
#endif
|
||||
protected:
|
||||
void on_hello_request(const HelloRequest &msg) override;
|
||||
@@ -209,6 +218,9 @@ class APIServerConnection : public APIServerConnectionBase {
|
||||
#ifdef USE_SELECT
|
||||
void on_select_command_request(const SelectCommandRequest &msg) override;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
void on_button_command_request(const ButtonCommandRequest &msg) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -77,7 +77,7 @@ void APIServer::setup() {
|
||||
this->last_connected_ = millis();
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
if (esp32_camera::global_esp32_camera != nullptr) {
|
||||
if (esp32_camera::global_esp32_camera != nullptr && !esp32_camera::global_esp32_camera->is_internal()) {
|
||||
esp32_camera::global_esp32_camera->add_image_callback(
|
||||
[this](const std::shared_ptr<esp32_camera::CameraImage> &image) {
|
||||
for (auto &c : this->clients_)
|
||||
|
||||
@@ -27,6 +27,9 @@ bool ListEntitiesIterator::on_sensor(sensor::Sensor *sensor) { return this->clie
|
||||
#ifdef USE_SWITCH
|
||||
bool ListEntitiesIterator::on_switch(switch_::Switch *a_switch) { return this->client_->send_switch_info(a_switch); }
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool ListEntitiesIterator::on_button(button::Button *button) { return this->client_->send_button_info(button); }
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
bool ListEntitiesIterator::on_text_sensor(text_sensor::TextSensor *text_sensor) {
|
||||
return this->client_->send_text_sensor_info(text_sensor);
|
||||
|
||||
@@ -30,6 +30,9 @@ class ListEntitiesIterator : public ComponentIterator {
|
||||
#ifdef USE_SWITCH
|
||||
bool on_switch(switch_::Switch *a_switch) override;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool on_button(button::Button *button) override;
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
bool on_text_sensor(text_sensor::TextSensor *text_sensor) override;
|
||||
#endif
|
||||
|
||||
@@ -31,6 +31,9 @@ class InitialStateIterator : public ComponentIterator {
|
||||
#ifdef USE_SWITCH
|
||||
bool on_switch(switch_::Switch *a_switch) override;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
bool on_button(button::Button *button) override { return true; };
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
bool on_text_sensor(text_sensor::TextSensor *text_sensor) override;
|
||||
#endif
|
||||
|
||||
@@ -116,6 +116,21 @@ void ComponentIterator::advance() {
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
case IteratorState::BUTTON:
|
||||
if (this->at_ >= App.get_buttons().size()) {
|
||||
advance_platform = true;
|
||||
} else {
|
||||
auto *button = App.get_buttons()[this->at_];
|
||||
if (button->is_internal()) {
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
success = this->on_button(button);
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
case IteratorState::TEXT_SENSOR:
|
||||
if (this->at_ >= App.get_text_sensors().size()) {
|
||||
|
||||
@@ -38,6 +38,9 @@ class ComponentIterator {
|
||||
#ifdef USE_SWITCH
|
||||
virtual bool on_switch(switch_::Switch *a_switch) = 0;
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
virtual bool on_button(button::Button *button) = 0;
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor) = 0;
|
||||
#endif
|
||||
@@ -78,6 +81,9 @@ class ComponentIterator {
|
||||
#ifdef USE_SWITCH
|
||||
SWITCH,
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
BUTTON,
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
TEXT_SENSOR,
|
||||
#endif
|
||||
|
||||
@@ -12,6 +12,7 @@ from esphome.const import (
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PERCENT,
|
||||
@@ -49,12 +50,14 @@ CONFIG_SCHEMA = (
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_BATTERY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_VOLT,
|
||||
accuracy_decimals=3,
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -17,6 +17,7 @@ from esphome.const import (
|
||||
DEVICE_CLASS_POWER_FACTOR,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
ICON_LIGHTBULB,
|
||||
ICON_CURRENT_AC,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
@@ -125,6 +126,7 @@ CONFIG_SCHEMA = (
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Required(CONF_LINE_FREQUENCY): cv.enum(LINE_FREQS, upper=True),
|
||||
cv.Optional(CONF_CURRENT_PHASES, default="3"): cv.enum(
|
||||
|
||||
@@ -13,6 +13,7 @@ from esphome.const import (
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_LUX,
|
||||
@@ -51,6 +52,7 @@ CONFIG_SCHEMA = (
|
||||
accuracy_decimals=3,
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_MOISTURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
|
||||
@@ -79,6 +79,9 @@ void BH1750Sensor::read_data_() {
|
||||
|
||||
float lx = float(raw_value) / 1.2f;
|
||||
lx *= 69.0f / this->measurement_duration_;
|
||||
if (this->resolution_ == BH1750_RESOLUTION_0P5_LX) {
|
||||
lx /= 2.0f;
|
||||
}
|
||||
ESP_LOGD(TAG, "'%s': Got illuminance=%.1flx", this->get_name().c_str(), lx);
|
||||
this->publish_state(lx);
|
||||
this->status_clear_warning();
|
||||
|
||||
@@ -44,9 +44,11 @@ from esphome.const import (
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_PRESENCE,
|
||||
DEVICE_CLASS_PROBLEM,
|
||||
DEVICE_CLASS_RUNNING,
|
||||
DEVICE_CLASS_SAFETY,
|
||||
DEVICE_CLASS_SMOKE,
|
||||
DEVICE_CLASS_SOUND,
|
||||
DEVICE_CLASS_TAMPER,
|
||||
DEVICE_CLASS_UPDATE,
|
||||
DEVICE_CLASS_VIBRATION,
|
||||
DEVICE_CLASS_WINDOW,
|
||||
@@ -76,9 +78,11 @@ DEVICE_CLASSES = [
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_PRESENCE,
|
||||
DEVICE_CLASS_PROBLEM,
|
||||
DEVICE_CLASS_RUNNING,
|
||||
DEVICE_CLASS_SAFETY,
|
||||
DEVICE_CLASS_SMOKE,
|
||||
DEVICE_CLASS_SOUND,
|
||||
DEVICE_CLASS_TAMPER,
|
||||
DEVICE_CLASS_UPDATE,
|
||||
DEVICE_CLASS_VIBRATION,
|
||||
DEVICE_CLASS_WINDOW,
|
||||
@@ -313,6 +317,7 @@ def validate_multi_click_timing(value):
|
||||
|
||||
device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_")
|
||||
|
||||
|
||||
BINARY_SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(BinarySensor),
|
||||
|
||||
@@ -388,6 +388,15 @@ BLEDescriptor *BLECharacteristic::get_descriptor(uint16_t uuid) {
|
||||
return this->get_descriptor(espbt::ESPBTUUID::from_uint16(uuid));
|
||||
}
|
||||
|
||||
void BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size) {
|
||||
auto client = this->service->client;
|
||||
auto status = esp_ble_gattc_write_char(client->gattc_if, client->conn_id, this->handle, new_val_size, new_val,
|
||||
ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
|
||||
if (status) {
|
||||
ESP_LOGW(TAG, "Error sending write value to BLE gattc server, status=%d", status);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ble_client
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ class BLECharacteristic {
|
||||
void parse_descriptors();
|
||||
BLEDescriptor *get_descriptor(espbt::ESPBTUUID uuid);
|
||||
BLEDescriptor *get_descriptor(uint16_t uuid);
|
||||
|
||||
void write_value(uint8_t *new_val, int16_t new_val_size);
|
||||
BLEService *service;
|
||||
};
|
||||
|
||||
|
||||
67
esphome/components/ble_client/output/__init__.py
Normal file
67
esphome/components/ble_client/output/__init__.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import output, ble_client, esp32_ble_tracker
|
||||
from esphome.const import CONF_ID, CONF_SERVICE_UUID
|
||||
from .. import ble_client_ns
|
||||
|
||||
|
||||
DEPENDENCIES = ["ble_client"]
|
||||
|
||||
CONF_CHARACTERISTIC_UUID = "characteristic_uuid"
|
||||
|
||||
BLEBinaryOutput = ble_client_ns.class_(
|
||||
"BLEBinaryOutput", output.BinaryOutput, ble_client.BLEClientNode, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
output.BINARY_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(BLEBinaryOutput),
|
||||
cv.Required(CONF_SERVICE_UUID): esp32_ble_tracker.bt_uuid,
|
||||
cv.Required(CONF_CHARACTERISTIC_UUID): esp32_ble_tracker.bt_uuid,
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(ble_client.BLE_CLIENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
if len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid16_format):
|
||||
cg.add(
|
||||
var.set_service_uuid16(esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]))
|
||||
)
|
||||
elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid32_format):
|
||||
cg.add(
|
||||
var.set_service_uuid32(esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]))
|
||||
)
|
||||
elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid128_format):
|
||||
uuid128 = esp32_ble_tracker.as_reversed_hex_array(config[CONF_SERVICE_UUID])
|
||||
cg.add(var.set_service_uuid128(uuid128))
|
||||
|
||||
if len(config[CONF_CHARACTERISTIC_UUID]) == len(esp32_ble_tracker.bt_uuid16_format):
|
||||
cg.add(
|
||||
var.set_char_uuid16(
|
||||
esp32_ble_tracker.as_hex(config[CONF_CHARACTERISTIC_UUID])
|
||||
)
|
||||
)
|
||||
elif len(config[CONF_CHARACTERISTIC_UUID]) == len(
|
||||
esp32_ble_tracker.bt_uuid32_format
|
||||
):
|
||||
cg.add(
|
||||
var.set_char_uuid32(
|
||||
esp32_ble_tracker.as_hex(config[CONF_CHARACTERISTIC_UUID])
|
||||
)
|
||||
)
|
||||
elif len(config[CONF_CHARACTERISTIC_UUID]) == len(
|
||||
esp32_ble_tracker.bt_uuid128_format
|
||||
):
|
||||
uuid128 = esp32_ble_tracker.as_reversed_hex_array(
|
||||
config[CONF_CHARACTERISTIC_UUID]
|
||||
)
|
||||
cg.add(var.set_char_uuid128(uuid128))
|
||||
|
||||
yield output.register_output(var, config)
|
||||
yield ble_client.register_ble_node(var, config)
|
||||
yield cg.register_component(var, config)
|
||||
71
esphome/components/ble_client/output/ble_binary_output.cpp
Normal file
71
esphome/components/ble_client/output/ble_binary_output.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "ble_binary_output.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
namespace esphome {
|
||||
namespace ble_client {
|
||||
|
||||
static const char *const TAG = "ble_binary_output";
|
||||
|
||||
void BLEBinaryOutput::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "BLE Binary Output:");
|
||||
ESP_LOGCONFIG(TAG, " MAC address : %s", this->parent_->address_str().c_str());
|
||||
ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
|
||||
ESP_LOGCONFIG(TAG, " Characteristic UUID: %s", this->char_uuid_.to_string().c_str());
|
||||
LOG_BINARY_OUTPUT(this);
|
||||
}
|
||||
|
||||
void BLEBinaryOutput::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
|
||||
esp_ble_gattc_cb_param_t *param) {
|
||||
switch (event) {
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
this->client_state_ = espbt::ClientState::ESTABLISHED;
|
||||
ESP_LOGW(TAG, "[%s] Connected successfully!", this->char_uuid_.to_string().c_str());
|
||||
break;
|
||||
case ESP_GATTC_DISCONNECT_EVT:
|
||||
ESP_LOGW(TAG, "[%s] Disconnected", this->char_uuid_.to_string().c_str());
|
||||
this->client_state_ = espbt::ClientState::IDLE;
|
||||
break;
|
||||
case ESP_GATTC_WRITE_CHAR_EVT: {
|
||||
if (param->write.status == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
|
||||
if (chr == nullptr) {
|
||||
ESP_LOGW(TAG, "[%s] Characteristic not found.", this->char_uuid_.to_string().c_str());
|
||||
break;
|
||||
}
|
||||
if (param->write.handle == chr->handle) {
|
||||
ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_string().c_str(), param->write.status);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BLEBinaryOutput::write_state(bool state) {
|
||||
if (this->client_state_ != espbt::ClientState::ESTABLISHED) {
|
||||
ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
|
||||
this->char_uuid_.to_string().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
auto chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
|
||||
if (chr == nullptr) {
|
||||
ESP_LOGW(TAG, "[%s] Characteristic not found. State update can not be written.",
|
||||
this->char_uuid_.to_string().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t state_as_uint = (uint8_t) state;
|
||||
ESP_LOGV(TAG, "[%s] Write State: %d", this->char_uuid_.to_string().c_str(), state_as_uint);
|
||||
chr->write_value(&state_as_uint, sizeof(state_as_uint));
|
||||
}
|
||||
|
||||
} // namespace ble_client
|
||||
} // namespace esphome
|
||||
#endif
|
||||
39
esphome/components/ble_client/output/ble_binary_output.h
Normal file
39
esphome/components/ble_client/output/ble_binary_output.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/ble_client/ble_client.h"
|
||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#include <esp_gattc_api.h>
|
||||
namespace esphome {
|
||||
namespace ble_client {
|
||||
|
||||
namespace espbt = esphome::esp32_ble_tracker;
|
||||
|
||||
class BLEBinaryOutput : public output::BinaryOutput, public BLEClientNode, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void loop() override {}
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
|
||||
void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
|
||||
void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
|
||||
void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
|
||||
void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
|
||||
void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
|
||||
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
|
||||
esp_ble_gattc_cb_param_t *param) override;
|
||||
|
||||
protected:
|
||||
void write_state(bool state) override;
|
||||
espbt::ESPBTUUID service_uuid_;
|
||||
espbt::ESPBTUUID char_uuid_;
|
||||
espbt::ClientState client_state_;
|
||||
};
|
||||
|
||||
} // namespace ble_client
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
@@ -67,7 +67,7 @@ async def to_code(config):
|
||||
var.set_service_uuid32(esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]))
|
||||
)
|
||||
elif len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid128_format):
|
||||
uuid128 = esp32_ble_tracker.as_hex_array(config[CONF_SERVICE_UUID])
|
||||
uuid128 = esp32_ble_tracker.as_reversed_hex_array(config[CONF_SERVICE_UUID])
|
||||
cg.add(var.set_service_uuid128(uuid128))
|
||||
|
||||
if len(config[CONF_CHARACTERISTIC_UUID]) == len(esp32_ble_tracker.bt_uuid16_format):
|
||||
@@ -87,7 +87,9 @@ async def to_code(config):
|
||||
elif len(config[CONF_CHARACTERISTIC_UUID]) == len(
|
||||
esp32_ble_tracker.bt_uuid128_format
|
||||
):
|
||||
uuid128 = esp32_ble_tracker.as_hex_array(config[CONF_CHARACTERISTIC_UUID])
|
||||
uuid128 = esp32_ble_tracker.as_reversed_hex_array(
|
||||
config[CONF_CHARACTERISTIC_UUID]
|
||||
)
|
||||
cg.add(var.set_char_uuid128(uuid128))
|
||||
|
||||
if CONF_DESCRIPTOR_UUID in config:
|
||||
@@ -108,7 +110,9 @@ async def to_code(config):
|
||||
elif len(config[CONF_DESCRIPTOR_UUID]) == len(
|
||||
esp32_ble_tracker.bt_uuid128_format
|
||||
):
|
||||
uuid128 = esp32_ble_tracker.as_hex_array(config[CONF_DESCRIPTOR_UUID])
|
||||
uuid128 = esp32_ble_tracker.as_reversed_hex_array(
|
||||
config[CONF_DESCRIPTOR_UUID]
|
||||
)
|
||||
cg.add(var.set_descr_uuid128(uuid128))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
|
||||
@@ -33,6 +33,7 @@ static const uint8_t BME280_REGISTER_CONTROLHUMID = 0xF2;
|
||||
static const uint8_t BME280_REGISTER_STATUS = 0xF3;
|
||||
static const uint8_t BME280_REGISTER_CONTROL = 0xF4;
|
||||
static const uint8_t BME280_REGISTER_CONFIG = 0xF5;
|
||||
static const uint8_t BME280_REGISTER_MEASUREMENTS = 0xF7;
|
||||
static const uint8_t BME280_REGISTER_PRESSUREDATA = 0xF7;
|
||||
static const uint8_t BME280_REGISTER_TEMPDATA = 0xFA;
|
||||
static const uint8_t BME280_REGISTER_HUMIDDATA = 0xFD;
|
||||
@@ -178,23 +179,29 @@ void BME280Component::update() {
|
||||
return;
|
||||
}
|
||||
|
||||
float meas_time = 1.5;
|
||||
float meas_time = 1.5f;
|
||||
meas_time += 2.3f * oversampling_to_time(this->temperature_oversampling_);
|
||||
meas_time += 2.3f * oversampling_to_time(this->pressure_oversampling_) + 0.575f;
|
||||
meas_time += 2.3f * oversampling_to_time(this->humidity_oversampling_) + 0.575f;
|
||||
|
||||
this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
|
||||
uint8_t data[8];
|
||||
if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
|
||||
ESP_LOGW(TAG, "Error reading registers.");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
int32_t t_fine = 0;
|
||||
float temperature = this->read_temperature_(&t_fine);
|
||||
float temperature = this->read_temperature_(data, &t_fine);
|
||||
if (std::isnan(temperature)) {
|
||||
ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values.");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
float pressure = this->read_pressure_(t_fine);
|
||||
float humidity = this->read_humidity_(t_fine);
|
||||
float pressure = this->read_pressure_(data, t_fine);
|
||||
float humidity = this->read_humidity_(data, t_fine);
|
||||
|
||||
ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity);
|
||||
ESP_LOGV(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity);
|
||||
if (this->temperature_sensor_ != nullptr)
|
||||
this->temperature_sensor_->publish_state(temperature);
|
||||
if (this->pressure_sensor_ != nullptr)
|
||||
@@ -204,11 +211,8 @@ void BME280Component::update() {
|
||||
this->status_clear_warning();
|
||||
});
|
||||
}
|
||||
float BME280Component::read_temperature_(int32_t *t_fine) {
|
||||
uint8_t data[3];
|
||||
if (!this->read_bytes(BME280_REGISTER_TEMPDATA, data, 3))
|
||||
return NAN;
|
||||
int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
|
||||
float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) {
|
||||
int32_t adc = ((data[3] & 0xFF) << 16) | ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
|
||||
adc >>= 4;
|
||||
if (adc == 0x80000)
|
||||
// temperature was disabled
|
||||
@@ -226,10 +230,7 @@ float BME280Component::read_temperature_(int32_t *t_fine) {
|
||||
return temperature / 100.0f;
|
||||
}
|
||||
|
||||
float BME280Component::read_pressure_(int32_t t_fine) {
|
||||
uint8_t data[3];
|
||||
if (!this->read_bytes(BME280_REGISTER_PRESSUREDATA, data, 3))
|
||||
return NAN;
|
||||
float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
|
||||
int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
|
||||
adc >>= 4;
|
||||
if (adc == 0x80000)
|
||||
@@ -265,9 +266,9 @@ float BME280Component::read_pressure_(int32_t t_fine) {
|
||||
return (p / 256.0f) / 100.0f;
|
||||
}
|
||||
|
||||
float BME280Component::read_humidity_(int32_t t_fine) {
|
||||
uint16_t raw_adc;
|
||||
if (!this->read_byte_16(BME280_REGISTER_HUMIDDATA, &raw_adc) || raw_adc == 0x8000)
|
||||
float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
|
||||
uint16_t raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
|
||||
if (raw_adc == 0x8000)
|
||||
return NAN;
|
||||
|
||||
int32_t adc = raw_adc;
|
||||
|
||||
@@ -82,11 +82,11 @@ class BME280Component : public PollingComponent, public i2c::I2CDevice {
|
||||
|
||||
protected:
|
||||
/// Read the temperature value and store the calculated ambient temperature in t_fine.
|
||||
float read_temperature_(int32_t *t_fine);
|
||||
float read_temperature_(const uint8_t *data, int32_t *t_fine);
|
||||
/// Read the pressure value in hPa using the provided t_fine value.
|
||||
float read_pressure_(int32_t t_fine);
|
||||
float read_pressure_(const uint8_t *data, int32_t t_fine);
|
||||
/// Read the humidity value in % using the provided t_fine value.
|
||||
float read_humidity_(int32_t t_fine);
|
||||
float read_humidity_(const uint8_t *data, int32_t t_fine);
|
||||
uint8_t read_u8_(uint8_t a_register);
|
||||
uint16_t read_u16_le_(uint8_t a_register);
|
||||
int16_t read_s16_le_(uint8_t a_register);
|
||||
|
||||
@@ -2,7 +2,6 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.core import CORE
|
||||
|
||||
CODEOWNERS = ["@trvrnrth"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
@@ -62,9 +61,8 @@ async def to_code(config):
|
||||
var.set_state_save_interval(config[CONF_STATE_SAVE_INTERVAL].total_milliseconds)
|
||||
)
|
||||
|
||||
if CORE.is_esp32:
|
||||
# Although this component does not use SPI, the BSEC library requires the SPI library
|
||||
cg.add_library("SPI", None)
|
||||
# Although this component does not use SPI, the BSEC library requires the SPI library
|
||||
cg.add_library("SPI", None)
|
||||
|
||||
cg.add_define("USE_BSEC")
|
||||
cg.add_library("boschsensortec/BSEC Software Library", "1.6.1480")
|
||||
|
||||
127
esphome/components/button/__init__.py
Normal file
127
esphome/components/button/__init__.py
Normal file
@@ -0,0 +1,127 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
from esphome.components import mqtt
|
||||
from esphome.const import (
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_ENTITY_CATEGORY,
|
||||
CONF_ICON,
|
||||
CONF_ID,
|
||||
CONF_ON_PRESS,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_MQTT_ID,
|
||||
DEVICE_CLASS_RESTART,
|
||||
DEVICE_CLASS_UPDATE,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
from esphome.cpp_helpers import setup_entity
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
|
||||
DEVICE_CLASSES = [
|
||||
DEVICE_CLASS_RESTART,
|
||||
DEVICE_CLASS_UPDATE,
|
||||
]
|
||||
|
||||
button_ns = cg.esphome_ns.namespace("button")
|
||||
Button = button_ns.class_("Button", cg.EntityBase)
|
||||
ButtonPtr = Button.operator("ptr")
|
||||
|
||||
PressAction = button_ns.class_("PressAction", automation.Action)
|
||||
|
||||
ButtonPressTrigger = button_ns.class_(
|
||||
"ButtonPressTrigger", automation.Trigger.template()
|
||||
)
|
||||
|
||||
validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_")
|
||||
|
||||
|
||||
BUTTON_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
|
||||
{
|
||||
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTButtonComponent),
|
||||
cv.Optional(CONF_DEVICE_CLASS): validate_device_class,
|
||||
cv.Optional(CONF_ON_PRESS): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ButtonPressTrigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
_UNDEF = object()
|
||||
|
||||
|
||||
def button_schema(
|
||||
icon: str = _UNDEF,
|
||||
entity_category: str = _UNDEF,
|
||||
device_class: str = _UNDEF,
|
||||
) -> cv.Schema:
|
||||
schema = BUTTON_SCHEMA
|
||||
if icon is not _UNDEF:
|
||||
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon})
|
||||
if entity_category is not _UNDEF:
|
||||
schema = schema.extend(
|
||||
{
|
||||
cv.Optional(
|
||||
CONF_ENTITY_CATEGORY, default=entity_category
|
||||
): cv.entity_category
|
||||
}
|
||||
)
|
||||
if device_class is not _UNDEF:
|
||||
schema = schema.extend(
|
||||
{
|
||||
cv.Optional(
|
||||
CONF_DEVICE_CLASS, default=device_class
|
||||
): validate_device_class
|
||||
}
|
||||
)
|
||||
return schema
|
||||
|
||||
|
||||
async def setup_button_core_(var, config):
|
||||
await setup_entity(var, config)
|
||||
|
||||
for conf in config.get(CONF_ON_PRESS, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
if CONF_DEVICE_CLASS in config:
|
||||
cg.add(var.set_device_class(config[CONF_DEVICE_CLASS]))
|
||||
|
||||
if CONF_MQTT_ID in config:
|
||||
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
|
||||
await mqtt.register_mqtt_component(mqtt_, config)
|
||||
|
||||
|
||||
async def register_button(var, config):
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
var = cg.Pvariable(config[CONF_ID], var)
|
||||
cg.add(cg.App.register_button(var))
|
||||
await setup_button_core_(var, config)
|
||||
|
||||
|
||||
async def new_button(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await register_button(var, config)
|
||||
return var
|
||||
|
||||
|
||||
BUTTON_PRESS_SCHEMA = maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Button),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action("button.press", PressAction, BUTTON_PRESS_SCHEMA)
|
||||
async def button_press_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
|
||||
@coroutine_with_priority(100.0)
|
||||
async def to_code(config):
|
||||
cg.add_global(button_ns.using)
|
||||
cg.add_define("USE_BUTTON")
|
||||
28
esphome/components/button/automation.h
Normal file
28
esphome/components/button/automation.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/button/button.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace button {
|
||||
|
||||
template<typename... Ts> class PressAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit PressAction(Button *button) : button_(button) {}
|
||||
|
||||
void play(Ts... x) override { this->button_->press(); }
|
||||
|
||||
protected:
|
||||
Button *button_;
|
||||
};
|
||||
|
||||
class ButtonPressTrigger : public Trigger<> {
|
||||
public:
|
||||
ButtonPressTrigger(Button *button) {
|
||||
button->add_on_press_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace button
|
||||
} // namespace esphome
|
||||
28
esphome/components/button/button.cpp
Normal file
28
esphome/components/button/button.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "button.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace button {
|
||||
|
||||
static const char *const TAG = "button";
|
||||
|
||||
Button::Button(const std::string &name) : EntityBase(name) {}
|
||||
Button::Button() : Button("") {}
|
||||
|
||||
void Button::press() {
|
||||
ESP_LOGD(TAG, "'%s' Pressed.", this->get_name().c_str());
|
||||
this->press_action();
|
||||
this->press_callback_.call();
|
||||
}
|
||||
void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
|
||||
uint32_t Button::hash_base() { return 1495763804UL; }
|
||||
|
||||
void Button::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
|
||||
std::string Button::get_device_class() {
|
||||
if (this->device_class_.has_value())
|
||||
return *this->device_class_;
|
||||
return "";
|
||||
}
|
||||
|
||||
} // namespace button
|
||||
} // namespace esphome
|
||||
57
esphome/components/button/button.h
Normal file
57
esphome/components/button/button.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace button {
|
||||
|
||||
#define LOG_BUTTON(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
|
||||
} \
|
||||
}
|
||||
|
||||
/** Base class for all buttons.
|
||||
*
|
||||
* A button is just a momentary switch that does not have a state, only a trigger.
|
||||
*/
|
||||
class Button : public EntityBase {
|
||||
public:
|
||||
explicit Button();
|
||||
explicit Button(const std::string &name);
|
||||
|
||||
/** Press this button. This is called by the front-end.
|
||||
*
|
||||
* For implementing buttons, please override press_action.
|
||||
*/
|
||||
void press();
|
||||
|
||||
/** Set callback for state changes.
|
||||
*
|
||||
* @param callback The void() callback.
|
||||
*/
|
||||
void add_on_press_callback(std::function<void()> &&callback);
|
||||
|
||||
/// Set the Home Assistant device class (see button::device_class).
|
||||
void set_device_class(const std::string &device_class);
|
||||
|
||||
/// Get the device class for this button.
|
||||
std::string get_device_class();
|
||||
|
||||
protected:
|
||||
/** You should implement this virtual method if you want to create your own button.
|
||||
*/
|
||||
virtual void press_action(){};
|
||||
|
||||
uint32_t hash_base() override;
|
||||
|
||||
CallbackManager<void()> press_callback_{};
|
||||
optional<std::string> device_class_{};
|
||||
};
|
||||
|
||||
} // namespace button
|
||||
} // namespace esphome
|
||||
45
esphome/components/cap1188/__init__.py
Normal file
45
esphome/components/cap1188/__init__.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c
|
||||
from esphome.const import CONF_ID, CONF_RESET_PIN
|
||||
from esphome import pins
|
||||
|
||||
CONF_TOUCH_THRESHOLD = "touch_threshold"
|
||||
CONF_ALLOW_MULTIPLE_TOUCHES = "allow_multiple_touches"
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
AUTO_LOAD = ["binary_sensor", "output"]
|
||||
CODEOWNERS = ["@MrEditor97"]
|
||||
|
||||
cap1188_ns = cg.esphome_ns.namespace("cap1188")
|
||||
CONF_CAP1188_ID = "cap1188_id"
|
||||
CAP1188Component = cap1188_ns.class_("CAP1188Component", cg.Component, i2c.I2CDevice)
|
||||
|
||||
MULTI_CONF = True
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(CAP1188Component),
|
||||
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Optional(CONF_TOUCH_THRESHOLD, default=0x20): cv.int_range(
|
||||
min=0x01, max=0x80
|
||||
),
|
||||
cv.Optional(CONF_ALLOW_MULTIPLE_TOUCHES, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(i2c.i2c_device_schema(0x29))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD]))
|
||||
cg.add(var.set_allow_multiple_touches(config[CONF_ALLOW_MULTIPLE_TOUCHES]))
|
||||
|
||||
if CONF_RESET_PIN in config:
|
||||
pin = await cg.gpio_pin_expression(config[CONF_RESET_PIN])
|
||||
cg.add(var.set_reset_pin(pin))
|
||||
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
25
esphome/components/cap1188/binary_sensor.py
Normal file
25
esphome/components/cap1188/binary_sensor.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_CHANNEL, CONF_ID
|
||||
from . import cap1188_ns, CAP1188Component, CONF_CAP1188_ID
|
||||
|
||||
DEPENDENCIES = ["cap1188"]
|
||||
CAP1188Channel = cap1188_ns.class_("CAP1188Channel", binary_sensor.BinarySensor)
|
||||
|
||||
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(CAP1188Channel),
|
||||
cv.GenerateID(CONF_CAP1188_ID): cv.use_id(CAP1188Component),
|
||||
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=7),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await binary_sensor.register_binary_sensor(var, config)
|
||||
hub = await cg.get_variable(config[CONF_CAP1188_ID])
|
||||
cg.add(var.set_channel(config[CONF_CHANNEL]))
|
||||
|
||||
cg.add(hub.register_channel(var))
|
||||
88
esphome/components/cap1188/cap1188.cpp
Normal file
88
esphome/components/cap1188/cap1188.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "cap1188.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace cap1188 {
|
||||
|
||||
static const char *const TAG = "cap1188";
|
||||
|
||||
void CAP1188Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up CAP1188...");
|
||||
|
||||
// Reset device using the reset pin
|
||||
if (this->reset_pin_ != nullptr) {
|
||||
this->reset_pin_->setup();
|
||||
this->reset_pin_->digital_write(false);
|
||||
delay(100); // NOLINT
|
||||
this->reset_pin_->digital_write(true);
|
||||
delay(100); // NOLINT
|
||||
this->reset_pin_->digital_write(false);
|
||||
delay(100); // NOLINT
|
||||
}
|
||||
|
||||
// Check if CAP1188 is actually connected
|
||||
this->read_byte(CAP1188_PRODUCT_ID, &this->cap1188_product_id_);
|
||||
this->read_byte(CAP1188_MANUFACTURE_ID, &this->cap1188_manufacture_id_);
|
||||
this->read_byte(CAP1188_REVISION, &this->cap1188_revision_);
|
||||
|
||||
if ((this->cap1188_product_id_ != 0x50) || (this->cap1188_manufacture_id_ != 0x5D)) {
|
||||
this->error_code_ = COMMUNICATION_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
// Set sensitivity
|
||||
uint8_t sensitivity = 0;
|
||||
this->read_byte(CAP1188_SENSITVITY, &sensitivity);
|
||||
sensitivity = sensitivity & 0x0f;
|
||||
this->write_byte(CAP1188_SENSITVITY, sensitivity | this->touch_threshold_);
|
||||
|
||||
// Allow multiple touches
|
||||
this->write_byte(CAP1188_MULTI_TOUCH, this->allow_multiple_touches_);
|
||||
|
||||
// Have LEDs follow touches
|
||||
this->write_byte(CAP1188_LED_LINK, 0xFF);
|
||||
|
||||
// Speed up a bit
|
||||
this->write_byte(CAP1188_STAND_BY_CONFIGURATION, 0x30);
|
||||
}
|
||||
|
||||
void CAP1188Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "CAP1188:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||
ESP_LOGCONFIG(TAG, " Product ID: 0x%x", this->cap1188_product_id_);
|
||||
ESP_LOGCONFIG(TAG, " Manufacture ID: 0x%x", this->cap1188_manufacture_id_);
|
||||
ESP_LOGCONFIG(TAG, " Revision ID: 0x%x", this->cap1188_revision_);
|
||||
|
||||
switch (this->error_code_) {
|
||||
case COMMUNICATION_FAILED:
|
||||
ESP_LOGE(TAG, "Product ID or Manufacture ID of the connected device does not match a known CAP1188.");
|
||||
break;
|
||||
case NONE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void CAP1188Component::loop() {
|
||||
uint8_t touched = 0;
|
||||
|
||||
this->read_register(CAP1188_SENSOR_INPUT_STATUS, &touched, 1);
|
||||
|
||||
if (touched) {
|
||||
uint8_t data = 0;
|
||||
this->read_register(CAP1188_MAIN, &data, 1);
|
||||
data = data & ~CAP1188_MAIN_INT;
|
||||
|
||||
this->write_register(CAP1188_MAIN, &data, 2);
|
||||
}
|
||||
|
||||
for (auto *channel : this->channels_) {
|
||||
channel->process(touched);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cap1188
|
||||
} // namespace esphome
|
||||
68
esphome/components/cap1188/cap1188.h
Normal file
68
esphome/components/cap1188/cap1188.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace cap1188 {
|
||||
|
||||
enum {
|
||||
CAP1188_I2CADDR = 0x29,
|
||||
CAP1188_SENSOR_INPUT_STATUS = 0x3,
|
||||
CAP1188_MULTI_TOUCH = 0x2A,
|
||||
CAP1188_LED_LINK = 0x72,
|
||||
CAP1188_PRODUCT_ID = 0xFD,
|
||||
CAP1188_MANUFACTURE_ID = 0xFE,
|
||||
CAP1188_STAND_BY_CONFIGURATION = 0x41,
|
||||
CAP1188_REVISION = 0xFF,
|
||||
CAP1188_MAIN = 0x00,
|
||||
CAP1188_MAIN_INT = 0x01,
|
||||
CAP1188_LEDPOL = 0x73,
|
||||
CAP1188_INTERUPT_REPEAT = 0x28,
|
||||
CAP1188_SENSITVITY = 0x1f,
|
||||
};
|
||||
|
||||
class CAP1188Channel : public binary_sensor::BinarySensor {
|
||||
public:
|
||||
void set_channel(uint8_t channel) { channel_ = channel; }
|
||||
void process(uint8_t data) { this->publish_state(static_cast<bool>(data & (1 << this->channel_))); }
|
||||
|
||||
protected:
|
||||
uint8_t channel_{0};
|
||||
};
|
||||
|
||||
class CAP1188Component : public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
void register_channel(CAP1188Channel *channel) { this->channels_.push_back(channel); }
|
||||
void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; };
|
||||
void set_allow_multiple_touches(bool allow_multiple_touches) {
|
||||
this->allow_multiple_touches_ = allow_multiple_touches ? 0x41 : 0x80;
|
||||
};
|
||||
void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void loop() override;
|
||||
|
||||
protected:
|
||||
std::vector<CAP1188Channel *> channels_{};
|
||||
uint8_t touch_threshold_{0x20};
|
||||
uint8_t allow_multiple_touches_{0x80};
|
||||
|
||||
GPIOPin *reset_pin_{nullptr};
|
||||
|
||||
uint8_t cap1188_product_id_{0};
|
||||
uint8_t cap1188_manufacture_id_{0};
|
||||
uint8_t cap1188_revision_{0};
|
||||
|
||||
enum ErrorCode {
|
||||
NONE = 0,
|
||||
COMMUNICATION_FAILED,
|
||||
} error_code_{NONE};
|
||||
};
|
||||
|
||||
} // namespace cap1188
|
||||
} // namespace esphome
|
||||
@@ -36,3 +36,5 @@ async def to_code(config):
|
||||
if CORE.is_esp32:
|
||||
cg.add_library("DNSServer", None)
|
||||
cg.add_library("WiFi", None)
|
||||
if CORE.is_esp8266:
|
||||
cg.add_library("DNSServer", None)
|
||||
|
||||
@@ -67,6 +67,7 @@ void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
|
||||
ESP_LOGI(TAG, " SSID='%s'", ssid.c_str());
|
||||
ESP_LOGI(TAG, " Password=" LOG_SECRET("'%s'"), psk.c_str());
|
||||
wifi::global_wifi_component->save_wifi_sta(ssid, psk);
|
||||
wifi::global_wifi_component->start_scanning();
|
||||
request->redirect("/?save=true");
|
||||
}
|
||||
|
||||
@@ -84,14 +85,7 @@ void CaptivePortal::start() {
|
||||
this->dns_server_->start(53, "*", (uint32_t) ip);
|
||||
|
||||
this->base_->get_server()->onNotFound([this](AsyncWebServerRequest *req) {
|
||||
bool not_found = false;
|
||||
if (!this->active_) {
|
||||
not_found = true;
|
||||
} else if (req->host().c_str() == wifi::global_wifi_component->wifi_soft_ap_ip().str()) {
|
||||
not_found = true;
|
||||
}
|
||||
|
||||
if (not_found) {
|
||||
if (!this->active_ || req->host().c_str() == wifi::global_wifi_component->wifi_soft_ap_ip().str()) {
|
||||
req->send(404, "text/html", "File not found");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ void CCS811Component::setup() {
|
||||
|
||||
if (this->baseline_.has_value()) {
|
||||
// baseline available, write to sensor
|
||||
this->write_bytes(0x11, decode_uint16(*this->baseline_));
|
||||
this->write_bytes(0x11, decode_value(*this->baseline_));
|
||||
}
|
||||
|
||||
auto hardware_version_data = this->read_bytes<1>(0x21);
|
||||
|
||||
@@ -20,6 +20,7 @@ from esphome.const import (
|
||||
CONF_MODE,
|
||||
CONF_MODE_COMMAND_TOPIC,
|
||||
CONF_MODE_STATE_TOPIC,
|
||||
CONF_ON_STATE,
|
||||
CONF_PRESET,
|
||||
CONF_SWING_MODE,
|
||||
CONF_SWING_MODE_COMMAND_TOPIC,
|
||||
@@ -34,6 +35,7 @@ from esphome.const import (
|
||||
CONF_TARGET_TEMPERATURE_LOW_COMMAND_TOPIC,
|
||||
CONF_TARGET_TEMPERATURE_LOW_STATE_TOPIC,
|
||||
CONF_TEMPERATURE_STEP,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_VISUAL,
|
||||
CONF_MQTT_ID,
|
||||
)
|
||||
@@ -101,6 +103,7 @@ validate_climate_swing_mode = cv.enum(CLIMATE_SWING_MODES, upper=True)
|
||||
|
||||
# Actions
|
||||
ControlAction = climate_ns.class_("ControlAction", automation.Action)
|
||||
StateTrigger = climate_ns.class_("StateTrigger", automation.Trigger.template())
|
||||
|
||||
CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
|
||||
{
|
||||
@@ -161,6 +164,11 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
|
||||
cv.Optional(CONF_TARGET_TEMPERATURE_LOW_STATE_TOPIC): cv.All(
|
||||
cv.requires_component("mqtt"), cv.publish_topic
|
||||
),
|
||||
cv.Optional(CONF_ON_STATE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StateTrigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -205,7 +213,7 @@ async def setup_climate_core_(var, config):
|
||||
if CONF_MODE_COMMAND_TOPIC in config:
|
||||
cg.add(mqtt_.set_custom_mode_command_topic(config[CONF_MODE_COMMAND_TOPIC]))
|
||||
if CONF_MODE_STATE_TOPIC in config:
|
||||
cg.add(mqtt_.set_custom_state_topic(config[CONF_MODE_STATE_TOPIC]))
|
||||
cg.add(mqtt_.set_custom_mode_state_topic(config[CONF_MODE_STATE_TOPIC]))
|
||||
|
||||
if CONF_SWING_MODE_COMMAND_TOPIC in config:
|
||||
cg.add(
|
||||
@@ -256,6 +264,10 @@ async def setup_climate_core_(var, config):
|
||||
)
|
||||
)
|
||||
|
||||
for conf in config.get(CONF_ON_STATE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
|
||||
async def register_climate(var, config):
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
|
||||
@@ -42,5 +42,12 @@ template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||
Climate *climate_;
|
||||
};
|
||||
|
||||
class StateTrigger : public Trigger<> {
|
||||
public:
|
||||
StateTrigger(Climate *climate) {
|
||||
climate->add_on_state_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace climate
|
||||
} // namespace esphome
|
||||
|
||||
@@ -102,8 +102,6 @@ void CS5460AComponent::hw_init_() {
|
||||
|
||||
/* Doesn't reset the register values etc., just restarts the "computation cycle" */
|
||||
void CS5460AComponent::restart_() {
|
||||
int cnt;
|
||||
|
||||
this->enable();
|
||||
/* Stop running conversion, wake up if needed */
|
||||
this->write_byte(CMD_POWER_UP);
|
||||
|
||||
0
esphome/components/cse7761/__init__.py
Normal file
0
esphome/components/cse7761/__init__.py
Normal file
244
esphome/components/cse7761/cse7761.cpp
Normal file
244
esphome/components/cse7761/cse7761.cpp
Normal file
@@ -0,0 +1,244 @@
|
||||
#include "cse7761.h"
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace cse7761 {
|
||||
|
||||
static const char *const TAG = "cse7761";
|
||||
|
||||
/*********************************************************************************************\
|
||||
* CSE7761 - Energy (Sonoff Dual R3 Pow v1.x)
|
||||
*
|
||||
* Based on Tasmota source code
|
||||
* See https://github.com/arendst/Tasmota/discussions/10793
|
||||
* https://github.com/arendst/Tasmota/blob/development/tasmota/xnrg_19_cse7761.ino
|
||||
\*********************************************************************************************/
|
||||
|
||||
static const int CSE7761_UREF = 42563; // RmsUc
|
||||
static const int CSE7761_IREF = 52241; // RmsIAC
|
||||
static const int CSE7761_PREF = 44513; // PowerPAC
|
||||
|
||||
static const uint8_t CSE7761_REG_SYSCON = 0x00; // (2) System Control Register (0x0A04)
|
||||
static const uint8_t CSE7761_REG_EMUCON = 0x01; // (2) Metering control register (0x0000)
|
||||
static const uint8_t CSE7761_REG_EMUCON2 = 0x13; // (2) Metering control register 2 (0x0001)
|
||||
static const uint8_t CSE7761_REG_PULSE1SEL = 0x1D; // (2) Pin function output select register (0x3210)
|
||||
|
||||
static const uint8_t CSE7761_REG_RMSIA = 0x24; // (3) The effective value of channel A current (0x000000)
|
||||
static const uint8_t CSE7761_REG_RMSIB = 0x25; // (3) The effective value of channel B current (0x000000)
|
||||
static const uint8_t CSE7761_REG_RMSU = 0x26; // (3) Voltage RMS (0x000000)
|
||||
static const uint8_t CSE7761_REG_POWERPA = 0x2C; // (4) Channel A active power, update rate 27.2Hz (0x00000000)
|
||||
static const uint8_t CSE7761_REG_POWERPB = 0x2D; // (4) Channel B active power, update rate 27.2Hz (0x00000000)
|
||||
static const uint8_t CSE7761_REG_SYSSTATUS = 0x43; // (1) System status register
|
||||
|
||||
static const uint8_t CSE7761_REG_COEFFCHKSUM = 0x6F; // (2) Coefficient checksum
|
||||
static const uint8_t CSE7761_REG_RMSIAC = 0x70; // (2) Channel A effective current conversion coefficient
|
||||
|
||||
static const uint8_t CSE7761_SPECIAL_COMMAND = 0xEA; // Start special command
|
||||
static const uint8_t CSE7761_CMD_RESET = 0x96; // Reset command, after receiving the command, the chip resets
|
||||
static const uint8_t CSE7761_CMD_CLOSE_WRITE = 0xDC; // Close write operation
|
||||
static const uint8_t CSE7761_CMD_ENABLE_WRITE = 0xE5; // Enable write operation
|
||||
|
||||
enum CSE7761 { RMS_IAC, RMS_IBC, RMS_UC, POWER_PAC, POWER_PBC, POWER_SC, ENERGY_AC, ENERGY_BC };
|
||||
|
||||
void CSE7761Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up CSE7761...");
|
||||
this->write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
|
||||
uint16_t syscon = this->read_(0x00, 2); // Default 0x0A04
|
||||
if ((0x0A04 == syscon) && this->chip_init_()) {
|
||||
this->write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
|
||||
ESP_LOGD(TAG, "CSE7761 found");
|
||||
this->data_.ready = true;
|
||||
} else {
|
||||
this->mark_failed();
|
||||
}
|
||||
}
|
||||
|
||||
void CSE7761Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "CSE7761:");
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, "Communication with CSE7761 failed!");
|
||||
}
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
this->check_uart_settings(38400, 1, uart::UART_CONFIG_PARITY_EVEN, 8);
|
||||
}
|
||||
|
||||
float CSE7761Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
void CSE7761Component::update() {
|
||||
if (this->data_.ready) {
|
||||
this->get_data_();
|
||||
}
|
||||
}
|
||||
|
||||
void CSE7761Component::write_(uint8_t reg, uint16_t data) {
|
||||
uint8_t buffer[5];
|
||||
|
||||
buffer[0] = 0xA5;
|
||||
buffer[1] = reg;
|
||||
uint32_t len = 2;
|
||||
if (data) {
|
||||
if (data < 0xFF) {
|
||||
buffer[2] = data & 0xFF;
|
||||
len = 3;
|
||||
} else {
|
||||
buffer[2] = (data >> 8) & 0xFF;
|
||||
buffer[3] = data & 0xFF;
|
||||
len = 4;
|
||||
}
|
||||
uint8_t crc = 0;
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
crc += buffer[i];
|
||||
}
|
||||
buffer[len] = ~crc;
|
||||
len++;
|
||||
}
|
||||
|
||||
this->write_array(buffer, len);
|
||||
}
|
||||
|
||||
bool CSE7761Component::read_once_(uint8_t reg, uint8_t size, uint32_t *value) {
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
|
||||
this->write_(reg, 0);
|
||||
|
||||
uint8_t buffer[8] = {0};
|
||||
uint32_t rcvd = 0;
|
||||
|
||||
for (uint32_t i = 0; i <= size; i++) {
|
||||
int value = this->read();
|
||||
if (value > -1 && rcvd < sizeof(buffer) - 1) {
|
||||
buffer[rcvd++] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rcvd) {
|
||||
ESP_LOGD(TAG, "Received 0 bytes for register %hhu", reg);
|
||||
return false;
|
||||
}
|
||||
|
||||
rcvd--;
|
||||
uint32_t result = 0;
|
||||
// CRC check
|
||||
uint8_t crc = 0xA5 + reg;
|
||||
for (uint32_t i = 0; i < rcvd; i++) {
|
||||
result = (result << 8) | buffer[i];
|
||||
crc += buffer[i];
|
||||
}
|
||||
crc = ~crc;
|
||||
if (crc != buffer[rcvd]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*value = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t CSE7761Component::read_(uint8_t reg, uint8_t size) {
|
||||
bool result = false; // Start loop
|
||||
uint8_t retry = 3; // Retry up to three times
|
||||
uint32_t value = 0; // Default no value
|
||||
while (!result && retry > 0) {
|
||||
retry--;
|
||||
if (this->read_once_(reg, size, &value))
|
||||
return value;
|
||||
}
|
||||
ESP_LOGE(TAG, "Reading register %hhu failed!", reg);
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t CSE7761Component::coefficient_by_unit_(uint32_t unit) {
|
||||
switch (unit) {
|
||||
case RMS_UC:
|
||||
return 0x400000 * 100 / this->data_.coefficient[RMS_UC];
|
||||
case RMS_IAC:
|
||||
return (0x800000 * 100 / this->data_.coefficient[RMS_IAC]) * 10; // Stay within 32 bits
|
||||
case POWER_PAC:
|
||||
return 0x80000000 / this->data_.coefficient[POWER_PAC];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool CSE7761Component::chip_init_() {
|
||||
uint16_t calc_chksum = 0xFFFF;
|
||||
for (uint32_t i = 0; i < 8; i++) {
|
||||
this->data_.coefficient[i] = this->read_(CSE7761_REG_RMSIAC + i, 2);
|
||||
calc_chksum += this->data_.coefficient[i];
|
||||
}
|
||||
calc_chksum = ~calc_chksum;
|
||||
uint16_t coeff_chksum = this->read_(CSE7761_REG_COEFFCHKSUM, 2);
|
||||
if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
|
||||
ESP_LOGD(TAG, "Default calibration");
|
||||
this->data_.coefficient[RMS_IAC] = CSE7761_IREF;
|
||||
this->data_.coefficient[RMS_UC] = CSE7761_UREF;
|
||||
this->data_.coefficient[POWER_PAC] = CSE7761_PREF;
|
||||
}
|
||||
|
||||
this->write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
|
||||
|
||||
uint8_t sys_status = this->read_(CSE7761_REG_SYSSTATUS, 1);
|
||||
if (sys_status & 0x10) { // Write enable to protected registers (WREN)
|
||||
this->write_(CSE7761_REG_SYSCON | 0x80, 0xFF04);
|
||||
this->write_(CSE7761_REG_EMUCON | 0x80, 0x1183);
|
||||
this->write_(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1);
|
||||
this->write_(CSE7761_REG_PULSE1SEL | 0x80, 0x3290);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Write failed at chip_init");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CSE7761Component::get_data_() {
|
||||
// The effective value of current and voltage Rms is a 24-bit signed number,
|
||||
// the highest bit is 0 for valid data,
|
||||
// and when the highest bit is 1, the reading will be processed as zero
|
||||
// The active power parameter PowerA/B is in two’s complement format, 32-bit
|
||||
// data, the highest bit is Sign bit.
|
||||
uint32_t value = this->read_(CSE7761_REG_RMSU, 3);
|
||||
this->data_.voltage_rms = (value >= 0x800000) ? 0 : value;
|
||||
|
||||
value = this->read_(CSE7761_REG_RMSIA, 3);
|
||||
this->data_.current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
|
||||
value = this->read_(CSE7761_REG_POWERPA, 4);
|
||||
this->data_.active_power[0] = (0 == this->data_.current_rms[0]) ? 0 : ((uint32_t) abs((int) value));
|
||||
|
||||
value = this->read_(CSE7761_REG_RMSIB, 3);
|
||||
this->data_.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
|
||||
value = this->read_(CSE7761_REG_POWERPB, 4);
|
||||
this->data_.active_power[1] = (0 == this->data_.current_rms[1]) ? 0 : ((uint32_t) abs((int) value));
|
||||
|
||||
// convert values and publish to sensors
|
||||
|
||||
float voltage = (float) this->data_.voltage_rms / this->coefficient_by_unit_(RMS_UC);
|
||||
if (this->voltage_sensor_ != nullptr) {
|
||||
this->voltage_sensor_->publish_state(voltage);
|
||||
}
|
||||
|
||||
for (uint32_t channel = 0; channel < 2; channel++) {
|
||||
// Active power = PowerPA * PowerPAC * 1000 / 0x80000000
|
||||
float active_power = (float) this->data_.active_power[channel] / this->coefficient_by_unit_(POWER_PAC); // W
|
||||
float amps = (float) this->data_.current_rms[channel] / this->coefficient_by_unit_(RMS_IAC); // A
|
||||
ESP_LOGD(TAG, "Channel %d power %f W, current %f A", channel + 1, active_power, amps);
|
||||
if (channel == 0) {
|
||||
if (this->power_sensor_1_ != nullptr) {
|
||||
this->power_sensor_1_->publish_state(active_power);
|
||||
}
|
||||
if (this->current_sensor_1_ != nullptr) {
|
||||
this->current_sensor_1_->publish_state(amps);
|
||||
}
|
||||
} else if (channel == 1) {
|
||||
if (this->power_sensor_2_ != nullptr) {
|
||||
this->power_sensor_2_->publish_state(active_power);
|
||||
}
|
||||
if (this->current_sensor_2_ != nullptr) {
|
||||
this->current_sensor_2_->publish_state(amps);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cse7761
|
||||
} // namespace esphome
|
||||
52
esphome/components/cse7761/cse7761.h
Normal file
52
esphome/components/cse7761/cse7761.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace cse7761 {
|
||||
|
||||
struct CSE7761DataStruct {
|
||||
uint32_t frequency = 0;
|
||||
uint32_t voltage_rms = 0;
|
||||
uint32_t current_rms[2] = {0};
|
||||
uint32_t energy[2] = {0};
|
||||
uint32_t active_power[2] = {0};
|
||||
uint16_t coefficient[8] = {0};
|
||||
uint8_t energy_update = 0;
|
||||
bool ready = false;
|
||||
};
|
||||
|
||||
/// This class implements support for the CSE7761 UART power sensor.
|
||||
class CSE7761Component : public PollingComponent, public uart::UARTDevice {
|
||||
public:
|
||||
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
||||
void set_active_power_1_sensor(sensor::Sensor *power_sensor_1) { power_sensor_1_ = power_sensor_1; }
|
||||
void set_current_1_sensor(sensor::Sensor *current_sensor_1) { current_sensor_1_ = current_sensor_1; }
|
||||
void set_active_power_2_sensor(sensor::Sensor *power_sensor_2) { power_sensor_2_ = power_sensor_2; }
|
||||
void set_current_2_sensor(sensor::Sensor *current_sensor_2) { current_sensor_2_ = current_sensor_2; }
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
// Sensors
|
||||
sensor::Sensor *voltage_sensor_{nullptr};
|
||||
sensor::Sensor *power_sensor_1_{nullptr};
|
||||
sensor::Sensor *current_sensor_1_{nullptr};
|
||||
sensor::Sensor *power_sensor_2_{nullptr};
|
||||
sensor::Sensor *current_sensor_2_{nullptr};
|
||||
CSE7761DataStruct data_;
|
||||
|
||||
void write_(uint8_t reg, uint16_t data);
|
||||
bool read_once_(uint8_t reg, uint8_t size, uint32_t *value);
|
||||
uint32_t read_(uint8_t reg, uint8_t size);
|
||||
uint32_t coefficient_by_unit_(uint32_t unit);
|
||||
bool chip_init_();
|
||||
void get_data_();
|
||||
};
|
||||
|
||||
} // namespace cse7761
|
||||
} // namespace esphome
|
||||
90
esphome/components/cse7761/sensor.py
Normal file
90
esphome/components/cse7761/sensor.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor, uart
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_VOLTAGE,
|
||||
DEVICE_CLASS_CURRENT,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_VOLT,
|
||||
UNIT_AMPERE,
|
||||
UNIT_WATT,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@berfenger"]
|
||||
DEPENDENCIES = ["uart"]
|
||||
|
||||
cse7761_ns = cg.esphome_ns.namespace("cse7761")
|
||||
CSE7761Component = cse7761_ns.class_(
|
||||
"CSE7761Component", cg.PollingComponent, uart.UARTDevice
|
||||
)
|
||||
|
||||
CONF_CURRENT_1 = "current_1"
|
||||
CONF_CURRENT_2 = "current_2"
|
||||
CONF_ACTIVE_POWER_1 = "active_power_1"
|
||||
CONF_ACTIVE_POWER_2 = "active_power_2"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(CSE7761Component),
|
||||
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_VOLT,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_CURRENT_1): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_AMPERE,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_CURRENT_2): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_AMPERE,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_ACTIVE_POWER_1): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_WATT,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_ACTIVE_POWER_2): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_WATT,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(uart.UART_DEVICE_SCHEMA)
|
||||
)
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
|
||||
"cse7761", baud_rate=38400, require_rx=True, require_tx=True
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await uart.register_uart_device(var, config)
|
||||
|
||||
for key in [
|
||||
CONF_VOLTAGE,
|
||||
CONF_CURRENT_1,
|
||||
CONF_CURRENT_2,
|
||||
CONF_ACTIVE_POWER_1,
|
||||
CONF_ACTIVE_POWER_2,
|
||||
]:
|
||||
if key not in config:
|
||||
continue
|
||||
conf = config[key]
|
||||
sens = await sensor.new_sensor(conf)
|
||||
cg.add(getattr(var, f"set_{key}_sensor")(sens))
|
||||
@@ -90,6 +90,7 @@ void CSE7766Component::parse_data_() {
|
||||
uint32_t power_cycle = this->get_24_bit_uint_(17);
|
||||
|
||||
uint8_t adj = this->raw_data_[20];
|
||||
uint32_t cf_pulses = (this->raw_data_[21] << 8) + this->raw_data_[22];
|
||||
|
||||
bool power_ok = true;
|
||||
bool voltage_ok = true;
|
||||
@@ -127,6 +128,18 @@ void CSE7766Component::parse_data_() {
|
||||
power = power_calib / float(power_cycle);
|
||||
this->power_acc_ += power;
|
||||
this->power_counts_ += 1;
|
||||
|
||||
uint32_t difference;
|
||||
if (this->cf_pulses_last_ == 0)
|
||||
this->cf_pulses_last_ = cf_pulses;
|
||||
|
||||
if (cf_pulses < this->cf_pulses_last_) {
|
||||
difference = cf_pulses + (0x10000 - this->cf_pulses_last_);
|
||||
} else {
|
||||
difference = cf_pulses - this->cf_pulses_last_;
|
||||
}
|
||||
this->cf_pulses_last_ = cf_pulses;
|
||||
this->energy_total_ += difference * float(power_calib) / 1000000.0 / 3600.0;
|
||||
}
|
||||
|
||||
if ((adj & 0x20) == 0x20 && current_ok && voltage_ok && power != 0.0) {
|
||||
@@ -136,9 +149,9 @@ void CSE7766Component::parse_data_() {
|
||||
}
|
||||
}
|
||||
void CSE7766Component::update() {
|
||||
float voltage = this->voltage_counts_ > 0 ? this->voltage_acc_ / this->voltage_counts_ : 0.0;
|
||||
float current = this->current_counts_ > 0 ? this->current_acc_ / this->current_counts_ : 0.0;
|
||||
float power = this->power_counts_ > 0 ? this->power_acc_ / this->power_counts_ : 0.0;
|
||||
float voltage = this->voltage_counts_ > 0 ? this->voltage_acc_ / this->voltage_counts_ : 0.0f;
|
||||
float current = this->current_counts_ > 0 ? this->current_acc_ / this->current_counts_ : 0.0f;
|
||||
float power = this->power_counts_ > 0 ? this->power_acc_ / this->power_counts_ : 0.0f;
|
||||
|
||||
ESP_LOGV(TAG, "Got voltage_acc=%.2f current_acc=%.2f power_acc=%.2f", this->voltage_acc_, this->current_acc_,
|
||||
this->power_acc_);
|
||||
@@ -152,6 +165,8 @@ void CSE7766Component::update() {
|
||||
this->current_sensor_->publish_state(current);
|
||||
if (this->power_sensor_ != nullptr)
|
||||
this->power_sensor_->publish_state(power);
|
||||
if (this->energy_sensor_ != nullptr)
|
||||
this->energy_sensor_->publish_state(this->energy_total_);
|
||||
|
||||
this->voltage_acc_ = 0.0f;
|
||||
this->current_acc_ = 0.0f;
|
||||
@@ -172,6 +187,7 @@ void CSE7766Component::dump_config() {
|
||||
LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
|
||||
LOG_SENSOR(" ", "Current", this->current_sensor_);
|
||||
LOG_SENSOR(" ", "Power", this->power_sensor_);
|
||||
LOG_SENSOR(" ", "Energy", this->energy_sensor_);
|
||||
this->check_uart_settings(4800);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ class CSE7766Component : public PollingComponent, public uart::UARTDevice {
|
||||
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
||||
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
|
||||
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
|
||||
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
|
||||
|
||||
void loop() override;
|
||||
float get_setup_priority() const override;
|
||||
@@ -29,9 +30,12 @@ class CSE7766Component : public PollingComponent, public uart::UARTDevice {
|
||||
sensor::Sensor *voltage_sensor_{nullptr};
|
||||
sensor::Sensor *current_sensor_{nullptr};
|
||||
sensor::Sensor *power_sensor_{nullptr};
|
||||
sensor::Sensor *energy_sensor_{nullptr};
|
||||
float voltage_acc_{0.0f};
|
||||
float current_acc_{0.0f};
|
||||
float power_acc_{0.0f};
|
||||
float energy_total_{0.0f};
|
||||
uint32_t cf_pulses_last_{0};
|
||||
uint32_t voltage_counts_{0};
|
||||
uint32_t current_counts_{0};
|
||||
uint32_t power_counts_{0};
|
||||
|
||||
@@ -3,16 +3,20 @@ import esphome.config_validation as cv
|
||||
from esphome.components import sensor, uart
|
||||
from esphome.const import (
|
||||
CONF_CURRENT,
|
||||
CONF_ENERGY,
|
||||
CONF_ID,
|
||||
CONF_POWER,
|
||||
CONF_VOLTAGE,
|
||||
DEVICE_CLASS_CURRENT,
|
||||
DEVICE_CLASS_ENERGY,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
UNIT_VOLT,
|
||||
UNIT_AMPERE,
|
||||
UNIT_WATT,
|
||||
UNIT_WATT_HOURS,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["uart"]
|
||||
@@ -44,6 +48,12 @@ CONFIG_SCHEMA = (
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_ENERGY): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_WATT_HOURS,
|
||||
accuracy_decimals=3,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
state_class=STATE_CLASS_TOTAL_INCREASING,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
@@ -71,3 +81,7 @@ async def to_code(config):
|
||||
conf = config[CONF_POWER]
|
||||
sens = await sensor.new_sensor(conf)
|
||||
cg.add(var.set_power_sensor(sens))
|
||||
if CONF_ENERGY in config:
|
||||
conf = config[CONF_ENERGY]
|
||||
sens = await sensor.new_sensor(conf)
|
||||
cg.add(var.set_energy_sensor(sens))
|
||||
|
||||
@@ -231,7 +231,7 @@ bool DaikinClimate::on_receive(remote_base::RemoteReceiveData data) {
|
||||
// frame header
|
||||
if (byte != 0x27)
|
||||
return false;
|
||||
} else if (pos == 3) {
|
||||
} else if (pos == 3) { // NOLINT(bugprone-branch-clone)
|
||||
// frame header
|
||||
if (byte != 0x00)
|
||||
return false;
|
||||
|
||||
@@ -29,6 +29,13 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
}
|
||||
)
|
||||
|
||||
WIFI_CONFIG = """
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
"""
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
cg.add_define("USE_DASHBOARD_IMPORT")
|
||||
@@ -41,5 +48,12 @@ def import_config(path: str, name: str, project_name: str, import_url: str) -> N
|
||||
if p.exists():
|
||||
raise FileExistsError
|
||||
|
||||
config = {"substitutions": {"name": name}, "packages": {project_name: import_url}}
|
||||
p.write_text(dump(config), encoding="utf8")
|
||||
config = {
|
||||
"substitutions": {"name": name},
|
||||
"packages": {project_name: import_url},
|
||||
"esphome": {"name_add_mac_suffix": False},
|
||||
}
|
||||
p.write_text(
|
||||
dump(config) + WIFI_CONFIG,
|
||||
encoding="utf8",
|
||||
)
|
||||
|
||||
@@ -4,20 +4,23 @@
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/version.h"
|
||||
|
||||
#ifdef USE_ESP_IDF
|
||||
#include <esp_heap_caps.h>
|
||||
#include <esp_system.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#if ESP_IDF_VERSION_MAJOR >= 4
|
||||
#include <esp32/rom/rtc.h>
|
||||
#else
|
||||
#include <rom/rtc.h>
|
||||
#include <esp_idf_version.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include <Esp.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP_IDF
|
||||
#include <esp_heap_caps.h>
|
||||
#include <esp_system.h>
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace debug {
|
||||
|
||||
|
||||
@@ -41,15 +41,30 @@ EXT1_WAKEUP_MODES = {
|
||||
"ALL_LOW": esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ALL_LOW,
|
||||
"ANY_HIGH": esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ANY_HIGH,
|
||||
}
|
||||
WakeupCauseToRunDuration = deep_sleep_ns.struct("WakeupCauseToRunDuration")
|
||||
|
||||
CONF_WAKEUP_PIN_MODE = "wakeup_pin_mode"
|
||||
CONF_ESP32_EXT1_WAKEUP = "esp32_ext1_wakeup"
|
||||
CONF_TOUCH_WAKEUP = "touch_wakeup"
|
||||
CONF_DEFAULT = "default"
|
||||
CONF_GPIO_WAKEUP_REASON = "gpio_wakeup_reason"
|
||||
CONF_TOUCH_WAKEUP_REASON = "touch_wakeup_reason"
|
||||
|
||||
WAKEUP_CAUSES_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_DEFAULT): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_TOUCH_WAKEUP_REASON): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_GPIO_WAKEUP_REASON): cv.positive_time_period_milliseconds,
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(DeepSleepComponent),
|
||||
cv.Optional(CONF_RUN_DURATION): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_RUN_DURATION): cv.Any(
|
||||
cv.All(cv.only_on_esp32, WAKEUP_CAUSES_SCHEMA),
|
||||
cv.positive_time_period_milliseconds,
|
||||
),
|
||||
cv.Optional(CONF_SLEEP_DURATION): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_WAKEUP_PIN): cv.All(
|
||||
cv.only_on_esp32, pins.internal_gpio_input_pin_schema, validate_pin_number
|
||||
@@ -85,7 +100,28 @@ async def to_code(config):
|
||||
if CONF_WAKEUP_PIN_MODE in config:
|
||||
cg.add(var.set_wakeup_pin_mode(config[CONF_WAKEUP_PIN_MODE]))
|
||||
if CONF_RUN_DURATION in config:
|
||||
cg.add(var.set_run_duration(config[CONF_RUN_DURATION]))
|
||||
run_duration_config = config[CONF_RUN_DURATION]
|
||||
if not isinstance(run_duration_config, dict):
|
||||
cg.add(var.set_run_duration(config[CONF_RUN_DURATION]))
|
||||
else:
|
||||
default_run_duration = run_duration_config[CONF_DEFAULT]
|
||||
wakeup_cause_to_run_duration = cg.StructInitializer(
|
||||
WakeupCauseToRunDuration,
|
||||
("default_cause", default_run_duration),
|
||||
(
|
||||
"touch_cause",
|
||||
run_duration_config.get(
|
||||
CONF_TOUCH_WAKEUP_REASON, default_run_duration
|
||||
),
|
||||
),
|
||||
(
|
||||
"gpio_cause",
|
||||
run_duration_config.get(
|
||||
CONF_GPIO_WAKEUP_REASON, default_run_duration
|
||||
),
|
||||
),
|
||||
)
|
||||
cg.add(var.set_run_duration(wakeup_cause_to_run_duration))
|
||||
|
||||
if CONF_ESP32_EXT1_WAKEUP in config:
|
||||
conf = config[CONF_ESP32_EXT1_WAKEUP]
|
||||
|
||||
@@ -13,12 +13,35 @@ static const char *const TAG = "deep_sleep";
|
||||
|
||||
bool global_has_deep_sleep = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
optional<uint32_t> DeepSleepComponent::get_run_duration_() const {
|
||||
#ifdef USE_ESP32
|
||||
if (this->wakeup_cause_to_run_duration_.has_value()) {
|
||||
esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
|
||||
switch (wakeup_cause) {
|
||||
case ESP_SLEEP_WAKEUP_EXT0:
|
||||
case ESP_SLEEP_WAKEUP_EXT1:
|
||||
return this->wakeup_cause_to_run_duration_->gpio_cause;
|
||||
case ESP_SLEEP_WAKEUP_TOUCHPAD:
|
||||
return this->wakeup_cause_to_run_duration_->touch_cause;
|
||||
default:
|
||||
return this->wakeup_cause_to_run_duration_->default_cause;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return this->run_duration_;
|
||||
}
|
||||
|
||||
void DeepSleepComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
|
||||
global_has_deep_sleep = true;
|
||||
|
||||
if (this->run_duration_.has_value())
|
||||
this->set_timeout(*this->run_duration_, [this]() { this->begin_sleep(); });
|
||||
const optional<uint32_t> run_duration = get_run_duration_();
|
||||
if (run_duration.has_value()) {
|
||||
ESP_LOGI(TAG, "Scheduling Deep Sleep to start in %u ms", *run_duration);
|
||||
this->set_timeout(*run_duration, [this]() { this->begin_sleep(); });
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Not scheduling Deep Sleep, as no run duration is configured.");
|
||||
}
|
||||
}
|
||||
void DeepSleepComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
|
||||
@@ -33,6 +56,11 @@ void DeepSleepComponent::dump_config() {
|
||||
if (wakeup_pin_ != nullptr) {
|
||||
LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
|
||||
}
|
||||
if (this->wakeup_cause_to_run_duration_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Default Wakeup Run Duration: %u ms", this->wakeup_cause_to_run_duration_->default_cause);
|
||||
ESP_LOGCONFIG(TAG, " Touch Wakeup Run Duration: %u ms", this->wakeup_cause_to_run_duration_->touch_cause);
|
||||
ESP_LOGCONFIG(TAG, " GPIO Wakeup Run Duration: %u ms", this->wakeup_cause_to_run_duration_->gpio_cause);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void DeepSleepComponent::loop() {
|
||||
@@ -49,6 +77,9 @@ void DeepSleepComponent::set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode) {
|
||||
}
|
||||
void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
|
||||
void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
|
||||
void DeepSleepComponent::set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration) {
|
||||
wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
|
||||
}
|
||||
#endif
|
||||
void DeepSleepComponent::set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
|
||||
void DeepSleepComponent::begin_sleep(bool manual) {
|
||||
@@ -77,9 +108,10 @@ void DeepSleepComponent::begin_sleep(bool manual) {
|
||||
if (this->sleep_duration_.has_value())
|
||||
esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
|
||||
if (this->wakeup_pin_ != nullptr) {
|
||||
bool level = this->wakeup_pin_->is_inverted();
|
||||
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && this->wakeup_pin_->digital_read())
|
||||
bool level = !this->wakeup_pin_->is_inverted();
|
||||
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && this->wakeup_pin_->digital_read()) {
|
||||
level = !level;
|
||||
}
|
||||
esp_sleep_enable_ext0_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()), level);
|
||||
}
|
||||
if (this->ext1_wakeup_.has_value()) {
|
||||
|
||||
@@ -32,6 +32,15 @@ struct Ext1Wakeup {
|
||||
esp_sleep_ext1_wakeup_mode_t wakeup_mode;
|
||||
};
|
||||
|
||||
struct WakeupCauseToRunDuration {
|
||||
// Run duration if woken up by timer or any other reason besides those below.
|
||||
uint32_t default_cause;
|
||||
// Run duration if woken up by touch pads.
|
||||
uint32_t touch_cause;
|
||||
// Run duration if woken up by GPIO pins.
|
||||
uint32_t gpio_cause;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
template<typename... Ts> class EnterDeepSleepAction;
|
||||
@@ -59,6 +68,11 @@ class DeepSleepComponent : public Component {
|
||||
void set_ext1_wakeup(Ext1Wakeup ext1_wakeup);
|
||||
|
||||
void set_touch_wakeup(bool touch_wakeup);
|
||||
|
||||
// Set the duration in ms for how long the code should run before entering
|
||||
// deep sleep mode, according to the cause the ESP32 has woken.
|
||||
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration);
|
||||
|
||||
#endif
|
||||
/// Set a duration in ms for how long the code should run before entering deep sleep mode.
|
||||
void set_run_duration(uint32_t time_ms);
|
||||
@@ -75,12 +89,17 @@ class DeepSleepComponent : public Component {
|
||||
void prevent_deep_sleep();
|
||||
|
||||
protected:
|
||||
// Returns nullopt if no run duration is set. Otherwise, returns the run
|
||||
// duration before entering deep sleep.
|
||||
optional<uint32_t> get_run_duration_() const;
|
||||
|
||||
optional<uint64_t> sleep_duration_;
|
||||
#ifdef USE_ESP32
|
||||
InternalGPIOPin *wakeup_pin_;
|
||||
WakeupPinMode wakeup_pin_mode_{WAKEUP_PIN_MODE_IGNORE};
|
||||
optional<Ext1Wakeup> ext1_wakeup_;
|
||||
optional<bool> touch_wakeup_;
|
||||
optional<WakeupCauseToRunDuration> wakeup_cause_to_run_duration_;
|
||||
#endif
|
||||
optional<uint32_t> run_duration_;
|
||||
bool next_enter_deep_sleep_{false};
|
||||
|
||||
@@ -3,6 +3,7 @@ import esphome.config_validation as cv
|
||||
from esphome import core, automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
from esphome.const import (
|
||||
CONF_AUTO_CLEAR_ENABLED,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_PAGES,
|
||||
@@ -79,6 +80,7 @@ FULL_DISPLAY_SCHEMA = BASIC_DISPLAY_SCHEMA.extend(
|
||||
cv.Optional(CONF_TO): cv.use_id(DisplayPage),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_AUTO_CLEAR_ENABLED, default=True): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -86,6 +88,10 @@ FULL_DISPLAY_SCHEMA = BASIC_DISPLAY_SCHEMA.extend(
|
||||
async def setup_display_core_(var, config):
|
||||
if CONF_ROTATION in config:
|
||||
cg.add(var.set_rotation(DISPLAY_ROTATIONS[config[CONF_ROTATION]]))
|
||||
|
||||
if CONF_AUTO_CLEAR_ENABLED in config:
|
||||
cg.add(var.set_auto_clear(config[CONF_AUTO_CLEAR_ENABLED]))
|
||||
|
||||
if CONF_PAGES in config:
|
||||
pages = []
|
||||
for conf in config[CONF_PAGES]:
|
||||
|
||||
@@ -336,7 +336,9 @@ void DisplayBuffer::show_page(DisplayPage *page) {
|
||||
void DisplayBuffer::show_next_page() { this->page_->show_next(); }
|
||||
void DisplayBuffer::show_prev_page() { this->page_->show_prev(); }
|
||||
void DisplayBuffer::do_update_() {
|
||||
this->clear();
|
||||
if (this->auto_clear_enabled_) {
|
||||
this->clear();
|
||||
}
|
||||
if (this->page_ != nullptr) {
|
||||
this->page_->get_writer()(*this);
|
||||
} else if (this->writer_.has_value()) {
|
||||
@@ -494,7 +496,7 @@ bool Animation::get_pixel(int x, int y) const {
|
||||
return false;
|
||||
const uint32_t width_8 = ((this->width_ + 7u) / 8u) * 8u;
|
||||
const uint32_t frame_index = this->height_ * width_8 * this->current_frame_;
|
||||
if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_)
|
||||
if (frame_index >= (uint32_t)(this->width_ * this->height_ * this->animation_frame_count_))
|
||||
return false;
|
||||
const uint32_t pos = x + y * width_8 + frame_index;
|
||||
return progmem_read_byte(this->data_start_ + (pos / 8u)) & (0x80 >> (pos % 8u));
|
||||
@@ -503,7 +505,7 @@ Color Animation::get_color_pixel(int x, int y) const {
|
||||
if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
|
||||
return Color::BLACK;
|
||||
const uint32_t frame_index = this->width_ * this->height_ * this->current_frame_;
|
||||
if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_)
|
||||
if (frame_index >= (uint32_t)(this->width_ * this->height_ * this->animation_frame_count_))
|
||||
return Color::BLACK;
|
||||
const uint32_t pos = (x + y * this->width_ + frame_index) * 3;
|
||||
const uint32_t color32 = (progmem_read_byte(this->data_start_ + pos + 2) << 0) |
|
||||
@@ -515,7 +517,7 @@ Color Animation::get_grayscale_pixel(int x, int y) const {
|
||||
if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
|
||||
return Color::BLACK;
|
||||
const uint32_t frame_index = this->width_ * this->height_ * this->current_frame_;
|
||||
if (frame_index >= this->width_ * this->height_ * this->animation_frame_count_)
|
||||
if (frame_index >= (uint32_t)(this->width_ * this->height_ * this->animation_frame_count_))
|
||||
return Color::BLACK;
|
||||
const uint32_t pos = (x + y * this->width_ + frame_index);
|
||||
const uint8_t gray = progmem_read_byte(this->data_start_ + pos);
|
||||
|
||||
@@ -333,6 +333,9 @@ class DisplayBuffer {
|
||||
/// Internal method to set the display rotation with.
|
||||
void set_rotation(DisplayRotation rotation);
|
||||
|
||||
// Internal method to set display auto clearing.
|
||||
void set_auto_clear(bool auto_clear_enabled) { this->auto_clear_enabled_ = auto_clear_enabled; }
|
||||
|
||||
protected:
|
||||
void vprintf_(int x, int y, Font *font, Color color, TextAlign align, const char *format, va_list arg);
|
||||
|
||||
@@ -352,6 +355,7 @@ class DisplayBuffer {
|
||||
DisplayPage *page_{nullptr};
|
||||
DisplayPage *previous_page_{nullptr};
|
||||
std::vector<DisplayOnPageChangeTrigger *> on_page_change_triggers_;
|
||||
bool auto_clear_enabled_{true};
|
||||
};
|
||||
|
||||
class DisplayPage {
|
||||
|
||||
@@ -42,7 +42,7 @@ class ColorUtil {
|
||||
? esp_scale(((colorcode >> third_bits) & ((1 << second_bits) - 1)), ((1 << second_bits) - 1))
|
||||
: esp_scale(((colorcode >> 8) & 0xFF), ((1 << second_bits) - 1));
|
||||
|
||||
third_color = (right_bit_aligned ? esp_scale(((colorcode >> 0) & 0xFF), ((1 << third_bits) - 1))
|
||||
third_color = (right_bit_aligned ? esp_scale(((colorcode >> 0) & ((1 << third_bits) - 1)), ((1 << third_bits) - 1))
|
||||
: esp_scale(((colorcode >> 0) & 0xFF), (1 << third_bits) - 1));
|
||||
|
||||
Color color_return;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import uart
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_UART_ID,
|
||||
CONF_RECEIVE_TIMEOUT,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@glmnet", "@zuidwijk"]
|
||||
@@ -11,10 +13,13 @@ CODEOWNERS = ["@glmnet", "@zuidwijk"]
|
||||
DEPENDENCIES = ["uart"]
|
||||
AUTO_LOAD = ["sensor", "text_sensor"]
|
||||
|
||||
CONF_DSMR_ID = "dsmr_id"
|
||||
CONF_DECRYPTION_KEY = "decryption_key"
|
||||
CONF_CRC_CHECK = "crc_check"
|
||||
CONF_DECRYPTION_KEY = "decryption_key"
|
||||
CONF_DSMR_ID = "dsmr_id"
|
||||
CONF_GAS_MBUS_ID = "gas_mbus_id"
|
||||
CONF_MAX_TELEGRAM_LENGTH = "max_telegram_length"
|
||||
CONF_REQUEST_INTERVAL = "request_interval"
|
||||
CONF_REQUEST_PIN = "request_pin"
|
||||
|
||||
# Hack to prevent compile error due to ambiguity with lib namespace
|
||||
dsmr_ns = cg.esphome_ns.namespace("esphome::dsmr")
|
||||
@@ -46,6 +51,14 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(CONF_DECRYPTION_KEY): _validate_key,
|
||||
cv.Optional(CONF_CRC_CHECK, default=True): cv.boolean,
|
||||
cv.Optional(CONF_GAS_MBUS_ID, default=1): cv.int_,
|
||||
cv.Optional(CONF_MAX_TELEGRAM_LENGTH, default=1500): cv.int_,
|
||||
cv.Optional(CONF_REQUEST_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Optional(
|
||||
CONF_REQUEST_INTERVAL, default="0ms"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(
|
||||
CONF_RECEIVE_TIMEOUT, default="200ms"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
}
|
||||
).extend(uart.UART_DEVICE_SCHEMA),
|
||||
cv.only_with_arduino,
|
||||
@@ -55,10 +68,17 @@ CONFIG_SCHEMA = cv.All(
|
||||
async def to_code(config):
|
||||
uart_component = await cg.get_variable(config[CONF_UART_ID])
|
||||
var = cg.new_Pvariable(config[CONF_ID], uart_component, config[CONF_CRC_CHECK])
|
||||
cg.add(var.set_max_telegram_length(config[CONF_MAX_TELEGRAM_LENGTH]))
|
||||
if CONF_DECRYPTION_KEY in config:
|
||||
cg.add(var.set_decryption_key(config[CONF_DECRYPTION_KEY]))
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if CONF_REQUEST_PIN in config:
|
||||
request_pin = await cg.gpio_pin_expression(config[CONF_REQUEST_PIN])
|
||||
cg.add(var.set_request_pin(request_pin))
|
||||
cg.add(var.set_request_interval(config[CONF_REQUEST_INTERVAL].total_milliseconds))
|
||||
cg.add(var.set_receive_timeout(config[CONF_RECEIVE_TIMEOUT].total_milliseconds))
|
||||
|
||||
cg.add_define("DSMR_GAS_MBUS_ID", config[CONF_GAS_MBUS_ID])
|
||||
|
||||
# DSMR Parser
|
||||
|
||||
@@ -12,156 +12,275 @@ namespace dsmr {
|
||||
|
||||
static const char *const TAG = "dsmr";
|
||||
|
||||
void Dsmr::setup() {
|
||||
this->telegram_ = new char[this->max_telegram_len_]; // NOLINT
|
||||
if (this->request_pin_ != nullptr) {
|
||||
this->request_pin_->setup();
|
||||
}
|
||||
}
|
||||
|
||||
void Dsmr::loop() {
|
||||
if (this->decryption_key_.empty())
|
||||
this->receive_telegram_();
|
||||
else
|
||||
this->receive_encrypted_();
|
||||
if (this->ready_to_request_data_()) {
|
||||
if (this->decryption_key_.empty()) {
|
||||
this->receive_telegram_();
|
||||
} else {
|
||||
this->receive_encrypted_telegram_();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Dsmr::ready_to_request_data_() {
|
||||
// When using a request pin, then wait for the next request interval.
|
||||
if (this->request_pin_ != nullptr) {
|
||||
if (!this->requesting_data_ && this->request_interval_reached_()) {
|
||||
this->start_requesting_data_();
|
||||
}
|
||||
}
|
||||
// Otherwise, sink serial data until next request interval.
|
||||
else {
|
||||
if (this->request_interval_reached_()) {
|
||||
this->start_requesting_data_();
|
||||
}
|
||||
if (!this->requesting_data_) {
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
}
|
||||
}
|
||||
return this->requesting_data_;
|
||||
}
|
||||
|
||||
bool Dsmr::request_interval_reached_() {
|
||||
if (this->last_request_time_ == 0) {
|
||||
return true;
|
||||
}
|
||||
return millis() - this->last_request_time_ > this->request_interval_;
|
||||
}
|
||||
|
||||
bool Dsmr::receive_timeout_reached_() { return millis() - this->last_read_time_ > this->receive_timeout_; }
|
||||
|
||||
bool Dsmr::available_within_timeout_() {
|
||||
// Data are available for reading on the UART bus?
|
||||
// Then we can start reading right away.
|
||||
if (this->available()) {
|
||||
this->last_read_time_ = millis();
|
||||
return true;
|
||||
}
|
||||
// When we're not in the process of reading a telegram, then there is
|
||||
// no need to actively wait for new data to come in.
|
||||
if (!header_found_) {
|
||||
return false;
|
||||
}
|
||||
// A telegram is being read. The smart meter might not deliver a telegram
|
||||
// in one go, but instead send it in chunks with small pauses in between.
|
||||
// When the UART RX buffer cannot hold a full telegram, then make sure
|
||||
// that the UART read buffer does not overflow while other components
|
||||
// perform their work in their loop. Do this by not returning control to
|
||||
// the main loop, until the read timeout is reached.
|
||||
if (this->parent_->get_rx_buffer_size() < this->max_telegram_len_) {
|
||||
while (!this->receive_timeout_reached_()) {
|
||||
delay(5);
|
||||
if (this->available()) {
|
||||
this->last_read_time_ = millis();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// No new data has come in during the read timeout? Then stop reading the
|
||||
// telegram and start waiting for the next one to arrive.
|
||||
if (this->receive_timeout_reached_()) {
|
||||
ESP_LOGW(TAG, "Timeout while reading data for telegram");
|
||||
this->reset_telegram_();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Dsmr::start_requesting_data_() {
|
||||
if (!this->requesting_data_) {
|
||||
if (this->request_pin_ != nullptr) {
|
||||
ESP_LOGV(TAG, "Start requesting data from P1 port");
|
||||
this->request_pin_->digital_write(true);
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Start reading data from P1 port");
|
||||
}
|
||||
this->requesting_data_ = true;
|
||||
this->last_request_time_ = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void Dsmr::stop_requesting_data_() {
|
||||
if (this->requesting_data_) {
|
||||
if (this->request_pin_ != nullptr) {
|
||||
ESP_LOGV(TAG, "Stop requesting data from P1 port");
|
||||
this->request_pin_->digital_write(false);
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Stop reading data from P1 port");
|
||||
}
|
||||
while (this->available()) {
|
||||
this->read();
|
||||
}
|
||||
this->requesting_data_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Dsmr::reset_telegram_() {
|
||||
this->header_found_ = false;
|
||||
this->footer_found_ = false;
|
||||
this->bytes_read_ = 0;
|
||||
this->crypt_bytes_read_ = 0;
|
||||
this->crypt_telegram_len_ = 0;
|
||||
this->last_read_time_ = 0;
|
||||
}
|
||||
|
||||
void Dsmr::receive_telegram_() {
|
||||
int count = MAX_BYTES_PER_LOOP;
|
||||
while (available() && count-- > 0) {
|
||||
const char c = read();
|
||||
while (this->available_within_timeout_()) {
|
||||
const char c = this->read();
|
||||
|
||||
// Find a new telegram header, i.e. forward slash.
|
||||
if (c == '/') {
|
||||
ESP_LOGV(TAG, "Header found");
|
||||
header_found_ = true;
|
||||
footer_found_ = false;
|
||||
telegram_len_ = 0;
|
||||
ESP_LOGV(TAG, "Header of telegram found");
|
||||
this->reset_telegram_();
|
||||
this->header_found_ = true;
|
||||
}
|
||||
if (!header_found_)
|
||||
if (!this->header_found_)
|
||||
continue;
|
||||
|
||||
// Check for buffer overflow.
|
||||
if (telegram_len_ >= MAX_TELEGRAM_LENGTH) {
|
||||
header_found_ = false;
|
||||
footer_found_ = false;
|
||||
ESP_LOGE(TAG, "Error: Message larger than buffer");
|
||||
if (this->bytes_read_ >= this->max_telegram_len_) {
|
||||
this->reset_telegram_();
|
||||
ESP_LOGE(TAG, "Error: telegram larger than buffer (%d bytes)", this->max_telegram_len_);
|
||||
return;
|
||||
}
|
||||
|
||||
// Some v2.2 or v3 meters will send a new value which starts with '('
|
||||
// in a new line while the value belongs to the previous ObisId. For
|
||||
// proper parsing remove these new line characters
|
||||
while (c == '(' && (telegram_[telegram_len_ - 1] == '\n' || telegram_[telegram_len_ - 1] == '\r'))
|
||||
telegram_len_--;
|
||||
// in a new line, while the value belongs to the previous ObisId. For
|
||||
// proper parsing, remove these new line characters.
|
||||
if (c == '(') {
|
||||
while (true) {
|
||||
auto previous_char = this->telegram_[this->bytes_read_ - 1];
|
||||
if (previous_char == '\n' || previous_char == '\r') {
|
||||
this->bytes_read_--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store the byte in the buffer.
|
||||
telegram_[telegram_len_] = c;
|
||||
telegram_len_++;
|
||||
this->telegram_[this->bytes_read_] = c;
|
||||
this->bytes_read_++;
|
||||
|
||||
// Check for a footer, i.e. exlamation mark, followed by a hex checksum.
|
||||
if (c == '!') {
|
||||
ESP_LOGV(TAG, "Footer found");
|
||||
footer_found_ = true;
|
||||
ESP_LOGV(TAG, "Footer of telegram found");
|
||||
this->footer_found_ = true;
|
||||
continue;
|
||||
}
|
||||
// Check for the end of the hex checksum, i.e. a newline.
|
||||
if (footer_found_ && c == '\n') {
|
||||
header_found_ = false;
|
||||
if (this->footer_found_ && c == '\n') {
|
||||
// Parse the telegram and publish sensor values.
|
||||
if (parse_telegram())
|
||||
return;
|
||||
this->parse_telegram();
|
||||
this->reset_telegram_();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Dsmr::receive_encrypted_() {
|
||||
// Encrypted buffer
|
||||
uint8_t buffer[MAX_TELEGRAM_LENGTH];
|
||||
size_t buffer_length = 0;
|
||||
void Dsmr::receive_encrypted_telegram_() {
|
||||
while (this->available_within_timeout_()) {
|
||||
const char c = this->read();
|
||||
|
||||
size_t packet_size = 0;
|
||||
while (available()) {
|
||||
const char c = read();
|
||||
|
||||
if (!header_found_) {
|
||||
if ((uint8_t) c == 0xdb) {
|
||||
ESP_LOGV(TAG, "Start byte 0xDB found");
|
||||
header_found_ = true;
|
||||
// Find a new telegram start byte.
|
||||
if (!this->header_found_) {
|
||||
if ((uint8_t) c != 0xDB) {
|
||||
continue;
|
||||
}
|
||||
ESP_LOGV(TAG, "Start byte 0xDB of encrypted telegram found");
|
||||
this->reset_telegram_();
|
||||
this->header_found_ = true;
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
if (!header_found_ || buffer_length >= MAX_TELEGRAM_LENGTH) {
|
||||
if (buffer_length == 0) {
|
||||
ESP_LOGE(TAG, "First byte of encrypted telegram should be 0xDB, aborting.");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unexpected data");
|
||||
}
|
||||
this->status_momentary_warning("unexpected_data");
|
||||
this->flush();
|
||||
while (available())
|
||||
read();
|
||||
// Check for buffer overflow.
|
||||
if (this->crypt_bytes_read_ >= this->max_telegram_len_) {
|
||||
this->reset_telegram_();
|
||||
ESP_LOGE(TAG, "Error: encrypted telegram larger than buffer (%d bytes)", this->max_telegram_len_);
|
||||
return;
|
||||
}
|
||||
|
||||
buffer[buffer_length++] = c;
|
||||
// Store the byte in the buffer.
|
||||
this->crypt_telegram_[this->crypt_bytes_read_] = c;
|
||||
this->crypt_bytes_read_++;
|
||||
|
||||
if (packet_size == 0 && buffer_length > 20) {
|
||||
// Complete header + a few bytes of data
|
||||
packet_size = buffer[11] << 8 | buffer[12];
|
||||
}
|
||||
if (buffer_length == packet_size + 13 && packet_size > 0) {
|
||||
ESP_LOGV(TAG, "Encrypted data: %d bytes", buffer_length);
|
||||
|
||||
GCM<AES128> *gcmaes128{new GCM<AES128>()};
|
||||
gcmaes128->setKey(this->decryption_key_.data(), gcmaes128->keySize());
|
||||
// the iv is 8 bytes of the system title + 4 bytes frame counter
|
||||
// system title is at byte 2 and frame counter at byte 15
|
||||
for (int i = 10; i < 14; i++)
|
||||
buffer[i] = buffer[i + 4];
|
||||
constexpr uint16_t iv_size{12};
|
||||
gcmaes128->setIV(&buffer[2], iv_size);
|
||||
gcmaes128->decrypt(reinterpret_cast<uint8_t *>(this->telegram_),
|
||||
// the ciphertext start at byte 18
|
||||
&buffer[18],
|
||||
// cipher size
|
||||
buffer_length - 17);
|
||||
delete gcmaes128; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
|
||||
telegram_len_ = strnlen(this->telegram_, sizeof(this->telegram_));
|
||||
ESP_LOGV(TAG, "Decrypted data length: %d", telegram_len_);
|
||||
ESP_LOGVV(TAG, "Decrypted data %s", this->telegram_);
|
||||
|
||||
parse_telegram();
|
||||
telegram_len_ = 0;
|
||||
return;
|
||||
// Read the length of the incoming encrypted telegram.
|
||||
if (this->crypt_telegram_len_ == 0 && this->crypt_bytes_read_ > 20) {
|
||||
// Complete header + data bytes
|
||||
this->crypt_telegram_len_ = 13 + (this->crypt_telegram_[11] << 8 | this->crypt_telegram_[12]);
|
||||
ESP_LOGV(TAG, "Encrypted telegram length: %d bytes", this->crypt_telegram_len_);
|
||||
}
|
||||
|
||||
if (!available()) {
|
||||
// baud rate is 115200 for encrypted data, this means a few byte should arrive every time
|
||||
// program runs faster than buffer loading then available() might return false in the middle
|
||||
delay(4); // Wait for data
|
||||
// Check for the end of the encrypted telegram.
|
||||
if (this->crypt_telegram_len_ == 0 || this->crypt_bytes_read_ != this->crypt_telegram_len_) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (buffer_length > 0) {
|
||||
ESP_LOGW(TAG, "Timeout while waiting for encrypted data or invalid data received.");
|
||||
ESP_LOGV(TAG, "End of encrypted telegram found");
|
||||
|
||||
// Decrypt the encrypted telegram.
|
||||
GCM<AES128> *gcmaes128{new GCM<AES128>()};
|
||||
gcmaes128->setKey(this->decryption_key_.data(), gcmaes128->keySize());
|
||||
// the iv is 8 bytes of the system title + 4 bytes frame counter
|
||||
// system title is at byte 2 and frame counter at byte 15
|
||||
for (int i = 10; i < 14; i++)
|
||||
this->crypt_telegram_[i] = this->crypt_telegram_[i + 4];
|
||||
constexpr uint16_t iv_size{12};
|
||||
gcmaes128->setIV(&this->crypt_telegram_[2], iv_size);
|
||||
gcmaes128->decrypt(reinterpret_cast<uint8_t *>(this->telegram_),
|
||||
// the ciphertext start at byte 18
|
||||
&this->crypt_telegram_[18],
|
||||
// cipher size
|
||||
this->crypt_bytes_read_ - 17);
|
||||
delete gcmaes128; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
|
||||
this->bytes_read_ = strnlen(this->telegram_, this->max_telegram_len_);
|
||||
ESP_LOGV(TAG, "Decrypted telegram size: %d bytes", this->bytes_read_);
|
||||
ESP_LOGVV(TAG, "Decrypted telegram: %s", this->telegram_);
|
||||
|
||||
// Parse the decrypted telegram and publish sensor values.
|
||||
this->parse_telegram();
|
||||
this->reset_telegram_();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool Dsmr::parse_telegram() {
|
||||
MyData data;
|
||||
ESP_LOGV(TAG, "Trying to parse");
|
||||
ESP_LOGV(TAG, "Trying to parse telegram");
|
||||
this->stop_requesting_data_();
|
||||
::dsmr::ParseResult<void> res =
|
||||
::dsmr::P1Parser::parse(&data, telegram_, telegram_len_, false,
|
||||
::dsmr::P1Parser::parse(&data, this->telegram_, this->bytes_read_, false,
|
||||
this->crc_check_); // Parse telegram according to data definition. Ignore unknown values.
|
||||
if (res.err) {
|
||||
// Parsing error, show it
|
||||
auto err_str = res.fullError(telegram_, telegram_ + telegram_len_);
|
||||
auto err_str = res.fullError(this->telegram_, this->telegram_ + this->bytes_read_);
|
||||
ESP_LOGE(TAG, "%s", err_str.c_str());
|
||||
return false;
|
||||
} else {
|
||||
this->status_clear_warning();
|
||||
publish_sensors(data);
|
||||
this->publish_sensors(data);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void Dsmr::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "dsmr:");
|
||||
ESP_LOGCONFIG(TAG, "DSMR:");
|
||||
ESP_LOGCONFIG(TAG, " Max telegram length: %d", this->max_telegram_len_);
|
||||
ESP_LOGCONFIG(TAG, " Receive timeout: %.1fs", this->receive_timeout_ / 1e3f);
|
||||
if (this->request_pin_ != nullptr) {
|
||||
LOG_PIN(" Request Pin: ", this->request_pin_);
|
||||
}
|
||||
if (this->request_interval_ > 0) {
|
||||
ESP_LOGCONFIG(TAG, " Request Interval: %.1fs", this->request_interval_ / 1e3f);
|
||||
}
|
||||
|
||||
#define DSMR_LOG_SENSOR(s) LOG_SENSOR(" ", #s, this->s_##s##_);
|
||||
DSMR_SENSOR_LIST(DSMR_LOG_SENSOR, )
|
||||
@@ -174,23 +293,31 @@ void Dsmr::set_decryption_key(const std::string &decryption_key) {
|
||||
if (decryption_key.length() == 0) {
|
||||
ESP_LOGI(TAG, "Disabling decryption");
|
||||
this->decryption_key_.clear();
|
||||
if (this->crypt_telegram_ != nullptr) {
|
||||
delete[] this->crypt_telegram_;
|
||||
this->crypt_telegram_ = nullptr;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (decryption_key.length() != 32) {
|
||||
ESP_LOGE(TAG, "Error, decryption key must be 32 character long.");
|
||||
ESP_LOGE(TAG, "Error, decryption key must be 32 character long");
|
||||
return;
|
||||
}
|
||||
this->decryption_key_.clear();
|
||||
|
||||
ESP_LOGI(TAG, "Decryption key is set.");
|
||||
ESP_LOGI(TAG, "Decryption key is set");
|
||||
// Verbose level prints decryption key
|
||||
ESP_LOGV(TAG, "Using decryption key: %s", decryption_key.c_str());
|
||||
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(decryption_key.c_str()[i * 2]), 2);
|
||||
decryption_key_.push_back(std::strtoul(temp, nullptr, 16));
|
||||
this->decryption_key_.push_back(std::strtoul(temp, nullptr, 16));
|
||||
}
|
||||
|
||||
if (this->crypt_telegram_ == nullptr) {
|
||||
this->crypt_telegram_ = new uint8_t[this->max_telegram_len_]; // NOLINT
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
namespace esphome {
|
||||
namespace dsmr {
|
||||
|
||||
static constexpr uint32_t MAX_TELEGRAM_LENGTH = 1500;
|
||||
static constexpr uint32_t MAX_BYTES_PER_LOOP = 50;
|
||||
static constexpr uint32_t POLL_TIMEOUT = 1000;
|
||||
|
||||
using namespace ::dsmr::fields;
|
||||
|
||||
// DSMR_**_LIST generated by ESPHome and written in esphome/core/defines
|
||||
@@ -53,6 +49,7 @@ class Dsmr : public Component, public uart::UARTDevice {
|
||||
public:
|
||||
Dsmr(uart::UARTComponent *uart, bool crc_check) : uart::UARTDevice(uart), crc_check_(crc_check) {}
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
|
||||
bool parse_telegram();
|
||||
@@ -72,6 +69,10 @@ class Dsmr : public Component, public uart::UARTDevice {
|
||||
void dump_config() override;
|
||||
|
||||
void set_decryption_key(const std::string &decryption_key);
|
||||
void set_max_telegram_length(size_t length) { this->max_telegram_len_ = length; }
|
||||
void set_request_pin(GPIOPin *request_pin) { this->request_pin_ = request_pin; }
|
||||
void set_request_interval(uint32_t interval) { this->request_interval_ = interval; }
|
||||
void set_receive_timeout(uint32_t timeout) { this->receive_timeout_ = timeout; }
|
||||
|
||||
// Sensor setters
|
||||
#define DSMR_SET_SENSOR(s) \
|
||||
@@ -84,13 +85,40 @@ class Dsmr : public Component, public uart::UARTDevice {
|
||||
|
||||
protected:
|
||||
void receive_telegram_();
|
||||
void receive_encrypted_();
|
||||
void receive_encrypted_telegram_();
|
||||
void reset_telegram_();
|
||||
|
||||
// Telegram buffer
|
||||
char telegram_[MAX_TELEGRAM_LENGTH];
|
||||
int telegram_len_{0};
|
||||
/// Wait for UART data to become available within the read timeout.
|
||||
///
|
||||
/// The smart meter might provide data in chunks, causing available() to
|
||||
/// return 0. When we're already reading a telegram, then we don't return
|
||||
/// right away (to handle further data in an upcoming loop) but wait a
|
||||
/// little while using this method to see if more data are incoming.
|
||||
/// By not returning, we prevent other components from taking so much
|
||||
/// time that the UART RX buffer overflows and bytes of the telegram get
|
||||
/// lost in the process.
|
||||
bool available_within_timeout_();
|
||||
|
||||
// Serial parser
|
||||
// Request telegram
|
||||
uint32_t request_interval_;
|
||||
bool request_interval_reached_();
|
||||
GPIOPin *request_pin_{nullptr};
|
||||
uint32_t last_request_time_{0};
|
||||
bool requesting_data_{false};
|
||||
bool ready_to_request_data_();
|
||||
void start_requesting_data_();
|
||||
void stop_requesting_data_();
|
||||
|
||||
// Read telegram
|
||||
uint32_t receive_timeout_;
|
||||
bool receive_timeout_reached_();
|
||||
size_t max_telegram_len_;
|
||||
char *telegram_{nullptr};
|
||||
size_t bytes_read_{0};
|
||||
uint8_t *crypt_telegram_{nullptr};
|
||||
size_t crypt_telegram_len_{0};
|
||||
size_t crypt_bytes_read_{0};
|
||||
uint32_t last_read_time_{0};
|
||||
bool header_found_{false};
|
||||
bool footer_found_{false};
|
||||
|
||||
|
||||
@@ -75,14 +75,14 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE_HOURS,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_ENERGY,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_NONE,
|
||||
),
|
||||
cv.Optional("total_exported_energy"): sensor.sensor_schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE_HOURS,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_ENERGY,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_NONE,
|
||||
),
|
||||
cv.Optional("power_delivered"): sensor.sensor_schema(
|
||||
@@ -166,42 +166,42 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional("reactive_power_delivered_l2"): sensor.sensor_schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional("reactive_power_delivered_l3"): sensor.sensor_schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional("reactive_power_returned_l1"): sensor.sensor_schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional("reactive_power_returned_l2"): sensor.sensor_schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional("reactive_power_returned_l3"): sensor.sensor_schema(
|
||||
UNIT_KILOVOLT_AMPS_REACTIVE,
|
||||
ICON_EMPTY,
|
||||
3,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional("voltage_l1"): sensor.sensor_schema(
|
||||
|
||||
@@ -12,7 +12,6 @@ void DutyCycleSensor::setup() {
|
||||
this->pin_->setup();
|
||||
this->store_.pin = this->pin_->to_isr();
|
||||
this->store_.last_level = this->pin_->digital_read();
|
||||
this->last_update_ = micros();
|
||||
this->store_.last_interrupt = micros();
|
||||
|
||||
this->pin_->attach_interrupt(DutyCycleSensorStore::gpio_intr, &this->store_, gpio::INTERRUPT_ANY_EDGE);
|
||||
@@ -24,21 +23,24 @@ void DutyCycleSensor::dump_config() {
|
||||
}
|
||||
void DutyCycleSensor::update() {
|
||||
const uint32_t now = micros();
|
||||
const bool level = this->store_.last_level;
|
||||
const uint32_t last_interrupt = this->store_.last_interrupt;
|
||||
const uint32_t last_interrupt = this->store_.last_interrupt; // Read the measurement taken by the interrupt
|
||||
uint32_t on_time = this->store_.on_time;
|
||||
|
||||
if (level)
|
||||
on_time += now - last_interrupt;
|
||||
|
||||
const float total_time = float(now - this->last_update_);
|
||||
|
||||
const float value = (on_time / total_time) * 100.0f;
|
||||
ESP_LOGD(TAG, "'%s' Got duty cycle=%.1f%%", this->get_name().c_str(), value);
|
||||
this->publish_state(value);
|
||||
|
||||
this->store_.on_time = 0;
|
||||
this->store_.on_time = 0; // Start new measurement, exactly aligned with the micros() reading
|
||||
this->store_.last_interrupt = now;
|
||||
|
||||
if (this->last_update_ != 0) {
|
||||
const bool level = this->store_.last_level;
|
||||
|
||||
if (level)
|
||||
on_time += now - last_interrupt;
|
||||
|
||||
const float total_time = float(now - this->last_update_);
|
||||
|
||||
const float value = (on_time * 100.0f) / total_time;
|
||||
ESP_LOGD(TAG, "'%s' Got duty cycle=%.1f%%", this->get_name().c_str(), value);
|
||||
this->publish_state(value);
|
||||
}
|
||||
this->last_update_ = now;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ class DutyCycleSensor : public sensor::Sensor, public PollingComponent {
|
||||
InternalGPIOPin *pin_;
|
||||
|
||||
DutyCycleSensorStore store_{};
|
||||
uint32_t last_update_;
|
||||
uint32_t last_update_{0};
|
||||
};
|
||||
|
||||
} // namespace duty_cycle
|
||||
|
||||
@@ -28,6 +28,7 @@ from .const import ( # noqa
|
||||
KEY_SDKCONFIG_OPTIONS,
|
||||
KEY_VARIANT,
|
||||
VARIANT_ESP32C3,
|
||||
VARIANT_FRIENDLY,
|
||||
VARIANTS,
|
||||
)
|
||||
from .boards import BOARD_TO_VARIANT
|
||||
@@ -147,8 +148,9 @@ def _arduino_check_versions(value):
|
||||
value[CONF_VERSION] = str(version)
|
||||
value[CONF_SOURCE] = source or _format_framework_arduino_version(version)
|
||||
|
||||
platform_version = value.get(CONF_PLATFORM_VERSION, ARDUINO_PLATFORM_VERSION)
|
||||
value[CONF_PLATFORM_VERSION] = str(platform_version)
|
||||
value[CONF_PLATFORM_VERSION] = value.get(
|
||||
CONF_PLATFORM_VERSION, _parse_platform_version(str(ARDUINO_PLATFORM_VERSION))
|
||||
)
|
||||
|
||||
if version != RECOMMENDED_ARDUINO_FRAMEWORK_VERSION:
|
||||
_LOGGER.warning(
|
||||
@@ -184,8 +186,9 @@ def _esp_idf_check_versions(value):
|
||||
value[CONF_VERSION] = str(version)
|
||||
value[CONF_SOURCE] = source or _format_framework_espidf_version(version)
|
||||
|
||||
platform_version = value.get(CONF_PLATFORM_VERSION, ESP_IDF_PLATFORM_VERSION)
|
||||
value[CONF_PLATFORM_VERSION] = str(platform_version)
|
||||
value[CONF_PLATFORM_VERSION] = value.get(
|
||||
CONF_PLATFORM_VERSION, _parse_platform_version(str(ESP_IDF_PLATFORM_VERSION))
|
||||
)
|
||||
|
||||
if version != RECOMMENDED_ESP_IDF_FRAMEWORK_VERSION:
|
||||
_LOGGER.warning(
|
||||
@@ -196,6 +199,15 @@ def _esp_idf_check_versions(value):
|
||||
return value
|
||||
|
||||
|
||||
def _parse_platform_version(value):
|
||||
try:
|
||||
# if platform version is a valid version constraint, prefix the default package
|
||||
cv.platformio_version_constraint(value)
|
||||
return f"platformio/espressif32 @ {value}"
|
||||
except cv.Invalid:
|
||||
return value
|
||||
|
||||
|
||||
def _detect_variant(value):
|
||||
if CONF_VARIANT not in value:
|
||||
board = value[CONF_BOARD]
|
||||
@@ -218,7 +230,7 @@ ARDUINO_FRAMEWORK_SCHEMA = cv.All(
|
||||
{
|
||||
cv.Optional(CONF_VERSION, default="recommended"): cv.string_strict,
|
||||
cv.Optional(CONF_SOURCE): cv.string_strict,
|
||||
cv.Optional(CONF_PLATFORM_VERSION): cv.string_strict,
|
||||
cv.Optional(CONF_PLATFORM_VERSION): _parse_platform_version,
|
||||
}
|
||||
),
|
||||
_arduino_check_versions,
|
||||
@@ -230,7 +242,7 @@ ESP_IDF_FRAMEWORK_SCHEMA = cv.All(
|
||||
{
|
||||
cv.Optional(CONF_VERSION, default="recommended"): cv.string_strict,
|
||||
cv.Optional(CONF_SOURCE): cv.string_strict,
|
||||
cv.Optional(CONF_PLATFORM_VERSION): cv.string_strict,
|
||||
cv.Optional(CONF_PLATFORM_VERSION): _parse_platform_version,
|
||||
cv.Optional(CONF_SDKCONFIG_OPTIONS, default={}): {
|
||||
cv.string_strict: cv.string_strict
|
||||
},
|
||||
@@ -276,14 +288,14 @@ async def to_code(config):
|
||||
cg.add_build_flag("-DUSE_ESP32")
|
||||
cg.add_define("ESPHOME_BOARD", config[CONF_BOARD])
|
||||
cg.add_build_flag(f"-DUSE_ESP32_VARIANT_{config[CONF_VARIANT]}")
|
||||
cg.add_define("ESPHOME_VARIANT", VARIANT_FRIENDLY[config[CONF_VARIANT]])
|
||||
|
||||
cg.add_platformio_option("lib_ldf_mode", "off")
|
||||
|
||||
conf = config[CONF_FRAMEWORK]
|
||||
cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
|
||||
|
||||
if conf[CONF_TYPE] == FRAMEWORK_ESP_IDF:
|
||||
cg.add_platformio_option(
|
||||
"platform", f"espressif32 @ {conf[CONF_PLATFORM_VERSION]}"
|
||||
)
|
||||
cg.add_platformio_option("framework", "espidf")
|
||||
cg.add_build_flag("-DUSE_ESP_IDF")
|
||||
cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ESP_IDF")
|
||||
@@ -299,9 +311,16 @@ async def to_code(config):
|
||||
)
|
||||
add_idf_sdkconfig_option("CONFIG_COMPILER_OPTIMIZATION_DEFAULT", False)
|
||||
add_idf_sdkconfig_option("CONFIG_COMPILER_OPTIMIZATION_SIZE", True)
|
||||
|
||||
# Increase freertos tick speed from 100Hz to 1kHz so that delay() resolution is 1ms
|
||||
add_idf_sdkconfig_option("CONFIG_FREERTOS_HZ", 1000)
|
||||
|
||||
# Setup watchdog
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT", True)
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True)
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
|
||||
|
||||
cg.add_platformio_option("board_build.partitions", "partitions.csv")
|
||||
|
||||
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
|
||||
@@ -314,9 +333,6 @@ async def to_code(config):
|
||||
)
|
||||
|
||||
elif conf[CONF_TYPE] == FRAMEWORK_ARDUINO:
|
||||
cg.add_platformio_option(
|
||||
"platform", f"espressif32 @ {conf[CONF_PLATFORM_VERSION]}"
|
||||
)
|
||||
cg.add_platformio_option("framework", "arduino")
|
||||
cg.add_build_flag("-DUSE_ARDUINO")
|
||||
cg.add_build_flag("-DUSE_ESP32_FRAMEWORK_ARDUINO")
|
||||
|
||||
@@ -18,4 +18,12 @@ VARIANTS = [
|
||||
VARIANT_ESP32H2,
|
||||
]
|
||||
|
||||
VARIANT_FRIENDLY = {
|
||||
VARIANT_ESP32: "ESP32",
|
||||
VARIANT_ESP32S2: "ESP32-S2",
|
||||
VARIANT_ESP32S3: "ESP32-S3",
|
||||
VARIANT_ESP32C3: "ESP32-C3",
|
||||
VARIANT_ESP32H2: "ESP32-H2",
|
||||
}
|
||||
|
||||
esp32_ns = cg.esphome_ns.namespace("esp32")
|
||||
|
||||
@@ -6,12 +6,17 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_idf_version.h>
|
||||
#include <esp_task_wdt.h>
|
||||
#include <soc/rtc.h>
|
||||
|
||||
#if ESP_IDF_VERSION_MAJOR >= 4
|
||||
#include <hal/cpu_hal.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include <esp32-hal.h>
|
||||
#endif
|
||||
|
||||
void setup();
|
||||
void loop();
|
||||
|
||||
@@ -21,11 +26,7 @@ void IRAM_ATTR HOT yield() { vPortYield(); }
|
||||
uint32_t IRAM_ATTR HOT millis() { return (uint32_t)(esp_timer_get_time() / 1000ULL); }
|
||||
void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
|
||||
uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); }
|
||||
void IRAM_ATTR HOT delayMicroseconds(uint32_t us) {
|
||||
auto start = (uint64_t) esp_timer_get_time();
|
||||
while (((uint64_t) esp_timer_get_time()) - start < us)
|
||||
;
|
||||
}
|
||||
void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
|
||||
void arch_restart() {
|
||||
esp_restart();
|
||||
// restart() doesn't always end execution
|
||||
@@ -33,24 +34,24 @@ void arch_restart() {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
void IRAM_ATTR HOT arch_feed_wdt() {
|
||||
#ifdef USE_ARDUINO
|
||||
#if CONFIG_ARDUINO_RUNNING_CORE == 0
|
||||
#ifdef CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0
|
||||
// ESP32 uses "Task Watchdog" which is hooked to the FreeRTOS idle task.
|
||||
// To cause the Watchdog to be triggered we need to put the current task
|
||||
// to sleep to get the idle task scheduled.
|
||||
delay(1);
|
||||
#endif
|
||||
#endif
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
#ifdef USE_ESP_IDF
|
||||
#ifdef CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0
|
||||
delay(1);
|
||||
void arch_init() {
|
||||
// Enable the task watchdog only on the loop task (from which we're currently running)
|
||||
#if defined(USE_ESP_IDF)
|
||||
esp_task_wdt_add(nullptr);
|
||||
// Idle task watchdog is disabled on ESP-IDF
|
||||
#elif defined(USE_ARDUINO)
|
||||
enableLoopWDT();
|
||||
// Disable idle task watchdog on the core we're using (Arduino pins the task to a core)
|
||||
#if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0) && CONFIG_ARDUINO_RUNNING_CORE == 0
|
||||
disableCore0WDT();
|
||||
#endif
|
||||
#if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1) && CONFIG_ARDUINO_RUNNING_CORE == 1
|
||||
disableCore1WDT();
|
||||
#endif
|
||||
#endif
|
||||
#endif // USE_ESP_IDF
|
||||
}
|
||||
void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); }
|
||||
|
||||
uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
|
||||
uint32_t arch_get_cpu_cycle_count() {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
@@ -17,10 +18,24 @@ import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
|
||||
from . import boards
|
||||
from .const import KEY_BOARD, KEY_ESP32, esp32_ns
|
||||
from .const import (
|
||||
KEY_BOARD,
|
||||
KEY_ESP32,
|
||||
KEY_VARIANT,
|
||||
VARIANT_ESP32,
|
||||
VARIANT_ESP32C3,
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
VARIANT_ESP32H2,
|
||||
esp32_ns,
|
||||
)
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
from .gpio_esp32 import esp32_validate_gpio_pin, esp32_validate_supports
|
||||
from .gpio_esp32_s2 import esp32_s2_validate_gpio_pin, esp32_s2_validate_supports
|
||||
from .gpio_esp32_c3 import esp32_c3_validate_gpio_pin, esp32_c3_validate_supports
|
||||
from .gpio_esp32_s3 import esp32_s3_validate_gpio_pin, esp32_s3_validate_supports
|
||||
from .gpio_esp32_h2 import esp32_h2_validate_gpio_pin, esp32_h2_validate_supports
|
||||
|
||||
|
||||
IDFInternalGPIOPin = esp32_ns.class_("IDFInternalGPIOPin", cg.InternalGPIOPin)
|
||||
@@ -59,65 +74,61 @@ def _translate_pin(value):
|
||||
return _lookup_pin(value)
|
||||
|
||||
|
||||
_ESP_SDIO_PINS = {
|
||||
6: "Flash Clock",
|
||||
7: "Flash Data 0",
|
||||
8: "Flash Data 1",
|
||||
11: "Flash Command",
|
||||
@dataclass
|
||||
class ESP32ValidationFunctions:
|
||||
pin_validation: Any
|
||||
usage_validation: Any
|
||||
|
||||
|
||||
_esp32_validations = {
|
||||
VARIANT_ESP32: ESP32ValidationFunctions(
|
||||
pin_validation=esp32_validate_gpio_pin, usage_validation=esp32_validate_supports
|
||||
),
|
||||
VARIANT_ESP32S2: ESP32ValidationFunctions(
|
||||
pin_validation=esp32_s2_validate_gpio_pin,
|
||||
usage_validation=esp32_s2_validate_supports,
|
||||
),
|
||||
VARIANT_ESP32C3: ESP32ValidationFunctions(
|
||||
pin_validation=esp32_c3_validate_gpio_pin,
|
||||
usage_validation=esp32_c3_validate_supports,
|
||||
),
|
||||
VARIANT_ESP32S3: ESP32ValidationFunctions(
|
||||
pin_validation=esp32_s3_validate_gpio_pin,
|
||||
usage_validation=esp32_s3_validate_supports,
|
||||
),
|
||||
VARIANT_ESP32H2: ESP32ValidationFunctions(
|
||||
pin_validation=esp32_h2_validate_gpio_pin,
|
||||
usage_validation=esp32_h2_validate_supports,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def validate_gpio_pin(value):
|
||||
value = _translate_pin(value)
|
||||
if value < 0 or value > 39:
|
||||
raise cv.Invalid(f"Invalid pin number: {value} (must be 0-39)")
|
||||
if value in _ESP_SDIO_PINS:
|
||||
raise cv.Invalid(
|
||||
f"This pin cannot be used on ESP32s and is already used by the flash interface (function: {_ESP_SDIO_PINS[value]})"
|
||||
)
|
||||
if 9 <= value <= 10:
|
||||
_LOGGER.warning(
|
||||
"Pin %s (9-10) might already be used by the "
|
||||
"flash interface in QUAD IO flash mode.",
|
||||
value,
|
||||
)
|
||||
if value in (20, 24, 28, 29, 30, 31):
|
||||
# These pins are not exposed in GPIO mux (reason unknown)
|
||||
# but they're missing from IO_MUX list in datasheet
|
||||
raise cv.Invalid(f"The pin GPIO{value} is not usable on ESP32s.")
|
||||
return value
|
||||
variant = CORE.data[KEY_ESP32][KEY_VARIANT]
|
||||
if variant not in _esp32_validations:
|
||||
raise cv.Invalid("Unsupported ESP32 variant {variant}")
|
||||
|
||||
return _esp32_validations[variant].pin_validation(value)
|
||||
|
||||
|
||||
def validate_supports(value):
|
||||
num = value[CONF_NUMBER]
|
||||
mode = value[CONF_MODE]
|
||||
is_input = mode[CONF_INPUT]
|
||||
is_output = mode[CONF_OUTPUT]
|
||||
is_open_drain = mode[CONF_OPEN_DRAIN]
|
||||
is_pullup = mode[CONF_PULLUP]
|
||||
is_pulldown = mode[CONF_PULLDOWN]
|
||||
variant = CORE.data[KEY_ESP32][KEY_VARIANT]
|
||||
if variant not in _esp32_validations:
|
||||
raise cv.Invalid("Unsupported ESP32 variant {variant}")
|
||||
|
||||
if is_input:
|
||||
# All ESP32 pins support input mode
|
||||
pass
|
||||
if is_output and 34 <= num <= 39:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} (34-39) does not support output pin mode.",
|
||||
[CONF_MODE, CONF_OUTPUT],
|
||||
)
|
||||
if is_open_drain and not is_output:
|
||||
raise cv.Invalid(
|
||||
"Open-drain only works with output mode", [CONF_MODE, CONF_OPEN_DRAIN]
|
||||
)
|
||||
if is_pullup and 34 <= num <= 39:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} (34-39) does not support pullups.", [CONF_MODE, CONF_PULLUP]
|
||||
)
|
||||
if is_pulldown and 34 <= num <= 39:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} (34-39) does not support pulldowns.", [CONF_MODE, CONF_PULLDOWN]
|
||||
)
|
||||
|
||||
value = _esp32_validations[variant].usage_validation(value)
|
||||
if CORE.using_arduino:
|
||||
# (input, output, open_drain, pullup, pulldown)
|
||||
supported_modes = {
|
||||
@@ -138,7 +149,6 @@ def validate_supports(value):
|
||||
"This pin mode is not supported on ESP32 for arduino frameworks",
|
||||
[CONF_MODE],
|
||||
)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
|
||||
77
esphome/components/esp32/gpio_esp32.py
Normal file
77
esphome/components/esp32/gpio_esp32.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import logging
|
||||
|
||||
from esphome.const import (
|
||||
CONF_INPUT,
|
||||
CONF_MODE,
|
||||
CONF_NUMBER,
|
||||
CONF_OUTPUT,
|
||||
CONF_PULLDOWN,
|
||||
CONF_PULLUP,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
|
||||
|
||||
_ESP_SDIO_PINS = {
|
||||
6: "Flash Clock",
|
||||
7: "Flash Data 0",
|
||||
8: "Flash Data 1",
|
||||
11: "Flash Command",
|
||||
}
|
||||
|
||||
_ESP32_STRAPPING_PINS = {0, 2, 4, 12, 15}
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def esp32_validate_gpio_pin(value):
|
||||
if value < 0 or value > 39:
|
||||
raise cv.Invalid(f"Invalid pin number: {value} (must be 0-39)")
|
||||
if value in _ESP_SDIO_PINS:
|
||||
raise cv.Invalid(
|
||||
f"This pin cannot be used on ESP32s and is already used by the flash interface (function: {_ESP_SDIO_PINS[value]})"
|
||||
)
|
||||
if 9 <= value <= 10:
|
||||
_LOGGER.warning(
|
||||
"Pin %s (9-10) might already be used by the "
|
||||
"flash interface in QUAD IO flash mode.",
|
||||
value,
|
||||
)
|
||||
if value in _ESP32_STRAPPING_PINS:
|
||||
_LOGGER.warning(
|
||||
"GPIO%d is a Strapping PIN and should be avoided.\n"
|
||||
"Attaching external pullup/down resistors to strapping pins can cause unexpected failures.\n"
|
||||
"See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins",
|
||||
value,
|
||||
)
|
||||
if value in (20, 24, 28, 29, 30, 31):
|
||||
# These pins are not exposed in GPIO mux (reason unknown)
|
||||
# but they're missing from IO_MUX list in datasheet
|
||||
raise cv.Invalid(f"The pin GPIO{value} is not usable on ESP32s.")
|
||||
return value
|
||||
|
||||
|
||||
def esp32_validate_supports(value):
|
||||
num = value[CONF_NUMBER]
|
||||
mode = value[CONF_MODE]
|
||||
is_input = mode[CONF_INPUT]
|
||||
is_output = mode[CONF_OUTPUT]
|
||||
is_pullup = mode[CONF_PULLUP]
|
||||
is_pulldown = mode[CONF_PULLDOWN]
|
||||
|
||||
if is_input:
|
||||
# All ESP32 pins support input mode
|
||||
pass
|
||||
if is_output and 34 <= num <= 39:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} (34-39) does not support output pin mode.",
|
||||
[CONF_MODE, CONF_OUTPUT],
|
||||
)
|
||||
if is_pullup and 34 <= num <= 39:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} (34-39) does not support pullups.", [CONF_MODE, CONF_PULLUP]
|
||||
)
|
||||
if is_pulldown and 34 <= num <= 39:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} (34-39) does not support pulldowns.", [CONF_MODE, CONF_PULLDOWN]
|
||||
)
|
||||
|
||||
return value
|
||||
53
esphome/components/esp32/gpio_esp32_c3.py
Normal file
53
esphome/components/esp32/gpio_esp32_c3.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import logging
|
||||
|
||||
from esphome.const import (
|
||||
CONF_INPUT,
|
||||
CONF_MODE,
|
||||
CONF_NUMBER,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
|
||||
_ESP32C3_SPI_PSRAM_PINS = {
|
||||
12: "SPIHD",
|
||||
13: "SPIWP",
|
||||
14: "SPICS0",
|
||||
15: "SPICLK",
|
||||
16: "SPID",
|
||||
17: "SPIQ",
|
||||
}
|
||||
|
||||
_ESP32C3_STRAPPING_PINS = {2, 8, 9}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def esp32_c3_validate_gpio_pin(value):
|
||||
if value < 0 or value > 21:
|
||||
raise cv.Invalid(f"Invalid pin number: {value} (must be 0-21)")
|
||||
if value in _ESP32C3_SPI_PSRAM_PINS:
|
||||
raise cv.Invalid(
|
||||
f"This pin cannot be used on ESP32-C3s and is already used by the SPI/PSRAM interface (function: {_ESP32C3_SPI_PSRAM_PINS[value]})"
|
||||
)
|
||||
if value in _ESP32C3_STRAPPING_PINS:
|
||||
_LOGGER.warning(
|
||||
"GPIO%d is a Strapping PIN and should be avoided.\n"
|
||||
"Attaching external pullup/down resistors to strapping pins can cause unexpected failures.\n"
|
||||
"See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins",
|
||||
value,
|
||||
)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def esp32_c3_validate_supports(value):
|
||||
num = value[CONF_NUMBER]
|
||||
mode = value[CONF_MODE]
|
||||
is_input = mode[CONF_INPUT]
|
||||
|
||||
if num < 0 or num > 21:
|
||||
raise cv.Invalid(f"Invalid pin number: {value} (must be 0-21)")
|
||||
|
||||
if is_input:
|
||||
# All ESP32 pins support input mode
|
||||
pass
|
||||
return value
|
||||
11
esphome/components/esp32/gpio_esp32_h2.py
Normal file
11
esphome/components/esp32/gpio_esp32_h2.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import esphome.config_validation as cv
|
||||
|
||||
|
||||
def esp32_h2_validate_gpio_pin(value):
|
||||
# ESP32-H2 not yet supported
|
||||
raise cv.Invalid("ESP32-H2 isn't supported yet")
|
||||
|
||||
|
||||
def esp32_h2_validate_supports(value):
|
||||
# ESP32-H2 not yet supported
|
||||
raise cv.Invalid("ESP32-H2 isn't supported yet")
|
||||
80
esphome/components/esp32/gpio_esp32_s2.py
Normal file
80
esphome/components/esp32/gpio_esp32_s2.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import logging
|
||||
|
||||
from esphome.const import (
|
||||
CONF_INPUT,
|
||||
CONF_MODE,
|
||||
CONF_NUMBER,
|
||||
CONF_OUTPUT,
|
||||
CONF_PULLDOWN,
|
||||
CONF_PULLUP,
|
||||
)
|
||||
|
||||
import esphome.config_validation as cv
|
||||
|
||||
_ESP32S2_SPI_PSRAM_PINS = {
|
||||
26: "SPICS1",
|
||||
27: "SPIHD",
|
||||
28: "SPIWP",
|
||||
29: "SPICS0",
|
||||
30: "SPICLK",
|
||||
31: "SPIQ",
|
||||
32: "SPID",
|
||||
}
|
||||
|
||||
_ESP32S2_STRAPPING_PINS = {0, 45, 46}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def esp32_s2_validate_gpio_pin(value):
|
||||
if value < 0 or value > 46:
|
||||
raise cv.Invalid(f"Invalid pin number: {value} (must be 0-46)")
|
||||
|
||||
if value in _ESP32S2_SPI_PSRAM_PINS:
|
||||
raise cv.Invalid(
|
||||
f"This pin cannot be used on ESP32-S2s and is already used by the SPI/PSRAM interface (function: {_ESP32S2_SPI_PSRAM_PINS[value]})"
|
||||
)
|
||||
if value in _ESP32S2_STRAPPING_PINS:
|
||||
_LOGGER.warning(
|
||||
"GPIO%d is a Strapping PIN and should be avoided.\n"
|
||||
"Attaching external pullup/down resistors to strapping pins can cause unexpected failures.\n"
|
||||
"See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins",
|
||||
value,
|
||||
)
|
||||
|
||||
if value in (22, 23, 24, 25):
|
||||
# These pins are not exposed in GPIO mux (reason unknown)
|
||||
# but they're missing from IO_MUX list in datasheet
|
||||
raise cv.Invalid(f"The pin GPIO{value} is not usable on ESP32-S2s.")
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def esp32_s2_validate_supports(value):
|
||||
num = value[CONF_NUMBER]
|
||||
mode = value[CONF_MODE]
|
||||
is_input = mode[CONF_INPUT]
|
||||
is_output = mode[CONF_OUTPUT]
|
||||
is_pullup = mode[CONF_PULLUP]
|
||||
is_pulldown = mode[CONF_PULLDOWN]
|
||||
|
||||
if num < 0 or num > 46:
|
||||
raise cv.Invalid(f"Invalid pin number: {num} (must be 0-46)")
|
||||
if is_input:
|
||||
# All ESP32 pins support input mode
|
||||
pass
|
||||
if is_output and num == 46:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} does not support output pin mode.",
|
||||
[CONF_MODE, CONF_OUTPUT],
|
||||
)
|
||||
if is_pullup and num == 46:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} does not support pullups.", [CONF_MODE, CONF_PULLUP]
|
||||
)
|
||||
if is_pulldown and num == 46:
|
||||
raise cv.Invalid(
|
||||
f"GPIO{num} does not support pulldowns.", [CONF_MODE, CONF_PULLDOWN]
|
||||
)
|
||||
|
||||
return value
|
||||
74
esphome/components/esp32/gpio_esp32_s3.py
Normal file
74
esphome/components/esp32/gpio_esp32_s3.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import logging
|
||||
|
||||
from esphome.const import (
|
||||
CONF_INPUT,
|
||||
CONF_MODE,
|
||||
CONF_NUMBER,
|
||||
)
|
||||
|
||||
import esphome.config_validation as cv
|
||||
|
||||
_ESP_32S3_SPI_PSRAM_PINS = {
|
||||
26: "SPICS1",
|
||||
27: "SPIHD",
|
||||
28: "SPIWP",
|
||||
29: "SPICS0",
|
||||
30: "SPICLK",
|
||||
31: "SPIQ",
|
||||
32: "SPID",
|
||||
}
|
||||
|
||||
_ESP_32_ESP32_S3R8_PSRAM_PINS = {
|
||||
33: "SPIIO4",
|
||||
34: "SPIIO5",
|
||||
35: "SPIIO6",
|
||||
36: "SPIIO7",
|
||||
37: "SPIDQS",
|
||||
}
|
||||
|
||||
_ESP_32S3_STRAPPING_PINS = {0, 3, 45, 46}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def esp32_s3_validate_gpio_pin(value):
|
||||
if value < 0 or value > 48:
|
||||
raise cv.Invalid(f"Invalid pin number: {value} (must be 0-46)")
|
||||
|
||||
if value in _ESP_32S3_SPI_PSRAM_PINS:
|
||||
raise cv.Invalid(
|
||||
f"This pin cannot be used on ESP32-S3s and is already used by the SPI/PSRAM interface(function: {_ESP_32S3_SPI_PSRAM_PINS[value]})"
|
||||
)
|
||||
if value in _ESP_32_ESP32_S3R8_PSRAM_PINS:
|
||||
_LOGGER.warning(
|
||||
"GPIO%d is used by the PSRAM interface on ESP32-S3R8 / ESP32-S3R8V and should be avoided on these models",
|
||||
value,
|
||||
)
|
||||
|
||||
if value in _ESP_32S3_STRAPPING_PINS:
|
||||
_LOGGER.warning(
|
||||
"GPIO%d is a Strapping PIN and should be avoided.\n"
|
||||
"Attaching external pullup/down resistors to strapping pins can cause unexpected failures.\n"
|
||||
"See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins",
|
||||
value,
|
||||
)
|
||||
|
||||
if value in (22, 23, 24, 25):
|
||||
# These pins are not exposed in GPIO mux (reason unknown)
|
||||
# but they're missing from IO_MUX list in datasheet
|
||||
raise cv.Invalid(f"The pin GPIO{value} is not usable on ESP32-S3s.")
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def esp32_s3_validate_supports(value):
|
||||
num = value[CONF_NUMBER]
|
||||
mode = value[CONF_MODE]
|
||||
is_input = mode[CONF_INPUT]
|
||||
|
||||
if num < 0 or num > 48:
|
||||
raise cv.Invalid(f"Invalid pin number: {num} (must be 0-46)")
|
||||
if is_input:
|
||||
# All ESP32 pins support input mode
|
||||
pass
|
||||
return value
|
||||
@@ -76,6 +76,7 @@ class ESP32Preferences : public ESPPreferences {
|
||||
uint32_t current_offset = 0;
|
||||
|
||||
void open() {
|
||||
nvs_flash_init();
|
||||
esp_err_t err = nvs_open("esphome", NVS_READWRITE, &nvs_handle);
|
||||
if (err == 0)
|
||||
return;
|
||||
|
||||
@@ -18,7 +18,6 @@ from esphome.core import CORE
|
||||
from esphome.components.esp32 import add_idf_sdkconfig_option
|
||||
|
||||
DEPENDENCIES = ["esp32"]
|
||||
AUTO_LOAD = ["xiaomi_ble", "ruuvi_ble"]
|
||||
|
||||
CONF_ESP32_BLE_ID = "esp32_ble_id"
|
||||
CONF_SCAN_PARAMETERS = "scan_parameters"
|
||||
|
||||
@@ -483,6 +483,7 @@ optional<ESPBLEiBeacon> ESPBLEiBeacon::from_manufacturer_data(const ServiceData
|
||||
}
|
||||
|
||||
void ESPBTDevice::parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param ¶m) {
|
||||
this->scan_result_ = param;
|
||||
for (uint8_t i = 0; i < ESP_BD_ADDR_LEN; i++)
|
||||
this->address_[i] = param.bda[i];
|
||||
this->address_type_ = param.ble_addr_type;
|
||||
@@ -524,7 +525,7 @@ void ESPBTDevice::parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_e
|
||||
ESP_LOGVV(TAG, " Service UUID: %s", uuid.to_string().c_str());
|
||||
}
|
||||
for (auto &data : this->manufacturer_datas_) {
|
||||
ESP_LOGVV(TAG, " Manufacturer data: %s", hexencode(data.data).c_str());
|
||||
ESP_LOGVV(TAG, " Manufacturer data: %s", format_hex_pretty(data.data).c_str());
|
||||
if (this->get_ibeacon().has_value()) {
|
||||
auto ibeacon = this->get_ibeacon().value();
|
||||
ESP_LOGVV(TAG, " iBeacon data:");
|
||||
@@ -537,10 +538,10 @@ void ESPBTDevice::parse_scan_rst(const esp_ble_gap_cb_param_t::ble_scan_result_e
|
||||
for (auto &data : this->service_datas_) {
|
||||
ESP_LOGVV(TAG, " Service data:");
|
||||
ESP_LOGVV(TAG, " UUID: %s", data.uuid.to_string().c_str());
|
||||
ESP_LOGVV(TAG, " Data: %s", hexencode(data.data).c_str());
|
||||
ESP_LOGVV(TAG, " Data: %s", format_hex_pretty(data.data).c_str());
|
||||
}
|
||||
|
||||
ESP_LOGVV(TAG, "Adv data: %s", hexencode(param.ble_adv, param.adv_data_len + param.scan_rsp_len).c_str());
|
||||
ESP_LOGVV(TAG, "Adv data: %s", format_hex_pretty(param.ble_adv, param.adv_data_len + param.scan_rsp_len).c_str());
|
||||
#endif
|
||||
}
|
||||
void ESPBTDevice::parse_adv_(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param ¶m) {
|
||||
|
||||
@@ -97,6 +97,8 @@ class ESPBTDevice {
|
||||
|
||||
const std::vector<ServiceData> &get_service_datas() const { return service_datas_; }
|
||||
|
||||
const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &get_scan_result() const { return scan_result_; }
|
||||
|
||||
optional<ESPBLEiBeacon> get_ibeacon() const {
|
||||
for (auto &it : this->manufacturer_datas_) {
|
||||
auto res = ESPBLEiBeacon::from_manufacturer_data(it);
|
||||
@@ -121,6 +123,7 @@ class ESPBTDevice {
|
||||
std::vector<ESPBTUUID> service_uuids_;
|
||||
std::vector<ServiceData> manufacturer_datas_{};
|
||||
std::vector<ServiceData> service_datas_{};
|
||||
esp_ble_gap_cb_param_t::ble_scan_result_evt_param scan_result_{};
|
||||
};
|
||||
|
||||
class ESP32BLETracker;
|
||||
|
||||
@@ -17,7 +17,7 @@ from esphome.core import CORE
|
||||
from esphome.components.esp32 import add_idf_sdkconfig_option
|
||||
from esphome.cpp_helpers import setup_entity
|
||||
|
||||
DEPENDENCIES = ["esp32", "api"]
|
||||
DEPENDENCIES = ["esp32"]
|
||||
|
||||
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
|
||||
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
|
||||
@@ -57,6 +57,9 @@ CONF_IDLE_FRAMERATE = "idle_framerate"
|
||||
CONF_JPEG_QUALITY = "jpeg_quality"
|
||||
CONF_VERTICAL_FLIP = "vertical_flip"
|
||||
CONF_HORIZONTAL_MIRROR = "horizontal_mirror"
|
||||
CONF_AEC2 = "aec2"
|
||||
CONF_AE_LEVEL = "ae_level"
|
||||
CONF_AEC_VALUE = "aec_value"
|
||||
CONF_SATURATION = "saturation"
|
||||
CONF_TEST_PATTERN = "test_pattern"
|
||||
|
||||
@@ -102,6 +105,9 @@ CONFIG_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
|
||||
cv.Optional(CONF_SATURATION, default=0): camera_range_param,
|
||||
cv.Optional(CONF_VERTICAL_FLIP, default=True): cv.boolean,
|
||||
cv.Optional(CONF_HORIZONTAL_MIRROR, default=True): cv.boolean,
|
||||
cv.Optional(CONF_AEC2, default=False): cv.boolean,
|
||||
cv.Optional(CONF_AE_LEVEL, default=0): camera_range_param,
|
||||
cv.Optional(CONF_AEC_VALUE, default=300): cv.int_range(min=0, max=1200),
|
||||
cv.Optional(CONF_TEST_PATTERN, default=False): cv.boolean,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
@@ -116,6 +122,9 @@ SETTERS = {
|
||||
CONF_JPEG_QUALITY: "set_jpeg_quality",
|
||||
CONF_VERTICAL_FLIP: "set_vertical_flip",
|
||||
CONF_HORIZONTAL_MIRROR: "set_horizontal_mirror",
|
||||
CONF_AEC2: "set_aec2",
|
||||
CONF_AE_LEVEL: "set_ae_level",
|
||||
CONF_AEC_VALUE: "set_aec_value",
|
||||
CONF_CONTRAST: "set_contrast",
|
||||
CONF_BRIGHTNESS: "set_brightness",
|
||||
CONF_SATURATION: "set_saturation",
|
||||
|
||||
@@ -26,6 +26,9 @@ void ESP32Camera::setup() {
|
||||
sensor_t *s = esp_camera_sensor_get();
|
||||
s->set_vflip(s, this->vertical_flip_);
|
||||
s->set_hmirror(s, this->horizontal_mirror_);
|
||||
s->set_aec2(s, this->aec2_); // 0 = disable , 1 = enable
|
||||
s->set_ae_level(s, this->ae_level_); // -2 to 2
|
||||
s->set_aec_value(s, this->aec_value_); // 0 to 1200
|
||||
s->set_contrast(s, this->contrast_);
|
||||
s->set_brightness(s, this->brightness_);
|
||||
s->set_saturation(s, this->saturation_);
|
||||
@@ -45,6 +48,7 @@ void ESP32Camera::dump_config() {
|
||||
auto conf = this->config_;
|
||||
ESP_LOGCONFIG(TAG, "ESP32 Camera:");
|
||||
ESP_LOGCONFIG(TAG, " Name: %s", this->name_.c_str());
|
||||
ESP_LOGCONFIG(TAG, " Internal: %s", YESNO(this->internal_));
|
||||
#ifdef USE_ARDUINO
|
||||
ESP_LOGCONFIG(TAG, " Board Has PSRAM: %s", YESNO(psramFound()));
|
||||
#endif // USE_ARDUINO
|
||||
@@ -110,9 +114,9 @@ void ESP32Camera::dump_config() {
|
||||
// ESP_LOGCONFIG(TAG, " Auto White Balance: %u", st.awb);
|
||||
// ESP_LOGCONFIG(TAG, " Auto White Balance Gain: %u", st.awb_gain);
|
||||
// ESP_LOGCONFIG(TAG, " Auto Exposure Control: %u", st.aec);
|
||||
// ESP_LOGCONFIG(TAG, " Auto Exposure Control 2: %u", st.aec2);
|
||||
// ESP_LOGCONFIG(TAG, " Auto Exposure Level: %d", st.ae_level);
|
||||
// ESP_LOGCONFIG(TAG, " Auto Exposure Value: %u", st.aec_value);
|
||||
ESP_LOGCONFIG(TAG, " Auto Exposure Control 2: %u", st.aec2);
|
||||
ESP_LOGCONFIG(TAG, " Auto Exposure Level: %d", st.ae_level);
|
||||
ESP_LOGCONFIG(TAG, " Auto Exposure Value: %u", st.aec_value);
|
||||
// ESP_LOGCONFIG(TAG, " AGC: %u", st.agc);
|
||||
// ESP_LOGCONFIG(TAG, " AGC Gain: %u", st.agc_gain);
|
||||
// ESP_LOGCONFIG(TAG, " Gain Ceiling: %u", st.gainceiling);
|
||||
@@ -249,6 +253,9 @@ void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraIm
|
||||
}
|
||||
void ESP32Camera::set_vertical_flip(bool vertical_flip) { this->vertical_flip_ = vertical_flip; }
|
||||
void ESP32Camera::set_horizontal_mirror(bool horizontal_mirror) { this->horizontal_mirror_ = horizontal_mirror; }
|
||||
void ESP32Camera::set_aec2(bool aec2) { this->aec2_ = aec2; }
|
||||
void ESP32Camera::set_ae_level(int ae_level) { this->ae_level_ = ae_level; }
|
||||
void ESP32Camera::set_aec_value(uint32_t aec_value) { this->aec_value_ = aec_value; }
|
||||
void ESP32Camera::set_contrast(int contrast) { this->contrast_ = contrast; }
|
||||
void ESP32Camera::set_brightness(int brightness) { this->brightness_ = brightness; }
|
||||
void ESP32Camera::set_saturation(int saturation) { this->saturation_ = saturation; }
|
||||
|
||||
@@ -67,6 +67,9 @@ class ESP32Camera : public Component, public EntityBase {
|
||||
void set_power_down_pin(uint8_t pin);
|
||||
void set_vertical_flip(bool vertical_flip);
|
||||
void set_horizontal_mirror(bool horizontal_mirror);
|
||||
void set_aec2(bool aec2);
|
||||
void set_ae_level(int ae_level);
|
||||
void set_aec_value(uint32_t aec_value);
|
||||
void set_contrast(int contrast);
|
||||
void set_brightness(int brightness);
|
||||
void set_saturation(int saturation);
|
||||
@@ -91,6 +94,9 @@ class ESP32Camera : public Component, public EntityBase {
|
||||
camera_config_t config_{};
|
||||
bool vertical_flip_{true};
|
||||
bool horizontal_mirror_{true};
|
||||
bool aec2_{false};
|
||||
int ae_level_{0};
|
||||
uint32_t aec_value_{300};
|
||||
int contrast_{0};
|
||||
int brightness_{0};
|
||||
int saturation_{0};
|
||||
|
||||
28
esphome/components/esp32_camera_web_server/__init__.py
Normal file
28
esphome/components/esp32_camera_web_server/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.const import CONF_ID, CONF_PORT, CONF_MODE
|
||||
|
||||
CODEOWNERS = ["@ayufan"]
|
||||
DEPENDENCIES = ["esp32_camera"]
|
||||
MULTI_CONF = True
|
||||
|
||||
esp32_camera_web_server_ns = cg.esphome_ns.namespace("esp32_camera_web_server")
|
||||
CameraWebServer = esp32_camera_web_server_ns.class_("CameraWebServer", cg.Component)
|
||||
Mode = esp32_camera_web_server_ns.enum("Mode")
|
||||
|
||||
MODES = {"STREAM": Mode.STREAM, "SNAPSHOT": Mode.SNAPSHOT}
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(CameraWebServer),
|
||||
cv.Required(CONF_PORT): cv.port,
|
||||
cv.Required(CONF_MODE): cv.enum(MODES, upper=True),
|
||||
},
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
server = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(server.set_port(config[CONF_PORT]))
|
||||
cg.add(server.set_mode(config[CONF_MODE]))
|
||||
await cg.register_component(server, config)
|
||||
248
esphome/components/esp32_camera_web_server/camera_web_server.cpp
Normal file
248
esphome/components/esp32_camera_web_server/camera_web_server.cpp
Normal file
@@ -0,0 +1,248 @@
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include "camera_web_server.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/util.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <esp_http_server.h>
|
||||
#include <utility>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_camera_web_server {
|
||||
|
||||
static const int IMAGE_REQUEST_TIMEOUT = 2000;
|
||||
static const char *const TAG = "esp32_camera_web_server";
|
||||
|
||||
#define PART_BOUNDARY "123456789000000000000987654321"
|
||||
#define CONTENT_TYPE "image/jpeg"
|
||||
#define CONTENT_LENGTH "Content-Length"
|
||||
|
||||
static const char *const STREAM_HEADER = "HTTP/1.0 200 OK\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Type: multipart/x-mixed-replace;boundary=" PART_BOUNDARY "\r\n"
|
||||
"\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
static const char *const STREAM_ERROR = "Content-Type: text/plain\r\n"
|
||||
"\r\n"
|
||||
"No frames send.\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
static const char *const STREAM_PART = "Content-Type: " CONTENT_TYPE "\r\n" CONTENT_LENGTH ": %u\r\n\r\n";
|
||||
static const char *const STREAM_BOUNDARY = "\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
|
||||
CameraWebServer::CameraWebServer() {}
|
||||
|
||||
CameraWebServer::~CameraWebServer() {}
|
||||
|
||||
void CameraWebServer::setup() {
|
||||
if (!esp32_camera::global_esp32_camera || esp32_camera::global_esp32_camera->is_failed()) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
this->semaphore_ = xSemaphoreCreateBinary();
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = this->port_;
|
||||
config.ctrl_port = this->port_;
|
||||
config.max_open_sockets = 1;
|
||||
config.backlog_conn = 2;
|
||||
config.lru_purge_enable = true;
|
||||
|
||||
if (httpd_start(&this->httpd_, &config) != ESP_OK) {
|
||||
mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
httpd_uri_t uri = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = [](struct httpd_req *req) { return ((CameraWebServer *) req->user_ctx)->handler_(req); },
|
||||
.user_ctx = this};
|
||||
|
||||
httpd_register_uri_handler(this->httpd_, &uri);
|
||||
|
||||
esp32_camera::global_esp32_camera->add_image_callback([this](std::shared_ptr<esp32_camera::CameraImage> image) {
|
||||
if (this->running_) {
|
||||
this->image_ = std::move(image);
|
||||
xSemaphoreGive(this->semaphore_);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void CameraWebServer::on_shutdown() {
|
||||
this->running_ = false;
|
||||
this->image_ = nullptr;
|
||||
httpd_stop(this->httpd_);
|
||||
this->httpd_ = nullptr;
|
||||
vSemaphoreDelete(this->semaphore_);
|
||||
this->semaphore_ = nullptr;
|
||||
}
|
||||
|
||||
void CameraWebServer::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "ESP32 Camera Web Server:");
|
||||
ESP_LOGCONFIG(TAG, " Port: %d", this->port_);
|
||||
if (this->mode_ == STREAM)
|
||||
ESP_LOGCONFIG(TAG, " Mode: stream");
|
||||
else
|
||||
ESP_LOGCONFIG(TAG, " Mode: snapshot");
|
||||
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, " Setup Failed");
|
||||
}
|
||||
}
|
||||
|
||||
float CameraWebServer::get_setup_priority() const { return setup_priority::LATE; }
|
||||
|
||||
void CameraWebServer::loop() {
|
||||
if (!this->running_) {
|
||||
this->image_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> CameraWebServer::wait_for_image_() {
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> image;
|
||||
image.swap(this->image_);
|
||||
|
||||
if (!image) {
|
||||
// retry as we might still be fetching image
|
||||
xSemaphoreTake(this->semaphore_, IMAGE_REQUEST_TIMEOUT / portTICK_PERIOD_MS);
|
||||
image.swap(this->image_);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
esp_err_t CameraWebServer::handler_(struct httpd_req *req) {
|
||||
esp_err_t res = ESP_FAIL;
|
||||
|
||||
this->image_ = nullptr;
|
||||
this->running_ = true;
|
||||
|
||||
switch (this->mode_) {
|
||||
case STREAM:
|
||||
res = this->streaming_handler_(req);
|
||||
break;
|
||||
|
||||
case SNAPSHOT:
|
||||
res = this->snapshot_handler_(req);
|
||||
break;
|
||||
}
|
||||
|
||||
this->running_ = false;
|
||||
this->image_ = nullptr;
|
||||
return res;
|
||||
}
|
||||
|
||||
static esp_err_t httpd_send_all(httpd_req_t *r, const char *buf, size_t buf_len) {
|
||||
int ret;
|
||||
|
||||
while (buf_len > 0) {
|
||||
ret = httpd_send(r, buf, buf_len);
|
||||
if (ret < 0) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf += ret;
|
||||
buf_len -= ret;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
|
||||
esp_err_t res = ESP_OK;
|
||||
char part_buf[64];
|
||||
|
||||
// This manually constructs HTTP response to avoid chunked encoding
|
||||
// which is not supported by some clients
|
||||
|
||||
res = httpd_send_all(req, STREAM_HEADER, strlen(STREAM_HEADER));
|
||||
if (res != ESP_OK) {
|
||||
ESP_LOGW(TAG, "STREAM: failed to set HTTP header");
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t last_frame = millis();
|
||||
uint32_t frames = 0;
|
||||
|
||||
while (res == ESP_OK && this->running_) {
|
||||
if (esp32_camera::global_esp32_camera != nullptr) {
|
||||
esp32_camera::global_esp32_camera->request_stream();
|
||||
}
|
||||
|
||||
auto image = this->wait_for_image_();
|
||||
|
||||
if (!image) {
|
||||
ESP_LOGW(TAG, "STREAM: failed to acquire frame");
|
||||
res = ESP_FAIL;
|
||||
}
|
||||
if (res == ESP_OK) {
|
||||
size_t hlen = snprintf(part_buf, 64, STREAM_PART, image->get_data_length());
|
||||
res = httpd_send_all(req, part_buf, hlen);
|
||||
}
|
||||
if (res == ESP_OK) {
|
||||
res = httpd_send_all(req, (const char *) image->get_data_buffer(), image->get_data_length());
|
||||
}
|
||||
if (res == ESP_OK) {
|
||||
res = httpd_send_all(req, STREAM_BOUNDARY, strlen(STREAM_BOUNDARY));
|
||||
}
|
||||
if (res == ESP_OK) {
|
||||
frames++;
|
||||
int64_t frame_time = millis() - last_frame;
|
||||
last_frame = millis();
|
||||
|
||||
ESP_LOGD(TAG, "MJPG: %uB %ums (%.1ffps)", (uint32_t) image->get_data_length(), (uint32_t) frame_time,
|
||||
1000.0 / (uint32_t) frame_time);
|
||||
}
|
||||
}
|
||||
|
||||
if (!frames) {
|
||||
res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "STREAM: closed. Frames: %u", frames);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
|
||||
esp_err_t res = ESP_OK;
|
||||
|
||||
if (esp32_camera::global_esp32_camera != nullptr) {
|
||||
esp32_camera::global_esp32_camera->request_image();
|
||||
}
|
||||
|
||||
auto image = this->wait_for_image_();
|
||||
|
||||
if (!image) {
|
||||
ESP_LOGW(TAG, "SNAPSHOT: failed to acquire frame");
|
||||
httpd_resp_send_500(req);
|
||||
res = ESP_FAIL;
|
||||
return res;
|
||||
}
|
||||
|
||||
res = httpd_resp_set_type(req, CONTENT_TYPE);
|
||||
if (res != ESP_OK) {
|
||||
ESP_LOGW(TAG, "SNAPSHOT: failed to set HTTP response type");
|
||||
return res;
|
||||
}
|
||||
|
||||
httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
|
||||
|
||||
if (res == ESP_OK) {
|
||||
res = httpd_resp_set_hdr(req, CONTENT_LENGTH, esphome::to_string(image->get_data_length()).c_str());
|
||||
}
|
||||
if (res == ESP_OK) {
|
||||
res = httpd_resp_send(req, (const char *) image->get_data_buffer(), image->get_data_length());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace esp32_camera_web_server
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP32
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
#include "esphome/components/esp32_camera/esp32_camera.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
|
||||
struct httpd_req;
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_camera_web_server {
|
||||
|
||||
enum Mode { STREAM, SNAPSHOT };
|
||||
|
||||
class CameraWebServer : public Component {
|
||||
public:
|
||||
CameraWebServer();
|
||||
~CameraWebServer();
|
||||
|
||||
void setup() override;
|
||||
void on_shutdown() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void set_port(uint16_t port) { this->port_ = port; }
|
||||
void set_mode(Mode mode) { this->mode_ = mode; }
|
||||
void loop() override;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> wait_for_image_();
|
||||
esp_err_t handler_(struct httpd_req *req);
|
||||
esp_err_t streaming_handler_(struct httpd_req *req);
|
||||
esp_err_t snapshot_handler_(struct httpd_req *req);
|
||||
|
||||
protected:
|
||||
uint16_t port_{0};
|
||||
void *httpd_{nullptr};
|
||||
SemaphoreHandle_t semaphore_;
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> image_;
|
||||
bool running_{false};
|
||||
Mode mode_{STREAM};
|
||||
};
|
||||
|
||||
} // namespace esp32_camera_web_server
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP32
|
||||
@@ -4,7 +4,7 @@ from esphome.components import binary_sensor, output, esp32_ble_server
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
|
||||
AUTO_LOAD = ["binary_sensor", "output", "improv", "esp32_ble_server"]
|
||||
AUTO_LOAD = ["binary_sensor", "output", "esp32_ble_server"]
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
CONFLICTS_WITH = ["esp32_ble_tracker", "esp32_ble_beacon"]
|
||||
DEPENDENCIES = ["wifi", "esp32"]
|
||||
@@ -56,6 +56,7 @@ async def to_code(config):
|
||||
cg.add(ble_server.register_service_component(var))
|
||||
|
||||
cg.add_define("USE_IMPROV")
|
||||
cg.add_library("esphome/Improv", "1.0.0")
|
||||
|
||||
cg.add(var.set_identify_duration(config[CONF_IDENTIFY_DURATION]))
|
||||
cg.add(var.set_authorized_duration(config[CONF_AUTHORIZED_DURATION]))
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace esphome {
|
||||
namespace esp32_improv {
|
||||
|
||||
static const char *const TAG = "esp32_improv.component";
|
||||
static const char *const ESPHOME_MY_LINK = "https://my.home-assistant.io/redirect/config_flow_start?domain=esphome";
|
||||
|
||||
ESP32ImprovComponent::ESP32ImprovComponent() { global_improv_component = this; }
|
||||
|
||||
@@ -124,8 +125,13 @@ void ESP32ImprovComponent::loop() {
|
||||
this->cancel_timeout("wifi-connect-timeout");
|
||||
this->set_state_(improv::STATE_PROVISIONED);
|
||||
|
||||
std::string url = "https://my.home-assistant.io/redirect/config_flow_start?domain=esphome";
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, {url});
|
||||
std::vector<std::string> urls = {ESPHOME_MY_LINK};
|
||||
#ifdef USE_WEBSERVER
|
||||
auto ip = wifi::global_wifi_component->wifi_sta_ip();
|
||||
std::string webserver_url = "http://" + ip.str() + ":" + to_string(WEBSERVER_PORT);
|
||||
urls.push_back(webserver_url);
|
||||
#endif
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls);
|
||||
this->send_response_(data);
|
||||
this->set_timeout("end-service", 1000, [this] {
|
||||
this->service_->stop();
|
||||
@@ -213,7 +219,7 @@ void ESP32ImprovComponent::dump_config() {
|
||||
void ESP32ImprovComponent::process_incoming_data_() {
|
||||
uint8_t length = this->incoming_data_[1];
|
||||
|
||||
ESP_LOGD(TAG, "Processing bytes - %s", hexencode(this->incoming_data_).c_str());
|
||||
ESP_LOGD(TAG, "Processing bytes - %s", format_hex_pretty(this->incoming_data_).c_str());
|
||||
if (this->incoming_data_.size() - 3 == length) {
|
||||
this->set_error_(improv::ERROR_NONE);
|
||||
improv::ImprovCommand command = improv::parse_improv_data(this->incoming_data_);
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_server.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_characteristic.h"
|
||||
#include "esphome/components/improv/improv.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_server.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#include "esphome/core/component.h"
|
||||
@@ -12,6 +11,8 @@
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <improv.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_improv {
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include "esp32_touch.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
@@ -93,7 +94,6 @@ void ESP32TouchComponent::dump_config() {
|
||||
|
||||
if (this->iir_filter_enabled_()) {
|
||||
ESP_LOGCONFIG(TAG, " IIR Filter: %ums", this->iir_filter_);
|
||||
touch_pad_filter_start(this->iir_filter_);
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " IIR Filter DISABLED");
|
||||
}
|
||||
@@ -125,6 +125,8 @@ void ESP32TouchComponent::loop() {
|
||||
if (should_print) {
|
||||
ESP_LOGD(TAG, "Touch Pad '%s' (T%u): %u", child->get_name().c_str(), child->get_touch_pad(), value);
|
||||
}
|
||||
|
||||
App.feed_wdt();
|
||||
}
|
||||
|
||||
if (should_print) {
|
||||
|
||||
@@ -63,7 +63,7 @@ RECOMMENDED_ARDUINO_FRAMEWORK_VERSION = cv.Version(2, 7, 4)
|
||||
# The platformio/espressif8266 version to use for arduino 2 framework versions
|
||||
# - https://github.com/platformio/platform-espressif8266/releases
|
||||
# - https://api.registry.platformio.org/v3/packages/platformio/platform/espressif8266
|
||||
ARDUINO_2_PLATFORM_VERSION = cv.Version(2, 6, 2)
|
||||
ARDUINO_2_PLATFORM_VERSION = cv.Version(2, 6, 3)
|
||||
# for arduino 3 framework versions
|
||||
ARDUINO_3_PLATFORM_VERSION = cv.Version(3, 2, 0)
|
||||
|
||||
@@ -93,12 +93,12 @@ def _arduino_check_versions(value):
|
||||
platform_version = value.get(CONF_PLATFORM_VERSION)
|
||||
if platform_version is None:
|
||||
if version >= cv.Version(3, 0, 0):
|
||||
platform_version = ARDUINO_3_PLATFORM_VERSION
|
||||
platform_version = _parse_platform_version(str(ARDUINO_3_PLATFORM_VERSION))
|
||||
elif version >= cv.Version(2, 5, 0):
|
||||
platform_version = ARDUINO_2_PLATFORM_VERSION
|
||||
platform_version = _parse_platform_version(str(ARDUINO_2_PLATFORM_VERSION))
|
||||
else:
|
||||
platform_version = cv.Version(1, 8, 0)
|
||||
value[CONF_PLATFORM_VERSION] = str(platform_version)
|
||||
platform_version = _parse_platform_version(str(cv.Version(1, 8, 0)))
|
||||
value[CONF_PLATFORM_VERSION] = platform_version
|
||||
|
||||
if version != RECOMMENDED_ARDUINO_FRAMEWORK_VERSION:
|
||||
_LOGGER.warning(
|
||||
@@ -109,13 +109,22 @@ def _arduino_check_versions(value):
|
||||
return value
|
||||
|
||||
|
||||
def _parse_platform_version(value):
|
||||
try:
|
||||
# if platform version is a valid version constraint, prefix the default package
|
||||
cv.platformio_version_constraint(value)
|
||||
return f"platformio/espressif8266 @ {value}"
|
||||
except cv.Invalid:
|
||||
return value
|
||||
|
||||
|
||||
CONF_PLATFORM_VERSION = "platform_version"
|
||||
ARDUINO_FRAMEWORK_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_VERSION, default="recommended"): cv.string_strict,
|
||||
cv.Optional(CONF_SOURCE): cv.string_strict,
|
||||
cv.Optional(CONF_PLATFORM_VERSION): cv.string_strict,
|
||||
cv.Optional(CONF_PLATFORM_VERSION): _parse_platform_version,
|
||||
}
|
||||
),
|
||||
_arduino_check_versions,
|
||||
@@ -142,21 +151,22 @@ CONFIG_SCHEMA = cv.All(
|
||||
async def to_code(config):
|
||||
cg.add(esp8266_ns.setup_preferences())
|
||||
|
||||
cg.add_platformio_option("lib_ldf_mode", "off")
|
||||
|
||||
cg.add_platformio_option("board", config[CONF_BOARD])
|
||||
cg.add_build_flag("-DUSE_ESP8266")
|
||||
cg.add_define("ESPHOME_BOARD", config[CONF_BOARD])
|
||||
cg.add_define("ESPHOME_VARIANT", "ESP8266")
|
||||
|
||||
conf = config[CONF_FRAMEWORK]
|
||||
cg.add_platformio_option("framework", "arduino")
|
||||
cg.add_build_flag("-DUSE_ARDUINO")
|
||||
cg.add_build_flag("-DUSE_ESP8266_FRAMEWORK_ARDUINO")
|
||||
cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
|
||||
cg.add_platformio_option(
|
||||
"platform_packages",
|
||||
[f"platformio/framework-arduinoespressif8266 @ {conf[CONF_SOURCE]}"],
|
||||
)
|
||||
cg.add_platformio_option(
|
||||
"platform", f"platformio/espressif8266 @ {conf[CONF_PLATFORM_VERSION]}"
|
||||
)
|
||||
|
||||
# Default for platformio is LWIP2_LOW_MEMORY with:
|
||||
# - MSS=536
|
||||
|
||||
@@ -206,61 +206,3 @@ ESP8266_BOARD_PINS = {
|
||||
"wio_node": {"LED": 2, "GROVE": 15, "D0": 3, "D1": 5, "BUTTON": 0},
|
||||
"xinabox_cw01": {"SDA": 2, "SCL": 14, "LED": 5, "LED_RED": 12, "LED_GREEN": 13},
|
||||
}
|
||||
|
||||
FLASH_SIZE_1_MB = 2 ** 20
|
||||
FLASH_SIZE_512_KB = FLASH_SIZE_1_MB // 2
|
||||
FLASH_SIZE_2_MB = 2 * FLASH_SIZE_1_MB
|
||||
FLASH_SIZE_4_MB = 4 * FLASH_SIZE_1_MB
|
||||
FLASH_SIZE_16_MB = 16 * FLASH_SIZE_1_MB
|
||||
|
||||
ESP8266_FLASH_SIZES = {
|
||||
"d1": FLASH_SIZE_4_MB,
|
||||
"d1_mini": FLASH_SIZE_4_MB,
|
||||
"d1_mini_lite": FLASH_SIZE_1_MB,
|
||||
"d1_mini_pro": FLASH_SIZE_16_MB,
|
||||
"esp01": FLASH_SIZE_512_KB,
|
||||
"esp01_1m": FLASH_SIZE_1_MB,
|
||||
"esp07": FLASH_SIZE_4_MB,
|
||||
"esp12e": FLASH_SIZE_4_MB,
|
||||
"esp210": FLASH_SIZE_4_MB,
|
||||
"esp8285": FLASH_SIZE_1_MB,
|
||||
"esp_wroom_02": FLASH_SIZE_2_MB,
|
||||
"espduino": FLASH_SIZE_4_MB,
|
||||
"espectro": FLASH_SIZE_4_MB,
|
||||
"espino": FLASH_SIZE_4_MB,
|
||||
"espinotee": FLASH_SIZE_4_MB,
|
||||
"espmxdevkit": FLASH_SIZE_1_MB,
|
||||
"espresso_lite_v1": FLASH_SIZE_4_MB,
|
||||
"espresso_lite_v2": FLASH_SIZE_4_MB,
|
||||
"gen4iod": FLASH_SIZE_512_KB,
|
||||
"heltec_wifi_kit_8": FLASH_SIZE_4_MB,
|
||||
"huzzah": FLASH_SIZE_4_MB,
|
||||
"inventone": FLASH_SIZE_4_MB,
|
||||
"modwifi": FLASH_SIZE_2_MB,
|
||||
"nodemcu": FLASH_SIZE_4_MB,
|
||||
"nodemcuv2": FLASH_SIZE_4_MB,
|
||||
"oak": FLASH_SIZE_4_MB,
|
||||
"phoenix_v1": FLASH_SIZE_4_MB,
|
||||
"phoenix_v2": FLASH_SIZE_4_MB,
|
||||
"sonoff_basic": FLASH_SIZE_1_MB,
|
||||
"sonoff_s20": FLASH_SIZE_1_MB,
|
||||
"sonoff_sv": FLASH_SIZE_1_MB,
|
||||
"sonoff_th": FLASH_SIZE_1_MB,
|
||||
"sparkfunBlynk": FLASH_SIZE_4_MB,
|
||||
"thing": FLASH_SIZE_512_KB,
|
||||
"thingdev": FLASH_SIZE_512_KB,
|
||||
"wifi_slot": FLASH_SIZE_1_MB,
|
||||
"wifiduino": FLASH_SIZE_4_MB,
|
||||
"wifinfo": FLASH_SIZE_1_MB,
|
||||
"wio_link": FLASH_SIZE_4_MB,
|
||||
"wio_node": FLASH_SIZE_4_MB,
|
||||
"xinabox_cw01": FLASH_SIZE_4_MB,
|
||||
}
|
||||
|
||||
ESP8266_LD_SCRIPTS = {
|
||||
FLASH_SIZE_512_KB: ("eagle.flash.512k0.ld", "eagle.flash.512k.ld"),
|
||||
FLASH_SIZE_1_MB: ("eagle.flash.1m0.ld", "eagle.flash.1m.ld"),
|
||||
FLASH_SIZE_2_MB: ("eagle.flash.2m.ld", "eagle.flash.2m.ld"),
|
||||
FLASH_SIZE_4_MB: ("eagle.flash.4m.ld", "eagle.flash.4m.ld"),
|
||||
FLASH_SIZE_16_MB: ("eagle.flash.16m.ld", "eagle.flash.16m14m.ld"),
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ void IRAM_ATTR HOT yield() { ::yield(); }
|
||||
uint32_t IRAM_ATTR HOT millis() { return ::millis(); }
|
||||
void IRAM_ATTR HOT delay(uint32_t ms) { ::delay(ms); }
|
||||
uint32_t IRAM_ATTR HOT micros() { return ::micros(); }
|
||||
void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { ::delayMicroseconds(us); }
|
||||
void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
|
||||
void arch_restart() {
|
||||
ESP.restart(); // NOLINT(readability-static-accessed-through-instance)
|
||||
// restart() doesn't always end execution
|
||||
@@ -20,6 +20,7 @@ void arch_restart() {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
void arch_init() {}
|
||||
void IRAM_ATTR HOT arch_feed_wdt() {
|
||||
ESP.wdtFeed(); // NOLINT(readability-static-accessed-through-instance)
|
||||
}
|
||||
@@ -27,7 +28,7 @@ void IRAM_ATTR HOT arch_feed_wdt() {
|
||||
uint8_t progmem_read_byte(const uint8_t *addr) {
|
||||
return pgm_read_byte(addr); // NOLINT
|
||||
}
|
||||
uint32_t arch_get_cpu_cycle_count() {
|
||||
uint32_t IRAM_ATTR HOT arch_get_cpu_cycle_count() {
|
||||
return ESP.getCycleCount(); // NOLINT(readability-static-accessed-through-instance)
|
||||
}
|
||||
uint32_t arch_get_cpu_freq_hz() { return F_CPU; }
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace esp8266 {
|
||||
static const char *const TAG = "esp8266";
|
||||
|
||||
static int IRAM_ATTR flags_to_mode(gpio::Flags flags, uint8_t pin) {
|
||||
if (flags == gpio::FLAG_INPUT) {
|
||||
if (flags == gpio::FLAG_INPUT) { // NOLINT(bugprone-branch-clone)
|
||||
return INPUT;
|
||||
} else if (flags == gpio::FLAG_OUTPUT) {
|
||||
return OUTPUT;
|
||||
|
||||
@@ -55,7 +55,7 @@ static inline bool esp_rtc_user_mem_write(uint32_t index, uint32_t value) {
|
||||
|
||||
extern "C" uint32_t _SPIFFS_end; // NOLINT
|
||||
|
||||
static const uint32_t get_esp8266_flash_sector() {
|
||||
static uint32_t get_esp8266_flash_sector() {
|
||||
union {
|
||||
uint32_t *ptr;
|
||||
uint32_t uint;
|
||||
@@ -63,7 +63,7 @@ static const uint32_t get_esp8266_flash_sector() {
|
||||
data.ptr = &_SPIFFS_end;
|
||||
return (data.uint - 0x40200000) / SPI_FLASH_SEC_SIZE;
|
||||
}
|
||||
static const uint32_t get_esp8266_flash_address() { return get_esp8266_flash_sector() * SPI_FLASH_SEC_SIZE; }
|
||||
static uint32_t get_esp8266_flash_address() { return get_esp8266_flash_sector() * SPI_FLASH_SEC_SIZE; }
|
||||
|
||||
template<class It> uint32_t calculate_crc(It first, It last, uint32_t type) {
|
||||
uint32_t crc = type;
|
||||
|
||||
@@ -184,7 +184,9 @@ void EthernetComponent::start_connect_() {
|
||||
}
|
||||
|
||||
err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
|
||||
ESPHL_ERROR_CHECK(err, "DHCPC stop error");
|
||||
if (err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) {
|
||||
ESPHL_ERROR_CHECK(err, "DHCPC stop error");
|
||||
}
|
||||
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
|
||||
ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ from esphome.const import (
|
||||
CONF_TYPE,
|
||||
CONF_EXTERNAL_COMPONENTS,
|
||||
CONF_PATH,
|
||||
CONF_USERNAME,
|
||||
CONF_PASSWORD,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
from esphome import git, loader
|
||||
@@ -27,6 +29,8 @@ TYPE_LOCAL = "local"
|
||||
GIT_SCHEMA = {
|
||||
cv.Required(CONF_URL): cv.url,
|
||||
cv.Optional(CONF_REF): cv.git_ref,
|
||||
cv.Optional(CONF_USERNAME): cv.string,
|
||||
cv.Optional(CONF_PASSWORD): cv.string,
|
||||
}
|
||||
LOCAL_SCHEMA = {
|
||||
cv.Required(CONF_PATH): cv.directory,
|
||||
@@ -43,19 +47,27 @@ def validate_source_shorthand(value):
|
||||
# Regex for GitHub repo name with optional branch/tag
|
||||
# Note: git allows other branch/tag names as well, but never seen them used before
|
||||
m = re.match(
|
||||
r"github://([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-\._]+)(?:@([a-zA-Z0-9\-_.\./]+))?",
|
||||
r"github://(?:([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-\._]+)(?:@([a-zA-Z0-9\-_.\./]+))?|pr#([0-9]+))",
|
||||
value,
|
||||
)
|
||||
if m is None:
|
||||
raise cv.Invalid(
|
||||
"Source is not a file system path or in expected github://username/name[@branch-or-tag] format!"
|
||||
"Source is not a file system path, in expected github://username/name[@branch-or-tag] or github://pr#1234 format!"
|
||||
)
|
||||
conf = {
|
||||
CONF_TYPE: TYPE_GIT,
|
||||
CONF_URL: f"https://github.com/{m.group(1)}/{m.group(2)}.git",
|
||||
}
|
||||
if m.group(3):
|
||||
conf[CONF_REF] = m.group(3)
|
||||
if m.group(4):
|
||||
conf = {
|
||||
CONF_TYPE: TYPE_GIT,
|
||||
CONF_URL: "https://github.com/esphome/esphome.git",
|
||||
CONF_REF: f"pull/{m.group(4)}/head",
|
||||
}
|
||||
else:
|
||||
conf = {
|
||||
CONF_TYPE: TYPE_GIT,
|
||||
CONF_URL: f"https://github.com/{m.group(1)}/{m.group(2)}.git",
|
||||
}
|
||||
if m.group(3):
|
||||
conf[CONF_REF] = m.group(3)
|
||||
|
||||
return SOURCE_SCHEMA(conf)
|
||||
|
||||
|
||||
@@ -91,6 +103,8 @@ def _process_git_config(config: dict, refresh) -> str:
|
||||
ref=config.get(CONF_REF),
|
||||
refresh=refresh,
|
||||
domain=DOMAIN,
|
||||
username=config.get(CONF_USERNAME),
|
||||
password=config.get(CONF_PASSWORD),
|
||||
)
|
||||
|
||||
if (repo_dir / "esphome" / "components").is_dir():
|
||||
|
||||
@@ -32,7 +32,7 @@ void EZOSensor::update() {
|
||||
}
|
||||
|
||||
void EZOSensor::loop() {
|
||||
uint8_t buf[20];
|
||||
uint8_t buf[21];
|
||||
if (!(this->state_ & EZO_STATE_WAIT)) {
|
||||
if (this->state_ & EZO_STATE_SEND_TEMP) {
|
||||
int len = sprintf((char *) buf, "T,%0.3f", this->tempcomp_);
|
||||
@@ -74,7 +74,12 @@ void EZOSensor::loop() {
|
||||
if (buf[0] != 1)
|
||||
return;
|
||||
|
||||
float val = strtof((char *) &buf[1], nullptr);
|
||||
// some sensors return multiple comma-separated values, terminate string after first one
|
||||
for (size_t i = 1; i < sizeof(buf) - 1; i++)
|
||||
if (buf[i] == ',')
|
||||
buf[i] = '\0';
|
||||
|
||||
float val = parse_number<float>((char *) &buf[1]).value_or(0);
|
||||
this->publish_state(val);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ from esphome.const import (
|
||||
CONF_LAST_FINGER_ID,
|
||||
CONF_SECURITY_LEVEL,
|
||||
CONF_STATUS,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
ICON_ACCOUNT,
|
||||
ICON_ACCOUNT_CHECK,
|
||||
ICON_DATABASE,
|
||||
@@ -26,30 +27,36 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
icon=ICON_FINGERPRINT,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_NONE,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_STATUS): sensor.sensor_schema(
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_NONE,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_CAPACITY): sensor.sensor_schema(
|
||||
icon=ICON_DATABASE,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_NONE,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_SECURITY_LEVEL): sensor.sensor_schema(
|
||||
icon=ICON_SECURITY,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_NONE,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_LAST_FINGER_ID): sensor.sensor_schema(
|
||||
icon=ICON_ACCOUNT,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_NONE,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_LAST_CONFIDENCE): sensor.sensor_schema(
|
||||
icon=ICON_ACCOUNT_CHECK,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_NONE,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -86,7 +86,7 @@ void Graph::draw(DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Colo
|
||||
// Look back in trace data to best-fit into local range
|
||||
float mx = NAN;
|
||||
float mn = NAN;
|
||||
for (int16_t i = 0; i < this->width_; i++) {
|
||||
for (uint32_t i = 0; i < this->width_; i++) {
|
||||
for (auto *trace : traces_) {
|
||||
float v = trace->get_tracedata()->get_value(i);
|
||||
if (!std::isnan(v)) {
|
||||
@@ -132,7 +132,7 @@ void Graph::draw(DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Colo
|
||||
if (!std::isnan(this->gridspacing_y_)) {
|
||||
for (int y = yn; y <= ym; y++) {
|
||||
int16_t py = (int16_t) roundf((this->height_ - 1) * (1.0 - (float) (y - yn) / (ym - yn)));
|
||||
for (int x = 0; x < this->width_; x += 2) {
|
||||
for (uint32_t x = 0; x < this->width_; x += 2) {
|
||||
buff->draw_pixel_at(x_offset + x, y_offset + py, color);
|
||||
}
|
||||
}
|
||||
@@ -147,7 +147,7 @@ void Graph::draw(DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Colo
|
||||
ESP_LOGW(TAG, "Graphing reducing x-scale to prevent too many gridlines");
|
||||
}
|
||||
for (int i = 0; i <= n; i++) {
|
||||
for (int y = 0; y < this->height_; y += 2) {
|
||||
for (uint32_t y = 0; y < this->height_; y += 2) {
|
||||
buff->draw_pixel_at(x_offset + i * (this->width_ - 1) / n, y_offset + y, color);
|
||||
}
|
||||
}
|
||||
@@ -158,14 +158,14 @@ void Graph::draw(DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Colo
|
||||
for (auto *trace : traces_) {
|
||||
Color c = trace->get_line_color();
|
||||
uint16_t thick = trace->get_line_thickness();
|
||||
for (int16_t i = 0; i < this->width_; i++) {
|
||||
for (uint32_t i = 0; i < this->width_; i++) {
|
||||
float v = (trace->get_tracedata()->get_value(i) - ymin) / yrange;
|
||||
if (!std::isnan(v) && (thick > 0)) {
|
||||
int16_t x = this->width_ - 1 - i;
|
||||
uint8_t b = (i % (thick * LineType::PATTERN_LENGTH)) / thick;
|
||||
if (((uint8_t) trace->get_line_type() & (1 << b)) == (1 << b)) {
|
||||
int16_t y = (int16_t) roundf((this->height_ - 1) * (1.0 - v)) - thick / 2;
|
||||
for (int16_t t = 0; t < thick; t++) {
|
||||
for (uint16_t t = 0; t < thick; t++) {
|
||||
buff->draw_pixel_at(x_offset + x, y_offset + y + t, c);
|
||||
}
|
||||
}
|
||||
@@ -179,8 +179,8 @@ void GraphLegend::init(Graph *g) {
|
||||
parent_ = g;
|
||||
|
||||
// Determine maximum expected text and value width / height
|
||||
int txtw = 0, txtos = 0, txtbl = 0, txth = 0;
|
||||
int valw = 0, valos = 0, valbl = 0, valh = 0;
|
||||
int txtw = 0, txth = 0;
|
||||
int valw = 0, valh = 0;
|
||||
int lt = 0;
|
||||
for (auto *trace : g->traces_) {
|
||||
std::string txtstr = trace->get_name();
|
||||
@@ -320,7 +320,7 @@ void Graph::draw_legend(display::DisplayBuffer *buff, uint16_t x_offset, uint16_
|
||||
|
||||
if (legend_->lines_) {
|
||||
uint16_t thick = trace->get_line_thickness();
|
||||
for (int16_t i = 0; i < legend_->x0_ * 4 / 3; i++) {
|
||||
for (int i = 0; i < legend_->x0_ * 4 / 3; i++) {
|
||||
uint8_t b = (i % (thick * LineType::PATTERN_LENGTH)) / thick;
|
||||
if (((uint8_t) trace->get_line_type() & (1 << b)) == (1 << b)) {
|
||||
buff->vertical_line(x - legend_->x0_ * 2 / 3 + i, y + legend_->yl_ - thick / 2, thick,
|
||||
|
||||
@@ -93,13 +93,12 @@ PV_SENSORS = {
|
||||
CONF_VOLTAGE_SAMPLED_BY_SECONDARY_CPU: sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_VOLT,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
CONF_INSULATION_OF_P_TO_GROUND: sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_KOHM,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
@@ -135,7 +134,6 @@ CONFIG_SCHEMA = (
|
||||
cv.Optional(CONF_REACTIVE_POWER): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_ENERGY_PRODUCTION_DAY): sensor.sensor_schema(
|
||||
|
||||
@@ -30,6 +30,7 @@ PROTOCOLS = {
|
||||
"gree": Protocol.PROTOCOL_GREE,
|
||||
"greeya": Protocol.PROTOCOL_GREEYAA,
|
||||
"greeyan": Protocol.PROTOCOL_GREEYAN,
|
||||
"greeyac": Protocol.PROTOCOL_GREEYAC,
|
||||
"hisense_aud": Protocol.PROTOCOL_HISENSE_AUD,
|
||||
"hitachi": Protocol.PROTOCOL_HITACHI,
|
||||
"hyundai": Protocol.PROTOCOL_HYUNDAI,
|
||||
@@ -111,4 +112,6 @@ def to_code(config):
|
||||
cg.add(var.set_max_temperature(config[CONF_MIN_TEMPERATURE]))
|
||||
cg.add(var.set_min_temperature(config[CONF_MAX_TEMPERATURE]))
|
||||
|
||||
cg.add_library("tonia/HeatpumpIR", "1.0.15")
|
||||
# PIO isn't updating releases, so referencing the release tag directly. See:
|
||||
# https://github.com/ToniA/arduino-heatpumpir/commit/0948c619d86407a4e50e8db2f3c193e0576c86fd
|
||||
cg.add_library("", "", "https://github.com/ToniA/arduino-heatpumpir.git#1.0.18")
|
||||
|
||||
@@ -25,6 +25,7 @@ const std::map<Protocol, std::function<HeatpumpIR *()>> PROTOCOL_CONSTRUCTOR_MAP
|
||||
{PROTOCOL_GREE, []() { return new GreeGenericHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_GREEYAA, []() { return new GreeYAAHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_GREEYAN, []() { return new GreeYANHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_GREEYAC, []() { return new GreeYACHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_HISENSE_AUD, []() { return new HisenseHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_HITACHI, []() { return new HitachiHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_HYUNDAI, []() { return new HyundaiHeatpumpIR(); }}, // NOLINT
|
||||
@@ -61,6 +62,19 @@ void HeatpumpIRClimate::setup() {
|
||||
}
|
||||
this->heatpump_ir_ = protocol_constructor->second();
|
||||
climate_ir::ClimateIR::setup();
|
||||
if (this->sensor_) {
|
||||
this->sensor_->add_on_state_callback([this](float state) {
|
||||
this->current_temperature = state;
|
||||
|
||||
IRSenderESPHome esp_sender(this->transmitter_);
|
||||
this->heatpump_ir_->send(esp_sender, uint8_t(lround(this->current_temperature + 0.5)));
|
||||
|
||||
// current temperature changed, publish state
|
||||
this->publish_state();
|
||||
});
|
||||
this->current_temperature = this->sensor_->state;
|
||||
} else
|
||||
this->current_temperature = NAN;
|
||||
}
|
||||
|
||||
void HeatpumpIRClimate::transmit_state() {
|
||||
@@ -171,8 +185,7 @@ void HeatpumpIRClimate::transmit_state() {
|
||||
|
||||
temperature_cmd = (uint8_t) clamp(this->target_temperature, this->min_temperature_, this->max_temperature_);
|
||||
|
||||
IRSenderESPHome esp_sender(0, this->transmitter_);
|
||||
|
||||
IRSenderESPHome esp_sender(this->transmitter_);
|
||||
heatpump_ir_->send(esp_sender, power_mode_cmd, operating_mode_cmd, fan_speed_cmd, temperature_cmd, swing_v_cmd,
|
||||
swing_h_cmd);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ enum Protocol {
|
||||
PROTOCOL_GREE,
|
||||
PROTOCOL_GREEYAA,
|
||||
PROTOCOL_GREEYAN,
|
||||
PROTOCOL_GREEYAC,
|
||||
PROTOCOL_HISENSE_AUD,
|
||||
PROTOCOL_HITACHI,
|
||||
PROTOCOL_HYUNDAI,
|
||||
|
||||
@@ -11,8 +11,8 @@ namespace heatpumpir {
|
||||
|
||||
class IRSenderESPHome : public IRSender {
|
||||
public:
|
||||
IRSenderESPHome(uint8_t pin, remote_transmitter::RemoteTransmitterComponent *transmitter)
|
||||
: IRSender(pin), transmit_(transmitter->transmit()){};
|
||||
IRSenderESPHome(remote_transmitter::RemoteTransmitterComponent *transmitter)
|
||||
: IRSender(0), transmit_(transmitter->transmit()){};
|
||||
void setFrequency(int frequency) override; // NOLINT(readability-identifier-naming)
|
||||
void space(int space_length) override;
|
||||
void mark(int mark_length) override;
|
||||
|
||||
@@ -299,9 +299,7 @@ bool HitachiClimate::parse_swing_(const uint8_t remote_state[]) {
|
||||
GETBITS8(remote_state[HITACHI_AC344_SWINGH_BYTE], HITACHI_AC344_SWINGH_OFFSET, HITACHI_AC344_SWINGH_SIZE);
|
||||
ESP_LOGV(TAG, "SwingH: %02X %02X", remote_state[HITACHI_AC344_SWINGH_BYTE], swing_modeh);
|
||||
|
||||
if ((swing_modeh & 0x7) == 0x0) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
|
||||
} else if ((swing_modeh & 0x3) == 0x3) {
|
||||
if ((swing_modeh & 0x3) == 0x3) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_OFF;
|
||||
} else {
|
||||
this->swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
|
||||
|
||||
@@ -300,9 +300,7 @@ bool HitachiClimate::parse_swing_(const uint8_t remote_state[]) {
|
||||
HITACHI_AC424_SWINGH_SIZE);
|
||||
ESP_LOGV(TAG, "SwingH: %02X %02X", remote_state[HITACHI_AC424_SWINGH_BYTE], swing_modeh);
|
||||
|
||||
if ((swing_modeh & 0x7) == 0x0) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
|
||||
} else if ((swing_modeh & 0x3) == 0x3) {
|
||||
if ((swing_modeh & 0x3) == 0x3) {
|
||||
this->swing_mode = climate::CLIMATE_SWING_OFF;
|
||||
} else {
|
||||
this->swing_mode = climate::CLIMATE_SWING_HORIZONTAL;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include <cstdint>
|
||||
|
||||
namespace esphome {
|
||||
@@ -13,5 +12,3 @@ class AbstractAQICalculator {
|
||||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
#include "abstract_aqi_calculator.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -33,7 +31,7 @@ class AQICalculator : public AbstractAQICalculator {
|
||||
int conc_lo = array[grid_index][0];
|
||||
int conc_hi = array[grid_index][1];
|
||||
|
||||
return ((aqi_hi - aqi_lo) / (conc_hi - conc_lo)) * (value - conc_lo) + aqi_lo;
|
||||
return (value - conc_lo) * (aqi_hi - aqi_lo) / (conc_hi - conc_lo) + aqi_lo;
|
||||
}
|
||||
|
||||
int get_grid_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2]) {
|
||||
@@ -48,5 +46,3 @@ class AQICalculator : public AbstractAQICalculator {
|
||||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
#include "caqi_calculator.h"
|
||||
#include "aqi_calculator.h"
|
||||
|
||||
@@ -29,5 +27,3 @@ class AQICalculatorFactory {
|
||||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
#include "abstract_aqi_calculator.h"
|
||||
|
||||
@@ -37,9 +35,7 @@ class CAQICalculator : public AbstractAQICalculator {
|
||||
int conc_lo = array[grid_index][0];
|
||||
int conc_hi = array[grid_index][1];
|
||||
|
||||
int aqi = ((aqi_hi - aqi_lo) / (conc_hi - conc_lo)) * (value - conc_lo) + aqi_lo;
|
||||
|
||||
return aqi;
|
||||
return (value - conc_lo) * (aqi_hi - aqi_lo) / (conc_hi - conc_lo) + aqi_lo;
|
||||
}
|
||||
|
||||
int get_grid_index_(uint16_t value, int array[AMOUNT_OF_LEVELS][2]) {
|
||||
@@ -54,5 +50,3 @@ class CAQICalculator : public AbstractAQICalculator {
|
||||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
#include "hm3301.h"
|
||||
|
||||
@@ -14,9 +12,8 @@ static const uint8_t PM_10_0_VALUE_INDEX = 7;
|
||||
|
||||
void HM3301Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up HM3301...");
|
||||
hm3301_ = make_unique<HM330X>();
|
||||
error_code_ = hm3301_->init();
|
||||
if (error_code_ != NO_ERROR) {
|
||||
if (i2c::ERROR_OK != this->write(&SELECT_COMM_CMD, 1)) {
|
||||
error_code_ = ERROR_COMM;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
@@ -38,7 +35,7 @@ void HM3301Component::dump_config() {
|
||||
float HM3301Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
void HM3301Component::update() {
|
||||
if (!this->read_sensor_value_(data_buffer_)) {
|
||||
if (this->read(data_buffer_, 29) != i2c::ERROR_OK) {
|
||||
ESP_LOGW(TAG, "Read result failed");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
@@ -87,8 +84,6 @@ void HM3301Component::update() {
|
||||
this->status_clear_warning();
|
||||
}
|
||||
|
||||
bool HM3301Component::read_sensor_value_(uint8_t *data) { return !hm3301_->read_sensor_value(data, 29); }
|
||||
|
||||
bool HM3301Component::validate_checksum_(const uint8_t *data) {
|
||||
uint8_t sum = 0;
|
||||
for (int i = 0; i < 28; i++) {
|
||||
@@ -104,5 +99,3 @@ uint16_t HM3301Component::get_sensor_value_(const uint8_t *data, uint8_t i) {
|
||||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "aqi_calculator_factory.h"
|
||||
|
||||
#include <Seeed_HM330X.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace hm3301 {
|
||||
|
||||
static const uint8_t SELECT_COMM_CMD = 0X88;
|
||||
|
||||
class HM3301Component : public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
HM3301Component() = default;
|
||||
@@ -29,9 +27,12 @@ class HM3301Component : public PollingComponent, public i2c::I2CDevice {
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
std::unique_ptr<HM330X> hm3301_;
|
||||
|
||||
HM330XErrorCode error_code_{NO_ERROR};
|
||||
enum {
|
||||
NO_ERROR = 0,
|
||||
ERROR_PARAM = -1,
|
||||
ERROR_COMM = -2,
|
||||
ERROR_OTHERS = -128,
|
||||
} error_code_{NO_ERROR};
|
||||
|
||||
uint8_t data_buffer_[30];
|
||||
|
||||
@@ -43,12 +44,9 @@ class HM3301Component : public PollingComponent, public i2c::I2CDevice {
|
||||
AQICalculatorType aqi_calc_type_;
|
||||
AQICalculatorFactory aqi_calculator_factory_ = AQICalculatorFactory();
|
||||
|
||||
bool read_sensor_value_(uint8_t *);
|
||||
bool validate_checksum_(const uint8_t *);
|
||||
uint16_t get_sensor_value_(const uint8_t *, uint8_t);
|
||||
};
|
||||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
@@ -84,7 +84,6 @@ CONFIG_SCHEMA = cv.All(
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x40)),
|
||||
_validate,
|
||||
cv.only_with_arduino,
|
||||
)
|
||||
|
||||
|
||||
@@ -109,6 +108,3 @@ async def to_code(config):
|
||||
sens = await sensor.new_sensor(config[CONF_AQI])
|
||||
cg.add(var.set_aqi_sensor(sens))
|
||||
cg.add(var.set_aqi_calculation_type(config[CONF_AQI][CONF_CALCULATION_TYPE]))
|
||||
|
||||
# https://platformio.org/lib/show/6306/Grove%20-%20Laser%20PM2.5%20Sensor%20HM3301
|
||||
cg.add_library("seeed-studio/Grove - Laser PM2.5 Sensor HM3301", "1.0.3")
|
||||
|
||||
@@ -114,8 +114,8 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
def auto_data_rate(config):
|
||||
interval_sec = config[CONF_UPDATE_INTERVAL].seconds
|
||||
interval_hz = 1.0 / interval_sec
|
||||
interval_msec = config[CONF_UPDATE_INTERVAL].total_milliseconds
|
||||
interval_hz = 1000.0 / interval_msec
|
||||
for datarate in sorted(HMC5883LDatarates.keys()):
|
||||
if float(datarate) >= interval_hz:
|
||||
return HMC5883LDatarates[datarate]
|
||||
|
||||
@@ -10,7 +10,7 @@ static const char *const TAG = "homeassistant.sensor";
|
||||
void HomeassistantSensor::setup() {
|
||||
api::global_api_server->subscribe_home_assistant_state(
|
||||
this->entity_id_, this->attribute_, [this](const std::string &state) {
|
||||
auto val = parse_float(state);
|
||||
auto val = parse_number<float>(state);
|
||||
if (!val.has_value()) {
|
||||
ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
|
||||
this->publish_state(NAN);
|
||||
|
||||
@@ -44,7 +44,7 @@ void HrxlMaxsonarWrComponent::check_buffer_() {
|
||||
|
||||
if (this->buffer_.length() == MAX_DATA_LENGTH_BYTES && this->buffer_[0] == 'R' &&
|
||||
this->buffer_.back() == static_cast<char>(ASCII_CR)) {
|
||||
int millimeters = strtol(this->buffer_.substr(1, MAX_DATA_LENGTH_BYTES - 2).c_str(), nullptr, 10);
|
||||
int millimeters = parse_number<int>(this->buffer_.substr(1, MAX_DATA_LENGTH_BYTES - 2)).value_or(0);
|
||||
float meters = float(millimeters) / 1000.0;
|
||||
ESP_LOGV(TAG, "Distance from sensor: %d mm, %f m", millimeters, meters);
|
||||
this->publish_state(meters);
|
||||
|
||||
@@ -96,6 +96,8 @@ async def to_code(config):
|
||||
if CORE.is_esp32:
|
||||
cg.add_library("WiFiClientSecure", None)
|
||||
cg.add_library("HTTPClient", None)
|
||||
if CORE.is_esp8266:
|
||||
cg.add_library("ESP8266HTTPClient", None)
|
||||
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "i2c_bus.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/optional.h"
|
||||
#include <array>
|
||||
#include <vector>
|
||||
@@ -32,16 +33,8 @@ class I2CRegister {
|
||||
|
||||
// like ntohs/htons but without including networking headers.
|
||||
// ("i2c" byte order is big-endian)
|
||||
inline uint16_t i2ctohs(uint16_t i2cshort) {
|
||||
union {
|
||||
uint16_t x;
|
||||
uint8_t y[2];
|
||||
} conv;
|
||||
conv.x = i2cshort;
|
||||
return ((uint16_t) conv.y[0] << 8) | ((uint16_t) conv.y[1] << 0);
|
||||
}
|
||||
|
||||
inline uint16_t htoi2cs(uint16_t hostshort) { return i2ctohs(hostshort); }
|
||||
inline uint16_t i2ctohs(uint16_t i2cshort) { return convert_big_endian(i2cshort); }
|
||||
inline uint16_t htoi2cs(uint16_t hostshort) { return convert_big_endian(hostshort); }
|
||||
|
||||
class I2CDevice {
|
||||
public:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace i2c {
|
||||
@@ -40,6 +42,20 @@ class I2CBus {
|
||||
return writev(address, &buf, 1);
|
||||
}
|
||||
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) = 0;
|
||||
|
||||
protected:
|
||||
void i2c_scan_() {
|
||||
for (uint8_t address = 8; address < 120; address++) {
|
||||
auto err = writev(address, nullptr, 0);
|
||||
if (err == ERROR_OK) {
|
||||
scan_results_.emplace_back(address, true);
|
||||
} else if (err == ERROR_UNKNOWN) {
|
||||
scan_results_.emplace_back(address, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<std::pair<uint8_t, bool>> scan_results_;
|
||||
bool scan_{false};
|
||||
};
|
||||
|
||||
} // namespace i2c
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "i2c_bus_arduino.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <Arduino.h>
|
||||
#include <cstring>
|
||||
|
||||
@@ -27,6 +28,10 @@ void ArduinoI2CBus::setup() {
|
||||
wire_->begin(sda_pin_, scl_pin_);
|
||||
wire_->setClock(frequency_);
|
||||
initialized_ = true;
|
||||
if (this->scan_) {
|
||||
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
|
||||
this->i2c_scan_();
|
||||
}
|
||||
}
|
||||
void ArduinoI2CBus::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
||||
@@ -45,22 +50,20 @@ void ArduinoI2CBus::dump_config() {
|
||||
break;
|
||||
}
|
||||
if (this->scan_) {
|
||||
ESP_LOGI(TAG, "Scanning i2c bus for active devices...");
|
||||
uint8_t found = 0;
|
||||
for (uint8_t address = 8; address < 120; address++) {
|
||||
auto err = writev(address, nullptr, 0);
|
||||
if (err == ERROR_OK) {
|
||||
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", address);
|
||||
found++;
|
||||
} else if (err == ERROR_UNKNOWN) {
|
||||
ESP_LOGI(TAG, "Unknown error at address 0x%02X", address);
|
||||
}
|
||||
}
|
||||
if (found == 0) {
|
||||
ESP_LOGI(TAG, "Results from i2c bus scan:");
|
||||
if (scan_results_.empty()) {
|
||||
ESP_LOGI(TAG, "Found no i2c devices!");
|
||||
} else {
|
||||
for (const auto &s : scan_results_) {
|
||||
if (s.second)
|
||||
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", s.first);
|
||||
else
|
||||
ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ErrorCode ArduinoI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
||||
// logging is only enabled with vv level, if warnings are shown the caller
|
||||
// should log them
|
||||
|
||||
@@ -34,7 +34,6 @@ class ArduinoI2CBus : public I2CBus, public Component {
|
||||
|
||||
protected:
|
||||
TwoWire *wire_;
|
||||
bool scan_;
|
||||
uint8_t sda_pin_;
|
||||
uint8_t scl_pin_;
|
||||
uint32_t frequency_;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "i2c_bus_esp_idf.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace esphome {
|
||||
@@ -37,6 +38,10 @@ void IDFI2CBus::setup() {
|
||||
return;
|
||||
}
|
||||
initialized_ = true;
|
||||
if (this->scan_) {
|
||||
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
|
||||
this->i2c_scan_();
|
||||
}
|
||||
}
|
||||
void IDFI2CBus::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
||||
@@ -55,23 +60,20 @@ void IDFI2CBus::dump_config() {
|
||||
break;
|
||||
}
|
||||
if (this->scan_) {
|
||||
ESP_LOGI(TAG, "Scanning i2c bus for active devices...");
|
||||
uint8_t found = 0;
|
||||
for (uint8_t address = 8; address < 120; address++) {
|
||||
auto err = writev(address, nullptr, 0);
|
||||
|
||||
if (err == ERROR_OK) {
|
||||
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", address);
|
||||
found++;
|
||||
} else if (err == ERROR_UNKNOWN) {
|
||||
ESP_LOGI(TAG, "Unknown error at address 0x%02X", address);
|
||||
}
|
||||
}
|
||||
if (found == 0) {
|
||||
ESP_LOGI(TAG, "Results from i2c bus scan:");
|
||||
if (scan_results_.empty()) {
|
||||
ESP_LOGI(TAG, "Found no i2c devices!");
|
||||
} else {
|
||||
for (const auto &s : scan_results_) {
|
||||
if (s.second)
|
||||
ESP_LOGI(TAG, "Found i2c device at address 0x%02X", s.first);
|
||||
else
|
||||
ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ErrorCode IDFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
||||
// logging is only enabled with vv level, if warnings are shown the caller
|
||||
// should log them
|
||||
|
||||
@@ -36,7 +36,6 @@ class IDFI2CBus : public I2CBus, public Component {
|
||||
|
||||
protected:
|
||||
i2c_port_t port_;
|
||||
bool scan_;
|
||||
uint8_t sda_pin_;
|
||||
bool sda_pullup_enabled_;
|
||||
uint8_t scl_pin_;
|
||||
|
||||
@@ -86,8 +86,8 @@ void ILI9341Display::update() {
|
||||
|
||||
void ILI9341Display::display_() {
|
||||
// we will only update the changed window to the display
|
||||
int w = this->x_high_ - this->x_low_ + 1;
|
||||
int h = this->y_high_ - this->y_low_ + 1;
|
||||
uint16_t w = this->x_high_ - this->x_low_ + 1;
|
||||
uint16_t h = this->y_high_ - this->y_low_ + 1;
|
||||
|
||||
set_addr_window_(this->x_low_, this->y_low_, w, h);
|
||||
this->start_data_();
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
@@ -1,91 +0,0 @@
|
||||
#include "improv.h"
|
||||
|
||||
namespace improv {
|
||||
|
||||
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data) {
|
||||
return parse_improv_data(data.data(), data.size());
|
||||
}
|
||||
|
||||
ImprovCommand parse_improv_data(const uint8_t *data, size_t length) {
|
||||
Command command = (Command) data[0];
|
||||
uint8_t data_length = data[1];
|
||||
|
||||
if (data_length != length - 3) {
|
||||
return {.command = UNKNOWN};
|
||||
}
|
||||
|
||||
uint8_t checksum = data[length - 1];
|
||||
|
||||
uint32_t calculated_checksum = 0;
|
||||
for (uint8_t i = 0; i < length - 1; i++) {
|
||||
calculated_checksum += data[i];
|
||||
}
|
||||
|
||||
if ((uint8_t) calculated_checksum != checksum) {
|
||||
return {.command = BAD_CHECKSUM};
|
||||
}
|
||||
|
||||
if (command == WIFI_SETTINGS) {
|
||||
uint8_t ssid_length = data[2];
|
||||
uint8_t ssid_start = 3;
|
||||
size_t ssid_end = ssid_start + ssid_length;
|
||||
|
||||
uint8_t pass_length = data[ssid_end];
|
||||
size_t pass_start = ssid_end + 1;
|
||||
size_t pass_end = pass_start + pass_length;
|
||||
|
||||
std::string ssid(data + ssid_start, data + ssid_end);
|
||||
std::string password(data + pass_start, data + pass_end);
|
||||
return {.command = command, .ssid = ssid, .password = password};
|
||||
}
|
||||
|
||||
return {
|
||||
.command = command,
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum) {
|
||||
std::vector<uint8_t> out;
|
||||
uint32_t length = 0;
|
||||
out.push_back(command);
|
||||
for (auto str : datum) {
|
||||
uint8_t len = str.length();
|
||||
length += len;
|
||||
out.push_back(len);
|
||||
out.insert(out.end(), str.begin(), str.end());
|
||||
}
|
||||
out.insert(out.begin() + 1, length);
|
||||
|
||||
uint32_t calculated_checksum = 0;
|
||||
|
||||
for (uint8_t byte : out) {
|
||||
calculated_checksum += byte;
|
||||
}
|
||||
out.push_back(calculated_checksum);
|
||||
return out;
|
||||
}
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum) {
|
||||
std::vector<uint8_t> out;
|
||||
uint32_t length = 0;
|
||||
out.push_back(command);
|
||||
for (auto str : datum) {
|
||||
uint8_t len = str.length();
|
||||
length += len;
|
||||
out.push_back(len);
|
||||
out.insert(out.end(), str.begin(), str.end());
|
||||
}
|
||||
out.insert(out.begin() + 1, length);
|
||||
|
||||
uint32_t calculated_checksum = 0;
|
||||
|
||||
for (uint8_t byte : out) {
|
||||
calculated_checksum += byte;
|
||||
}
|
||||
out.push_back(calculated_checksum);
|
||||
return out;
|
||||
}
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
} // namespace improv
|
||||
@@ -1,60 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include "WString.h"
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace improv {
|
||||
|
||||
static const char *const SERVICE_UUID = "00467768-6228-2272-4663-277478268000";
|
||||
static const char *const STATUS_UUID = "00467768-6228-2272-4663-277478268001";
|
||||
static const char *const ERROR_UUID = "00467768-6228-2272-4663-277478268002";
|
||||
static const char *const RPC_COMMAND_UUID = "00467768-6228-2272-4663-277478268003";
|
||||
static const char *const RPC_RESULT_UUID = "00467768-6228-2272-4663-277478268004";
|
||||
static const char *const CAPABILITIES_UUID = "00467768-6228-2272-4663-277478268005";
|
||||
|
||||
enum Error : uint8_t {
|
||||
ERROR_NONE = 0x00,
|
||||
ERROR_INVALID_RPC = 0x01,
|
||||
ERROR_UNKNOWN_RPC = 0x02,
|
||||
ERROR_UNABLE_TO_CONNECT = 0x03,
|
||||
ERROR_NOT_AUTHORIZED = 0x04,
|
||||
ERROR_UNKNOWN = 0xFF,
|
||||
};
|
||||
|
||||
enum State : uint8_t {
|
||||
STATE_STOPPED = 0x00,
|
||||
STATE_AWAITING_AUTHORIZATION = 0x01,
|
||||
STATE_AUTHORIZED = 0x02,
|
||||
STATE_PROVISIONING = 0x03,
|
||||
STATE_PROVISIONED = 0x04,
|
||||
};
|
||||
|
||||
enum Command : uint8_t {
|
||||
UNKNOWN = 0x00,
|
||||
WIFI_SETTINGS = 0x01,
|
||||
IDENTIFY = 0x02,
|
||||
BAD_CHECKSUM = 0xFF,
|
||||
};
|
||||
|
||||
static const uint8_t CAPABILITY_IDENTIFY = 0x01;
|
||||
|
||||
struct ImprovCommand {
|
||||
Command command;
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data);
|
||||
ImprovCommand parse_improv_data(const uint8_t *data, size_t length);
|
||||
|
||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum);
|
||||
#ifdef USE_ARDUINO
|
||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum);
|
||||
#endif // USE_ARDUINO
|
||||
|
||||
} // namespace improv
|
||||
33
esphome/components/improv_serial/__init__.py
Normal file
33
esphome/components/improv_serial/__init__.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from esphome.const import CONF_BAUD_RATE, CONF_ID, CONF_LOGGER
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
import esphome.final_validate as fv
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
DEPENDENCIES = ["logger", "wifi"]
|
||||
|
||||
improv_serial_ns = cg.esphome_ns.namespace("improv_serial")
|
||||
|
||||
ImprovSerialComponent = improv_serial_ns.class_("ImprovSerialComponent", cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ImprovSerialComponent),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
def validate_logger_baud_rate(config):
|
||||
logger_conf = fv.full_config.get()[CONF_LOGGER]
|
||||
if logger_conf[CONF_BAUD_RATE] == 0:
|
||||
raise cv.Invalid("improv_serial requires the logger baud_rate to be not 0")
|
||||
return config
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = validate_logger_baud_rate
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
cg.add_library("esphome/Improv", "1.0.0")
|
||||
266
esphome/components/improv_serial/improv_serial_component.cpp
Normal file
266
esphome/components/improv_serial/improv_serial_component.cpp
Normal file
@@ -0,0 +1,266 @@
|
||||
#include "improv_serial_component.h"
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/version.h"
|
||||
|
||||
#include "esphome/components/logger/logger.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace improv_serial {
|
||||
|
||||
static const char *const TAG = "improv_serial";
|
||||
|
||||
void ImprovSerialComponent::setup() {
|
||||
global_improv_serial_component = this;
|
||||
#ifdef USE_ARDUINO
|
||||
this->hw_serial_ = logger::global_logger->get_hw_serial();
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
this->uart_num_ = logger::global_logger->get_uart_num();
|
||||
#endif
|
||||
|
||||
if (wifi::global_wifi_component->has_sta()) {
|
||||
this->state_ = improv::STATE_PROVISIONED;
|
||||
}
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::dump_config() { ESP_LOGCONFIG(TAG, "Improv Serial:"); }
|
||||
|
||||
int ImprovSerialComponent::available_() {
|
||||
#ifdef USE_ARDUINO
|
||||
return this->hw_serial_->available();
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
size_t available;
|
||||
uart_get_buffered_data_len(this->uart_num_, &available);
|
||||
return available;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t ImprovSerialComponent::read_byte_() {
|
||||
uint8_t data;
|
||||
#ifdef USE_ARDUINO
|
||||
this->hw_serial_->readBytes(&data, 1);
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
uart_read_bytes(this->uart_num_, &data, 1, 20 / portTICK_RATE_MS);
|
||||
#endif
|
||||
return data;
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::write_data_(std::vector<uint8_t> &data) {
|
||||
data.push_back('\n');
|
||||
#ifdef USE_ARDUINO
|
||||
this->hw_serial_->write(data.data(), data.size());
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
uart_write_bytes(this->uart_num_, data.data(), data.size());
|
||||
#endif
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::loop() {
|
||||
const uint32_t now = millis();
|
||||
if (now - this->last_read_byte_ > 50) {
|
||||
this->rx_buffer_.clear();
|
||||
this->last_read_byte_ = now;
|
||||
}
|
||||
|
||||
while (this->available_()) {
|
||||
uint8_t byte = this->read_byte_();
|
||||
if (this->parse_improv_serial_byte_(byte)) {
|
||||
this->last_read_byte_ = now;
|
||||
} else {
|
||||
this->rx_buffer_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (this->state_ == improv::STATE_PROVISIONING) {
|
||||
if (wifi::global_wifi_component->is_connected()) {
|
||||
wifi::global_wifi_component->save_wifi_sta(this->connecting_sta_.get_ssid(),
|
||||
this->connecting_sta_.get_password());
|
||||
this->connecting_sta_ = {};
|
||||
this->cancel_timeout("wifi-connect-timeout");
|
||||
this->set_state_(improv::STATE_PROVISIONED);
|
||||
|
||||
std::vector<uint8_t> url = this->build_rpc_settings_response_(improv::WIFI_SETTINGS);
|
||||
this->send_response_(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ImprovSerialComponent::build_rpc_settings_response_(improv::Command command) {
|
||||
std::vector<std::string> urls;
|
||||
#ifdef USE_WEBSERVER
|
||||
auto ip = wifi::global_wifi_component->wifi_sta_ip();
|
||||
std::string webserver_url = "http://" + ip.str() + ":" + to_string(WEBSERVER_PORT);
|
||||
urls.push_back(webserver_url);
|
||||
#endif
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(command, urls, false);
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ImprovSerialComponent::build_version_info_() {
|
||||
std::vector<std::string> infos = {"ESPHome", ESPHOME_VERSION, ESPHOME_VARIANT, App.get_name()};
|
||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::GET_DEVICE_INFO, infos, false);
|
||||
return data;
|
||||
};
|
||||
|
||||
bool ImprovSerialComponent::parse_improv_serial_byte_(uint8_t byte) {
|
||||
size_t at = this->rx_buffer_.size();
|
||||
this->rx_buffer_.push_back(byte);
|
||||
ESP_LOGD(TAG, "Improv Serial byte: 0x%02X", byte);
|
||||
const uint8_t *raw = &this->rx_buffer_[0];
|
||||
if (at == 0)
|
||||
return byte == 'I';
|
||||
if (at == 1)
|
||||
return byte == 'M';
|
||||
if (at == 2)
|
||||
return byte == 'P';
|
||||
if (at == 3)
|
||||
return byte == 'R';
|
||||
if (at == 4)
|
||||
return byte == 'O';
|
||||
if (at == 5)
|
||||
return byte == 'V';
|
||||
|
||||
if (at == 6)
|
||||
return byte == IMPROV_SERIAL_VERSION;
|
||||
|
||||
if (at == 7)
|
||||
return true;
|
||||
uint8_t type = raw[7];
|
||||
|
||||
if (at == 8)
|
||||
return true;
|
||||
uint8_t data_len = raw[8];
|
||||
|
||||
if (at < 8 + data_len)
|
||||
return true;
|
||||
|
||||
if (at == 8 + data_len)
|
||||
return true;
|
||||
|
||||
if (at == 8 + data_len + 1) {
|
||||
uint8_t checksum = 0x00;
|
||||
for (size_t i = 0; i < at; i++)
|
||||
checksum += raw[i];
|
||||
|
||||
if (checksum != byte) {
|
||||
ESP_LOGW(TAG, "Error decoding Improv payload");
|
||||
this->set_error_(improv::ERROR_INVALID_RPC);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == TYPE_RPC) {
|
||||
this->set_error_(improv::ERROR_NONE);
|
||||
auto command = improv::parse_improv_data(&raw[9], data_len, false);
|
||||
return this->parse_improv_payload_(command);
|
||||
}
|
||||
}
|
||||
|
||||
// If we got here then the command coming is is improv, but not an RPC command
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ImprovSerialComponent::parse_improv_payload_(improv::ImprovCommand &command) {
|
||||
switch (command.command) {
|
||||
case improv::WIFI_SETTINGS: {
|
||||
wifi::WiFiAP sta{};
|
||||
sta.set_ssid(command.ssid);
|
||||
sta.set_password(command.password);
|
||||
this->connecting_sta_ = sta;
|
||||
|
||||
wifi::global_wifi_component->set_sta(sta);
|
||||
wifi::global_wifi_component->start_scanning();
|
||||
this->set_state_(improv::STATE_PROVISIONING);
|
||||
ESP_LOGD(TAG, "Received Improv wifi settings ssid=%s, password=" LOG_SECRET("%s"), command.ssid.c_str(),
|
||||
command.password.c_str());
|
||||
|
||||
auto f = std::bind(&ImprovSerialComponent::on_wifi_connect_timeout_, this);
|
||||
this->set_timeout("wifi-connect-timeout", 30000, f);
|
||||
return true;
|
||||
}
|
||||
case improv::GET_CURRENT_STATE:
|
||||
this->set_state_(this->state_);
|
||||
if (this->state_ == improv::STATE_PROVISIONED) {
|
||||
std::vector<uint8_t> url = this->build_rpc_settings_response_(improv::GET_CURRENT_STATE);
|
||||
this->send_response_(url);
|
||||
}
|
||||
return true;
|
||||
case improv::GET_DEVICE_INFO: {
|
||||
std::vector<uint8_t> info = this->build_version_info_();
|
||||
this->send_response_(info);
|
||||
return true;
|
||||
}
|
||||
default: {
|
||||
ESP_LOGW(TAG, "Unknown Improv payload");
|
||||
this->set_error_(improv::ERROR_UNKNOWN_RPC);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::set_state_(improv::State state) {
|
||||
this->state_ = state;
|
||||
|
||||
std::vector<uint8_t> data = {'I', 'M', 'P', 'R', 'O', 'V'};
|
||||
data.resize(11);
|
||||
data[6] = IMPROV_SERIAL_VERSION;
|
||||
data[7] = TYPE_CURRENT_STATE;
|
||||
data[8] = 1;
|
||||
data[9] = state;
|
||||
|
||||
uint8_t checksum = 0x00;
|
||||
for (uint8_t d : data)
|
||||
checksum += d;
|
||||
data[10] = checksum;
|
||||
|
||||
this->write_data_(data);
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::set_error_(improv::Error error) {
|
||||
std::vector<uint8_t> data = {'I', 'M', 'P', 'R', 'O', 'V'};
|
||||
data.resize(11);
|
||||
data[6] = IMPROV_SERIAL_VERSION;
|
||||
data[7] = TYPE_ERROR_STATE;
|
||||
data[8] = 1;
|
||||
data[9] = error;
|
||||
|
||||
uint8_t checksum = 0x00;
|
||||
for (uint8_t d : data)
|
||||
checksum += d;
|
||||
data[10] = checksum;
|
||||
this->write_data_(data);
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::send_response_(std::vector<uint8_t> &response) {
|
||||
std::vector<uint8_t> data = {'I', 'M', 'P', 'R', 'O', 'V'};
|
||||
data.resize(9);
|
||||
data[6] = IMPROV_SERIAL_VERSION;
|
||||
data[7] = TYPE_RPC_RESPONSE;
|
||||
data[8] = response.size();
|
||||
data.insert(data.end(), response.begin(), response.end());
|
||||
|
||||
uint8_t checksum = 0x00;
|
||||
for (uint8_t d : data)
|
||||
checksum += d;
|
||||
data.push_back(checksum);
|
||||
|
||||
this->write_data_(data);
|
||||
}
|
||||
|
||||
void ImprovSerialComponent::on_wifi_connect_timeout_() {
|
||||
this->set_error_(improv::ERROR_UNABLE_TO_CONNECT);
|
||||
this->set_state_(improv::STATE_AUTHORIZED);
|
||||
ESP_LOGW(TAG, "Timed out trying to connect to given WiFi network");
|
||||
wifi::global_wifi_component->clear_sta();
|
||||
}
|
||||
|
||||
ImprovSerialComponent *global_improv_serial_component = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
} // namespace improv_serial
|
||||
} // namespace esphome
|
||||
70
esphome/components/improv_serial/improv_serial_component.h
Normal file
70
esphome/components/improv_serial/improv_serial_component.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#include <improv.h>
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include <HardwareSerial.h>
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
#include <driver/uart.h>
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace improv_serial {
|
||||
|
||||
enum ImprovSerialType : uint8_t {
|
||||
TYPE_CURRENT_STATE = 0x01,
|
||||
TYPE_ERROR_STATE = 0x02,
|
||||
TYPE_RPC = 0x03,
|
||||
TYPE_RPC_RESPONSE = 0x04
|
||||
};
|
||||
|
||||
static const uint8_t IMPROV_SERIAL_VERSION = 1;
|
||||
|
||||
class ImprovSerialComponent : public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
protected:
|
||||
bool parse_improv_serial_byte_(uint8_t byte);
|
||||
bool parse_improv_payload_(improv::ImprovCommand &command);
|
||||
|
||||
void set_state_(improv::State state);
|
||||
void set_error_(improv::Error error);
|
||||
void send_response_(std::vector<uint8_t> &response);
|
||||
void on_wifi_connect_timeout_();
|
||||
|
||||
std::vector<uint8_t> build_rpc_settings_response_(improv::Command command);
|
||||
std::vector<uint8_t> build_version_info_();
|
||||
|
||||
int available_();
|
||||
uint8_t read_byte_();
|
||||
void write_data_(std::vector<uint8_t> &data);
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
HardwareSerial *hw_serial_{nullptr};
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
uart_port_t uart_num_;
|
||||
#endif
|
||||
|
||||
std::vector<uint8_t> rx_buffer_;
|
||||
uint32_t last_read_byte_{0};
|
||||
wifi::WiFiAP connecting_sta_;
|
||||
improv::State state_{improv::STATE_AUTHORIZED};
|
||||
};
|
||||
|
||||
extern ImprovSerialComponent
|
||||
*global_improv_serial_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
} // namespace improv_serial
|
||||
} // namespace esphome
|
||||
@@ -9,6 +9,7 @@ from esphome.const import (
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PERCENT,
|
||||
@@ -53,6 +54,7 @@ CONFIG_SCHEMA = (
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_BATTERY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ void GPIOLCDDisplay::setup() {
|
||||
this->enable_pin_->setup(); // OUTPUT
|
||||
this->enable_pin_->digital_write(false);
|
||||
|
||||
for (uint8_t i = 0; i < (this->is_four_bit_mode() ? 4 : 8); i++) {
|
||||
for (uint8_t i = 0; i < (uint8_t)(this->is_four_bit_mode() ? 4u : 8u); i++) {
|
||||
this->data_pins_[i]->setup(); // OUTPUT
|
||||
this->data_pins_[i]->digital_write(false);
|
||||
}
|
||||
|
||||
@@ -15,17 +15,37 @@ namespace ledc {
|
||||
|
||||
static const char *const TAG = "ledc.output";
|
||||
|
||||
#ifdef USE_ESP_IDF
|
||||
static const int MAX_RES_BITS = LEDC_TIMER_BIT_MAX - 1;
|
||||
#if SOC_LEDC_SUPPORT_HS_MODE
|
||||
// Only ESP32 has LEDC_HIGH_SPEED_MODE
|
||||
inline ledc_mode_t get_speed_mode(uint8_t channel) { return channel < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE; }
|
||||
#else
|
||||
// S2, C3, S3 only support LEDC_LOW_SPEED_MODE
|
||||
// See
|
||||
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-reference/peripherals/ledc.html#functionality-overview
|
||||
inline ledc_mode_t get_speed_mode(uint8_t) { return LEDC_LOW_SPEED_MODE; }
|
||||
#endif
|
||||
#else
|
||||
static const int MAX_RES_BITS = 20;
|
||||
#endif
|
||||
|
||||
float ledc_max_frequency_for_bit_depth(uint8_t bit_depth) { return 80e6f / float(1 << bit_depth); }
|
||||
float ledc_min_frequency_for_bit_depth(uint8_t bit_depth) {
|
||||
const float max_div_num = ((1 << 20) - 1) / 256.0f;
|
||||
|
||||
float ledc_min_frequency_for_bit_depth(uint8_t bit_depth, bool low_frequency) {
|
||||
const float max_div_num = ((1 << MAX_RES_BITS) - 1) / (low_frequency ? 32.0f : 256.0f);
|
||||
return 80e6f / (max_div_num * float(1 << bit_depth));
|
||||
}
|
||||
|
||||
optional<uint8_t> ledc_bit_depth_for_frequency(float frequency) {
|
||||
for (int i = 20; i >= 1; i--) {
|
||||
const float min_frequency = ledc_min_frequency_for_bit_depth(i);
|
||||
ESP_LOGD(TAG, "Calculating resolution bit-depth for frequency %f", frequency);
|
||||
for (int i = MAX_RES_BITS; i >= 1; i--) {
|
||||
const float min_frequency = ledc_min_frequency_for_bit_depth(i, (frequency < 100));
|
||||
const float max_frequency = ledc_max_frequency_for_bit_depth(i);
|
||||
if (min_frequency <= frequency && frequency <= max_frequency)
|
||||
if (min_frequency <= frequency && frequency <= max_frequency) {
|
||||
ESP_LOGD(TAG, "Resolution calculated as %d", i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -48,7 +68,7 @@ void LEDCOutput::write_state(float state) {
|
||||
ledcWrite(this->channel_, duty);
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE;
|
||||
auto speed_mode = get_speed_mode(channel_);
|
||||
auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
|
||||
ledc_set_duty(speed_mode, chan_num, duty);
|
||||
ledc_update_duty(speed_mode, chan_num);
|
||||
@@ -63,11 +83,15 @@ void LEDCOutput::setup() {
|
||||
ledcAttachPin(this->pin_->get_pin(), this->channel_);
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE;
|
||||
auto speed_mode = get_speed_mode(channel_);
|
||||
auto timer_num = static_cast<ledc_timer_t>((channel_ % 8) / 2);
|
||||
auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
|
||||
|
||||
bit_depth_ = *ledc_bit_depth_for_frequency(frequency_);
|
||||
if (bit_depth_ < 1) {
|
||||
ESP_LOGW(TAG, "Frequency %f can't be achieved with any bit depth", frequency_);
|
||||
this->status_set_warning();
|
||||
}
|
||||
|
||||
ledc_timer_config_t timer_conf{};
|
||||
timer_conf.speed_mode = speed_mode;
|
||||
@@ -114,7 +138,7 @@ void LEDCOutput::update_frequency(float frequency) {
|
||||
ESP_LOGW(TAG, "LEDC output hasn't been initialized yet!");
|
||||
return;
|
||||
}
|
||||
auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE;
|
||||
auto speed_mode = get_speed_mode(channel_);
|
||||
auto timer_num = static_cast<ledc_timer_t>((channel_ % 8) / 2);
|
||||
|
||||
ledc_timer_config_t timer_conf{};
|
||||
|
||||
@@ -14,6 +14,7 @@ from esphome.const import (
|
||||
CONF_RESTORE_MODE,
|
||||
CONF_ON_TURN_OFF,
|
||||
CONF_ON_TURN_ON,
|
||||
CONF_ON_STATE,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_COLD_WHITE_COLOR_TEMPERATURE,
|
||||
CONF_WARM_WHITE_COLOR_TEMPERATURE,
|
||||
@@ -37,6 +38,7 @@ from .types import ( # noqa
|
||||
AddressableLight,
|
||||
LightTurnOnTrigger,
|
||||
LightTurnOffTrigger,
|
||||
LightStateTrigger,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
@@ -69,6 +71,11 @@ LIGHT_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).ex
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LightTurnOffTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_STATE): auto.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LightStateTrigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -151,6 +158,9 @@ async def setup_light_core_(light_var, output_var, config):
|
||||
for conf in config.get(CONF_ON_TURN_OFF, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var)
|
||||
await auto.build_automation(trigger, [], conf)
|
||||
for conf in config.get(CONF_ON_STATE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var)
|
||||
await auto.build_automation(trigger, [], conf)
|
||||
|
||||
if CONF_COLOR_CORRECT in config:
|
||||
cg.add(output_var.set_correction(*config[CONF_COLOR_CORRECT]))
|
||||
|
||||
@@ -87,7 +87,7 @@ class AddressableLight : public LightOutput, public Component {
|
||||
void mark_shown_() {
|
||||
#ifdef USE_POWER_SUPPLY
|
||||
for (const auto &c : *this) {
|
||||
if (c.get().is_on()) {
|
||||
if (c.get_red_raw() > 0 || c.get_green_raw() > 0 || c.get_blue_raw() > 0 || c.get_white_raw() > 0) {
|
||||
this->power_.request();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ class AddressableScanEffect : public AddressableLightEffect {
|
||||
this->last_move_ = now;
|
||||
|
||||
it.all() = Color::BLACK;
|
||||
for (auto i = 0; i < this->scan_width_; i++) {
|
||||
for (uint32_t i = 0; i < this->scan_width_; i++) {
|
||||
it[this->at_led_ + i] = current_color;
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ class AddressableScanEffect : public AddressableLightEffect {
|
||||
uint32_t move_interval_{};
|
||||
uint32_t scan_width_{1};
|
||||
uint32_t last_move_{0};
|
||||
int at_led_{0};
|
||||
uint32_t at_led_{0};
|
||||
bool direction_{true};
|
||||
};
|
||||
|
||||
|
||||
@@ -141,6 +141,13 @@ class LightTurnOffTrigger : public Trigger<> {
|
||||
}
|
||||
};
|
||||
|
||||
class LightStateTrigger : public Trigger<> {
|
||||
public:
|
||||
LightStateTrigger(LightState *a_light) {
|
||||
a_light->add_new_remote_values_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
// This is slightly ugly, but we can't log in headers, and can't make this a static method on AddressableSet
|
||||
// due to the template. It's just a temporary warning anyway.
|
||||
void addressableset_warn_about_scale(const char *field);
|
||||
|
||||
@@ -98,7 +98,7 @@ void LightCall::perform() {
|
||||
// EFFECT
|
||||
auto effect = this->effect_;
|
||||
const char *effect_s;
|
||||
if (effect == 0)
|
||||
if (effect == 0u)
|
||||
effect_s = "None";
|
||||
else
|
||||
effect_s = this->parent_->effects_[*this->effect_ - 1]->get_name().c_str();
|
||||
|
||||
@@ -41,6 +41,7 @@ LightTurnOnTrigger = light_ns.class_(
|
||||
LightTurnOffTrigger = light_ns.class_(
|
||||
"LightTurnOffTrigger", automation.Trigger.template()
|
||||
)
|
||||
LightStateTrigger = light_ns.class_("LightStateTrigger", automation.Trigger.template())
|
||||
|
||||
# Effects
|
||||
LightEffect = light_ns.class_("LightEffect")
|
||||
|
||||
@@ -221,7 +221,7 @@ UARTSelection Logger::get_uart() const { return this->uart_; }
|
||||
void Logger::add_on_log_callback(std::function<void(int, const char *, const char *)> &&callback) {
|
||||
this->log_callback_.add(std::move(callback));
|
||||
}
|
||||
float Logger::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
|
||||
float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
|
||||
const char *const LOG_LEVELS[] = {"NONE", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "VERBOSE", "VERY_VERBOSE"};
|
||||
#ifdef USE_ESP32
|
||||
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "UART2"};
|
||||
|
||||
@@ -97,7 +97,7 @@ void LTR390Component::read_mode_(int mode_index) {
|
||||
|
||||
// If there are more modes to read then begin the next
|
||||
// otherwise stop
|
||||
if (mode_index + 1 < this->mode_funcs_.size()) {
|
||||
if (mode_index + 1 < (int) this->mode_funcs_.size()) {
|
||||
this->read_mode_(mode_index + 1);
|
||||
} else {
|
||||
this->reading_ = false;
|
||||
|
||||
@@ -203,16 +203,16 @@ float MAX31865Sensor::calc_temperature_(float rtd_ratio) {
|
||||
rtd_resistance *= 100;
|
||||
}
|
||||
float rpoly = rtd_resistance;
|
||||
float neg_temp = -242.02;
|
||||
neg_temp += 2.2228 * rpoly;
|
||||
float neg_temp = -242.02f;
|
||||
neg_temp += 2.2228f * rpoly;
|
||||
rpoly *= rtd_resistance; // square
|
||||
neg_temp += 2.5859e-3 * rpoly;
|
||||
neg_temp += 2.5859e-3f * rpoly;
|
||||
rpoly *= rtd_resistance; // ^3
|
||||
neg_temp -= 4.8260e-6 * rpoly;
|
||||
neg_temp -= 4.8260e-6f * rpoly;
|
||||
rpoly *= rtd_resistance; // ^4
|
||||
neg_temp -= 2.8183e-8 * rpoly;
|
||||
neg_temp -= 2.8183e-8f * rpoly;
|
||||
rpoly *= rtd_resistance; // ^5
|
||||
neg_temp += 1.5243e-10 * rpoly;
|
||||
neg_temp += 1.5243e-10f * rpoly;
|
||||
return neg_temp;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,20 @@ CONF_SCROLL_DELAY = "scroll_delay"
|
||||
CONF_SCROLL_ENABLE = "scroll_enable"
|
||||
CONF_SCROLL_MODE = "scroll_mode"
|
||||
CONF_REVERSE_ENABLE = "reverse_enable"
|
||||
CONF_NUM_CHIP_LINES = "num_chip_lines"
|
||||
CONF_CHIP_LINES_STYLE = "chip_lines_style"
|
||||
|
||||
integration_ns = cg.esphome_ns.namespace("max7219digit")
|
||||
ChipLinesStyle = integration_ns.enum("ChipLinesStyle")
|
||||
CHIP_LINES_STYLE = {
|
||||
"ZIGZAG": ChipLinesStyle.ZIGZAG,
|
||||
"SNAKE": ChipLinesStyle.SNAKE,
|
||||
}
|
||||
|
||||
ScrollMode = integration_ns.enum("ScrollMode")
|
||||
SCROLL_MODES = {
|
||||
"CONTINUOUS": 0,
|
||||
"STOP": 1,
|
||||
"CONTINUOUS": ScrollMode.CONTINUOUS,
|
||||
"STOP": ScrollMode.STOP,
|
||||
}
|
||||
|
||||
CHIP_MODES = {
|
||||
@@ -37,6 +47,10 @@ CONFIG_SCHEMA = (
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(MAX7219Component),
|
||||
cv.Optional(CONF_NUM_CHIPS, default=4): cv.int_range(min=1, max=255),
|
||||
cv.Optional(CONF_NUM_CHIP_LINES, default=1): cv.int_range(min=1, max=255),
|
||||
cv.Optional(CONF_CHIP_LINES_STYLE, default="SNAKE"): cv.enum(
|
||||
CHIP_LINES_STYLE, upper=True
|
||||
),
|
||||
cv.Optional(CONF_INTENSITY, default=15): cv.int_range(min=0, max=15),
|
||||
cv.Optional(CONF_ROTATE_CHIP, default="0"): cv.enum(CHIP_MODES, upper=True),
|
||||
cv.Optional(CONF_SCROLL_MODE, default="CONTINUOUS"): cv.enum(
|
||||
@@ -67,6 +81,8 @@ async def to_code(config):
|
||||
await display.register_display(var, config)
|
||||
|
||||
cg.add(var.set_num_chips(config[CONF_NUM_CHIPS]))
|
||||
cg.add(var.set_num_chip_lines(config[CONF_NUM_CHIP_LINES]))
|
||||
cg.add(var.set_chip_lines_style(config[CONF_CHIP_LINES_STYLE]))
|
||||
cg.add(var.set_intensity(config[CONF_INTENSITY]))
|
||||
cg.add(var.set_chip_orientation(config[CONF_ROTATE_CHIP]))
|
||||
cg.add(var.set_scroll_speed(config[CONF_SCROLL_SPEED]))
|
||||
|
||||
@@ -26,9 +26,12 @@ void MAX7219Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up MAX7219_DIGITS...");
|
||||
this->spi_setup();
|
||||
this->stepsleft_ = 0;
|
||||
this->max_displaybuffer_.reserve(500); // Create base space to write buffer
|
||||
// Initialize buffer with 0 for display so all non written pixels are blank
|
||||
this->max_displaybuffer_.resize(this->num_chips_ * 8, 0);
|
||||
for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
|
||||
std::vector<uint8_t> vec(1);
|
||||
this->max_displaybuffer_.push_back(vec);
|
||||
// Initialize buffer with 0 for display so all non written pixels are blank
|
||||
this->max_displaybuffer_[chip_line].resize(get_width_internal(), 0);
|
||||
}
|
||||
// let's assume the user has all 8 digits connected, only important in daisy chained setups anyway
|
||||
this->send_to_all_(MAX7219_REGISTER_SCAN_LIMIT, 7);
|
||||
// let's use our own ASCII -> led pattern encoding
|
||||
@@ -46,6 +49,8 @@ void MAX7219Component::setup() {
|
||||
void MAX7219Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "MAX7219DIGIT:");
|
||||
ESP_LOGCONFIG(TAG, " Number of Chips: %u", this->num_chips_);
|
||||
ESP_LOGCONFIG(TAG, " Number of Chips Lines: %u", this->num_chip_lines_);
|
||||
ESP_LOGCONFIG(TAG, " Chips Lines Style : %u", this->chip_lines_style_);
|
||||
ESP_LOGCONFIG(TAG, " Intensity: %u", this->intensity_);
|
||||
ESP_LOGCONFIG(TAG, " Scroll Mode: %u", this->scroll_mode_);
|
||||
ESP_LOGCONFIG(TAG, " Scroll Speed: %u", this->scroll_speed_);
|
||||
@@ -59,19 +64,19 @@ void MAX7219Component::loop() {
|
||||
uint32_t now = millis();
|
||||
|
||||
// check if the buffer has shrunk past the current position since last update
|
||||
if ((this->max_displaybuffer_.size() >= this->old_buffer_size_ + 3) ||
|
||||
(this->max_displaybuffer_.size() <= this->old_buffer_size_ - 3)) {
|
||||
if ((this->max_displaybuffer_[0].size() >= this->old_buffer_size_ + 3) ||
|
||||
(this->max_displaybuffer_[0].size() <= this->old_buffer_size_ - 3)) {
|
||||
this->stepsleft_ = 0;
|
||||
this->display();
|
||||
this->old_buffer_size_ = this->max_displaybuffer_.size();
|
||||
this->old_buffer_size_ = this->max_displaybuffer_[0].size();
|
||||
}
|
||||
|
||||
// Reset the counter back to 0 when full string has been displayed.
|
||||
if (this->stepsleft_ > this->max_displaybuffer_.size())
|
||||
if (this->stepsleft_ > this->max_displaybuffer_[0].size())
|
||||
this->stepsleft_ = 0;
|
||||
|
||||
// Return if there is no need to scroll or scroll is off
|
||||
if (!this->scroll_ || (this->max_displaybuffer_.size() <= this->num_chips_ * 8)) {
|
||||
if (!this->scroll_ || (this->max_displaybuffer_[0].size() <= (size_t) get_width_internal())) {
|
||||
this->display();
|
||||
return;
|
||||
}
|
||||
@@ -82,8 +87,8 @@ void MAX7219Component::loop() {
|
||||
}
|
||||
|
||||
// Dwell time at end of string in case of stop at end
|
||||
if (this->scroll_mode_ == 1) {
|
||||
if (this->stepsleft_ >= this->max_displaybuffer_.size() - this->num_chips_ * 8 + 1) {
|
||||
if (this->scroll_mode_ == ScrollMode::STOP) {
|
||||
if (this->stepsleft_ >= this->max_displaybuffer_[0].size() - (size_t) get_width_internal() + 1) {
|
||||
if (now - this->last_scroll_ >= this->scroll_dwell_) {
|
||||
this->stepsleft_ = 0;
|
||||
this->last_scroll_ = now;
|
||||
@@ -107,30 +112,53 @@ void MAX7219Component::display() {
|
||||
// Run this routine for the rows of every chip 8x row 0 top to 7 bottom
|
||||
// Fill the pixel parameter with display data
|
||||
// Send the data to the chip
|
||||
for (uint8_t i = 0; i < this->num_chips_; i++) {
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
if (this->reverse_) {
|
||||
pixels[j] = this->max_displaybuffer_[(this->num_chips_ - i - 1) * 8 + j];
|
||||
} else {
|
||||
pixels[j] = this->max_displaybuffer_[i * 8 + j];
|
||||
for (uint8_t chip = 0; chip < this->num_chips_ / this->num_chip_lines_; chip++) {
|
||||
for (uint8_t chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
bool reverse =
|
||||
chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE ? !this->reverse_ : this->reverse_;
|
||||
if (reverse) {
|
||||
pixels[j] =
|
||||
this->max_displaybuffer_[chip_line][(this->num_chips_ / this->num_chip_lines_ - chip - 1) * 8 + j];
|
||||
} else {
|
||||
pixels[j] = this->max_displaybuffer_[chip_line][chip * 8 + j];
|
||||
}
|
||||
}
|
||||
if (chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE)
|
||||
this->orientation_ = orientation_180_();
|
||||
this->send64pixels(chip_line * this->num_chips_ / this->num_chip_lines_ + chip, pixels);
|
||||
if (chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE)
|
||||
this->orientation_ = orientation_180_();
|
||||
}
|
||||
this->send64pixels(i, pixels);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t MAX7219Component::orientation_180_() {
|
||||
switch (this->orientation_) {
|
||||
case 0:
|
||||
return 2;
|
||||
case 1:
|
||||
return 3;
|
||||
case 2:
|
||||
return 0;
|
||||
case 3:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int MAX7219Component::get_height_internal() {
|
||||
return 8; // TO BE DONE -> STACK TWO DISPLAYS ON TOP OF EACH OTHER
|
||||
// TO BE DONE -> CREATE Virtual size of screen and scroll
|
||||
return 8 * this->num_chip_lines_; // TO BE DONE -> CREATE Virtual size of screen and scroll
|
||||
}
|
||||
|
||||
int MAX7219Component::get_width_internal() { return this->num_chips_ * 8; }
|
||||
|
||||
size_t MAX7219Component::get_buffer_length_() { return this->num_chips_ * 8; }
|
||||
int MAX7219Component::get_width_internal() { return this->num_chips_ / this->num_chip_lines_ * 8; }
|
||||
|
||||
void HOT MAX7219Component::draw_absolute_pixel_internal(int x, int y, Color color) {
|
||||
if (x + 1 > this->max_displaybuffer_.size()) { // Extend the display buffer in case required
|
||||
this->max_displaybuffer_.resize(x + 1, this->bckgrnd_);
|
||||
if (x + 1 > (int) this->max_displaybuffer_[0].size()) { // Extend the display buffer in case required
|
||||
for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
|
||||
this->max_displaybuffer_[chip_line].resize(x + 1, this->bckgrnd_);
|
||||
}
|
||||
}
|
||||
|
||||
if ((y >= this->get_height_internal()) || (y < 0) || (x < 0)) // If pixel is outside display then dont draw
|
||||
@@ -140,9 +168,9 @@ void HOT MAX7219Component::draw_absolute_pixel_internal(int x, int y, Color colo
|
||||
uint8_t subpos = y; // Y is starting at 0 top left
|
||||
|
||||
if (color.is_on()) {
|
||||
this->max_displaybuffer_[pos] |= (1 << subpos);
|
||||
this->max_displaybuffer_[subpos / 8][pos] |= (1 << subpos % 8);
|
||||
} else {
|
||||
this->max_displaybuffer_[pos] &= ~(1 << subpos);
|
||||
this->max_displaybuffer_[subpos / 8][pos] &= ~(1 << subpos % 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,8 +186,10 @@ void MAX7219Component::send_to_all_(uint8_t a_register, uint8_t data) {
|
||||
}
|
||||
void MAX7219Component::update() {
|
||||
this->update_ = true;
|
||||
this->max_displaybuffer_.clear();
|
||||
this->max_displaybuffer_.resize(this->num_chips_ * 8, this->bckgrnd_);
|
||||
for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
|
||||
this->max_displaybuffer_[chip_line].clear();
|
||||
this->max_displaybuffer_[chip_line].resize(get_width_internal(), this->bckgrnd_);
|
||||
}
|
||||
if (this->writer_local_.has_value()) // insert Labda function if available
|
||||
(*this->writer_local_)(*this);
|
||||
}
|
||||
@@ -175,7 +205,7 @@ void MAX7219Component::turn_on_off(bool on_off) {
|
||||
}
|
||||
}
|
||||
|
||||
void MAX7219Component::scroll(bool on_off, uint8_t mode, uint16_t speed, uint16_t delay, uint16_t dwell) {
|
||||
void MAX7219Component::scroll(bool on_off, ScrollMode mode, uint16_t speed, uint16_t delay, uint16_t dwell) {
|
||||
this->set_scroll(on_off);
|
||||
this->set_scroll_mode(mode);
|
||||
this->set_scroll_speed(speed);
|
||||
@@ -183,7 +213,7 @@ void MAX7219Component::scroll(bool on_off, uint8_t mode, uint16_t speed, uint16_
|
||||
this->set_scroll_delay(delay);
|
||||
}
|
||||
|
||||
void MAX7219Component::scroll(bool on_off, uint8_t mode) {
|
||||
void MAX7219Component::scroll(bool on_off, ScrollMode mode) {
|
||||
this->set_scroll(on_off);
|
||||
this->set_scroll_mode(mode);
|
||||
}
|
||||
@@ -196,24 +226,26 @@ void MAX7219Component::intensity(uint8_t intensity) {
|
||||
void MAX7219Component::scroll(bool on_off) { this->set_scroll(on_off); }
|
||||
|
||||
void MAX7219Component::scroll_left() {
|
||||
if (this->update_) {
|
||||
this->max_displaybuffer_.push_back(this->bckgrnd_);
|
||||
for (uint16_t i = 0; i < this->stepsleft_; i++) {
|
||||
this->max_displaybuffer_.push_back(this->max_displaybuffer_.front());
|
||||
this->max_displaybuffer_.erase(this->max_displaybuffer_.begin());
|
||||
this->update_ = false;
|
||||
for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
|
||||
if (this->update_) {
|
||||
this->max_displaybuffer_[chip_line].push_back(this->bckgrnd_);
|
||||
for (uint16_t i = 0; i < this->stepsleft_; i++) {
|
||||
this->max_displaybuffer_[chip_line].push_back(this->max_displaybuffer_[chip_line].front());
|
||||
this->max_displaybuffer_[chip_line].erase(this->max_displaybuffer_[chip_line].begin());
|
||||
}
|
||||
} else {
|
||||
this->max_displaybuffer_[chip_line].push_back(this->max_displaybuffer_[chip_line].front());
|
||||
this->max_displaybuffer_[chip_line].erase(this->max_displaybuffer_[chip_line].begin());
|
||||
}
|
||||
} else {
|
||||
this->max_displaybuffer_.push_back(this->max_displaybuffer_.front());
|
||||
this->max_displaybuffer_.erase(this->max_displaybuffer_.begin());
|
||||
}
|
||||
this->update_ = false;
|
||||
this->stepsleft_++;
|
||||
}
|
||||
|
||||
void MAX7219Component::send_char(uint8_t chip, uint8_t data) {
|
||||
// get this character from PROGMEM
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
this->max_displaybuffer_[chip * 8 + i] = progmem_read_byte(&MAX7219_DOT_MATRIX_FONT[data][i]);
|
||||
this->max_displaybuffer_[0][chip * 8 + i] = progmem_read_byte(&MAX7219_DOT_MATRIX_FONT[data][i]);
|
||||
} // end of send_char
|
||||
|
||||
// send one character (data) to position (chip)
|
||||
|
||||
@@ -12,6 +12,16 @@
|
||||
namespace esphome {
|
||||
namespace max7219digit {
|
||||
|
||||
enum ChipLinesStyle {
|
||||
ZIGZAG = 0,
|
||||
SNAKE,
|
||||
};
|
||||
|
||||
enum ScrollMode {
|
||||
CONTINUOUS = 0,
|
||||
STOP,
|
||||
};
|
||||
|
||||
class MAX7219Component;
|
||||
|
||||
using max7219_writer_t = std::function<void(MAX7219Component &)>;
|
||||
@@ -46,20 +56,22 @@ class MAX7219Component : public PollingComponent,
|
||||
|
||||
void set_intensity(uint8_t intensity) { this->intensity_ = intensity; };
|
||||
void set_num_chips(uint8_t num_chips) { this->num_chips_ = num_chips; };
|
||||
void set_num_chip_lines(uint8_t num_chip_lines) { this->num_chip_lines_ = num_chip_lines; };
|
||||
void set_chip_lines_style(ChipLinesStyle chip_lines_style) { this->chip_lines_style_ = chip_lines_style; };
|
||||
void set_chip_orientation(uint8_t rotate) { this->orientation_ = rotate; };
|
||||
void set_scroll_speed(uint16_t speed) { this->scroll_speed_ = speed; };
|
||||
void set_scroll_dwell(uint16_t dwell) { this->scroll_dwell_ = dwell; };
|
||||
void set_scroll_delay(uint16_t delay) { this->scroll_delay_ = delay; };
|
||||
void set_scroll(bool on_off) { this->scroll_ = on_off; };
|
||||
void set_scroll_mode(uint8_t mode) { this->scroll_mode_ = mode; };
|
||||
void set_scroll_mode(ScrollMode mode) { this->scroll_mode_ = mode; };
|
||||
void set_reverse(bool on_off) { this->reverse_ = on_off; };
|
||||
|
||||
void send_char(uint8_t chip, uint8_t data);
|
||||
void send64pixels(uint8_t chip, const uint8_t pixels[8]);
|
||||
|
||||
void scroll_left();
|
||||
void scroll(bool on_off, uint8_t mode, uint16_t speed, uint16_t delay, uint16_t dwell);
|
||||
void scroll(bool on_off, uint8_t mode);
|
||||
void scroll(bool on_off, ScrollMode mode, uint16_t speed, uint16_t delay, uint16_t dwell);
|
||||
void scroll(bool on_off, ScrollMode mode);
|
||||
void scroll(bool on_off);
|
||||
void intensity(uint8_t intensity);
|
||||
|
||||
@@ -84,9 +96,12 @@ class MAX7219Component : public PollingComponent,
|
||||
protected:
|
||||
void send_byte_(uint8_t a_register, uint8_t data);
|
||||
void send_to_all_(uint8_t a_register, uint8_t data);
|
||||
uint8_t orientation_180_();
|
||||
|
||||
uint8_t intensity_; /// Intensity of the display from 0 to 15 (most)
|
||||
uint8_t num_chips_;
|
||||
uint8_t num_chip_lines_;
|
||||
ChipLinesStyle chip_lines_style_;
|
||||
bool scroll_;
|
||||
bool reverse_;
|
||||
bool update_{false};
|
||||
@@ -94,11 +109,11 @@ class MAX7219Component : public PollingComponent,
|
||||
uint16_t scroll_delay_;
|
||||
uint16_t scroll_dwell_;
|
||||
uint16_t old_buffer_size_ = 0;
|
||||
uint8_t scroll_mode_;
|
||||
ScrollMode scroll_mode_;
|
||||
bool invert_ = false;
|
||||
uint8_t orientation_;
|
||||
uint8_t bckgrnd_ = 0x0;
|
||||
std::vector<uint8_t> max_displaybuffer_;
|
||||
std::vector<std::vector<uint8_t>> max_displaybuffer_;
|
||||
uint32_t last_scroll_ = 0;
|
||||
uint16_t stepsleft_;
|
||||
size_t get_buffer_length_();
|
||||
|
||||
@@ -35,7 +35,6 @@ void MCP23S08::dump_config() {
|
||||
}
|
||||
|
||||
bool MCP23S08::read_reg(uint8_t reg, uint8_t *value) {
|
||||
uint8_t data;
|
||||
this->enable();
|
||||
this->transfer_byte(this->device_opcode_ | 1);
|
||||
this->transfer_byte(reg);
|
||||
|
||||
@@ -24,6 +24,7 @@ void MCP23X17Base::pin_mode(uint8_t pin, gpio::Flags flags) {
|
||||
uint8_t gppu = pin < 8 ? mcp23x17_base::MCP23X17_GPPUA : mcp23x17_base::MCP23X17_GPPUB;
|
||||
if (flags == gpio::FLAG_INPUT) {
|
||||
this->update_reg(pin, true, iodir);
|
||||
this->update_reg(pin, false, gppu);
|
||||
} else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
|
||||
this->update_reg(pin, true, iodir);
|
||||
this->update_reg(pin, true, gppu);
|
||||
|
||||
@@ -127,9 +127,6 @@ canbus::Error MCP2515::set_mode_(const CanctrlReqopMode mode) {
|
||||
}
|
||||
|
||||
canbus::Error MCP2515::set_clk_out_(const CanClkOut divisor) {
|
||||
canbus::Error res;
|
||||
uint8_t cfg3;
|
||||
|
||||
if (divisor == CLKOUT_DISABLE) {
|
||||
/* Turn off CLKEN */
|
||||
modify_register_(MCP_CANCTRL, CANCTRL_CLKEN, 0x00);
|
||||
|
||||
@@ -23,7 +23,7 @@ void MD5Digest::get_hex(char *output) {
|
||||
}
|
||||
}
|
||||
|
||||
bool MD5Digest::equals_bytes(const char *expected) {
|
||||
bool MD5Digest::equals_bytes(const uint8_t *expected) {
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
if (expected[i] != this->digest_[i]) {
|
||||
return false;
|
||||
@@ -33,18 +33,10 @@ bool MD5Digest::equals_bytes(const char *expected) {
|
||||
}
|
||||
|
||||
bool MD5Digest::equals_hex(const char *expected) {
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
auto high = parse_hex(expected[i * 2]);
|
||||
auto low = parse_hex(expected[i * 2 + 1]);
|
||||
if (!high.has_value() || !low.has_value()) {
|
||||
return false;
|
||||
}
|
||||
auto value = (*high << 4) | *low;
|
||||
if (value != this->digest_[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
uint8_t parsed[16];
|
||||
if (!parse_hex(expected, parsed, 16))
|
||||
return false;
|
||||
return equals_bytes(parsed);
|
||||
}
|
||||
|
||||
} // namespace md5
|
||||
|
||||
@@ -44,7 +44,7 @@ class MD5Digest {
|
||||
void get_hex(char *output);
|
||||
|
||||
/// Compare the digest against a provided byte-encoded digest (16 bytes).
|
||||
bool equals_bytes(const char *expected);
|
||||
bool equals_bytes(const uint8_t *expected);
|
||||
|
||||
/// Compare the digest against a provided hex-encoded digest (32 bytes).
|
||||
bool equals_hex(const char *expected);
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
#include "adapter.h"
|
||||
#include "ac_adapter.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
namespace ac {
|
||||
|
||||
const char *const Constants::TAG = "midea";
|
||||
const std::string Constants::FREEZE_PROTECTION = "freeze protection";
|
||||
@@ -171,6 +172,7 @@ void Converters::to_climate_traits(ClimateTraits &traits, const dudanov::midea::
|
||||
traits.add_supported_custom_preset(Constants::FREEZE_PROTECTION);
|
||||
}
|
||||
|
||||
} // namespace ac
|
||||
} // namespace midea
|
||||
} // namespace esphome
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
// MideaUART
|
||||
#include <Appliance/AirConditioner/AirConditioner.h>
|
||||
|
||||
#include "esphome/components/climate/climate_traits.h"
|
||||
#include "appliance_base.h"
|
||||
#include "air_conditioner.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
namespace ac {
|
||||
|
||||
using MideaMode = dudanov::midea::ac::Mode;
|
||||
using MideaSwingMode = dudanov::midea::ac::SwingMode;
|
||||
@@ -41,6 +44,7 @@ class Converters {
|
||||
static void to_climate_traits(ClimateTraits &traits, const dudanov::midea::ac::Capabilities &capabilities);
|
||||
};
|
||||
|
||||
} // namespace ac
|
||||
} // namespace midea
|
||||
} // namespace esphome
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
namespace ac {
|
||||
|
||||
template<typename... Ts> class MideaActionBase : public Action<Ts...> {
|
||||
public:
|
||||
@@ -55,6 +56,7 @@ template<typename... Ts> class PowerOffAction : public MideaActionBase<Ts...> {
|
||||
void play(Ts... x) override { this->parent_->do_power_off(); }
|
||||
};
|
||||
|
||||
} // namespace ac
|
||||
} // namespace midea
|
||||
} // namespace esphome
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
#include "air_conditioner.h"
|
||||
#include "adapter.h"
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
#include "midea_ir.h"
|
||||
#endif
|
||||
#include "ac_adapter.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
namespace ac {
|
||||
|
||||
static void set_sensor(Sensor *sensor, float value) {
|
||||
if (sensor != nullptr && (!sensor->has_state() || sensor->get_raw_state() != value))
|
||||
@@ -122,7 +120,7 @@ void AirConditioner::dump_config() {
|
||||
void AirConditioner::do_follow_me(float temperature, bool beeper) {
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
IrFollowMeData data(static_cast<uint8_t>(lroundf(temperature)), beeper);
|
||||
this->transmit_ir(data);
|
||||
this->transmitter_.transmit(data);
|
||||
#else
|
||||
ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
|
||||
#endif
|
||||
@@ -131,7 +129,7 @@ void AirConditioner::do_follow_me(float temperature, bool beeper) {
|
||||
void AirConditioner::do_swing_step() {
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
IrSpecialData data(0x01);
|
||||
this->transmit_ir(data);
|
||||
this->transmitter_.transmit(data);
|
||||
#else
|
||||
ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
|
||||
#endif
|
||||
@@ -143,13 +141,14 @@ void AirConditioner::do_display_toggle() {
|
||||
} else {
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
IrSpecialData data(0x08);
|
||||
this->transmit_ir(data);
|
||||
this->transmitter_.transmit(data);
|
||||
#else
|
||||
ESP_LOGW(Constants::TAG, "Action needs remote_transmitter component");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ac
|
||||
} // namespace midea
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -2,17 +2,25 @@
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
// MideaUART
|
||||
#include <Appliance/AirConditioner/AirConditioner.h>
|
||||
|
||||
#include "appliance_base.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
namespace ac {
|
||||
|
||||
using sensor::Sensor;
|
||||
using climate::ClimateCall;
|
||||
using climate::ClimatePreset;
|
||||
using climate::ClimateTraits;
|
||||
using climate::ClimateMode;
|
||||
using climate::ClimateSwingMode;
|
||||
using climate::ClimateFanMode;
|
||||
|
||||
class AirConditioner : public ApplianceBase<dudanov::midea::ac::AirConditioner> {
|
||||
class AirConditioner : public ApplianceBase<dudanov::midea::ac::AirConditioner>, public climate::Climate {
|
||||
public:
|
||||
void dump_config() override;
|
||||
void set_outdoor_temperature_sensor(Sensor *sensor) { this->outdoor_sensor_ = sensor; }
|
||||
@@ -31,15 +39,26 @@ class AirConditioner : public ApplianceBase<dudanov::midea::ac::AirConditioner>
|
||||
void do_beeper_off() { this->set_beeper_feedback(false); }
|
||||
void do_power_on() { this->base_.setPowerState(true); }
|
||||
void do_power_off() { this->base_.setPowerState(false); }
|
||||
void set_supported_modes(const std::set<ClimateMode> &modes) { this->supported_modes_ = modes; }
|
||||
void set_supported_swing_modes(const std::set<ClimateSwingMode> &modes) { this->supported_swing_modes_ = modes; }
|
||||
void set_supported_presets(const std::set<ClimatePreset> &presets) { this->supported_presets_ = presets; }
|
||||
void set_custom_presets(const std::set<std::string> &presets) { this->supported_custom_presets_ = presets; }
|
||||
void set_custom_fan_modes(const std::set<std::string> &modes) { this->supported_custom_fan_modes_ = modes; }
|
||||
|
||||
protected:
|
||||
void control(const ClimateCall &call) override;
|
||||
ClimateTraits traits() override;
|
||||
std::set<ClimateMode> supported_modes_{};
|
||||
std::set<ClimateSwingMode> supported_swing_modes_{};
|
||||
std::set<ClimatePreset> supported_presets_{};
|
||||
std::set<std::string> supported_custom_presets_{};
|
||||
std::set<std::string> supported_custom_fan_modes_{};
|
||||
Sensor *outdoor_sensor_{nullptr};
|
||||
Sensor *humidity_sensor_{nullptr};
|
||||
Sensor *power_sensor_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace ac
|
||||
} // namespace midea
|
||||
} // namespace esphome
|
||||
|
||||
|
||||
@@ -2,84 +2,97 @@
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
|
||||
// MideaUART
|
||||
#include <Appliance/ApplianceBase.h>
|
||||
#include <Helpers/Logger.h>
|
||||
|
||||
// Include global defines
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/components/climate/climate.h"
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
#include "esphome/components/remote_base/midea_protocol.h"
|
||||
#include "esphome/components/remote_transmitter/remote_transmitter.h"
|
||||
#endif
|
||||
#include <Appliance/ApplianceBase.h>
|
||||
#include <Helpers/Logger.h>
|
||||
#include "ir_transmitter.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
|
||||
using climate::ClimatePreset;
|
||||
using climate::ClimateTraits;
|
||||
using climate::ClimateMode;
|
||||
using climate::ClimateSwingMode;
|
||||
using climate::ClimateFanMode;
|
||||
/* Stream from UART component */
|
||||
class UARTStream : public Stream {
|
||||
public:
|
||||
void set_uart(uart::UARTComponent *uart) { this->uart_ = uart; }
|
||||
|
||||
template<typename T>
|
||||
class ApplianceBase : public Component, public uart::UARTDevice, public climate::Climate, public Stream {
|
||||
/* Stream interface implementation */
|
||||
|
||||
int available() override { return this->uart_->available(); }
|
||||
int read() override {
|
||||
uint8_t data;
|
||||
this->uart_->read_byte(&data);
|
||||
return data;
|
||||
}
|
||||
int peek() override {
|
||||
uint8_t data;
|
||||
this->uart_->peek_byte(&data);
|
||||
return data;
|
||||
}
|
||||
size_t write(uint8_t data) override {
|
||||
this->uart_->write_byte(data);
|
||||
return 1;
|
||||
}
|
||||
size_t write(const uint8_t *data, size_t size) override {
|
||||
this->uart_->write_array(data, size);
|
||||
return size;
|
||||
}
|
||||
void flush() override { this->uart_->flush(); }
|
||||
|
||||
protected:
|
||||
uart::UARTComponent *uart_;
|
||||
};
|
||||
|
||||
template<typename T> class ApplianceBase : public Component {
|
||||
static_assert(std::is_base_of<dudanov::midea::ApplianceBase, T>::value,
|
||||
"T must derive from dudanov::midea::ApplianceBase class");
|
||||
|
||||
public:
|
||||
ApplianceBase() {
|
||||
this->base_.setStream(this);
|
||||
this->base_.setStream(&this->stream_);
|
||||
this->base_.addOnStateCallback(std::bind(&ApplianceBase::on_status_change, this));
|
||||
dudanov::midea::ApplianceBase::setLogger(
|
||||
[](int level, const char *tag, int line, const String &format, va_list args) {
|
||||
esp_log_vprintf_(level, tag, line, format.c_str(), args);
|
||||
});
|
||||
}
|
||||
bool can_proceed() override {
|
||||
return this->base_.getAutoconfStatus() != dudanov::midea::AutoconfStatus::AUTOCONF_PROGRESS;
|
||||
}
|
||||
float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; }
|
||||
void setup() override { this->base_.setup(); }
|
||||
void loop() override { this->base_.loop(); }
|
||||
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_.set_transmitter(transmitter); }
|
||||
#endif
|
||||
|
||||
/* UART communication */
|
||||
|
||||
void set_uart_parent(uart::UARTComponent *parent) { this->stream_.set_uart(parent); }
|
||||
void set_period(uint32_t ms) { this->base_.setPeriod(ms); }
|
||||
void set_response_timeout(uint32_t ms) { this->base_.setTimeout(ms); }
|
||||
void set_request_attempts(uint32_t attempts) { this->base_.setNumAttempts(attempts); }
|
||||
|
||||
/* Component methods */
|
||||
|
||||
void setup() override { this->base_.setup(); }
|
||||
void loop() override { this->base_.loop(); }
|
||||
float get_setup_priority() const override { return setup_priority::BEFORE_CONNECTION; }
|
||||
bool can_proceed() override {
|
||||
return this->base_.getAutoconfStatus() != dudanov::midea::AutoconfStatus::AUTOCONF_PROGRESS;
|
||||
}
|
||||
|
||||
void set_beeper_feedback(bool state) { this->base_.setBeeper(state); }
|
||||
void set_autoconf(bool value) { this->base_.setAutoconf(value); }
|
||||
void set_supported_modes(const std::set<ClimateMode> &modes) { this->supported_modes_ = modes; }
|
||||
void set_supported_swing_modes(const std::set<ClimateSwingMode> &modes) { this->supported_swing_modes_ = modes; }
|
||||
void set_supported_presets(const std::set<ClimatePreset> &presets) { this->supported_presets_ = presets; }
|
||||
void set_custom_presets(const std::set<std::string> &presets) { this->supported_custom_presets_ = presets; }
|
||||
void set_custom_fan_modes(const std::set<std::string> &modes) { this->supported_custom_fan_modes_ = modes; }
|
||||
virtual void on_status_change() = 0;
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
void set_transmitter(remote_transmitter::RemoteTransmitterComponent *transmitter) {
|
||||
this->transmitter_ = transmitter;
|
||||
}
|
||||
void transmit_ir(remote_base::MideaData &data) {
|
||||
data.finalize();
|
||||
auto transmit = this->transmitter_->transmit();
|
||||
remote_base::MideaProtocol().encode(transmit.get_data(), data);
|
||||
transmit.perform();
|
||||
}
|
||||
#endif
|
||||
|
||||
int available() override { return uart::UARTDevice::available(); }
|
||||
int read() override { return uart::UARTDevice::read(); }
|
||||
int peek() override { return uart::UARTDevice::peek(); }
|
||||
void flush() override { uart::UARTDevice::flush(); }
|
||||
size_t write(uint8_t data) override { return uart::UARTDevice::write(data); }
|
||||
|
||||
protected:
|
||||
T base_;
|
||||
std::set<ClimateMode> supported_modes_{};
|
||||
std::set<ClimateSwingMode> supported_swing_modes_{};
|
||||
std::set<ClimatePreset> supported_presets_{};
|
||||
std::set<std::string> supported_custom_presets_{};
|
||||
std::set<std::string> supported_custom_fan_modes_{};
|
||||
UARTStream stream_;
|
||||
#ifdef USE_REMOTE_TRANSMITTER
|
||||
remote_transmitter::RemoteTransmitterComponent *transmitter_{nullptr};
|
||||
IrTransmitter transmitter_;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -40,9 +40,9 @@ AUTO_LOAD = ["sensor"]
|
||||
CONF_OUTDOOR_TEMPERATURE = "outdoor_temperature"
|
||||
CONF_POWER_USAGE = "power_usage"
|
||||
CONF_HUMIDITY_SETPOINT = "humidity_setpoint"
|
||||
midea_ns = cg.esphome_ns.namespace("midea")
|
||||
AirConditioner = midea_ns.class_("AirConditioner", climate.Climate, cg.Component)
|
||||
Capabilities = midea_ns.namespace("Constants")
|
||||
midea_ac_ns = cg.esphome_ns.namespace("midea").namespace("ac")
|
||||
AirConditioner = midea_ac_ns.class_("AirConditioner", climate.Climate, cg.Component)
|
||||
Capabilities = midea_ac_ns.namespace("Constants")
|
||||
|
||||
|
||||
def templatize(value):
|
||||
@@ -156,13 +156,13 @@ CONFIG_SCHEMA = cv.All(
|
||||
)
|
||||
|
||||
# Actions
|
||||
FollowMeAction = midea_ns.class_("FollowMeAction", automation.Action)
|
||||
DisplayToggleAction = midea_ns.class_("DisplayToggleAction", automation.Action)
|
||||
SwingStepAction = midea_ns.class_("SwingStepAction", automation.Action)
|
||||
BeeperOnAction = midea_ns.class_("BeeperOnAction", automation.Action)
|
||||
BeeperOffAction = midea_ns.class_("BeeperOffAction", automation.Action)
|
||||
PowerOnAction = midea_ns.class_("PowerOnAction", automation.Action)
|
||||
PowerOffAction = midea_ns.class_("PowerOffAction", automation.Action)
|
||||
FollowMeAction = midea_ac_ns.class_("FollowMeAction", automation.Action)
|
||||
DisplayToggleAction = midea_ac_ns.class_("DisplayToggleAction", automation.Action)
|
||||
SwingStepAction = midea_ac_ns.class_("SwingStepAction", automation.Action)
|
||||
BeeperOnAction = midea_ac_ns.class_("BeeperOnAction", automation.Action)
|
||||
BeeperOffAction = midea_ac_ns.class_("BeeperOffAction", automation.Action)
|
||||
PowerOnAction = midea_ac_ns.class_("PowerOnAction", automation.Action)
|
||||
PowerOffAction = midea_ac_ns.class_("PowerOffAction", automation.Action)
|
||||
|
||||
MIDEA_ACTION_BASE_SCHEMA = cv.Schema(
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
namespace esphome {
|
||||
namespace midea {
|
||||
|
||||
using remote_base::RemoteTransmitterBase;
|
||||
using IrData = remote_base::MideaData;
|
||||
|
||||
class IrFollowMeData : public IrData {
|
||||
@@ -38,6 +39,20 @@ class IrSpecialData : public IrData {
|
||||
IrSpecialData(uint8_t code) : IrData({MIDEA_TYPE_SPECIAL, code, 0xFF, 0xFF, 0xFF}) {}
|
||||
};
|
||||
|
||||
class IrTransmitter {
|
||||
public:
|
||||
void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_ = transmitter; }
|
||||
void transmit(IrData &data) {
|
||||
data.finalize();
|
||||
auto transmit = this->transmitter_->transmit();
|
||||
remote_base::MideaProtocol().encode(transmit.get_data(), data);
|
||||
transmit.perform();
|
||||
}
|
||||
|
||||
protected:
|
||||
RemoteTransmitterBase *transmitter_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace midea
|
||||
} // namespace esphome
|
||||
|
||||
@@ -139,7 +139,9 @@ void Modbus::send(uint8_t address, uint8_t function_code, uint16_t start_address
|
||||
uint8_t payload_len, const uint8_t *payload) {
|
||||
static const size_t MAX_VALUES = 128;
|
||||
|
||||
if (number_of_entities > MAX_VALUES) {
|
||||
// Only check max number of registers for standard function codes
|
||||
// Some devices use non standard codes like 0x43
|
||||
if (number_of_entities > MAX_VALUES && function_code <= 0x10) {
|
||||
ESP_LOGE(TAG, "send too many values %d max=%zu", number_of_entities, MAX_VALUES);
|
||||
return;
|
||||
}
|
||||
@@ -179,7 +181,7 @@ void Modbus::send(uint8_t address, uint8_t function_code, uint16_t start_address
|
||||
this->flow_control_pin_->digital_write(false);
|
||||
waiting_for_response = address;
|
||||
last_send_ = millis();
|
||||
ESP_LOGV(TAG, "Modbus write: %s", hexencode(data).c_str());
|
||||
ESP_LOGV(TAG, "Modbus write: %s", format_hex_pretty(data).c_str());
|
||||
}
|
||||
|
||||
// Helper function for lambdas
|
||||
@@ -200,7 +202,7 @@ void Modbus::send_raw(const std::vector<uint8_t> &payload) {
|
||||
if (this->flow_control_pin_ != nullptr)
|
||||
this->flow_control_pin_->digital_write(false);
|
||||
waiting_for_response = payload[0];
|
||||
ESP_LOGV(TAG, "Modbus write raw: %s", hexencode(payload).c_str());
|
||||
ESP_LOGV(TAG, "Modbus write raw: %s", format_hex_pretty(payload).c_str());
|
||||
last_send_ = millis();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
import binascii
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import modbus
|
||||
from esphome.const import CONF_ID, CONF_ADDRESS
|
||||
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_NAME, CONF_LAMBDA, CONF_OFFSET
|
||||
from esphome.cpp_helpers import logging
|
||||
from .const import (
|
||||
CONF_BITMASK,
|
||||
CONF_BYTE_OFFSET,
|
||||
CONF_COMMAND_THROTTLE,
|
||||
CONF_CUSTOM_COMMAND,
|
||||
CONF_FORCE_NEW_RANGE,
|
||||
CONF_MODBUS_CONTROLLER_ID,
|
||||
CONF_REGISTER_COUNT,
|
||||
CONF_REGISTER_TYPE,
|
||||
CONF_RESPONSE_SIZE,
|
||||
CONF_SKIP_UPDATES,
|
||||
CONF_VALUE_TYPE,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@martgras"]
|
||||
@@ -37,6 +48,7 @@ MODBUS_FUNCTION_CODE = {
|
||||
ModbusRegisterType_ns = modbus_controller_ns.namespace("ModbusRegisterType")
|
||||
ModbusRegisterType = ModbusRegisterType_ns.enum("ModbusRegisterType")
|
||||
MODBUS_REGISTER_TYPE = {
|
||||
"custom": ModbusRegisterType.CUSTOM,
|
||||
"coil": ModbusRegisterType.COIL,
|
||||
"discrete_input": ModbusRegisterType.DISCRETE_INPUT,
|
||||
"holding": ModbusRegisterType.HOLDING,
|
||||
@@ -61,6 +73,21 @@ SENSOR_VALUE_TYPE = {
|
||||
"FP32_R": SensorValueType.FP32_R,
|
||||
}
|
||||
|
||||
TYPE_REGISTER_MAP = {
|
||||
"RAW": 1,
|
||||
"U_WORD": 1,
|
||||
"S_WORD": 1,
|
||||
"U_DWORD": 2,
|
||||
"U_DWORD_R": 2,
|
||||
"S_DWORD": 2,
|
||||
"S_DWORD_R": 2,
|
||||
"U_QWORD": 4,
|
||||
"U_QWORDU_R": 4,
|
||||
"S_QWORD": 4,
|
||||
"U_QWORD_R": 4,
|
||||
"FP32": 2,
|
||||
"FP32_R": 2,
|
||||
}
|
||||
|
||||
MULTI_CONF = True
|
||||
|
||||
@@ -80,6 +107,100 @@ CONFIG_SCHEMA = cv.All(
|
||||
)
|
||||
|
||||
|
||||
ModbusItemBaseSchema = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
|
||||
cv.Optional(CONF_ADDRESS): cv.positive_int,
|
||||
cv.Optional(CONF_CUSTOM_COMMAND): cv.ensure_list(cv.hex_uint8_t),
|
||||
cv.Exclusive(
|
||||
CONF_OFFSET,
|
||||
"offset",
|
||||
f"{CONF_OFFSET} and {CONF_BYTE_OFFSET} can't be used together",
|
||||
): cv.positive_int,
|
||||
cv.Exclusive(
|
||||
CONF_BYTE_OFFSET,
|
||||
"offset",
|
||||
f"{CONF_OFFSET} and {CONF_BYTE_OFFSET} can't be used together",
|
||||
): cv.positive_int,
|
||||
cv.Optional(CONF_BITMASK, default=0xFFFFFFFF): cv.hex_uint32_t,
|
||||
cv.Optional(CONF_SKIP_UPDATES, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
cv.Optional(CONF_RESPONSE_SIZE, default=0): cv.positive_int,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def validate_modbus_register(config):
|
||||
if CONF_CUSTOM_COMMAND not in config and CONF_ADDRESS not in config:
|
||||
raise cv.Invalid(
|
||||
f" {CONF_ADDRESS} is a required property if '{CONF_CUSTOM_COMMAND}:' isn't used"
|
||||
)
|
||||
if CONF_CUSTOM_COMMAND in config and CONF_REGISTER_TYPE in config:
|
||||
raise cv.Invalid(
|
||||
f"can't use '{CONF_REGISTER_TYPE}:' together with '{CONF_CUSTOM_COMMAND}:'",
|
||||
)
|
||||
|
||||
if CONF_CUSTOM_COMMAND not in config and CONF_REGISTER_TYPE not in config:
|
||||
raise cv.Invalid(
|
||||
f" {CONF_REGISTER_TYPE} is a required property if '{CONF_CUSTOM_COMMAND}:' isn't used"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def modbus_calc_properties(config):
|
||||
byte_offset = 0
|
||||
reg_count = 0
|
||||
if CONF_OFFSET in config:
|
||||
byte_offset = config[CONF_OFFSET]
|
||||
# A CONF_BYTE_OFFSET setting overrides CONF_OFFSET
|
||||
if CONF_BYTE_OFFSET in config:
|
||||
byte_offset = config[CONF_BYTE_OFFSET]
|
||||
if CONF_REGISTER_COUNT in config:
|
||||
reg_count = config[CONF_REGISTER_COUNT]
|
||||
if CONF_VALUE_TYPE in config:
|
||||
value_type = config[CONF_VALUE_TYPE]
|
||||
if reg_count == 0:
|
||||
reg_count = TYPE_REGISTER_MAP[value_type]
|
||||
if CONF_CUSTOM_COMMAND in config:
|
||||
if CONF_ADDRESS not in config:
|
||||
# generate a unique modbus address using the hash of the name
|
||||
# CONF_NAME set even if only CONF_ID is used.
|
||||
# a modbus register address is required to add the item to sensormap
|
||||
value = config[CONF_NAME]
|
||||
if isinstance(value, str):
|
||||
value = value.encode()
|
||||
config[CONF_ADDRESS] = binascii.crc_hqx(value, 0)
|
||||
config[CONF_REGISTER_TYPE] = ModbusRegisterType.CUSTOM
|
||||
config[CONF_FORCE_NEW_RANGE] = True
|
||||
return byte_offset, reg_count
|
||||
|
||||
|
||||
async def add_modbus_base_properties(
|
||||
var, config, sensor_type, lamdba_param_type=cg.float_, lamdba_return_type=float
|
||||
):
|
||||
if CONF_CUSTOM_COMMAND in config:
|
||||
cg.add(var.set_custom_data(config[CONF_CUSTOM_COMMAND]))
|
||||
|
||||
if config[CONF_RESPONSE_SIZE] > 0:
|
||||
cg.add(var.set_register_size(config[CONF_RESPONSE_SIZE]))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA],
|
||||
[
|
||||
(sensor_type.operator("ptr"), "item"),
|
||||
(lamdba_param_type, "x"),
|
||||
(
|
||||
cg.std_vector.template(cg.uint8).operator("const").operator("ref"),
|
||||
"data",
|
||||
),
|
||||
],
|
||||
return_type=cg.optional.template(lamdba_return_type),
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_COMMAND_THROTTLE])
|
||||
cg.add(var.set_command_throttle(config[CONF_COMMAND_THROTTLE]))
|
||||
@@ -104,11 +225,3 @@ def function_code_to_register(function_code):
|
||||
"write_multiple_registers": ModbusRegisterType.HOLDING,
|
||||
}
|
||||
return FUNCTION_CODE_TYPE_MAP[function_code]
|
||||
|
||||
|
||||
def find_by_value(dict, find_value):
|
||||
for (key, value) in MODBUS_REGISTER_TYPE.items():
|
||||
print(find_value, value)
|
||||
if find_value == value:
|
||||
return key
|
||||
return "not found"
|
||||
|
||||
@@ -2,16 +2,18 @@ from esphome.components import binary_sensor
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
|
||||
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_LAMBDA, CONF_OFFSET
|
||||
from esphome.const import CONF_ADDRESS, CONF_ID
|
||||
from .. import (
|
||||
SensorItem,
|
||||
add_modbus_base_properties,
|
||||
modbus_controller_ns,
|
||||
ModbusController,
|
||||
modbus_calc_properties,
|
||||
validate_modbus_register,
|
||||
ModbusItemBaseSchema,
|
||||
SensorItem,
|
||||
MODBUS_REGISTER_TYPE,
|
||||
)
|
||||
from ..const import (
|
||||
CONF_BITMASK,
|
||||
CONF_BYTE_OFFSET,
|
||||
CONF_FORCE_NEW_RANGE,
|
||||
CONF_MODBUS_CONTROLLER_ID,
|
||||
CONF_REGISTER_TYPE,
|
||||
@@ -27,30 +29,20 @@ ModbusBinarySensor = modbus_controller_ns.class_(
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
binary_sensor.BINARY_SENSOR_SCHEMA.extend(
|
||||
binary_sensor.BINARY_SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(ModbusItemBaseSchema)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ModbusBinarySensor),
|
||||
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
|
||||
cv.Required(CONF_ADDRESS): cv.positive_int,
|
||||
cv.Required(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE),
|
||||
cv.Optional(CONF_OFFSET, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_BYTE_OFFSET): cv.positive_int,
|
||||
cv.Optional(CONF_BITMASK, default=0x1): cv.hex_uint32_t,
|
||||
cv.Optional(CONF_SKIP_UPDATES, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
),
|
||||
validate_modbus_register,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
byte_offset = 0
|
||||
if CONF_OFFSET in config:
|
||||
byte_offset = config[CONF_OFFSET]
|
||||
# A CONF_BYTE_OFFSET setting overrides CONF_OFFSET
|
||||
if CONF_BYTE_OFFSET in config:
|
||||
byte_offset = config[CONF_BYTE_OFFSET]
|
||||
byte_offset, _ = modbus_calc_properties(config)
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID],
|
||||
config[CONF_REGISTER_TYPE],
|
||||
@@ -65,17 +57,4 @@ async def to_code(config):
|
||||
|
||||
paren = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
|
||||
cg.add(paren.add_sensor_item(var))
|
||||
if CONF_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA],
|
||||
[
|
||||
(ModbusBinarySensor.operator("ptr"), "item"),
|
||||
(cg.float_, "x"),
|
||||
(
|
||||
cg.std_vector.template(cg.uint8).operator("const").operator("ref"),
|
||||
"data",
|
||||
),
|
||||
],
|
||||
return_type=cg.optional.template(bool),
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
await add_modbus_base_properties(var, config, ModbusBinarySensor, cg.float_, bool)
|
||||
|
||||
@@ -13,8 +13,6 @@ void ModbusBinarySensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
|
||||
switch (this->register_type) {
|
||||
case ModbusRegisterType::DISCRETE_INPUT:
|
||||
value = coil_from_vector(this->offset, data);
|
||||
break;
|
||||
case ModbusRegisterType::COIL:
|
||||
// offset for coil is the actual number of the coil not the byte offset
|
||||
value = coil_from_vector(this->offset, data);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
CONF_BITMASK = "bitmask"
|
||||
CONF_BYTE_OFFSET = "byte_offset"
|
||||
CONF_COMMAND_THROTTLE = "command_throttle"
|
||||
CONF_CUSTOM_COMMAND = "custom_command"
|
||||
CONF_FORCE_NEW_RANGE = "force_new_range"
|
||||
CONF_MODBUS_CONTROLLER_ID = "modbus_controller_id"
|
||||
CONF_MODBUS_FUNCTIONCODE = "modbus_functioncode"
|
||||
@@ -9,5 +10,6 @@ CONF_REGISTER_COUNT = "register_count"
|
||||
CONF_REGISTER_TYPE = "register_type"
|
||||
CONF_RESPONSE_SIZE = "response_size"
|
||||
CONF_SKIP_UPDATES = "skip_updates"
|
||||
CONF_USE_WRITE_MULTIPLE = "use_write_multiple"
|
||||
CONF_VALUE_TYPE = "value_type"
|
||||
CONF_WRITE_LAMBDA = "write_lambda"
|
||||
|
||||
@@ -28,7 +28,10 @@ bool ModbusController::send_next_command_() {
|
||||
command->register_address, command->register_count);
|
||||
command->send();
|
||||
this->last_command_timestamp_ = millis();
|
||||
if (!command->on_data_func) { // No handler remove from queue directly after sending
|
||||
// remove from queue if no handler is defined or command was sent too often
|
||||
if (!command->on_data_func || command->send_countdown < 1) {
|
||||
ESP_LOGD(TAG, "Modbus command to device=%d register=0x%02X countdown=%d removed from queue after send",
|
||||
this->address_, command->register_address, command->send_countdown);
|
||||
command_queue_.pop_front();
|
||||
}
|
||||
}
|
||||
@@ -69,24 +72,30 @@ void ModbusController::on_modbus_error(uint8_t function_code, uint8_t exception_
|
||||
}
|
||||
}
|
||||
|
||||
void ModbusController::on_register_data(ModbusRegisterType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "data for register address : 0x%X : ", start_address);
|
||||
|
||||
std::map<uint64_t, SensorItem *>::iterator ModbusController::find_register_(ModbusRegisterType register_type,
|
||||
uint16_t start_address) {
|
||||
auto vec_it = find_if(begin(register_ranges_), end(register_ranges_), [=](RegisterRange const &r) {
|
||||
return (r.start_address == start_address && r.register_type == register_type);
|
||||
});
|
||||
|
||||
if (vec_it == register_ranges_.end()) {
|
||||
ESP_LOGE(TAG, "Handle incoming data : No matching range for sensor found - start_address : 0x%X", start_address);
|
||||
return;
|
||||
}
|
||||
auto map_it = sensormap_.find(vec_it->first_sensorkey);
|
||||
if (map_it == sensormap_.end()) {
|
||||
ESP_LOGE(TAG, "Handle incoming data : No sensor found in at start_address : 0x%X (0x%llX)", start_address,
|
||||
vec_it->first_sensorkey);
|
||||
return;
|
||||
ESP_LOGE(TAG, "No matching range for sensor found - start_address : 0x%X", start_address);
|
||||
} else {
|
||||
auto map_it = sensormap_.find(vec_it->first_sensorkey);
|
||||
if (map_it == sensormap_.end()) {
|
||||
ESP_LOGE(TAG, "No sensor found in at start_address : 0x%X (0x%llX)", start_address, vec_it->first_sensorkey);
|
||||
} else {
|
||||
return sensormap_.find(vec_it->first_sensorkey);
|
||||
}
|
||||
}
|
||||
// not found
|
||||
return std::end(sensormap_);
|
||||
}
|
||||
void ModbusController::on_register_data(ModbusRegisterType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "data for register address : 0x%X : ", start_address);
|
||||
|
||||
auto map_it = find_register_(register_type, start_address);
|
||||
// loop through all sensors with the same start address
|
||||
while (map_it != sensormap_.end() && map_it->second->start_address == start_address) {
|
||||
if (map_it->second->register_type == register_type) {
|
||||
@@ -116,9 +125,23 @@ void ModbusController::update_range_(RegisterRange &r) {
|
||||
ESP_LOGV(TAG, "Range : %X Size: %x (%d) skip: %d", r.start_address, r.register_count, (int) r.register_type,
|
||||
r.skip_updates_counter);
|
||||
if (r.skip_updates_counter == 0) {
|
||||
ModbusCommandItem command_item =
|
||||
ModbusCommandItem::create_read_command(this, r.register_type, r.start_address, r.register_count);
|
||||
queue_command(command_item);
|
||||
// if a custom command is used the user supplied custom_data is only available in the SensorItem.
|
||||
if (r.register_type == ModbusRegisterType::CUSTOM) {
|
||||
auto it = this->find_register_(r.register_type, r.start_address);
|
||||
if (it != sensormap_.end()) {
|
||||
auto command_item = ModbusCommandItem::create_custom_command(
|
||||
this, it->second->custom_data,
|
||||
[this](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->on_register_data(ModbusRegisterType::CUSTOM, start_address, data);
|
||||
});
|
||||
command_item.register_address = it->second->start_address;
|
||||
command_item.register_count = it->second->register_count;
|
||||
command_item.function_code = ModbusFunctionCode::CUSTOM;
|
||||
queue_command(command_item);
|
||||
}
|
||||
} else {
|
||||
queue_command(ModbusCommandItem::create_read_command(this, r.register_type, r.start_address, r.register_count));
|
||||
}
|
||||
r.skip_updates_counter = r.skip_updates; // reset counter to config value
|
||||
} else {
|
||||
r.skip_updates_counter--;
|
||||
@@ -422,6 +445,7 @@ bool ModbusCommandItem::send() {
|
||||
modbusdevice->send_raw(this->payload);
|
||||
}
|
||||
ESP_LOGV(TAG, "Command sent %d 0x%X %d", uint8_t(this->function_code), this->register_address, this->register_count);
|
||||
send_countdown--;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -549,6 +573,9 @@ float payload_to_float(const std::vector<uint8_t> &data, SensorValueType sensor_
|
||||
ESP_LOGD(TAG, "FP32_R = 0x%08X => %f", raw_to_float.raw, raw_to_float.float_value);
|
||||
result = raw_to_float.float_value;
|
||||
} break;
|
||||
case SensorValueType::RAW:
|
||||
result = NAN;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -102,8 +102,6 @@ inline ModbusFunctionCode modbus_register_write_function(ModbusRegisterType reg_
|
||||
return ModbusFunctionCode::READ_WRITE_MULTIPLE_REGISTERS;
|
||||
break;
|
||||
case ModbusRegisterType::READ:
|
||||
return ModbusFunctionCode::CUSTOM;
|
||||
break;
|
||||
default:
|
||||
return ModbusFunctionCode::CUSTOM;
|
||||
break;
|
||||
@@ -221,7 +219,7 @@ template<typename N> N mask_and_shift_by_rightbit(N data, uint32_t mask) {
|
||||
if (result == 0) {
|
||||
return result;
|
||||
}
|
||||
for (int pos = 0; pos < sizeof(N) << 3; pos++) {
|
||||
for (size_t pos = 0; pos < sizeof(N) << 3; pos++) {
|
||||
if ((mask & (1 << pos)) != 0)
|
||||
return result >> pos;
|
||||
}
|
||||
@@ -247,53 +245,36 @@ float payload_to_float(const std::vector<uint8_t> &data, SensorValueType sensor_
|
||||
|
||||
class ModbusController;
|
||||
|
||||
struct SensorItem {
|
||||
class SensorItem {
|
||||
public:
|
||||
virtual void parse_and_publish(const std::vector<uint8_t> &data) = 0;
|
||||
|
||||
void set_custom_data(const std::vector<uint8_t> &data) { custom_data = data; }
|
||||
uint64_t getkey() const { return calc_key(register_type, start_address, offset, bitmask); }
|
||||
size_t virtual get_register_size() const {
|
||||
if (register_type == ModbusRegisterType::COIL || register_type == ModbusRegisterType::DISCRETE_INPUT)
|
||||
return 1;
|
||||
else // if CONF_RESPONSE_BYTES is used override the default
|
||||
return response_bytes > 0 ? response_bytes : register_count * 2;
|
||||
}
|
||||
// Override register size for modbus devices not using 1 register for one dword
|
||||
void set_register_size(uint8_t register_size) { response_bytes = register_size; }
|
||||
ModbusRegisterType register_type;
|
||||
SensorValueType sensor_value_type;
|
||||
uint16_t start_address;
|
||||
uint32_t bitmask;
|
||||
uint8_t offset;
|
||||
uint8_t register_count;
|
||||
uint8_t response_bytes{0};
|
||||
uint8_t skip_updates;
|
||||
std::vector<uint8_t> custom_data{};
|
||||
bool force_new_range{false};
|
||||
|
||||
virtual void parse_and_publish(const std::vector<uint8_t> &data) = 0;
|
||||
|
||||
uint64_t getkey() const { return calc_key(register_type, start_address, offset, bitmask); }
|
||||
|
||||
size_t virtual get_register_size() const {
|
||||
size_t size = 0;
|
||||
switch (sensor_value_type) {
|
||||
case SensorValueType::BIT:
|
||||
size = 1;
|
||||
break;
|
||||
case SensorValueType::U_WORD:
|
||||
case SensorValueType::S_WORD:
|
||||
size = 2;
|
||||
break;
|
||||
case SensorValueType::U_DWORD:
|
||||
case SensorValueType::S_DWORD:
|
||||
case SensorValueType::U_DWORD_R:
|
||||
case SensorValueType::S_DWORD_R:
|
||||
case SensorValueType::FP32:
|
||||
case SensorValueType::FP32_R:
|
||||
size = 4;
|
||||
break;
|
||||
case SensorValueType::U_QWORD:
|
||||
case SensorValueType::U_QWORD_R:
|
||||
case SensorValueType::S_QWORD:
|
||||
case SensorValueType::S_QWORD_R:
|
||||
size = 8;
|
||||
break;
|
||||
case SensorValueType::RAW:
|
||||
size = this->register_count * 2;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
struct ModbusCommandItem {
|
||||
class ModbusCommandItem {
|
||||
public:
|
||||
static const size_t MAX_PAYLOAD_BYTES = 240;
|
||||
static const uint8_t MAX_SEND_REPEATS = 5;
|
||||
ModbusController *modbusdevice;
|
||||
uint16_t register_address;
|
||||
uint16_t register_count;
|
||||
@@ -303,7 +284,9 @@ struct ModbusCommandItem {
|
||||
on_data_func;
|
||||
std::vector<uint8_t> payload = {};
|
||||
bool send();
|
||||
|
||||
// wrong commands (esp. custom commands) can block the send queue
|
||||
// limit the number of repeats
|
||||
uint8_t send_countdown{MAX_SEND_REPEATS};
|
||||
/// factory methods
|
||||
/** Create modbus read command
|
||||
* Function code 02-04
|
||||
@@ -416,6 +399,8 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
|
||||
protected:
|
||||
/// parse sensormap_ and create range of sequential addresses
|
||||
size_t create_register_ranges_();
|
||||
// find register in sensormap. Returns iterator with all registers having the same start address
|
||||
std::map<uint64_t, SensorItem *>::iterator find_register_(ModbusRegisterType register_type, uint16_t start_address);
|
||||
/// submit the read command for the address range to the send queue
|
||||
void update_range_(RegisterRange &r);
|
||||
/// parse incoming modbus data
|
||||
|
||||
@@ -4,29 +4,28 @@ from esphome.components import number
|
||||
from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_MAX_VALUE,
|
||||
CONF_MIN_VALUE,
|
||||
CONF_MULTIPLY,
|
||||
CONF_OFFSET,
|
||||
CONF_STEP,
|
||||
)
|
||||
|
||||
from .. import (
|
||||
add_modbus_base_properties,
|
||||
modbus_controller_ns,
|
||||
ModbusController,
|
||||
SENSOR_VALUE_TYPE,
|
||||
modbus_calc_properties,
|
||||
ModbusItemBaseSchema,
|
||||
SensorItem,
|
||||
SENSOR_VALUE_TYPE,
|
||||
)
|
||||
|
||||
|
||||
from ..const import (
|
||||
CONF_BITMASK,
|
||||
CONF_BYTE_OFFSET,
|
||||
CONF_CUSTOM_COMMAND,
|
||||
CONF_FORCE_NEW_RANGE,
|
||||
CONF_MODBUS_CONTROLLER_ID,
|
||||
CONF_REGISTER_COUNT,
|
||||
CONF_SKIP_UPDATES,
|
||||
CONF_USE_WRITE_MULTIPLE,
|
||||
CONF_VALUE_TYPE,
|
||||
CONF_WRITE_LAMBDA,
|
||||
)
|
||||
@@ -39,22 +38,6 @@ ModbusNumber = modbus_controller_ns.class_(
|
||||
"ModbusNumber", cg.Component, number.Number, SensorItem
|
||||
)
|
||||
|
||||
TYPE_REGISTER_MAP = {
|
||||
"RAW": 1,
|
||||
"U_WORD": 1,
|
||||
"S_WORD": 1,
|
||||
"U_DWORD": 2,
|
||||
"U_DWORD_R": 2,
|
||||
"S_DWORD": 2,
|
||||
"S_DWORD_R": 2,
|
||||
"U_QWORD": 4,
|
||||
"U_QWORDU_R": 4,
|
||||
"S_QWORD": 4,
|
||||
"U_QWORD_R": 4,
|
||||
"FP32": 2,
|
||||
"FP32_R": 2,
|
||||
}
|
||||
|
||||
|
||||
def validate_min_max(config):
|
||||
if config[CONF_MAX_VALUE] <= config[CONF_MIN_VALUE]:
|
||||
@@ -66,45 +49,38 @@ def validate_min_max(config):
|
||||
return config
|
||||
|
||||
|
||||
def validate_modbus_number(config):
|
||||
if CONF_CUSTOM_COMMAND not in config and CONF_ADDRESS not in config:
|
||||
raise cv.Invalid(
|
||||
f" {CONF_ADDRESS} is a required property if '{CONF_CUSTOM_COMMAND}:' isn't used"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
number.NUMBER_SCHEMA.extend(
|
||||
number.NUMBER_SCHEMA.extend(ModbusItemBaseSchema)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ModbusNumber),
|
||||
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
|
||||
cv.Required(CONF_ADDRESS): cv.positive_int,
|
||||
cv.Optional(CONF_OFFSET, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_BYTE_OFFSET): cv.positive_int,
|
||||
cv.Optional(CONF_BITMASK, default=0xFFFFFFFF): cv.hex_uint32_t,
|
||||
cv.Optional(CONF_VALUE_TYPE, default="U_WORD"): cv.enum(SENSOR_VALUE_TYPE),
|
||||
cv.Optional(CONF_REGISTER_COUNT, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_SKIP_UPDATES, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda,
|
||||
cv.GenerateID(): cv.declare_id(ModbusNumber),
|
||||
# 24 bits are the maximum value for fp32 before precison is lost
|
||||
# 0x00FFFFFF = 16777215
|
||||
cv.Optional(CONF_MAX_VALUE, default=16777215.0): cv.float_,
|
||||
cv.Optional(CONF_MIN_VALUE, default=-16777215.0): cv.float_,
|
||||
cv.Optional(CONF_STEP, default=1): cv.positive_float,
|
||||
cv.Optional(CONF_MULTIPLY, default=1.0): cv.float_,
|
||||
cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean,
|
||||
}
|
||||
).extend(cv.polling_component_schema("60s")),
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s")),
|
||||
validate_min_max,
|
||||
validate_modbus_number,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
byte_offset = 0
|
||||
if CONF_OFFSET in config:
|
||||
byte_offset = config[CONF_OFFSET]
|
||||
# A CONF_BYTE_OFFSET setting overrides CONF_OFFSET
|
||||
if CONF_BYTE_OFFSET in config:
|
||||
byte_offset = config[CONF_BYTE_OFFSET]
|
||||
value_type = config[CONF_VALUE_TYPE]
|
||||
reg_count = config[CONF_REGISTER_COUNT]
|
||||
if reg_count == 0:
|
||||
reg_count = TYPE_REGISTER_MAP[value_type]
|
||||
byte_offset, reg_count = modbus_calc_properties(config)
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID],
|
||||
config[CONF_ADDRESS],
|
||||
@@ -130,28 +106,16 @@ async def to_code(config):
|
||||
|
||||
cg.add(var.set_parent(parent))
|
||||
cg.add(parent.add_sensor_item(var))
|
||||
if CONF_LAMBDA in config:
|
||||
await add_modbus_base_properties(var, config, ModbusNumber)
|
||||
cg.add(var.set_use_write_mutiple(config[CONF_USE_WRITE_MULTIPLE]))
|
||||
if CONF_WRITE_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA],
|
||||
config[CONF_WRITE_LAMBDA],
|
||||
[
|
||||
(ModbusNumber.operator("ptr"), "item"),
|
||||
(cg.float_, "x"),
|
||||
(
|
||||
cg.std_vector.template(cg.uint8).operator("const").operator("ref"),
|
||||
"data",
|
||||
),
|
||||
(cg.std_vector.template(cg.uint16).operator("ref"), "payload"),
|
||||
],
|
||||
return_type=cg.optional.template(float),
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
if CONF_WRITE_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_WRITE_LAMBDA],
|
||||
[
|
||||
(ModbusNumber.operator("ptr"), "item"),
|
||||
(cg.float_, "x"),
|
||||
(cg.std_vector.template(cg.uint16).operator("ref"), "payload"),
|
||||
],
|
||||
return_type=cg.optional.template(float),
|
||||
)
|
||||
cg.add(var.set_write_template(template_))
|
||||
cg.add(var.set_write_template(template_))
|
||||
|
||||
@@ -8,12 +8,7 @@ namespace modbus_controller {
|
||||
static const char *const TAG = "modbus.number";
|
||||
|
||||
void ModbusNumber::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
union {
|
||||
float float_value;
|
||||
uint32_t raw;
|
||||
} raw_to_float;
|
||||
|
||||
float result = payload_to_float(data, *this);
|
||||
float result = payload_to_float(data, *this) / multiply_by_;
|
||||
|
||||
// Is there a lambda registered
|
||||
// call it with the pre converted value and the raw data array
|
||||
@@ -31,13 +26,8 @@ void ModbusNumber::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
}
|
||||
|
||||
void ModbusNumber::control(float value) {
|
||||
union {
|
||||
float float_value;
|
||||
uint32_t raw;
|
||||
} raw_to_float;
|
||||
|
||||
std::vector<uint16_t> data;
|
||||
auto original_value = value;
|
||||
float write_value = value;
|
||||
// Is there are lambda configured?
|
||||
if (this->write_transform_func_.has_value()) {
|
||||
// data is passed by reference
|
||||
@@ -46,28 +36,32 @@ void ModbusNumber::control(float value) {
|
||||
auto val = (*this->write_transform_func_)(this, value, data);
|
||||
if (val.has_value()) {
|
||||
ESP_LOGV(TAG, "Value overwritten by lambda");
|
||||
value = val.value();
|
||||
write_value = val.value();
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
value = multiply_by_ * value;
|
||||
write_value = multiply_by_ * write_value;
|
||||
}
|
||||
|
||||
// lambda didn't set payload
|
||||
if (data.empty()) {
|
||||
data = float_to_payload(value, this->sensor_value_type);
|
||||
data = float_to_payload(write_value, this->sensor_value_type);
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG,
|
||||
"Updating register: connected Sensor=%s start address=0x%X register count=%d new value=%.02f (val=%.02f)",
|
||||
this->get_name().c_str(), this->start_address, this->register_count, value, value);
|
||||
this->get_name().c_str(), this->start_address, this->register_count, value, write_value);
|
||||
|
||||
// Create and send the write command
|
||||
auto write_cmd = ModbusCommandItem::create_write_multiple_command(parent_, this->start_address + this->offset,
|
||||
this->register_count, data);
|
||||
|
||||
ModbusCommandItem write_cmd;
|
||||
if (this->register_count == 1 && !this->use_write_multiple_) {
|
||||
write_cmd = ModbusCommandItem::create_write_single_command(parent_, this->start_address + this->offset, data[0]);
|
||||
} else {
|
||||
write_cmd = ModbusCommandItem::create_write_multiple_command(parent_, this->start_address + this->offset,
|
||||
this->register_count, data);
|
||||
}
|
||||
// publish new value
|
||||
write_cmd.on_data_func = [this, write_cmd, value](ModbusRegisterType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
|
||||
@@ -35,6 +35,7 @@ class ModbusNumber : public number::Number, public Component, public SensorItem
|
||||
using write_transform_func_t = std::function<optional<float>(ModbusNumber *, float, std::vector<uint16_t> &)>;
|
||||
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
|
||||
protected:
|
||||
void control(float value) override;
|
||||
@@ -42,6 +43,7 @@ class ModbusNumber : public number::Number, public Component, public SensorItem
|
||||
optional<write_transform_func_t> write_transform_func_;
|
||||
ModbusController *parent_;
|
||||
float multiply_by_{1.0};
|
||||
bool use_write_multiple_{false};
|
||||
};
|
||||
|
||||
} // namespace modbus_controller
|
||||
|
||||
@@ -6,22 +6,22 @@ from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_ID,
|
||||
CONF_MULTIPLY,
|
||||
CONF_OFFSET,
|
||||
)
|
||||
|
||||
from .. import (
|
||||
SensorItem,
|
||||
modbus_controller_ns,
|
||||
ModbusController,
|
||||
modbus_calc_properties,
|
||||
validate_modbus_register,
|
||||
ModbusItemBaseSchema,
|
||||
SensorItem,
|
||||
)
|
||||
|
||||
from ..const import (
|
||||
CONF_BYTE_OFFSET,
|
||||
CONF_MODBUS_CONTROLLER_ID,
|
||||
CONF_USE_WRITE_MULTIPLE,
|
||||
CONF_VALUE_TYPE,
|
||||
CONF_WRITE_LAMBDA,
|
||||
)
|
||||
from ..sensor import SENSOR_VALUE_TYPE
|
||||
|
||||
DEPENDENCIES = ["modbus_controller"]
|
||||
CODEOWNERS = ["@martgras"]
|
||||
@@ -32,34 +32,31 @@ ModbusOutput = modbus_controller_ns.class_(
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
output.FLOAT_OUTPUT_SCHEMA.extend(ModbusItemBaseSchema).extend(
|
||||
{
|
||||
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
|
||||
cv.GenerateID(): cv.declare_id(ModbusOutput),
|
||||
cv.Required(CONF_ADDRESS): cv.positive_int,
|
||||
cv.Optional(CONF_OFFSET, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_BYTE_OFFSET): cv.positive_int,
|
||||
cv.Optional(CONF_VALUE_TYPE, default="U_WORD"): cv.enum(SENSOR_VALUE_TYPE),
|
||||
cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda,
|
||||
cv.Optional(CONF_MULTIPLY, default=1.0): cv.float_,
|
||||
cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean,
|
||||
}
|
||||
),
|
||||
validate_modbus_register,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
byte_offset = 0
|
||||
if CONF_OFFSET in config:
|
||||
byte_offset = config[CONF_OFFSET]
|
||||
# A CONF_BYTE_OFFSET setting overrides CONF_OFFSET
|
||||
if CONF_BYTE_OFFSET in config:
|
||||
byte_offset = config[CONF_BYTE_OFFSET]
|
||||
byte_offset, reg_count = modbus_calc_properties(config)
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID], config[CONF_ADDRESS], byte_offset, config[CONF_VALUE_TYPE]
|
||||
config[CONF_ID],
|
||||
config[CONF_ADDRESS],
|
||||
byte_offset,
|
||||
config[CONF_VALUE_TYPE],
|
||||
reg_count,
|
||||
)
|
||||
await output.register_output(var, config)
|
||||
cg.add(var.set_write_multiply(config[CONF_MULTIPLY]))
|
||||
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
|
||||
cg.add(var.set_use_write_mutiple(config[CONF_USE_WRITE_MULTIPLE]))
|
||||
cg.add(var.set_parent(parent))
|
||||
if CONF_WRITE_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
|
||||
@@ -13,11 +13,6 @@ void ModbusOutput::setup() {}
|
||||
*
|
||||
*/
|
||||
void ModbusOutput::write_state(float value) {
|
||||
union {
|
||||
float float_value;
|
||||
uint32_t raw;
|
||||
} raw_to_float;
|
||||
|
||||
std::vector<uint16_t> data;
|
||||
auto original_value = value;
|
||||
// Is there are lambda configured?
|
||||
@@ -45,8 +40,14 @@ void ModbusOutput::write_state(float value) {
|
||||
this->start_address, this->register_count, value, original_value);
|
||||
|
||||
// Create and send the write command
|
||||
auto write_cmd =
|
||||
ModbusCommandItem::create_write_multiple_command(parent_, this->start_address, this->register_count, data);
|
||||
// Create and send the write command
|
||||
ModbusCommandItem write_cmd;
|
||||
if (this->register_count == 1 && !this->use_write_multiple_) {
|
||||
write_cmd = ModbusCommandItem::create_write_single_command(parent_, this->start_address + this->offset, data[0]);
|
||||
} else {
|
||||
write_cmd = ModbusCommandItem::create_write_multiple_command(parent_, this->start_address + this->offset,
|
||||
this->register_count, data);
|
||||
}
|
||||
parent_->queue_command(write_cmd);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,12 +11,13 @@ using value_to_data_t = std::function<float>(float);
|
||||
|
||||
class ModbusOutput : public output::FloatOutput, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusOutput(uint16_t start_address, uint8_t offset, SensorValueType value_type)
|
||||
ModbusOutput(uint16_t start_address, uint8_t offset, SensorValueType value_type, int register_count)
|
||||
: output::FloatOutput(), Component() {
|
||||
this->register_type = ModbusRegisterType::HOLDING;
|
||||
this->start_address = start_address;
|
||||
this->offset = offset;
|
||||
this->bitmask = bitmask;
|
||||
this->register_count = register_count;
|
||||
this->sensor_value_type = value_type;
|
||||
this->skip_updates = 0;
|
||||
this->start_address += offset;
|
||||
@@ -32,6 +33,7 @@ class ModbusOutput : public output::FloatOutput, public Component, public Sensor
|
||||
|
||||
using write_transform_func_t = std::function<optional<float>(ModbusOutput *, float, std::vector<uint16_t> &)>;
|
||||
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
|
||||
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
|
||||
|
||||
protected:
|
||||
void write_state(float value) override;
|
||||
@@ -39,6 +41,7 @@ class ModbusOutput : public output::FloatOutput, public Component, public Sensor
|
||||
|
||||
ModbusController *parent_;
|
||||
float multiply_by_{1.0};
|
||||
bool use_write_multiple_;
|
||||
};
|
||||
|
||||
} // namespace modbus_controller
|
||||
|
||||
@@ -2,17 +2,19 @@ from esphome.components import sensor
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
|
||||
from esphome.const import CONF_ID, CONF_ADDRESS, CONF_LAMBDA, CONF_OFFSET
|
||||
from esphome.const import CONF_ID, CONF_ADDRESS
|
||||
from .. import (
|
||||
SensorItem,
|
||||
add_modbus_base_properties,
|
||||
modbus_controller_ns,
|
||||
ModbusController,
|
||||
modbus_calc_properties,
|
||||
validate_modbus_register,
|
||||
ModbusItemBaseSchema,
|
||||
SensorItem,
|
||||
MODBUS_REGISTER_TYPE,
|
||||
SENSOR_VALUE_TYPE,
|
||||
)
|
||||
from ..const import (
|
||||
CONF_BITMASK,
|
||||
CONF_BYTE_OFFSET,
|
||||
CONF_FORCE_NEW_RANGE,
|
||||
CONF_MODBUS_CONTROLLER_ID,
|
||||
CONF_REGISTER_COUNT,
|
||||
@@ -29,61 +31,31 @@ ModbusSensor = modbus_controller_ns.class_(
|
||||
"ModbusSensor", cg.Component, sensor.Sensor, SensorItem
|
||||
)
|
||||
|
||||
TYPE_REGISTER_MAP = {
|
||||
"RAW": 1,
|
||||
"U_WORD": 1,
|
||||
"S_WORD": 1,
|
||||
"U_DWORD": 2,
|
||||
"U_DWORD_R": 2,
|
||||
"S_DWORD": 2,
|
||||
"S_DWORD_R": 2,
|
||||
"U_QWORD": 4,
|
||||
"U_QWORDU_R": 4,
|
||||
"S_QWORD": 4,
|
||||
"U_QWORD_R": 4,
|
||||
"FP32": 2,
|
||||
"FP32_R": 2,
|
||||
}
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
sensor.SENSOR_SCHEMA.extend(
|
||||
sensor.SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(ModbusItemBaseSchema)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ModbusSensor),
|
||||
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
|
||||
cv.Required(CONF_ADDRESS): cv.positive_int,
|
||||
cv.Required(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE),
|
||||
cv.Optional(CONF_OFFSET, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_BYTE_OFFSET): cv.positive_int,
|
||||
cv.Optional(CONF_BITMASK, default=0xFFFFFFFF): cv.hex_uint32_t,
|
||||
cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE),
|
||||
cv.Optional(CONF_VALUE_TYPE, default="U_WORD"): cv.enum(SENSOR_VALUE_TYPE),
|
||||
cv.Optional(CONF_REGISTER_COUNT, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_SKIP_UPDATES, default=0): cv.positive_int,
|
||||
cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
),
|
||||
validate_modbus_register,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
byte_offset = 0
|
||||
if CONF_OFFSET in config:
|
||||
byte_offset = config[CONF_OFFSET]
|
||||
# A CONF_BYTE_OFFSET setting overrides CONF_OFFSET
|
||||
if CONF_BYTE_OFFSET in config:
|
||||
byte_offset = config[CONF_BYTE_OFFSET]
|
||||
byte_offset, reg_count = modbus_calc_properties(config)
|
||||
value_type = config[CONF_VALUE_TYPE]
|
||||
reg_count = config[CONF_REGISTER_COUNT]
|
||||
if reg_count == 0:
|
||||
reg_count = TYPE_REGISTER_MAP[value_type]
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID],
|
||||
config[CONF_REGISTER_TYPE],
|
||||
config[CONF_ADDRESS],
|
||||
byte_offset,
|
||||
config[CONF_BITMASK],
|
||||
config[CONF_VALUE_TYPE],
|
||||
value_type,
|
||||
reg_count,
|
||||
config[CONF_SKIP_UPDATES],
|
||||
config[CONF_FORCE_NEW_RANGE],
|
||||
@@ -93,17 +65,4 @@ async def to_code(config):
|
||||
|
||||
paren = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
|
||||
cg.add(paren.add_sensor_item(var))
|
||||
if CONF_LAMBDA in config:
|
||||
template_ = await cg.process_lambda(
|
||||
config[CONF_LAMBDA],
|
||||
[
|
||||
(ModbusSensor.operator("ptr"), "item"),
|
||||
(cg.float_, "x"),
|
||||
(
|
||||
cg.std_vector.template(cg.uint8).operator("const").operator("ref"),
|
||||
"data",
|
||||
),
|
||||
],
|
||||
return_type=cg.optional.template(float),
|
||||
)
|
||||
cg.add(var.set_template(template_))
|
||||
await add_modbus_base_properties(var, config, ModbusSensor)
|
||||
|
||||
@@ -10,11 +10,6 @@ static const char *const TAG = "modbus_controller.sensor";
|
||||
void ModbusSensor::dump_config() { LOG_SENSOR(TAG, "Modbus Controller Sensor", this); }
|
||||
|
||||
void ModbusSensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
union {
|
||||
float float_value;
|
||||
uint32_t raw;
|
||||
} raw_to_float;
|
||||
|
||||
float result = payload_to_float(data, *this);
|
||||
|
||||
// Is there a lambda registered
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user