feat: add a button gesture to return to pairing mode

A light that has a network only ever rejoins that one, so there was no way
to re-pair it or move it to another coordinator without reflashing.

Holding the encoder button for 10 seconds now makes the light flash green
twice, forget its Zigbee network (saved to flash) and restart, after which
it looks for a network with an open pairing window, in ZHA with Add device.
The coordinator is not told, so Home Assistant keeps the device and
recognises it by its IEEE address when it joins again.

While the light is looking for a network the on-board LED blinks fast
instead of once a second, so a waiting pairing window can be seen to be
picked up, and the change between joined and searching is logged.

The README documents the gesture and how pairing works, and notes that the
extended PAN ID is broadcast by the network and is not a secret.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Matt SpencerandCopilot committed 2026-11-28 23:26:29 +00:00
1 parent 90af6197c6
commit 575cd8896f
6 files changed
+136 -12

No files matched your search

+60
View File
@@ -6,9 +6,12 @@
use core::ops::Range;
use embassy_embedded_hal::adapter::BlockingAsync;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::signal::Signal;
use embassy_time::Duration;
use embassy_time::Instant;
use embassy_time::Timer;
use embassy_time::with_timeout;
use esp_storage::FlashStorage;
use zigbee::aps::apsde::ApsdeSapConfirmStatus;
use zigbee::CurrentPowerMode;
@@ -57,6 +60,7 @@ use zigbee_types::ShortAddress;
use crate::effects_cluster;
use crate::effects_cluster::EffectsServer;
use crate::light;
use crate::status;
/// Flash region reserved for Zigbee persistence.
///
@@ -565,3 +569,59 @@ pub async fn identify_task() {
}
}
}
static NETWORK_RESET: Signal<CriticalSectionRawMutex, ()> = Signal::new();
/// Asks for the Zigbee network to be forgotten, which [`network_reset_task`]
/// then does.
pub fn request_network_reset() {
NETWORK_RESET.signal(());
}
/// Puts the light back into pairing mode on request.
///
/// A light that has a network only ever rejoins that one, so this is how it is
/// moved to another or re-paired from scratch: it forgets the network, then
/// restarts and goes looking for one with an open pairing window (in ZHA,
/// **Add device**).
///
/// The coordinator is not told. Home Assistant keeps the device, and recognises
/// it by its IEEE address when it joins again.
#[embassy_executor::task]
pub async fn network_reset_task(stack: &'static Stack) {
NETWORK_RESET.wait().await;
log::warn!("network reset: forgetting the Zigbee network");
// two green flashes, so the reset can be seen before the light restarts
light::play_effect(light::Effect::Okay);
Timer::after(Duration::from_millis(1500)).await;
// forgets the network and writes that to flash, so the next boot starts
// commissioning from scratch rather than resuming
stack.forget_network().await;
log::warn!("network reset: network forgotten and saved, restarting");
esp_hal::system::software_reset()
}
/// Shows on the status LED whether the light is looking for a network.
#[embassy_executor::task]
pub async fn pairing_status_task(stack: &'static Stack) {
let mut was_joined = None;
loop {
// the join gate stays open once joined, so this returns at once when
// the light is on a network and times out when it is not
let joined = with_timeout(Duration::from_millis(1), stack.wait_until_joined())
.await
.is_ok();
status::set_searching(!joined);
if was_joined != Some(joined) {
if joined {
log::info!("on a Zigbee network");
} else {
log::info!("not on a Zigbee network: looking for one to join");
}
was_joined = Some(joined);
}
Timer::after(Duration::from_millis(250)).await;
}
}