Client

class lifx.lan.client.asynchronous.Client(tasks: Iterable[Any])

An asynchronous trivial client example

Example:

>>> import asyncio
>>> import lifx
>>> import sys
>>>
>>> async def process_responses(msg):
...     is_on = False
...     (header, body) = msg.decode()
...     if header.type == lifx.lan.Header.State.acknowledgement:
...         print("got an ack")
...     elif header.type == lifx.lan.Header.State.state_power_light:
...         is_on = body.level == body.ON
...         if is_on:
...             print("got light is powered")
...         else:
...             print("got light is not powered")
>>>
>>>
>>> async def create_datagram_endpoint():
...     loop_ = asyncio.get_event_loop()
...     transport_, protocol_ = await loop_.create_datagram_endpoint(lambda: Client([process_responses]),
...                                                                  local_addr=('0.0.0.0', 56700))
...     return transport_, protocol_
>>>
>>> loop = asyncio.get_event_loop()
>>> transport, protocol = loop.run_until_complete(loop.create_task(create_datagram_endpoint()))
>>>
>>> body = lifx.lan.light.SetPower()
>>> body.field.level = lifx.lan.light.SetPower.ON
>>> header = lifx.lan.header.make(body.state)
>>> msg_on = lifx.lan.Msg.encode(header, body, '172.31.10.245', 56700)
>>> body.field.level = lifx.lan.light.SetPower.OFF
>>> msg_off = lifx.lan.Msg.encode(header, body, '172.31.10.245', 56700)
>>> body = lifx.lan.light.GetPower()
>>> header = lifx.lan.header.make(body.state)
>>> msg_get_power = lifx.lan.Msg.encode(header, body, '172.31.10.245', 56700)
>>> loop.run_until_complete(loop.create_task(protocol.write([msg_on, msg_get_power, msg_off, msg_get_power])))
got an ack
got an ack
got light is powered
got an ack
got an ack
got light is not powered
connection_made(transport)

Called when a connection is made.

The argument is the transport representing the pipe connection. To receive data, wait for data_received() calls. When the connection is closed, connection_lost() is called.

connection_lost(exc)

Called when the connection is lost or closed.

The argument is an exception object or None (the latter meaning a regular EOF is received or the connection was aborted or closed).

error_received(exc)

Called when a send or receive operation raises an OSError.

(Other than BlockingIOError or InterruptedError.)

datagram_received(data: bytes, addr: Tuple[str, int]) None

Called when some datagram is received.

async write(msgs: Iterable[lifx.Msg])