Skip to main content

API for local communication with Yeelight devices over a LAN with asyncio.

Project description

aioxiaomi

aioxiaomi is a Python 3/asyncio library to control Xiaomi Yeelight LED lightbulbs over your LAN.

PyPI version fury.io MIT license GITHUB-BADGE

Installation

We are on PyPi so

 pip3 install aioxiaomi

or python3 -m pip install aioyeelight

Encryption Key

THis library uses the MIHome binary protocol as described by OpenMiHome This means you must acquire the envryption key that is generated during provisioning.

The easiest way is to provision the bulbs with aioiotprov.

How to use

Essentially, you create an object with at least 2 methods:

- register
- unregister

You then use start_yeelight_discovery, to search for light bulbs with a callback that will create and .activate() any new bulb. Upon connection with the bulb, it will register itself with the parent. All the method communicating with the bulb can be passed a callback function to react to the bulb response. The callback should take 1 parameters:

- the response message

Checkout main.py to see how it works.

In essence, the test program is this

tokenlist = { <mac>: <secret token>}
class bulbs():
""" A simple class with a register and  unregister methods
"""
    def __init__(self):
        self.bulbs=[]
        self.pending_bulbs = []

    def register(self,bulb):
        self.bulbs.append(bulb)
        try:
            self.pending_bulbs.remove(bulb)
        except:
            pass

    def unregister(self,bulb):
        idx=0
        for x in list([ y.bulb_id for y in self.bulbs]):
            if x == bulb.bulb_id:
                del(self.bulbs[idx])
                break
            idx+=1

    def new_bulb(self, info):
        if "light" in info["name"] and info["mac"] in tokenlist:
            newbulb = aiox.YeelightBulb(
                aio.get_event_loop(),
                tokenlist[info["mac"]],
                info["address"],
                info["mac"],
                self,
                )
        found = False
        for x in self.bulbs:
            if x.bulb_id == newbulb.bulb_id:
                found = True
                break
        if not found:
            for x in self.pending_bulbs:
                if x.bulb_id == newbulb.bulb_id:
                    found = True
                    break
        if not found:
            newbulb.activate()
        else:
            del(newbulb)


def readin():
"""Reading from stdin and displaying menu"""

    selection = sys.stdin.readline().strip("\n")
    DoSomething()

MyBulbs= bulbs()
loop = aio.get_event_loop()
zc, browser = aiox.start_yeelight_discovery(MyBulbs.new_bulb)
try:
    loop.add_reader(sys.stdin,readin)
    loop.run_forever()
except:
    pass
finally:
    browser.cancel()
    loop.run_until_complete(zc.close())
    MyBulbs.close()
    loop.remove_reader(sys.stdin)
    loop.close()

Other things worth noting:

  • Discovery is done using aiozeroconf

  • Yeelights allows only about 1 command per second per connection. To counter that,one can start more than one connection to a bulb. There is a limit of 4 connections per bulb, but given that there can only be 144 command per minute per bulb, only 2 connections can be handled without starting to overload the bulb. Use .set_connection(x) before activate to set the number of connections

  • aioyeelight ensure that there is at most 1 command per second per connection. To do so it keeps a buffer of messages and pace the sending (using round-robin if there is more then one connection). The buffer can thus become quite big.

  • aioyeelight will ping a bulb with a 'hello' message. This appears to be necessary for the bulb to keep responding.

  • I only have "Color" model, so I could not test with other types of bulbs

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

aioyeelight-0.1.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

aioyeelight-0.1.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file aioyeelight-0.1.0.tar.gz.

File metadata

  • Download URL: aioyeelight-0.1.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5

File hashes

Hashes for aioyeelight-0.1.0.tar.gz
Algorithm Hash digest
SHA256 142ff75df2fa7baf342c9607d427fdcfc689142b00c67eb0897e278b73ce6774
MD5 655db264c675cfca986ce57692477034
BLAKE2b-256 970dffc45bcffd28470c550e437396b3d327168a55878520833034255e8a1c00

See more details on using hashes here.

File details

Details for the file aioyeelight-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aioyeelight-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5

File hashes

Hashes for aioyeelight-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 69d3ea02a55b877b5d8910437c1b899bda06010c08f31c79e25413b177ca83ce
MD5 f836f94cc9946df2d49a7f8049b4f405
BLAKE2b-256 34f18384a6f4aa06ac6427a91656b5f628779979949cd8a4b63e75afbd281327

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