Skip to content

majamassarini/lifx-lib

Repository files navigation

lifx-lib

Build Status codecov Documentation Status

A Python 3 library able to encode/decode Lifx (lan) messages.

It can be used with an asynchronous or synchronous client.

Examples

Encode

    >>> import lifx
    >>> body = lifx.lan.light.SetColor()
    >>> header = lifx.lan.header.make(body.state)
    >>> body.rgb = (0, 255, 0)
    >>> body.kelvin = 3500
    >>> body.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, 0x01, 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]

Encode from a dictionary

   >>> import lifx
   >>> dictionary = {"hue": 335, "saturation": 90, "brightness": 90, "kelvin": 3500, "duration": 1000,
   ...               "transient": True, "period": 180000, "cycles": 30, "skew_ratio": 0.5, "waveform": "sine"}
   >>> body = lifx.lan.light.State_Factory.make("SetWaveform", dictionary)
   >>> header = lifx.lan.header.make(body.state)
   >>> msg = lifx.lan.Msg.encode(header, body)
   >>> msg
   [0x39, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x01, 0x38, 0xEE, 0x66, 0xE6, 0x66, 0xE6, 0xAC, 0x0D, 0x20, 0xBF, 0x02, 0x00, 0x00, 0x00, 0xF0, 0x41, 0x00, 0x00, 0x01]

Decode

    >>> import lifx
    >>> msg = lifx.lan.Msg.from_bytes([0x58, 0x00, 0x00, 0x54, 0xB9, 0x71, 0x5D, 0x07, 0xD0, 0x73, 0xD5, 0x12, 0x1A, 0xF1, 0x00, 0x00, 0x4C, 0x49, 0x46, 0x58, 0x56, 0x32, 0x00, 0x4D, 0x18, 0x52, 0x42, 0x1E, 0xB5, 0xFC, 0x82, 0x14, 0x6B, 0x00, 0x00, 0x00, 0x71, 0x7E, 0xCC, 0x4C, 0x09, 0x57, 0xAC, 0x0D, 0x00, 0x00, 0xFF, 0xFF, 0x4C, 0x49, 0x46, 0x58, 0x20, 0x42, 0x75, 0x6C, 0x62, 0x20, 0x31, 0x32, 0x31, 0x61, 0x66, 0x31, 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.state_light: 107>
    >>> body.kelvin
    3500
    >>> body.rgb
    (61, 87, 86)
    >>> str(body)
    'State {power: 65535, hue: 178, saturation: 30, brightness: 34, kelvin: 3500, rgb: (61, 87, 86), label: LIFX Bulb 121af1}'

Decode to a dictionary

    >>> import lifx
    >>> msg = lifx.lan.Msg.from_bytes([0x39, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x01, 0x38, 0xEE, 0x66, 0xE6, 0x66, 0xE6, 0xAC, 0x0D, 0x20, 0xBF, 0x02, 0x00, 0x00, 0x00, 0xF0, 0x41, 0x00, 0x00, 0x01])
    >>> (header, body) = msg.decode()
    >>> header.type
    <State.set_waveform_light: 103>
    >>> factory = lifx.lan.light.Description_Factory()
    >>> s = factory.make(body)
    >>> s[0]
    'SetWaveform'
    >>> s[1]['waveform']
    'sine'

Getting Started

pip install lifx-lib

Diving In

Documentation

Contributing

Pull requests are welcome!.

License

lifx-lib is licensed under the MIT license.