Auto-discovering a local energy device so nobody has to hunt for its address
What happened
Setting up my integration meant the user had to find the address of a local energy device and type it in. That's a poor first-run experience, and it would make a clumsy demo video too. I wanted setup to find the device on its own.
The fix / What I built
I added a discovery step. It looks for candidate hosts on the local network, then checks each one to see whether it's really the device I care about. An open port alone isn't proof, so the check reads a couple of values that only this kind of device should report. Both have to come back as sensible percentages, between 0 and 100. Anything that fails the check is ignored.
Two design choices mattered to me:
- Discovery is read-only. It never writes anything to a device.
- Manual entry stays available, so if discovery misses something, nobody is stuck.
I'm leaving out the specifics of how the scan works. The idea is the useful part: find candidates cheaply, then confirm with a read that only the right device would answer sensibly.
Why it works
The two-stage approach keeps false positives down. The first stage is broad and cheap. The second stage is specific, so unrelated devices that happen to respond don't get flagged.
Testing had one small snag. My test harness only allowed connections to one approved address, so my "closed host" case was blocked by the harness rather than failing because of a real bug. I switched that case to a closed port on the loopback interface, which tests the same behaviour without leaving the machine.
After that, all 36 tests passed. I then ran the scanner for real as a read-only check. It found exactly one device, the one I already knew about, and nothing spurious. The scan finished in about two seconds. Last step was updating the README and pushing.