MylesMor / nanoleafapi

A Python3 wrapper for the Nanoleaf OpenAPI, for use with the Light Panels, Canvas and Shapes (Hexagons, Triangles and Elements).
https://nanoleafapi.readthedocs.io
MIT License
59 stars 15 forks source link

Feature request: Auto-Registration #10

Closed henryruhs closed 3 years ago

henryruhs commented 3 years ago

Got your library running and works flawless accept the token handling.

It would be nice to have automated registration and storage of the token, on the first run an NanoleafRegistrationException should be thrown. The general Exception on false connection should also throw an more accurate NanoleafConnectionException as pylint does not like except with undefinied exception type.

I got use to this experience with phue that is made for Philips Hue and it's bridge:

https://github.com/studioimaginaire/phue/blob/master/phue.py#L702-L722

It turns out they save the config file in the userspace according to the OS:

https://github.com/studioimaginaire/phue/blob/master/phue.py#L608-L615

This is how I integrated it - no need for token handling on my side what is perfect for a user:

https://github.com/redaxmedia/chroma-feedback/blob/master/chroma_feedback/consumer/philips_hue/api.py#L15-L29

@MylesMor Are you active on Discord? I tried to reach out to you for a live chat if you don't mind.

henryruhs commented 3 years ago

Here comes my integration that includes a tempoary get_token workaround:

https://github.com/redaxmedia/chroma-feedback/blob/master/chroma_feedback/consumer/nanoleaf_light/api.py#L16-L35

Wanted code with a perfect setup of exceptions:

def api_factory(ip : str) -> Any:
    api = None

    try:
        from nanoleafapi import Nanoleaf, NanoleafConnectionException, NanoleafRegistrationException

        try:
            api = Nanoleaf(ip, token)
        except NanoleafConnectionException:
            exit(wording.get('connection_no').format('NANOLEAF LIGHT') + wording.get('exclamation_mark'))
        except NanoleafRegistrationException:
            exit(wording.get('press_button').format('PAIRING', 'NANOLEAF LIGHT') + wording.get('exclamation_mark'))
        return api
    except ImportError:
        exit(wording.get('package_no').format('NANOLEAF LIGHT') + wording.get('exclamation_mark'))
MylesMor commented 3 years ago

Implemented this in the latest package version (1.2.0), thanks for the suggestion!