Skip to content

Commit

Permalink
Update the example for the Python client
Browse files Browse the repository at this point in the history
The API changed a little bit since it was written.
  • Loading branch information
lopter committed Feb 3, 2017
1 parent ae17036 commit d827de6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
38 changes: 27 additions & 11 deletions clients/python/lightsc/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ A Python client to control your smart bulbs through lightsd
lightsd_ is a daemon (background service) to control your LIFX_ WiFi "smart"
bulbs. This package allows you to make RPC calls to lightsd to control your
light bulbs from Python. It is built on top of the ``asyncio`` module and
requires Python ≥ 3.5:
requires Python ≥ 3.5, here is a complete example:

.. code-block:: python
import asyncio
import click
from lightsc import LightsView, create_async_lightsd_connection
from lightsc import create_async_lightsd_connection
from lightsc.requests import (
GetLightState,
PowerOff,
PowerOn,
SetLightFromHSBK,
)
async def example(url, targets):
async with create_async_lightsd_connection(url) as client:
client = await create_async_lightsd_connection(url)
try:
click.echo("Connected to lightsd running at {}".format(client.url))
view = LightsView()
view.update(await client.apply(GetLightState(targets))
click.echo("Discovered bulbs: {}".format(view))
lights_state = await client.apply(GetLightState(targets))
click.echo("Discovered {} bulbs: {}".format(
len(lights_state.bulbs), lights_state.bulbs
))
transition_ms = 600
red_hsbk = (0., 1., 1., 3500)
Expand All @@ -34,29 +37,42 @@ requires Python ≥ 3.5:
batch.append(PowerOn(targets))
batch.append(SetLightFromHSBK(targets, *red_hsbk, transition_ms=transition_ms))
await asyncio.sleep(transition_ms / 1000)
click.echo("{}ms transition done".format(transition_ms))
click.echo("Restoring original state")
async with client.batch() as batch:
for b in view.bulbs:
for b in lights_state.bulbs:
PowerState = PowerOn if b.power else PowerOff
hsbk = (b.h, b.s, b.b, b.k)
batch.append(PowerState([b.label]))
batch.append(SetLightFromHSBK([b.label], *hsbk, transition_ms=transition_ms))
finally:
await client.close()
@click.command()
@click.option("--lightsd-url", help="supported schemes: tcp+jsonrpc://, unix+jsonrpc://")
@click.argument("bulb_targets", nargs=-1, required=True)
def main(lightsd_url, bulb_targets)
"""This example will turn all your bulbs to red before restoring their
def main(lightsd_url, bulb_targets):
"""This example will turn the targeted bulbs to red before restoring their
original state.
If an URL is not provided this script will attempt to connect to
lightsd's UNIX socket.
Read the `protocol docs`_ to learn how to format targets.
If an URL is not provided this script will attempt to connect to lightsd's
UNIX socket.
.. _protocol docs: protocol.html#targeting-bulbs
"""
evl = asyncio.get_event_loop()
evl.run_until_complete(evl.create_task(example(lightsd_url, bulb_targets)))
if __name__ == "__main__":
main()
.. _lightsd: https://www.lightsd.io/
.. _LIFX: http://lifx.co/

Expand Down
2 changes: 1 addition & 1 deletion clients/python/lightsc/lightsc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import locale
import logging
import os
import urllib
import urllib.parse
import uuid

from typing import (
Expand Down

0 comments on commit d827de6

Please sign in to comment.