Skip to content
This repository was archived by the owner on Nov 19, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project are recorded here. The format follows
Keep a Changelog; versions follow Semantic Versioning.

## Unreleased

### Added

- The LIFAair LM05 air purifier (0x4EC2) is supported as `lifaair`:
station readings (CO2, TVOC, formaldehyde, PM1, PM2.5, PM10), the
purifier's temperature and humidity, and fan mode and speed, with
matching CLI options. Each request uses a new UDP source port.
Contributed by pit3k.

## 1.3.0 - 2026-11-16

### Added
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The following devices are supported:
- **Curtain motors**: Dooya DT360E-45/20
- **Thermostats**: Hysen HY02/HY03
- **Hubs**: S3
- **Air purifiers**: LIFAair LM05

## Timing

Expand Down Expand Up @@ -428,6 +429,21 @@ await devices[0].set_state(bulb_colormode=1)
data = await device.check_sensors()
```

## Air purifiers

### Fetching purifier state
```python3
from broadlink_async import lifaair

data = await device.get_state()
```

### Controlling purifier fan
```python3
await device.set_fan_mode(lifaair.FanMode.TURBO)
await device.set_fan_speed(50)
```

## Hubs

### Discovering subdevices
Expand Down
4 changes: 4 additions & 0 deletions broadlink_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .device import Device, ping, scan, send_setup_packet
from .hub import s3
from .light import fl, lb1, lb2
from .purifier import lifaair
from .remote import rm, rm4, rm4mini, rm4pro, rm5plus, rmmini, rmminib, rmpro
from .sensor import a1, a2
from .switch import bg1, ehc31, mp1, mp1s, sp1, sp2, sp2s, sp3, sp3s, sp4, sp4b
Expand Down Expand Up @@ -226,6 +227,9 @@
ehc31: {
0x6480: ("EHC31", "BG Electrical"),
},
lifaair: {
0x4EC2: ("LM05", "LIFAair"),
},
}


Expand Down
127 changes: 68 additions & 59 deletions broadlink_async/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ class Device:

TYPE = "Unknown"

# Close the UDP endpoint after every request so the next one binds a new port.
_fresh_endpoint_per_request = False

__INIT_KEY = "097628343fe99e23765c1513accf8b02"
__INIT_VECT = "562e17996d093d28ddb3ba695a2e6f58"

Expand Down Expand Up @@ -562,71 +565,77 @@ async def _exchange(self, packet: bytes) -> bytes:
device has not used recently is accepted, for firmware that may not
echo it.
"""
transport, protocol = await self._endpoint()
protocol.drain()
loop = asyncio.get_running_loop()
start = loop.time()
timeout = self.timeout
count = int.from_bytes(packet[0x28:0x2A], "little")
self._recent.append(count)
sends = 0

while True:
transport.sendto(packet)
sends += 1
if sends > 1:
_LOGGER.debug("%s: no reply, resending (%d)", self.host[0], sends)
resend_at = loop.time() + DEFAULT_RETRY_INTVL
try:
transport, protocol = await self._endpoint()
protocol.drain()
loop = asyncio.get_running_loop()
start = loop.time()
timeout = self.timeout
count = int.from_bytes(packet[0x28:0x2A], "little")
self._recent.append(count)
sends = 0

while True:
now = loop.time()
if now - start >= timeout:
break
wait = min(resend_at, start + timeout) - now
try:
resp, _ = await asyncio.wait_for(protocol.queue.get(), max(wait, 0))
except TimeoutError:
if loop.time() - start >= timeout:
transport.sendto(packet)
sends += 1
if sends > 1:
_LOGGER.debug("%s: no reply, resending (%d)", self.host[0], sends)
resend_at = loop.time() + DEFAULT_RETRY_INTVL
while True:
now = loop.time()
if now - start >= timeout:
break
if loop.time() >= resend_at:
break # Resend.
continue
if resp is None:
raise e.EndpointClosedError(
-4013, "Endpoint closed", "The device endpoint was closed"
)
if _is_silence(resp):
# ICMP unreachable of one kind or another: the host is up
# with nothing listening, the device is off or rebooting,
# or a router answered for it. The original library's
# unconnected socket never saw these, so keep waiting and
# let the timeout decide, as it did.
wait = min(resend_at, start + timeout) - now
try:
resp, _ = await asyncio.wait_for(
protocol.queue.get(), max(wait, 0)
)
except TimeoutError:
if loop.time() - start >= timeout:
break
if loop.time() >= resend_at:
break # Resend.
continue
if resp is None:
raise e.EndpointClosedError(
-4013, "Endpoint closed", "The device endpoint was closed"
)
if _is_silence(resp):
# ICMP unreachable of one kind or another: the host is up
# with nothing listening, the device is off or rebooting,
# or a router answered for it. The original library's
# unconnected socket never saw these, so keep waiting and
# let the timeout decide, as it did.
_LOGGER.debug(
"%s: unreachable (%s), still waiting", self.host[0], resp
)
continue
if isinstance(resp, Exception):
# A send failure (no route, address gone) or a fatal
# transport error: fail now and throw the socket away.
_LOGGER.debug("%s: socket error: %s", self.host[0], resp)
self._drop_endpoint()
raise resp
resp = self._validate(resp)
reply_count = int.from_bytes(resp[0x28:0x2A], "little")
if reply_count == count or reply_count not in self._recent:
return resp
_LOGGER.debug(
"%s: unreachable (%s), still waiting", self.host[0], resp
"%s: dropped a reply for an earlier request (counter 0x%04x)",
self.host[0],
reply_count,
)
continue
if isinstance(resp, Exception):
# A send failure (no route, address gone) or a fatal
# transport error: fail now and throw the socket away.
_LOGGER.debug("%s: socket error: %s", self.host[0], resp)
if loop.time() - start >= timeout:
_LOGGER.debug("%s: no reply within %ss", self.host[0], timeout)
self._drop_endpoint()
raise resp
resp = self._validate(resp)
reply_count = int.from_bytes(resp[0x28:0x2A], "little")
if reply_count == count or reply_count not in self._recent:
return resp
_LOGGER.debug(
"%s: dropped a reply for an earlier request (counter 0x%04x)",
self.host[0],
reply_count,
)
if loop.time() - start >= timeout:
_LOGGER.debug("%s: no reply within %ss", self.host[0], timeout)
raise e.NetworkTimeoutError(
-4000,
"Network timeout",
f"No response received within {timeout}s",
) from None
finally:
if self._fresh_endpoint_per_request:
self._drop_endpoint()
raise e.NetworkTimeoutError(
-4000,
"Network timeout",
f"No response received within {timeout}s",
) from None

async def send_packet(self, packet_type: int, payload: bytes | bytearray) -> bytes:
"""Send a packet to the device and return the raw response frame.
Expand Down
Loading
Loading