Message

Msg

class lifx.Msg(octects: Iterable[lifx.Octect], addr: str = None, port: int = None)
classmethod from_string(s: str, addr: str = None, port: int = None) lifx.Msg
>>> import lifx
>>> s = "310000340000000000000000000000000000000000000000000000000000000066000000005555FFFFFFFFAC0D00040000"
>>> message = lifx.Msg.from_string(s, "1.1.1.1", 1)
>>> message
[0x31, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0xFF, 0xFF, 0xFF, 0xFF, 0xAC, 0x0D, 0x00, 0x04, 0x00, 0x00]
>>> message[0].nibble.high = 0x3
>>> message[0].nibble.low = 0x1
>>> message.addr
'1.1.1.1'
>>> message.port
1

Create a lifx.Msg from its string representation

Parameters
  • s – a string representation of a Lifx message

  • addr – an IP address bound to this message

  • port – an IP port bound to this message

Returns

a lifx.Msg

classmethod from_bytes(byts: bytes, addr: str = None, port: int = None) lifx.Msg
>>> import lifx
>>> bts = bytes([0xFF, 0xFE, 0xFD])
>>> message = lifx.Msg.from_bytes(bts, "1.1.1.1", 1)
>>> message
[0xFF, 0xFE, 0xFD]
>>> message[1].nibble.high = 0xF
>>> message[1].nibble.low = 0xE
>>> message.addr
'1.1.1.1'
>>> message.port
1

Create a lifx.Msg from its byte representation

Parameters
  • byts – a bytearray

  • addr – an IP address bound to this message

  • port – an IP port bound to this message

Returns

a lifx.Msg

abstract classmethod encode(header: lifx.Msg, body: lifx.Msg, addr: str = None, port: int = None) lifx.Msg
abstract decode() Tuple[Any]
property addr
property port

Octect

class lifx.Octect
>>> o = Octect(Nibbles(high=1, low=0))
>>> o.value
16
>>> o = Octect(value=45)
nibble

Structure/Union member

value

Structure/Union member

LAN

Msg

class lifx.lan.Msg(octects: Iterable[lifx.Octect], addr: str = None, port: int = None)
>>> import lifx
>>> msg = lifx.lan.Msg.from_bytes(bytearray([0x64, 0x00, 0x00, 0x54, 0x42, 0x52, 0x4B, 0x52, 0xD0, 0x73, 0xD5, 0x12, 0x1A, 0xF1, 0x00, 0x00, 0x4C, 0x49, 0x46, 0x58, 0x56, 0x32, 0x00, 0x00, 0x98, 0xFE, 0xB5, 0x2A, 0xD5, 0x77, 0x81, 0x14, 0x3B, 0x00, 0x00, 0x00, 0x4C, 0x49, 0x46, 0x58, 0xA0, 0x10, 0xB8, 0x31, 0xD5, 0x77, 0x81, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
>>> (header, body) = msg.decode()
>>> header.type
<State.echo_response: 59>
>>> msg = lifx.lan.Msg.from_bytes([0x32, 0x00, 0x00, 0x54, 0x42, 0x52, 0x4B, 0x52, 0xD0, 0x73, 0xD5, 0x12, 0x1A, 0xF1, 0x00, 0x00, 0x4C, 0x49, 0x46, 0x58, 0x56, 0x32, 0x00, 0x00, 0x98, 0xA8, 0xDC, 0x8F, 0xD6, 0x77, 0x81, 0x14, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5E, 0x0F])
>>> (header, body) = msg.decode()
>>> header.type
<State.state_host_info: 13>
>>> msg = lifx.lan.Msg.from_bytes([0x58, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x73, 0xD5, 0x12, 0x1A, 0xF1, 0x00, 0x00, 0x4C, 0x49, 0x46, 0x58, 0x56, 0x32, 0x01, 0x00, 0x08, 0x3B, 0xD6, 0xBE, 0x58, 0x3E, 0x39, 0x16, 0x6B, 0x00, 0x00, 0x00, 0x54, 0xD5, 0xBB, 0x05, 0x5B, 0xFF, 0xAC, 0x0D, 0x00, 0x00, 0xFF, 0xFF, 0x42, 0x61, 0x67, 0x6E, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
>>> (header, body) = msg.decode()
>>> str(body)
'State {power: 65535, hue: 300, saturation: 2, brightness: 100, kelvin: 3500, rgb: (255, 250, 255), label: Bagno}'
classmethod encode(header: lifx.lan.Header, body: Union[lifx.lan.light.GetService, lifx.lan.light.SetPower, lifx.lan.light.GetPower, lifx.lan.light.SetColor, lifx.lan.light.SetWaveform], addr: str = None, port: int = None) lifx.lan.Msg
>>> import lifx
>>> header = lifx.lan.header.make("set_color_light")
>>> body = lifx.lan.light.SetColor()
>>> body.field.color.rgb = (0, 255, 0)
>>> body.field.color.kelvin = 3500
>>> body.field.duration = 1024
>>> msg = lifx.lan.Msg.encode(header, body)
>>> msg
[0x31, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0xFF, 0xFF, 0xFF, 0xFE, 0xAC, 0x0D, 0x00, 0x04, 0x00, 0x00]
Parameters
  • header – a lifx.lan.Header

  • body – a lifx lan payload

  • addr – an ip address to associate the message with

  • port – an ip port to associate the message with

Returns

a lifx.lan.Msg

decode() Tuple[lifx.lan.header.Header, Union[lifx.lan.light.StateService, lifx.lan.light.StatePower, lifx.lan.light.State]]
>>> import lifx
>>> s = "310000340000000000000000000000000000000000000000000000000000000066000000005555FFFFFFFFAC0D00040000"
>>> msg = lifx.lan.Msg.from_string(s)
>>> (header, body) = msg.decode()
>>> header.type
<State.set_color_light: 102>
>>> header.field.size
49
>>> header.field.protocol
1024
>>> header.field.type
102
>>> s = str(body)
>>> 'SetColor' in s
True
Returns

a tuple (header, body)