Clean up duplicate work in publish_device_info - #324
Conversation
getProperties was called and parsed twice for the same model, then the loop iterated one copy while reading each property's metadata out of the other. Iterating pub_device["properties"] directly drops the second parse. With one copy left, the `if k in pub_device["properties"]` guard around the metadata lookup is trivially true, so it goes as well and the block dedents. It was never doing anything: both copies came from the same getProperties output, so the key was always present. No change to what gets published: same topics, same payloads.
publish_device_tracker was called from inside the per-property loop but doesn't depend on the loop variable, so it re-published the same retained config once per decoder property: twice for HOLYIOT, BM2 and BM6, four times for Miband. Moving the call above the loop publishes it once. It stays ahead of the per-property configs, so the order on the wire is unchanged. This keeps the intent of theengs#232, which moved the call to the top of the loop body because tracker-only devices declare a single "device" property that hits the `continue` below. Above the loop it can't be skipped at all.
Just fo9r verification can yo9u turn on Advanced and Advertising Data with and see if they are both still identical, or if |
|
Good idea - I tested with They can't actually diverge: I also verified this live on my production gateway with |
|
Thanks |
Description:
Two independent cleanups to
DiscoveryGateway.publish_device_info, neither of which changes what gets published. They came out of the review on #323 and are split out here so that PR can be just the feature.Only parse decoder properties once.
getPropertieswas called and parsed twice for the same model - once intopub_device["properties"], once into a localdata- and the loop then iterated one copy while reading each property's metadata out of the other. The loop now iteratespub_device["properties"]directly. With a single copy left, theif k in pub_device["properties"]guard around the metadata lookup is trivially true, so it goes as well and the block dedents. It was never doing anything; both copies came from the samegetPropertiesoutput, so the key was always present.Publish
device_trackerdiscovery once per device.publish_device_trackerwas called from inside the per-property loop but doesn't depend on the loop variable, so it republished the same retained config once per decoder property - twice for HOLYIOT, BM2 and BM6, four times for MB/SW. Moving the call above the loop publishes it once. It still runs ahead of the per-property configs, so the order on the wire is unchanged.That second one keeps the intent of #232, which moved the call to the top of the loop body because tracker-only devices declare a single
deviceproperty that hits thecontinuebelow - at the bottom of the loop it never fired for them. Above the loop it can't be skipped at all. I checked that case explicitly: TILE (onedeviceproperty) publishes its tracker config exactly once before and after.Verification
I stubbed
publishon a bareDiscoveryGatewayand captured every call - retained config payloads keyed by topic, plus a raw call count per topic - across a set of models picked to hit each branch of the loop: CGPR1 (binary_sensor viamotion,macskip, and theentity_typecarry-over between iterations), CGDK2 and TPMS (several measurement properties), IBEACON (nothing skipped), APPLEWATCH (theunlockedval_tplspecial case), LYWSD03MMC/MJWSD05MMC_PVVX_ENCR (cipher/ctr/micskips), HOLYIOT and MB/SW (tracked, 2 and 4 properties), and TILE (tracker-only).Retained configs are byte-identical to
developmentfor every one of them. The only thing that changes is the tracker publish count: HOLYIOT 2 → 1, MB/SW 4 → 1, TILE 1 → 1. I ran the first commit on its own as well - identical configs and identical counts, as expected, since the republishing is untouched at that point.Checklist: