Oro / pynanoleaf

A Python3 wrapper for the Nanoleaf API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add method to get nanoleaf serial number

keis opened this issue · comments

It would be useful to have a method to get the serial number of the nanoleaf to help facilitate storing tokens for multiple lights for example.

The serial number is available on a unauthenticated endpoint http://host:80/device_info

Should be easily doable, I'll probably be able to take a swing at it this weekend (pull requests welcome though :) )

call is reasonable easy to implement (I already did so in home-assistant/core#46407) but because it's on a different port and not under /api/v1 will need some juggling of internals to make it look nice. I'll have a look at making a PR but that sounds like a weekend project for me too :)

Cheers!

Oh I see, that's a weird endpoint then; Having a quick glance at the API docs I wasn't even able to find it, seems to be undocumented (or my bookmarks for the nanoleaf API docs are too old, https://forum.nanoleaf.me/docs )

Maybe (spitballing here, I haven't looked at the PR for homeassistant too closely yet) a new property is good enough for your usecase?

    @property
    def serialNo(self):
        return self._get("/")["serialNo"]

hmm, didn't check the docs to closely, I just stumbled on this endpoint after opening / which is a small web page that queries this.

the problem with getting the serialNo from the api is I need this to not be authenticated because my idea was to use this to look up the token. I notice the mDNS has some sort of device id so it's possible I could use that instead and then query the serialNo with the api like that to attach as metadata.

Oh yeah right, you don't have an auth token yet because the serial would be used to get the corect auth token.

mDNS would be great to fetch the serial via its TXT record, good idea!

Using the mDNS seems to be the better approach so went with that, still have the need for serialNo as metadata so created a PR with your suggestion above.

btw I notice that reading (or writing) multiple properties results in multiple http requests, is there a reason for this?

Thanks again for the Issue and the fix!

No particular reason for it, the nanoleaf API would allow it afaik; I just don't know how (yet) to allow multiple properties to be set at the same time (consider me a beginner at best at Python).

yeah, the api accepts an update like this e.g {'hue': {'value': 20}, 'sat': {'value': 20}} it doesn't seem to make much a difference probably it's buffered on the device side.

python doesn't let you set multiple properties in one go. So it would have to be something like mynanoleaf.update(hue=20, saturation=20) or perhaps

# Staging changes to be made
state = pynanoleaf.State()
state.hue = 20
state.saturation = 20
# Commit changes by calling nanoleaf API
mynanoleaf.state = state

Ah okay, I see, thanks for the explanation.

I don't have any strong feelings about that, my thought process might have been that it'd be easier to reason from the outside (whatever hit last, overrules), e.g. setting effects and color inside one update, what should count? - but that's just a flimsy excuse, the more reasonable explanation might be that that's what Homeassistant expected, this API was really just implemented to be able to call the Nanoleafs with Homeassistant :)

Sidenotes: If you have anything you'd like added//changed, please feel free to create a PR; I am happy that it is useful to someone else apart from myself!
Having a look around it seems there is actually nowadays a library which seems comprehensive//maintained enough with https://pypi.org/project/nanoleafapi/ , it might make sense to switch home assistant to that one if that proves more useful (saying that without having looked at it too closely)