Skip to main content

A python3 library able to encode/decode Lifx (lan) messages

Project description

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.

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

lifx-lib-0.9.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

lifx_lib-0.9.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file lifx-lib-0.9.1.tar.gz.

File metadata

  • Download URL: lifx-lib-0.9.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for lifx-lib-0.9.1.tar.gz
Algorithm Hash digest
SHA256 7b90bbdfbf897f62870b264b9212f1d27cad0b5b413121755846d36974f24151
MD5 f56b224d619b3165369e8c224a473c3c
BLAKE2b-256 567a20f0a784044b21656c5c9314c85fadc1c5de55da16d8f1d3b5673dcc85fb

See more details on using hashes here.

File details

Details for the file lifx_lib-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: lifx_lib-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for lifx_lib-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 15cbffdc8aa27c55a0bbd3845ef9c66e185544b4ca387017d173fc3b69129257
MD5 76652507eeacd0401e909d6a49a34f82
BLAKE2b-256 8143ce9d170ec03f8f53afc0ccd37d34bfbdb6ba081888a3a572d45d77283d46

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