Commit Graph
27922 Commits
Author SHA1 Message Date
J. Nick Koston ae41427523 [mdns] Enforce network-interface availability at config time on ESP8266/RP2040
Addresses Copilot review feedback: if someone enables mdns on ESP8266 or RP2040
without wifi (or ethernet on RP2040), the listener-based setup() is a no-op and
the user sees a silent failure rather than a helpful error.

FINAL_VALIDATE_SCHEMA rejects mdns on these platforms when no compatible
network component is present, naming the specific options that would satisfy
the requirement. The existing DEPENDENCIES = ["network"] covers most
misconfigurations indirectly, but an explicit network: alone (without wifi or
ethernet) slips past that check — now it fails with a clear message.
2026-04-23 20:11:48 -05:00
J. Nick Koston 6938f2b400 [mdns] Gate listener code under USE_MDNS_EVENT_DRIVEN_POLLING for clang-tidy
clang-tidy compiles mdns_esp8266.cpp / mdns_rp2040.cpp with only the tidy env's
raw build flags, without the Python codegen defines (USE_WIFI_IP_STATE_LISTENERS,
USE_ETHERNET_IP_STATE_LISTENERS, USE_MDNS_EVENT_DRIVEN_POLLING). Wrap all
listener-specific code paths under USE_MDNS_EVENT_DRIVEN_POLLING so tidy sees a
compilable translation unit with an empty setup() instead of unresolved members
on WiFiComponent / MDNSComponent.

Production builds always have the listener defines via the mdns Python
to_code()'s wifi.request_wifi_ip_state_listener() /
ethernet.request_ethernet_ip_state_listener() calls, so this is tidy-only dead
code at runtime.
2026-04-23 20:06:14 -05:00
J. Nick Koston 09fe59c5cc [mdns] Drop fallback paths, collapse everything under USE_MDNS_EVENT_DRIVEN_POLLING
mDNS on ESP8266/RP2040 always runs over a network interface (WiFi on ESP8266;
WiFi or W5500/etc. ethernet on RP2040), and every such interface already
publishes an IP state listener in tree. USE_MDNS_EVENT_DRIVEN_POLLING is
therefore always defined in production, so the fallback set_interval() paths
in both platform files and the was_connected_ bookkeeping are dead code.

Also drop the ethernet-specific test — the existing wifi and ethernet+mdns
combos are already covered by the mdns test fixtures paired with their network
component tests.

Trim redundant comments throughout: the header now documents the ~9s
probe/announce window and why update() can stop afterward in a few lines
instead of a full essay; platform files keep only the non-obvious bits (why
RP2040 needs to drive begin()/notifyAPChange() itself, why re-arming on any
listener notification is correct).
2026-04-23 19:57:48 -05:00
J. Nick Koston ceada86325 [mdns] Drive event-driven polling from Ethernet IP state events on RP2040
Extends the WiFi-only listener pattern from the previous commit to also subscribe
to EthernetIPStateListener when Ethernet is configured. RP2040 can run mDNS over a
W5500 ethernet shield without WiFi, and mDNS and WiFi are mutually exclusive on
RP2040 (the framework doesn't support both simultaneously on the CYW43/PIO paths),
so this adds the ethernet-only path without touching the WiFi path.

ESPHome's wifi and ethernet components already publish compatible IP state listener
APIs (`WiFiIPStateListener::on_ip_state` and `EthernetIPStateListener::on_ip_state`
with identical signatures). MDNSComponent multiply-inherits both when available; a
single on_ip_state() override satisfies both vtable entries.

- New `USE_MDNS_WIFI_LISTENER` / `USE_MDNS_ETHERNET_LISTENER` gates control per-
  interface subscription. `USE_MDNS_EVENT_DRIVEN_POLLING` fires if either is
  available.
- Python side now calls `ethernet.request_ethernet_ip_state_listener()` when
  ethernet is in the config (RP2040 only — ESP8266 has no ethernet driver).
- setup() seeds current state for each registered listener so an already-up
  interface still triggers MDNS.begin() + polling window under AFTER_CONNECTION
  priority.

Tests: adds `test-enabled-ethernet.rp2040-ard.yaml` covering the ethernet-only
path. Existing `test-enabled.rp2040-ard.yaml` (WiFi-only) and ESP8266 tests
continue to pass.
2026-04-23 19:41:23 -05:00
J. Nick Koston 5cb258034b [mdns] Fall back to legacy polling when WiFi IP state listener isn't available
clang-tidy CI compiles the source with the esp8266-arduino-tidy env's raw build
flags (-DUSE_ESP8266 only) without running the Python codegen that adds
USE_WIFI_IP_STATE_LISTENERS. The previous guard assumed USE_WIFI_IP_STATE_LISTENERS
would always be defined on ESP8266, so clang-tidy failed with 'no member named
add_ip_state_listener in wifi::WiFiComponent'.

Gate USE_MDNS_EVENT_DRIVEN_POLLING on USE_WIFI + USE_WIFI_IP_STATE_LISTENERS for
both ESP8266 and RP2040. When either is absent, fall back to the pre-PR behaviour:
set_interval(MDNS_UPDATE_INTERVAL_MS, MDNS.update) running forever. Python side
already only requests the listener slot when WiFi is in the config, so real
production builds on ESP8266 (which always have WiFi) continue to use the
event-driven path — only the clang-tidy static-analysis build takes the fallback.
2026-04-23 19:36:11 -05:00
J. Nick Koston 25601434d9 [mdns] Simplify listener logic: always re-arm on IP notify, drop transition tracking
ESPHome's WiFiIPStateListener only notifies on IP acquisition (GOT_IP events), not
on IP loss — on disconnect, only the WiFiConnectStateListener's disconnect path
fires (see wifi_component_esp8266.cpp:952-962 and wifi_component_pico_w.cpp:340).

The previous commit's `ip_was_up_` transition tracking was broken: after the first
IP-up event, `ip_was_up_` latched to true and never reset, so subsequent
disconnect+reconnect cycles would see has_ip=true && ip_was_up_=true and skip
re-arming the polling window.

Fix: always re-arm on any IP notification. The scheduler's set_interval/set_timeout
with a uint32_t ID already performs atomic cancel-and-add for matching IDs
(Scheduler::set_timer_common_ line 232-234), so start_polling_window_ is idempotent
and needs no explicit cancel. Drop the ip_was_up_ field and cancel_polling_window_
helper entirely.

The !has_ip branch (cancel on disconnect) was dead code: it would never fire because
the listener doesn't receive disconnect events. Removing it; the polling window will
naturally expire on its own (at most 12s of harmless MDNS.update() calls during a
disconnect that isn't followed by reconnect within the window).
2026-04-23 19:26:01 -05:00
J. Nick Koston bf7083c501 [mdns] Drive MDNS.update() polling from WiFi IP state events on ESP8266/RP2040
The Arduino LEAmDNS library only has meaningful timer-driven work during the
~9 s probe+announce phase following MDNS.begin() or _restart(): 3 probes at
250 ms + 8 announcements at 1000 ms, then all internal timeouts are set to
resetToNeverExpires(). Incoming packets are handled via the lwIP UDP RX
callback independently of update(). ESPHome does not issue service queries,
so the query cache path is always a no-op.

The previous implementation ran set_interval(50) forever — ~20 dispatches/sec,
1200+ scheduler calls per minute of pure overhead once probing completed.

This PR arms a bounded MDNS_POLL_WINDOW_MS (12 s) polling window driven by
WiFiIPStateListener events. A fresh window covers each probe/announce cycle
(boot, wifi reconnect, or internal _restart() triggered by netif changes);
outside the window there are zero scheduler dispatches and the scheduler
heap contains no mDNS items.

ESP8266 is WiFi-only in the Arduino build so the path is unconditional.
RP2040 supports W5500 ethernet without WiFi, so the listener is requested
only when WiFi is in the config; ethernet-only RP2040 builds keep the
legacy polling loop.

Scheduler IDs use uint32_t (MDNS_POLL_ID / MDNS_POLL_STOP_ID) to avoid the
name-hash/strcmp cost of string-named timers on the cancel + re-arm paths.
2026-04-23 19:19:29 -05:00
J. Nick Koston 64592c0a4a Revert "revert time"
This reverts commit 7bb3d7e4a0.
2026-04-23 18:56:22 -05:00
J. Nick Koston 7bb3d7e4a0 revert time 2026-04-23 18:51:47 -05:00
J. Nick Koston cc8b8242fa Merge remote-tracking branch 'upstream/fast-millis-esp8266' into integration 2026-04-23 18:41:36 -05:00
J. Nick Koston d632e00e7d Trim overflow comment 2026-04-23 18:41:21 -05:00
J. Nick Koston 83b76f616b Address review: fix misleading static_assert, drop %= on LX106 2026-04-23 18:40:28 -05:00
J. Nick Koston d1af72e623 Merge remote-tracking branch 'upstream/fast-millis-esp8266' into integration 2026-04-23 18:34:27 -05:00
J. Nick Koston a82b00ba12 Merge branch 'dev' into fast-millis-esp8266 2026-04-23 18:34:11 -05:00
Kevin AhrendtandCopilot ddf1426f86 [sendspin] Add initial Sendspin hub component (PR1) (#15924)
Co-authored-by: Copilot <copilot@github.com>
2026-04-23 22:09:36 +00:00
J. Nick Koston c27977389f Merge remote-tracking branch 'upstream/dev' into integration 2026-04-23 16:54:55 -05:00
J. Nick Koston 90d7bfe02e [ci] Auto-close PRs opened from a fork's default branch (#15957) 2026-04-23 16:36:32 -05:00
Kevin AhrendtandCopilot d759f1a567 [audio_http] Add a media source for playing audio from HTTP URLs (#15741)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-23 15:53:52 -05:00
J. Nick Koston 835b9a55a0 Merge remote-tracking branch 'upstream/dev' into integration 2026-04-23 14:53:33 -05:00
f757cd1210 [zigbee][core] Add support for Zigbee binary sensors on ESP32 H2 and C6 (#11553)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-23 12:46:56 -04:00
Paulus Schoutsen 9b45b046a8 [core] Allow finding all devices as target that match mac suffix (#13135) 2026-04-23 08:43:32 -05:00
J. Nick Koston 70ae614abd [api] Fall back to plaintext for logger connections (#15938) 2026-04-23 08:23:38 -05:00
J. Nick Koston 8f9b91eece [wifi] Avoid BDK 3.0.78 wifi_event_sta_disconnected_t collision on BK72xx (#15942) 2026-04-23 08:22:17 -05:00
J. Nick Koston 3ca86fc3fc [core] Raise WDT_FEED_INTERVAL_MS to 2000ms on BK72xx (#15943) 2026-04-23 08:21:46 -05:00
J. Nick Koston b38db617a2 [core] Clean up stale includes and inline yield_with_select_ in application (#15945) 2026-04-23 08:21:05 -05:00
J. Nick Koston 13fe881f70 [scheduler][core] Lock-free fast-path on ESPHOME_THREAD_MULTI_NO_ATOMICS via __atomic builtins (#15947) 2026-04-23 08:20:31 -05:00
J. Nick Koston 50c181671c [ci] Better explain too-big bot review message (#15939) 2026-04-23 06:47:16 -05:00
J. Nick Koston c6d888e4d2 [scheduler][core] Address review: use preloaded major
In time_64.cpp's true-rollover branch, bump the just-loaded `major`
local first and __atomic_store_n that value to millis_major, instead
of reading the global again for the store expression. Equivalent
under the held lock; clearer and avoids a second read.

scheduler.h indent: the NO_ATOMICS #else branch bodies read at 2
spaces while the sibling #ifdef/#elif branches read at 4. clang-format
refuses to normalise these consistently — every manual re-indent to 4
spaces gets reverted by the hook. Leaving as clang-format produces it.
2026-04-23 06:34:40 -05:00
J. Nick Koston 3bfde7249a Merge branch 'scheduler-volatile-counters' into integration 2026-04-23 06:28:42 -05:00
J. Nick Koston 6d1d924ff9 [core] time_64 NO_ATOMICS: atomic re-reads + reload major under lock
Two fixes Copilot flagged on #15947:

1. Use __atomic_load_n to re-read last_millis under the lock, not a
   plain read. The forward-progression branch (else if) writes
   last_millis with __atomic_store_n without holding lock, so the
   under-lock plain read would race with it and be UB in the C++
   memory model.

2. Reload major from millis_major after acquiring the lock. The
   unlocked load at the top of the function can be stale by the time
   we get the lock: another thread may have completed a rollover
   between the unlocked load and the lock acquisition, leaving our
   local major behind by one. Without reload the function could
   return a 64-bit timestamp that jumps backwards by ~2^32 ms (~49.7
   days). The MULTI_ATOMICS branch already handles this via its retry
   loop; NO_ATOMICS just reloads under the lock.
2026-04-23 06:28:21 -05:00
J. Nick Koston 6ded06eff0 [core] time_64 NO_ATOMICS: atomic re-reads + reload major under lock
Two fixes Copilot flagged on #15947:

1. Use __atomic_load_n to re-read last_millis under the lock, not a
   plain read. The forward-progression branch (else if) writes
   last_millis with __atomic_store_n without holding lock, so the
   under-lock plain read would race with it and be UB in the C++
   memory model.

2. Reload major from millis_major after acquiring the lock. The
   unlocked load at the top of the function can be stale by the time
   we get the lock: another thread may have completed a rollover
   between the unlocked load and the lock acquisition, leaving our
   local major behind by one. Without reload the function could
   return a 64-bit timestamp that jumps backwards by ~2^32 ms (~49.7
   days). The MULTI_ATOMICS branch already handles this via its retry
   loop; NO_ATOMICS just reloads under the lock.
2026-04-23 06:28:01 -05:00
J. Nick Koston 8a7cd3683e Merge remote-tracking branch 'origin/scheduler-volatile-counters' into integration 2026-04-23 06:23:43 -05:00
J. Nick Koston 867fae3bb8 [scheduler,core] Make NO_ATOMICS writer paths fully memory-model-clean
Switch writer-side plain stores under lock_ to __atomic_store_n with
__ATOMIC_RELAXED on NO_ATOMICS. The input value for RMW is read
plainly (safe — only writers mutate, serialised by the lock; readers
only atomic-load so two reads don't race). Closes the formal C++
memory-model hole where plain-store vs atomic-load was a data race
in the standard even though aligned 32-bit STR/LDR on ARMv5TE is
atomic in practice.

Applies to scheduler.h counter mutators and the under-lock writes
to last_millis / millis_major in time_64.cpp's near-rollover branch.
Same ARMv5TE codegen (plain STR). ATOMICS / SINGLE paths unchanged.
2026-04-23 06:19:28 -05:00
J. Nick Koston 98e88ac02c [scheduler] Normalise to_remove_empty_/to_remove_count_ preprocessor form
Use `#if defined(X)` / `#elif defined(Y)` / `#else` for the three-way
ATOMICS / NO_ATOMICS / SINGLE split. Also fix the SINGLE-branch body
indentation to match the other branches.
2026-04-23 06:14:48 -05:00
J. Nick Koston b3f93a4da7 [core] Restore original Millis64 NO_ATOMICS comments
The preceding commit needlessly rewrote comments that were still
accurate. Revert the prose-only changes; keep only the two line-level
code changes (__atomic_load_n on the unlocked reads, __atomic_store_n
on the unlocked write).
2026-04-23 06:12:10 -05:00
J. Nick Koston 3c2396ab86 [core] Apply __atomic_load_n/store_n pattern to Millis64 NO_ATOMICS path
Same treatment as the scheduler counters (#15947):

  - Unlocked reads of millis_major / last_millis at the top of
    Millis64Impl::compute(): switch from plain reads to
    __atomic_load_n(&..., __ATOMIC_RELAXED).
  - Unlocked write of last_millis in the "normal forward progression"
    branch: switch from plain assignment to __atomic_store_n(...,
    __ATOMIC_RELAXED). This is the one write that happens without the
    lock, so it needs to be formally atomic to pair cleanly with the
    unlocked atomic reader in the C++ memory model.
  - Writes under `lock` stay plain (millis_major++, last_millis = now
    inside the near-rollover branch). The lock serialises them against
    other writers.

On ARMv5TE the builtins compile to plain LDR/STR — same codegen, no
libatomic dependency. Updates the "accepting minor races" comment to
describe the formally-defined version of the race.
2026-04-23 06:11:03 -05:00
J. Nick Koston 202bcf5b10 [scheduler] Use __atomic_load_n only on reader fast-path
Walk back the __atomic_store_n on the writer paths — the mutators
already hold lock_, so plain counter_++/=/+=/-- is sufficient to
serialise against other writers. The reader fast-path still uses
__atomic_load_n(&counter, __ATOMIC_RELAXED) to express concurrent-
read intent in the C++ memory model and keep the compiler from
caching/eliding the read. On ARMv5TE it compiles to a plain LDR —
same codegen as before.
2026-04-23 06:07:51 -05:00
J. Nick Koston 22ed9b3c1e [scheduler] Replace volatile with __atomic_{load,store}_n on NO_ATOMICS
Copilot review on #15947 flagged that `volatile uint32_t` is not a
well-defined concurrent access in the C++ memory model — it prevents
the compiler caching/eliding the read, but does not turn a plain
cross-thread read/write pair into a defined access. Technically still
a formal data race even though aligned 32-bit LDR/STR on ARMv5TE is
atomic at the hardware level.

Switch the NO_ATOMICS counter reads and writes to GCC's atomic
builtins with __ATOMIC_RELAXED:

  - Readers: __atomic_load_n(&counter, __ATOMIC_RELAXED)
  - Writers (under lock_): __atomic_store_n(&counter, new_value,
                                             __ATOMIC_RELAXED)
  - Increment/decrement (under lock_): explicit load + compute +
    __atomic_store_n. Lock_ serialises the load-modify-store against
    other writers; the atomic ops make the write visible to concurrent
    readers in the memory model.

On ARMv5TE these builtins compile to plain LDR/STR — same codegen as
the previous volatile approach, and no libatomic dependency (only RMW
builtins like __atomic_fetch_add would need the lib). ATOMICS and
SINGLE paths are unchanged.
2026-04-23 06:06:02 -05:00
J. Nick Koston 89fca4f446 merge 2026-04-23 06:01:08 -05:00
J. Nick Koston 2a0f2abd6b Merge remote-tracking branch 'origin/core-application-stale-includes' into integration 2026-04-23 05:57:27 -05:00
J. Nick Koston 64ca25ec84 Merge remote-tracking branch 'origin/scheduler-volatile-counters' into integration 2026-04-23 05:57:17 -05:00
J. Nick Koston 4ec2e42d4d [scheduler] Rename counter mutators with _locked_ suffix
Rename the seven counter RMW mutators to carry the `_locked_` suffix
that matches the existing convention (pop_raw_locked_,
is_item_removed_locked_, cancel_item_locked_, etc.):

  to_add_count_increment_   -> to_add_count_increment_locked_
  to_add_count_clear_       -> to_add_count_clear_locked_
  defer_count_increment_    -> defer_count_increment_locked_
  defer_count_clear_        -> defer_count_clear_locked_
  to_remove_add_            -> to_remove_add_locked_
  to_remove_decrement_      -> to_remove_decrement_locked_
  to_remove_clear_          -> to_remove_clear_locked_

The caller-must-hold-lock contract became load-bearing when the
underlying counters became volatile on NO_ATOMICS: ++/+=/-- compile to
a three-instruction LDR/OP/STR sequence that is not atomic against a
concurrent RMW from another task, so the lock is what keeps the
counter consistent. The new suffix makes the requirement explicit at
every call site, matching how the rest of the scheduler documents the
same invariant.

No behavioural change; all call sites already hold lock_.
2026-04-23 05:49:49 -05:00
J. Nick Koston c414cc393f [scheduler] Enable lock-free fast-path on ESPHOME_THREAD_MULTI_NO_ATOMICS
The _empty_() helpers (to_add_empty_, defer_empty_, to_remove_empty_)
forced the lock path on ESPHOME_THREAD_MULTI_NO_ATOMICS by hardcoding
`return false`. That made Scheduler::call() pay a FreeRTOS mutex
round-trip for each of process_defer_queue_ / process_to_add /
cleanup_ on every idle tick just to confirm "nothing to do".

On the only NO_ATOMICS target (BK72xx — ARMv5TE, single-core), an
aligned 32-bit load is atomic at the hardware level. Mark the three
skip-work counters volatile so the compiler cannot cache or elide the
read, and let _empty_() compare against zero directly. Writers still
hold lock_ for any RMW — that invariant is unchanged.

A stale 0 is benign: the counter is checked on every Scheduler::call()
iteration, so a missed update is caught next tick. Same pattern as the
NO_ATOMICS reads in time_64.cpp.

On BK72xx at ~3100 iter/min with ~8us/mutex this reclaims roughly
75ms/min of main-loop overhead. Measured on BK7238/BK7231N while
profiling alongside libretiny-eu/libretiny#360.

ATOMICS and SINGLE paths are unchanged (SINGLE keeps plain uint32_t,
no volatile-read overhead).
2026-04-23 05:47:52 -05:00
J. Nick Koston 8da2b8225a [core] Inline yield_with_select_ as direct wakeable_delay call
yield_with_select_ was a trivial one-line passthrough to
esphome::internal::wakeable_delay(). Remove the wrapper and call
wakeable_delay() directly at the two call sites.
2026-04-23 05:14:25 -05:00
J. Nick Koston 8fe40f33a6 [core] Drop unused lwip_fast_select.h include from application.h
No symbols from lwip_fast_select.h are referenced in application.h.
2026-04-23 05:13:12 -05:00
J. Nick Koston 14bd2f5a52 [core] Remove stale includes from application.cpp
socket.h is unused after #15931 moved the host wake mechanism to
wake.cpp. lwip_fast_select.h is already included via application.h
under the same guard.
2026-04-23 05:12:12 -05:00
J. Nick Koston 3b212b9944 [core] Add permalink to BDK sctrl_dpll_delay200us in WDT interval comment
So future readers can verify the errata claim without chasing the PR.
2026-04-23 04:47:52 -05:00
J. Nick Koston 72d4efb681 Merge remote-tracking branch 'upstream/bk72xx-wdt-feed-interval' into integration 2026-04-23 04:45:51 -05:00
J. Nick Koston c7fdd534bc [core] Raise WDT_FEED_INTERVAL_MS to 2000ms on BK72xx
BK72xx silicon requires a ~200us busy-wait (sctrl_dpll_delay200us) between
two watchdog register key writes on every reload, making each
arch_feed_wdt() call ~300us on BK7231T/N/BK7238. This is hardware errata
in the BDK's wdt_ctrl (beken378/driver/wdt/wdt.c:WCMD_RELOAD_PERIOD) and
cannot be worked around at the SDK level.

LibreTiny initialises the BK72xx HW watchdog at 10000ms, but ESPHome's
generic WDT_FEED_INTERVAL_MS of 300ms was sized for ESP8266's 1.6s soft
watchdog. That left BK72xx over-servicing the watchdog ~33x per timeout
window, paying ~60ms/min of main-loop overhead to no benefit.

Raise the interval to 2000ms on BK72xx only, which keeps a 5x safety
margin on the 10s HW WDT — matching the ESP8266 ratio that originally
motivated the 300ms value — while cutting feed frequency ~6x.

Measured on a NiceMCU XH-WB3S (BK7238) while testing
libretiny-eu/libretiny#360:

  Before (300ms interval):
    wdt_slow_path: hits=195 (5.9% of iters, avg=315.97us/hit)
    main_loop_before_breakdown: sched=73.2ms, wdt=61.6ms, residual=0.0ms

Other platforms retain the existing 300ms value.
2026-04-23 04:44:05 -05:00
J. Nick Koston b47a7e65a8 Merge remote-tracking branch 'upstream/wifi-bdk-bk7238-typedef-conflict' into integration 2026-04-23 04:25:52 -05:00