Checking to see if my extension has been enabled or not

I am having problems with my extension, I have to keep enabling it.

I have previously authorized it with discovery.py and stored my_core_id_file & my_token file. When I run this
code



try:
    core_id = open("/etc/roon_cad/my_core_id_file").read()
    token = open("/etc/roon_cad/my_token_file").read()
except OSError:
    print("Please authorise first using discovery.py")
    exit()

discover = RoonDiscovery(core_id)
server = discover.first()
discover.stop()

roonapi = RoonApi(appinfo, token, server[0], server[1], True)

When it runs the last line of the code, it hangs because the extension is not authorized. How can I have it return a status and continue?

Many thanks

Looks like you’re using my python roon library (which isn’t officially supported by roon).

If the call hangs it’s likely that the token wasn’t valid, and so the library is trying to authenticate again.

Do the examples here work?

They work, yes,

If the token is not valid is it possible to return a status and for the program to carry on?

For historical reasons that isn’t how the code works. It would require significant restructuring to work as you suggest.

If the demos work and your code doesn’t, suggest you look closer at the the contents of both the core_id and token_id file.

Having either file incorrect could cause the issue you’re seeing.

1 Like

My extension will work for a while but then it needs to be re-enabled in the Roon setttings / Extensions to get it working again. I cannot explain why.

The token is only needed when connecting to roon, so if you authorise when you connect and your extension doesn’t disconnect - then it will work.

But if you don’t save the new token after authorising- then it won’t work next time you connect.

You can see how to get the token (so you can save it) in the authorisation example in the demo code.

You could save on every run if you like.

If you click ‘view’ on the roon authorisation screen - you will probably see multiple authorisation tokens (which you can delete).

Authorisation tokens are very long lived. My home asisitant one is about a year old.

1 Like

I do discovery and authorization a little differently:

discover = RoonDiscovery(None)
server = discover.first()
discover.stop()

apis = [RoonApi(app, None, server[0], server[1], False)]
auth_api = []

print("\nWaiting for authorization - in Roon, click Settings -> Extensions -> Enable\n", flush=True)
while len(auth_api) == 0:
    auth_api = [api for api in apis if api.token is not None]
    time.sleep(10)

api = auth_api[0]

for api in apis:
    api.stop()

print("\nRoonCoreIP =", api.host)
print("RoonCorePort =", api._port)

# save the token for next time
with open(tokenfile, "w") as f:
    f.write(str(api.token))

This attempts to locate all Roon cores but I only grab the first. You are also only getting the first but supplying a core id to disover. Is this because you have multiple cores or wish to support a deployment with multiple cores? Also, I am saving the returned token for future use and this token gets refreshed and saved on subsequent access. Anyway, this is how I am doing it.

It’s based on my discovery/ connect example code.

I found that some roon clients - esp on Mac - respond to the server discovery packet - even though they are not cores.

So my discovery code gets all the clients that respond, asks all for authorisation, and notes which one gets authorised.

It then stores both the core id and the token, and then subsequent connect requests specify the core id, so they don’t get confused by the other responses.

Of course this also works if you do have multiple cores.