Make the library asynchronous - #2
Merged
Merged
Conversation
Every method that reaches a device is now a coroutine, with the same names, arguments and return values as before. Discovery, hello and setup are coroutines and xdiscover is an async generator. The packet, CRC and datetime helpers stay synchronous. There is no synchronous compatibility layer. Transport: each device keeps one UDP endpoint (asyncio DatagramProtocol) for its lifetime and serializes requests on it with an asyncio.Lock; the previous code opened a socket per call and declared a lock it never acquired. Retry and timeout behaviour is unchanged. An expired session key is re-authenticated once and the request repeated. async with / aclose() release the endpoint. Device classes are a mechanical port (async def and await); the oracle suite recorded in the previous change passes unchanged, so every method sends the same bytes and decodes the same results as 0.19.0. Transport tests use a fake endpoint and gain cases for lock serialization, endpoint reuse, stale-reply draining and re-auth. The CLI runs under asyncio.run. README and CHANGELOG describe the break. Live-checked against an RM4 Pro: discovery, hello, auth, sensors, concurrent calls, learning primitives, send, and the timeout path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The reason this fork exists: Home Assistant wants an asynchronous Broadlink library. This is the port. Method names, arguments and return values are unchanged; every call that reaches a device is now a coroutine.
Proposed change
Device.send_packet,auth,hello,ping,get_fwversion,set_name,set_lockand every public method on every device class areasync def.broadlink.discover,helloandsetupare coroutines;xdiscoveris an async generator.pulses_to_data,data_to_pulses,CRC16andDatetimestay synchronous.asyncio.DatagramProtocolendpoint per device, opened on first use and kept; requests serialized with anasyncio.Lock. The old code opened a socket per call and itswith self.lock and socket.socket(...)never acquired the lock. Retry every second untiltimeout, thenNetworkTimeoutError, exactly as before. Stale datagrams are drained before each request.async with device:orawait device.aclose()releases the endpoint.dooya.set_percentage_and_waitusesasyncio.sleep.asyncio.run.Verification
tests/oracle/fixtures.jsonuntouched: all 155 cases send byte-identical requests and decode identical results to 0.19.0. That is the equivalence proof for the device classes, including the ones no one here can bench.gather), endpoint reuse,acloseand reopen, stale-reply draining, re-auth once, and no second re-auth.create_datagram_endpointneeds an explicit address family for the unbound case.hello21 ms,auth36 ms, firmware, sensors, three concurrent calls in 65 ms,enter_learning/check_data,send_data, LAN discovery, timeout on a dead address, and endpoint close.Type of change
Additional information
Checklist
pytest).ruff check .passes.CHANGELOG.mdhas an entry under Unreleased.