Compare commits

..
Author SHA1 Message Date
J. Nick Koston 8c11a3ba7d Say when console_write() can write fewer bytes 2026-11-16 23:57:47 -06:00
J. Nick Koston a2c07ae165 Describe the console limits by the IDF option, include host 2026-11-16 23:18:51 -06:00
J. Nick Koston 18eff452ba Say where request_console() does nothing 2026-11-16 23:05:22 -06:00
J. Nick Koston f81920f9e0 Mark the USB CDC console guards as needed 2026-11-16 22:54:05 -06:00
J. Nick Koston d4efd41c38 Restore the USB CDC console guards for targets without USB OTG 2026-11-16 22:53:39 -06:00
J. Nick Koston 8e54625bde Drop the USB CDC sdkconfig checks the port selection implies, use pdMS_TO_TICKS, shorten the port table test 2026-11-16 22:29:33 -06:00
J. Nick Koston 3cee50f683 Merge remote-tracking branch 'upstream/dev' into logger-console-api 2026-11-16 22:17:34 -06:00
Mark Mayo e12784543d [logger] Copy log lines that have no arguments instead of formatting them (#22644) 2026-11-16 22:16:47 -06:00
J. Nick Koston bc3e9c88f4 [select] Set a fixed option by index and look up a template lambda's option once (#22655) 2026-11-16 22:48:05 -05:00
luar123andCopilot Autofix powered by AI 7e3c2ebc10 [zigbee] refactor ZigbeeAttribute (#22765)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-11-16 22:31:05 -05:00
J. Nick Koston 3f6c0f35fe [modem] Set the APN while the SIM is powered down (#22817) 2026-11-17 16:23:11 +13:00
J. Nick Koston 200bf69035 [wifi] Join the next network after a failed hidden network probe on ESP32 (#22764) 2026-11-17 16:23:06 +13:00
huangkongshengandhuangkongsheng 0054041d6a [core] Reject sign-padded bind key pairs (#22660)
Signed-off-by: huangkongsheng <huangkongsheng@users.noreply.github.com>
Co-authored-by: huangkongsheng <huangkongsheng@users.noreply.github.com>
2026-11-16 22:15:45 -05:00
J. Nick Koston 7b7f3cf50b Compile init_uart only for UART selections 2026-11-16 13:07:33 -06:00
J. Nick Koston 4304a92674 Assert the port name and selection per config, define them beside each port in defines.h 2026-11-16 13:07:33 -06:00
J. Nick Koston 6b4acbcdc4 Keep the hardware_uart selection map for device-builder, check the USB CDC read count 2026-11-16 13:07:33 -06:00
J. Nick Koston d618b41db9 Name the console UART and its selection with one define each 2026-11-16 13:07:33 -06:00
J. Nick Koston 941286480f [logger] Fix the console UART at build time and add a console read/write API 2026-11-16 13:07:33 -06:00
110 changed files with 1311 additions and 3930 deletions

No files matched your search

+2 -4
View File
@@ -20,9 +20,7 @@ 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.
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.
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.
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
@@ -160,7 +158,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,4 +87,3 @@ 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,26 +67,3 @@ 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,15 +86,3 @@ 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,116 +44,3 @@ 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).
@@ -1,30 +0,0 @@
# 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,22 +36,3 @@ 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,23 +37,3 @@ 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
@@ -1,129 +0,0 @@
"""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()
@@ -1,78 +0,0 @@
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
@@ -1,126 +0,0 @@
#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;
}
}
@@ -1,40 +0,0 @@
# 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);
@@ -1,15 +0,0 @@
#!/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 ]
@@ -1,11 +0,0 @@
#!/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
@@ -1,116 +0,0 @@
#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
@@ -1,17 +0,0 @@
#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
@@ -1,19 +0,0 @@
#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
@@ -1,12 +0,0 @@
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
@@ -1,16 +0,0 @@
#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
@@ -1,28 +0,0 @@
# 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
@@ -1,12 +0,0 @@
# 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}
@@ -1,5 +0,0 @@
substitutions:
name: c32pr
packages:
common: !include c-common.yaml
board: !include esp32-ir.yaml
@@ -1,98 +0,0 @@
"""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)
@@ -1,74 +0,0 @@
"""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())
@@ -1,24 +0,0 @@
# 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}
@@ -1,23 +0,0 @@
# 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);
@@ -1,53 +0,0 @@
"""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))
@@ -1,30 +0,0 @@
#!/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
@@ -1,14 +0,0 @@
# 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")
@@ -1,52 +0,0 @@
# 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();
@@ -1,29 +0,0 @@
#!/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
@@ -1,3 +0,0 @@
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}
@@ -1,6 +0,0 @@
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,9 +99,6 @@ 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}")
@@ -1,22 +0,0 @@
"""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")))
@@ -1,33 +0,0 @@
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());
@@ -1,22 +0,0 @@
#!/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")"
@@ -1,44 +0,0 @@
"""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")
@@ -1,99 +0,0 @@
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"
@@ -1,240 +0,0 @@
#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);
}
@@ -1,20 +0,0 @@
# 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:
@@ -1,13 +0,0 @@
"""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)
@@ -1,108 +0,0 @@
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"
@@ -1,97 +0,0 @@
#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());
}
@@ -1,16 +0,0 @@
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:
@@ -1,29 +0,0 @@
"""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())
@@ -1,36 +0,0 @@
"""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)
@@ -1,16 +0,0 @@
# 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());
@@ -1,19 +0,0 @@
# 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());
@@ -1,64 +0,0 @@
"""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()
@@ -1,86 +0,0 @@
"""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"]))
@@ -1,31 +0,0 @@
"""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}"
)
@@ -1,25 +0,0 @@
#!/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
@@ -1,29 +0,0 @@
#!/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
@@ -1,21 +0,0 @@
#!/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
@@ -1,208 +0,0 @@
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
@@ -1,116 +0,0 @@
"""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]))
@@ -1,194 +0,0 @@
#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
@@ -1,41 +0,0 @@
"""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 mibeacon import XB_FILL, c_rows, enc, plain, t16, th
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
OTA = "--ota" in sys.argv[1:]
CYCLES = 40
@@ -25,24 +25,41 @@ MIDX = {k: i for i, k in enumerate(MACS)}
def mibeacon_plain(dev, pid, cnt, obj):
return plain(MAC[dev], pid, cnt, obj)
return (
bytes([0x50, 0x20])
+ struct.pack("<H", pid)
+ bytes([cnt])
+ MAC[dev][::-1]
+ obj
)
def mibeacon_enc(dev, pid, cnt, obj, key, short, corrupt=False):
out = enc(
MAC[dev],
pid,
cnt,
obj,
key,
short,
version=0x59 if short else 0x58,
corrupt=corrupt,
)
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
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)]
@@ -108,19 +125,47 @@ 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 = (
XB_FILL
+ """
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;
}
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];
xb_inject_one(XB_MACS[row[0]], row[1] == 0 ? 0xFE95 : 0x181A, row + 3, row[2]);
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);
}
}
@@ -135,7 +180,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], 0xFE95, XB_PROBE, sizeof(XB_PROBE));
xb_fill(r, XB_MACS[0], true, 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);
@@ -144,7 +189,6 @@ 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
@@ -1,419 +0,0 @@
"""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)
)
@@ -1,84 +0,0 @@
"""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);
}
"""
@@ -1,20 +0,0 @@
#!/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
@@ -1,45 +0,0 @@
# 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)++;
@@ -1,24 +0,0 @@
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
+3 -1
View File
@@ -605,7 +605,9 @@ 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])
var = await select.new_select(
conf, options=conf[CONF_OPTIONS], fixed_options=True
)
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)
var = await select.new_select(config, options=DUCO_MODE_OPTIONS, fixed_options=True)
await register_duco_device(var, config, config[CONF_DUCO_ID])
+26 -15
View File
@@ -94,6 +94,7 @@ UART_SELECTION_RP2040 = [USB_CDC, UART0, UART1]
UART_SELECTION_NRF52 = [USB_CDC, UART0]
# Read by device-builder
HARDWARE_UART_TO_UART_SELECTION = {
UART0: logger_ns.UART_SELECTION_UART0,
UART0_SWAP: logger_ns.UART_SELECTION_UART0_SWAP,
@@ -538,17 +539,17 @@ async def to_code(config: ConfigType) -> None:
cg.add(log.set_boot_log_level(tag, LOG_LEVELS[log_level]))
if CORE.is_esp32 or CORE.is_host:
cg.add(log.create_pthread_key())
# set_uart_selection() must be called before pre_setup() because
# pre_setup() switches on uart_ to decide which hardware to initialize
# (e.g. UART0 vs USB_SERIAL_JTAG). Without this, uart_ is still the
# default UART_SELECTION_UART0 and the wrong hardware gets initialized.
# uart_ is UART0 in C++ except on LibreTiny where it is DEFAULT; skip the
# setter when the config matches it.
cpp_default_uart = DEFAULT if CORE.is_libretiny else UART0
if (
hardware_uart := config.get(CONF_HARDWARE_UART)
) is not None and hardware_uart != cpp_default_uart:
cg.add(log.set_uart_selection(HARDWARE_UART_TO_UART_SELECTION[hardware_uart]))
if (hardware_uart := config.get(CONF_HARDWARE_UART)) is not None:
cg.add_define(f"USE_LOGGER_UART_SELECTION_{hardware_uart}")
cg.add_define(
"ESPHOME_LOGGER_UART_SELECTION",
cg.RawExpression(f"logger::UART_SELECTION_{hardware_uart}"),
)
# ESP8266 without serial logging never opens a port
cg.add_define(
"ESPHOME_LOGGER_UART_NAME",
"NONE" if CORE.is_esp8266 and baud_rate == 0 else hardware_uart,
)
# pre_setup() sets global_logger and must run before any other code
# that may call ESP_LOG* (e.g. setup_preferences contains ESP_LOGVV).
cg.add(log.pre_setup())
@@ -645,7 +646,6 @@ async def _late_logger_init(config: ConfigType) -> None:
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_CDC", True)
elif config[CONF_HARDWARE_UART] == USB_SERIAL_JTAG:
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", True)
cg.add_define("USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG")
# Log V2 formats centrally in esp_log() (~4KB flash, ~180B RAM). 6.1
# adds CONSTRAINED_ENV_SAFE=n, keeping early/DRAM logs on
# esp_rom_printf instead of pulling ~1.2KB esp_rom_vprintf into IRAM.
@@ -663,10 +663,12 @@ async def _late_logger_init(config: ConfigType) -> None:
# --wrap: a strong definition collides; see logger_esp32.cpp
cg.add_build_flag("-Wl,--wrap=esp_log_format")
# Zephyr polls the USB port only when it logs there
if config.get(CONF_HARDWARE_UART) == USB_CDC and (
has_serial_logging or not CORE.is_nrf52
if (
CORE.using_zephyr
and has_serial_logging
and config[CONF_HARDWARE_UART] == USB_CDC
):
cg.add_define("USE_LOGGER_UART_SELECTION_USB_CDC")
cg.add_define("USE_LOGGER_ZEPHYR_CDC_LOOP")
# Define platform support flags for components that need auto-detection
try:
uart_selection(USB_SERIAL_JTAG)
@@ -884,6 +886,15 @@ def request_logger_level_listeners() -> None:
_request_log_listener_slot = cg.slot_counter("ESPHOME_LOG_MAX_LISTENERS")
def request_console() -> None:
"""Compile in Logger::console_read() and console_write(); call from to_code.
On ESP32 a USB_CDC logger only has a console where ESP-IDF sets CONFIG_ESP_CONSOLE_USB_CDC (targets with
USB OTG); elsewhere, such as the C3, C6 or H2, both do nothing. Host and Zephyr have no console API.
"""
cg.add_define("USE_LOGGER_CONSOLE")
def request_log_listener() -> None:
"""Reserve a log listener slot; call once per listener from to_code."""
cg.add_define("USE_LOG_LISTENERS")
+22 -1
View File
@@ -114,6 +114,26 @@ 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 "
@@ -161,7 +181,8 @@ struct LogBuffer {
this->pos += ANSI_RESET_LEN;
}
}
void strip_trailing_newlines_() {
// Forced inline: an outlined copy slows every formatted line
inline void ESPHOME_ALWAYS_INLINE strip_trailing_newlines_() {
while (this->pos > 0 && this->data[this->pos - 1] == '\n')
this->pos--;
}
+75 -8
View File
@@ -19,6 +19,15 @@ 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
@@ -51,11 +60,7 @@ 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
// 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]] {
if (under_lwip_core_lock()) [[unlikely]] {
this->log_vprintf_non_main_thread_(level, tag, line, format, args, nullptr);
return;
}
@@ -159,6 +164,49 @@ 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,
@@ -200,10 +248,10 @@ Logger::Logger(uint32_t baud_rate) : baud_rate_(baud_rate) {
#endif
}
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
void Logger::loop() {
this->process_messages_();
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
this->cdc_loop_();
#endif
}
@@ -230,7 +278,7 @@ void Logger::process_messages_() {
}
}
// Zephyr with USB CDC keeps the loop running for cdc_loop_()
#if !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
#ifndef USE_LOGGER_ZEPHYR_CDC_LOOP
else {
// A concurrent enable_loop_soon_any_context() still wakes the next iteration
this->disable_loop();
@@ -323,6 +371,25 @@ static const LogString *get_log_level_str(uint8_t level) {
return LogLevelStrings::get_log_str(level, LogLevelStrings::LAST_INDEX);
}
#ifndef USE_HOST
const LogString *Logger::get_uart_selection_() {
#ifdef USE_LOGGER_UART_SELECTION_DEFAULT
// LibreTiny resolves DEFAULT to the board's port only when serial logging is on
if (this->baud_rate_ > 0) {
switch (this->get_uart()) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
default:
return LOG_STR("UART2");
}
}
#endif
return LOG_STR(ESPHOME_LOGGER_UART_NAME);
}
#endif
void Logger::dump_config() {
ESP_LOGCONFIG(TAG,
"Logger:\n"
+57 -11
View File
@@ -124,6 +124,9 @@ enum UARTSelection : uint8_t {
UART_SELECTION_UART0_SWAP,
#endif // USE_ESP8266
};
// The port chosen by hardware_uart, fixed at build time
static constexpr UARTSelection CONFIGURED_UART_SELECTION = ESPHOME_LOGGER_UART_SELECTION;
#endif // USE_ESP32 || USE_ESP8266 || USE_RP2 || USE_LIBRETINY || USE_ZEPHYR
/**
@@ -146,7 +149,7 @@ enum UARTSelection : uint8_t {
class Logger final : public Component {
public:
explicit Logger(uint32_t baud_rate);
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
void loop() override;
#endif
#if defined(USE_RP2_CRASH_HANDLER) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
@@ -160,11 +163,11 @@ class Logger final : public Component {
#endif
#ifdef USE_ESP32
/// UART_NUM_0 when logging to USB
uart_port_t get_uart_num() const {
constexpr uart_port_t get_uart_num() const {
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
return UART_NUM_0;
#else
return static_cast<uart_port_t>(this->uart_);
return static_cast<uart_port_t>(CONFIGURED_UART_SELECTION);
#endif
}
#endif
@@ -172,9 +175,35 @@ class Logger final : public Component {
void create_pthread_key() { pthread_key_create(&this->log_recursion_key_, nullptr); }
#endif
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
void set_uart_selection(UARTSelection uart_selection) { this->uart_ = uart_selection; }
/// Get the UART used by the logger.
UARTSelection get_uart() const { return this->uart_; }
#ifdef USE_LIBRETINY
// DEFAULT resolves to the board's default port
constexpr UARTSelection get_uart() const {
return CONFIGURED_UART_SELECTION == UART_SELECTION_DEFAULT ? static_cast<UARTSelection>(LT_UART_DEFAULT_SERIAL + 1)
: CONFIGURED_UART_SELECTION;
}
#else
constexpr UARTSelection get_uart() const { return CONFIGURED_UART_SELECTION; }
#endif
#endif
#ifdef USE_LOGGER_CONSOLE
// Raw bytes on the console port, without log framing; request with logger.request_console()
// console_write() returns the bytes written: a UART write blocks until all are queued, but a USB write can stop
// short when the host is not reading (USB Serial/JTAG gives up after 20 ms)
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
size_t console_write(const uint8_t *data, size_t len) { return this->hw_serial_->write(data, len); }
bool console_read(uint8_t &byte) {
int c = this->hw_serial_->read();
if (c < 0)
return false;
byte = c;
return true;
}
#elif defined(USE_ESP32)
size_t console_write(const uint8_t *data, size_t len);
/// Non-blocking read of one byte; false if none is waiting.
bool console_read(uint8_t &byte);
#endif
#endif
/// Set the default log level for this logger.
@@ -223,6 +252,12 @@ 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
@@ -245,7 +280,7 @@ class Logger final : public Component {
void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args,
const char *thread_name);
#endif
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
void cdc_loop_();
#endif
void process_messages_();
@@ -292,7 +327,7 @@ class Logger final : public Component {
}
// Helper to write log buffer to console if logging is enabled
inline void HOT write_log_buffer_to_console_(LogBuffer &buf) {
inline void HOT ESPHOME_ALWAYS_INLINE write_log_buffer_to_console_(LogBuffer &buf) {
if (this->baud_rate_ > 0)
this->write_to_console_(buf);
}
@@ -321,6 +356,21 @@ 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,
@@ -390,10 +440,6 @@ class Logger final : public Component {
bool boot_log_done_{false}; // Set on first replay; stops further capture
#endif
uint8_t current_level_{ESPHOME_LOG_LEVEL_VERY_VERBOSE};
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
// 0 is UART0, or DEFAULT on LibreTiny; must match cpp_default_uart in __init__.py
UARTSelection uart_{};
#endif
bool main_task_recursion_guard_{false}; // Guards tx_buffer_ on the main loop
#ifdef USE_LIBRETINY
bool non_main_task_recursion_guard_{false}; // Shared guard for all non-main tasks on LibreTiny
+35 -37
View File
@@ -17,6 +17,10 @@
#include <driver/usb_serial_jtag_vfs.h>
#endif
#endif
// The logger accepts USB_CDC on targets without USB OTG (C3, C6, H2, ...), where IDF has no CDC console
#if defined(USE_LOGGER_CONSOLE) && defined(USE_LOGGER_UART_SELECTION_USB_CDC) && defined(CONFIG_ESP_CONSOLE_USB_CDC)
#include <esp_private/usb_console.h>
#endif
#if defined(CONFIG_PM_ENABLE) && defined(CONFIG_FREERTOS_USE_TICKLESS_IDLE) && \
(ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
#include "esp_sleep.h"
@@ -73,6 +77,7 @@ static void init_usb_serial_jtag() {
}
#endif
#if !defined(USE_LOGGER_UART_SELECTION_USB_CDC) && !defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
uart_config_t uart_config{};
uart_config.baud_rate = (int) baud_rate;
@@ -97,6 +102,7 @@ static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_si
esp_sleep_set_console_uart_handling_mode(ESP_SLEEP_ALWAYS_FLUSH_UART);
#endif
}
#endif
// get_uart_num() and pre_setup() cast the selection straight to a port
static_assert(static_cast<int>(UART_SELECTION_UART0) == static_cast<int>(UART_NUM_0) &&
@@ -107,26 +113,11 @@ static_assert(static_cast<int>(UART_SELECTION_UART2) == static_cast<int>(UART_NU
void Logger::pre_setup() {
if (this->baud_rate_ > 0) {
switch (this->uart_) {
case UART_SELECTION_UART0:
case UART_SELECTION_UART1:
#ifdef USE_ESP32_VARIANT_ESP32
case UART_SELECTION_UART2:
#endif
init_uart(static_cast<uart_port_t>(this->uart_), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
break;
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
break;
#endif
#ifdef USE_LOGGER_USB_SERIAL_JTAG
case UART_SELECTION_USB_SERIAL_JTAG:
#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
init_usb_serial_jtag();
init_usb_serial_jtag();
#elif !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
init_uart(this->get_uart_num(), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
#endif
break;
#endif
}
}
global_logger = this;
@@ -138,29 +129,36 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
#ifdef USE_ESP32_VARIANT_ESP32
case UART_SELECTION_UART2:
return LOG_STR("UART2");
#ifdef USE_LOGGER_CONSOLE
size_t Logger::console_write(const uint8_t *data, size_t len) {
int written = 0;
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#ifdef CONFIG_ESP_CONSOLE_USB_CDC // keep: not set without USB OTG, see the include above
written = esp_usb_console_write_buf(reinterpret_cast<const char *>(data), len);
#endif
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
return LOG_STR("USB_CDC");
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
written = usb_serial_jtag_write_bytes(data, len, pdMS_TO_TICKS(20));
#else
written = uart_write_bytes(this->get_uart_num(), data, len);
#endif
#ifdef USE_LOGGER_USB_SERIAL_JTAG
case UART_SELECTION_USB_SERIAL_JTAG:
return LOG_STR("USB_SERIAL_JTAG");
#endif
default:
return LOG_STR("UNKNOWN");
}
return written > 0 ? written : 0;
}
bool Logger::console_read(uint8_t &byte) {
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#ifdef CONFIG_ESP_CONSOLE_USB_CDC // keep: not set without USB OTG, see the include above
return esp_usb_console_available_for_read() && esp_usb_console_read_buf(reinterpret_cast<char *>(&byte), 1) == 1;
#else
return false;
#endif
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
return usb_serial_jtag_read_bytes(&byte, 1, 0) > 0;
#else
return uart_read_bytes(this->get_uart_num(), &byte, 1, 0) > 0;
#endif
}
#endif
#ifdef USE_CRASH_HANDLER
bool crash_log_pending() { return esp32::crash_handler_has_data(); }
void log_crash_to_client() {
+3 -16
View File
@@ -14,9 +14,9 @@ void Logger::pre_setup() {
#if defined(USE_ESP8266_LOGGER_SERIAL)
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
Serial.swap();
}
#ifdef USE_LOGGER_UART_SELECTION_UART0_SWAP
Serial.swap();
#endif
Serial.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
this->hw_serial_ = &Serial1;
@@ -35,19 +35,6 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
#if defined(USE_ESP8266_LOGGER_SERIAL)
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
return LOG_STR("UART0_SWAP");
}
return LOG_STR("UART0");
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
return LOG_STR("UART1");
#else
return LOG_STR("NONE");
#endif
}
#ifdef USE_CRASH_HANDLER
bool crash_log_pending() { return esp8266::crash_handler_has_data(); }
void log_crash_to_client() { esp8266::crash_handler_log(); }
+20 -51
View File
@@ -12,45 +12,28 @@ ESPHOME_LOG_TAG(TAG, "logger");
void Logger::pre_setup() {
if (this->baud_rate_ > 0) {
switch (this->uart_) {
#if LT_HW_UART0
case UART_SELECTION_UART0:
this->hw_serial_ = &Serial0;
libretiny::ensure_serial_handle(0);
Serial0.begin(this->baud_rate_);
break;
#endif
#if LT_HW_UART1
case UART_SELECTION_UART1:
this->hw_serial_ = &Serial1;
libretiny::ensure_serial_handle(1);
Serial1.begin(this->baud_rate_);
break;
#endif
#if LT_HW_UART2
case UART_SELECTION_UART2:
this->hw_serial_ = &Serial2;
libretiny::ensure_serial_handle(2);
Serial2.begin(this->baud_rate_);
break;
#endif
default:
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
if (this->uart_ != UART_SELECTION_DEFAULT) {
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
"The default port was used instead.");
}
break;
#if defined(USE_LOGGER_UART_SELECTION_UART0) && LT_HW_UART0
this->hw_serial_ = &Serial0;
libretiny::ensure_serial_handle(0);
Serial0.begin(this->baud_rate_);
#elif defined(USE_LOGGER_UART_SELECTION_UART1) && LT_HW_UART1
this->hw_serial_ = &Serial1;
libretiny::ensure_serial_handle(1);
Serial1.begin(this->baud_rate_);
#elif defined(USE_LOGGER_UART_SELECTION_UART2) && LT_HW_UART2
this->hw_serial_ = &Serial2;
libretiny::ensure_serial_handle(2);
Serial2.begin(this->baud_rate_);
#else
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
if (CONFIGURED_UART_SELECTION != UART_SELECTION_DEFAULT) {
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
"The default port was used instead.");
}
#endif
// change lt_log() port to match default Serial
if (this->uart_ == UART_SELECTION_DEFAULT) {
this->uart_ = (UARTSelection) (LT_UART_DEFAULT_SERIAL + 1);
lt_log_set_port(LT_UART_DEFAULT_SERIAL);
} else {
lt_log_set_port(this->uart_ - 1);
}
lt_log_set_port(this->get_uart() - 1);
}
global_logger = this;
@@ -60,20 +43,6 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_DEFAULT:
return LOG_STR("DEFAULT");
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
case UART_SELECTION_UART2:
default:
return LOG_STR("UART2");
}
}
#ifdef USE_LIBRETINY_CRASH_HANDLER
bool crash_log_pending() { return libretiny::crash_handler_has_data(); }
void log_crash_to_client() { libretiny::crash_handler_log(); }
+10 -27
View File
@@ -26,20 +26,16 @@ void Logger::setup() {
void Logger::pre_setup() {
if (this->baud_rate_ > 0) {
switch (this->uart_) {
case UART_SELECTION_UART0:
this->hw_serial_ = &Serial1;
Serial1.begin(this->baud_rate_);
break;
case UART_SELECTION_UART1:
this->hw_serial_ = &Serial2;
Serial2.begin(this->baud_rate_);
break;
case UART_SELECTION_USB_CDC:
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
break;
}
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
this->hw_serial_ = &Serial2;
Serial2.begin(this->baud_rate_);
#else
this->hw_serial_ = &Serial1;
Serial1.begin(this->baud_rate_);
#endif
}
global_logger = this;
ESP_LOGI(TAG, "Log initialized");
@@ -49,19 +45,6 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
case UART_SELECTION_USB_CDC:
return LOG_STR("USB_CDC");
default:
return LOG_STR("UNKNOWN");
}
}
#ifdef USE_CRASH_HANDLER
bool crash_log_pending() { return rp2::crash_handler_has_data(); }
void log_crash_to_client() { rp2::crash_handler_log(); }
+13 -34
View File
@@ -42,7 +42,7 @@ __attribute__((section(".noinit"))) struct {
ESPHOME_LOG_TAG(TAG, "logger");
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
void Logger::cdc_loop_() {
if (this->uart_dev_ == nullptr) {
return;
@@ -75,25 +75,19 @@ void Logger::pre_setup() {
#ifdef CONFIG_SERIAL
if (this->baud_rate_ > 0) {
const struct device *uart_dev = nullptr;
switch (this->uart_) {
case UART_SELECTION_UART0: // NOLINT(bugprone-branch-clone)
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
break;
case UART_SELECTION_UART1:
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
break;
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
if (device_is_ready(uart_dev)) {
// Nothing can log this early, and the logger has no output without USB anyway
zephyr::usb_device_enable();
}
#endif
break;
#endif
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
if (device_is_ready(uart_dev)) {
// Nothing can log this early, and the logger has no output without USB anyway
zephyr::usb_device_enable();
}
#endif
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
#else
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
#endif
if (device_is_ready(uart_dev)) {
this->uart_dev_ = uart_dev;
#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
@@ -117,7 +111,7 @@ void Logger::pre_setup() {
this->dump_crash_();
#endif
// Nobody reads a USB port this early; cdc_loop_() reports the dump once the port is opened
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
// Without a UART (baud_rate: 0, or one that did not come up) nothing reaches the console
if (this->uart_dev_ != nullptr)
log_crash_to_client();
@@ -139,21 +133,6 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
}
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
return LOG_STR("USB_CDC");
#endif
default:
return LOG_STR("UNKNOWN");
}
}
static constexpr uint8_t REASON_BUF_SIZE = 32;
static const char *reason_to_str(unsigned int reason, char *buf) {
+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)
var = await select.new_select(config, options=levels, fixed_options=True)
await register_parented(var, parent)
await register_component(var, config)
@@ -103,7 +103,9 @@ async def to_code(config: ConfigType) -> None:
)
await cg.register_component(var, config)
await select.register_select(var, config, options=list(options_map.keys()))
await select.register_select(
var, config, options=list(options_map.keys()), fixed_options=True
)
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
cg.add(parent.add_sensor_item(var))
+32 -23
View File
@@ -56,7 +56,8 @@ 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_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
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 uint8_t UART_EVENT_QUEUE_SIZE = 10;
static constexpr uint32_t BAUD_PREF_HASH = 76007670UL;
static constexpr uint32_t PIN_PREF_HASH = 76007671UL;
@@ -670,22 +671,28 @@ 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");
}
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");
}
// 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");
}
// 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_);
@@ -752,7 +759,7 @@ ModemState ModemComponent::handle_state_connecting_() {
return ModemState::MODEM_STATE_ENABLING;
}
}
// ENABLING left the radio at CFUN=4, where the SIM answers but the modem cannot attach yet
// ENABLING powered the SIM up (CFUN=4, or CFUN=1 on a modem without it), so the SIM answers
switch (this->prepare_sim_()) {
case SimResult::SIM_RESULT_READY:
break;
@@ -769,13 +776,14 @@ ModemState ModemComponent::handle_state_connecting_() {
this->target_state_.store(ModemState::MODEM_STATE_DISABLED);
return ModemState::MODEM_STATE_DISCONNECTING;
}
// 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->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;
}
if (this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio on");
@@ -862,11 +870,12 @@ 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. 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) {
// 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) {
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,6 +299,7 @@ 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
+73 -11
View File
@@ -1,3 +1,6 @@
from dataclasses import dataclass, field
import functools
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
@@ -26,7 +29,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
from esphome.types import ConfigType, TemplateArgsType
CODEOWNERS = ["@esphome/core"]
DOMAIN = "select"
@@ -47,6 +50,25 @@ 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)
@@ -105,7 +127,11 @@ 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]):
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)
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
queue_entity_register("select", config)
@@ -113,9 +139,9 @@ async def register_select(var, config, *, options: list[str]):
await setup_select_core_(var, config, options=options)
async def new_select(config, *args, options: list[str]):
async def new_select(config, *args, options: list[str], fixed_options: bool = False):
var = cg.new_Pvariable(config[CONF_ID], *args)
await register_select(var, config, options=options)
await register_select(var, config, options=options, fixed_options=fixed_options)
return var
@@ -131,7 +157,10 @@ 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``."""
return SubEntities(new_select, config, parent, APPLY_ENTITY)
# Hub selects get their options from code generation only
return SubEntities(
functools.partial(new_select, fixed_options=True), config, parent, APPLY_ENTITY
)
@coroutine_with_priority(CoroPriority.CORE)
@@ -146,13 +175,20 @@ OPERATION_BASE_SCHEMA = cv.Schema(
)
automation.register_apply_action(
_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(
"select.set",
OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
),
_SELECT_SET_SCHEMA,
automation.ApplyField(
CONF_OPTION,
"set_option",
@@ -161,6 +197,32 @@ automation.register_apply_action(
),
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)
await select.register_select(var, config, options=options, fixed_options=True)
if CONF_LAMBDA in config:
lambda_ = await cg.process_lambda(
@@ -36,13 +36,8 @@ 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 (!sel_comp->has_option(*val)) {
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
return;
}
if (val.has_value())
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)
var = await select.new_select(config, options=options, fixed_options=True)
if mappings:
cg.add(var.set_select_mappings(mappings))
hub = await cg.add_variable(var.set_tuya_parent, config[CONF_TUYA_ID])
+31 -21
View File
@@ -5,47 +5,57 @@
namespace esphome::wifi {
// 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.
/// 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.
class ExpectedDisconnect {
public:
void arm(const uint8_t *bssid) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->armed_ = true;
}
void clear() { this->armed_ = false; }
void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
void clear() { this->expected_ = ExpectedLeave::EXPECTED_LEAVE_NONE; }
/// 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.
/// 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.
template<typename ReadBssid, typename Disconnect>
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
this->armed_ = false;
this->clear();
std::array<uint8_t, 6> bssid{};
const bool had_ap = read_bssid(bssid.data());
if (!disconnect())
return false;
if (had_ap)
this->arm(bssid.data());
this->arm_(bssid.data(), had_ap ? ExpectedLeave::EXPECTED_LEAVE_AP : ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
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->armed_ && std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
return this->expected_ != ExpectedLeave::EXPECTED_LEAVE_NONE &&
std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
}
/// 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;
/// 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;
}
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_{};
bool armed_{false};
ExpectedLeave expected_{ExpectedLeave::EXPECTED_LEAVE_NONE};
};
} // namespace esphome::wifi
@@ -1158,10 +1158,12 @@ 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 bool expected_disconnect =
s_expected_disconnect.consume_disconnect(it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE);
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;
if (expected_disconnect) {
ESP_LOGD(TAG, "Expected disconnect from previous AP");
ESP_LOGD(TAG, "Expected leave of the previous %s",
expected == ExpectedLeave::EXPECTED_LEAVE_AP ? LOG_STR_LITERAL("AP") : LOG_STR_LITERAL("attempt"));
} 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);
@@ -1181,7 +1183,9 @@ 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
this->notify_disconnect_state_listeners_();
// An attempt that never associated ended no session listeners know of
if (expected != ExpectedLeave::EXPECTED_LEAVE_ATTEMPT)
this->notify_disconnect_state_listeners_();
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
@@ -8,18 +8,16 @@ namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.attribute");
void ZigbeeAttribute::set_attr_() {
void ZigbeeAttribute::set_attr_internal() {
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_(true);
this->report_internal(true);
}
// Check for error
if (state != EZB_ZCL_STATUS_SUCCESS) {
@@ -30,15 +28,12 @@ void ZigbeeAttribute::set_attr_() {
}
}
void ZigbeeAttribute::report_(bool has_lock) {
void ZigbeeAttribute::report_internal(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)) {
@@ -61,43 +56,6 @@ void ZigbeeAttribute::report_(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
@@ -4,9 +4,8 @@
#include <limits>
#include <type_traits>
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "zigbee_entity_base_esp32.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
@@ -29,34 +28,22 @@
namespace esphome::zigbee {
enum ZigbeeReportT {
ZIGBEE_REPORT_DEFAULT,
ZIGBEE_REPORT_COORDINATOR,
ZIGBEE_REPORT_ENABLE,
ZIGBEE_REPORT_FORCE,
};
class ZigbeeAttribute final : public Component {
class ZigbeeAttribute final : public ZigbeeEntityBase {
public:
ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
uint8_t attr_type, float scale, uint8_t max_size)
: zb_(parent),
endpoint_id_(endpoint_id),
: ZigbeeEntityBase(parent, endpoint_id),
cluster_id_(cluster_id),
role_(role),
attr_id_(attr_id),
attr_type_(attr_type),
max_size_(max_size),
scale_(scale) {}
void loop() override;
template<typename T> void add_attr(T value);
template<typename T> void set_attr(const T &value);
uint8_t attr_type() { return attr_type_; }
void set_report(ZigbeeReportT report);
void report();
template<typename F> void add_on_value_callback(F &&callback) { on_value_callback_.add(std::forward<F>(callback)); }
void on_value(ezb_zcl_attribute_t attribute) {
void on_value(ezb_zcl_attribute_t attribute) override {
if (attribute.data.type == this->attr_type() && attribute.data.value) {
this->on_value_callback_.call(attribute);
}
@@ -75,13 +62,10 @@ class ZigbeeAttribute final : public Component {
#ifdef USE_NUMBER
template<typename T> void connect(number::Number *device);
#endif
bool report_enabled = false;
protected:
void set_attr_();
void report_(bool has_lock);
ZigbeeComponent *zb_;
uint8_t endpoint_id_;
void set_attr_internal() override;
void report_internal(bool has_lock) override;
uint16_t cluster_id_;
uint8_t role_;
uint16_t attr_id_;
@@ -89,9 +73,6 @@ class ZigbeeAttribute final : public Component {
uint8_t max_size_;
float scale_;
void *value_p_{nullptr};
bool set_attr_requested_{false};
bool report_requested_{false};
bool force_report_{false};
LazyCallbackManager<void(ezb_zcl_attribute_t attribute)> on_value_callback_{};
template<typename T> T scale_value_(float value);
template<typename T> T invalid_value_();
@@ -0,0 +1,51 @@
#include "zigbee_entity_base_esp32.h"
#include "esphome/core/log.h"
#include "esphome/core/defines.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.entity_base");
void ZigbeeEntityBase::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 ZigbeeEntityBase::report() {
if (!this->report_enabled) {
return;
}
this->report_requested_ = true;
this->enable_loop();
}
void ZigbeeEntityBase::loop() {
if (this->set_attr_requested_) {
this->set_attr_internal();
}
if (this->report_requested_ && this->zb_->is_joined()) {
this->report_internal(false);
}
if ((!this->report_requested_ || !this->zb_->is_joined()) && !this->set_attr_requested_) {
this->disable_loop();
}
}
} // namespace esphome::zigbee
#endif
#endif
@@ -0,0 +1,47 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
#include "esp_zigbee.h"
#include "zigbee_esp32.h"
namespace esphome::zigbee {
enum ZigbeeReportT {
ZIGBEE_REPORT_DEFAULT,
ZIGBEE_REPORT_COORDINATOR,
ZIGBEE_REPORT_ENABLE,
ZIGBEE_REPORT_FORCE,
};
class ZigbeeEntityBase : public Component, public ZigbeeAttributeInterface {
public:
void loop() override;
void set_report(ZigbeeReportT report);
void report() override;
bool report_enabled = false;
protected:
ZigbeeEntityBase(ZigbeeComponent *parent, uint8_t endpoint_id) : zb_(parent), endpoint_id_(endpoint_id) {}
/// Handle pending attribute writes and clear set_attr_requested_ when complete; leave it set to retry.
virtual void set_attr_internal() = 0;
/// Handle pending reports and clear report_requested_ when complete; leave it set to retry.
/// Acquire/release the Zigbee lock only when has_lock is false; otherwise the caller owns it.
virtual void report_internal(bool has_lock) = 0;
ZigbeeComponent *zb_;
uint8_t endpoint_id_;
bool set_attr_requested_{false};
bool report_requested_{false};
bool force_report_{false};
};
} // namespace esphome::zigbee
#endif
#endif
+12 -8
View File
@@ -31,7 +31,11 @@ static constexpr uint8_t MAX_ZB_QUEUE_SIZE = 32;
uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
class ZigbeeAttribute;
class ZigbeeAttributeInterface {
public:
virtual void on_value(ezb_zcl_attribute_t attribute) = 0;
virtual void report() = 0;
};
class ZigbeeComponent final : public Component {
public:
@@ -48,8 +52,8 @@ class ZigbeeComponent final : public Component {
bool register_device();
template<typename T>
void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
uint8_t max_size, T value);
void add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, uint8_t max_size, T value);
template<typename T>
void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value);
@@ -94,13 +98,13 @@ class ZigbeeComponent final : public Component {
void end_parent_link_failures_(uint8_t seq);
void reset_parent_link_failures_();
template<typename T>
void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
T *value_p);
void add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, T *value_p);
void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
// attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
// automations
// key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttribute *> attributes_;
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttributeInterface *> attributes_;
ezb_af_device_desc_t dev_desc_ = ezb_af_create_device_desc();
CallbackManager<void(bool)> join_cb_{};
template<typename... Args> friend void enqueue_zb_event(Args... args);
@@ -133,7 +137,7 @@ void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t
}
template<typename T>
void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
void ZigbeeComponent::add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, uint8_t max_size, T value) {
// The size byte of the zcl_str must be set to the maximum value,
// even though the initial string may be shorter.
@@ -151,7 +155,7 @@ void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint1
}
template<typename T>
void ZigbeeComponent::add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
void ZigbeeComponent::add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, T *value_p) {
ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
if (ep_desc == NULL) {
+5 -6
View File
@@ -1497,12 +1497,11 @@ class BindKeyValidator(SensitiveValidator):
if any(len(part) != 2 for part in parts):
raise Invalid(f"{self._name} must be format XX")
for part in parts:
try:
parts_int.append(int(part, 16))
except ValueError:
raise Invalid(
f"{self._name} must be hex values from 00 to FF"
) from None
# int() also accepts surrounding whitespace and a +/- sign, which
# would silently validate a key different from the one typed
if not re.fullmatch(r"[0-9A-Fa-f]{2}", part):
raise Invalid(f"{self._name} must be hex values from 00 to FF")
parts_int.append(int(part, 16))
return "".join(f"{part:02X}" for part in parts_int)
+25 -3
View File
@@ -153,6 +153,7 @@
#define USE_LINE_READER
#define USE_LOCK
#define USE_LOGGER
#define USE_LOGGER_CONSOLE
#define USE_LOGGER_LEVEL_LISTENERS
#define USE_LOGGER_RUNTIME_TAG_LEVELS
#define USE_LVGL
@@ -767,16 +768,26 @@
#if defined(USE_ESP32_VARIANT_ESP32S2)
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
#elif defined(USE_ESP32_VARIANT_ESP32H21)
#define USE_LOGGER_USB_SERIAL_JTAG
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || \
defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32H2) || \
defined(USE_ESP32_VARIANT_ESP32H21) || defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || \
defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32S31)
defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S3) || \
defined(USE_ESP32_VARIANT_ESP32S31)
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define USE_LOGGER_USB_SERIAL_JTAG
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
#else
#define USE_LOGGER_UART_SELECTION_UART0
#define ESPHOME_LOGGER_UART_NAME "UART0"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
#endif
#endif
@@ -790,6 +801,9 @@
#define USE_WIFI_SCAN_RESULTS_LOCK
#define USE_ESP8266_LOGGER_SERIAL
#define USE_ESP8266_LOGGER_SERIAL1
#define USE_LOGGER_UART_SELECTION_UART0
#define ESPHOME_LOGGER_UART_NAME "UART0"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
#define USE_ESP8266_PREFERENCES_FLASH
#define ESP8266_PREFERENCES_FLASH_WORDS 128
#define ESP8266_PREFERENCES_RTC_EBOOT_WORDS 32
@@ -835,6 +849,8 @@
#define USE_I2C
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
#define USE_SOCKET_IMPL_LWIP_TCP
#define USE_RP2040_BLE
#define USE_RP2_BLE_TRACKER
@@ -865,6 +881,9 @@
#if defined(USE_BK72XX) || defined(USE_RTL87XX) || defined(USE_LN882X)
#define USE_LIBRETINY_CRASH_HANDLER
#endif
#define USE_LOGGER_UART_SELECTION_DEFAULT
#define ESPHOME_LOGGER_UART_NAME "DEFAULT"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_DEFAULT
#ifdef USE_BK72XX
#define USE_REMOTE_RECEIVER_TIMER_SAMPLING
#endif
@@ -919,8 +938,11 @@
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
#define USE_LOGGER_EARLY_MESSAGE
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_WAIT_FOR_CDC
#define USE_LOGGER_ZEPHYR_CDC_LOOP
#define USE_NRF52_DFU
#define USE_NRF52_REG0_VOUT 5
#define USE_NRF52_UICR_ERASE
+36
View File
@@ -1472,6 +1472,42 @@ __attribute__((format(printf, 4, 5))) inline size_t buf_append_printf(char *buf,
}
#endif
/// Copy a PROGMEM string to `dst`, stopping at its null terminator or at `end`, and return the new end of `dst`.
/// No terminator is written. Flash only allows aligned 32-bit loads on ESP8266, so `src` is read a whole word at a
/// time, including the bytes after its null in the last word; `src` must be readable up to the end of that word.
inline char *ESPHOME_ALWAYS_INLINE progmem_copy_str(char *dst, const char *end, const char *src) {
const uint32_t offset = reinterpret_cast<uintptr_t>(src) & 3;
const uint32_t *word = reinterpret_cast<const uint32_t *>(src - offset);
uint32_t bytes = *word++;
// Enters the loop at the first byte of src; after that each word runs its four steps without another dispatch, and
// one step per byte keeps each a single extract
switch (offset) {
for (;;) {
bytes = *word++;
[[fallthrough]];
case 0:
if (dst >= end || static_cast<char>(bytes) == '\0')
return dst;
*dst++ = static_cast<char>(bytes);
[[fallthrough]];
case 1:
if (dst >= end || static_cast<char>(bytes >> 8) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 8);
[[fallthrough]];
case 2:
if (dst >= end || static_cast<char>(bytes >> 16) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 16);
[[fallthrough]];
default:
if (dst >= end || static_cast<char>(bytes >> 24) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 24);
}
}
}
#ifdef USE_ESP8266
/// Safely append a PROGMEM string to buffer, returning new position (capped at size).
/// ESP8266 internal implementation — prefer the `buf_append_str` macro which wraps
+28
View File
@@ -48,6 +48,34 @@ void HOT esp_log_printf_(int level, const char *tag, int line, const __FlashStri
}
#endif
void HOT esp_log_str_(int level, const char *tag, int line, const char *format) { // NOLINT
#ifdef USE_LOGGER
#ifdef ESPHOME_DEBUG
// Before Logger::pre_setup(), the formatting path below reports the early call
if (logger::global_logger != nullptr)
#endif
{
if (logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format))
return;
}
// Formats the line log_str_() could not copy; it has no '%', so it is its own format
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
esp_log_printf_(level, tag, line, format);
#pragma GCC diagnostic pop
#endif
}
#ifdef USE_ESP8266
void HOT esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format) {
#ifdef USE_LOGGER
ESPHOME_DEBUG_ASSERT(logger::global_logger != nullptr);
logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format);
#endif
}
#endif
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
#ifdef USE_LOGGER
#ifdef ESPHOME_DEBUG
+57 -14
View File
@@ -67,6 +67,14 @@ void esp_log_printf_(int level, const char *tag, int line, const char *format, .
// NOLINTNEXTLINE(readability-identifier-naming)
void esp_log_printf_(int level, const char *tag, int line, const __FlashStringHelper *format, ...);
#endif
// esp_log_printf_ for a line with no arguments and no '%': it is copied without formatting
void esp_log_str_(int level, const char *tag, int line, const char *format); // NOLINT
#ifdef USE_ESP8266
// NOLINTNEXTLINE(readability-identifier-naming)
void esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format);
#endif
// Never defined or called: the ESP_LOGx macros name it in dead code so a line without arguments has its format checked
int esp_log_format_check_(const char *format, ...) __attribute__((format(printf, 1, 2))); // NOLINT
void esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args); // NOLINT
#if defined(USE_ESP32)
int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
@@ -78,9 +86,50 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#define ESPHOME_LOG_FORMAT(format) format
#endif
#define ESPHOME_LOG_ARGS_(level, tag, format, ...) \
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format), __VA_ARGS__)
// The format check is a statement-level if (false), not sizeof: GCC 16 skips format checks in unevaluated or folded
// expressions. __builtin_strchr folds on a literal, so only a line with '%' (such as "100%%") is formatted.
#ifdef USE_ESP8266
// F() only takes a literal, so using format more than once cannot repeat a side effect
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
({ \
if (false) \
::esphome::esp_log_format_check_(format); \
if (__builtin_strchr(format, '%') != nullptr) \
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
else \
::esphome::esp_log_str_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
})
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
// A function argument, so format is evaluated once and a temporary such as std::string().c_str() outlives the call
// NOLINTNEXTLINE(readability-identifier-naming)
inline void __attribute__((always_inline)) esp_log_no_args_(int level, const char *tag, int line, const char *format) {
if (__builtin_strchr(format, '%') != nullptr) {
esp_log_printf_(level, tag, line, format);
} else {
esp_log_str_(level, tag, line, format);
}
}
#pragma GCC diagnostic pop
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
({ \
if (false) \
::esphome::esp_log_format_check_(format); \
::esphome::esp_log_no_args_(level, tag, __LINE__, format); \
})
#endif
// ESPHOME_LOG_NO_ARGS_ when there are no arguments after the format, ESPHOME_LOG_ARGS_ otherwise
#define ESPHOME_LOG_PICK_(skip, name, ...) name
#define ESPHOME_LOG_(level, tag, format, ...) \
ESPHOME_LOG_PICK_(__VA_OPT__(, ) ESPHOME_LOG_ARGS_, ESPHOME_LOG_NO_ARGS_)(level, tag, \
format __VA_OPT__(, ) __VA_ARGS__)
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
#define esph_log_vv(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_vv(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_VERY_VERBOSE
#else
@@ -88,8 +137,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
#define esph_log_v(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_v(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERBOSE, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_VERBOSE
#else
@@ -97,10 +145,8 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
#define esph_log_d(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_config(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_d(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_DEBUG, tag, format, __VA_ARGS__)
#define esph_log_config(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_CONFIG, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_DEBUG
#define ESPHOME_LOG_HAS_CONFIG
@@ -110,8 +156,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_INFO
#define esph_log_i(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_i(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_INFO, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_INFO
#else
@@ -119,8 +164,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
#define esph_log_w(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_WARN, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_w(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_WARN, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_WARN
#else
@@ -128,8 +172,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
#define esph_log_e(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_ERROR, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_e(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_ERROR, tag, format, __VA_ARGS__)
#define ESPHOME_LOG_HAS_ERROR
#else
+53 -44
View File
@@ -68,38 +68,6 @@ def test_logger_pre_setup_before_other_components(generate_main):
)
def test_default_uart_selection_is_not_emitted(generate_main):
"""UART0 is the C++ initializer on ESP8266, so the setter is skipped."""
main_cpp = generate_main("tests/component_tests/logger/test_logger.yaml")
assert "set_uart_selection(" not in main_cpp
def test_custom_uart_selection_is_emitted(generate_main):
"""A non default UART still reaches the setter before pre_setup()."""
main_cpp = generate_main("tests/component_tests/logger/test_logger_uart1.yaml")
assert "set_uart_selection(logger::UART_SELECTION_UART1);" in main_cpp
def test_libretiny_default_uart_selection_is_not_emitted(generate_main):
"""DEFAULT is the C++ initializer on LibreTiny, so the setter is skipped."""
main_cpp = generate_main(
"tests/component_tests/logger/test_logger_libretiny_default.yaml"
)
assert "set_uart_selection(" not in main_cpp
def test_libretiny_uart0_is_emitted(generate_main):
"""UART0 is not the LibreTiny initializer, so it must still be set."""
main_cpp = generate_main(
"tests/component_tests/logger/test_logger_libretiny_uart0.yaml"
)
assert "set_uart_selection(logger::UART_SELECTION_UART0);" in main_cpp
def test_ram_log_strings_is_deprecated(
generate_main: Callable[[str | Path], str],
caplog: pytest.LogCaptureFixture,
@@ -168,35 +136,76 @@ def test_log_v2_only_on_native_toolchain(
@pytest.mark.parametrize(
("config", "expected"),
("config", "port", "zephyr_cdc_loop"),
[
pytest.param("test_logger_rp2_usb_cdc.yaml", {"USB_CDC"}, id="rp2_usb_cdc"),
pytest.param("test_logger_rp2_uart0.yaml", set(), id="rp2_uart0"),
pytest.param("test_logger_esp32_usb_cdc.yaml", {"USB_CDC"}, id="esp32_usb_cdc"),
pytest.param("test_logger.yaml", "UART0", False, id="esp8266_default"),
pytest.param("test_logger_uart1.yaml", "UART1", False, id="esp8266_uart1"),
pytest.param(
"test_logger_uart0_swap.yaml", "UART0_SWAP", False, id="esp8266_uart0_swap"
),
pytest.param(
"test_logger_libretiny_default.yaml",
"DEFAULT",
False,
id="libretiny_default",
),
pytest.param(
"test_logger_libretiny_uart0.yaml", "UART0", False, id="libretiny_uart0"
),
pytest.param(
"test_logger_rp2_usb_cdc.yaml", "USB_CDC", False, id="rp2_usb_cdc"
),
pytest.param("test_logger_rp2_uart0.yaml", "UART0", False, id="rp2_uart0"),
pytest.param(
"test_logger_esp32_usb_cdc.yaml", "USB_CDC", False, id="esp32_usb_cdc"
),
pytest.param(
"test_logger_esp32_usb_serial_jtag.yaml",
{"USB_SERIAL_JTAG"},
"USB_SERIAL_JTAG",
False,
id="esp32_usb_serial_jtag",
),
pytest.param("test_logger_nrf52_usb_cdc.yaml", {"USB_CDC"}, id="nrf52_usb_cdc"),
pytest.param(
"test_logger_nrf52_usb_cdc.yaml", "USB_CDC", True, id="nrf52_usb_cdc"
),
# Zephyr polls the USB port only when it logs there
pytest.param(
"test_logger_nrf52_usb_cdc_no_serial.yaml", set(), id="nrf52_no_serial"
"test_logger_nrf52_usb_cdc_no_serial.yaml",
"USB_CDC",
False,
id="nrf52_no_serial",
),
],
)
def test_uart_selection_defines(
generate_main: Callable[[str | Path], str],
config: str,
expected: set[str],
port: str,
zephyr_cdc_loop: bool,
) -> None:
"""USB ports get a selection define, which gates their port code and crash replay."""
"""The chosen port is fixed at build time: one flag, its enum value and its dump_config name."""
generate_main(f"tests/component_tests/logger/{config}")
defines = {d.name: d.value for d in CORE.defines}
prefix = "USE_LOGGER_UART_SELECTION_"
assert {
d.name.removeprefix(prefix) for d in CORE.defines if d.name.startswith(prefix)
} == expected
assert {d.removeprefix(prefix) for d in defines if d.startswith(prefix)} == {port}
assert (
str(defines["ESPHOME_LOGGER_UART_SELECTION"])
== f"logger::UART_SELECTION_{port}"
)
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == f'"{port}"'
assert ("USE_LOGGER_ZEPHYR_CDC_LOOP" in defines) is zephyr_cdc_loop
def test_esp8266_without_serial_dumps_no_port(
generate_main: Callable[[str | Path], str],
) -> None:
"""ESP8266 without serial logging keeps UART0 selected but never opens it."""
generate_main("tests/component_tests/logger/test_logger_no_serial.yaml")
defines = {d.name: d.value for d in CORE.defines}
assert "USE_LOGGER_UART_SELECTION_UART0" in defines
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == '"NONE"'
def test_log_listener_slots_count_every_listener(
@@ -0,0 +1,9 @@
---
esphome:
name: test
esp8266:
board: d1_mini_lite
logger:
baud_rate: 0
@@ -0,0 +1,9 @@
---
esphome:
name: test
esp8266:
board: d1_mini_lite
logger:
hardware_uart: UART0_SWAP
@@ -0,0 +1,39 @@
esphome:
name: test
esp8266:
board: esp01_1m
button:
# Declared before the selects, so the action is generated before them
- platform: template
id: early
name: Early
on_press:
- select.set:
id: fan
option: high
- select.set:
id: fan
option: turbo
- select.set:
id: fan
option: !lambda return "low";
- select.set:
id: fan_copy
option: medium
select:
- platform: template
id: fan
name: Fan
optimistic: true
options: [low, medium, high]
on_value:
- select.set:
id: fan
option: medium
- platform: copy
id: fan_copy
name: Fan Copy
source_id: fan
Loaded 100 of 110 files, more files were not shown because too many files have changed in this diff. Show more