Compare commits

..
Author SHA1 Message Date
J. Nick Koston 07c5ef5c2c [hardware-test] Write the Xiaomi run config as run_NAME.yaml and ignore it 2026-11-17 00:17:24 -06:00
J. Nick Koston 75ba3cbbae [hardware-test] Fail whirlpool/all.sh on a failed variant and compare.py on empty input 2026-11-16 23:45:39 -06:00
J. Nick Koston da6686fe83 [hardware-test] Keep the Xiaomi and Pronto runners' exit status and ignore the dumpall diff outputs 2026-11-16 23:13:35 -06:00
J. Nick Koston e988dbd872 [hardware-test] Exit non-zero from the Tuya runner and the IR comparers on failures and mismatches 2026-11-16 22:44:05 -06:00
J. Nick Koston 6d5d793986 [hardware-test] Fail the IR and sensor runners on missing inputs, bad corpora and fatal connect errors 2026-11-16 22:15:47 -06:00
J. Nick Koston 1263a56c52 [hardware-test] Lock the scan timer counters, fix the whirlpool short frame idle and two generator edge cases 2026-11-16 21:50:00 -06:00
J. Nick Koston 8f07b49fd9 [hardware-test] Fix the IR runners' output paths, interpreter and capture stop, and end raw frames without line numbers 2026-11-16 21:41:53 -06:00
J. Nick Koston 75c3e8e00c [hardware-test] Share board packages, comparers and helpers in the IR and sensor flows
- One-pin loopback board packages and one base package for the climate and Pronto configs.
- compare.py --flat replaces flatcmp.py; Pronto run.sh drives press.py and counts on serial;
  one trigger block parser; one host harness case template; aeha_random in the fuzz header.
- lognorm.py for the A/B log diffs; mibeacon.py shared by both Xiaomi generators.
- Tuya checksums via sum_bytes, BLE soak waits on the capture, gen_dumpers.py for dumpall.
- Review fixes: ha.py connect bounded by the soak time, consist.py skips the binary sensor
  line, the Pronto capture length follows ROUNDS.
2026-11-16 21:35:01 -06:00
J. Nick Koston 7d697a8a62 Add climate IR, remote_base A/B, Tuya, Xiaomi and BLE scan flows to the hardware-test skill 2026-11-16 21:12:02 -06:00
131 changed files with 4198 additions and 2174 deletions

No files matched your search

+4 -2
View File
@@ -20,7 +20,9 @@ driven, what the logs show, and the same flow on a `dev` build when a quirk coul
down. Never run `esphome logs` in a `while true` loop: the loop respawns it and holds the port.
2. **Credentials stay local.** Put a `secrets.yaml` next to the scratch configs and reference it with
`!secret`. A copy of a device's own config keeps using the secrets file next to that config: two
checkouts can hold different networks, and the wrong one leaves the device unreachable after the OTA. Never paste a config with credentials into a PR, never commit scratch configs or logs.
checkouts can hold different networks, and the wrong one leaves the device unreachable after the OTA.
Generate an `api_key` with `openssl rand -base64 32` when you have none. Never paste a config with
credentials into a PR, never commit scratch configs or logs.
3. **Build from the tree under test.** `python -m esphome` puts the current directory first on
`sys.path`, so run it with the checkout or worktree as the working directory and pass the yaml by
its absolute path. A venv `esphome` entry point may import an installed snapshot instead of the
@@ -158,7 +160,7 @@ recorder, dump saver and decoder, cover cycle driver), `online_image/`
(host driver, Zephyr driver probe), `rtsp_server/` (protocol client, raw rate reader, measurement set,
API image grab), `cse7766/` (API observer with ping), `zephyr_coredump/` (reader flow, decode with the
Zephyr tools), `libretiny_prefs/` (rtc preference harness header), `libretiny_crash/` (BK72xx crash probe header,
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
## Templates in `$SKILL/templates`
@@ -87,3 +87,4 @@ One file per component, named after it:
- [`improv.md`](improv.md): Improv serial with stray bytes, scan, bad and good passwords from a client that reads frames out of the log stream, Improv over BLE, per board notes
- [`arduino_pico.md`](arduino_pico.md): arduino-pico framework bump A/B: W5100S Ethernet soak, Pico W fallback AP client count, Pico W bluetooth_proxy GATT hold, web_server SSE stalls, `/events` churn crashes and never completed connects, W5100S OTA without `interrupt_pin`
- [`bluedroid_gattc_queue.md`](bluedroid_gattc_queue.md): queued MTU request and execute write recursion that overflows BTU_TASK, ble_client central against an ESP32-S3 peer
- [`tuya.md`](tuya.md): Tuya MCU simulator on a cross-wired second UART (handshake, every datapoint type, command frames logged), hub vs entity order, dump A/B
@@ -67,3 +67,26 @@ reference from an ESP32 proxy (unset `API_KEY` for a plaintext API).
- On rp2 before #22679, BTstack truncated 32-bit base UUIDs with a nonzero low half to 16 bits (`5ec10001-...` came
out as `00000001-...`); a zero low half (5ec10000) came through intact.
- Connects through the Pico W proxy fail with 0x3E now and then (see `reference/boards.md`); the scripts retry.
## Scan complete never arrives (#22770)
`scripts/ble_scan_complete/`: `scan.yaml` is a proxy config like the report's (ESP32-C6, interval = window = 1100 ms,
debug sensors, `api: reboot_timeout: 0s`, a `duration` substitution, 5 s by default; the validator wants at least
three intervals, so 2 s is refused). The tracker watchdog is `duration * 2`, so a short duration shortens it too.
`scaninst.yaml` adds `scan_inst.hpp` and five `-Wl,--wrap` flags that count one scan's duration timer through
Bluedroid: `BTM_BleScan` starts and stops, `btu_start_timer(BTU_TTYPE_BLE_SCAN)`, the timer's alarm firing (a shim on
its `osi_alarm_new` callback, BTC task), `btm_ble_timeout` (BTU task) and the INQ_CMPL handed to `btc_transfer_context`
(PID 5, act 3; the param is 108 bytes in libbt, so do not compare with the C++ `sizeof`). A 30 s interval logs them as
`scaninst`; a lost completion shows where the chain stopped (timer > fire + 1, fire > timeout, timeout > cmpl_post, or
cmpl_post above the ESPHome-side `ends / 2`). `ha.py HOST OUT SECONDS [--toggle S --jitter J]` is the Home Assistant
stand-in (raw adverts, scanner state, set mode passive once, optional active/passive toggles). `soak.sh TAG HOST
SECONDS [ha.py options]` (env PORT, API_KEY or else `apikey.txt`) runs a bounded serial capture plus ha.py.
`wait.sh CAPTURE SECONDS [FROM_LINE]` blocks until a timeout, drop or unexpected-state line and prints the counters.
- Bluedroid and IDF log lines reach the ESPHome log as tag `esp-idf` at VERBOSE (`[V][esp-idf:000][BTU_TASK]: E ...`),
so a user capturing at INFO over the API never sees a Bluedroid error.
- A stop from Home Assistant landing just after the timer fired (toggle period 5.03 s with 0.08 s jitter against 5 s
scans hits it about once per 20 minutes) gives `Unexpected state: STOPPING on scan complete`, `stop complete -
status 1`, `BTM_BleScan scan not active`; the tracker recovers. A stop while STARTING logs `Cannot stop scan:
STARTING` and the mode change waits for the next restart.
- With about 55 devices around (about 95 adverts/s), no lost completion in about 2800 natural 5 s cycles on 2026.10.2
(VERBOSE and INFO builds) plus about 670 toggled stops, and none on dev.
@@ -86,3 +86,15 @@ Notes:
them from the device's `Got state` log lines.
- Turning a script off stops it mid run (the flag stays on). A script that finishes at once reports on, then off, in
the same second.
## Subscription order and dump order (#22725)
- `hadrive.py` also prints `<phase> SUBORDER NNN <line>` in the order the device sent the subscriptions (the SUB lines
are sorted). `grep -v SUBORDER drive.txt` diffs the sets, `grep SUBORDER` the order.
- Dump block order per build: `python3 scripts/lognorm.py boot1.txt | grep -a "^\[C\]\[" |
sed -E 's/^\[C\]\[([a-z_.0-9]+)\]: (.{0,40}).*/\1 | \2/' | awk -F' [|] ' '{ if ($1!=p) {print $1 " :: " $2; p=$1} }'`
prints one line per component run; diff dump text between builds through `lognorm.py` too, which drops the
`:NNN` line numbers.
- Idle ESP8266 free heap in this config swings by 2 kB between readings; compare the steady reading (the fifth one
after boot) per boot, which matched the heap_size change exactly (+168 B for 15 import entities on #22725).
- The wizard light (`l_wiz`) needs #21908; a stack based before it needs it dropped on both builds (`NO_LIGHT=1`).
@@ -44,3 +44,116 @@ only the last line sees "Failed to upload" first, so rerun before blaming the bu
output. `scripts/esp8266_lock_load/ir-one-pin.yaml` does this, and its `run.sh` adds Wi-Fi load (see
[one_wire.md](one_wire.md)). Under load, the build that sends the whole frame in one lock hit a Hardware Watchdog in
`wDev_ProcessFiq` after 11 minutes.
## Climate IR loopback over the API (#22704 whirlpool, #22709 climate_ir)
- Pattern: one image with a sending climate and a receiving climate (`receiver_id`) per platform on one shared pin, an
aioesphomeapi script that sends climate calls and reads every climate's state after each step. Several platforms can
share one receiver; each rejects the others' frames. Scripts in `scripts/climate_ir_loop/`: `drive.py`
(steps `Name:mode=cool,t=24,fan=low,swing=vertical,preset=eco` or `svc:action?flag=1`), `run.sh NAME PORT [STEPS]`
with `steps.sh` (prelude and steps), `frames.py NAME` (splits the `dump: raw` lines per step), `compare.py A B
[TOL] [--flat]` (per step, or with `--flat` the whole sequence timing by timing), `base.yaml` (network, API and
logger), `c-common.yaml` (Samsung, Friedrich, Siemens IRA211 and Mistral senders and receivers), `esp32-ir.yaml`
(GPIO27) and `esp8266-ir.yaml` (GPIO12) as one-pin loopback board packages (`pin`, and on the ESP32 `rmt_symbols`
and `receive_symbols`, are substitutions; the Pronto configs include them too), `c32pr.yaml` (an example run file)
and `whirlpool-inject-common.yaml` (injected frame actions).
- ESP32-PICO-D4 kit: GPIO4 does not loop back cleanly (every mark reads 162 µs short, a -162 space leads the frame, so
nothing decodes). GPIO27 gives exact timings (572/553/1659 µs). Try another pin before suspecting the code.
- RMT ends a frame on any level longer than `idle`, mark or space. A whirlpool 14 byte frame ends after a 7,960 µs gap,
so its decoder needs the trailing idle to be within 25% of that, but the 9 ms header mark must stay below idle:
`idle: 9500us` works, 8500 cuts the header off.
- Restored climate state changes the first frames (power toggles, Samsung power section): start every run with a
prelude that puts each sender in a fixed state (every field each sender supports, such as humidity for daikin_arc
or a fan mode daikin_brc supports), and compare only the frames after it. ESP8266 restore lives in RTC memory, so a
receiver's state can carry over from the previous image.
- Inject frames a sender cannot produce (an out of range temperature code, a short frame) from an API action lambda with
`item_bytes<BitTimings>` and `call.perform()`.
- Assigning frames to steps by host timestamps is fragile on the D1 mini (a log line can land after the next step
starts); compare the whole frame sequence in order instead. Two frames sent back to back (siemens_ira211 power plus
sync) come out as one dump, and the gap between them varies by up to 1 ms on ESP8266.
- Samsung power frames are 347 timings: an ESP32 receiver needs `rmt_symbols: 384` and `receive_symbols: 384`.
## Protocol A/B with `dump: all` (#22711)
- One button per `transmit_<protocol>` action, one binary sensor and one `on_<protocol>` trigger per protocol, each
logging a marker; count markers and compare the dumper lines of the dev and PR runs in order. All of nec, dooya,
nexus, aok and cardin_s438 decode in the ESP32 GPIO27 loopback with the default idle.
- Cardin S438 picks a random seed per frame (`random_uint32()`), so its pronto and cardin lines differ between any two
runs; compare code and channel only. Each press sends 3 repeats, so its trigger and binary sensor fire 3 times.
- Start the dump extraction at the first `MARK tx` of any protocol: the API lists buttons by name, not config order.
- A Python-only change can still move generated code: diff `src/main.cpp` of the two builds (field order in action
setters, defaults now written explicitly).
## Large climate frames in the loopback (#22710)
- At 115200 baud the serial log cannot keep up with long `dump: raw` frames (hitachi_ac344 is 691 timings, daikin
sends 3 frames per command). Lines get lost or merged, frames show up under the wrong step, and the script reads the
receiver state before it has decoded the frame. Use `logger: baud_rate: 921600`, capture with `--baud 921600`, and
wait 4 s per step (`BAUD=921600 STEP_S=4 run.sh ...`). A CH340 D1 mini can garble 921600; 460800 carries the long
frames cleanly there.
- On the classic ESP32 the receiver has no RX ping-pong, so a whole frame has to fit `rmt_symbols`. With a 64 symbol
transmitter, `rmt_symbols: 448` and `receive_symbols: 448` hold hitachi_ac344.
- Receivers that keep state the frame does not carry (climate_ir_lg swing is a toggle, zhlt01 keeps its preset on
"none") start each run from restored state, so compare them without those fields or erase the preferences first.
## Whirlpool A/B by hex lines (#22710 retest)
- whirlpool logs every frame at VERBOSE (`Sending:` and `Received:` hex), so on the D1 mini compare those bytes line by
line instead of raw timings. One image per model (DG11J1-3A, HITACHI_HCRA31NEWH, HISENSE_ARG; "Hisense ARG" is a
whirlpool model, not its own component). `scripts/climate_ir_loop/whirlpool/`: `cl.yaml` (the model
as a substitution, combined with `../base.yaml` and `../esp8266-ir.yaml`), `steps.sh`, and `all.sh PORT BASE_TREE PR_TREE`, which builds and runs base then PR per model so the
board ends on a PR build.
- The HISENSE_ARG receiver reports fan medium for fan_only while the sender says auto (dev too).
## Pronto dump A/B with `dump: all` (#22752)
- Scripts in `scripts/remote_base/pronto/`: `gen.py` writes `gen-common.yaml` (buttons for NEC and raw frames of
21/41/43/121/245 timings, a soak button with heap logs, a `pronto:` binary sensor), `esp32-ir.yaml` (GPIO27) and
`d1-ir.yaml` (GPIO12) include it with the board packages from `climate_ir_loop/`, `run.sh NAME TREE YAML PORT BAUD
ROUNDS` flashes, captures, presses every tx button ROUNDS times and then the soak button with `../press.py`, and
counts the markers, triggers and `on_press` lines on serial; `extract.py` keeps the dump lines without timestamps
and source line numbers, `consist.py` checks every dump against the `on_pronto` text, `jit.py` counts frames that
differ between two runs.
- The received frame ends with the idle gap as an extra timing (`0181` for the 10 ms idle), so a `pronto:` binary sensor
built from the sent timings needs one more number at the end, or the length check rejects every frame. A 41 timing
frame is then exactly 230 characters, one full log chunk: a good boundary case.
- ESP32 RMT loopback gives byte-identical Pronto text across builds. The D1 mini edge interrupt moves timings by one
Pronto unit between any two runs (about half the frames), so compare D1 runs by dump against trigger consistency.
- `dump: all` prints NEC lines between the `on_pronto` trigger output and the Pronto dump; the API binary sensor
ON/OFF pair can arrive collapsed, so count `on_press` log lines on serial instead.
## Protocol decode A/B on the host, climate fuzz, AEHA/Haier/Mirage loopback (#22773)
- `scripts/remote_base/hostharness/`: a `host:` config (`rbhost.yaml`) with the local `rbtest` component in
`scripts/remote_base/ext/` (AUTO_LOAD remote_base, `request_protocol()` for every protocol source) and `rbh.hpp` called from `on_boot`; it encodes, mutates, decodes
and dumps, then `exit(0)`. Run the `program` binary and compare stdout through `scripts/lognorm.py`. The Haier
and Mirage decoders want one more item after the last mark (the receiver's idle gap): append `-20000` to transmitter
timings or every clean frame fails.
- AEHA frames longer than 35 bytes decode as their first 35 bytes (the trailer mark equals a bit mark).
- `scripts/climate_ir_fuzz/`: copy `climate_fuzz.hpp` plus the matching `*_fuzz.cpp.example` (as `.cpp`) into
`tests/components/<climate>/` of each tree (haier_tundra also needs `haier_init.py` as `__init__.py`), run
`script/cpp_unit_test.py`, diff the `FUZZ` lines. `run()` appends the receiver's idle gap to every frame, and
`aeha_random()` builds the random frames for the AEHA climates. All tests of one run share one main.cpp: use a macro include guard,
not `#pragma once`, for a header copied to several test dirs.
- Lambda idiom checks: compile `void f(AEHAData x) { ... }` snippets with `-fsyntax-only -I<host build>/src` and
`#include "esphome.h"` (format_hex lives in alloc_helpers.h).
- Loopback config `ir-common.yaml` (AEHA 2 to 35 bytes, Haier, Mirage from an API button, `../press.py HOST tx_all:14 ...`;
the trigger lambdas call `rbh_on_frame()` from `rbh.hpp`, so keep the run file next to it). An ESP32-PICO-D4
on GPIO27 never decodes 34 or 35 byte AEHA, even with `rmt_symbols: 384`, `buffer_size: 8kb` (dev too); a D1 mini on
GPIO12 decodes all of them.
## Every dumper over a saved corpus, on the host (#22814)
`scripts/remote_base/dumpall/`: the same `rbtest` component requests every `_PROTOCOL_STEMS` source; `rbh.hpp` needs a
`dumpers.h` that `gen_dumpers.py TREE` writes from the `DECLARE_REMOTE_PROTOCOL(...)` lines of the tree (plus Pronto,
RCSwitch, ABBWelcome; Raw only when nothing matched). Encoders that randomise (Cardin S438 picks a random seed) make two builds see
different inputs: run once with `RBH_CORPUS=<file>` to write the corpus, then replay that file on both builds.
Merge back-to-back marks or spaces before decoding (Drayton never decodes otherwise). Nudging a quarter of all
timings to the tolerance edges proves nothing (the frame fails elsewhere): put only the sync mark at lo-1, lo, hi,
hi+1 and keep the rest exact; that variant caught a planted `< max` in 1,000 decodes per protocol.
`cmp.sh` compares the DEBUG and VERBOSE outputs (`rbhost.yaml` built with `-s level DEBUG` and `-s level VERBOSE`)
through `scripts/lognorm.py`.
- Loopback with `dump: all` (`ir-common.yaml`, `../press.py HOST tx_all:9 tx_all:9 verbose tx_all:9 debug`): Beo4 never decodes on one pin (needs its carrier) and
Drayton's channel/command form has no checksum; compare decoded lines, raw and Pronto timings jitter between runs.
- `cc-common.yaml`: an ESP32-C3 with a CC1101 in async OOK at 433.92 MHz, GDO0 into remote_receiver, for the RF
dumpers on real air (a quiet band gives only a few Pronto dumps a minute and no protocol hits).
@@ -0,0 +1,30 @@
# Tuya MCU simulator (#22724)
No Tuya device needed. `scripts/tuya_sim/`: `tuyasim.hpp` is a Tuya MCU on a second UART of the same ESP32, cross-wired
through the GPIO matrix to the hub's UART (hub tx 9 rx 10, MCU tx 10 rx 9 on an ESP32-PICO-D4, `board: pico32`,
`allow_other_uses: true` on all four pins, 9600 baud). It answers heartbeat (0x00 first, then 0x01), product query,
conf query with an empty payload (so the hub takes the WIFI_STATE path), wifi state and datapoint query (one report with
all 14 datapoints), echoes each DATAPOINT_DELIVER back as a report like a real MCU, and logs every hub frame as
`SIM RX #n cmd XX: <hex>`. `tuyasim::start()` (the "Sim start" button) plays 18 scripted steps 1.5 s apart: every
datapoint type (bool, value incl. negative, string, enum, 1/2/4 byte bitmask, raw), a four datapoint frame, one
DATAPOINT_REPORT_SYNC (the hub must ack with 0x23), a repeated value.
- `tuya.yaml`: hub plus binary_sensor, switch, three sensors (int, enum, bitmask), three text sensors (string, enum,
raw), three selects (enum, string, int), water heater (switch/target/current/mode), number and fan. Hub
`on_datapoint_update` lambdas log `HUB dpN x=.. <entity>=..` with the entity's state, and entity triggers log
`ENT <id> <value>`, so the hub-before-entity order and the old state the hub sees are visible per datapoint.
`name: ${name}`, built with `pio_build.sh compile <name>` per tree.
- `tuyadrive.py HOST OUT.json` (API_KEY env): subscribes states, presses Sim start, then sends switch, select,
water heater (on, target, mode eco/electric, off), number and fan commands 2 s apart; prints per phase counts.
- `run.sh NAME TREE` (env PORT, API_KEY): upload, serial capture with a reset (stopped after the drive), drive.
- `compare.sh A B`: normalises both logs with `../lognorm.py` and diffs API states, the entity dump block, the 1 s re-dump, the HUB/ENT order and the non-heartbeat
frames the simulator received. Heartbeats move with timing, so they are left out.
Facts from the run:
- At init the fan delivers its own state (`1f01000100`) before DP31 is known and the hub logs `Setting unknown
datapoint 31`; same on dev.
- The API coalesces states published in one loop pass: the four datapoint water heater frame gives one state.
- A switch or select command for the value it already has sends no frame.
- Water heater target 55 with multiplier 0.5 is sent as 110 (`15 02 0004 0000006e`); eco/electric as enum 0/2 on DP23.
- #22724 changed only the dump order (children now print right after the hub's own lines); `setup_priority:` on a
tuya sensor is rejected on the PR and accepted on dev.
@@ -36,3 +36,22 @@ tracker as scan results: everything after the radio runs as normal.
first one it hears.
- Each frame arrives about 4 times; the counts matched the injection run exactly apart from one radio loss, so the
over the air comparison allows two missing publishes per tag.
## Every Xiaomi platform at once (#22727)
- `gen_all.py` writes `frames_all.hpp`, `xball_entities.yaml` and `expected_all.json`: one device per platform
(the 20 listener-only platforms, `xiaomi_mue4094rt`, `xiaomi_rtcgq02lm` hub, `xiaomi_body_scale`, ATC, PVVX),
10 cycles, every frame sent twice. `mibeacon.py` holds the frame builders and the C++ injector that `gen.py`
and `gen_all.py` share. Each entity logs `PUB <dev>.<key> <value>` on publish (binary sensors with
`trigger_on_initial_state: true`). Frame shapes that parse: long encrypted (fc 0x58) only up to 24 bytes, so
a 7 byte `th` object needs the short 19 byte frame, which only lywsd02mmc, xmwsdj04mmc, mjyd02yla, cgpr1 and
rtcgq02lm accept; hhccjcy10 is FD50 9 bytes and miscale v1 181D 10 bytes (no counter, the repeat publishes
again); mue4094rt is a plain fc 0x40 frame; body_scale an S200 0x4E16 object in a 24 byte short frame.
`xiaomi_mccgq02hl` publishes 9 of 10: the first frame is "closed", which equals its initial off state.
- `xball.yaml` is an ESP32-S3 (ESP-IDF, no network, USB_SERIAL_JTAG log) that injects one cycle per second from
20 s and logs free heap before and after (`IDLE` lines). `run_all.sh TREE NAME PORT` builds, flashes, captures
and counts; `count_all.py cap1 cap2 ...` also says whether the values are identical across builds.
- `xbsetup_priority.yaml`: `esphome config` with `setup_priority:` on a moved sensor, a moved binary sensor and
mue4094rt (still a component); remove one at a time, since validation stops at the first platform error.
- On #22727: 441 publishes on the PR, its stacked base and dev, identical values; dump text identical, the PR prints
the 20 listener dumps after `BLE Tracker:`.
@@ -37,3 +37,23 @@ npm1300/*-cap.err
npm1300/*-run.log
rp2_sched_lock/*.txt
rp2_sched_lock/*.log
climate_ir_loop/*-*.txt
climate_ir_loop/*-frames.json
climate_ir_loop/whirlpool/w??base.yaml
climate_ir_loop/whirlpool/w??pr.yaml
tuya_sim/*.txt
tuya_sim/*.json
remote_base/pronto/*.txt
remote_base/pronto/gen-common.yaml
remote_base/dumpall/dumpers.h
remote_base/dumpall/db.txt
remote_base/dumpall/dp.txt
remote_base/dumpall/vb.txt
remote_base/dumpall/vp.txt
xiaomi_ble/*-cap.txt
xiaomi_ble/frames_all.hpp
xiaomi_ble/xball_entities.yaml
xiaomi_ble/expected_all.json
xiaomi_ble/run_*.yaml
ble_scan_complete/*.txt
ble_scan_complete/*.err
@@ -0,0 +1,129 @@
"""Home Assistant stand-in for a bluetooth_proxy scan soak (esphome#22770).
usage: ha.py HOST OUT SECONDS [--toggle SECS [--jitter SECS]] [--mode passive|active|none]
Subscribes to raw advertisements and the scanner state like Home Assistant, sets the scanner mode once after connect
(passive by default, as HA does for its proxies) and with --toggle flips active/passive every SECS seconds to stress
the stop/restart path. Writes one line per event to OUT: scanner state changes, a per-second advert count, mode
requests, connection drops. Reconnects after a drop (device reboot) and keeps going until SECONDS have passed.
API_KEY in the env selects Noise.
"""
import argparse
import asyncio
import pathlib
import random
import sys
import time
_here = pathlib.Path(__file__).resolve().parent
# apiconn.py: copied next to it, or the skill scripts dir
sys.path[:0] = [str(_here), str(_here.parent)]
from apiconn import FATAL, connect, stop_event # noqa: E402
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("host")
ap.add_argument("out")
ap.add_argument("seconds", type=float)
ap.add_argument("--toggle", type=float, default=0)
ap.add_argument(
"--jitter",
type=float,
default=0,
help="random +- seconds added to each toggle period",
)
ap.add_argument("--mode", default="passive", choices=["passive", "active", "none"])
a = ap.parse_args()
asyncio.run(run(a))
async def run(a: argparse.Namespace) -> None:
from aioesphomeapi import BluetoothScannerMode
end = time.monotonic() + a.seconds
f = pathlib.Path(a.out).open("a", encoding="utf-8", buffering=1) # noqa: SIM115
def w(s: str) -> None:
f.write(
f"{time.strftime('%H:%M:%S')}.{int(time.time() * 1000) % 1000:03d} {s}\n"
)
session = 0
connected = False
while time.monotonic() < end:
session += 1
stopped, on_stop = stop_event()
# a try takes up to 11 s (10 s timeout, 1 s pause): bound the tries by the soak time left instead of
# cancelling connect(), which could leave a half open client behind
tries = max(1, int((end - time.monotonic()) // 11))
try:
c = await connect(a.host, tries=tries, on_stop=on_stop)
except SystemExit as e:
w(f"CONNECT FAILED {e}")
# a wrong key or a plaintext/Noise mismatch never recovers: stop instead of soaking without a client
if isinstance(e.__context__, FATAL):
raise
await asyncio.sleep(5)
continue
w(f"CONNECTED session={session}")
connected = True
adv = [0]
macs: set[int] = set()
def on_raw(resp, adv=adv, macs=macs) -> None:
for x in resp.advertisements:
adv[0] += 1
macs.add(x.address)
def on_state(st) -> None:
w(f"SCANNER state={st.state.name} mode={st.mode.name}")
unsub_raw = c.subscribe_bluetooth_le_raw_advertisements(on_raw)
unsub_state = c.subscribe_bluetooth_scanner_state(on_state)
if a.mode != "none":
m = (
BluetoothScannerMode.PASSIVE
if a.mode == "passive"
else BluetoothScannerMode.ACTIVE
)
c.bluetooth_scanner_set_mode(m)
w(f"SET_MODE {a.mode}")
last_toggle = time.monotonic()
period = a.toggle
active = a.mode == "active"
last_report = time.monotonic()
while time.monotonic() < end and not stopped.is_set():
await asyncio.sleep(0.05)
now = time.monotonic()
if a.toggle and now - last_toggle >= period:
active = not active
c.bluetooth_scanner_set_mode(
BluetoothScannerMode.ACTIVE
if active
else BluetoothScannerMode.PASSIVE
)
w(f"SET_MODE {'active' if active else 'passive'}")
last_toggle = now
period = a.toggle + random.uniform(-a.jitter, a.jitter)
if now - last_report >= 10:
w(f"ADV {adv[0]} in {now - last_report:.1f}s devices={len(macs)}")
adv[0] = 0
macs.clear()
last_report = now
if stopped.is_set():
w("DISCONNECTED")
await asyncio.sleep(2)
continue
unsub_raw()
unsub_state()
await c.disconnect()
w("DONE")
if not connected:
sys.exit("no API connection during the whole soak")
if __name__ == "__main__":
main()
@@ -0,0 +1,78 @@
substitutions:
name: scan2610
duration: 5s
esphome:
name: ${name}
esp32:
board: esp32-c6-devkitc-1
variant: esp32c6
flash_size: 4MB
framework:
type: esp-idf
logger:
level: VERBOSE
hardware_uart: USB_SERIAL_JTAG
logs:
esp32_ble_tracker: VERBOSE
esp32_ble: DEBUG
bluetooth_proxy: DEBUG
api: DEBUG
api.connection: DEBUG
api.service: DEBUG
sensor: INFO
text_sensor: INFO
debug: INFO
wifi: DEBUG
scheduler: DEBUG
component: DEBUG
ota:
- platform: esphome
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
esp32_ble_tracker:
scan_parameters:
interval: 1100ms
window: 1100ms
active: true
duration: ${duration}
bluetooth_proxy:
active: true
sensor:
- platform: wifi_signal
name: WiFi signal
update_interval: 60s
- platform: uptime
name: Uptime
- platform: debug
free:
name: Free memory
block:
name: Largest free block
min_free:
name: Lowest free memory
loop_time:
name: Loop time
debug:
update_interval: 60s
text_sensor:
- platform: version
name: ESPHome version
- platform: debug
reset_reason:
name: Reset reason
api:
reboot_timeout: 0s
encryption:
key: !secret api_key
@@ -0,0 +1,126 @@
#pragma once
// esphome#22770 instrumentation: follows one scan's duration timer through Bluedroid.
// Link with -Wl,--wrap= for BTM_BleScan, btu_start_timer, btm_ble_timeout, osi_alarm_new, btc_transfer_context.
#include <cstdint>
#include <esp_gap_ble_api.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
extern "C" {
struct scan_inst_t {
uint32_t ble_scan_start; // BTM_BleScan(TRUE) calls
uint32_t ble_scan_start_bad; // ... that did not return BTM_CMD_STARTED
uint32_t ble_scan_stop; // BTM_BleScan(FALSE) calls
uint32_t timer_start; // btu_start_timer(BTU_TTYPE_BLE_SCAN)
uint32_t alarm_fire; // esp_timer fired for the scan timer entry (btc thread)
uint32_t ble_timeout; // btm_ble_timeout for the scan timer entry (btu thread)
uint32_t inq_cmpl_post; // INQ_CMPL handed to btc_transfer_context
uint32_t inq_cmpl_post_fail; // ... that failed
uint8_t pid;
void *scan_tle;
int64_t last_timer_start_us;
int64_t last_fire_us;
int64_t last_timeout_us;
int64_t last_inq_cmpl_us;
};
// Written from the Bluedroid and timer tasks, read on the main loop: every access holds the lock, since the 64 bit
// timestamps can tear on the 32 bit chips. Read with scan_inst_snapshot() and log the copy.
static scan_inst_t g_scan_inst{};
static portMUX_TYPE g_scan_inst_mux = portMUX_INITIALIZER_UNLOCKED;
static scan_inst_t scan_inst_snapshot() {
portENTER_CRITICAL(&g_scan_inst_mux);
scan_inst_t copy = g_scan_inst;
portEXIT_CRITICAL(&g_scan_inst_mux);
return copy;
}
typedef void (*scan_inst_cb_t)(void *);
// the scan timer entry's original alarm callback (btu_general_alarm_cb)
static scan_inst_cb_t s_orig_cb = nullptr;
static void scan_inst_shim(void *data) {
int64_t now = esp_timer_get_time();
portENTER_CRITICAL(&g_scan_inst_mux);
g_scan_inst.alarm_fire++;
g_scan_inst.last_fire_us = now;
portEXIT_CRITICAL(&g_scan_inst_mux);
s_orig_cb(data);
}
uint8_t __real_BTM_BleScan(uint8_t start, uint32_t duration, void *a, void *b, void *c);
uint8_t __wrap_BTM_BleScan(uint8_t start, uint32_t duration, void *a, void *b, void *c) {
uint8_t r = __real_BTM_BleScan(start, duration, a, b, c);
portENTER_CRITICAL(&g_scan_inst_mux);
if (start) {
g_scan_inst.ble_scan_start++;
if (r != 1 /* BTM_CMD_STARTED */)
g_scan_inst.ble_scan_start_bad++;
} else {
g_scan_inst.ble_scan_stop++;
}
portEXIT_CRITICAL(&g_scan_inst_mux);
return r;
}
void __real_btu_start_timer(void *p_tle, uint16_t type, uint32_t timeout_sec);
void __wrap_btu_start_timer(void *p_tle, uint16_t type, uint32_t timeout_sec) {
if (type == 109 /* BTU_TTYPE_BLE_SCAN */) {
int64_t now = esp_timer_get_time();
portENTER_CRITICAL(&g_scan_inst_mux);
g_scan_inst.scan_tle = p_tle;
g_scan_inst.timer_start++;
g_scan_inst.last_timer_start_us = now;
portEXIT_CRITICAL(&g_scan_inst_mux);
}
__real_btu_start_timer(p_tle, type, timeout_sec);
}
void __real_btm_ble_timeout(void *p_tle);
void __wrap_btm_ble_timeout(void *p_tle) {
int64_t now = esp_timer_get_time();
portENTER_CRITICAL(&g_scan_inst_mux);
if (p_tle == g_scan_inst.scan_tle) {
g_scan_inst.ble_timeout++;
g_scan_inst.last_timeout_us = now;
}
portEXIT_CRITICAL(&g_scan_inst_mux);
__real_btm_ble_timeout(p_tle);
}
void *__real_osi_alarm_new(const char *name, scan_inst_cb_t cb, void *data, uint64_t expire);
void *__wrap_osi_alarm_new(const char *name, scan_inst_cb_t cb, void *data, uint64_t expire) {
// btu_start_timer's wrap records the scan entry before the real one creates its alarm. Shim every alarm made for
// it, since Bluedroid may free and recreate it; they all use the same callback (btu_general_alarm_cb).
portENTER_CRITICAL(&g_scan_inst_mux);
void *scan_tle = g_scan_inst.scan_tle;
portEXIT_CRITICAL(&g_scan_inst_mux);
if (data != nullptr && data == scan_tle) {
s_orig_cb = cb;
return __real_osi_alarm_new(name, scan_inst_shim, data, expire);
}
return __real_osi_alarm_new(name, cb, data, expire);
}
struct scan_inst_msg_t {
uint8_t sig, aid, pid, act;
};
int __real_btc_transfer_context(scan_inst_msg_t *msg, void *arg, int arg_len, void *cp, void *fr);
int __wrap_btc_transfer_context(scan_inst_msg_t *msg, void *arg, int arg_len, void *cp, void *fr) {
bool cmpl = msg != nullptr && arg != nullptr && msg->sig == 1 && msg->act == ESP_GAP_BLE_SCAN_RESULT_EVT &&
arg_len >= 4 && msg->pid == 5 /* BTC_PID_GAP_BLE */ &&
static_cast<esp_ble_gap_cb_param_t *>(arg)->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT;
int r = __real_btc_transfer_context(msg, arg, arg_len, cp, fr);
if (cmpl) {
int64_t now = esp_timer_get_time();
portENTER_CRITICAL(&g_scan_inst_mux);
g_scan_inst.pid = msg->pid;
g_scan_inst.inq_cmpl_post++;
g_scan_inst.last_inq_cmpl_us = now;
if (r != 0)
g_scan_inst.inq_cmpl_post_fail++;
portEXIT_CRITICAL(&g_scan_inst_mux);
}
return r;
}
}
@@ -0,0 +1,40 @@
# scan.yaml plus the Bluedroid scan timer counters (scan_inst.hpp) logged every 30 s as `scaninst`
substitutions:
name: scaninst
packages:
base: !include scan.yaml
esphome:
includes:
- scan_inst.hpp
platformio_options:
build_flags:
- -Wl,--wrap=BTM_BleScan
- -Wl,--wrap=btu_start_timer
- -Wl,--wrap=btm_ble_timeout
- -Wl,--wrap=osi_alarm_new
- -Wl,--wrap=btc_transfer_context
esp32_ble_tracker:
on_scan_end:
- lambda: id(scan_ends)++;
globals:
- id: scan_ends
type: uint32_t
initial_value: '0'
interval:
- interval: 30s
then:
- lambda: |-
const scan_inst_t g = scan_inst_snapshot();
int64_t now = esp_timer_get_time();
ESP_LOGI("scaninst", "start=%u bad=%u stop=%u timer=%u fire=%u timeout=%u cmpl_post=%u cmpl_fail=%u pid=%u ends=%u "
"ago_ms timer=%lld fire=%lld timeout=%lld cmpl=%lld",
(unsigned) g.ble_scan_start, (unsigned) g.ble_scan_start_bad, (unsigned) g.ble_scan_stop,
(unsigned) g.timer_start, (unsigned) g.alarm_fire, (unsigned) g.ble_timeout,
(unsigned) g.inq_cmpl_post, (unsigned) g.inq_cmpl_post_fail, (unsigned) g.pid, (unsigned) id(scan_ends),
(now - g.last_timer_start_us) / 1000, (now - g.last_fire_us) / 1000,
(now - g.last_timeout_us) / 1000, (now - g.last_inq_cmpl_us) / 1000);
@@ -0,0 +1,15 @@
#!/bin/bash
# usage: soak.sh TAG HOST SECONDS [ha.py options] serial capture plus HA stand-in, both bounded
# env: PORT (serial port), PY, API_KEY (falls back to apikey.txt next to the script; unset or - for plaintext)
W=$(cd "$(dirname "$0")" && pwd); cd "$W" || exit 1; T=$1; H=$2; S=$3; shift 3
PY=${PY:-python3}
PORT=${PORT:?serial port}
[ -z "${API_KEY+x}" ] && [ -f apikey.txt ] && API_KEY=$(cat apikey.txt)
$PY "$(dirname "$W")/serial_capture.py" "$PORT" "$S" "$T.txt" > /dev/null 2>&1 &
CAP=$!
API_KEY=$API_KEY PYTHONUNBUFFERED=1 $PY ha.py "$H" "$T-ha.txt" "$S" "$@" > "$T-ha.err" 2>&1 &
HA=$!
wait $CAP; CRC=$?
wait $HA; HRC=$?
echo "$T done: capture rc=$CRC ha.py rc=$HRC (see $T-ha.err)"
[ $CRC -eq 0 ] && [ $HRC -eq 0 ]
@@ -0,0 +1,11 @@
#!/bin/bash
# usage: wait.sh CAPTURE SECONDS [FROM_LINE] block until an anomaly after FROM_LINE or SECONDS pass, then print status
C=$1; L=${3:-1}
P="Scan timeout exceeded|Scan never terminated|Dropped [0-9]+ BLE|Guru|Backtrace|Unexpected|cmpl_fail=[1-9]|bad=[1-9]"
timeout "$2" tail -n +"$L" -F "$C" 2> /dev/null | grep -a -m1 -q -E "$P"
tail -n +"$L" "$C" | awk -v p="$P" -v t="$(date +%T)" '
$0 ~ p && shown < 5 { print; shown++ }
/Starting scan/ { starts++ }
/Scan complete, set/ { completes++ }
END { printf "%s starts=%d completes=%d\n", t, starts, completes }'
grep -a scaninst "$C" | tail -1 | cut -c1-260
@@ -0,0 +1,116 @@
#ifndef FUZZ22773_CLIMATE_FUZZ_H
#define FUZZ22773_CLIMATE_FUZZ_H
// Test only (esphome#22773 A/B): feeds a climate's own frames, mutated copies and random frames to on_receive
// and prints whether each was accepted and the state that followed.
#include <cstdio>
#include <random>
#include <vector>
#include "esphome/components/climate_ir/climate_ir.h"
#include "esphome/components/remote_base/aeha_protocol.h"
#include "esphome/components/remote_base/remote_base.h"
#include "esphome/core/application.h"
#include "esphome/core/preferences.h"
namespace esphome::fuzz22773 {
class Cap : public remote_base::RemoteTransmitterBase {
public:
Cap() : RemoteTransmitterBase(nullptr) {}
std::vector<remote_base::RawTimings> frames;
protected:
void send_internal(uint32_t, uint32_t) override { this->frames.push_back(this->temp_.get_data()); }
};
template<typename C> void prep(C &c, Cap &tx) {
if (global_preferences != nullptr)
global_preferences->reset();
static char name[] = "fuzz";
static char fn[] = "";
App.pre_setup(name, sizeof(name) - 1, fn, 0);
c.set_transmitter(&tx);
static_cast<Component &>(c).setup();
}
template<typename C> void print_state(const char *tag, int i, bool ok, C &c) {
printf("FUZZ %s %d ok=%d mode=%d t=%.2f fan=%d swing=%d\n", tag, i, ok ? 1 : 0, (int) c.mode,
(double) c.target_temperature, c.fan_mode.has_value() ? (int) *c.fan_mode : -1, (int) c.swing_mode);
}
// A random AEHA frame of len bytes; half start with the climate's header (set_header), 3 in 4 use its address
template<typename Header>
void aeha_random(std::mt19937 &rng, remote_base::RawTimings &raw, size_t len, uint16_t address, Header set_header) {
uint8_t d[35];
for (auto &b : d)
b = rng();
if (rng() % 2)
set_header(d);
remote_base::RemoteTransmitData tx;
remote_base::AEHAProtocol().encode(&tx, (rng() % 4) ? address : uint16_t(rng()), d, len);
raw = tx.get_data();
}
// make() returns a fresh climate; random_frame(rng, raw) fills raw with a random frame of the protocol, without the
// trailing idle gap
template<typename Make, typename Rand> void run(const char *tag, Make make, Rand random_frame) {
std::mt19937 rng(22773);
// Valid frames: everything the climate itself sends
std::vector<remote_base::RawTimings> valid;
{
Cap tx;
auto c = make();
prep(*c, tx);
for (auto mode : {climate::CLIMATE_MODE_OFF, climate::CLIMATE_MODE_COOL, climate::CLIMATE_MODE_HEAT,
climate::CLIMATE_MODE_DRY, climate::CLIMATE_MODE_FAN_ONLY, climate::CLIMATE_MODE_HEAT_COOL})
for (float t : {16.0f, 18.0f, 21.0f, 24.0f, 27.0f, 30.0f, 60.0f, 66.0f, 72.0f, 80.0f, 88.0f})
for (auto fan : {climate::CLIMATE_FAN_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM,
climate::CLIMATE_FAN_HIGH}) {
auto call = c->make_call();
call.set_mode(mode);
call.set_target_temperature(t);
call.set_fan_mode(fan);
call.perform();
}
valid = tx.frames;
for (auto &v : valid)
v.push_back(-20000); // the idle gap a receiver records after the last mark
}
int i = 0;
auto feed = [&](const remote_base::RawTimings &raw, const char *kind) {
Cap tx;
auto c = make();
prep(*c, tx);
remote_base::RemoteReceiveData data(raw, 25, remote_base::TOLERANCE_MODE_PERCENTAGE);
bool ok = static_cast<remote_base::RemoteReceiverListener &>(*c).on_receive(data);
printf("%s ", kind);
print_state(tag, i++, ok, *c);
};
for (const auto &v : valid) {
feed(v, "valid");
for (int m = 0; m < 4; m++) {
auto raw = v;
if (raw.size() < 8)
continue;
// a space swapped with another space's length flips a bit (or does nothing when they are equal)
size_t a = 3 + rng() % (raw.size() - 3), b = 3 + rng() % (raw.size() - 3);
if (raw[a] < 0 && raw[b] < 0)
std::swap(raw[a], raw[b]);
else
raw[a] = raw[a] < 0 ? -int32_t(100 + rng() % 3000) : int32_t(100 + rng() % 3000);
feed(raw, "mut");
}
auto t = v;
t.resize(rng() % t.size());
feed(t, "trunc");
}
for (int r = 0; r < 3000; r++) {
remote_base::RawTimings raw;
random_frame(rng, raw);
raw.push_back(-20000);
feed(raw, "rand");
}
fflush(stdout);
}
} // namespace esphome::fuzz22773
#endif
@@ -0,0 +1,17 @@
#include <gtest/gtest.h>
#include <memory>
#include "climate_fuzz.hpp"
#include "esphome/components/friedrich/friedrich.h"
namespace esphome::fuzz22773 {
TEST(Fuzz22773, Friedrich) {
run("friedrich", [] { return std::make_unique<friedrich::FriedrichClimate>(); },
[](std::mt19937 &rng, remote_base::RawTimings &raw) {
size_t len = (rng() % 3 == 0) ? 2 + rng() % 34 : ((rng() & 1) ? 14 : 5);
aeha_random(rng, raw, len, 0x28C6, [](uint8_t *d) {
d[0] = 0x00;
d[1] = 0x08;
d[2] = 0x08;
});
});
}
} // namespace esphome::fuzz22773
@@ -0,0 +1,19 @@
#include <gtest/gtest.h>
#include <memory>
#include "climate_fuzz.hpp"
#include "esphome/components/haier_tundra/haier_tundra.h"
#include "esphome/components/remote_base/haier_protocol.h"
namespace esphome::fuzz22773 {
TEST(Fuzz22773, HaierTundra) {
run("haier_tundra", [] { return std::make_unique<haier_tundra::HaierTundra>(haier_tundra::HAIER_HEADER_UNIT_A); },
[](std::mt19937 &rng, remote_base::RawTimings &raw) {
uint8_t d[16];
size_t len = (rng() % 3 == 0) ? 1 + rng() % 16 : 13;
for (auto &b : d) b = rng();
if (rng() % 2) d[0] = haier_tundra::HAIER_HEADER_UNIT_A;
remote_base::RemoteTransmitData tx;
remote_base::HaierProtocol().encode(&tx, d, len);
raw = tx.get_data();
});
}
} // namespace esphome::fuzz22773
@@ -0,0 +1,12 @@
from esphome.components import remote_base
from esphome.types import ConfigType
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
manifest.auto_load = ["climate_ir", "climate"]
async def to_code_testing(config: ConfigType) -> None:
remote_base.request_protocol("haier")
manifest.to_code = to_code_testing
@@ -0,0 +1,16 @@
#include <gtest/gtest.h>
#include <memory>
#include "climate_fuzz.hpp"
#include "esphome/components/mistral_ir/mistral_ir.h"
namespace esphome::fuzz22773 {
TEST(Fuzz22773, MistralIR) {
run("mistral", [] { return std::make_unique<mistral_ir::MistralIR>(); },
[](std::mt19937 &rng, remote_base::RawTimings &raw) {
size_t len = (rng() % 3 == 0) ? 2 + rng() % 34 : 12;
aeha_random(rng, raw, len, mistral_ir::MISTRAL_ADDRESS, [](uint8_t *d) {
d[0] = 0x56;
d[2] = 0x08;
});
});
}
} // namespace esphome::fuzz22773
@@ -0,0 +1,28 @@
# Network, API and the VERBOSE logger setup shared by the climate loopback configs.
esphome:
name: ${name}
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
logger:
level: VERBOSE
logs:
wifi: DEBUG
api: DEBUG
api.service: DEBUG
api.connection: DEBUG
scheduler: DEBUG
component: DEBUG
esp32.preferences: DEBUG
esp8266.preferences: DEBUG
preferences: DEBUG
mdns: DEBUG
sensor: DEBUG
remote_receiver: DEBUG
remote_transmitter: DEBUG
climate: DEBUG
api:
reboot_timeout: 0s
ota:
- platform: esphome
@@ -0,0 +1,12 @@
# Samsung, Friedrich, Siemens IRA211 and Mistral senders and receivers on one loopback pin.
packages:
base: !include base.yaml
climate:
- {platform: samsung, id: sam_tx, name: SAM TX, transmitter_id: irtx}
- {platform: samsung, id: sam_rx, name: SAM RX, transmitter_id: irtx, receiver_id: irrx}
- {platform: friedrich, id: fr_tx, name: FR TX, transmitter_id: irtx}
- {platform: friedrich, id: fr_rx, name: FR RX, transmitter_id: irtx, receiver_id: irrx}
- {platform: siemens_ira211, id: si_tx, name: SI TX, transmitter_id: irtx}
- {platform: siemens_ira211, id: si_rx, name: SI RX, transmitter_id: irtx, receiver_id: irrx}
- {platform: mistral_ir, id: mi_tx, name: MI TX, transmitter_id: irtx}
- {platform: mistral_ir, id: mi_rx, name: MI RX, transmitter_id: irtx, receiver_id: irrx}
@@ -0,0 +1,5 @@
substitutions:
name: c32pr
packages:
common: !include c-common.yaml
board: !include esp32-ir.yaml
@@ -0,0 +1,98 @@
"""compare.py A B [TOL] [--flat]: compare two runs (A-frames.json, B-frames.json from frames.py).
Per step (default): frames equal within TOL us and RX states equal.
--flat: the whole frame sequence in order, timing by timing, ignoring step boundaries (for the D1 mini, where a log
line can land in the next step); reports every item over TOL and the largest deviation elsewhere.
"""
import json
import pathlib
import sys
args = [a for a in sys.argv[1:] if a != "--flat"]
FLAT = "--flat" in sys.argv[1:]
TOL = int(args[2]) if len(args) > 2 else 0
def load(name):
steps = json.loads(pathlib.Path(f"{name}-frames.json").read_text(encoding="utf-8"))
# an empty run must not compare as equal to another empty run
if not steps or not any(s["frames"] for s in steps):
sys.exit(f"{name}-frames.json has no steps or no frames")
return steps
def same_timing(u, v):
return (u > 0) == (v > 0) and abs(u - v) <= TOL
def frame_dev(p, q):
"""Largest deviation of two frames, or None when they differ in length or a timing is over TOL."""
if len(p) != len(q) or not all(
same_timing(u, v) for u, v in zip(p, q, strict=False)
):
return None
return max((abs(u - v) for u, v in zip(p, q, strict=False)), default=0)
def flat(a, b):
fa = [f for s in a for f in s["frames"]]
fb = [f for s in b for f in s["frames"]]
mx = bad = items = 0
print("frames", len(fa), len(fb))
if len(fa) != len(fb):
print("DIFF frame counts differ (a lost or extra transmission)")
bad += 1
for i, (p, q) in enumerate(zip(fa, fb, strict=False)):
if len(p) != len(q):
print("frame", i, "length", len(p), len(q))
bad += 1
continue
for j, (u, v) in enumerate(zip(p, q, strict=False)):
items += 1
if same_timing(u, v):
mx = max(mx, abs(u - v))
else:
print("frame", i, "item", j, u, v)
bad += 1
print(f"items {items} over_tol {bad} max_dev_within_tol {mx}")
return bad == 0
def per_step(a, b):
if len(a) != len(b):
print(f"DIFF step counts {len(a)} vs {len(b)} (an interrupted run?)")
sys.exit(1)
same_f = same_s = maxdev = 0
for x, y in zip(a, b, strict=False):
assert x["step"] == y["step"]
fa, fb = x["frames"], y["frames"]
devs = [frame_dev(p, q) for p, q in zip(fa, fb, strict=False)]
ok = len(fa) == len(fb) and None not in devs
if ok:
maxdev = max([maxdev, *devs])
same_f += ok
st = x["rx"] == y["rx"]
same_s += st
if not ok or not st:
print(
"DIFF",
x["step"],
"frames",
len(fa),
len(fb),
[len(p) for p in fa],
[len(q) for q in fb],
)
for k in x["rx"]:
if x["rx"][k] != y["rx"].get(k):
print(" ", k, x["rx"][k], "|", y["rx"].get(k))
print(
f"steps {len(a)}/{len(b)} frames_equal {same_f} rx_states_equal {same_s} "
f"total_frames {sum(len(s['frames']) for s in a)}/{sum(len(s['frames']) for s in b)} max_dev_us {maxdev}"
)
return same_f == len(a) and same_s == len(a)
if not (flat if FLAT else per_step)(load(args[0]), load(args[1])):
sys.exit(1)
@@ -0,0 +1,74 @@
"""drive.py IP STEP...: STEP is 'Name:mode=cool,t=24,fan=low,swing=vertical' or 'svc:action'; prints climate states after each."""
import asyncio
import os
import pathlib
import sys
import time
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from aioesphomeapi import (
ClimateFanMode,
ClimateInfo,
ClimateMode,
ClimatePreset,
ClimateSwingMode,
)
from apiconn import connect
MODES = {m.name.lower(): m for m in ClimateMode}
FANS = {m.name.lower(): m for m in ClimateFanMode}
SWINGS = {m.name.lower(): m for m in ClimateSwingMode}
PRESETS = {m.name.lower(): m for m in ClimatePreset}
async def main() -> None:
c = await connect(sys.argv[1])
ents, svcs = await c.list_entities_services()
clim = {e.name: e for e in ents if isinstance(e, ClimateInfo)}
byk = {e.key: e.name for e in clim.values()}
st = {}
c.subscribe_states(
lambda s: st.__setitem__(byk[s.key], s) if s.key in byk else None
)
await asyncio.sleep(2)
def show(tag):
for n in sorted(st):
s = st[n]
print(
f" {tag} {n:11s} mode={s.mode.name} target={s.target_temperature:g} fan={s.fan_mode.name} swing={s.swing_mode.name} preset={s.preset.name}"
)
show("init")
for step in sys.argv[2:]:
name, _, args = step.partition(":")
print(time.strftime("%H:%M:%S"), "STEP", step, flush=True)
if name == "svc":
sname, _, sargs = args.partition("?")
data = {
k: v == "1"
for k, v in (a.split("=") for a in filter(None, sargs.split("&")))
}
await c.execute_service(next(s for s in svcs if s.name == sname), data)
else:
kw = {}
for a in filter(None, args.split(",")):
k, v = a.split("=")
if k == "mode":
kw["mode"] = MODES[v]
elif k == "t":
kw["target_temperature"] = float(v)
elif k == "fan":
kw["fan_mode"] = FANS[v]
elif k == "swing":
kw["swing_mode"] = SWINGS[v]
elif k == "preset":
kw["preset"] = PRESETS[v]
c.climate_command(clim[name].key, **kw)
await asyncio.sleep(float(os.environ.get("STEP_S", "2.5")))
show("after")
await c.disconnect()
asyncio.run(main())
@@ -0,0 +1,24 @@
# One-pin IR loopback on an ESP32 devkit: transmitter and receiver share GPIO27, RMT reads its own output.
# Override the substitutions (or `dump:`) from the including config.
substitutions:
pin: GPIO27
rmt_symbols: "384"
receive_symbols: "384"
esp32:
board: esp32dev
framework:
type: esp-idf
remote_transmitter:
id: irtx
pin:
number: ${pin}
allow_other_uses: true
carrier_duty_percent: 100%
remote_receiver:
id: irrx
pin:
number: ${pin}
allow_other_uses: true
dump: raw
rmt_symbols: ${rmt_symbols}
receive_symbols: ${receive_symbols}
@@ -0,0 +1,23 @@
# One-pin IR loopback on a D1 mini: transmitter and receiver share GPIO12. The receiver's setup leaves the shared pin
# an input, so an interval drives it as an output again. Override the substitutions (or `dump:`) from the including
# config.
substitutions:
pin: "12"
esp8266:
board: d1_mini
remote_transmitter:
id: irtx
pin:
number: ${pin}
allow_other_uses: true
carrier_duty_percent: 100%
remote_receiver:
id: irrx
pin:
number: ${pin}
allow_other_uses: true
dump: raw
interval:
- interval: 250ms
then:
- lambda: pinMode(${pin}, OUTPUT);
@@ -0,0 +1,53 @@
"""frames.py NAME: per step, the raw frames and the RX states. Prints JSON to NAME-frames.json."""
import json
import pathlib
import re
import sys
n = sys.argv[1]
steps = []
cur = None
with pathlib.Path(f"{n}-drive.txt").open(encoding="utf-8") as fh:
for text in fh:
m = re.match(r"(\d\d:\d\d:\d\d) STEP (.*)", text)
if m:
cur = {"t": m[1], "step": m[2], "rx": {}, "tx": {}, "frames": []}
steps.append(cur)
m = re.match(r"\s+after (\S+ \S+)\s+(.*)", text)
if m and cur:
(cur["rx"] if m[1].endswith("RX") else cur["tx"])[m[1]] = m[2]
frames = []
buf = None
with pathlib.Path(f"{n}-ser.txt").open(encoding="utf-8", errors="replace") as fh:
for text in fh:
m = re.match(r"\[(\d\d:\d\d:\d\d)\]\[I\]\[remote\.raw(?::\d+)?\]: ?(.*)", text)
if not m:
continue
ts, body = m[1], m[2]
if body.startswith("Received Raw:"):
buf = [ts, body[len("Received Raw:") :]]
elif buf is not None:
buf[1] += body
# the raw dumper splits long frames into lines that end with a comma; the last line does not
if buf is not None and not body.rstrip().endswith(","):
frames.append(
(
buf[0],
[
int(x)
for x in buf[1].replace(" ", "").strip(",").split(",")
if x
],
)
)
buf = None
for ts, f in frames:
owner = None
for s in steps:
if s["t"] <= ts:
owner = s
if owner:
owner["frames"].append(f)
pathlib.Path(f"{n}-frames.json").write_text(json.dumps(steps), encoding="utf-8")
print(n, "steps", len(steps), "frames", sum(len(s["frames"]) for s in steps))
@@ -0,0 +1,30 @@
#!/bin/bash
# run.sh NAME PORT [STEPS_FILE]: serial capture with a reset, then the prelude and steps of STEPS_FILE (default
# steps.sh here, which sets the PRE and STEPS arrays) through drive.py; writes NAME-ser.txt, NAME-pre.txt and
# NAME-drive.txt. env: PY, BAUD (default 115200; use 921600 for long climate frames), STEP_S (default 2.5, the wait
# after each step; the prelude always uses 2.5 s since it is not compared)
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); N=${1:?name}; P=${2:?port}; PY=${PY:-python3}
# shellcheck source=/dev/null
source "${3:-$D/steps.sh}"
cd "$D" || exit 1
pkill -f "[s]erial_capture.py $P "; sleep 1
# a stale capture would feed the IP lookup below
rm -f "$D/$N-ser.txt"
nohup $PY "$S/serial_capture.py" "$P" 1000 "$D/$N-ser.txt" --reset --baud "${BAUD:-115200}" > /dev/null 2>&1 &
IP=
for _ in $(seq 1 60); do
IP=$(LC_ALL=C grep -a -o -E "IP Address: [0-9.]+" "$N-ser.txt" 2>/dev/null | tail -1 | cut -d' ' -f3)
[ -n "$IP" ] && break; sleep 2
done
[ -z "$IP" ] && { echo "no IP in $N-ser.txt"; pkill -f "[s]erial_capture.py $P "; exit 1; }
sleep 3
STEP_S=2.5 $PY drive.py "$IP" "${PRE[@]}" > "$N-pre.txt" 2>&1; PRC=$?
STEP_S=${STEP_S:-2.5} $PY drive.py "$IP" "${STEPS[@]}" > "$N-drive.txt" 2>&1; DRC=$?
sleep 2; pkill -f "[s]erial_capture.py $P "; sleep 1
echo "$N steps=$(grep -c STEP "$N-drive.txt") raw=$(LC_ALL=C grep -a -c 'Received Raw' "$N-ser.txt")" \
"boots=$(LC_ALL=C grep -a -c 'Running through setup\|rst cause' "$N-ser.txt")"
# a failed drive leaves a partial run: fail so frames.py and compare.py are not run on it
if [ $PRC -ne 0 ] || [ $DRC -ne 0 ]; then
echo "$N drive failed: prelude rc=$PRC steps rc=$DRC (see $N-pre.txt, $N-drive.txt)"
exit 1
fi
@@ -0,0 +1,14 @@
# prelude puts every sender in the same state on both builds; frames before the first MARK step are not compared
PRE=("SAM TX:mode=off,t=24,fan=auto,swing=off" "FR TX:mode=off,t=72,fan=auto,swing=off" "SI TX:mode=off,t=21,fan=auto,preset=comfort" "MI TX:mode=off,t=24,fan=auto")
STEPS=()
for p in "SAM TX" "FR TX" "MI TX"; do
for m in heat_cool cool dry fan_only heat off; do STEPS+=("${p}:mode=$m"); done
STEPS+=("${p}:mode=cool")
if [ "$p" = "FR TX" ]; then fans=(high medium low quiet auto); else fans=(low medium high auto); fi
for f in "${fans[@]}"; do STEPS+=("${p}:fan=$f"); done
[ "$p" = "SAM TX" ] && for s in vertical horizontal both off; do STEPS+=("${p}:swing=$s"); done
STEPS+=("${p}:mode=off")
done
STEPS+=("SI TX:mode=heat_cool" "SI TX:preset=eco" "SI TX:preset=comfort")
for f in low medium high auto; do STEPS+=("SI TX:fan=$f"); done
STEPS+=("SI TX:mode=off")
@@ -0,0 +1,52 @@
# API actions that send whirlpool frames the sender cannot produce; add a board package and whirlpool/cl.yaml.
packages:
base: !include base.yaml
remote_receiver:
# a short frame ends in the idle gap, which the decoder takes as the second divider only up to 9,950 us
# (7,960 + 25%); the 9 ms header mark must stay below idle
idle: 9500us
api:
actions:
# A Hitachi frame the sender cannot produce: temperature nibble 15 (16 + 15 = 31 C, above the 30 C max), cool, fan low
- action: inject_t15
then:
- lambda: |-
static constexpr remote_base::BitTimings T{.bit_mark = 572, .one_space = 1659, .zero_space = 553};
uint8_t s[21] = {0};
s[0] = 0xA9; s[1] = 0x59; s[2] = 0x03; s[3] = 0xF2; s[6] = 0x80; s[10] = 0x20; s[15] = 0x02;
s[18] = 0x38; s[19] = 0x08;
s[13] = xor_bytes(&s[2], 11);
s[20] = xor_bytes(&s[14], 6);
ESP_LOGI("w22704", "inject t15");
auto call = id(irtx).transmit();
auto *d = call.get_data();
d->set_carrier_frequency(38000);
d->mark(9000); d->space(4494);
for (int i = 0; i < 21; i++) {
if (i == 6 || i == 14) { d->mark(572); d->space(7960); }
d->item_bytes<T>(s + i, 1);
}
d->mark(572);
call.perform();
# A 14-byte frame (what some whirlpool remotes send): bytes 0 to 13, the second divider, then silence
- action: inject_short
variables:
hitachi: bool
then:
- lambda: |-
static constexpr remote_base::BitTimings T{.bit_mark = 572, .one_space = 1659, .zero_space = 553};
uint8_t s[14] = {0};
s[0] = hitachi ? 0xA9 : 0x83; s[1] = hitachi ? 0x59 : 0x06; s[2] = 0x03; s[3] = 0xE2; s[6] = 0x80;
if (hitachi) s[10] = 0x20;
s[13] = xor_bytes(&s[2], 11);
ESP_LOGI("w22704", "inject short hitachi=%d", (int) hitachi);
auto call = id(irtx).transmit();
auto *d = call.get_data();
d->set_carrier_frequency(38000);
d->mark(9000); d->space(4494);
for (int i = 0; i < 14; i++) {
if (i == 6) { d->mark(572); d->space(7960); }
d->item_bytes<T>(s + i, 1);
}
d->mark(572);
call.perform();
@@ -0,0 +1,29 @@
#!/bin/bash
# all.sh PORT BASE_TREE PR_TREE: per whirlpool model (DG11J1-3A, HITACHI_HCRA31NEWH, HISENSE_ARG), build ../base.yaml
# plus cl.yaml plus the D1 mini one-pin loopback from the base tree, then the PR tree, flash, run the whirlpool steps and split the frames. Base
# runs first per model, so the board ends on a PR build. Compare the hex lines with `grep -a "Sending:\|Received:"`.
# env: PY, BUILD_LOCK (optional)
W=$(cd "$(dirname "$0")" && pwd); L=$(dirname "$W"); S=$(dirname "$L"); P=${1:?port}
export PIO_CORE=${PIO_CORE:-$W/pio}
FAILED=()
for mm in dg:DG11J1-3A hi:HITACHI_HCRA31NEWH hs:HISENSE_ARG; do
m=${mm%%:*}
for v in base pr; do
if [ $v = base ]; then T=${2:?base tree}; else T=${3:?pr tree}; fi
N=w$m$v
printf 'substitutions:\n name: %s\n model: %s\npackages:\n common: !include ../base.yaml\n model: !include cl.yaml\n board: !include ../esp8266-ir.yaml\n' \
"$N" "${mm#*:}" > "$W/$N.yaml"
echo "== $N"
# a skipped variant must not leave an older run's outputs behind for the comparison
rm -f "$L/$N-ser.txt" "$L/$N-pre.txt" "$L/$N-drive.txt" "$L/$N-frames.json"
TREE=$T bash "$S/pio_build.sh" compile "$N" "$W/$N.yaml" || { FAILED+=("$N:compile"); continue; }
TREE=$T bash "$S/pio_build.sh" upload "$N" "$W/$N.yaml" "$P" || { FAILED+=("$N:upload"); continue; }
RPY=${PY:-$T/venv/bin/python}
(cd "$L" && STEP_S=4 PY=$RPY bash run.sh "$N" "$P" "$W/steps.sh" && $RPY frames.py "$N") || FAILED+=("$N:run")
done
done
if [ ${#FAILED[@]} -gt 0 ]; then
echo "DONE with ${#FAILED[@]} failed variant(s): ${FAILED[*]}"
exit 1
fi
echo DONE
@@ -0,0 +1,3 @@
climate:
- {platform: whirlpool, id: w_tx, name: W TX, model: "${model}", transmitter_id: irtx}
- {platform: whirlpool, id: w_rx, name: W RX, model: "${model}", transmitter_id: irtx, receiver_id: irrx}
@@ -0,0 +1,6 @@
PRE=("W TX:mode=off,t=24,fan=auto,swing=off")
STEPS=()
for m in heat_cool heat cool dry fan_only off; do STEPS+=("W TX:mode=${m}"); done
STEPS+=("W TX:mode=cool")
for f in low medium high auto; do STEPS+=("W TX:fan=${f}"); done
STEPS+=("W TX:mode=off")
@@ -99,6 +99,9 @@ class Client(BaseClient):
for line in out["STATE"]:
name, _, value = line.partition(" = ")
self.last[name] = value
# SUB lines are printed sorted below; SUBORDER keeps the order the device sent them
for i, line in enumerate(out["SUB"]):
print(f" {label} SUBORDER {i:03d} {line}")
for kind in ("SUB", "STATE", "ACTION"):
for line in sorted(out[kind]) if kind == "SUB" else out[kind]:
print(f" {label} {kind} {line}")
@@ -0,0 +1,22 @@
"""lognorm.py [FILE...]: print ESPHome log lines without ANSI colours, the capture's `[hh:mm:ss]` or
`[hh:mm:ss.mmm]` timestamp and the `:NNN` source line number of the tag, so two builds' logs can be diffed.
Reads stdin when no file is given. Scripts can import normalize() instead.
"""
import fileinput
import re
ANSI = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
STAMP = re.compile(r"^\[\d\d:\d\d:\d\d(?:\.\d+)?\]")
LINENO = re.compile(r"(\[[A-Z]\]\[[\w.\-]+):\d+\]")
def normalize(line: str) -> str:
return LINENO.sub(r"\1]", STAMP.sub("", ANSI.sub("", line)))
if __name__ == "__main__":
with fileinput.input(encoding="utf-8", errors="replace") as fh:
for raw in fh:
print(normalize(raw.rstrip("\n")))
@@ -0,0 +1,33 @@
esphome:
name: ${name}
esp32:
board: esp32-c3-devkitm-1
framework:
type: esp-idf
logger:
level: DEBUG
hardware_uart: USB_SERIAL_JTAG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
api:
reboot_timeout: 0s
encryption:
key: !secret api_key
ota:
- platform: esphome
spi:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO10
cc1101:
id: radio
cs_pin: GPIO3
frequency: 433.92MHz
remote_receiver:
pin: GPIO4
dump: all
interval:
- interval: 30s
then:
- lambda: ESP_LOGI("alive", "up %u heap %u", (unsigned) (millis() / 1000), (unsigned) esp_get_free_heap_size());
@@ -0,0 +1,22 @@
#!/bin/bash
# cmp.sh BASE_DEBUG PR_DEBUG BASE_VERBOSE PR_VERBOSE: compare host harness outputs from the first INPUT line on,
# with colours, timestamps and line numbers removed (../../lognorm.py). Writes db/dp/vb/vp.txt to the current directory.
PY=${PY:-python3}; LN=$(cd "$(dirname "$0")/../.." && pwd)/lognorm.py
TMP=$(mktemp -d); trap 'rm -rf "$TMP"' EXIT
[ $# -eq 4 ] || { echo "usage: cmp.sh BASE_DEBUG PR_DEBUG BASE_VERBOSE PR_VERBOSE"; exit 1; }
i=0
for f in "$1" "$2" "$3" "$4"; do
i=$((i + 1))
[ -s "$f" ] || { echo "missing or empty: $f"; exit 1; }
"$PY" "$LN" "$f" | awk '/^INPUT /{f=1} f' > "$TMP/$i"
# no INPUT line: the harness did not run, and two such files would compare as identical
[ -s "$TMP/$i" ] || { echo "no INPUT lines in $f"; exit 1; }
done
NSYM='s/^\[[DV]\]\[remote.beo4\]: Beo4: n_sym/[*][remote.beo4]: Beo4: n_sym/'
sed -E "$NSYM" "$TMP/3" > vb.txt
sed -E "$NSYM" "$TMP/4" > vp.txt
echo "VERBOSE diff (n_sym level mapped): $(diff vb.txt vp.txt | grep -c '^[<>]') of $(wc -l < vp.txt)"
grep -v 'Beo4: n_sym' "$TMP/1" > db.txt; cp "$TMP/2" dp.txt
echo "DEBUG diff (base n_sym dropped): $(diff db.txt dp.txt | grep -c '^[<>]') of $(wc -l < dp.txt)"
echo "n_sym: DEBUG base $(grep -c '\[D\]\[remote.beo4\]: Beo4: n_sym' "$TMP/1") pr $(grep -c 'Beo4: n_sym' dp.txt);" \
"VERBOSE base D $(grep -c '\[D\]\[remote.beo4\]: Beo4: n_sym' "$TMP/3") pr V $(grep -c '\[V\]\[remote.beo4\]: Beo4: n_sym' "$TMP/4")"
@@ -0,0 +1,44 @@
"""gen_dumpers.py TREE [OUT]: write OUT (default dumpers.h here) for rbh.hpp, one dumper per DECLARE_REMOTE_PROTOCOL(...) in TREE's
remote_base plus Pronto, RCSwitch and ABBWelcome (Raw is called by rbh.hpp only when nothing matched)."""
import pathlib
import re
import sys
rb = pathlib.Path(sys.argv[1]) / "esphome" / "components" / "remote_base"
headers, names = [], []
for h in sorted(rb.glob("*_protocol.h")):
found = re.findall(
r"^DECLARE_REMOTE_PROTOCOL\((\w+)\)",
h.read_text(encoding="utf-8"),
re.MULTILINE,
)
if found or h.name in (
"pronto_protocol.h",
"rc_switch_protocol.h",
"abbwelcome_protocol.h",
"raw_protocol.h",
):
headers.append(h.name)
names += found
names += [n for n in ("Pronto", "RCSwitch", "ABBWelcome") if n not in names]
out = ["#pragma once"]
out += [f'#include "esphome/components/remote_base/{h}"' for h in headers]
out += [
"using namespace esphome::remote_base;",
"struct RbDumper {",
" const char *name;",
" RemoteReceiverDumperBase *d;",
"};",
]
out += [f"static {n}Dumper rbd_{n.lower()};" for n in names]
out += ["static RbDumper RB_DUMPERS[] = {"]
out += [f' {{"{n}", &rbd_{n.lower()}}},' for n in names]
out += ["};", ""]
out_path = (
pathlib.Path(sys.argv[2])
if len(sys.argv) > 2
else pathlib.Path(__file__).with_name("dumpers.h")
)
out_path.write_text("\n".join(out), encoding="utf-8")
print(len(names), "dumpers")
@@ -0,0 +1,99 @@
esphome:
name: ${name}
on_boot:
priority: -100
then:
- logger.set_level: DEBUG
logger:
level: VERBOSE
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
api:
reboot_timeout: 0s
encryption:
key: !secret api_key
ota:
- platform: esphome
remote_transmitter:
id: irtx
pin:
number: ${pin}
allow_other_uses: true
carrier_duty_percent: 100%
remote_receiver:
pin:
number: ${pin}
allow_other_uses: true
dump: all
idle: 25ms
button:
- platform: template
name: TX all
on_press:
- script.execute: tx_all
- platform: template
name: Verbose
on_press:
- logger.set_level: VERBOSE
- platform: template
name: Debug
on_press:
- logger.set_level: DEBUG
script:
- id: tx_all
mode: single
then:
- logger.log: "TXMARK aok 1"
- lambda: ${prep}
- remote_transmitter.transmit_aok:
remote_id: 0x123456
channels: 0x00A1
command: 0x0B
- delay: 500ms
- logger.log: "TXMARK aok 2"
- lambda: ${prep}
- remote_transmitter.transmit_aok:
remote_id: 0xA5C381
channels: 0x1000
command: 0x43
- delay: 500ms
- logger.log: "TXMARK cardin 1"
- lambda: ${prep}
- remote_transmitter.transmit_cardin_s438:
code: "01001001110100"
channel: B
- delay: 700ms
- logger.log: "TXMARK cardin 2"
- lambda: ${prep}
- remote_transmitter.transmit_cardin_s438:
code: "11100011000101"
channel: A
- delay: 700ms
- logger.log: "TXMARK drayton 1"
- lambda: ${prep}
- remote_transmitter.transmit_drayton:
address: 0x1230
data: 0x8
- delay: 500ms
- logger.log: "TXMARK drayton 2"
- lambda: ${prep}
- remote_transmitter.transmit_drayton:
address: 0x1234
channel: 0x01
command: 0x05
- delay: 500ms
- logger.log: "TXMARK beo4 1"
- lambda: ${prep}
- remote_transmitter.transmit_beo4:
source: 0x01
command: 0x0C
- delay: 500ms
- logger.log: "TXMARK beo4 2"
- lambda: ${prep}
- remote_transmitter.transmit_beo4:
source: 0x00
command: 0x60
- delay: 500ms
- logger.log: "TXMARK end"
@@ -0,0 +1,240 @@
#pragma once
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <string>
#include <vector>
#include "dumpers.h"
static std::mt19937 rbh_rng(22814);
static uint32_t rbh_r() { return rbh_rng(); }
static RawDumper rbh_raw;
static void rbh_dump_all(const RawTimings &raw, uint32_t tol, ToleranceMode mode, const char *what, int i) {
printf("INPUT %d %s tol=%u mode=%d n=%u\n", i, what, (unsigned) tol, (int) mode, (unsigned) raw.size());
bool any = false;
for (auto &nd : RB_DUMPERS) {
RemoteReceiveData src(raw, tol, mode);
bool r = nd.d->dump(src);
if (r) {
printf("HIT %s\n", nd.name);
any = true;
}
}
if (!any) {
RemoteReceiveData src(raw, tol, mode);
rbh_raw.dump(src);
}
RemoteReceiveData s1(raw, tol, mode);
auto a = AOKProtocol().decode(s1);
if (a)
printf("AOK id=%06X ch=%04X cmd=%02X\n", (unsigned) a->remote_id, a->channels, a->command);
RemoteReceiveData s2(raw, tol, mode);
auto c = CardinS438Protocol().decode(s2);
if (c)
printf("CARDIN code=%04X ch=%u\n", c->code, c->channel);
RemoteReceiveData s3(raw, tol, mode);
auto d = DraytonProtocol().decode(s3);
if (d)
printf("DRAYTON addr=%04X ch=%u cmd=%u data=%u\n", d->address, d->channel, d->command, d->data);
RemoteReceiveData s4(raw, tol, mode);
auto b = Beo4Protocol().decode(s4);
if (b)
printf("BEO4 src=%u cmd=%u rep=%u\n", b->source, b->command, b->repeats);
}
struct Mode {
uint32_t tol;
ToleranceMode mode;
};
static const Mode RBH_MODES[] = {{25, TOLERANCE_MODE_PERCENTAGE},
{10, TOLERANCE_MODE_PERCENTAGE},
{150, TOLERANCE_MODE_TIME},
{60, TOLERANCE_MODE_TIME}};
// One mark or space nudged to just inside or just outside the tolerance of its own length
static int32_t rbh_edge(int32_t v, const Mode &m) {
int32_t n = v < 0 ? -v : v;
int32_t lo, hi;
if (m.mode == TOLERANCE_MODE_TIME) {
lo = n - (int32_t) m.tol;
hi = n + (int32_t) m.tol;
} else {
lo = int32_t(100 - m.tol) * n / 100;
hi = int32_t(100 + m.tol) * n / 100;
}
int32_t pick[] = {lo - 1, lo, hi, hi + 1};
int32_t e = pick[rbh_r() % 4];
if (e < 1)
e = 1;
return v < 0 ? -e : e;
}
static RawTimings rbh_frame(int proto) {
RemoteTransmitData t;
switch (proto) {
case 0:
AOKProtocol().encode(&t, AOKData{rbh_r() & 0xFFFFFF, (uint16_t) rbh_r(), (uint8_t) rbh_r()});
break;
case 1: {
CardinS438Data c;
c.code = rbh_r();
c.channel = rbh_r() % 4;
c.seed = rbh_r();
c.repeats = 1 + rbh_r() % 3;
CardinS438Protocol().encode(&t, c);
break;
}
case 2:
DraytonProtocol().encode(&t, DraytonData{(uint16_t) rbh_r(), 0, 0, (uint8_t) (rbh_r() & 0x7F)});
break;
default:
Beo4Protocol().encode(&t, Beo4Data{(uint8_t) rbh_r(), (uint8_t) rbh_r(), 0});
break;
}
// A receiver sees back to back marks (or spaces) as one longer timing
RawTimings merged;
for (int32_t v : t.get_data()) {
if (!merged.empty() && (merged.back() > 0) == (v > 0))
merged.back() += v;
else
merged.push_back(v);
}
return merged;
}
static void rbh_noise(RawTimings &raw, int n) {
bool mark = raw.empty() || raw.back() < 0;
for (int i = 0; i < n; i++, mark = !mark) {
int32_t v = 50 + rbh_r() % 12000;
raw.push_back(mark ? v : -v);
}
}
static const char *RBH_PROTO[] = {"aok", "cardin", "drayton", "beo4"};
struct RbhInput {
std::string what;
int mode;
RawTimings raw;
};
static void rbh_generate(std::vector<RbhInput> &out) {
for (int rep = 0; rep < 250; rep++) {
for (int proto = 0; proto < 4; proto++) {
RawTimings f = rbh_frame(proto);
for (int variant = 0; variant < 9; variant++) {
RawTimings raw;
if (variant == 1 || variant == 4)
rbh_noise(raw, 1 + rbh_r() % 30); // leading noise
if (!raw.empty() && raw.back() > 0 && f[0] > 0)
raw.push_back(-(int32_t) (3000 + rbh_r() % 9000));
int copies = (variant == 2 || variant == 4) ? 2 + rbh_r() % 2 : 1; // repeats
for (int c = 0; c < copies; c++) {
if (c > 0 && raw.back() > 0)
raw.push_back(-(int32_t) (5000 + rbh_r() % 8000));
raw.insert(raw.end(), f.begin(), f.end());
}
for (int mi = 0; mi < 4; mi++) {
const auto &m = RBH_MODES[mi];
RawTimings x = raw;
if (variant == 3) { // nudge about a quarter of the timings to the tolerance edges
for (auto &v : x)
if (rbh_r() % 4 == 0)
v = rbh_edge(v, m);
} else if (variant >= 5 && proto < 3) { // only the sync mark, at one tolerance edge, the rest exact
const int32_t nominal[] = {5000, 6400, 1000};
int32_t n = nominal[proto], lo, hi;
if (m.mode == TOLERANCE_MODE_TIME) {
lo = n - (int32_t) m.tol;
hi = n + (int32_t) m.tol;
} else {
lo = int32_t(100 - m.tol) * n / 100;
hi = int32_t(100 + m.tol) * n / 100;
}
const int32_t edge[] = {lo - 1, lo, hi, hi + 1};
for (auto &v : x) {
if (v == n) {
v = edge[variant - 5];
if (proto == 2)
break; // Drayton: the first 1000 us mark is the sync
}
}
} else if (variant == 4) { // jitter every timing by up to 30%
for (auto &v : x) {
int32_t n = v < 0 ? -v : v;
n = n + (int32_t) (n * ((int) (rbh_r() % 61) - 30) / 100);
if (n < 1)
n = 1;
v = v < 0 ? -n : n;
}
}
x.push_back(-20000);
char what[32];
snprintf(what, sizeof(what), "%s/v%d", RBH_PROTO[proto], variant);
out.push_back({what, mi, x});
}
}
}
}
for (int r = 0; r < 4000; r++) { // random timing corpus
RawTimings raw;
rbh_noise(raw, 4 + rbh_r() % 250);
raw.push_back(-20000);
out.push_back({"random", r % 4, raw});
}
}
static void rbh_fail(const char *what, const char *path) {
fprintf(stderr, "RBH_CORPUS %s: %s\n", what, path);
exit(1);
}
// RBH_CORPUS=<file>: replay the file when it exists, else generate a corpus and write it there. Any read, parse or
// write error exits 1, so two builds never compare different inputs.
static void rbh_run() {
std::vector<RbhInput> in;
const char *path = getenv("RBH_CORPUS");
errno = 0;
FILE *f = path ? fopen(path, "rb") : nullptr;
if (path != nullptr && f == nullptr && errno != ENOENT)
rbh_fail("cannot open", path);
if (f != nullptr) {
char what[64];
int mode;
unsigned n;
int got;
while ((got = fscanf(f, "%63s %d %u", what, &mode, &n)) == 3) {
if (mode < 0 || mode >= (int) (sizeof(RBH_MODES) / sizeof(RBH_MODES[0])))
rbh_fail("bad mode in", path);
RawTimings raw(n);
for (auto &v : raw) {
if (fscanf(f, "%d", &v) != 1)
rbh_fail("truncated frame in", path);
}
in.push_back({what, mode, raw});
}
if (got != EOF || ferror(f) || in.empty())
rbh_fail("unreadable or empty", path);
fclose(f);
} else {
rbh_generate(in);
if (path != nullptr) {
if ((f = fopen(path, "wb")) == nullptr)
rbh_fail("cannot create", path);
for (auto &x : in) {
fprintf(f, "%s %d %u", x.what.c_str(), x.mode, (unsigned) x.raw.size());
for (auto v : x.raw)
fprintf(f, " %d", v);
fprintf(f, "\n");
}
if (ferror(f) || fclose(f) != 0)
rbh_fail("write failed", path);
}
}
int i = 0;
for (auto &x : in)
rbh_dump_all(x.raw, RBH_MODES[x.mode].tol, RBH_MODES[x.mode].mode, x.what.c_str(), i++);
fflush(stdout);
exit(0);
}
@@ -0,0 +1,20 @@
# build twice: `-s level DEBUG` and `-s level VERBOSE` (with a different `-s name`), then compare with cmp.sh
substitutions:
level: DEBUG
esphome:
name: ${name}
includes:
- dumpers.h
- rbh.hpp
on_boot:
priority: -100
then:
- lambda: rbh_run();
host:
logger:
level: ${level}
external_components:
- source:
type: local
path: ../ext
rbtest:
@@ -0,0 +1,13 @@
"""Test only: pulls remote_base with every protocol source into a host build (hostharness and dumpall)."""
from esphome.components import remote_base
import esphome.config_validation as cv
AUTO_LOAD = ["remote_base"]
DOMAIN = "rbtest"
CONFIG_SCHEMA = cv.Schema({})
async def to_code(config):
for stem in remote_base._PROTOCOL_STEMS:
remote_base.request_protocol(stem)
@@ -0,0 +1,108 @@
esphome:
name: ${name}
includes:
- rbh.hpp
logger:
level: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
api:
reboot_timeout: 0s
encryption:
key: !secret api_key
ota:
- platform: esphome
remote_transmitter:
id: irtx
pin:
number: ${pin}
allow_other_uses: true
carrier_duty_percent: 100%
remote_receiver:
pin:
number: ${pin}
allow_other_uses: true
dump: [aeha, haier, mirage]
on_aeha:
then:
- lambda: rbh_on_frame("ON_AEHA", x.data);
on_haier:
then:
- lambda: rbh_on_frame("ON_HAIER", x.data);
on_mirage:
then:
- lambda: rbh_on_frame("ON_MIRAGE", x.data);
button:
- platform: template
name: TX all
on_press:
- script.execute: tx_all
script:
- id: tx_all
mode: single
then:
- logger.log: "TXMARK aeha 2"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0xE6C2
data: [0x79, 0xB7]
- delay: 900ms
- logger.log: "TXMARK aeha 3"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0x7EDA
data: [0x61, 0x89, 0x59]
- delay: 900ms
- logger.log: "TXMARK aeha 8"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0x9D2C
data: [0xEC, 0xD4, 0x26, 0x32, 0xB9, 0xA1, 0x14, 0xD7]
- delay: 900ms
- logger.log: "TXMARK aeha 13"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0x3E1C
data: [0xAE, 0x41, 0x17, 0xB0, 0xDC, 0x2C, 0xF8, 0x86, 0xF4, 0xF3, 0xDC, 0xD0, 0x92]
- delay: 900ms
- logger.log: "TXMARK aeha 20"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0xDD3D
data: [0x30, 0x65, 0xC9, 0x64, 0xB7, 0x32, 0xD3, 0xF5, 0x21, 0x6A, 0x62, 0x88, 0xE2, 0x5A, 0x59, 0x35, 0xF4, 0xC1, 0xC2, 0xE4]
- delay: 900ms
- logger.log: "TXMARK aeha 34"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0x9C21
data: [0x27, 0x6B, 0x15, 0x4D, 0x81, 0x14, 0x08, 0xCD, 0x36, 0xC1, 0x82, 0x3F, 0x85, 0xFE, 0x9D, 0xDD, 0x5D, 0xF9, 0x11, 0x98, 0x22, 0x88, 0x42, 0xEF, 0x73, 0xFD, 0xD7, 0x3A, 0x96, 0x50, 0xA1, 0x4E, 0xDB, 0x8D]
- delay: 900ms
- logger.log: "TXMARK aeha 35"
- lambda: ${prep}
- remote_transmitter.transmit_aeha:
address: 0x1050
data: [0x8F, 0xCA, 0xE5, 0x0A, 0x97, 0xF9, 0x34, 0xD7, 0x06, 0x64, 0x96, 0x52, 0xBE, 0x49, 0xFB, 0xB7, 0xA8, 0x46, 0xBA, 0x27, 0x8B, 0x9E, 0xA1, 0x8F, 0x6F, 0xAF, 0x97, 0xA2, 0xD1, 0x67, 0x88, 0x72, 0x8F, 0xC1, 0xD0]
- delay: 900ms
- logger.log: "TXMARK haier 13"
- lambda: ${prep}
- remote_transmitter.transmit_haier:
code: [0x22, 0x48, 0x40, 0x36, 0x31, 0x7A, 0x21, 0x08, 0x57, 0x0C, 0x7B, 0x16, 0x3A]
- delay: 600ms
- logger.log: "TXMARK haier 8"
- lambda: ${prep}
- remote_transmitter.transmit_haier:
code: [0x0C, 0xA0, 0x1C, 0x67, 0x3D, 0x0D, 0xF7, 0x81]
- delay: 600ms
- logger.log: "TXMARK mirage 14"
- lambda: ${prep}
- remote_transmitter.transmit_mirage:
code: [0x41, 0x1E, 0xF7, 0xA9, 0x30, 0xCC, 0x0B, 0xA1, 0x47, 0x86, 0xE8, 0xFF, 0x8C, 0xCF]
- delay: 600ms
- logger.log: "TXMARK mirage 14"
- lambda: ${prep}
- remote_transmitter.transmit_mirage:
code: [0x68, 0x72, 0x38, 0xAB, 0xA8, 0xF9, 0x4F, 0x86, 0x74, 0xA1, 0x39, 0x0A, 0x49, 0xC8]
- delay: 600ms
- logger.log: "TXMARK end"
@@ -0,0 +1,97 @@
#pragma once
#include <cstdio>
#include <cstdlib>
#include <random>
#include <type_traits>
#include <vector>
#include "esphome/components/remote_base/aeha_protocol.h"
#include "esphome/components/remote_base/haier_protocol.h"
#include "esphome/components/remote_base/mirage_protocol.h"
#include "esphome/core/alloc_helpers.h"
#include "esphome/core/log.h"
using namespace esphome::remote_base;
inline std::mt19937 rbh_rng(22773);
inline uint32_t rbh_r() { return rbh_rng(); }
template<typename D> void rbh_print(const char *proto, int i, const char *kind, const esphome::optional<D> &d) {
printf("CASE %s %d %s has=%d", proto, i, kind, d.has_value() ? 1 : 0);
if (d.has_value()) {
printf(" n=%u data=", (unsigned) d->data.size());
for (uint8_t b : d->data)
printf("%02X", b);
}
printf("\n");
}
// kind 0 clean, 1 one timing corrupted, 2 truncated
inline RawTimings rbh_mutate(RawTimings raw, int kind) {
raw.push_back(-20000); // the idle gap a receiver records after the last mark
if (kind == 1 && !raw.empty()) {
size_t k = rbh_r() % raw.size();
int32_t v = 100 + rbh_r() % 6000;
raw[k] = raw[k] < 0 ? -v : v;
} else if (kind == 2 && raw.size() > 1) {
raw.resize(rbh_r() % raw.size());
}
return raw;
}
inline const char *const RBH_KIND[] = {"clean", "corrupt", "trunc"};
// Encode one frame with enc, mutate it by kind, decode it, print the case and dump what decoded.
template<typename P, typename Enc>
void rbh_case(P &proto, const char *name, const char *prefix, int i, int kind, Enc enc) {
RemoteTransmitData t;
enc(&t);
RawTimings raw = rbh_mutate(t.get_data(), kind);
auto d = proto.decode(RemoteReceiveData(raw, 25, TOLERANCE_MODE_PERCENTAGE));
printf("%s", prefix);
rbh_print(name, i, RBH_KIND[kind], d);
if (!d.has_value())
return;
if constexpr (std::is_same_v<P, AEHAProtocol>)
printf("ADDR %04X\n", d->address);
proto.dump(*d);
}
inline void rbh_run() {
AEHAProtocol aeha;
HaierProtocol haier;
MirageProtocol mirage;
char prefix[48];
int i = 0;
for (int rep = 0; rep < 400; rep++) {
for (int kind = 0; kind < 3; kind++) {
uint8_t buf[48];
// AEHA: 1 to 40 bytes (35 is the cap)
size_t len = 1 + rbh_r() % 40;
for (auto &b : buf)
b = rbh_r();
uint16_t addr = rbh_r();
snprintf(prefix, sizeof(prefix), "AEHA len=%u addr=%04X ", (unsigned) len, addr);
rbh_case(aeha, "aeha", prefix, i, kind, [&](RemoteTransmitData *t) { aeha.encode(t, addr, buf, len); });
// Haier: 8 and 13 mostly, sometimes 1 to 16
size_t hl = (rep % 4 == 3) ? 1 + rbh_r() % 16 : ((rbh_r() & 1) ? 8 : 13);
snprintf(prefix, sizeof(prefix), "HAIER len=%u ", (unsigned) hl);
rbh_case(haier, "haier", prefix, i, kind, [&](RemoteTransmitData *t) { haier.encode(t, buf, hl); });
// Mirage: 14 mostly, sometimes 10 to 18
size_t ml = (rep % 4 == 3) ? 10 + rbh_r() % 9 : 14;
snprintf(prefix, sizeof(prefix), "MIRAGE len=%u ", (unsigned) ml);
rbh_case(mirage, "mirage", prefix, i, kind, [&](RemoteTransmitData *t) { mirage.encode(t, buf, ml); });
i++;
}
}
fflush(stdout);
exit(0);
}
// Loopback trigger log (ir-common.yaml): also checks that the vector idioms compile against the trigger's data type.
template<typename T> void rbh_on_frame(const char *tag, const T &data) {
std::vector<uint8_t> v = data;
uint32_t s = 0;
for (auto b : data)
s += b;
bool eq = (v == data) && (data == v) && !(data != v);
ESP_LOGI("loop", "%s n=%u hex=%s sum=%u eq=%d front=%u", tag, (unsigned) data.size(),
esphome::format_hex(data).c_str(), (unsigned) s, eq ? 1 : 0, (unsigned) data.front());
}
@@ -0,0 +1,16 @@
esphome:
name: ${name}
includes:
- rbh.hpp
on_boot:
priority: -100
then:
- lambda: rbh_run();
host:
logger:
level: DEBUG
external_components:
- source:
type: local
path: ../ext
rbtest:
@@ -0,0 +1,29 @@
"""press.py HOST BUTTON[:SECS]...: press each button (by object id) in order, waiting SECS (default 1) after each.
hostharness: `press.py HOST tx_all:14 tx_all:14 ...`; dumpall: `press.py HOST tx_all:9 tx_all:9 verbose tx_all:9 debug`;
pronto/run.sh: every tx button ROUNDS times, then `soak:90`.
API_KEY in the env selects Noise (see apiconn.py).
"""
import asyncio
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from aioesphomeapi import ButtonInfo # noqa: E402
from apiconn import connect # noqa: E402
async def main() -> None:
c = await connect(sys.argv[1], tries=60)
ents, _ = await c.list_entities_services()
keys = {e.object_id: e.key for e in ents if isinstance(e, ButtonInfo)}
for step in sys.argv[2:]:
name, _, secs = step.partition(":")
c.button_command(keys[name])
await asyncio.sleep(float(secs or 1))
await c.disconnect()
print("pressed", len(sys.argv) - 2)
asyncio.run(main())
@@ -0,0 +1,36 @@
"""consist.py EXT: per frame, the trigger text (decode) must equal the dump chunks joined."""
import sys
from extract import read_lines, trigger_blocks
DUMP_START = "remote.pronto: Received Pronto: data="
DUMP = "remote.pronto: "
lines = read_lines(sys.argv[1])
ok = bad = 0
for n, t, i in trigger_blocks(lines):
# dump: all prints NEC lines and the pronto binary sensor can log between the trigger and the Pronto dump
while i < len(lines) and (
lines[i].startswith("remote.nec") or "BS pronto" in lines[i]
):
i += 1
if i < len(lines) and lines[i] == DUMP_START:
i += 1
d = ""
while (
i < len(lines) and lines[i].startswith(DUMP) and "Received" not in lines[i]
):
d += lines[i][len(DUMP) :]
i += 1
if d == t and len(t) == n:
ok += 1
else:
bad += 1
print("MISMATCH", n, len(t), len(d))
else:
bad += 1
print("MISSING dump after a trigger of", n)
print(f"{ok + bad} trigger frames, {ok} consistent, {bad} not")
if bad or not ok:
sys.exit(1)
@@ -0,0 +1,16 @@
# D1 mini, one-pin loopback on GPIO12 with every dumper; the board and network come from climate_ir_loop.
packages:
base: !include ../../climate_ir_loop/base.yaml
board: !include ../../climate_ir_loop/esp8266-ir.yaml
common: !include gen-common.yaml
logger:
baud_rate: 460800
level: DEBUG
remote_receiver:
dump: all
buffer_size: 3kb
on_pronto:
- lambda: |-
ESP_LOGI("trig", "TRIG len=%u", (unsigned) x.data.size());
for (size_t i = 0; i < x.data.size(); i += 200)
ESP_LOGI("trig", "TRIG %s", x.data.substr(i, 200).c_str());
@@ -0,0 +1,19 @@
# ESP32 devkit, one-pin loopback on GPIO27 with every dumper; the board and network come from climate_ir_loop.
packages:
base: !include ../../climate_ir_loop/base.yaml
board: !include
file: ../../climate_ir_loop/esp32-ir.yaml
vars:
rmt_symbols: "256"
receive_symbols: "256"
common: !include gen-common.yaml
logger:
baud_rate: 921600
level: DEBUG
remote_receiver:
dump: all
on_pronto:
- lambda: |-
ESP_LOGI("trig", "TRIG len=%u", (unsigned) x.data.size());
for (size_t i = 0; i < x.data.size(); i += 200)
ESP_LOGI("trig", "TRIG %s", x.data.substr(i, 200).c_str());
@@ -0,0 +1,64 @@
"""extract.py SERIAL OUT: keep MARK, pronto dump, trigger, binary sensor and heap lines from the first MARK on,
with timestamps and source line numbers removed (lognorm.py). Also holds the trigger block parser that consist.py
and jit.py share."""
import pathlib
import re
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from lognorm import normalize # noqa: E402
TRIG_LEN = "trig: TRIG len="
TRIG = "trig: TRIG "
def trigger_blocks(lines):
"""Yield (n, text, i) per on_pronto trigger: the length it logged, its chunks joined, and the index of the first
line after the block."""
i = 0
while i < len(lines):
if not lines[i].startswith(TRIG_LEN):
i += 1
continue
n = int(lines[i][len(TRIG_LEN) :])
i += 1
text = ""
while (
i < len(lines)
and lines[i].startswith(TRIG)
and not lines[i].startswith(TRIG_LEN)
):
text += lines[i][len(TRIG) :]
i += 1
yield n, text, i
def read_lines(path):
return pathlib.Path(path).read_text(encoding="utf-8").splitlines()
def main() -> None:
out, on = [], False
with pathlib.Path(sys.argv[1]).open(encoding="utf-8", errors="replace") as fh:
for raw in fh:
m = re.match(r"\[(\w)\]\[([\w.]+)\]: ?(.*)$", normalize(raw.rstrip("\n")))
if not m:
continue
_lvl, tag, msg = m.groups()
if "MARK tx" in msg:
on = True
if not on:
continue
if (
tag in ("remote.pronto", "trig", "heap")
or "MARK" in msg
or "BS pronto" in msg
or tag == "remote.nec"
):
out.append(f"{tag}: {msg}")
pathlib.Path(sys.argv[2]).write_text("\n".join(out) + "\n", encoding="utf-8")
if __name__ == "__main__":
main()
@@ -0,0 +1,86 @@
"""gen.py [OUT]: write OUT (default gen-common.yaml next to this script) with raw frames, buttons, on_pronto trigger,
pronto binary sensors and heap logging; prints each frame's length and the pronto binary sensor data."""
import pathlib
import sys
def frame(n, seed):
out = []
for i in range(n):
if i % 2 == 0:
out.append(300 + (i * 37 + seed * 101) % 900)
else:
out.append(-(400 + (i * 53 + seed * 59) % 1500))
return out
def pronto(t):
nums = [0, 4145146 // 38000, (len(t) + 1) // 2, 0]
for x in t:
d = x - 20 if x > 0 else -x + 20
nums.append((d + 13) // 26)
return " ".join(f"{v:04X}" for v in nums) + " 0181 "
frames = {
"raw21a": frame(21, 1),
"raw21b": [x + 260 if x > 0 else x - 260 for x in frame(21, 1)],
"raw41": frame(41, 2),
"raw43": frame(43, 3),
"raw121": frame(121, 4),
"raw245": frame(245, 5),
}
y = []
y.append("button:")
y.append(
" - {platform: template, name: tx nec, on_press: [{logger.log: 'MARK tx nec'}, {remote_transmitter.transmit_nec: {address: 0x1234, command: 0x78}}]}"
)
for k, v in frames.items():
y.append(
f" - platform: template\n name: tx {k}\n on_press:\n - logger.log: 'MARK tx {k}'\n - remote_transmitter.transmit_raw:\n code: {v}"
)
y.append(
""" - platform: template
name: soak
on_press:
- lambda: id(heap_log).execute("before");
- repeat:
count: 100
then:
- remote_transmitter.transmit_nec: {{address: 0x1234, command: 0x78}}
- delay: 150ms
- remote_transmitter.transmit_raw:
code: {}
- delay: 250ms
- delay: 1s
- lambda: id(heap_log).execute("after");
""".format(frames["raw245"])
)
y.append(f"""script:
- id: heap_log
parameters: {{tag: string}}
then:
- lambda: |-
#ifdef USE_ESP32
ESP_LOGI("heap", "HEAP %s free=%u largest=%u", tag.c_str(), (unsigned) heap_caps_get_free_size(MALLOC_CAP_INTERNAL), (unsigned) heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL));
#else
ESP_LOGI("heap", "HEAP %s free=%u largest=%u", tag.c_str(), (unsigned) ESP.getFreeHeap(), (unsigned) ESP.getMaxFreeBlockSize());
#endif
binary_sensor:
- platform: remote_receiver
name: pronto a
receiver_id: irrx
pronto:
data: "{pronto(frames["raw21a"]).strip()}"
on_press: [{{logger.log: 'BS pronto a ON'}}]
""")
out = (
pathlib.Path(sys.argv[1])
if len(sys.argv) > 1
else pathlib.Path(__file__).with_name("gen-common.yaml")
)
out.write_text("\n".join(y), encoding="utf-8")
for k, v in frames.items():
print(k, len(v), (4 + len(v)) * 5)
print("PRONTO_A", pronto(frames["raw21a"]))
@@ -0,0 +1,31 @@
"""jit.py EXT_A EXT_B: compare the on_pronto trigger frames of two extract.py outputs in order; counts identical
frames, frames whose length differs, and the largest difference in Pronto units (timing jitter) between same-length
frames, leaving out the header and the trailing idle."""
import sys
from extract import read_lines, trigger_blocks
def frames(p):
return [
[int(x, 16) for x in text.split()]
for _n, text, _i in trigger_blocks(read_lines(p))
]
a, b = frames(sys.argv[1]), frames(sys.argv[2])
same = sum(x == y for x, y in zip(a, b, strict=False))
lens = sum(len(x) != len(y) for x, y in zip(a, b, strict=False))
md = max(
(
abs(p - q)
for x, y in zip(a, b, strict=False)
if len(x) == len(y)
for p, q in zip(x[4:-1], y[4:-1], strict=False)
),
default=0,
)
print(
f"{len(a)}/{len(b)} frames, {same} identical, {lens} length differs, max unit diff {md}"
)
@@ -0,0 +1,25 @@
#!/bin/bash
# run.sh NAME TREE YAML PORT BAUD ROUNDS: flash NAME, capture serial, press each tx button ROUNDS times with
# ../press.py, then the soak button, and count the markers, triggers and binary sensor presses on serial
S=$(cd "$(dirname "$0")" && pwd); K=$(dirname "$(dirname "$S")"); N=$1; T=$2; Y=$3; P=$4; B=$5; R=$6
PY=${PY:-$T/venv/bin/python}
pkill -f "[s]erial_capture.py $P "
TREE=$T PIO_CORE=${PIO_CORE:-$S/pio} bash "$K/pio_build.sh" upload "$N" "$Y" "$P" || exit 1
rm -f "$S/$N-serial.txt"
# boot and IP lookup (up to 160 s), 7 presses of 1.5 s per round, the 90 s soak, margin; stopped after the drive
"$PY" "$K/serial_capture.py" "$P" $((160 + R * 11 + 120)) "$S/$N-serial.txt" --baud "$B" --reset > /dev/null 2>&1 &
CAP=$!
IP=""; for _ in $(seq 60); do IP=$(grep -a -o 'IP Address: [0-9.]*' "$S/$N-serial.txt" 2>/dev/null | tail -1 | cut -d' ' -f3); [ -n "$IP" ] && break; sleep 2; done
[ -n "$IP" ] || IP=$(bash "$K/resolve.sh" "$N" 20) || { echo no ip; kill $CAP; exit 1; }
sleep 5
PRESSES=()
for _ in $(seq "$R"); do
for b in tx_nec tx_raw21a tx_raw21b tx_raw41 tx_raw43 tx_raw121 tx_raw245; do PRESSES+=("$b:1.5"); done
done
"$PY" "$S/../press.py" "$IP" "${PRESSES[@]}" soak:90 > "$S/$N-drive.txt" 2>&1; RC=$?
sleep 3; kill $CAP; wait $CAP 2>/dev/null
cat "$S/$N-drive.txt"
echo "MARK tx $(grep -a -c 'MARK tx' "$S/$N-serial.txt") triggers $(grep -a -c 'TRIG len=' "$S/$N-serial.txt")" \
"binary sensor on_press $(grep -a -c 'BS pronto a ON' "$S/$N-serial.txt")"
[ $RC -eq 0 ] || echo "press.py failed (rc=$RC)"
exit $RC
+29
View File
@@ -0,0 +1,29 @@
#!/bin/bash
# compare.sh A B: diff two run.sh results (NAME prefixes) in this directory: API states, entity dump block, the
# re-dump 1 s after init, hub trigger vs entity publish order, and the command frames the simulator received.
# Timestamps (with or without milliseconds) and line numbers are removed with ../lognorm.py.
# Exits 1 when an input is missing or empty or a part extracts nothing, so two failed runs never compare as identical,
# and 2 when a part differs.
cd "$(dirname "$0")" || exit 1; A=${1:?name}; B=${2:?name}; PY=${PY:-python3}
for f in $A-states.json $A-log.txt $B-states.json $B-log.txt; do
[ -s "$f" ] || { echo "missing or empty: $f"; exit 1; }
done
for n in $A $B; do
$PY -c "
import json
for e in json.load(open('$n-states.json')): print(e['phase'], e['entity'], json.dumps(e['value'], sort_keys=True))" > $n-states.txt ||
{ echo "unreadable: $n-states.json"; exit 1; }
$PY ../lognorm.py $n-log.txt > $n-norm.txt || exit 1
awk '/^\[C\]\[logger\]: Logger:/{on=1} on' $n-norm.txt | grep -av "^\[[DIVW]\]" > $n-dumpfull.txt
awk '/Tuya Binary Sensor:/{on=1} on{print} /Current Temperature has datapoint ID 22/{exit}' $n-dumpfull.txt > $n-ent.txt
awk '/\[C\]\[tuya\]: Tuya:/{c++} c>=2 && /\[C\]\[tuya\]/' $n-dumpfull.txt > $n-redump.txt
grep -a "\[ord\|SIM \(STEP\|TX\|DELIVER\)\|Datapoint [0-9]* update to" $n-norm.txt > $n-ord.txt
grep -a "SIM RX" $n-norm.txt | grep -v "cmd 00" | sed -E 's/#[0-9]+ //' > $n-rx.txt
done
bad=0
for k in states ent redump ord rx; do
if [ ! -s $A-$k.txt ] || [ ! -s $B-$k.txt ]; then echo "$k: EMPTY in $A or $B"; bad=1
elif diff -q $A-$k.txt $B-$k.txt > /dev/null; then echo "$k: identical ($(wc -l < $A-$k.txt) lines)"
else echo "$k: DIFFERS"; [ $bad -eq 0 ] && bad=2; fi
done
exit $bad
+21
View File
@@ -0,0 +1,21 @@
#!/bin/bash
# run.sh NAME TREE: flash NAME (already compiled from TREE with pio_build.sh), capture the serial log with a reset,
# and drive the scenario over the API. Outputs NAME-log.txt, NAME-states.json, NAME-drive.txt.
# Needs secrets.yaml (wifi_ssid, wifi_password, api_key) next to tuya.yaml.
# env: PORT (the board), API_KEY, PY
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); N=${1:?name}; T=${2:?tree}; P=${PORT:?set PORT}
PY=${PY:-$T/venv/bin/python}
pkill -f "[s]erial_capture.py $P "
# outputs of an earlier run must not pass for this one in compare.sh
rm -f "$D/$N-log.txt" "$D/$N-states.json" "$D/$N-drive.txt"
TREE=$T PY=$PY "$S/pio_build.sh" upload "$N" "$D/tuya.yaml" "$P" || exit 1
# 130 s is an upper bound; the capture is stopped right after the drive
$PY "$S/serial_capture.py" "$P" 130 "$D/$N-log.txt" --reset &
CAP=$!
IP=$("$S/resolve.sh" "$N" 40) || { kill $CAP; exit 1; }
PYTHONUNBUFFERED=1 timeout 120 $PY "$D/tuyadrive.py" "$IP" "$D/$N-states.json" > "$D/$N-drive.txt" 2>&1
RC=$?
echo "drive rc=$RC"
sleep 2; kill $CAP; wait $CAP 2>/dev/null
grep -a -c "SIM RX" "$D/$N-log.txt"
exit $RC
@@ -0,0 +1,208 @@
substitutions:
name: tuyax
# Tuya hub plus every moved platform, fed by tuyasim.hpp on a cross-wired second UART (esphome#22724).
# The run file sets `name` and includes this file as a package.
esphome:
name: ${name}
includes:
- tuyasim.hpp
on_boot:
priority: 800
then:
- lambda: tuyasim::bus = id(mcu_uart);
esp32:
board: pico32
framework:
type: esp-idf
logger:
level: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
api:
reboot_timeout: 0s
encryption:
key: !secret api_key
uart:
- id: hub_uart
tx_pin: {number: GPIO9, allow_other_uses: true}
rx_pin: {number: GPIO10, allow_other_uses: true}
baud_rate: 9600
- id: mcu_uart
tx_pin: {number: GPIO10, allow_other_uses: true}
rx_pin: {number: GPIO9, allow_other_uses: true}
baud_rate: 9600
interval:
- interval: 5ms
then:
- lambda: tuyasim::poll();
- interval: 10s
then:
- lambda: |-
ESP_LOGI("heap", "HEAP up=%us free=%u min=%u largest=%u", (unsigned) (millis() / 1000),
(unsigned) esp_get_free_heap_size(), (unsigned) esp_get_minimum_free_heap_size(),
(unsigned) heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
button:
- platform: template
name: Sim start
id: sim_start
on_press:
- lambda: tuyasim::start();
tuya:
id: hub
uart_id: hub_uart
on_datapoint_update:
- sensor_datapoint: 1
datapoint_type: bool
then:
- lambda: ESP_LOGI("ord", "HUB dp1 x=%d bs1=%d sw1=%d", x, id(bs1).state, id(sw1).state);
- sensor_datapoint: 2
datapoint_type: int
then:
- lambda: ESP_LOGI("ord", "HUB dp2 x=%d s_int=%.0f", x, id(s_int).state);
- sensor_datapoint: 3
datapoint_type: string
then:
- lambda: ESP_LOGI("ord", "HUB dp3 x=%s ts_str=%s", x.c_str(), id(ts_str).state.c_str());
- sensor_datapoint: 4
datapoint_type: enum
then:
- lambda: ESP_LOGI("ord", "HUB dp4 x=%u sel_enum=%s", x, id(sel_enum).current_option().c_str());
- sensor_datapoint: 23
datapoint_type: enum
then:
- lambda: ESP_LOGI("ord", "HUB dp23 x=%u wh_mode=%d", x, (int) id(wh).get_mode());
binary_sensor:
- platform: tuya
id: bs1
name: BS dp1
sensor_datapoint: 1
trigger_on_initial_state: true
on_state:
- lambda: ESP_LOGI("ord", "ENT bs1 %d", x);
switch:
- platform: tuya
id: sw1
name: SW dp1
switch_datapoint: 1
on_state:
- lambda: ESP_LOGI("ord", "ENT sw1 %d", x);
sensor:
- platform: tuya
id: s_int
name: S int dp2
sensor_datapoint: 2
on_value:
- lambda: ESP_LOGI("ord", "ENT s_int %.0f", x);
- platform: tuya
id: s_enum
name: S enum dp4
sensor_datapoint: 4
on_value:
- lambda: ESP_LOGI("ord", "ENT s_enum %.0f", x);
- platform: tuya
id: s_bm
name: S bitmask dp5
sensor_datapoint: 5
on_value:
- lambda: ESP_LOGI("ord", "ENT s_bm %.0f", x);
text_sensor:
- platform: tuya
id: ts_str
name: TS string dp3
sensor_datapoint: 3
on_value:
- lambda: ESP_LOGI("ord", "ENT ts_str %s", x.c_str());
- platform: tuya
id: ts_enum
name: TS enum dp4
sensor_datapoint: 4
on_value:
- lambda: ESP_LOGI("ord", "ENT ts_enum %s", x.c_str());
- platform: tuya
id: ts_raw
name: TS raw dp6
sensor_datapoint: 6
on_value:
- lambda: ESP_LOGI("ord", "ENT ts_raw %s", x.c_str());
select:
- platform: tuya
id: sel_enum
name: Sel enum dp4
enum_datapoint: 4
options:
0: Zero
1: One
2: Two
on_value:
- lambda: ESP_LOGI("ord", "ENT sel_enum %s", x.c_str());
- platform: tuya
id: sel_str
name: Sel string dp24
string_datapoint: 24
options:
- auto
- manual
- eco
on_value:
- lambda: ESP_LOGI("ord", "ENT sel_str %s", x.c_str());
- platform: tuya
id: sel_int
name: Sel int dp25
int_datapoint: 25
options:
0: Low
1: High
on_value:
- lambda: ESP_LOGI("ord", "ENT sel_int %s", x.c_str());
water_heater:
- platform: tuya
id: wh
name: WH
switch_datapoint: 20
target_temperature_datapoint: 21
current_temperature_datapoint: 22
current_temperature_multiplier: 0.5
target_temperature_multiplier: 0.5
mode_datapoint: 23
eco_value: 0
electric_value: 2
supported_modes:
- "OFF"
- ECO
- ELECTRIC
visual:
min_temperature: 30
max_temperature: 75
target_temperature_step: 1
number:
- platform: tuya
id: num
name: Num dp30
number_datapoint: 30
min_value: 0
max_value: 10
step: 1
fan:
- platform: tuya
id: fan1
name: Fan dp31
switch_datapoint: 31
@@ -0,0 +1,116 @@
"""Drive the tuyasim scenario over the API and record every entity state (esphome#22724).
usage: tuyadrive.py HOST OUT.json (API_KEY in the env)
Presses "Sim start", waits for the scripted datapoint changes, then sends switch, select, water heater, number and
fan commands. OUT.json gets the ordered state list per entity; a summary is printed.
"""
import asyncio
from collections import Counter
import json
import pathlib
import sys
import time
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from aioesphomeapi import WaterHeaterMode # noqa: E402
from apiconn import connect # noqa: E402
STEPS_WAIT = 32 # 18 steps 1.5 s apart, plus margin
def val(st):
d = {}
for k in (
"state",
"missing_state",
"current_temperature",
"target_temperature",
"mode",
"oscillating",
"speed_level",
):
if hasattr(st, k):
x = getattr(st, k)
d[k] = x.value if hasattr(x, "value") else x
return d
async def main(host: str, out: str) -> None:
c = await connect(host)
ents, _ = await c.list_entities_services()
by_key = {e.key: e for e in ents}
by_id = {e.object_id: e for e in ents}
events = []
phase = ["initial"]
t0 = time.monotonic()
def on_state(st):
e = by_key.get(st.key)
events.append(
{
"t": round(time.monotonic() - t0, 3),
"phase": phase[0],
"entity": e.object_id if e else st.key,
"value": val(st),
}
)
c.subscribe_states(on_state)
await asyncio.sleep(3)
phase[0] = "scenario"
c.button_command(by_id["sim_start"].key)
await asyncio.sleep(STEPS_WAIT)
phase[0] = "commands"
k = {n: by_id[n].key for n in by_id}
cmds = [
("sw on", lambda: c.switch_command(k["sw_dp1"], True)),
("sw off", lambda: c.switch_command(k["sw_dp1"], False)),
("sel_enum Two", lambda: c.select_command(k["sel_enum_dp4"], "Two")),
("sel_str manual", lambda: c.select_command(k["sel_string_dp24"], "manual")),
("sel_int High", lambda: c.select_command(k["sel_int_dp25"], "High")),
("wh on", lambda: c.water_heater_command(k["wh"], on=True)),
(
"wh target 55",
lambda: c.water_heater_command(k["wh"], target_temperature=55),
),
(
"wh mode eco",
lambda: c.water_heater_command(k["wh"], mode=WaterHeaterMode.ECO),
),
(
"wh mode electric",
lambda: c.water_heater_command(k["wh"], mode=WaterHeaterMode.ELECTRIC),
),
("wh off", lambda: c.water_heater_command(k["wh"], on=False)),
("num 3", lambda: c.number_command(k["num_dp30"], 3)),
("fan on", lambda: c.fan_command(k["fan_dp31"], state=True)),
("fan off", lambda: c.fan_command(k["fan_dp31"], state=False)),
]
for name, fn in cmds:
events.append(
{
"t": round(time.monotonic() - t0, 3),
"phase": "cmd",
"entity": "CMD",
"value": name,
}
)
fn()
await asyncio.sleep(2)
await asyncio.sleep(3)
await c.disconnect()
with pathlib.Path(out).open("w", encoding="utf-8") as f:
json.dump(events, f, indent=1)
counts = Counter(
(ev["phase"], ev["entity"]) for ev in events if ev["entity"] != "CMD"
)
for (ph, ent), n in sorted(counts.items()):
print(f"{ph:9} {ent:20} {n}")
print("TOTAL", sum(counts.values()))
if __name__ == "__main__":
asyncio.run(main(sys.argv[1], sys.argv[2]))
@@ -0,0 +1,194 @@
#pragma once
// A Tuya MCU on a second UART, cross-wired to the tuya hub's UART (esphome#22724, 2026-11-14).
// Answers the handshake (heartbeat, product, conf query without status pin so the hub sends WIFI_STATE,
// wifi state, datapoint query), echoes every DATAPOINT_DELIVER back as a report like a real MCU, and plays a
// scripted list of datapoint changes once tuyasim::start() is called. Every frame from the hub is logged as
// `SIM RX #n cmd XX: <hex>` so command streams can be diffed between builds.
// Call tuyasim::poll() from a short interval; set tuyasim::bus in on_boot.
#include <string>
#include <vector>
#include "esphome/components/uart/uart.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace tuyasim {
inline esphome::uart::UARTComponent *bus = nullptr;
inline std::vector<uint8_t> rx;
inline uint32_t frames = 0;
inline uint32_t delivers = 0;
inline bool hb_sent = false;
inline int step = -1; // -1: scenario not started
inline uint32_t next_step_ms = 0;
inline uint32_t step_gap_ms = 1500;
struct Dp {
uint8_t id;
uint8_t type; // 0 raw, 1 bool, 2 value, 3 string, 4 enum, 5 bitmask
std::vector<uint8_t> data;
};
inline Dp b(uint8_t id, bool v) { return {id, 1, {static_cast<uint8_t>(v)}}; }
inline Dp v(uint8_t id, int32_t x) {
uint32_t u = static_cast<uint32_t>(x);
return {id, 2, {uint8_t(u >> 24), uint8_t(u >> 16), uint8_t(u >> 8), uint8_t(u)}};
}
inline Dp s(uint8_t id, const char *str) { return {id, 3, std::vector<uint8_t>(str, str + strlen(str))}; }
inline Dp e(uint8_t id, uint8_t x) { return {id, 4, {x}}; }
inline Dp m(uint8_t id, std::vector<uint8_t> bytes) { return {id, 5, bytes}; }
inline Dp r(uint8_t id, std::vector<uint8_t> bytes) { return {id, 0, bytes}; }
// Current MCU state, reported in full on DATAPOINT_QUERY
inline std::vector<Dp> &state() {
static std::vector<Dp> st{b(1, false), v(2, 100), s(3, "boot"), e(4, 1), m(5, {0x01}),
r(6, {0x01, 0x02}), b(20, false), v(21, 100), v(22, 80), e(23, 2),
s(24, "auto"), v(25, 0), v(30, 5), b(31, false)};
return st;
}
inline void store(const Dp &dp) {
for (auto &d : state()) {
if (d.id == dp.id) {
d = dp;
return;
}
}
state().push_back(dp);
}
inline void send(uint8_t cmd, const std::vector<uint8_t> &payload) {
std::vector<uint8_t> out{0x55, 0xAA, 0x03, cmd, uint8_t(payload.size() >> 8), uint8_t(payload.size())};
out.insert(out.end(), payload.begin(), payload.end());
out.push_back(esphome::sum_bytes(out.data(), out.size()));
bus->write_array(out.data(), out.size());
}
inline void report(const std::vector<Dp> &dps, uint8_t cmd = 0x07) {
std::vector<uint8_t> p;
for (const auto &d : dps) {
store(d);
p.push_back(d.id);
p.push_back(d.type);
p.push_back(uint8_t(d.data.size() >> 8));
p.push_back(uint8_t(d.data.size()));
p.insert(p.end(), d.data.begin(), d.data.end());
}
char hex[160];
esphome::format_hex_to(hex, sizeof(hex), p.data(), p.size() > 70 ? 70 : p.size());
ESP_LOGI("sim", "SIM TX cmd %02X: %s", cmd, hex);
send(cmd, p);
}
// The scripted datapoint changes, one entry per step
inline std::vector<std::vector<Dp>> &script() {
static std::vector<std::vector<Dp>> sc{
{b(1, true)},
{v(2, 1234)},
{s(3, "hello")},
{e(4, 2)},
{m(5, {0x01, 0x05})},
{r(6, {0xDE, 0xAD, 0xBE, 0xEF})},
{b(20, true), v(21, 120), v(22, 90), e(23, 0)},
{b(1, false)}, // sent as DATAPOINT_REPORT_SYNC, the hub must ack with 0x23
{v(2, -5)},
{s(3, "world 42")},
{e(4, 0)},
{s(24, "eco"), v(25, 1)},
{m(5, {0x80, 0x00, 0x00, 0x01})},
{e(23, 2), v(22, 100)},
{v(30, 7), b(31, true)},
{b(1, true), v(2, 77), s(3, "multi")},
{v(2, 77)}, // same value again
{b(20, false)},
};
return sc;
}
inline void start() {
step = 0;
next_step_ms = esphome::millis();
ESP_LOGI("sim", "SIM SCENARIO START (%u steps)", (unsigned) script().size());
}
inline void handle(uint8_t cmd, const uint8_t *d, size_t len) {
frames++;
char hex[160];
esphome::format_hex_to(hex, sizeof(hex), d, len > 70 ? 70 : len);
ESP_LOGI("sim", "SIM RX #%u cmd %02X: %s", (unsigned) frames, cmd, hex);
switch (cmd) {
case 0x00:
send(0x00, {static_cast<uint8_t>(hb_sent ? 0x01 : 0x00)});
hb_sent = true;
break;
case 0x01: {
const char *p = R"({"p":"tuyasim","v":"1.0.0","m":0})";
send(0x01, std::vector<uint8_t>(p, p + strlen(p)));
break;
}
case 0x02:
send(0x02, {}); // no status pin: the hub reports wifi state itself
break;
case 0x03:
send(0x03, {});
break;
case 0x08:
report(state());
break;
case 0x06: {
delivers++;
// echo each delivered datapoint back as a report, as a real MCU does
std::vector<Dp> dps;
size_t i = 0;
while (i + 4 <= len) {
size_t n = (d[i + 2] << 8) | d[i + 3];
if (i + 4 + n > len)
break;
dps.push_back({d[i], d[i + 1], std::vector<uint8_t>(d + i + 4, d + i + 4 + n)});
i += 4 + n;
}
ESP_LOGI("sim", "SIM DELIVER #%u (%u dps)", (unsigned) delivers, (unsigned) dps.size());
report(dps);
break;
}
default:
break;
}
}
inline void poll() {
if (bus == nullptr)
return;
uint8_t c;
while (bus->available() && bus->read_byte(&c)) {
rx.push_back(c);
if (rx.size() == 1 && rx[0] != 0x55) {
rx.clear();
continue;
}
if (rx.size() == 2 && rx[1] != 0xAA) {
rx.clear();
continue;
}
if (rx.size() < 6)
continue;
size_t n = (rx[4] << 8) | rx[5];
if (rx.size() < 7 + n)
continue;
if (esphome::sum_bytes(rx.data(), 6 + n) != rx[6 + n])
ESP_LOGW("sim", "SIM RX bad checksum");
else
handle(rx[3], rx.data() + 6, n);
rx.clear();
}
if (step >= 0 && step < (int) script().size() && (int32_t) (esphome::millis() - next_step_ms) >= 0) {
ESP_LOGI("sim", "SIM STEP %d", step);
report(script()[step], step == 7 ? 0x22 : 0x07);
step++;
next_step_ms = esphome::millis() + step_gap_ms;
if (step == (int) script().size())
ESP_LOGI("sim", "SIM SCENARIO DONE");
}
}
} // namespace tuyasim
@@ -0,0 +1,41 @@
"""Count "PUB <dev>.<key> <value>" lines per entity in a capture, compare with expected_all.json and print a values
digest per entity so two builds can be compared. Usage: count_all.py CAPTURE [CAPTURE...]"""
import collections
import hashlib
import json
from pathlib import Path
import re
import sys
exp = json.loads(
Path(__file__).with_name("expected_all.json").read_text(encoding="utf-8")
)
PUB = re.compile(r"PUB (\S+) (\S+)")
results = {}
failed = False
for cap in sys.argv[1:]:
vals = collections.defaultdict(list)
for line in Path(cap).read_text(encoding="utf-8", errors="replace").splitlines():
if m := PUB.search(line):
vals[m.group(1)].append(m.group(2))
results[cap] = vals
bad = 0
print(f"== {cap}")
for ent in sorted(set(exp) | set(vals)):
got, want = len(vals.get(ent, [])), exp.get(ent)
ok = got == want
bad += not ok
dig = hashlib.sha1(" ".join(vals.get(ent, [])).encode()).hexdigest()[:8]
print(f" {ent:26} want {want!s:>3} got {got:3} {dig} {'ok' if ok else 'DIFF'}")
failed |= bad > 0
print(
f" total publishes {sum(len(v) for v in vals.values())}, entities with a mismatch: {bad}"
)
if len(results) > 1:
caps = list(results)
same = all(results[c] == results[caps[0]] for c in caps[1:])
print("values identical across captures:", same)
failed |= not same
if failed:
sys.exit(1)
@@ -9,7 +9,7 @@ from pathlib import Path
import struct
import sys
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
from mibeacon import XB_FILL, c_rows, enc, plain, t16, th
OTA = "--ota" in sys.argv[1:]
CYCLES = 40
@@ -25,41 +25,24 @@ MIDX = {k: i for i, k in enumerate(MACS)}
def mibeacon_plain(dev, pid, cnt, obj):
return (
bytes([0x50, 0x20])
+ struct.pack("<H", pid)
+ bytes([cnt])
+ MAC[dev][::-1]
+ obj
)
return plain(MAC[dev], pid, cnt, obj)
def mibeacon_enc(dev, pid, cnt, obj, key, short, corrupt=False):
ext = bytes([cnt, 0x00, 0x00])
if short:
hdr = bytes([0x48, 0x59]) + struct.pack("<H", pid) + bytes([cnt])
else:
hdr = (
bytes([0x58, 0x58]) + struct.pack("<H", pid) + bytes([cnt]) + MAC[dev][::-1]
)
nonce = MAC[dev][::-1] + hdr[2:5] + ext
ct = AESCCM(key, tag_length=4).encrypt(nonce, obj, b"\x11")
body, tag = ct[:-4], ct[-4:]
if corrupt:
tag = bytes([tag[0] ^ 0xFF]) + tag[1:]
out = hdr + body + ext + tag
out = enc(
MAC[dev],
pid,
cnt,
obj,
key,
short,
version=0x59 if short else 0x58,
corrupt=corrupt,
)
assert len(out) == (19 if short else 18 + len(obj)), len(out)
return out
def t16(v): # 0x1004 temperature object
return bytes([0x04, 0x10, 0x02]) + struct.pack("<h", round(v * 10))
def th(t, h): # 0x100D temperature + humidity
return bytes([0x0D, 0x10, 0x04]) + struct.pack("<hh", round(t * 10), round(h * 10))
frames = [] # (dev, kind 0 = FE95 / 1 = 181A, data, tag for the expectation)
frames.append(("D", 0, mibeacon_plain("D", 0x0347, 0, t16(11.1)), "D_boot"))
steps = [len(frames)]
@@ -125,47 +108,19 @@ print("pr ", expected["pr"])
Path("expected.json").write_text(json.dumps(expected), encoding="utf-8")
def c_rows(name, rows):
out = [f"static const uint8_t {name}[{len(rows)}][{len(rows[0][0])}] = {{"]
out += [
" {" + ",".join(f"0x{b:02X}" for b in row) + "}," + (f" // {c}" if c else "")
for row, c in rows
]
return "\n".join([*out, "};", ""])
mac_rows = [(MAC[k], "") for k in MACS]
frame_rows = [
((bytes([MIDX[d], kind, len(data)]) + data).ljust(28, b"\0"), tag)
for d, kind, data, tag in frames
]
INJECT = """
inline void xb_fill(esphome::esp32_ble::BLEScanResult &r, const uint8_t *mac, bool fe95, const uint8_t *data,
uint8_t len) {
memcpy(r.bda, mac, 6);
r.ble_addr_type = 0;
r.rssi = -50;
uint8_t n = 0;
r.ble_adv[n++] = 2;
r.ble_adv[n++] = 0x01;
r.ble_adv[n++] = 0x06;
r.ble_adv[n++] = len + 3;
r.ble_adv[n++] = 0x16;
r.ble_adv[n++] = fe95 ? 0x95 : 0x1A;
r.ble_adv[n++] = fe95 ? 0xFE : 0x18;
memcpy(r.ble_adv + n, data, len);
r.adv_data_len = n + len;
r.scan_rsp_len = 0;
r.search_evt = ESP_GAP_SEARCH_INQ_RES_EVT;
}
INJECT = (
XB_FILL
+ """
inline void xb_inject(size_t first, size_t last) {
for (size_t i = first; i < last; i++) {
const uint8_t *row = XB_FRAMES[i];
esphome::esp32_ble::BLEScanResult r{};
xb_fill(r, XB_MACS[row[0]], row[1] == 0, row + 3, row[2]);
esphome::esp32_ble_tracker::global_esp32_ble_tracker->gap_scan_event_handler(r);
xb_inject_one(XB_MACS[row[0]], row[1] == 0 ? 0xFE95 : 0x181A, row + 3, row[2]);
}
}
@@ -180,7 +135,7 @@ inline void xb_hex(const char *label, const std::vector<uint8_t> &v) {
// Parse one encrypted frame with the listener directly and show whether it rewrote the shared service data.
inline void xb_rewrite_probe(esphome::ble_device_base::ESPBTDeviceListener *listener) {
esphome::esp32_ble::BLEScanResult r{};
xb_fill(r, XB_MACS[0], true, XB_PROBE, sizeof(XB_PROBE));
xb_fill(r, XB_MACS[0], 0xFE95, XB_PROBE, sizeof(XB_PROBE));
esphome::ble_device_base::ESPBTDevice device;
device.parse_scan_rst(r);
xb_hex("before", device.get_service_datas()[0].data);
@@ -189,6 +144,7 @@ inline void xb_rewrite_probe(esphome::ble_device_base::ESPBTDeviceListener *list
xb_hex("after ", device.get_service_datas()[0].data);
}
"""
)
ADVERTISE = """
// Sends where a GAP call returned an error when it was queued. Failures the controller reports later in the GAP
@@ -0,0 +1,419 @@
"""One device per Xiaomi platform (20 listener-only platforms, the three that stay components, plus ATC/PVVX):
writes frames_all.hpp (injected by xball.yaml), xball_entities.yaml (the platform configs, each entity logs
"PUB <dev>.<key> <value>" on every publish) and expected_all.json (publishes per entity) for count_all.py.
Usage: gen_all.py
Every frame is injected twice in a row: MiBeacon devices drop the repeat by frame counter, the FD50/181D/181A
parsers without a counter publish it again. Binary sensors only publish on a change (trigger_on_initial_state on)."""
import json
from pathlib import Path
import struct
import mibeacon
from mibeacon import XB_FILL, c_rows, o, plain, t16, th
HERE = (
Path(__file__).resolve().parent
) # count_all.py and run_all.sh read the outputs here
CYCLES = 10
KEY = bytes.fromhex("6c0d1a3e5b7f29c4a18e90d2f3b46a57")
KEYHEX = KEY.hex()
def mac(i):
return bytes.fromhex(f"A4C138001{i:03X}"[:12])
def enc(m, pid, cnt, obj, short=False, fc=None):
return mibeacon.enc(m, pid, cnt, obj, KEY, short, fc)
# name: (platform, kind, yaml body, sensors {key: publishes-per-new-frame or "bin"}, frame builder k -> [(uuid, data)])
DEVS = {}
def dev(name, platform, body, ents, build, moved=True, hub=None):
DEVS[name] = {
"platform": platform,
"body": body,
"ents": ents,
"build": build,
"moved": moved,
"hub": hub,
}
FE95, FD50, S181D, S181A, S181B = 0xFE95, 0xFD50, 0x181D, 0x181A, 0x181B
# moved platforms (sensor)
dev(
"cgd1",
"xiaomi_cgd1",
"bindkey",
{"temperature": "s"},
lambda m, k: [(FE95, enc(m, 0x0576, k, t16(30 + k / 10)))],
)
dev(
"cgdk2",
"xiaomi_cgdk2",
"bindkey",
{"temperature": "s"},
lambda m, k: [(FE95, enc(m, 0x066F, k, t16(31 + k / 10)))],
)
dev(
"cgg1",
"xiaomi_cgg1",
"",
{"temperature": "s", "humidity": "s"},
lambda m, k: [(FE95, plain(m, 0x0347, k, th(32 + k / 10, 40 + k)))],
)
dev(
"gcls002",
"xiaomi_gcls002",
"",
{"temperature": "s"},
lambda m, k: [(FE95, plain(m, 0x03BC, k, t16(33 + k / 10)))],
)
dev(
"hhccjcy01",
"xiaomi_hhccjcy01",
"",
{"temperature": "s", "moisture": "s"},
lambda m, k: [
(FE95, plain(m, 0x0098, 2 * k, t16(34 + k / 10))),
(FE95, plain(m, 0x0098, 2 * k + 1, o(0x1008, bytes([k + 10])))),
],
)
dev(
"hhccjcy10",
"xiaomi_hhccjcy10",
"",
{
"temperature": "r",
"moisture": "r",
"illuminance": "r",
"conductivity": "r",
"battery_level": "r",
},
lambda m, k: [
(
FD50,
bytes([k + 20])
+ struct.pack(">h", 350 + k)
+ (1000 + k).to_bytes(3, "big")
+ bytes([90 - k])
+ struct.pack(">H", 300 + k),
)
],
)
dev(
"hhccpot002",
"xiaomi_hhccpot002",
"",
{"moisture": "s"},
lambda m, k: [(FE95, plain(m, 0x015D, k, o(0x1008, bytes([k + 30]))))],
)
dev(
"jqjcy01ym",
"xiaomi_jqjcy01ym",
"",
{"formaldehyde": "s"},
lambda m, k: [(FE95, plain(m, 0x02DF, k, o(0x1010, struct.pack("<H", k))))],
)
dev(
"lywsd02",
"xiaomi_lywsd02",
"",
{"temperature": "s"},
lambda m, k: [(FE95, plain(m, 0x045B, k, t16(36 + k / 10)))],
)
dev(
"lywsd02mmc",
"xiaomi_lywsd02mmc",
"bindkey",
{"temperature": "s", "humidity": "s"},
lambda m, k: [(FE95, enc(m, 0x2542, k, th(37 + k / 10, 50 + k), short=True))],
)
dev(
"lywsd03mmc",
"xiaomi_lywsd03mmc",
"bindkey",
{"temperature": "s"},
lambda m, k: [(FE95, enc(m, 0x055B, k, t16(38 + k / 10)))],
)
dev(
"lywsdcgq",
"xiaomi_lywsdcgq",
"",
{"temperature": "s", "humidity": "s"},
lambda m, k: [(FE95, plain(m, 0x01AA, k, th(39 + k / 10, 60 + k)))],
)
dev(
"mhoc303",
"xiaomi_mhoc303",
"",
{"temperature": "s", "humidity": "s"},
lambda m, k: [(FE95, plain(m, 0x06D3, k, th(40 + k / 10, 70 + k)))],
)
dev(
"mhoc401",
"xiaomi_mhoc401",
"bindkey",
{"temperature": "s"},
lambda m, k: [(FE95, enc(m, 0x0387, k, t16(41 + k / 10)))],
)
dev(
"miscale",
"xiaomi_miscale",
"",
{"weight": "r"},
lambda m, k: [
(
S181D,
bytes([0x22])
+ struct.pack("<H", 14000 + 20 * k)
+ bytes([0xE8, 0x07, 11, 14, 12, 0, k]),
)
],
)
dev(
"xmwsdj04mmc",
"xiaomi_xmwsdj04mmc",
"bindkey",
{"temperature": "s", "humidity": "s"},
lambda m, k: [(FE95, enc(m, 0x1203, k, th(42 + k / 10, 45 + k), short=True))],
)
# moved platforms (binary_sensor; the main entity is the platform itself)
dev(
"cgpr1",
"xiaomi_cgpr1",
"bindkey",
{"": "b", "idle_time": "s"},
lambda m, k: [
(
FE95,
enc(
m,
0x0A83,
k,
o(0x1017, struct.pack("<I", 0 if k % 2 else 60 * k)),
short=True,
),
)
],
)
dev(
"mccgq02hl",
"xiaomi_mccgq02hl",
"bindkey",
{"": "b"},
lambda m, k: [(FE95, enc(m, 0x098B, k, o(0x0019, bytes([k % 2]))))],
)
dev(
"mjyd02yla",
"xiaomi_mjyd02yla",
"bindkey",
{"": "z", "light": "b"},
lambda m, k: [(FE95, enc(m, 0x07F6, k, o(0x1018, bytes([k % 2]))))],
)
dev(
"wx08zm",
"xiaomi_wx08zm",
"",
{"": "b", "tablet": "s"},
lambda m, k: [
(
FE95,
plain(
m, 0x040A, k, o(0x1012, bytes([k % 2])) + o(0x1013, bytes([100 - k]))
),
)
],
)
# still components
dev(
"mue4094rt",
"xiaomi_mue4094rt",
"",
{"": "m"},
lambda m, k: [
(FE95, plain(m, 0x03DD, k, o(0x000F, (200 + k).to_bytes(3, "little")), fc=0x40))
],
moved=False,
)
dev(
"rtcgq02lm",
"xiaomi_rtcgq02lm",
"bindkey",
{"light": "b", "battery_level": "s"},
lambda m, k: [
(FE95, enc(m, 0x0A8D, 2 * k, o(0x1018, bytes([k % 2])))),
(FE95, enc(m, 0x0A8D, 2 * k + 1, o(0x100A, bytes([80 + k])))),
],
moved=False,
hub=True,
)
def body_scale(m, k):
obj = o(
0x4E16,
bytes([1])
+ struct.pack("<I", 7000 + 10 * k)
+ struct.pack("<I", 1700000000 + k),
)
assert len(obj) == 12
return [(FE95, enc(m, 0x1234, k, obj, short=True))]
dev(
"body_scale",
"xiaomi_body_scale",
"bindkey",
{"weight": "s", "profile_id": "s"},
body_scale,
moved=False,
)
dev(
"atc",
"atc_mithermometer",
"",
{"temperature": "s"},
lambda m, k: [
(
S181A,
m
+ struct.pack(">h", 250 + k)
+ bytes([45, 90])
+ struct.pack(">H", 2950)
+ bytes([k]),
)
],
moved=None,
)
dev(
"pvvx",
"pvvx_mithermometer",
"",
{"temperature": "s"},
lambda m, k: [
(S181A, m[::-1] + struct.pack("<hHH", 2600 + k, 4600, 3010) + bytes([88, k, 0]))
],
moved=None,
)
MACS = {n: mac(i + 1) for i, n in enumerate(DEVS)}
frames = [] # (dev index, uuid, data, cycle)
for k in range(1, CYCLES + 1):
for i, (n, d) in enumerate(DEVS.items()):
for uuid, data in d["build"](MACS[n], k):
assert (
len(data) <= 24
or uuid != FE95
or not (data[0] & 0x08)
or n == "mccgq02hl"
), (n, len(data))
frames.append((i, uuid, data))
frames.append((i, uuid, data)) # repeat: dropped by counter-based dedup
# Expected publishes per entity
exp = {}
for n, d in DEVS.items():
for key, kind in d["ents"].items():
ent = f"{n}.{key or 'main'}"
if kind == "s":
exp[ent] = CYCLES
elif kind == "r": # no counter: the repeat publishes again
exp[ent] = CYCLES * 2
elif (
kind == "b"
): # alternating value, dedup on change; mccgq02hl starts closed = the initial off state
exp[ent] = CYCLES - (n == "mccgq02hl")
elif kind == "z": # configured, no frame sets it
exp[ent] = 0
elif kind == "m": # mue4094rt: motion true once, false 5 s after the last frame
exp[ent] = 2
HERE.joinpath("expected_all.json").write_text(
json.dumps(exp, indent=1, sort_keys=True), encoding="utf-8"
)
frame_rows = [
((bytes([i, uuid & 0xFF, uuid >> 8, len(data)]) + data).ljust(28, b"\0"), "")
for i, uuid, data in frames
]
per = len(frames) // CYCLES
HDR = '#pragma once\n#include <cstring>\n#include "esphome/components/esp32_ble/ble_scan_result.h"\n'
INJECT = (
XB_FILL
+ """
inline void xa_inject(size_t first, size_t last) {
for (size_t i = first; i < last && i < XA_COUNT; i++) {
const uint8_t *row = XA_FRAMES[i];
xb_inject_one(XA_MACS[row[0]], row[1] | row[2] << 8, row + 4, row[3]);
}
}
"""
)
HERE.joinpath("frames_all.hpp").write_text(
HDR
+ c_rows("XA_MACS", [(m, n) for n, m in MACS.items()])
+ c_rows("XA_FRAMES", frame_rows)
+ f"static constexpr size_t XA_COUNT = {len(frames)};\n"
+ f"static constexpr size_t XA_PER_CYCLE = {per};\n"
+ INJECT,
encoding="utf-8",
)
def macstr(m):
return ":".join(f"{b:02X}" for b in m)
def ent_yaml(ent, kind, ind):
"""name plus a publish log at indent ind."""
sp = " " * ind
if kind in ("b", "m", "z"):
return (
f"{sp}trigger_on_initial_state: true\n{sp}on_state:\n"
f'{sp} - lambda: \'ESP_LOGI("pub", "PUB {ent} %d", x);\'\n'
)
return f'{sp}on_value:\n{sp} - lambda: \'ESP_LOGI("pub", "PUB {ent} %.3f", x);\'\n'
sensor, binary, top = [], [], []
for n, d in DEVS.items():
m = macstr(MACS[n])
common = f" mac_address: {m}\n" + (
f" bindkey: {KEYHEX}\n" if d["body"] == "bindkey" else ""
)
subs = {key: kind for key, kind in d["ents"].items() if key}
if d["hub"]:
top.append(f"{d['platform']}:\n - id: hub_{n}\n" + common)
for kinds, lst in (({"b"}, binary), ({"s", "r"}, sensor)):
blk = f" - platform: {d['platform']}\n id: hub_{n}\n"
for key, kind in subs.items():
if kind in kinds:
blk += f' {key}:\n name: "{n} {key}"\n' + ent_yaml(
f"{n}.{key}", kind, 6
)
lst.append(blk)
continue
blk = f" - platform: {d['platform']}\n" + common
if "" in d["ents"]:
blk += f' name: "{n} main"\n' + ent_yaml(f"{n}.main", d["ents"][""], 4)
for key, kind in subs.items():
blk += f' {key}:\n name: "{n} {key}"\n' + ent_yaml(
f"{n}.{key}", kind, 6
)
(binary if "" in d["ents"] else sensor).append(blk)
out = (
"".join(top)
+ "\nsensor:\n"
+ "".join(sensor)
+ "\nbinary_sensor:\n"
+ "".join(binary)
)
HERE.joinpath("xball_entities.yaml").write_text(out, encoding="utf-8")
print(
"devices", len(DEVS), "frames", len(frames), "per cycle", per, "entities", len(exp)
)
@@ -0,0 +1,84 @@
"""MiBeacon frame builders and the C++ scan result injector shared by gen.py and gen_all.py."""
import struct
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
def plain(mac, pid, cnt, obj, fc=0x50):
"""Unencrypted MiBeacon; the MAC is included when fc has bit 0x10."""
hdr = bytes([fc, 0x20]) + struct.pack("<H", pid) + bytes([cnt])
return hdr + (mac[::-1] if fc & 0x10 else b"") + obj
def enc(mac, pid, cnt, obj, key, short=False, fc=None, version=0x59, corrupt=False):
"""Encrypted MiBeacon v4/v5 (AES-CCM, tag 4, AAD 0x11): long with the MAC (fc 0x58), short without (fc 0x48).
corrupt flips the first tag byte."""
ext = bytes([cnt, 0x00, 0x00])
hdr = (
bytes([fc or (0x48 if short else 0x58), version])
+ struct.pack("<H", pid)
+ bytes([cnt])
)
if not short:
hdr += mac[::-1]
nonce = mac[::-1] + hdr[2:5] + ext
ct = AESCCM(key, tag_length=4).encrypt(nonce, obj, b"\x11")
body, tag = ct[:-4], ct[-4:]
if corrupt:
tag = bytes([tag[0] ^ 0xFF]) + tag[1:]
return hdr + body + ext + tag
def o(t, data):
"""One MiBeacon object: type, length, data."""
return struct.pack("<H", t) + bytes([len(data)]) + data
def t16(v):
"""0x1004 temperature object."""
return o(0x1004, struct.pack("<h", round(v * 10)))
def th(t, h):
"""0x100D temperature and humidity object."""
return o(0x100D, struct.pack("<hh", round(t * 10), round(h * 10)))
def c_rows(name, rows):
"""A C byte table from (bytes, comment) rows of equal length."""
out = [f"static const uint8_t {name}[{len(rows)}][{len(rows[0][0])}] = {{"]
out += [
" {" + ",".join(f"0x{b:02X}" for b in row) + "}," + (f" // {c}" if c else "")
for row, c in rows
]
return "\n".join([*out, "};", ""])
# Fills a scan result with flags plus one service data field (16 bit uuid) and hands it to the tracker.
XB_FILL = """
inline void xb_fill(esphome::esp32_ble::BLEScanResult &r, const uint8_t *mac, uint16_t uuid, const uint8_t *data,
uint8_t len) {
memcpy(r.bda, mac, 6);
r.ble_addr_type = 0;
r.rssi = -50;
uint8_t n = 0;
r.ble_adv[n++] = 2;
r.ble_adv[n++] = 0x01;
r.ble_adv[n++] = 0x06;
r.ble_adv[n++] = len + 3;
r.ble_adv[n++] = 0x16;
r.ble_adv[n++] = uuid & 0xFF;
r.ble_adv[n++] = uuid >> 8;
memcpy(r.ble_adv + n, data, len);
r.adv_data_len = n + len;
r.scan_rsp_len = 0;
r.search_evt = ESP_GAP_SEARCH_INQ_RES_EVT;
}
inline void xb_inject_one(const uint8_t *mac, uint16_t uuid, const uint8_t *data, uint8_t len) {
esphome::esp32_ble::BLEScanResult r{};
xb_fill(r, mac, uuid, data, len);
esphome::esp32_ble_tracker::global_esp32_ble_tracker->gap_scan_event_handler(r);
}
"""
+20
View File
@@ -0,0 +1,20 @@
#!/bin/bash
# run_all.sh TREE NAME PORT: make run_NAME.yaml (xball.yaml + xball_entities.yaml), build it from TREE, flash over USB,
# capture 75 s from a reset and count the publishes. Run python gen_all.py first.
# env: BUILD_LOCK (optional), PY (default TREE's venv python)
D=$(cd "$(dirname "$0")" && pwd); T=${1:?tree}; N=${2:?name}; P=${3:?port}
export PY=${PY:-$T/venv/bin/python}
SC=$(dirname "$D")/serial_capture.py; B=$(dirname "$D")/pio_build.sh
{ sed "s/^ name: xball$/ name: $N/" "$D/xball.yaml"; cat "$D/xball_entities.yaml"; } > "$D/run_$N.yaml"
TREE=$T bash $B compile "$N" "$D/run_$N.yaml" || exit 1
pkill -f "[s]erial_capture.py $P "
TREE=$T bash $B upload "$N" "$D/run_$N.yaml" "$P" || exit 1
sleep 3
$PY "$SC" "$P" 75 "$D/$N-cap.txt" --reset > /dev/null 2>&1
COUNT=$($PY "$D/count_all.py" "$D/$N-cap.txt"); RC=$?
echo "$COUNT" | tail -2
grep -a "IDLE" "$D/$N-cap.txt"
CRASH=$(grep -a -c 'Guru\|abort()\|Backtrace' "$D/$N-cap.txt")
echo "crash lines: $CRASH"
[ "$CRASH" -eq 0 ] || RC=1
exit $RC
@@ -0,0 +1,45 @@
# All Xiaomi BLE platforms on one ESP32-S3 (ESP-IDF), frames from gen_all.py injected into the tracker.
# Build: python gen_all.py, then esphome -s name <name> compile xball.yaml. No network: logs on USB_SERIAL_JTAG.
substitutions:
name: xball
esphome:
name: ${name}
includes:
- frames_all.hpp
esp32:
board: esp32-s3-devkitc-1
framework:
type: esp-idf
logger:
level: DEBUG
hardware_uart: USB_SERIAL_JTAG
esp32_ble_tracker:
globals:
- id: xa_cycle
type: int
initial_value: "0"
interval:
- interval: 1s
startup_delay: 20s
then:
- lambda: |-
int c = id(xa_cycle);
if (c == 0)
ESP_LOGI("heap", "IDLE before inject: free %u internal %u min %u largest %u", (unsigned) esp_get_free_heap_size(),
(unsigned) heap_caps_get_free_size(MALLOC_CAP_INTERNAL), (unsigned) esp_get_minimum_free_heap_size(),
(unsigned) heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL));
if ((size_t) c * XA_PER_CYCLE < XA_COUNT) {
ESP_LOGI("xa", "inject cycle %d", c + 1);
xa_inject(c * XA_PER_CYCLE, (c + 1) * XA_PER_CYCLE);
} else if ((size_t) c * XA_PER_CYCLE == XA_COUNT + 10 * XA_PER_CYCLE) {
ESP_LOGI("xa", "INJECT DONE");
ESP_LOGI("heap", "IDLE after inject: free %u internal %u min %u", (unsigned) esp_get_free_heap_size(),
(unsigned) heap_caps_get_free_size(MALLOC_CAP_INTERNAL), (unsigned) esp_get_minimum_free_heap_size());
}
id(xa_cycle)++;
@@ -0,0 +1,24 @@
esphome:
name: xbsp
esp32:
board: esp32dev
framework:
type: esp-idf
logger:
esp32_ble_tracker:
sensor:
- platform: xiaomi_lywsd03mmc
mac_address: A4:C1:38:00:20:01
bindkey: 6c0d1a3e5b7f29c4a18e90d2f3b46a57
setup_priority: 600
temperature:
name: t
binary_sensor:
- platform: xiaomi_wx08zm
mac_address: A4:C1:38:00:20:02
name: w
setup_priority: 600
- platform: xiaomi_mue4094rt
mac_address: A4:C1:38:00:20:03
name: m
setup_priority: 600
+10 -4
View File
@@ -515,12 +515,18 @@ void CC1101Component::dump_config() {
}
void CC1101Component::begin_tx() {
if (this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO)) {
ESP_LOGW(TAG, "begin_tx is for async serial mode; use send_packet in packet mode");
return;
}
this->wake_up();
// Read the mode before the PKTCTRL0 write below overwrites it in state_
const bool packet_mode = this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO);
// Ensure Packet Format is 3 (Async Serial)
this->write_(Register::PKTCTRL0, 0x32);
ESP_LOGV(TAG, "Beginning TX sequence");
if (this->gdo0_pin_ != nullptr) {
// setup() only attaches the interrupt in packet mode. In async serial mode the pin may be
// shared with remote_receiver, and detaching here would remove the receiver's interrupt.
if (packet_mode) {
this->gdo0_pin_->detach_interrupt();
}
this->gdo0_pin_->pin_mode(gpio::FLAG_OUTPUT);
}
// Transition through IDLE to bypass CCA (Clear Channel Assessment) which can
@@ -63,25 +63,13 @@ void CST816Touchscreen::update_touches() {
uint8_t data[13];
if (!this->status_comm_result(this->read_bytes(REG_STATUS, data, sizeof data)))
return;
// The low nibble is the touch count. CST816/CST820 are single-touch;
// CST826/CST836 can report two points, but this driver only reads the first
// point. Some CST8xx controllers report an all-0xFF frame when idle or after
// disabling auto-sleep: the count lands above two and the coordinates are
// the 0xFFF sentinel, so both guards below reject it.
uint8_t num_of_touches = low_nibble(data[REG_TOUCH_NUM]);
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
if (num_of_touches == 0) {
return;
}
if (num_of_touches > 2) {
return;
}
uint16_t x = encode_uint16(low_nibble(data[REG_XPOS_HIGH]), data[REG_XPOS_LOW]);
uint16_t y = encode_uint16(low_nibble(data[REG_YPOS_HIGH]), data[REG_YPOS_LOW]);
if (x == 0x0FFF || y == 0x0FFF) {
return;
}
ESP_LOGV(TAG, "Read touch %d/%d", x, y);
this->add_raw_touch_position_(0, x, y);
}
-2
View File
@@ -94,7 +94,5 @@ FILTER_SOURCE_FILES = filter_source_files_from_platform(
PlatformFramework.LN882X_ARDUINO,
},
"debug_zephyr.cpp": {PlatformFramework.NRF52_ZEPHYR},
"debug_nrf52.cpp": {PlatformFramework.NRF52_ZEPHYR},
"debug_nrf54l.cpp": {PlatformFramework.NRF52_ZEPHYR},
}
)
-380
View File
@@ -1,380 +0,0 @@
#include "debug_component.h"
#ifdef USE_ZEPHYR
#include <nrfx.h>
#endif
#if defined(USE_ZEPHYR) && !defined(NRF54L_SERIES)
#include <cinttypes>
#include <cstdint>
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <hal/nrf_power.h>
#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
namespace esphome::debug {
ESPHOME_LOG_TAG(TAG, "debug");
// Check if an nRF peripheral's ENABLE register indicates it is enabled.
// periph: peripheral register prefix (e.g. USBD, UARTE, SPI)
// reg: register block pointer (e.g. NRF_USBD, NRF_UARTE0)
#define NRF_PERIPH_ENABLED(periph, reg) \
YESNO(((reg)->ENABLE & periph##_ENABLE_ENABLE_Msk) == (periph##_ENABLE_ENABLE_Enabled << periph##_ENABLE_ENABLE_Pos))
constexpr std::uintptr_t MBR_PARAM_PAGE_ADDR = 0xFFC;
constexpr std::uintptr_t MBR_BOOTLOADER_ADDR = 0xFF8;
static inline uint32_t read_mem_u32(uintptr_t addr) {
// NOLINTNEXTLINE(performance-no-int-to-ptr,clang-analyzer-core.FixedAddressDereference)
return *reinterpret_cast<volatile uint32_t *>(addr);
}
static inline uint8_t read_mem_u8(uintptr_t addr) {
// NOLINTNEXTLINE(performance-no-int-to-ptr,clang-analyzer-core.FixedAddressDereference)
return *reinterpret_cast<volatile uint8_t *>(addr);
}
// defines from https://github.com/adafruit/Adafruit_nRF52_Bootloader which prints those information
constexpr uint32_t SD_MAGIC_NUMBER = 0x51B1E5DB;
constexpr uintptr_t MBR_SIZE = 0x1000;
constexpr uintptr_t SOFTDEVICE_INFO_STRUCT_OFFSET = 0x2000;
constexpr uintptr_t SD_ID_OFFSET = SOFTDEVICE_INFO_STRUCT_OFFSET + 0x10;
constexpr uintptr_t SD_VERSION_OFFSET = SOFTDEVICE_INFO_STRUCT_OFFSET + 0x14;
static inline bool is_sd_present() {
return read_mem_u32(SOFTDEVICE_INFO_STRUCT_OFFSET + MBR_SIZE + 4) == SD_MAGIC_NUMBER;
}
static inline uint32_t sd_id_get() {
if (read_mem_u8(MBR_SIZE + SOFTDEVICE_INFO_STRUCT_OFFSET) > (SD_ID_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) {
return read_mem_u32(MBR_SIZE + SD_ID_OFFSET);
}
return 0;
}
static inline uint32_t sd_version_get() {
if (read_mem_u8(MBR_SIZE + SOFTDEVICE_INFO_STRUCT_OFFSET) > (SD_VERSION_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) {
return read_mem_u32(MBR_SIZE + SD_VERSION_OFFSET);
}
return 0;
}
#ifdef ESPHOME_LOG_HAS_VERBOSE
// NOLINTBEGIN(clang-analyzer-core.FixedAddressDereference) -- nRF peripheral registers are MMIO at fixed addresses
static void log_peripherals_info() {
// most peripherals are enabled only when in use so ESP_LOGV is enough
ESP_LOGV(TAG, "Peripherals status:");
ESP_LOGV(TAG, " USBD: %-3s| UARTE0: %-3s| UARTE1: %-3s| UART0: %-3s", //
NRF_PERIPH_ENABLED(USBD, NRF_USBD), NRF_PERIPH_ENABLED(UARTE, NRF_UARTE0),
NRF_PERIPH_ENABLED(UARTE, NRF_UARTE1), NRF_PERIPH_ENABLED(UART, NRF_UART0));
ESP_LOGV(TAG, " TWIS0: %-3s| TWIS1: %-3s| TWIM0: %-3s| TWIM1: %-3s", //
NRF_PERIPH_ENABLED(TWIS, NRF_TWIS0), NRF_PERIPH_ENABLED(TWIS, NRF_TWIS1),
NRF_PERIPH_ENABLED(TWIM, NRF_TWIM0), NRF_PERIPH_ENABLED(TWIM, NRF_TWIM1));
ESP_LOGV(TAG, " TWI0: %-3s| TWI1: %-3s| COMP: %-3s| CCM: %-3s", //
NRF_PERIPH_ENABLED(TWI, NRF_TWI0), NRF_PERIPH_ENABLED(TWI, NRF_TWI1), NRF_PERIPH_ENABLED(COMP, NRF_COMP),
NRF_PERIPH_ENABLED(CCM, NRF_CCM));
ESP_LOGV(TAG, " PDM: %-3s| SPIS0: %-3s| SPIS1: %-3s| SPIS2: %-3s", //
NRF_PERIPH_ENABLED(PDM, NRF_PDM), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS0), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS1),
NRF_PERIPH_ENABLED(SPIS, NRF_SPIS2));
ESP_LOGV(TAG, " SPIM0: %-3s| SPIM1: %-3s| SPIM2: %-3s| SPIM3: %-3s", //
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM0), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM1),
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM2), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM3));
ESP_LOGV(TAG, " SPI0: %-3s| SPI1: %-3s| SPI2: %-3s| SAADC: %-3s", //
NRF_PERIPH_ENABLED(SPI, NRF_SPI0), NRF_PERIPH_ENABLED(SPI, NRF_SPI1), NRF_PERIPH_ENABLED(SPI, NRF_SPI2),
NRF_PERIPH_ENABLED(SAADC, NRF_SAADC));
ESP_LOGV(TAG, " QSPI: %-3s| QDEC: %-3s| LPCOMP: %-3s| I2S: %-3s", //
NRF_PERIPH_ENABLED(QSPI, NRF_QSPI), NRF_PERIPH_ENABLED(QDEC, NRF_QDEC),
NRF_PERIPH_ENABLED(LPCOMP, NRF_LPCOMP), NRF_PERIPH_ENABLED(I2S, NRF_I2S));
ESP_LOGV(TAG, " PWM0: %-3s| PWM1: %-3s| PWM2: %-3s| PWM3: %-3s", //
NRF_PERIPH_ENABLED(PWM, NRF_PWM0), NRF_PERIPH_ENABLED(PWM, NRF_PWM1), NRF_PERIPH_ENABLED(PWM, NRF_PWM2),
NRF_PERIPH_ENABLED(PWM, NRF_PWM3));
ESP_LOGV(TAG, " AAR: %-3s| QSPI deep power-down:%-3s| CRYPTOCELL: %-3s", NRF_PERIPH_ENABLED(AAR, NRF_AAR),
YESNO((NRF_QSPI->IFCONFIG0 & QSPI_IFCONFIG0_DPMENABLE_Msk) ==
(QSPI_IFCONFIG0_DPMENABLE_Enable << QSPI_IFCONFIG0_DPMENABLE_Pos)),
YESNO((NRF_CRYPTOCELL->ENABLE & CRYPTOCELL_ENABLE_ENABLE_Msk) ==
(CRYPTOCELL_ENABLE_ENABLE_Enabled << CRYPTOCELL_ENABLE_ENABLE_Pos)));
}
// NOLINTEND(clang-analyzer-core.FixedAddressDereference)
#endif
static const char *regout0_to_str(uint32_t value) {
switch (value) {
case (UICR_REGOUT0_VOUT_DEFAULT):
return "1.8V (default)";
case (UICR_REGOUT0_VOUT_1V8):
return "1.8V";
case (UICR_REGOUT0_VOUT_2V1):
return "2.1V";
case (UICR_REGOUT0_VOUT_2V4):
return "2.4V";
case (UICR_REGOUT0_VOUT_2V7):
return "2.7V";
case (UICR_REGOUT0_VOUT_3V0):
return "3.0V";
case (UICR_REGOUT0_VOUT_3V3):
return "3.3V";
default:
break;
}
return "???V";
}
size_t DebugComponent::get_device_info_(std::span<char, DEVICE_INFO_BUFFER_SIZE> buffer, size_t pos) {
constexpr size_t size = DEVICE_INFO_BUFFER_SIZE;
char *buf = buffer.data();
// Main supply status
// NOLINTNEXTLINE(clang-analyzer-core.FixedAddressDereference) -- NRF_POWER is MMIO at a fixed address
auto regstatus = nrf_power_mainregstatus_get(NRF_POWER);
const char *supply_status = (regstatus == NRF_POWER_MAINREGSTATUS_NORMAL) ? "Normal voltage." : "High voltage.";
ESP_LOGD(TAG, "Main supply status: %s", supply_status);
pos = buf_append_str(buf, size, pos, "|Main supply status: ");
pos = buf_append_str(buf, size, pos, supply_status);
// Regulator stage 0
if (nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) {
const char *reg0_type = nrf_power_dcdcen_vddh_get(NRF_POWER) ? "DC/DC" : "LDO";
const char *reg0_voltage = regout0_to_str((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) >> UICR_REGOUT0_VOUT_Pos);
ESP_LOGD(TAG, "Regulator stage 0: %s, %s", reg0_type, reg0_voltage);
pos = buf_append_str(buf, size, pos, "|Regulator stage 0: ");
pos = buf_append_str(buf, size, pos, reg0_type);
pos = buf_append_str(buf, size, pos, ", ");
pos = buf_append_str(buf, size, pos, reg0_voltage);
#ifdef USE_NRF52_REG0_VOUT
if ((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) >> UICR_REGOUT0_VOUT_Pos != USE_NRF52_REG0_VOUT) {
ESP_LOGE(TAG, "Regulator stage 0: expected %s", regout0_to_str(USE_NRF52_REG0_VOUT));
}
#endif
} else {
ESP_LOGD(TAG, "Regulator stage 0: disabled");
pos = buf_append_str(buf, size, pos, "|Regulator stage 0: disabled");
}
// Regulator stage 1
const char *reg1_type = nrf_power_dcdcen_get(NRF_POWER) ? "DC/DC" : "LDO";
ESP_LOGD(TAG, "Regulator stage 1: %s", reg1_type);
pos = buf_append_str(buf, size, pos, "|Regulator stage 1: ");
pos = buf_append_str(buf, size, pos, reg1_type);
// USB power state
const char *usb_state;
if (nrf_power_usbregstatus_vbusdet_get(NRF_POWER)) {
usb_state = nrf_power_usbregstatus_outrdy_get(NRF_POWER) ? "ready" : "connected (regulator is not ready)";
} else {
usb_state = "disconnected";
}
ESP_LOGD(TAG, "USB power state: %s", usb_state);
pos = buf_append_str(buf, size, pos, "|USB power state: ");
pos = buf_append_str(buf, size, pos, usb_state);
// Power-fail comparator
bool enabled;
nrf_power_pof_thr_t pof_thr = nrf_power_pofcon_get(NRF_POWER, &enabled);
if (enabled) {
const char *pof_voltage;
switch (pof_thr) {
case POWER_POFCON_THRESHOLD_V17:
pof_voltage = "1.7V";
break;
case POWER_POFCON_THRESHOLD_V18:
pof_voltage = "1.8V";
break;
case POWER_POFCON_THRESHOLD_V19:
pof_voltage = "1.9V";
break;
case POWER_POFCON_THRESHOLD_V20:
pof_voltage = "2.0V";
break;
case POWER_POFCON_THRESHOLD_V21:
pof_voltage = "2.1V";
break;
case POWER_POFCON_THRESHOLD_V22:
pof_voltage = "2.2V";
break;
case POWER_POFCON_THRESHOLD_V23:
pof_voltage = "2.3V";
break;
case POWER_POFCON_THRESHOLD_V24:
pof_voltage = "2.4V";
break;
case POWER_POFCON_THRESHOLD_V25:
pof_voltage = "2.5V";
break;
case POWER_POFCON_THRESHOLD_V26:
pof_voltage = "2.6V";
break;
case POWER_POFCON_THRESHOLD_V27:
pof_voltage = "2.7V";
break;
case POWER_POFCON_THRESHOLD_V28:
pof_voltage = "2.8V";
break;
default:
pof_voltage = "???V";
break;
}
if (nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) {
const char *vddh_voltage;
switch (nrf_power_pofcon_vddh_get(NRF_POWER)) {
case NRF_POWER_POFTHRVDDH_V27:
vddh_voltage = "2.7V";
break;
case NRF_POWER_POFTHRVDDH_V28:
vddh_voltage = "2.8V";
break;
case NRF_POWER_POFTHRVDDH_V29:
vddh_voltage = "2.9V";
break;
case NRF_POWER_POFTHRVDDH_V30:
vddh_voltage = "3.0V";
break;
case NRF_POWER_POFTHRVDDH_V31:
vddh_voltage = "3.1V";
break;
case NRF_POWER_POFTHRVDDH_V32:
vddh_voltage = "3.2V";
break;
case NRF_POWER_POFTHRVDDH_V33:
vddh_voltage = "3.3V";
break;
case NRF_POWER_POFTHRVDDH_V34:
vddh_voltage = "3.4V";
break;
case NRF_POWER_POFTHRVDDH_V35:
vddh_voltage = "3.5V";
break;
case NRF_POWER_POFTHRVDDH_V36:
vddh_voltage = "3.6V";
break;
case NRF_POWER_POFTHRVDDH_V37:
vddh_voltage = "3.7V";
break;
case NRF_POWER_POFTHRVDDH_V38:
vddh_voltage = "3.8V";
break;
case NRF_POWER_POFTHRVDDH_V39:
vddh_voltage = "3.9V";
break;
case NRF_POWER_POFTHRVDDH_V40:
vddh_voltage = "4.0V";
break;
case NRF_POWER_POFTHRVDDH_V41:
vddh_voltage = "4.1V";
break;
case NRF_POWER_POFTHRVDDH_V42:
vddh_voltage = "4.2V";
break;
default:
vddh_voltage = "???V";
break;
}
ESP_LOGD(TAG, "Power-fail comparator: %s, VDDH: %s", pof_voltage, vddh_voltage);
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: ");
pos = buf_append_str(buf, size, pos, pof_voltage);
pos = buf_append_str(buf, size, pos, ", VDDH: ");
pos = buf_append_str(buf, size, pos, vddh_voltage);
} else {
ESP_LOGD(TAG, "Power-fail comparator: %s", pof_voltage);
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: ");
pos = buf_append_str(buf, size, pos, pof_voltage);
}
} else {
ESP_LOGD(TAG, "Power-fail comparator: disabled");
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: disabled");
}
auto package = [](uint32_t value) {
switch (value) {
case 0x2004:
return "QIxx - 7x7 73-pin aQFN";
case 0x2000:
return "QFxx - 6x6 48-pin QFN";
case 0x2005:
return "CKxx - 3.544 x 3.607 WLCSP";
default:
break;
}
return "Unspecified";
};
char mac_pretty[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
get_mac_address_pretty_into_buffer(mac_pretty);
ESP_LOGD(TAG,
"nRF debug info:\n"
" Code page size: %u, code size: %u, device id: 0x%08x%08x\n"
" Encryption root: 0x%08x%08x%08x%08x, Identity Root: 0x%08x%08x%08x%08x\n"
" Device address type: %s, address: %s\n"
" Part code: nRF%x, version: %c%c%c%c, package: %s\n"
" RAM: %ukB, Flash: %ukB, production test: %sdone",
NRF_FICR->CODEPAGESIZE, NRF_FICR->CODESIZE, NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0], NRF_FICR->ER[0],
NRF_FICR->ER[1], NRF_FICR->ER[2], NRF_FICR->ER[3], NRF_FICR->IR[0], NRF_FICR->IR[1], NRF_FICR->IR[2],
NRF_FICR->IR[3], (NRF_FICR->DEVICEADDRTYPE & 0x1 ? LOG_STR_LITERAL("Random") : LOG_STR_LITERAL("Public")),
mac_pretty, NRF_FICR->INFO.PART, NRF_FICR->INFO.VARIANT >> 24 & 0xFF, NRF_FICR->INFO.VARIANT >> 16 & 0xFF,
NRF_FICR->INFO.VARIANT >> 8 & 0xFF, NRF_FICR->INFO.VARIANT & 0xFF, package(NRF_FICR->INFO.PACKAGE),
NRF_FICR->INFO.RAM, NRF_FICR->INFO.FLASH,
(NRF_FICR->PRODTEST[0] == 0xBB42319F ? LOG_STR_LITERAL("") : LOG_STR_LITERAL("not ")));
bool n_reset_enabled = NRF_UICR->PSELRESET[0] == NRF_UICR->PSELRESET[1] &&
(NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) == UICR_PSELRESET_CONNECT_Connected
<< UICR_PSELRESET_CONNECT_Pos;
ESP_LOGD(
TAG, " GPIO as NFC pins: %s, GPIO as nRESET pin: %s",
YESNO((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)),
YESNO(n_reset_enabled));
if (n_reset_enabled) {
uint8_t port = (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_PORT_Msk) >> UICR_PSELRESET_PORT_Pos;
uint8_t pin = (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_PIN_Msk) >> UICR_PSELRESET_PIN_Pos;
ESP_LOGD(TAG, " nRESET port P%u.%02u", port, pin);
}
#ifdef USE_BOOTLOADER_MCUBOOT
ESP_LOGD(TAG, " Bootloader: mcuboot");
#else
ESP_LOGD(TAG, " Bootloader: Adafruit, version %u.%u.%u", (BOOTLOADER_VERSION_REGISTER >> 16) & 0xFF,
(BOOTLOADER_VERSION_REGISTER >> 8) & 0xFF, BOOTLOADER_VERSION_REGISTER & 0xFF);
ESP_LOGD(TAG, " MBR bootloader addr 0x%08x, UICR bootloader addr 0x%08x", read_mem_u32(MBR_BOOTLOADER_ADDR),
NRF_UICR->NRFFW[0]);
ESP_LOGD(TAG, " MBR param page addr 0x%08x, UICR param page addr 0x%08x", read_mem_u32(MBR_PARAM_PAGE_ADDR),
NRF_UICR->NRFFW[1]);
if (is_sd_present()) {
uint32_t const sd_id = sd_id_get();
uint32_t const sd_version = sd_version_get();
uint32_t ver[3];
ver[0] = sd_version / 1000000;
ver[1] = (sd_version - ver[0] * 1000000) / 1000;
ver[2] = (sd_version - ver[0] * 1000000 - ver[1] * 1000);
ESP_LOGD(TAG, " SoftDevice: S%u %u.%u.%u", sd_id, ver[0], ver[1], ver[2]);
#ifdef USE_SOFTDEVICE_ID
#ifdef USE_SOFTDEVICE_VERSION
if (USE_SOFTDEVICE_ID != sd_id || USE_SOFTDEVICE_VERSION != ver[0]) {
ESP_LOGE(TAG, "Built for SoftDevice S%u %u.x.y. It may crash due to mismatch of bootloader version.",
USE_SOFTDEVICE_ID, USE_SOFTDEVICE_VERSION);
}
#else
if (USE_SOFTDEVICE_ID != sd_id) {
ESP_LOGE(TAG, "Built for SoftDevice S%u. It may crash due to mismatch of bootloader version.", USE_SOFTDEVICE_ID);
}
#endif
#endif
}
#endif
// 13 words of 8 hex digits, separated by spaces
char uicr_buf[13 * 9];
auto uicr = [&uicr_buf](volatile uint32_t *data, uint8_t size) {
size_t len = 0;
for (size_t i = 0; i < size; i++) {
len = buf_append_printf(uicr_buf, sizeof(uicr_buf), len, i > 0 ? " %08" PRIX32 : "%08" PRIX32, data[i]);
}
return uicr_buf;
};
ESP_LOGD(TAG, " NRFFW %s", uicr(NRF_UICR->NRFFW, 13));
ESP_LOGD(TAG, " NRFHW %s", uicr(NRF_UICR->NRFHW, 12));
#ifdef ESPHOME_LOG_HAS_VERBOSE
log_peripherals_info();
#endif
return pos;
}
#undef NRF_PERIPH_ENABLED
} // namespace esphome::debug
#endif
-113
View File
@@ -1,113 +0,0 @@
#include "debug_component.h"
#ifdef USE_ZEPHYR
#include <nrfx.h>
#endif
#if defined(USE_ZEPHYR) && defined(NRF54L_SERIES)
#include <cinttypes>
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome::debug {
ESPHOME_LOG_TAG(TAG, "debug");
// Check if an nRF peripheral's ENABLE register indicates it is enabled.
// periph: peripheral register prefix (e.g. USBD, UARTE, SPI)
// reg: register block pointer (e.g. NRF_USBD, NRF_UARTE0)
#define NRF_PERIPH_ENABLED(periph, reg) \
YESNO(((reg)->ENABLE & periph##_ENABLE_ENABLE_Msk) == (periph##_ENABLE_ENABLE_Enabled << periph##_ENABLE_ENABLE_Pos))
#ifdef ESPHOME_LOG_HAS_VERBOSE
// NOLINTBEGIN(clang-analyzer-core.FixedAddressDereference) -- nRF peripheral registers are MMIO at fixed addresses
static void log_peripherals_info() {
// most peripherals are enabled only when in use so ESP_LOGV is enough
// UARTE, SPIM, SPIS, TWIM and TWIS with the same number share one instance
ESP_LOGV(TAG, "Peripherals status:");
ESP_LOGV(TAG, " UARTE00: %-3s| UARTE20: %-3s| UARTE21: %-3s| UARTE22: %-3s| UARTE30: %-3s", //
NRF_PERIPH_ENABLED(UARTE, NRF_UARTE00), NRF_PERIPH_ENABLED(UARTE, NRF_UARTE20),
NRF_PERIPH_ENABLED(UARTE, NRF_UARTE21), NRF_PERIPH_ENABLED(UARTE, NRF_UARTE22),
NRF_PERIPH_ENABLED(UARTE, NRF_UARTE30));
ESP_LOGV(TAG, " SPIM00: %-3s| SPIM20: %-3s| SPIM21: %-3s| SPIM22: %-3s| SPIM30: %-3s", //
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM00), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM20),
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM21), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM22),
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM30));
ESP_LOGV(TAG, " SPIS00: %-3s| SPIS20: %-3s| SPIS21: %-3s| SPIS22: %-3s| SPIS30: %-3s", //
NRF_PERIPH_ENABLED(SPIS, NRF_SPIS00), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS20),
NRF_PERIPH_ENABLED(SPIS, NRF_SPIS21), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS22),
NRF_PERIPH_ENABLED(SPIS, NRF_SPIS30));
ESP_LOGV(TAG, " TWIM20: %-3s| TWIM21: %-3s| TWIM22: %-3s| TWIM30: %-3s", //
NRF_PERIPH_ENABLED(TWIM, NRF_TWIM20), NRF_PERIPH_ENABLED(TWIM, NRF_TWIM21),
NRF_PERIPH_ENABLED(TWIM, NRF_TWIM22), NRF_PERIPH_ENABLED(TWIM, NRF_TWIM30));
ESP_LOGV(TAG, " TWIS20: %-3s| TWIS21: %-3s| TWIS22: %-3s| TWIS30: %-3s", //
NRF_PERIPH_ENABLED(TWIS, NRF_TWIS20), NRF_PERIPH_ENABLED(TWIS, NRF_TWIS21),
NRF_PERIPH_ENABLED(TWIS, NRF_TWIS22), NRF_PERIPH_ENABLED(TWIS, NRF_TWIS30));
ESP_LOGV(TAG, " PWM20: %-3s| PWM21: %-3s| PWM22: %-3s| SAADC: %-3s", //
NRF_PERIPH_ENABLED(PWM, NRF_PWM20), NRF_PERIPH_ENABLED(PWM, NRF_PWM21), NRF_PERIPH_ENABLED(PWM, NRF_PWM22),
NRF_PERIPH_ENABLED(SAADC, NRF_SAADC));
ESP_LOGV(TAG, " PDM20: %-3s| PDM21: %-3s| QDEC20: %-3s| QDEC21: %-3s", //
NRF_PERIPH_ENABLED(PDM, NRF_PDM20), NRF_PERIPH_ENABLED(PDM, NRF_PDM21), NRF_PERIPH_ENABLED(QDEC, NRF_QDEC20),
NRF_PERIPH_ENABLED(QDEC, NRF_QDEC21));
ESP_LOGV(TAG, " COMP: %-3s| LPCOMP: %-3s| AAR00: %-3s| CCM00: %-3s", //
NRF_PERIPH_ENABLED(COMP, NRF_COMP), NRF_PERIPH_ENABLED(LPCOMP, NRF_LPCOMP),
NRF_PERIPH_ENABLED(AAR, NRF_AAR00), NRF_PERIPH_ENABLED(CCM, NRF_CCM00));
}
// NOLINTEND(clang-analyzer-core.FixedAddressDereference)
#endif
// NOLINTBEGIN(clang-analyzer-core.FixedAddressDereference) -- nRF registers are MMIO at fixed addresses
size_t DebugComponent::get_device_info_(std::span<char, DEVICE_INFO_BUFFER_SIZE> buffer, size_t pos) {
constexpr size_t size = DEVICE_INFO_BUFFER_SIZE;
char *buf = buffer.data();
// Main regulator
const char *reg_type = (NRF_REGULATORS->VREGMAIN.DCDCEN & REGULATORS_VREGMAIN_DCDCEN_VAL_Msk) ==
(REGULATORS_VREGMAIN_DCDCEN_VAL_Enabled << REGULATORS_VREGMAIN_DCDCEN_VAL_Pos)
? "DC/DC"
: "LDO";
ESP_LOGD(TAG, "Main regulator: %s", reg_type);
pos = buf_append_str(buf, size, pos, "|Main regulator: ");
pos = buf_append_str(buf, size, pos, reg_type);
// Power-fail comparator, the threshold goes from 1.7V up in 0.1V steps
const uint32_t pofcon = NRF_REGULATORS->POFCON;
if ((pofcon & REGULATORS_POFCON_POF_Msk) == (REGULATORS_POFCON_POF_Enabled << REGULATORS_POFCON_POF_Pos)) {
const uint32_t decivolts = 17 + ((pofcon & REGULATORS_POFCON_THRESHOLD_Msk) >> REGULATORS_POFCON_THRESHOLD_Pos);
ESP_LOGD(TAG, "Power-fail comparator: %" PRIu32 ".%" PRIu32 "V", decivolts / 10, decivolts % 10);
pos = buf_append_printf(buf, size, pos, "|Power-fail comparator: %" PRIu32 ".%" PRIu32 "V", decivolts / 10,
decivolts % 10);
} else {
ESP_LOGD(TAG, "Power-fail comparator: disabled");
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: disabled");
}
char mac_pretty[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
get_mac_address_pretty_into_buffer(mac_pretty);
const uint32_t variant = NRF_FICR->INFO.VARIANT;
ESP_LOGD(TAG,
"nRF debug info:\n"
" Device id: 0x%08" PRIx32 "%08" PRIx32 "\n"
" Encryption root: 0x%08" PRIx32 "%08" PRIx32 "%08" PRIx32 "%08" PRIx32 ", Identity Root: 0x%08" PRIx32
"%08" PRIx32 "%08" PRIx32 "%08" PRIx32 "\n"
" Device address type: %s, address: %s\n"
" Part code: 0x%" PRIx32 ", version: %c%c%c%c, package: 0x%" PRIx32 "\n"
" RAM: %" PRIu32 "kB, RRAM: %" PRIu32 "kB",
NRF_FICR->INFO.DEVICEID[1], NRF_FICR->INFO.DEVICEID[0], NRF_FICR->ER[0], NRF_FICR->ER[1], NRF_FICR->ER[2],
NRF_FICR->ER[3], NRF_FICR->IR[0], NRF_FICR->IR[1], NRF_FICR->IR[2], NRF_FICR->IR[3],
(NRF_FICR->DEVICEADDRTYPE & 0x1 ? LOG_STR_LITERAL("Random") : LOG_STR_LITERAL("Public")), mac_pretty,
NRF_FICR->INFO.PART, static_cast<char>(variant >> 24 & 0xFF), static_cast<char>(variant >> 16 & 0xFF),
static_cast<char>(variant >> 8 & 0xFF), static_cast<char>(variant & 0xFF), NRF_FICR->INFO.PACKAGE,
NRF_FICR->INFO.RAM, NRF_FICR->INFO.RRAM);
#ifdef USE_BOOTLOADER_MCUBOOT
ESP_LOGD(TAG, " Bootloader: mcuboot");
#endif
#ifdef ESPHOME_LOG_HAS_VERBOSE
log_peripherals_info();
#endif
return pos;
}
// NOLINTEND(clang-analyzer-core.FixedAddressDereference)
#undef NRF_PERIPH_ENABLED
} // namespace esphome::debug
#endif
+364
View File
@@ -2,14 +2,55 @@
#ifdef USE_ZEPHYR
#include <cinttypes>
#include <climits>
#include "esphome/core/alloc_helpers.h"
#include "esphome/core/log.h"
#include <esphome/components/zephyr/reset_reason.h>
#include <zephyr/drivers/hwinfo.h>
#include <zephyr/kernel.h>
#include <hal/nrf_power.h>
#include <cstdint>
#include <zephyr/storage/flash_map.h>
#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
namespace esphome::debug {
ESPHOME_LOG_TAG(TAG, "debug");
constexpr std::uintptr_t MBR_PARAM_PAGE_ADDR = 0xFFC;
constexpr std::uintptr_t MBR_BOOTLOADER_ADDR = 0xFF8;
static inline uint32_t read_mem_u32(uintptr_t addr) {
// NOLINTNEXTLINE(performance-no-int-to-ptr,clang-analyzer-core.FixedAddressDereference)
return *reinterpret_cast<volatile uint32_t *>(addr);
}
static inline uint8_t read_mem_u8(uintptr_t addr) {
// NOLINTNEXTLINE(performance-no-int-to-ptr,clang-analyzer-core.FixedAddressDereference)
return *reinterpret_cast<volatile uint8_t *>(addr);
}
// defines from https://github.com/adafruit/Adafruit_nRF52_Bootloader which prints those information
constexpr uint32_t SD_MAGIC_NUMBER = 0x51B1E5DB;
constexpr uintptr_t MBR_SIZE = 0x1000;
constexpr uintptr_t SOFTDEVICE_INFO_STRUCT_OFFSET = 0x2000;
constexpr uintptr_t SD_ID_OFFSET = SOFTDEVICE_INFO_STRUCT_OFFSET + 0x10;
constexpr uintptr_t SD_VERSION_OFFSET = SOFTDEVICE_INFO_STRUCT_OFFSET + 0x14;
static inline bool is_sd_present() {
return read_mem_u32(SOFTDEVICE_INFO_STRUCT_OFFSET + MBR_SIZE + 4) == SD_MAGIC_NUMBER;
}
static inline uint32_t sd_id_get() {
if (read_mem_u8(MBR_SIZE + SOFTDEVICE_INFO_STRUCT_OFFSET) > (SD_ID_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) {
return read_mem_u32(MBR_SIZE + SD_ID_OFFSET);
}
return 0;
}
static inline uint32_t sd_version_get() {
if (read_mem_u8(MBR_SIZE + SOFTDEVICE_INFO_STRUCT_OFFSET) > (SD_VERSION_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) {
return read_mem_u32(MBR_SIZE + SD_VERSION_OFFSET);
}
return 0;
}
const char *DebugComponent::get_reset_reason_(std::span<char, RESET_REASON_BUFFER_SIZE> buffer) {
const char *buf = zephyr::get_reset_reason(buffer);
@@ -55,6 +96,329 @@ void DebugComponent::log_partition_info_() {
flash_area_foreach(fa_cb, nullptr);
}
#ifdef ESPHOME_LOG_HAS_VERBOSE
// Check if an nRF peripheral's ENABLE register indicates it is enabled.
// periph: peripheral register prefix (e.g. USBD, UARTE, SPI)
// reg: register block pointer (e.g. NRF_USBD, NRF_UARTE0)
#define NRF_PERIPH_ENABLED(periph, reg) \
YESNO(((reg)->ENABLE & periph##_ENABLE_ENABLE_Msk) == (periph##_ENABLE_ENABLE_Enabled << periph##_ENABLE_ENABLE_Pos))
// NOLINTBEGIN(clang-analyzer-core.FixedAddressDereference) -- nRF peripheral registers are MMIO at fixed addresses
static void log_peripherals_info() {
// most peripherals are enabled only when in use so ESP_LOGV is enough
ESP_LOGV(TAG, "Peripherals status:");
ESP_LOGV(TAG, " USBD: %-3s| UARTE0: %-3s| UARTE1: %-3s| UART0: %-3s", //
NRF_PERIPH_ENABLED(USBD, NRF_USBD), NRF_PERIPH_ENABLED(UARTE, NRF_UARTE0),
NRF_PERIPH_ENABLED(UARTE, NRF_UARTE1), NRF_PERIPH_ENABLED(UART, NRF_UART0));
ESP_LOGV(TAG, " TWIS0: %-3s| TWIS1: %-3s| TWIM0: %-3s| TWIM1: %-3s", //
NRF_PERIPH_ENABLED(TWIS, NRF_TWIS0), NRF_PERIPH_ENABLED(TWIS, NRF_TWIS1),
NRF_PERIPH_ENABLED(TWIM, NRF_TWIM0), NRF_PERIPH_ENABLED(TWIM, NRF_TWIM1));
ESP_LOGV(TAG, " TWI0: %-3s| TWI1: %-3s| COMP: %-3s| CCM: %-3s", //
NRF_PERIPH_ENABLED(TWI, NRF_TWI0), NRF_PERIPH_ENABLED(TWI, NRF_TWI1), NRF_PERIPH_ENABLED(COMP, NRF_COMP),
NRF_PERIPH_ENABLED(CCM, NRF_CCM));
ESP_LOGV(TAG, " PDM: %-3s| SPIS0: %-3s| SPIS1: %-3s| SPIS2: %-3s", //
NRF_PERIPH_ENABLED(PDM, NRF_PDM), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS0), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS1),
NRF_PERIPH_ENABLED(SPIS, NRF_SPIS2));
ESP_LOGV(TAG, " SPIM0: %-3s| SPIM1: %-3s| SPIM2: %-3s| SPIM3: %-3s", //
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM0), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM1),
NRF_PERIPH_ENABLED(SPIM, NRF_SPIM2), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM3));
ESP_LOGV(TAG, " SPI0: %-3s| SPI1: %-3s| SPI2: %-3s| SAADC: %-3s", //
NRF_PERIPH_ENABLED(SPI, NRF_SPI0), NRF_PERIPH_ENABLED(SPI, NRF_SPI1), NRF_PERIPH_ENABLED(SPI, NRF_SPI2),
NRF_PERIPH_ENABLED(SAADC, NRF_SAADC));
ESP_LOGV(TAG, " QSPI: %-3s| QDEC: %-3s| LPCOMP: %-3s| I2S: %-3s", //
NRF_PERIPH_ENABLED(QSPI, NRF_QSPI), NRF_PERIPH_ENABLED(QDEC, NRF_QDEC),
NRF_PERIPH_ENABLED(LPCOMP, NRF_LPCOMP), NRF_PERIPH_ENABLED(I2S, NRF_I2S));
ESP_LOGV(TAG, " PWM0: %-3s| PWM1: %-3s| PWM2: %-3s| PWM3: %-3s", //
NRF_PERIPH_ENABLED(PWM, NRF_PWM0), NRF_PERIPH_ENABLED(PWM, NRF_PWM1), NRF_PERIPH_ENABLED(PWM, NRF_PWM2),
NRF_PERIPH_ENABLED(PWM, NRF_PWM3));
ESP_LOGV(TAG, " AAR: %-3s| QSPI deep power-down:%-3s| CRYPTOCELL: %-3s", NRF_PERIPH_ENABLED(AAR, NRF_AAR),
YESNO((NRF_QSPI->IFCONFIG0 & QSPI_IFCONFIG0_DPMENABLE_Msk) ==
(QSPI_IFCONFIG0_DPMENABLE_Enable << QSPI_IFCONFIG0_DPMENABLE_Pos)),
YESNO((NRF_CRYPTOCELL->ENABLE & CRYPTOCELL_ENABLE_ENABLE_Msk) ==
(CRYPTOCELL_ENABLE_ENABLE_Enabled << CRYPTOCELL_ENABLE_ENABLE_Pos)));
}
// NOLINTEND(clang-analyzer-core.FixedAddressDereference)
#undef NRF_PERIPH_ENABLED
#endif
static const char *regout0_to_str(uint32_t value) {
switch (value) {
case (UICR_REGOUT0_VOUT_DEFAULT):
return "1.8V (default)";
case (UICR_REGOUT0_VOUT_1V8):
return "1.8V";
case (UICR_REGOUT0_VOUT_2V1):
return "2.1V";
case (UICR_REGOUT0_VOUT_2V4):
return "2.4V";
case (UICR_REGOUT0_VOUT_2V7):
return "2.7V";
case (UICR_REGOUT0_VOUT_3V0):
return "3.0V";
case (UICR_REGOUT0_VOUT_3V3):
return "3.3V";
default:
break;
}
return "???V";
}
size_t DebugComponent::get_device_info_(std::span<char, DEVICE_INFO_BUFFER_SIZE> buffer, size_t pos) {
constexpr size_t size = DEVICE_INFO_BUFFER_SIZE;
char *buf = buffer.data();
// Main supply status
// NOLINTNEXTLINE(clang-analyzer-core.FixedAddressDereference) -- NRF_POWER is MMIO at a fixed address
auto regstatus = nrf_power_mainregstatus_get(NRF_POWER);
const char *supply_status = (regstatus == NRF_POWER_MAINREGSTATUS_NORMAL) ? "Normal voltage." : "High voltage.";
ESP_LOGD(TAG, "Main supply status: %s", supply_status);
pos = buf_append_str(buf, size, pos, "|Main supply status: ");
pos = buf_append_str(buf, size, pos, supply_status);
// Regulator stage 0
if (nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) {
const char *reg0_type = nrf_power_dcdcen_vddh_get(NRF_POWER) ? "DC/DC" : "LDO";
const char *reg0_voltage = regout0_to_str((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) >> UICR_REGOUT0_VOUT_Pos);
ESP_LOGD(TAG, "Regulator stage 0: %s, %s", reg0_type, reg0_voltage);
pos = buf_append_str(buf, size, pos, "|Regulator stage 0: ");
pos = buf_append_str(buf, size, pos, reg0_type);
pos = buf_append_str(buf, size, pos, ", ");
pos = buf_append_str(buf, size, pos, reg0_voltage);
#ifdef USE_NRF52_REG0_VOUT
if ((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) >> UICR_REGOUT0_VOUT_Pos != USE_NRF52_REG0_VOUT) {
ESP_LOGE(TAG, "Regulator stage 0: expected %s", regout0_to_str(USE_NRF52_REG0_VOUT));
}
#endif
} else {
ESP_LOGD(TAG, "Regulator stage 0: disabled");
pos = buf_append_str(buf, size, pos, "|Regulator stage 0: disabled");
}
// Regulator stage 1
const char *reg1_type = nrf_power_dcdcen_get(NRF_POWER) ? "DC/DC" : "LDO";
ESP_LOGD(TAG, "Regulator stage 1: %s", reg1_type);
pos = buf_append_str(buf, size, pos, "|Regulator stage 1: ");
pos = buf_append_str(buf, size, pos, reg1_type);
// USB power state
const char *usb_state;
if (nrf_power_usbregstatus_vbusdet_get(NRF_POWER)) {
usb_state = nrf_power_usbregstatus_outrdy_get(NRF_POWER) ? "ready" : "connected (regulator is not ready)";
} else {
usb_state = "disconnected";
}
ESP_LOGD(TAG, "USB power state: %s", usb_state);
pos = buf_append_str(buf, size, pos, "|USB power state: ");
pos = buf_append_str(buf, size, pos, usb_state);
// Power-fail comparator
bool enabled;
nrf_power_pof_thr_t pof_thr = nrf_power_pofcon_get(NRF_POWER, &enabled);
if (enabled) {
const char *pof_voltage;
switch (pof_thr) {
case POWER_POFCON_THRESHOLD_V17:
pof_voltage = "1.7V";
break;
case POWER_POFCON_THRESHOLD_V18:
pof_voltage = "1.8V";
break;
case POWER_POFCON_THRESHOLD_V19:
pof_voltage = "1.9V";
break;
case POWER_POFCON_THRESHOLD_V20:
pof_voltage = "2.0V";
break;
case POWER_POFCON_THRESHOLD_V21:
pof_voltage = "2.1V";
break;
case POWER_POFCON_THRESHOLD_V22:
pof_voltage = "2.2V";
break;
case POWER_POFCON_THRESHOLD_V23:
pof_voltage = "2.3V";
break;
case POWER_POFCON_THRESHOLD_V24:
pof_voltage = "2.4V";
break;
case POWER_POFCON_THRESHOLD_V25:
pof_voltage = "2.5V";
break;
case POWER_POFCON_THRESHOLD_V26:
pof_voltage = "2.6V";
break;
case POWER_POFCON_THRESHOLD_V27:
pof_voltage = "2.7V";
break;
case POWER_POFCON_THRESHOLD_V28:
pof_voltage = "2.8V";
break;
default:
pof_voltage = "???V";
break;
}
if (nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) {
const char *vddh_voltage;
switch (nrf_power_pofcon_vddh_get(NRF_POWER)) {
case NRF_POWER_POFTHRVDDH_V27:
vddh_voltage = "2.7V";
break;
case NRF_POWER_POFTHRVDDH_V28:
vddh_voltage = "2.8V";
break;
case NRF_POWER_POFTHRVDDH_V29:
vddh_voltage = "2.9V";
break;
case NRF_POWER_POFTHRVDDH_V30:
vddh_voltage = "3.0V";
break;
case NRF_POWER_POFTHRVDDH_V31:
vddh_voltage = "3.1V";
break;
case NRF_POWER_POFTHRVDDH_V32:
vddh_voltage = "3.2V";
break;
case NRF_POWER_POFTHRVDDH_V33:
vddh_voltage = "3.3V";
break;
case NRF_POWER_POFTHRVDDH_V34:
vddh_voltage = "3.4V";
break;
case NRF_POWER_POFTHRVDDH_V35:
vddh_voltage = "3.5V";
break;
case NRF_POWER_POFTHRVDDH_V36:
vddh_voltage = "3.6V";
break;
case NRF_POWER_POFTHRVDDH_V37:
vddh_voltage = "3.7V";
break;
case NRF_POWER_POFTHRVDDH_V38:
vddh_voltage = "3.8V";
break;
case NRF_POWER_POFTHRVDDH_V39:
vddh_voltage = "3.9V";
break;
case NRF_POWER_POFTHRVDDH_V40:
vddh_voltage = "4.0V";
break;
case NRF_POWER_POFTHRVDDH_V41:
vddh_voltage = "4.1V";
break;
case NRF_POWER_POFTHRVDDH_V42:
vddh_voltage = "4.2V";
break;
default:
vddh_voltage = "???V";
break;
}
ESP_LOGD(TAG, "Power-fail comparator: %s, VDDH: %s", pof_voltage, vddh_voltage);
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: ");
pos = buf_append_str(buf, size, pos, pof_voltage);
pos = buf_append_str(buf, size, pos, ", VDDH: ");
pos = buf_append_str(buf, size, pos, vddh_voltage);
} else {
ESP_LOGD(TAG, "Power-fail comparator: %s", pof_voltage);
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: ");
pos = buf_append_str(buf, size, pos, pof_voltage);
}
} else {
ESP_LOGD(TAG, "Power-fail comparator: disabled");
pos = buf_append_str(buf, size, pos, "|Power-fail comparator: disabled");
}
auto package = [](uint32_t value) {
switch (value) {
case 0x2004:
return "QIxx - 7x7 73-pin aQFN";
case 0x2000:
return "QFxx - 6x6 48-pin QFN";
case 0x2005:
return "CKxx - 3.544 x 3.607 WLCSP";
default:
break;
}
return "Unspecified";
};
char mac_pretty[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
get_mac_address_pretty_into_buffer(mac_pretty);
ESP_LOGD(TAG,
"nRF debug info:\n"
" Code page size: %u, code size: %u, device id: 0x%08x%08x\n"
" Encryption root: 0x%08x%08x%08x%08x, Identity Root: 0x%08x%08x%08x%08x\n"
" Device address type: %s, address: %s\n"
" Part code: nRF%x, version: %c%c%c%c, package: %s\n"
" RAM: %ukB, Flash: %ukB, production test: %sdone",
NRF_FICR->CODEPAGESIZE, NRF_FICR->CODESIZE, NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0], NRF_FICR->ER[0],
NRF_FICR->ER[1], NRF_FICR->ER[2], NRF_FICR->ER[3], NRF_FICR->IR[0], NRF_FICR->IR[1], NRF_FICR->IR[2],
NRF_FICR->IR[3], (NRF_FICR->DEVICEADDRTYPE & 0x1 ? "Random" : "Public"), mac_pretty, NRF_FICR->INFO.PART,
NRF_FICR->INFO.VARIANT >> 24 & 0xFF, NRF_FICR->INFO.VARIANT >> 16 & 0xFF, NRF_FICR->INFO.VARIANT >> 8 & 0xFF,
NRF_FICR->INFO.VARIANT & 0xFF, package(NRF_FICR->INFO.PACKAGE), NRF_FICR->INFO.RAM, NRF_FICR->INFO.FLASH,
(NRF_FICR->PRODTEST[0] == 0xBB42319F ? "" : "not "));
bool n_reset_enabled = NRF_UICR->PSELRESET[0] == NRF_UICR->PSELRESET[1] &&
(NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) == UICR_PSELRESET_CONNECT_Connected
<< UICR_PSELRESET_CONNECT_Pos;
ESP_LOGD(
TAG, " GPIO as NFC pins: %s, GPIO as nRESET pin: %s",
YESNO((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)),
YESNO(n_reset_enabled));
if (n_reset_enabled) {
uint8_t port = (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_PORT_Msk) >> UICR_PSELRESET_PORT_Pos;
uint8_t pin = (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_PIN_Msk) >> UICR_PSELRESET_PIN_Pos;
ESP_LOGD(TAG, " nRESET port P%u.%02u", port, pin);
}
#ifdef USE_BOOTLOADER_MCUBOOT
ESP_LOGD(TAG, " Bootloader: mcuboot");
#else
ESP_LOGD(TAG, " Bootloader: Adafruit, version %u.%u.%u", (BOOTLOADER_VERSION_REGISTER >> 16) & 0xFF,
(BOOTLOADER_VERSION_REGISTER >> 8) & 0xFF, BOOTLOADER_VERSION_REGISTER & 0xFF);
ESP_LOGD(TAG, " MBR bootloader addr 0x%08x, UICR bootloader addr 0x%08x", read_mem_u32(MBR_BOOTLOADER_ADDR),
NRF_UICR->NRFFW[0]);
ESP_LOGD(TAG, " MBR param page addr 0x%08x, UICR param page addr 0x%08x", read_mem_u32(MBR_PARAM_PAGE_ADDR),
NRF_UICR->NRFFW[1]);
if (is_sd_present()) {
uint32_t const sd_id = sd_id_get();
uint32_t const sd_version = sd_version_get();
uint32_t ver[3];
ver[0] = sd_version / 1000000;
ver[1] = (sd_version - ver[0] * 1000000) / 1000;
ver[2] = (sd_version - ver[0] * 1000000 - ver[1] * 1000);
ESP_LOGD(TAG, " SoftDevice: S%u %u.%u.%u", sd_id, ver[0], ver[1], ver[2]);
#ifdef USE_SOFTDEVICE_ID
#ifdef USE_SOFTDEVICE_VERSION
if (USE_SOFTDEVICE_ID != sd_id || USE_SOFTDEVICE_VERSION != ver[0]) {
ESP_LOGE(TAG, "Built for SoftDevice S%u %u.x.y. It may crash due to mismatch of bootloader version.",
USE_SOFTDEVICE_ID, USE_SOFTDEVICE_VERSION);
}
#else
if (USE_SOFTDEVICE_ID != sd_id) {
ESP_LOGE(TAG, "Built for SoftDevice S%u. It may crash due to mismatch of bootloader version.", USE_SOFTDEVICE_ID);
}
#endif
#endif
}
#endif
// 13 words of 8 hex digits, separated by spaces
char uicr_buf[13 * 9];
auto uicr = [&uicr_buf](volatile uint32_t *data, uint8_t size) {
size_t len = 0;
for (size_t i = 0; i < size; i++) {
len = buf_append_printf(uicr_buf, sizeof(uicr_buf), len, i > 0 ? " %08" PRIX32 : "%08" PRIX32, data[i]);
}
return uicr_buf;
};
ESP_LOGD(TAG, " NRFFW %s", uicr(NRF_UICR->NRFFW, 13));
ESP_LOGD(TAG, " NRFHW %s", uicr(NRF_UICR->NRFHW, 12));
#ifdef ESPHOME_LOG_HAS_VERBOSE
log_peripherals_info();
#endif
return pos;
}
// The Python side enables the statistics only for a verbose logger
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
static void log_thread_usage(const struct k_thread *thread, void *user_data) {
+1 -3
View File
@@ -605,9 +605,7 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_type(conf[CONF_TYPE]))
for conf in config[CONF_SELECTS]:
var = await select.new_select(
conf, options=conf[CONF_OPTIONS], fixed_options=True
)
var = await select.new_select(conf, options=conf[CONF_OPTIONS])
await cg.register_component(var, conf)
for conf in config[CONF_SENSORS]:
+1 -1
View File
@@ -41,5 +41,5 @@ CONFIG_SCHEMA = (
async def to_code(config: ConfigType) -> None:
var = await select.new_select(config, options=DUCO_MODE_OPTIONS, fixed_options=True)
var = await select.new_select(config, options=DUCO_MODE_OPTIONS)
await register_duco_device(var, config, config[CONF_DUCO_ID])
+1 -22
View File
@@ -114,26 +114,6 @@ struct LogBuffer {
this->finalize_();
}
#endif
// Writes a message that needs no formatting, truncated and stripped of trailing newlines as format_body() does
void HOT write_unformatted_body(const char *text, size_t len) {
if (!this->full_()) {
const uint16_t limit = this->remaining_() - 1; // vsnprintf() keeps the last byte for the null terminator
const uint16_t copy_len = (len < limit) ? static_cast<uint16_t>(len) : limit;
memcpy(this->current_(), text, copy_len);
this->pos += copy_len;
this->strip_trailing_newlines_();
}
this->finalize_();
}
// write_unformatted_body() for a message in flash
inline void HOT ESPHOME_ALWAYS_INLINE write_unformatted_body_p(ESPHOME_PGM_P text) {
if (!this->full_()) {
char *const start = this->current_();
this->pos = static_cast<uint16_t>(progmem_copy_str(start, start + this->remaining_() - 1, text) - this->data);
this->strip_trailing_newlines_();
}
this->finalize_();
}
static constexpr uint16_t ANSI_COLOR_LEN = 7; // "\033[{bold};3{color}m" written by write_header for level != 0
static constexpr uint16_t ANSI_RESET_LEN = 4; // "\033[0m"
// Largest replay header: color plus "@4294967295ms "
@@ -181,8 +161,7 @@ struct LogBuffer {
this->pos += ANSI_RESET_LEN;
}
}
// Forced inline: an outlined copy slows every formatted line
inline void ESPHOME_ALWAYS_INLINE strip_trailing_newlines_() {
void strip_trailing_newlines_() {
while (this->pos > 0 && this->data[this->pos - 1] == '\n')
this->pos--;
}
+5 -53
View File
@@ -19,15 +19,6 @@ namespace esphome::logger {
ESPHOME_LOG_TAG(TAG, "logger");
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes to a socket would
// take that lock again and deadlock, so such a line is queued for loop() instead. It can print after later inline
// lines, and is lost if loop() never runs again (safe_reboot()).
static inline bool HOT ESPHOME_ALWAYS_INLINE under_lwip_core_lock() {
return sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED);
}
#endif
#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
// Implementation for multi-threaded platforms (ESP32 with FreeRTOS, Host with pthreads, LibreTiny with FreeRTOS,
// Zephyr) Main thread/task always uses direct buffer access for console output and callbacks
@@ -60,7 +51,11 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
// Pass nullptr for thread_name since we already know this is the main task
if (is_main_task && !this->main_task_recursion_guard_) [[likely]] {
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
if (under_lwip_core_lock()) [[unlikely]] {
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes
// to a socket would take that lock again and deadlock, so queue the message for loop() instead. Such lines
// can print after later inline ones, and are lost if loop() never runs again (safe_reboot()).
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED))
[[unlikely]] {
this->log_vprintf_non_main_thread_(level, tag, line, format, args, nullptr);
return;
}
@@ -164,49 +159,6 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
}
#endif // USE_ESP8266
// Not shared with log_message_to_buffer_and_send_(): sharing it slows formatted lines
inline void HOT ESPHOME_ALWAYS_INLINE Logger::send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf) {
#ifdef USE_LOGGER_BOOT_LOG_BUFFER
if (!this->boot_log_done_)
this->boot_log_capture_(level, tag, buf);
#endif
this->notify_listeners_(level, tag, buf);
this->write_log_buffer_to_console_(buf);
}
bool HOT Logger::log_str_(uint8_t level, const char *tag, int line, const char *msg) {
if (level > this->level_for(tag))
return true;
// Check the task first: the guard belongs to the main task and must not be read from other threads
if (!this->is_main_task_())
return false;
if (this->main_task_recursion_guard_)
return true;
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// The formatting path queues it
if (under_lwip_core_lock()) [[unlikely]]
return false;
#endif
RecursionGuard guard(this->main_task_recursion_guard_);
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
buf.write_header(level, tag, line, nullptr);
buf.write_unformatted_body(msg, strlen(msg));
this->send_unformatted_line_(level, tag, buf);
return true;
}
#ifdef USE_ESP8266
void Logger::log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg) {
if (level > this->level_for(tag) || this->main_task_recursion_guard_)
return;
RecursionGuard guard(this->main_task_recursion_guard_);
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
buf.write_header(level, tag, line, nullptr);
buf.write_unformatted_body_p(reinterpret_cast<PGM_P>(msg));
this->send_unformatted_line_(level, tag, buf);
}
#endif // USE_ESP8266
#if defined(USE_LOGGER_RUNTIME_TAG_LEVELS) || defined(USE_LOGGER_BOOT_LOG_PER_TAG_LEVELS)
// Level for tag from a per-tag map, or fallback when the tag has none; tags may live in flash
static inline uint8_t tag_level(const std::map<const char *, uint8_t, CStrCompare> &levels, const char *tag,
+1 -22
View File
@@ -223,12 +223,6 @@ class Logger final : public Component {
// NOLINTNEXTLINE(readability-identifier-naming)
void log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args);
#endif
// Copies a line with no '%' without formatting; false means the caller must format it
bool log_str_(uint8_t level, const char *tag, int line, const char *msg); // NOLINT
#ifdef USE_ESP8266
// NOLINTNEXTLINE(readability-identifier-naming)
void log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg);
#endif
protected:
// RAII guard for recursion flags - sets flag on construction, clears on destruction
@@ -298,7 +292,7 @@ class Logger final : public Component {
}
// Helper to write log buffer to console if logging is enabled
inline void HOT ESPHOME_ALWAYS_INLINE write_log_buffer_to_console_(LogBuffer &buf) {
inline void HOT write_log_buffer_to_console_(LogBuffer &buf) {
if (this->baud_rate_ > 0)
this->write_to_console_(buf);
}
@@ -327,21 +321,6 @@ class Logger final : public Component {
this->write_log_buffer_to_console_(buf);
}
inline void send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf);
// True on the task that runs the main loop, and always on single-task platforms
inline bool HOT is_main_task_() const {
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
return xTaskGetCurrentTaskHandle() == this->main_task_;
#elif defined(USE_ZEPHYR)
return k_current_get() == this->main_task_;
#elif defined(USE_HOST)
return pthread_equal(pthread_self(), this->main_thread_);
#else
return true;
#endif
}
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
// Helper to format a pre-formatted message from the task log buffer and notify listeners
inline void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line,
+1 -1
View File
@@ -9,7 +9,7 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
char buffer[TIMESTAMP_LEN + ESPHOME_LOGGER_TX_BUFFER_SIZE + HEADROOM];
time_t rawtime;
::time(&rawtime);
time(&rawtime);
struct tm timeinfo;
localtime_r(&rawtime, &timeinfo); // Thread-safe version
size_t pos = strftime(buffer, TIMESTAMP_LEN + 1, "[%H:%M:%S]", &timeinfo);
+1 -1
View File
@@ -34,6 +34,6 @@ async def to_code(config: ConfigType) -> None:
levels = list(LOG_LEVELS)
index = levels.index(CORE.data[DOMAIN][CONF_LEVEL])
levels = levels[: index + 1]
var = await select.new_select(config, options=levels, fixed_options=True)
var = await select.new_select(config, options=levels)
await register_parented(var, parent)
await register_component(var, config)
@@ -103,9 +103,7 @@ async def to_code(config: ConfigType) -> None:
)
await cg.register_component(var, config)
await select.register_select(
var, config, options=list(options_map.keys()), fixed_options=True
)
await select.register_select(var, config, options=list(options_map.keys()))
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
cg.add(parent.add_sensor_item(var))
+23 -32
View File
@@ -56,8 +56,7 @@ static constexpr uint8_t PIN_MAX_UNCONFIRMED_SENDS = 2;
// The attachment is polled every 4 s; the signal and operator queries only every 4th poll
static constexpr uint32_t ATTACH_POLL_MS = 4000;
static constexpr uint8_t ATTACH_DIAG_EVERY = 4;
static constexpr int RADIO_STATE_MINIMUM = 0; // CFUN=0: radio off and the SIM powered down
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr uint8_t UART_EVENT_QUEUE_SIZE = 10;
static constexpr uint32_t BAUD_PREF_HASH = 76007670UL;
static constexpr uint32_t PIN_PREF_HASH = 76007671UL;
@@ -671,28 +670,22 @@ ModemState ModemComponent::handle_state_enabling_() {
this->set_fault_(ModemFault::MODEM_FAULT_TEARDOWN);
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
// esp_modem keeps the last line before OK as the reply, which can be the echoed command
if (this->dce_->set_echo(false) != command_result::OK) {
ESP_LOGW(TAG, "Failed to disable command echo");
}
// The SIM7670G only uses an APN written at CFUN=0 (else the attach fails with an ESM failure); CFUN=0 also drops a
// session left by a warm reboot. A modem that refuses gets the APN after the SIM check instead, as before.
esp_modem::PdpContext pdp(this->apn_);
this->apn_pending_ = this->dce_->set_radio_state(RADIO_STATE_MINIMUM) != command_result::OK ||
this->dce_->set_pdp_context(pdp) != command_result::OK;
if (this->apn_pending_) {
ESP_LOGW(TAG, "Failed to set the APN with the SIM powered down; setting it after the SIM check");
int radio_state = 0;
if (this->dce_->get_radio_state(radio_state) != command_result::OK) {
ESP_LOGW(TAG, "Failed to read the radio state; skipping the radio off step");
} else if (radio_state != RADIO_STATE_RF_OFF) {
// Drops a session left by a warm reboot; unlike CFUN=0, which powers the SIM down on the SIM7600, CFUN=4
// keeps the SIM readable for the PIN and the APN. A modem that refuses is used as it is.
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio off");
}
}
// CFUN=4 powers the SIM up with the radio still off; a modem without it needs CFUN=1 to bring the SIM back
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK &&
this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to power the SIM up");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
this->modem_synced_ = true; // it answers, so a disable() still tears it down
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
if (this->baud_rate_ != 0 && this->baud_rate_ != this->current_baud_rate_) {
ESP_LOGD(TAG, "Setting baud rate: %d -> %d", this->current_baud_rate_, this->baud_rate_);
@@ -759,7 +752,7 @@ ModemState ModemComponent::handle_state_connecting_() {
return ModemState::MODEM_STATE_ENABLING;
}
}
// ENABLING powered the SIM up (CFUN=4, or CFUN=1 on a modem without it), so the SIM answers
// ENABLING left the radio at CFUN=4, where the SIM answers but the modem cannot attach yet
switch (this->prepare_sim_()) {
case SimResult::SIM_RESULT_READY:
break;
@@ -776,14 +769,13 @@ ModemState ModemComponent::handle_state_connecting_() {
this->target_state_.store(ModemState::MODEM_STATE_DISABLED);
return ModemState::MODEM_STATE_DISCONNECTING;
}
if (this->apn_pending_) {
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
}
this->apn_pending_ = false;
// The APN has to be in the PDP context before the radio comes on and the modem attaches; esp_modem only
// sends it when PPP starts
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
}
if (this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio on");
@@ -870,12 +862,11 @@ ModemState ModemComponent::handle_state_disconnecting_() {
ESP_LOGW(TAG, "Failed to leave data mode");
this->set_teardown_fault_();
}
// Turn the radio off; waiting for the lost IP event could take up to 2 min. The SIM is powered down as well, as
// ENABLING does anyway on a reconnect.
if (this->dce_->set_radio_state(RADIO_STATE_MINIMUM) == command_result::OK) {
// Turn the radio off; waiting for the lost IP event could take up to 2 min. A reconnect keeps the SIM powered
// so it is not unlocked again; only disable() powers it down.
const bool reconnect = this->target_state_.load() == ModemState::MODEM_STATE_CONNECTED;
if (this->dce_->set_radio_state(reconnect ? RADIO_STATE_RF_OFF : 0) == command_result::OK) {
ESP_LOGI(TAG, "Radio off");
} else if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) == command_result::OK) {
ESP_LOGW(TAG, "Radio off, but the SIM could not be powered down");
} else {
ESP_LOGW(TAG, "Radio off failed");
this->set_teardown_fault_();
@@ -299,7 +299,6 @@ class ModemComponent : public Component {
bool connect_timeout_armed_{false};
bool modem_synced_{false}; // a modem that never answered has nothing to tear down
bool resync_{false}; // the attachment read failed, so CONNECTING syncs again first
bool apn_pending_{false}; // ENABLING could not set the APN at CFUN=0, so CONNECTING sets it after the SIM check
#ifdef USE_MODEM_SEND_AT
char at_command_[AT_COMMAND_SIZE];
#endif
+3 -12
View File
@@ -323,17 +323,12 @@ bool MQTTClientComponent::can_proceed() {
}
void MQTTClientComponent::start_dnslookup_() {
this->status_set_warning();
// No lookup can start before the network is up: there is no DNS server to ask yet.
// Stay disconnected, and loop() calls this again.
if (!network::is_connected())
return;
for (auto &subscription : this->subscriptions_) {
subscription.subscribed = false;
subscription.resubscribe_timeout = 0;
}
this->status_set_warning();
this->dns_resolve_error_ = false;
this->dns_resolved_ = false;
ip_addr_t addr;
@@ -363,13 +358,9 @@ void MQTTClientComponent::start_dnslookup_() {
}
default:
case ERR_ARG: {
// The lookup never started, so there is no answer to wait for; loop() tries again in 5 seconds
// error
ESP_LOGW(TAG, "Error resolving broker IP address: %d", err);
this->state_ = MQTT_CLIENT_DISCONNECTED;
this->disconnect_reason_ = MQTTClientDisconnectReason::DNS_RESOLVE_ERROR;
this->on_disconnect_.call(MQTTClientDisconnectReason::DNS_RESOLVE_ERROR);
this->connect_begin_ = millis();
return;
break;
}
}
+39 -117
View File
@@ -1,5 +1,4 @@
from collections.abc import Callable
import logging
from esphome import automation
import esphome.codegen as cg
@@ -9,8 +8,6 @@ import esphome.config_validation as cv
from esphome.const import CONF_DURATION, CONF_ID, CONF_MODE, CONF_VOLTAGE
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@tomaszduda23"]
DEPENDENCIES = ["i2c"]
DOMAIN = "npm1300"
@@ -45,18 +42,20 @@ NPM1300_CHILD_SCHEMA = cv.Schema(
{cv.GenerateID(CONF_NPM1300_ID): cv.use_id(NPM1300Component)}
)
NPM1300Status = npm1300_ns.struct("NPM1300Status")
# The status registers read on each update
# Status registers read on each update: VBUSINSTATUS, BCHGCHARGESTATUS and BCHGERRREASON
STATUS_PUBLISHER = cg.HubFunction(
"status",
cg.void,
[(NPM1300Status.operator("const").operator("ref"), "status")],
[
(cg.uint8, "vbus_status"),
(cg.uint8, "charge_status"),
(cg.uint8, "error_reason"),
],
define="USE_NPM1300_STATUS_PUBLISHER",
)
STATUS = STATUS_PUBLISHER.param("status")
VBUS_STATUS = STATUS.vbus
CHARGE_STATUS = STATUS.charge
ERROR_REASON = STATUS.error_reason
VBUS_STATUS = STATUS_PUBLISHER.param("vbus_status")
CHARGE_STATUS = STATUS_PUBLISHER.param("charge_status")
ERROR_REASON = STATUS_PUBLISHER.param("error_reason")
# The ADC results and the amperes per code of the battery current
DATA_PUBLISHER = cg.HubFunction(
"data",
@@ -78,10 +77,7 @@ OUTPUTS = NPM1300Output.options(
# Written for a field the YAML leaves out
NPM1300_UNSET = 0xFF
# ADCNTCRSEL codes, by kiloohms
THERMISTOR_NONE = "none"
THERMISTOR_RESISTANCES = {10: 1, 47: 2, 100: 3}
# B25/50 of the thermistors the product specification lists for each resistance, by kiloohms
THERMISTOR_BETAS = {10: 3380, 47: 4050, 100: 4250}
LDO_MODES = {"load_switch": 0, "ldo": 1}
TERMINATION_CURRENTS = {"10%": 0, "20%": 1}
# BCHGVTRICKLESEL, by millivolts
@@ -105,13 +101,14 @@ def _one_of_units(
validator: Callable[[object], float],
allowed: list[int],
unit: str,
scale: float = 1000,
) -> Callable[[object], float]:
"""Validate a quantity with `validator` and accept only the listed values, given in thousandths of `unit`."""
"""Validate a quantity with `validator` and accept only the listed values, given in units * `scale`."""
def validate(value: object) -> float:
value = validator(value)
if _millis(value) not in allowed:
options = ", ".join(f"{v / 1000:g}{unit}" for v in allowed)
if round(value * scale) not in allowed:
options = ", ".join(f"{v / scale:g}{unit}" for v in allowed)
raise cv.Invalid(f"Must be one of {options}")
return value
@@ -138,23 +135,6 @@ def _stepped(
return validate
def _thermistor_resistance(value: object) -> float | str:
if isinstance(value, str) and value.lower() == THERMISTOR_NONE:
return THERMISTOR_NONE
value = cv.resistance(value)
if round(value) not in (kilohms * 1000 for kilohms in THERMISTOR_RESISTANCES):
options = ", ".join(f"{kilohms}kOhm" for kilohms in THERMISTOR_RESISTANCES)
raise cv.Invalid(f"Must be one of {options} or {THERMISTOR_NONE}")
return value
def thermistor_beta(config: ConfigType) -> int:
"""The B constant of the hub's thermistor: the configured one, or the one listed for its resistance."""
if (beta := config.get(CONF_THERMISTOR_BETA)) is not None:
return beta
return THERMISTOR_BETAS[round(config[CONF_THERMISTOR_RESISTANCE] / 1000)]
OUTPUT_VOLTAGE = _stepped(cv.voltage, 1000, 3300, 100, "V")
VBUS_CURRENT_LIMIT = _stepped(cv.current, 100, 1500, 100, "A")
@@ -170,58 +150,15 @@ LDO_SCHEMA = BUCK_SCHEMA.extend(
}
)
WARM_TERMINATION_VOLTAGE_DEFAULT = 3.6
def _warm_termination_default(config: ConfigType) -> ConfigType:
"""Keep the default warm termination voltage at or below the termination voltage.
Warn when the default stays below a higher termination voltage.
"""
if (
not isinstance(config, dict)
or CONF_WARM_TERMINATION_VOLTAGE in config
or CONF_TERMINATION_VOLTAGE not in config
):
return config
try:
termination = cv.voltage(config[CONF_TERMINATION_VOLTAGE])
except cv.Invalid:
# The schema reports it
return config
if termination < WARM_TERMINATION_VOLTAGE_DEFAULT - 0.001:
# A warm battery is never charged higher than a cool one
return {
**config,
CONF_WARM_TERMINATION_VOLTAGE: config[CONF_TERMINATION_VOLTAGE],
}
if termination > WARM_TERMINATION_VOLTAGE_DEFAULT + 0.001:
_LOGGER.warning(
"npm1300 '%s' is not set, so a warm battery (45 °C to 60 °C by default) stops "
"charging at %.2fV instead of the %.2fV of '%s'. Set '%s' to the value your "
"battery allows when warm",
CONF_WARM_TERMINATION_VOLTAGE,
WARM_TERMINATION_VOLTAGE_DEFAULT,
termination,
CONF_TERMINATION_VOLTAGE,
CONF_WARM_TERMINATION_VOLTAGE,
)
return config
_CHARGER_SETTINGS_SCHEMA = cv.Schema(
CHARGER_SCHEMA = cv.Schema(
{
# Defaults are the reset values of the product specification
cv.Optional(CONF_CHARGE_CURRENT, default="32mA"): _stepped(
cv.current, 32, 800, 2, "A"
cv.Required(CONF_CHARGE_CURRENT): _stepped(cv.current, 32, 800, 2, "A"),
cv.Optional(CONF_TERMINATION_VOLTAGE, default="4.2V"): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(CONF_TERMINATION_VOLTAGE, default="3.6V"): _one_of_units(
cv.Optional(CONF_WARM_TERMINATION_VOLTAGE): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(
CONF_WARM_TERMINATION_VOLTAGE,
default=f"{WARM_TERMINATION_VOLTAGE_DEFAULT}V",
): _one_of_units(cv.voltage, TERMINATION_VOLTAGES, "V"),
cv.Optional(CONF_TERMINATION_CURRENT, default="10%"): cv.enum(
TERMINATION_CURRENTS
),
@@ -232,37 +169,22 @@ _CHARGER_SETTINGS_SCHEMA = cv.Schema(
cv.current, list(DISCHARGE_CURRENT_LIMITS), "A"
),
cv.Optional(CONF_RECHARGE, default=True): cv.boolean,
cv.Optional(CONF_ENABLED, default=False): cv.boolean,
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
}
)
CHARGER_SCHEMA = cv.All(_warm_termination_default, _CHARGER_SETTINGS_SCHEMA)
def _check_thermistor(config: ConfigType) -> ConfigType:
"""A wrong thermistor shifts the battery temperature the charger sees, so charging needs one chosen."""
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
if CONF_CHARGER in config and resistance is None:
raise cv.Invalid(
f"'{CONF_THERMISTOR_RESISTANCE}' is required with '{CONF_CHARGER}': set the nominal "
f"resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
path=[CONF_THERMISTOR_RESISTANCE],
)
if CONF_THERMISTOR_BETA in config and resistance in (None, THERMISTOR_NONE):
raise cv.Invalid(
f"'{CONF_THERMISTOR_BETA}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
"resistance of the thermistor",
path=[CONF_THERMISTOR_BETA],
)
return config
CONFIG_SCHEMA = cv.All(
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(NPM1300Component),
cv.Optional(CONF_VBUS_CURRENT_LIMIT): VBUS_CURRENT_LIMIT,
cv.Optional(CONF_THERMISTOR_RESISTANCE): _thermistor_resistance,
cv.Optional(CONF_THERMISTOR_BETA): cv.int_range(min=1, max=65535),
cv.Optional(CONF_THERMISTOR_RESISTANCE): _one_of_units(
cv.resistance, list(THERMISTOR_RESISTANCES), "kOhm", scale=0.001
),
cv.Optional(CONF_THERMISTOR_BETA, default=3380): cv.int_range(
min=1, max=65535
),
cv.Optional(CONF_CHARGER): CHARGER_SCHEMA,
cv.Optional(CONF_BUCK1): BUCK_SCHEMA,
cv.Optional(CONF_BUCK2): BUCK_SCHEMA,
@@ -271,8 +193,7 @@ CONFIG_SCHEMA = cv.All(
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x6B)),
_check_thermistor,
.extend(i2c.i2c_device_schema(0x6B))
)
@@ -287,7 +208,7 @@ def _vbus_current_limit_code(amps: float) -> int:
def _charger_config(config: ConfigType) -> cg.StructInitializer:
termination = _termination_voltage_code(config[CONF_TERMINATION_VOLTAGE])
warm = _termination_voltage_code(config[CONF_WARM_TERMINATION_VOLTAGE])
warm = config.get(CONF_WARM_TERMINATION_VOLTAGE)
return cg.StructInitializer(
NPM1300ChargerConfig,
("current", _millis(config[CONF_CHARGE_CURRENT]) // 2),
@@ -296,7 +217,10 @@ def _charger_config(config: ConfigType) -> cg.StructInitializer:
DISCHARGE_CURRENT_LIMITS[_millis(config[CONF_DISCHARGE_CURRENT_LIMIT])],
),
("termination_voltage", termination),
("warm_termination_voltage", warm),
(
"warm_termination_voltage",
termination if warm is None else _termination_voltage_code(warm),
),
("trickle_voltage", TRICKLE_VOLTAGES[_millis(config[CONF_TRICKLE_VOLTAGE])]),
("termination_current", TERMINATION_CURRENTS[config[CONF_TERMINATION_CURRENT]]),
("recharge", config[CONF_RECHARGE]),
@@ -323,13 +247,6 @@ def _output_config(config: ConfigType | None) -> cg.StructInitializer:
def _config(config: ConfigType) -> cg.StructInitializer:
charger = config.get(CONF_CHARGER)
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
if resistance is None:
thermistor, beta = NPM1300_UNSET, 0
elif resistance == THERMISTOR_NONE:
thermistor, beta = 0, 0
else:
thermistor = THERMISTOR_RESISTANCES[round(resistance / 1000)]
beta = thermistor_beta(config)
return cg.StructInitializer(
NPM1300Config,
(
@@ -337,8 +254,13 @@ def _config(config: ConfigType) -> cg.StructInitializer:
cg.ArrayInitializer(*(_output_config(config.get(key)) for key in OUTPUTS)),
),
("charger", None if charger is None else _charger_config(charger)),
("thermistor_beta", beta),
("thermistor", thermistor),
("thermistor_beta", config[CONF_THERMISTOR_BETA]),
(
"thermistor",
NPM1300_UNSET
if resistance is None
else THERMISTOR_RESISTANCES[round(resistance / 1000)],
),
("charger_configured", charger is not None),
)
+34 -51
View File
@@ -29,7 +29,6 @@ static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
static constexpr uint16_t REG_BCHG_NTC_STATUS = 0x0332;
static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
@@ -54,9 +53,6 @@ static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
// VBUSINSTATUS
static constexpr uint8_t VBUS_PRESENT = 0x01;
// NTCSTATUS
static constexpr uint8_t NTC_STATUS_COLD = 0x01;
static constexpr uint8_t NTC_STATUS_HOT = 0x08;
// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
@@ -111,36 +107,26 @@ float ntc_temperature(uint16_t code, float inv_beta) {
#endif
#ifdef USE_TEXT_SENSOR
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Discharging", "Error", "Disabled", "Paused, PMIC Too Hot",
"Charge Complete", "Trickle Charge", "Constant Current", "Constant Voltage",
"Paused, Battery Too Cold", "Paused, Battery Too Hot", "Not Charging");
static constexpr uint8_t CHARGER_STATUS_FIRST_BIT = 3;
// BCHGCHARGESTATUS bits in order of priority, reported from string CHARGER_STATUS_FIRST_BIT on
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Error", "Paused, Die Too Hot", "Charge Complete", "Trickle Charge",
"Constant Current", "Constant Voltage", "Discharging", "Not Charging");
// BCHGCHARGESTATUS bits in order of priority; bit i is reported as string i + 1
static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
CHARGE_STATUS_CONSTANT_VOLTAGE};
static uint8_t charger_status_index(const NPM1300Status &status) {
if ((status.vbus & VBUS_PRESENT) == 0)
static uint8_t charger_status_index(uint8_t vbus_status, uint8_t charge_status, uint8_t error_reason) {
if (error_reason != 0)
return 0;
if (status.error_reason != 0)
return 1;
if ((status.charger_enable & 1) == 0)
return 2;
for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
if ((status.charge & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
return CHARGER_STATUS_FIRST_BIT + i;
if ((charge_status & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
return i + 1;
}
// The charger stops without an error outside the cold and hot limits of the battery temperature
if ((status.ntc & NTC_STATUS_COLD) != 0)
return 8;
if ((status.ntc & NTC_STATUS_HOT) != 0)
return 9;
return 10;
return (vbus_status & VBUS_PRESENT) == 0 ? 6 : 7;
}
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status) {
const uint8_t index = charger_status_index(status);
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason) {
const uint8_t index = charger_status_index(vbus_status, charge_status, error_reason);
if (index == last)
return;
last = index;
@@ -212,7 +198,7 @@ bool NPM1300Component::configure_charger_(const NPM1300Config &config) {
split_9bit(charger.discharge_limit, discharge);
uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
// Without a thermistor the charger reports an NTC error and does not charge
if (config.thermistor == 0 || config.thermistor == NPM1300_UNSET)
if (config.thermistor == NPM1300_UNSET)
disable |= CHARGER_DISABLE_NTC;
const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
@@ -306,40 +292,33 @@ void NPM1300Component::hibernate(uint32_t duration_ms) {
}
void NPM1300Component::update() {
NPM1300Status status;
// NTCSTATUS, DIETEMPSTATUS and BCHGCHARGESTATUS
uint8_t charger_status[3];
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &status.vbus) &&
this->read_byte_reg16(REG_BCHG_ENABLE_SET, &status.charger_enable) &&
this->read_register16(REG_BCHG_NTC_STATUS, charger_status, sizeof(charger_status)) ==
i2c::ERROR_OK &&
this->read_byte_reg16(REG_BCHG_ERR_REASON, &status.error_reason)))
uint8_t vbus_status;
uint8_t charge_status;
uint8_t error_reason;
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &vbus_status) &&
this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &charge_status) &&
this->read_byte_reg16(REG_BCHG_ERR_REASON, &error_reason)))
return;
status.ntc = charger_status[0];
status.charge = charger_status[2];
// Without a thermistor the NTC pin is grounded and can read as a hot battery
if (progmem_read_byte(&this->config_->thermistor) == 0)
status.ntc = 0;
// The chip goes back to its default VBUS current limit whenever VBUS is removed
const bool vbus_present = (status.vbus & VBUS_PRESENT) != 0;
const bool vbus_present = (vbus_status & VBUS_PRESENT) != 0;
if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
!this->status_comm_check(this->apply_vbus_current_limit_()))
return;
this->vbus_present_ = vbus_present;
if (status.error_reason != this->last_error_reason_) {
this->last_error_reason_ = status.error_reason;
if (status.error_reason != 0) {
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", status.error_reason);
if (error_reason != this->last_error_reason_) {
this->last_error_reason_ = error_reason;
if (error_reason != 0) {
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", error_reason);
}
}
#ifdef USE_NPM1300_STATUS_PUBLISHER
if (this->status_publisher_ != nullptr)
this->status_publisher_(status);
this->status_publisher_(vbus_status, charge_status, error_reason);
#endif
#ifdef USE_SWITCH
if (!this->status_comm_check(this->publish_switches_(status)))
if (!this->status_comm_check(this->publish_switches_()))
return;
#endif
@@ -369,10 +348,16 @@ void NPM1300Component::update() {
}
#ifdef USE_SWITCH
bool NPM1300Component::publish_switches_(const NPM1300Status &status) {
PUBLISH_STATE(this->charger_switch_, (status.charger_enable & 1) != 0);
bool NPM1300Component::publish_switches_() {
uint8_t value;
bool ok = true;
if (this->charger_switch_ != nullptr) {
if (this->read_byte_reg16(REG_BCHG_ENABLE_SET, &value)) {
this->charger_switch_->publish_state((value & 1) != 0);
} else {
ok = false;
}
}
switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
if (bucks[0] != nullptr || bucks[1] != nullptr) {
if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
@@ -476,9 +461,7 @@ void NPM1300Component::dump_config() {
ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
}
}
if (config.thermistor == 0) {
ESP_LOGCONFIG(TAG, " Thermistor: none");
} else if (config.thermistor != NPM1300_UNSET) {
if (config.thermistor != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
}
#ifdef USE_SENSOR
+5 -13
View File
@@ -54,19 +54,10 @@ struct NPM1300Config {
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
NPM1300ChargerConfig charger; // only applied when charger_configured
uint16_t thermistor_beta;
uint8_t thermistor; // ADCNTCRSEL code: 0 for none, 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
uint8_t thermistor; // ADCNTCRSEL code: 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
bool charger_configured;
};
/// The status registers read on every update
struct NPM1300Status {
uint8_t vbus; // VBUSINSTATUS
uint8_t charger_enable; // BCHGENABLESET
uint8_t ntc; // NTCSTATUS
uint8_t charge; // BCHGCHARGESTATUS
uint8_t error_reason; // BCHGERRREASON
};
#ifdef USE_NPM1300_DATA_PUBLISHER
/// The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at `shift` in adc[lsbs]
inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
@@ -77,7 +68,8 @@ float ntc_temperature(uint16_t code, float inv_beta);
#endif
#ifdef USE_TEXT_SENSOR
/// Publish the charger status text, only when it differs from `last`
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status);
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason);
#endif
class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
@@ -103,7 +95,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
#ifdef USE_NPM1300_STATUS_PUBLISHER
HUB_PUBLISHER(status, const NPM1300Status &)
HUB_PUBLISHER(status, uint8_t, uint8_t, uint8_t)
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
HUB_PUBLISHER(data, const uint8_t *, float)
@@ -136,7 +128,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
#endif
#ifdef USE_SWITCH
/// Returns false when a status read failed
bool publish_switches_(const NPM1300Status &status);
bool publish_switches_();
#endif
const NPM1300Config *config_;
+5 -6
View File
@@ -21,6 +21,7 @@ from esphome.types import ConfigType
from . import (
ADC,
CONF_NPM1300_ID,
CONF_THERMISTOR_BETA,
CONF_THERMISTOR_RESISTANCE,
CONF_VBUS_CURRENT_LIMIT,
DATA_PUBLISHER,
@@ -29,9 +30,7 @@ from . import (
DUMP_TAG,
IBAT_SCALE,
NPM1300_CHILD_SCHEMA,
THERMISTOR_NONE,
npm1300_ns,
thermistor_beta,
)
DEPENDENCIES = ["npm1300"]
@@ -110,10 +109,10 @@ def _final_validate(config: ConfigType) -> None:
if CONF_BATTERY_TEMPERATURE not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
if hub.get(CONF_THERMISTOR_RESISTANCE, THERMISTOR_NONE) == THERMISTOR_NONE:
if CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
"nominal resistance of the battery thermistor on the npm1300 component",
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' "
"set on the npm1300 component",
path=[CONF_BATTERY_TEMPERATURE],
)
@@ -146,7 +145,7 @@ async def to_code(config: ConfigType) -> None:
if (var := await sensors(CONF_BATTERY_TEMPERATURE, None)) is not None:
hub_config = next(c for c in CORE.config[DOMAIN] if c[CONF_ID] == hub_id)
state = npm1300_ns.ntc_temperature(
_adc(2, 5, 2), 1.0 / thermistor_beta(hub_config)
_adc(2, 5, 2), 1.0 / hub_config[CONF_THERMISTOR_BETA]
)
_add_adc_sensor(hub, var, state, "Battery Temperature")
await sensors(CONF_VBUS_CURRENT_LIMIT, hub.set_vbus_current_limit_sensor)
+3 -11
View File
@@ -15,7 +15,6 @@ from . import (
CONF_THERMISTOR_RESISTANCE,
NPM1300_CHILD_SCHEMA,
OUTPUTS,
THERMISTOR_NONE,
)
DEPENDENCIES = ["npm1300"]
@@ -45,18 +44,11 @@ def _final_validate(config: ConfigType) -> None:
if CONF_CHARGER not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
# A wrong thermistor shifts the battery temperature the charger sees
if CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"The '{CONF_CHARGER}' switch needs '{CONF_THERMISTOR_RESISTANCE}' on the npm1300 "
f"component: the nominal resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
path=[CONF_CHARGER],
)
# Only the charger block turns off the NTC check, without which the charger never starts
if CONF_CHARGER not in hub and hub[CONF_THERMISTOR_RESISTANCE] == THERMISTOR_NONE:
if CONF_CHARGER not in hub and CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block when "
f"'{CONF_THERMISTOR_RESISTANCE}' is '{THERMISTOR_NONE}'",
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block or "
f"'{CONF_THERMISTOR_RESISTANCE}' on the npm1300 component",
path=[CONF_CHARGER],
)
+6 -2
View File
@@ -11,12 +11,14 @@ from esphome.cpp_generator import (
from esphome.types import ConfigType
from . import (
CHARGE_STATUS,
CONF_NPM1300_ID,
DUMP,
DUMP_TAG,
ERROR_REASON,
NPM1300_CHILD_SCHEMA,
STATUS,
STATUS_PUBLISHER,
VBUS_STATUS,
npm1300_ns,
)
@@ -46,7 +48,9 @@ async def to_code(config: ConfigType) -> None:
)
STATUS_PUBLISHER.add(
hub,
npm1300_ns.publish_charger_status(parent_ref(var), last, STATUS),
npm1300_ns.publish_charger_status(
parent_ref(var), last, VBUS_STATUS, CHARGE_STATUS, ERROR_REASON
),
)
DUMP.add(
hub,
+3 -10
View File
@@ -13,7 +13,6 @@ from esphome.build_helpers.ccache import resolve_ccache_path
import esphome.codegen as cg
from esphome.components import network
from esphome.components.zephyr import (
PreferenceStorage,
Section,
add_extra_script,
copy_files as zephyr_copy_files,
@@ -107,7 +106,6 @@ from .const import (
VARIANT_NRF52832,
VARIANT_NRF52840,
VARIANT_REGULATOR,
VARIANTS_NRF54L,
VARIANTS_WITHOUT_USB,
)
from .framework import (
@@ -538,11 +536,6 @@ def _final_validate(config):
FINAL_VALIDATE_SCHEMA = _final_validate
def _preferences_storage(variant: str) -> PreferenceStorage:
"""The storage for preferences: ZMS on the RRAM of the nRF54L, NVS on flash."""
return "ZMS" if variant in VARIANTS_NRF54L else "NVS"
@coroutine_with_priority(CoroPriority.PLATFORM)
async def to_code(config: ConfigType) -> None:
"""Convert the configuration to code."""
@@ -551,7 +544,7 @@ async def to_code(config: ConfigType) -> None:
variant = get_nrf52_variant()
cg.add_define("ESPHOME_VARIANT", variant)
cg.add_build_flag(f"-DUSE_NRF52_VARIANT_{variant}")
if variant in VARIANTS_NRF54L:
if variant in (VARIANT_NRF54L15, VARIANT_NRF54LM20):
# The entropy driver runs on the CRACEN crypto unit, which needs Nordic's PSA crypto
include_west_project("mbedtls")
include_west_project("oberon-psa-crypto")
@@ -619,11 +612,11 @@ async def to_code(config: ConfigType) -> None:
if config[KEY_BOOTLOADER] in ADAFRUIT_BOOTLOADERS:
cg.add_define("USE_BOOTLOADER_ADAFRUIT")
zephyr_setup_preferences(_preferences_storage(variant))
zephyr_setup_preferences()
zephyr_to_code(config)
# The nRF54L has RRAM (cpuapp_rram) and no flash0 node
if CORE.testing_mode and variant not in VARIANTS_NRF54L:
if CORE.testing_mode and variant not in (VARIANT_NRF54L15, VARIANT_NRF54LM20):
zephyr_add_overlay(
f"""
&flash0 {{
+1 -2
View File
@@ -17,7 +17,6 @@ from .const import (
VARIANT_NRF54LM20,
VARIANT_NRF52832,
VARIANT_NRF52840,
VARIANTS_NRF54L,
)
from .framework import (
check_and_install,
@@ -146,7 +145,7 @@ def upload_using_cmsis_dap(unique_id: str | None = None) -> None:
pyocd = [str(paths["python_executable"]), "-m", "pyocd"]
probe = ["-t", target] + (["-u", unique_id] if unique_id else [])
cmd = [*pyocd, "flash", "--no-wait", *probe, "-e", "sector"]
if variant in VARIANTS_NRF54L:
if variant in (VARIANT_NRF54L15, VARIANT_NRF54LM20):
cmd += ["--script", str(_NRF54L_WATCHDOG_SCRIPT)]
cmd += [str(image) for image in images]
if not run_command_ok(cmd, env=env, stream_output=True):
-2
View File
@@ -41,8 +41,6 @@ VARIANT_I2C = {
VARIANT_NRF54LM20: VariantI2C("i2c21", (1, 3)),
}
VARIANTS_WITHOUT_USB = frozenset({VARIANT_NRF52832, VARIANT_NRF54L15})
# The nRF54L family: RRAM instead of flash, the CRACEN crypto unit and the wdt31 watchdog
VARIANTS_NRF54L = frozenset({VARIANT_NRF54L15, VARIANT_NRF54LM20})
# Devicetree label of the main regulator, reg1 on the others
VARIANT_REGULATOR = {VARIANT_NRF52832: "reg"}
VARIANT_FLASH_SIZE = {VARIANT_NRF52832: 0x80000, VARIANT_NRF52840: 0x100000}
+11 -73
View File
@@ -1,6 +1,3 @@
from dataclasses import dataclass, field
import functools
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
@@ -29,7 +26,7 @@ from esphome.core.entity_helpers import (
)
from esphome.cpp_generator import MockObj, MockObjClass, TemplateArguments
from esphome.cpp_types import global_ns
from esphome.types import ConfigType, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@esphome/core"]
DOMAIN = "select"
@@ -50,25 +47,6 @@ SELECT_OPERATION_OPTIONS = SelectOperation.options(
)
@dataclass
class SelectData:
# Options of each select whose option list never changes at run time, by id
fixed_options: dict[str, list[str]] = field(default_factory=dict)
def _get_data() -> SelectData:
if DOMAIN not in CORE.data:
CORE.data[DOMAIN] = SelectData()
return CORE.data[DOMAIN]
def _record_options(
config: ConfigType, options: list[str], fixed_options: bool
) -> None:
if fixed_options:
_get_data().fixed_options[config[CONF_ID].id] = options
_SELECT_SCHEMA = (
cv.ENTITY_BASE_SCHEMA.extend(entity_helpers.WEBSERVER_SORTING_SCHEMA)
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
@@ -127,11 +105,7 @@ async def setup_select_core_(var, config, *, options: list[str]):
await entity_helpers.setup_entity_integrations(var, config)
async def register_select(
var, config, *, options: list[str], fixed_options: bool = False
):
"""``fixed_options=True`` when the options never change at run time, so ``select.set`` can use the index."""
_record_options(config, options, fixed_options)
async def register_select(var, config, *, options: list[str]):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
queue_entity_register("select", config)
@@ -139,9 +113,9 @@ async def register_select(
await setup_select_core_(var, config, options=options)
async def new_select(config, *args, options: list[str], fixed_options: bool = False):
async def new_select(config, *args, options: list[str]):
var = cg.new_Pvariable(config[CONF_ID], *args)
await register_select(var, config, options=options, fixed_options=fixed_options)
await register_select(var, config, options=options)
return var
@@ -157,10 +131,7 @@ def sub_selects(
config: ConfigType, *, parent: MockObj | ID | None = None
) -> SubEntities:
"""Return a SubEntities bound to new_select; ``apply`` statements can use ``entity``, ``index`` and ``option``."""
# Hub selects get their options from code generation only
return SubEntities(
functools.partial(new_select, fixed_options=True), config, parent, APPLY_ENTITY
)
return SubEntities(new_select, config, parent, APPLY_ENTITY)
@coroutine_with_priority(CoroPriority.CORE)
@@ -175,20 +146,13 @@ OPERATION_BASE_SCHEMA = cv.Schema(
)
_SELECT_SET_SCHEMA = OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
)
def _option_index(config: ConfigType, option: str) -> str:
return str(_get_data().fixed_options[config[CONF_ID].id].index(option))
_set_option_builder = automation.apply_action_builder(
automation.register_apply_action(
"select.set",
_SELECT_SET_SCHEMA,
OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
),
automation.ApplyField(
CONF_OPTION,
"set_option",
@@ -197,32 +161,6 @@ _set_option_builder = automation.apply_action_builder(
),
call="make_call",
)
_set_index_builder = automation.apply_action_builder(
"select.set",
_SELECT_SET_SCHEMA,
automation.ApplyField(CONF_OPTION, "set_index", cg.size_t, const_fn=_option_index),
call="make_call",
)
@automation.register_action(
"select.set", automation.ApplyAction, _SELECT_SET_SCHEMA, synchronous=True
)
async def select_set_to_code(
config: ConfigType,
action_id: ID,
template_arg: TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
# A constant fixed option becomes its index
await cg.get_variable(config[CONF_ID])
option = config[CONF_OPTION]
if not cg.is_template(option) and option in _get_data().fixed_options.get(
config[CONF_ID].id, ()
):
return await _set_index_builder(config, action_id, template_arg, args)
return await _set_option_builder(config, action_id, template_arg, args)
automation.register_apply_action(
"select.set_index",
@@ -113,7 +113,7 @@ async def to_code(config):
milliseconds=SCHEDULER_DONT_RUN
)
await cg.register_component(var, component_config)
await select.register_select(var, config, options=options, fixed_options=True)
await select.register_select(var, config, options=options)
if CONF_LAMBDA in config:
lambda_ = await cg.process_lambda(
@@ -36,8 +36,13 @@ void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref,
}
void update_lambda(BaseTemplateSelect *sel_comp, const optional<std::string> &val) {
if (val.has_value())
if (val.has_value()) {
if (!sel_comp->has_option(*val)) {
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
return;
}
sel_comp->publish_state(*val);
}
}
} // namespace esphome::template_
+1 -1
View File
@@ -115,7 +115,7 @@ async def to_code(config):
options = list(options_map.values())
mappings = list(options_map.keys())
var = await select.new_select(config, options=options, fixed_options=True)
var = await select.new_select(config, options=options)
if mappings:
cg.add(var.set_select_mappings(mappings))
hub = await cg.add_variable(var.set_tuya_parent, config[CONF_TUYA_ID])
@@ -15,9 +15,6 @@ namespace esphome::uart {
ESPHOME_LOG_TAG(TAG, "uart");
// GPIO32-47 (RP2350B) and GPIO64 (CYW43) are in no mask; a 32-bit shift by them is undefined.
static constexpr bool pin_in_mask(uint8_t pin, uint32_t mask) { return pin < 32 && (mask & (1U << pin)) != 0; }
uint16_t RP2UartComponent::get_config() {
uint16_t config = 0;
@@ -84,9 +81,9 @@ void RP2UartComponent::setup() {
if (this->tx_pin_->is_inverted()) {
ESP_LOGD(TAG, "An inverted TX pin %u can only be used with SerialPIO", this->tx_pin_->get_pin());
} else {
if (pin_in_mask(this->tx_pin_->get_pin(), valid_tx_uart_0)) {
if (((1 << this->tx_pin_->get_pin()) & valid_tx_uart_0) != 0) {
tx_hw = 0;
} else if (pin_in_mask(this->tx_pin_->get_pin(), valid_tx_uart_1)) {
} else if (((1 << this->tx_pin_->get_pin()) & valid_tx_uart_1) != 0) {
tx_hw = 1;
} else {
ESP_LOGD(TAG, "TX pin %u can only be used with SerialPIO", this->tx_pin_->get_pin());
@@ -98,9 +95,9 @@ void RP2UartComponent::setup() {
if (this->rx_pin_->is_inverted()) {
ESP_LOGD(TAG, "An inverted RX pin %u can only be used with SerialPIO", this->rx_pin_->get_pin());
} else {
if (pin_in_mask(this->rx_pin_->get_pin(), valid_rx_uart_0)) {
if (((1 << this->rx_pin_->get_pin()) & valid_rx_uart_0) != 0) {
rx_hw = 0;
} else if (pin_in_mask(this->rx_pin_->get_pin(), valid_rx_uart_1)) {
} else if (((1 << this->rx_pin_->get_pin()) & valid_rx_uart_1) != 0) {
rx_hw = 1;
} else {
ESP_LOGD(TAG, "RX pin %u can only be used with SerialPIO", this->rx_pin_->get_pin());
+21 -31
View File
@@ -5,57 +5,47 @@
namespace esphome::wifi {
/// Which expected leave a disconnect matched.
enum class ExpectedLeave : uint8_t {
EXPECTED_LEAVE_NONE,
EXPECTED_LEAVE_AP,
EXPECTED_LEAVE_ATTEMPT, ///< An attempt that never associated, so no BSSID
};
// Switching APs calls esp_wifi_disconnect(), which queues a leave event for the old AP, or with no BSSID for an
// attempt that never associated. Remembers it so that event does not end the next attempt. Platform free for tests.
// Switching APs calls esp_wifi_disconnect(), which queues an association-leave event from the old AP. Remembers
// that AP so its leave event does not end the connection attempt started after it. Platform free for host tests.
class ExpectedDisconnect {
public:
void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
void clear() { this->expected_ = ExpectedLeave::EXPECTED_LEAVE_NONE; }
void arm(const uint8_t *bssid) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->armed_ = true;
}
void clear() { this->armed_ = false; }
/// Disconnects and, once that succeeds, expects the leave event. read_bssid(uint8_t *out) runs first, since the
/// record is gone after the disconnect, and writes the AP's BSSID only when it returns true; without an AP the
/// attempt's leave, with no BSSID, is expected. disconnect() returns whether it succeeded, which is the result.
/// Disconnects from the current AP and, once that succeeds, expects its leave event. read_bssid(uint8_t *out)
/// returns whether there was an AP and runs first, since the record is gone after the disconnect; disconnect()
/// returns whether it succeeded, which is also the result. Any earlier expectation is dropped.
template<typename ReadBssid, typename Disconnect>
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
this->clear();
this->armed_ = false;
std::array<uint8_t, 6> bssid{};
const bool had_ap = read_bssid(bssid.data());
if (!disconnect())
return false;
this->arm_(bssid.data(), had_ap ? ExpectedLeave::EXPECTED_LEAVE_AP : ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
if (had_ap)
this->arm(bssid.data());
return true;
}
/// A connected event still queued from the previous AP; it keeps the expectation.
bool is_previous_ap(const uint8_t *bssid) const {
return this->expected_ != ExpectedLeave::EXPECTED_LEAVE_NONE &&
std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
return this->armed_ && std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
}
/// Which expected leave a disconnect is. \p is_leave covers an AP's association leave (8) and the station leave
/// of an attempt that never associated (36). Every disconnect ends the expectation, so it matches at most once.
ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave) {
const ExpectedLeave matched =
is_leave && this->is_previous_ap(bssid) ? this->expected_ : ExpectedLeave::EXPECTED_LEAVE_NONE;
this->clear();
return matched;
/// Whether a disconnect is the previous AP's leave. Every disconnect ends the expectation, so it matches at
/// most once; the caller handles roaming disconnects before this.
bool consume_disconnect(const uint8_t *bssid, bool assoc_leave) {
const bool expected = assoc_leave && this->is_previous_ap(bssid);
this->armed_ = false;
return expected;
}
protected:
void arm_(const uint8_t *bssid, ExpectedLeave expected) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->expected_ = expected;
}
std::array<uint8_t, 6> bssid_{};
ExpectedLeave expected_{ExpectedLeave::EXPECTED_LEAVE_NONE};
bool armed_{false};
};
} // namespace esphome::wifi
@@ -1158,12 +1158,10 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
ESP_LOGI(TAG, "Disconnected ssid='%.*s' reason='Station Roaming'", it.ssid_len, (const char *) it.ssid);
return;
}
const ExpectedLeave expected = s_expected_disconnect.consume_disconnect(
it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE || it.reason == WIFI_REASON_STA_LEAVING);
const bool expected_disconnect = expected != ExpectedLeave::EXPECTED_LEAVE_NONE;
const bool expected_disconnect =
s_expected_disconnect.consume_disconnect(it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE);
if (expected_disconnect) {
ESP_LOGD(TAG, "Expected leave of the previous %s",
expected == ExpectedLeave::EXPECTED_LEAVE_AP ? LOG_STR_LITERAL("AP") : LOG_STR_LITERAL("attempt"));
ESP_LOGD(TAG, "Expected disconnect from previous AP");
} else if (it.reason == WIFI_REASON_NO_AP_FOUND) {
ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
(const char *) it.ssid);
@@ -1183,9 +1181,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
// Refresh is_connected() cache; the old session is no longer connected.
this->update_connected_state_();
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
// An attempt that never associated ended no session listeners know of
if (expected != ExpectedLeave::EXPECTED_LEAVE_ATTEMPT)
this->notify_disconnect_state_listeners_();
this->notify_disconnect_state_listeners_();
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
+3 -11
View File
@@ -1,7 +1,7 @@
from collections.abc import Callable
from pathlib import Path
import textwrap
from typing import Literal, NamedTuple, TypedDict
from typing import NamedTuple, TypedDict
import esphome.codegen as cg
import esphome.config_validation as cv
@@ -212,18 +212,10 @@ async def _cdc_acm_to_code() -> None:
_cdc_acm_add_ports()
PreferenceStorage = Literal["NVS", "ZMS"]
def zephyr_setup_preferences(storage: PreferenceStorage = "NVS") -> None:
"""Store preferences through the settings subsystem.
`storage` picks the Zephyr storage backend: NVS for flash, ZMS for RRAM and MRAM.
"""
def zephyr_setup_preferences() -> None:
cg.add(zephyr_ns.setup_preferences())
zephyr_add_prj_conf("SETTINGS", True)
zephyr_add_prj_conf(storage, True)
zephyr_add_prj_conf(f"SETTINGS_{storage}", True)
zephyr_add_prj_conf("NVS", True)
zephyr_add_prj_conf("FLASH_MAP", True)
zephyr_add_prj_conf("FLASH", True)
@@ -8,16 +8,18 @@ namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.attribute");
void ZigbeeAttribute::set_attr_internal() {
void ZigbeeAttribute::set_attr_() {
if (!this->zb_->is_started()) {
return;
}
if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
ezb_zcl_status_t state = ezb_zcl_set_attr_value(this->endpoint_id_, this->cluster_id_, this->role_, this->attr_id_,
EZB_ZCL_STD_MANUF_CODE, this->value_p_, false);
// cleared before report_() so it can disable the loop
// when the report has to wait for join
this->set_attr_requested_ = false;
if (this->force_report_) {
this->report_internal(true);
this->report_(true);
}
// Check for error
if (state != EZB_ZCL_STATUS_SUCCESS) {
@@ -28,12 +30,15 @@ void ZigbeeAttribute::set_attr_internal() {
}
}
void ZigbeeAttribute::report_internal(bool has_lock) {
void ZigbeeAttribute::report_(bool has_lock) {
if (!this->report_enabled) {
return;
}
if (!this->zb_->is_joined()) {
this->report_requested_ = true;
if (!this->set_attr_requested_) {
this->disable_loop();
}
return;
}
if (has_lock or esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
@@ -56,6 +61,43 @@ void ZigbeeAttribute::report_internal(bool has_lock) {
}
}
void ZigbeeAttribute::set_report(ZigbeeReportT report) {
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
return;
}
this->report_enabled = true;
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
this->force_report_ = true;
}
this->zb_->add_on_join_callback([this](bool) {
if (this->report_requested_) {
this->enable_loop();
}
});
}
void ZigbeeAttribute::report() {
if (!this->report_enabled) {
return;
}
this->report_requested_ = true;
this->enable_loop();
}
void ZigbeeAttribute::loop() {
if (this->set_attr_requested_) {
this->set_attr_();
}
if (this->report_requested_) {
this->report_(false);
}
if (!this->report_requested_ && !this->set_attr_requested_) {
this->disable_loop();
}
}
} // namespace esphome::zigbee
#endif
Loaded 100 of 131 files, more files were not shown because too many files have changed in this diff. Show more