From d332d447aa145f173c41e3166d4dfc5d6e60d472 Mon Sep 17 00:00:00 2001 From: jimmy Date: Tue, 27 Oct 2026 22:30:26 +1300 Subject: [PATCH] fix(v2): do not wipe a driver's groups when it announces and is in no zone The controller sent a GROUPS packet on every driver hello. For a driver in no zone that was an empty list, which replaced the groups stored on the driver. It is now sent at announce and resync only when the list is not empty; explicit zone and device edits still send every list. Co-Authored-By: Claude Sonnet 5.5 --- v2/main/engine.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/v2/main/engine.c b/v2/main/engine.c index 0e4b320..8bfb08b 100644 --- a/v2/main/engine.c +++ b/v2/main/engine.c @@ -788,8 +788,10 @@ static esp_err_t send_cmd(const uint8_t mac[6], const uint8_t *env, size_t len, return rel_send(mac, wire, len + 2, commit, 1); } -// GROUPS packet: the ids of every zone whose driver names include `name` -static void send_groups(const uint8_t mac[6], const char *name) +// GROUPS packet: the ids of every zone whose driver names include `name`. An empty list replaces +// whatever the driver stored, so it is only sent when allow_empty is set (a zone or device was edited); +// a driver that merely announced itself and is in no zone keeps the groups it already has. +static void send_groups(const uint8_t mac[6], const char *name, bool allow_empty) { uint8_t pkt[REL_FRAG_MAX]; size_t off = 3; @@ -807,6 +809,9 @@ static void send_groups(const uint8_t mac[6], const char *name) count++; } } + if (count == 0 && !allow_empty) { + return; + } pkt[0] = 0x4C; pkt[1] = 0x02; // GROUPS pkt[2] = count; @@ -830,7 +835,7 @@ void engine_groups_changed(void) uint8_t mac[6]; const char *hex = cJSON_IsString(addr) ? addr->valuestring : d->string; if (cJSON_IsString(name) && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") && hex_mac(hex, mac)) { - send_groups(mac, name->valuestring); + send_groups(mac, name->valuestring, true); } } store_unlock(); @@ -872,7 +877,7 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len) cJSON_ReplaceItemInObject(rec, "name", cJSON_CreateString(name)); store_save("device"); } - send_groups(mac, name); + send_groups(mac, name, false); store_unlock(); } @@ -883,7 +888,7 @@ static void resync_driver(const uint8_t mac[6]) store_lock(); const char *name = device_name_for(mac); if (name) { - send_groups(mac, name); // membership first, in case the driver lost it + send_groups(mac, name, false); // membership first, in case the driver lost it } const zone_cur_t *best = NULL; for (int i = 0; name && i < 16; i++) {