Comments (3)
@rytilahti I've decided to try and port the library to asyncio
. I can say that I already have a working communication based on asyncio
's DatagramProtocol
. I've implemented two DatagramProtocol
subclasses: MIIOProtocol
and MIIODiscoverProtocol
.
The MIIODiscoverProtocol
implements the handshake/discovery logic. Like in the current implementation, it can work either with broadcast and wait for multiple responses (it stores the results in asyncio.Queue
so external asyncio
code can consume the results on the fly, but also a callback callable can be provided which is called on each response) or with a single host and returns the response right away.
The MIIOProtocol
implements the request/response logic with timeouts and retries. It supports parallel requests (for example I can send two get_prop requests without waiting for first one to return before requesting the other set of properties). This class also uses the MIIODiscoveryProtocol
to do the handshake, but I've implemented it so that it doesn't do handshake before every request, but works in a loop with some sleep time (I'm using 60 seconds right now). I'm using an asyncio.Event
instance to make sure that the request can't be made before first successful handshake and then during every next scheduled one (the send coroutine waits on the Event
object to be set).
I haven't modified any other code or class in the library yet. I've been able to successfully reuse the construct schema. But I didn't touch any devices' code yet. I'm testing directly on my new classes.
Also something needs to be done with the zeroconf
library. The one currently in use doesn't support asyncio
. I've found aiozeroconf
which is a direct port of the zeroconf
for asyncio
.
When I'm happy with the code I'll push this as a branch to my fork, but I probably won't create a PR right away. I'll post it here so it can be decided what's next.
from python-miio.
I think this is really good idea. But I wonder about doing asyncio on top of the current implementation (if I understood this correctly). Doing it this way would require running some parts in the executor, missing the best asyncio has to offer - asynchronous, non-blocking sockets and other IO operations.
I think the only sensible solution is going fully asyncio and then add a compatibility layer so it can still be used from non-asyncio applications.
Dropping support for python 3.4 may seem like a hard decision to do, but asyncio in py34 based on decorators and generators is really ugly and hard to read when compared to async/await syntax in py35+. Also Home-Assistant already declared support py34 as deprecated last year and is going to drop it soon.
from python-miio.
This would indeed require quite a bit of refactoring, and would also be done while dropping the support for 3.4 (with what I'm okay with). The code-base itself should be converted to be purely asyncio, synchronous interfaces could be added if/where needed, the cli tool could be managed similarly to what's done in https://github.com/rytilahti/python-songpal/blob/master/songpal/main.py .
from python-miio.
Related Issues (20)
- Unsupported Model: Xiaomi Air Purifier 4 Lite (zhimi.airp.rma2) HOT 1
- Improve handling of unexpected device responses
- v6: Multiple Roborock devices swap attributes HOT 2
- Unsupported Aqara Double Switch
- xiaomi vacuum cleaner s12 HOT 2
- Roborock S8 - roborock.vacuum.a51 HOT 1
- Roborock S8 is roborock.vacuum.a51
- Request new Vacuum Mijia Robot Vacuum M30 Pro HOT 1
- Xiaomi trouver lds finder doesn't work HOT 3
- No set_voice implemented for dreame.vacuum.r2205
- The response time to get the current power usage varies a lot HOT 5
- ERR: miiocli genericmiot --ip 192.168.5.x --token xxxx status HOT 10
- O puedo conectar al wi fi mi xiamo home
- Question about how to Set Voice dreame.vacuum.r2211o
- Unable to get status from fanp5 using the genericmiot command HOT 2
- Unsupported model 'roborock.vacuum.a62'
- get_properties of xiaomi.vacuum.c107 timeout falsely if max_properties >= 4 when get property `vacuum-map:clean-record`
- Found an unsupported model 'roborock.vacuum.a70'
- Roidmi Eva (roidmi.vacuum.v66) with genericmiot HOT 3
- Found an unsupported model 'lumi.acpartner.v2'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-miio.