Skip to main content

Hyperion Ambient Lighting Python Package

Project description

Hyperion Library

Python library for Hyperion-NG. See JSON API for more details about the inputs and outputs of this library.

Usage

Client API calls

All API calls can be found in client.py. All async calls start with async_.

Calls

Note that the command and subcommand keys shown in the above documentation will automatically be included in the calls the client sends, and do not need to be specified.

Client inputs / outputs

The API parameters and output are all as defined in the JSON API documentation.

Example usage:

#!/usr/bin/python

import asyncio
from hyperion import client, const

HOST = "hyperion"

async def print_brightness():
    hyperion_client = client.HyperionClient(HOST)
    if not await hyperion_client.async_connect():
        return
    print("Brightness: %i%%" % hyperion_client.adjustment[0][const.KEY_BRIGHTNESS])

if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(print_brightness())

Running in the background

The HyperionClient subscribes to updates from the Hyperion server, which are used to keep the client state fresh. Optionally, callbacks can be triggered to the user. To receive these callbacks, the client needs to run as an asyncio background task.

Callbacks

The client can be configured to callback as the Hyperion server reports new values. There are two classes of callbacks supported:

  • default_callback: This callback will be called when a more specific callback is not specified.
  • callbacks: A dict of callbacks keyed on the Hyperion subscription 'command' (see JSON API documentation)

Callbacks can be specified in the HyperionClient constructor (default_callback= or callbacks= arguments) or after construction via the set_callbacks() and set_default_callback() methods.

As above, the callbacks dict is keyed on the relevant Hyperion subscription command (e.g. components-update, priorities-update). The client also provides a custom callback with command connection-update of the following form:

{"command": "connection-update",
 "connected": True}

This can be used to take special action as the client connects or disconnects from the server.

Example use of asyncio event loop to deliver callbacks

from hyperion import client, const

HOST = "hyperion"

def callback(json):
    print("Received Hyperion command: %s" % json)

if __name__ == "__main__":
    hyperion_client = client.HyperionClient(HOST, default_callback=callback)
    asyncio.get_event_loop().run_until_complete(hyperion_client.async_connect())

    # Start client in "background".
    hyperion_client.start_background_task()
    asyncio.get_event_loop().run_forever()

Output:

Received Hyperion command: {'command': 'connection-update', 'connected': True}

ThreadedHyperionClient

A ThreadedHyperionClient is also provided as a convenience wrapper to for non-async code. The ThreadedHyperionClient wraps the async calls with non-async versions (without the async_ on the method names shown above).

Example use of Threaded client

#!/usr/bin/python

from hyperion import client, const

HOST = "hyperion"

def callback(json):
    print("Received Hyperion command: %s" % json)

if __name__ == "__main__":
    hyperion_client = client.ThreadedHyperionClient(HOST, default_callback=callback)

    hyperion_client.connect()
    print("Brightness: %i%%" % hyperion_client.adjustment[0][const.KEY_BRIGHTNESS])

    # Run in a separate thread.
    hyperion_client.start()
    hyperion_client.join()

Output:

Received Hyperion command: {'command': 'connection-update', 'connected': True}
Brightness: 59%

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyperion-py-0.0.3.tar.gz (12.3 kB view hashes)

Uploaded Source

Built Distribution

hyperion_py-0.0.3-py3-none-any.whl (12.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page