Skip to main content

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

Metadata

Release files for aioyeelight 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioyeelight 0.1.0
File Size Uploaded
aioyeelight-0.1.0.tar.gz 20.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aioyeelight 0.1.0
File Interpreter ABI Platform
aioyeelight-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 41.9 kB

Release files / aioyeelight-0.1.0.tar.gz

Download URL aioyeelight-0.1.0.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
142ff75df2fa7baf342c9607d427fdcfc689142b00c67eb0897e278b73ce6774
BLAKE2b-256 checksum
How to use checksums
970dffc45bcffd28470c550e437396b3d327168a55878520833034255e8a1c00
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / aioyeelight-0.1.0-py3-none-any.whl

Download URL aioyeelight-0.1.0-py3-none-any.whl
Size 21.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69d3ea02a55b877b5d8910437c1b899bda06010c08f31c79e25413b177ca83ce
BLAKE2b-256 checksum
How to use checksums
34f18384a6f4aa06ac6427a91656b5f628779979949cd8a4b63e75afbd281327
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page