HomeKit Accessory Protocol (HAP) over CoAP/UDP (was: Nanoleaf Essentials bulb via Thread/CoAP)

@madisonjar it is pretty exciting to finally have all this more or less working.

So yes, Thread creates a mesh network very similarly to Zigbee. They both use 802.15.4 under the hood. Zigbee put a proprietary protocol on top of that while Thread stuck IPv6 on top and then Apple stuck HAP on top of Thread. For both technologies you need a way to access the 802.15.4 network: Zigbee needs a coordinator device while Thread needs one or more Border Routers. Thread likes to say they don’t have gateways but what they really mean is that many devices in the market are capable of being a Border Router and maybe you end up with a dozen BR-capable devices out of the 100 you purchase. The extremely unsurprising bit so far has been the unwillingness of different manufacturers to open their Thread networks to devices from other manufacturers. Nanoleaf has actually committed to this so we’ll see if others follow. I just read about Thread 1.3.0 which I didn’t realize had released. Apparently the big feature there is smashing roadblocks manufacturers have put up to ensure Thread devices/networks can all interoperate. You can probably thank Matter for that which will also use Thread just like HAP.

Anyway, all of that to say that yes, the mesh network allows communication to stretch further than a single device can reach. You should take a look at the OpenThread docs, they’re pretty good: บทบาทและประเภทโหนด  |  OpenThread

As for hardware you’ll need, as mentioned the big one is a Border Router. There are a few options currently from a HomePod mini to a few Nanoleaf products (Elements, Lines, Shapes). One of the Amazon Echo devices is supposed to turn on Thread eventually. Link to a list from The Verge.

Finally, you need a way to set up and provision joining devices. Thread has a few different ways that can happen but the simplest is you give new devices all the info they need & tell them to go join the specified network. That appears to be what HAP on Thread does. The normal workflow for Nanoleaf, as an example, would be to use your phone to set up a Border Router like their Elements which creates a network & stores the credentials on your phone. Then, when the NL app talks with Thread-capable devices, it sends those credentials to the new device & voilà: it joins the network. The reason I call all this out is that the Android NL app will happily show you all the network details including the network key. iOS, on the other hand, hides those away in protected storage. You can get at them but it isn’t easy. Moral of the story: don’t get a HomePod mini + iOS device & expect to plug Thread network credentials into HA any time soon. No investigation has been done on how the Thread network itself is created on any BR-capable device so we rely on 3rd-party apps (NL on any platform, iOS Home) to do that & then we grab the credentials.

EDIT to answer your first question: we have full “basic” control over color, brightness, etc. We don’t have scenes (although I did some investigation here) or other features yet.

2 Likes

I guess I’ll pick up that $50 shapes mini kit I saw the other day on Amazon haha.

Thanks for all the dedication to this project!

Thats exactly what I did already lol, Talked my wife into too by saying how our daughter would love to have some lights on her wall to go along with all the hobby lobby stuff she wanted to buy her lol! thinking about getting one more

Has anyone had any luck getting an Essentials Strip to connect via Thread using a Nanoleaf Border Router? I’m using the Nanoleaf Lines as my Border Router and I’m able to see it in the Nanoleaf app as a BR, but I haven’t had any luck getting the strip to switch over from Bluetooth control to Thread. Ive tried multiple Factory Resets on both Lines and Essentials but nothing seems to work. Any suggestions would be appreciated!

The app has a bug where it doesn’t always show essentials in the thread network viewer (new versions seems to have fixed this) so use an mDNS browser to see if the strip is connecting or not, if it is you should see it under _hap._udp

If it’s not connecting what sometimes helps is if you go into the app, select your lines, go to settings on it, then switch it to remote mode, wait a few minutes, and switch it back to thread mode.

I currently have 3 bulbs and a strip connecting through thread to HA, currently migrating to the August HA release and seeing what hiccups there is to connect them.

@madisonjar or @lambdafunction

Could one of you do me a favor and see if this works with just the Nanoleaf controller? (No light panels connected)
I would assume the border router is the controller itself and not each connected panel.

If so maybe I’ll just buy a used controller on eBay and when/if we ever get full native iOS support with thread/matter on the Nanoleaf platform or I can just use my HomePod mini, I’ll sell the controller or give it away.

I don’t really have a need for the panels and I found some cheap controllers used on eBay.

@mrdj863 the controller very likely contains the Thread radio. I had to get a warranty replacement of the controller from Nanoleaf a few months back & the Thread network was inaccessible during that time. The power connector on the bottom of the controller doesn’t fit the wall wart due to how they have it shaped, though. Without modifications you’d need at least one panel. I have Elements for reference.

Copy that. I’ll look into my options later tonight.

Thanks! I’m glad I just didn’t buy a power supply on a whim. Haha it almost sounds like a planned obscurity that they force you to at least buy one light kit to use it.

Hey. I’m using Elements as border router with 3 Essentials bulbs connected to it over Thread (via Android app). What do I need to do to add it to HA?

@spanky13 you need to find a way to add these environment variables to your HA which will depend on how you are running things:

export AIOHOMEKIT_TRANSPORT_BLE=1
export AIOHOMEKIT_TRANSPORT_COAP=1

After that, you’ll see HomeKit devices that are available over BLE or CoAP. You’ll need the 8 digit code on the bulbs to pair. Make sure to put the code in how HomeKit requires, not how it is printed on the bulb. Let us know how it goes!

Thank you for the quick response. I can’t seem to get these feature flags to stick after restarts. Will continue researching about that.

By the way, the process you described is for connecting the Essentials bulbs (bluetooth only) to HA via bluetooth, is that right? Adding them to HA over Thread is still a work in progress?

@spanky13 how are you running HA?

CoAP is the Thread support. Add those env variables and you’ll be able to find unpaired accessories over BLE or Thread (CoAP).

Think of a 7-layer cake: the Border Router sees a cake coming out of the kitchen, chops off the top 4 layers, puts them on a new 3-layer cake bottom, and finally smooths it over to look good again. That’s what the BR does when it translates (HAP over CoAP over UDP over IPv6 over) WiFi to Thread.

Here’s a screengrab of what the WiFi network traffic and Thread network traffic looks like. You can see they are both doing a CoAP PUT to /nlsecure with the 0101 TLV + 0020 bytes of data:

WiFi:
image

Thread:

Gotcha! I’d love to give it a shot and report back if I observe anything weird. I’m running HA OS on a Raspberry Pi.

I couldn’t figure out how to pass environment variables on my HA OS so I just hacked the code. If you’re familiar with vi/vim, give this a try.

Enter the homeassistant container from HA OS:

docker exec -it homeassistant sh

And then let’s edit some Python!

vi /usr/local/lib/python3.10/site-packages/aiohomekit/const.py

and edit line 36 (OLD)

    if "AIOHOMEKIT_TRANSPORT_COAP" in os.environ:

to read (NEW, spacing and capitalization are important):

    if True:

You’ll also have to edit aiocoap to fix an issue they haven’t merged a PR for yet:

vi /usr/local/lib/python3.10/site-packages/aiocoap/transports/ws.py

and edit line 187 (OLD):

            else:

to read (NEW):

            elif port != 0:

Finally, restart HA from the web interface. You should see Essentials as new devices!

1 Like

@lambdafunction Thanks. I applied the code hacks but somehow the bulbs still don’t show up in HA. I manually looked for them in HomeKit Controller integration too but only the Elements panel is there. Are there any logs that I can share to help diagnose the problem?

Have you already added them to a homekit system, google nest, or ios app? If so that’ll take up the one admin slot the essentials have and you’ll have to factory reset them.
Currently the best way to acquire them is to use the android app, which apparently also has a better thread network viewer.

Edit:
Updated HA finally and made the changes lambda outlined, seems to work fine for me though I already had my essentials acquired to HA previously.

I had them previously added to the Nanoleaf Android app only. Just to be sure, I did a factory reset on the bulbs, added them again to the Nanoleaf app, then looked for them in HA. Still no dice.

Here’s my HA setup:

  • HA OS on raspi
  • Nanoleaf Elements added to HA using the Nanoleaf integration (is this correct?)
  • Nanoleaf Essentials connected to Thread network created by the Elements border router (using Android app)

@spanky13 first, start with an mDNS service browser. On Android I use one called Service Browser by Andriy Druk on the Play Store. It’s OSS and seems to work fine. Look for _hap._udp. (HAP) services; there should be one for each bulb. Additionally, if you tap into a bulb you will see the mDNS properties; ensure that sf is 1 (pairable). On Mac I use dns-sd -Z _hap._udp to find the bulbs and on Linux I use avahi-browse -r _hap._udp.

You can also try to ping the bulbs.

ping6 Nanoleaf-A19-12FA.local
OR
ping -6 Nanoleaf-A19-12FA.local

If that looks good, go call the service “Logger: Set level” from the Developer Tools:

service: logger.set_level
data:
  aiohomekit: debug
  homeassistant.components.homekit_controller: debug
  zeroconf: debug

And let that sit for a bit to see if HA detects any HAP devices. Maybe turn a bulb off, wait a few seconds, then turn it back on with the logs at debug. Bulbs should trigger the BR to broadcast an mDNS service on startup.

1 Like

I need to call a freind who owns an android over to my house haha! All this development has me itching to try this.

You’re killing it lambda!

If sf is not set to 1, what are the next steps? Do I need to delete the bulbs from HomeKit in order for them to start showing up in Logger or at the very least having an sf value of 1?