Skip to main content

Hyperion Ambient Lighting Python Package

Project description

PyPi PyPi Build Status

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.1.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

hyperion_py-0.1.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file hyperion-py-0.1.1.tar.gz.

File metadata

  • Download URL: hyperion-py-0.1.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.7.5

File hashes

Hashes for hyperion-py-0.1.1.tar.gz
Algorithm Hash digest
SHA256 db93b8477f0cb8f545e9356efb771963429529040e69774c7424ba9082eb4605
MD5 2bb29e2d16e14a31b99f10593ae0d94f
BLAKE2b-256 c4bc863f28d22cd31f8147217ad655dc13c7ed03e3613d907e957ec3766a69fa

See more details on using hashes here.

File details

Details for the file hyperion_py-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hyperion_py-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.7.5

File hashes

Hashes for hyperion_py-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0f3c9139b405f26d6fb19f70b7af041fff7ba2718555e1439b41a308a6dec207
MD5 28792054e79a4efb26d73df195c6a957
BLAKE2b-256 2a8f036088a886be121c6f9433a6dfeb51b29fd2ba2eff730c7ca850cf431a03

See more details on using hashes here.

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