# Rewriting the firmware of my Zigbee light switches

> I wanted a double click that always fires. My $15 switches now meter power, drive shutters and lock their buttons when the alarm is armed, and the fork builds for 204 devices.

2026-11-17 · Wojciech Wentland · https://blog.wentland.io/blog/rewriting-zigbee-switch-firmware/

I wanted one thing from the light switches in my flat: a double click that triggers a Home Assistant automation every
time. What I have now is a [firmware fork](https://github.com/desty2k/tuya-zigbee-switch-bseed) that meters power,
drives shutters, finds the pinout of a device it can't see inside, locks its buttons when my alarm is armed, and builds
for 204 boards.

The double click was fixed early. Everything after that was me saying yes to the next idea, because saying yes cost me
one message to an agent.

## The thing I needed

The switches are BSEED wall switches, cheap Zigbee devices built on a Tuya design. Their stock firmware has a bug I
couldn't live with: press two buttons at once and only one relay toggles. There's an open-source replacement,
[romasku/tuya-zigbee-switch](https://github.com/romasku/tuya-zigbee-switch), that fixes it and installs over the air,
straight from the stock firmware. I installed it and it was fine.

Except the double click. Sometimes it fired, sometimes it didn't.

The firmware reported a button as a state (not pressed, pressed, long press) and Home Assistant had to guess a double
click from how that state changed. Two quick changes can arrive at the hub as one. On top of that, the light switched
on the first press, before anyone knew whether a second press was coming. With the button driving the relay, a double
click meant for an automation also toggled the light.

Fixing that meant waiting until the gesture is known before doing anything, and sending every click as its own event
instead of a state that can be overwritten. That's a different shape of code: something between the button and the
relay that collects presses and turns them into single clicks, double clicks and holds. Once it existed, the double
click fired every time.

![Relay mode setting of one switch button in Zigbee2MQTT. In deferred_single mode the relay waits until the press is known to be a single click](https://blog.wentland.io/images/zigbee-switch-firmware/relay-mode.png)

I wrote none of it, and none of the other C in the project. I worked with Codex (Astra 6) and Claude Code (Opus 5.5),
and the routine settled quickly: I'd discuss the architecture with an agent until I agreed with it, then let it
implement on its own while I did something else. Some evenings I went shopping while an agent flashed the switches
around my flat and ran the checks. I don't write C day to day and I didn't read the code line by line. My review was
the switch on the wall.

That could have been the end. Instead I decided the firmware should run on every Zigbee device in my flat, and not all
of them were light switches.

## The outlet

First came a BSEED outlet with a power meter, built on the same chip. One button, one relay and a meter chip. I
expected a small step. The meter reports power as thousands of pulses a second, which would drown the button code, so
it needed its own path. And the chip's flash couldn't hold every feature for every device anymore, so features had to
be compiled per board.

![Power, voltage, current and energy readings from the outlet in Zigbee2MQTT](https://blog.wentland.io/images/zigbee-switch-firmware/outlet-meter.png)

The fun problem was that I didn't know which pin did what, and I couldn't open the outlet without breaking it. So the
agent and I built a probe image: firmware that joins the network and lets you read any pin or send it a short pulse
over Zigbee. The agent sent pulses pin by pin and I watched for the light to flash. That's how the board got its
pinout.

It was the part I enjoyed most, and the most nervous I've been in the whole project. Driving an unknown pin high or
low could break something, and the only way to find out which pin was which was to try. A computer was switching
things in my home, and I had to stare at the lamp to catch a flash of a few tens of milliseconds.

## The shutters

A shutter switch drives a motor through two relays, one per direction. They must never be on at the same time, and
between one switching off and the other switching on there has to be a pause so the motor stops.

In the original firmware several parts of the code could switch a relay directly: the button handling, the Zigbee
code, the startup logic. Adding that rule would have meant a check in each of them, and hoping nobody added another
caller later. So relays got a single owner. Every request goes to one place, which enforces the pause and then switches
the pin. An auto-off timer, the kind a garage door opener needs, came along almost for free once that existed.

The shutter also has to know where it is, and it only knows by time. The stock firmware had one travel time for both
directions, and the only way to set it was to start the shutter and stop it at the right moment. Up and down can take
different times. The open-source firmware already took separate open and close times as numbers, and ran the motor
three seconds past each end so the shutter really got there. I made that overrun a setting.

![Shutter timing settings in Zigbee2MQTT: open time, close time, deadzones at each end and the overrun](https://blog.wentland.io/images/zigbee-switch-firmware/shutter-timing.png)

Then I flashed the shutter switch and it started buzzing loudly, both motor relays chattering.

The cause, as far as the agent and I worked out: the stock firmware holds the pins it doesn't use low. Mine left them
floating, and a floating pin next to mains wiring picks up enough noise to rattle a relay. Nothing broke, but it scared
me, and now every pin the firmware doesn't use is held low.

By the time the shutters worked, I had rewritten everything above the hardware layer.

## The alarm

When the alarm in my flat is armed, every switch now ignores its buttons while Home Assistant can still control the
lights. The firmware calls it a child lock.

It was small to build, because by then every button press already went through one place and the lock is one check
there. In the original code it would have been a change in every handler. I liked this part of the rabbit hole: every
structural change so far had come from a feature that needed it, and each one made the next feature cheaper.

## What each feature brought with it

| I asked for | It needed |
|---|---|
| Double click | Waiting for the gesture, clicks sent as events |
| Outlet with a meter | A separate path for meter pulses, features compiled per board, a probe image |
| Shutters | One owner per relay, a pause between directions, held-low pins |
| Alarm block | One entry point for every button press |
| (because I could) | A safe mode for broken configs, diagnostic counters, and every inherited board moved to the new code |

By the last row I wasn't fixing my flat anymore. It had turned into an experiment: how much can I fit into a $15
switch whose stock firmware has very few options? The original project already covered a long list of boards and
generated its Home Assistant and Zigbee2MQTT integrations from one device list. I could have kept the BSEED boards and
dropped the rest. Instead the agents moved every board to the new code and regenerated the integrations, and the fork
now builds images for 204 devices.

## What only the hardware caught

The project has 387 tests that run the whole firmware on my laptop, with a fake clock and a fake radio. They're good
tests. The agents wrote most of them and ran them all the time. Two bugs got past them anyway.

The first release candidate rebooted the switch on almost any button press. Nobody else had installed it, so the only
home it broke was mine, and I fixed it there. The cause was the compiler for the switch's chip, tc32-gcc 4.5.1. At
`-Os`, the setting that makes the image small enough to fit, it stored some `switch` statements as tables of one-byte
offsets and then read them as four-byte addresses, so the code jumped to random places. My laptop uses a different
compiler, so the tests never saw it. The fix was to build without jump tables, plus a check after linking that deletes
the image if one ever comes back.

Later a switch dropped off the network, and the devices that reached the network through it went offline too. Its
radio stack had hit a fault and kept running in that state: still joined, no longer passing anything on for its
neighbors. The fix was to reboot on such a fault, as the chip vendor's own sample code does. The fake radio in the
tests never faults.

Both bugs lived in the two things the laptop version replaces, the compiler and the radio. Only hardware sees
those, and hardware is my flat.

## Where it stands

Eleven devices run it on real walls: three outlets, four switches and four shutter controllers. The 4-gang switch, an
outlet and a shutter controller ran the previous release candidate for two to three weeks, and a 3-gang has run the
current one for a few days. The rest of the 204 build and are published as release candidates in
[the fork on GitHub](https://github.com/desty2k/tuya-zigbee-switch-bseed), waiting for someone with the hardware. The
builds for Silicon Labs, the second chip family the project supports, have never run on a device, and no board has
been through a series of power cuts yet. Anyone coming from the original firmware starts with fresh settings.

As far as I know, nobody outside my flat has installed it. The latest release candidate alone added seventeen entries
to the changelog.
