API for local communication with Xiaomi 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.
[](https://pypi.pyt hon.org/pypi/aioiotprov) [](https://lbesson .mit-licen) [](h ttps://github.com/psf/black)
Installation
We are on PyPi so
pip3 install aioxiaomi
or python3 -m pip install aioxiaomi
How to use
Essentially, you create an object with at least 2 methods:
- register
- unregister
You then start the XiaomiDiscovery task in asyncio 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 2 parameters:
- a light object
- the response message
Checkout main.py to see how it works.
In essence, the test program is this
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, sender, **kwargs):
newbulb = aiox.XiaomiBulb(aio.get_event_loop(),kwargs['headers'],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()
coro = aiox.start_xiaomi_discovery(MyBulbs.new_bulb)
transp, server = loop.run_until_complete(coro)
try:
loop.add_reader(sys.stdin,readin)
server.broadcast(2)
loop.run_forever()
except:
pass
finally:
server.cancel()
loop.remove_reader(sys.stdin)
loop.close()
Other things worth noting:
- Whilst XiaomiDiscover uses UDP broadcast, the bulbs are
connected with Unicast TCP
- Xiaomi 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
- aioxiaomi 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. To control this, one can
specify a maximum buffer length and what to do with messages that comes when the buffer
is full. Use set_queue_limit(length,policy) to control.
length is the maximum number of commands waiting to be sent
policy defines what to do with the extra packets:
drop: just drop them
head: queue them but discard the head of the queue
random: queue the message then discard a random element of the queue
adapt: switch to the so-called "music mode" and dump all the messages.
After 5 secs inactivity, the "music mode" is cancelled
- The socket connecting to a bulb is not closed unless the bulb is deemed to have
gone the way of the Dodo.
- 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
Built Distribution
File details
Details for the file aioxiaomi-0.1.5.tar.gz
.
File metadata
- Download URL: aioxiaomi-0.1.5.tar.gz
- Upload date:
- Size: 17.6 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.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2284f674d5722deea24e9cdaaf7775f131a8af09999f00c498553df20473b95e |
|
MD5 | fbfe66101ec0ec28d2b4f6989a674d49 |
|
BLAKE2b-256 | 1a3f70add5561d755f626901317806661f8ace64e667f2575e45213073ae0d78 |
File details
Details for the file aioxiaomi-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: aioxiaomi-0.1.5-py3-none-any.whl
- Upload date:
- Size: 17.8 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.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f749e3ec9836c460bf99049c5c9c17a787d094ed0a56df25e32239c92c9c2bb6 |
|
MD5 | 6049ee62585025a686ee99258e41fafc |
|
BLAKE2b-256 | e95e91138a1d003d070a2b638cce7cbf16864eaba419371c3707e39c383a2cb6 |