python-broadlink 1.0.4__py3-none-any.whl → 1.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
broadlink/__init__.py CHANGED
@@ -116,6 +116,9 @@ SUPPORTED_TYPES = {
116
116
  0x27A6: ("RM plus", "Broadlink"),
117
117
  0x27A9: ("RM pro+", "Broadlink"),
118
118
  0x27C3: ("RM pro+", "Broadlink"),
119
+ # Mapping follows the diff in upstream #838 (its text says rm4pro,
120
+ # its diff says rmpro). Testers in that thread reported the device
121
+ # answering "locked" on auth, so treat it as reported, not confirmed.
119
122
  0xAF8B: ("RM Max", "Broadlink"),
120
123
  },
121
124
  rmminib: {
@@ -281,12 +284,9 @@ async def discover(
281
284
  discover_ip_port: int = DEFAULT_PORT,
282
285
  ) -> list[Device]:
283
286
  """Discover devices connected to the local network."""
284
- return [
285
- device
286
- async for device in xdiscover(
287
- timeout, local_ip_address, discover_ip_address, discover_ip_port
288
- )
289
- ]
287
+ devices = xdiscover(timeout, local_ip_address, discover_ip_address, discover_ip_port)
288
+ async with contextlib.aclosing(devices):
289
+ return [device async for device in devices]
290
290
 
291
291
 
292
292
  async def xdiscover(
broadlink/device.py CHANGED
@@ -12,6 +12,7 @@ from __future__ import annotations
12
12
  import asyncio
13
13
  import collections
14
14
  import contextlib
15
+ import errno
15
16
  import logging
16
17
  import random
17
18
  import socket
@@ -53,6 +54,22 @@ _QueueItem = tuple[bytes | Exception | None, tuple[str, int] | None]
53
54
  error the socket reported (address ``None``), or ``_CLOSED``."""
54
55
 
55
56
 
57
+ def _is_silence(item: object) -> bool:
58
+ """True for the socket errors that mean "no device answered".
59
+
60
+ A connected datagram socket learns from ICMP that nobody is listening
61
+ (port unreachable, ``ConnectionRefusedError``; ``ConnectionResetError``
62
+ on Windows) or that the host cannot be reached (``EHOSTUNREACH``, from
63
+ a router answering for a host that is off). The original library used
64
+ an unconnected socket that never received any of these and simply
65
+ timed out, and callers such as Home Assistant treat a timeout more
66
+ leniently than an ``OSError``, so these are treated as silence.
67
+ """
68
+ if isinstance(item, ConnectionRefusedError | ConnectionResetError):
69
+ return True
70
+ return isinstance(item, OSError) and item.errno == errno.EHOSTUNREACH
71
+
72
+
56
73
  class _Protocol(asyncio.DatagramProtocol):
57
74
  """Datagram protocol that hands every received packet to a queue.
58
75
 
@@ -103,7 +120,14 @@ async def _open_endpoint(
103
120
  remote_addr: tuple[str, int] | None = None,
104
121
  broadcast: bool = False,
105
122
  ) -> tuple[asyncio.DatagramTransport, _Protocol]:
106
- """Create a UDP endpoint. Tests replace this to fake the network."""
123
+ """Create a UDP endpoint. Tests replace this to fake the network.
124
+
125
+ An endpoint with no address on either side is bound to ``0.0.0.0``
126
+ explicitly; the proactor loop on Windows starts receiving as soon as the
127
+ endpoint exists, which needs a bound socket.
128
+ """
129
+ if local_addr is None and remote_addr is None:
130
+ local_addr = ("0.0.0.0", 0)
107
131
  loop = asyncio.get_running_loop()
108
132
  transport, protocol = await loop.create_datagram_endpoint(
109
133
  _Protocol,
@@ -570,12 +594,15 @@ class Device:
570
594
  raise e.EndpointClosedError(
571
595
  -4013, "Endpoint closed", "The device endpoint was closed"
572
596
  )
573
- if isinstance(resp, ConnectionRefusedError):
574
- # ICMP port unreachable: the host is up and nothing is
575
- # listening, or the device is rebooting. The original
576
- # library's unconnected socket never saw these, so keep
577
- # waiting and let the timeout decide, as it did.
578
- _LOGGER.debug("%s: port unreachable, still waiting", self.host[0])
597
+ if _is_silence(resp):
598
+ # ICMP unreachable of one kind or another: the host is up
599
+ # with nothing listening, the device is off or rebooting,
600
+ # or a router answered for it. The original library's
601
+ # unconnected socket never saw these, so keep waiting and
602
+ # let the timeout decide, as it did.
603
+ _LOGGER.debug(
604
+ "%s: unreachable (%s), still waiting", self.host[0], resp
605
+ )
579
606
  continue
580
607
  if isinstance(resp, Exception):
581
608
  # A send failure (no route, address gone) or a fatal
@@ -623,6 +650,11 @@ class Device:
623
650
  if self._auth_generation == generation:
624
651
  try:
625
652
  await self.auth()
653
+ except (e.NetworkTimeoutError, e.EndpointClosedError):
654
+ # A network failure during re-authentication is
655
+ # reported as what it is, not as the device's
656
+ # original expired-key answer.
657
+ raise
626
658
  except e.BroadlinkException as err:
627
659
  _LOGGER.debug(
628
660
  "%s: re-authentication failed: %s", self.host[0], err
broadlink/remote.py CHANGED
@@ -1,11 +1,11 @@
1
1
  """Support for universal remotes."""
2
2
 
3
3
  import asyncio
4
+ import contextlib
4
5
  import enum
5
6
  import logging
6
7
  import struct
7
8
  import time
8
- import weakref
9
9
  from collections.abc import AsyncGenerator, Awaitable, Callable
10
10
  from dataclasses import dataclass, field
11
11
  from typing import Self
@@ -221,51 +221,39 @@ class rmmini(Device):
221
221
  # against the value it saw when it armed the device and re-arms
222
222
  # after any send, since the device has one front end for both.
223
223
  self._tx_generation = 0
224
- # Weak reference to the async generator of the current capture
225
- # window, if any. See _claim_window.
226
- self._window: weakref.ReferenceType | None = None
224
+ # True while a capture window holds the device's receiver. Set by
225
+ # the window on its first iteration, cleared when its generator
226
+ # finishes or is closed, including by asyncio's finalizer.
227
+ self._capturing = False
227
228
 
228
229
  @property
229
230
  def capture_active(self) -> bool:
230
231
  """True while a capture window is open on this device."""
231
- window = self._window() if self._window is not None else None
232
- return window is not None and window.ag_frame is not None
232
+ return self._capturing
233
233
 
234
- def _check_window(self) -> None:
235
- """Fail fast at call time if another window is being iterated now."""
236
- old = self._window() if self._window is not None else None
237
- if old is not None and old.ag_frame is not None and old.ag_running:
238
- raise e.CaptureInProgressError("A capture window is already open")
239
-
240
- async def _claim_window(self, new: weakref.ReferenceType) -> None:
241
- """Make sure the previous window is really gone, then register ``new``.
234
+ async def _claim_window(self) -> None:
235
+ """Take the receiver for a new window, or refuse.
242
236
 
243
237
  A consumer that walked away from a window without closing it (for
244
238
  example ``break`` out of ``async for`` with no ``aclosing``) leaves
245
239
  the generator to asyncio's finalizer, which closes it on the next
246
- loop iteration once nothing references it. Give that a turn. If the
247
- window is still alive after that, someone still holds it, whether
248
- they are inside ``__anext__`` or paused between signals, and the new
249
- window is refused rather than taken from under them.
240
+ loop iteration once nothing references it, and closing it releases
241
+ the claim. Give that a turn. If the claim is still held after that,
242
+ someone still has the window, whether inside ``__anext__`` or
243
+ paused between signals, and the new window is refused rather than
244
+ taken from under them.
250
245
  """
251
- prev = self._window
252
- if prev is not None:
253
- old = prev()
254
- if old is not None and old.ag_frame is not None:
255
- if old.ag_running:
256
- raise e.CaptureInProgressError("A capture window is already open")
257
- del old # Hold no reference while the finalizer gets its turn.
258
- await asyncio.sleep(0)
259
- await asyncio.sleep(0)
260
- old = prev()
261
- if old is not None and old.ag_frame is not None:
262
- raise e.CaptureInProgressError(
263
- "A capture window is already open; close it with aclose() first"
264
- )
265
- if self._window is not prev:
266
- # Another claimant got in during the two turns above.
267
- raise e.CaptureInProgressError("A capture window is already open")
268
- self._window = new
246
+ if self._capturing:
247
+ await asyncio.sleep(0)
248
+ await asyncio.sleep(0)
249
+ if self._capturing:
250
+ raise e.CaptureInProgressError(
251
+ "A capture window is already open; close it with aclose() first"
252
+ )
253
+ self._capturing = True
254
+
255
+ def _release_window(self) -> None:
256
+ self._capturing = False
269
257
 
270
258
  async def _send(self, command: int, data: bytes = b"") -> bytes:
271
259
  """Send a packet to the device."""
@@ -319,13 +307,11 @@ class rmmini(Device):
319
307
  Use ``contextlib.aclosing`` (or iterate to the end) so the window is
320
308
  released promptly. Only one capture window can be open per device:
321
309
  opening one while another is still held raises
322
- ``CaptureInProgressError``. A window whose generator was dropped
323
- without being closed is finalized by asyncio on the next loop
324
- iteration and does not block.
310
+ ``CaptureInProgressError`` on its first iteration. A window whose
311
+ generator was dropped without being closed is finalized by asyncio
312
+ on the next loop iteration and does not block.
325
313
  """
326
- self._check_window()
327
- holder: list = []
328
- gen = self._capture_loop(
314
+ return self._capture_loop(
329
315
  self.enter_learning,
330
316
  window,
331
317
  stop_after_first,
@@ -333,10 +319,8 @@ class rmmini(Device):
333
319
  rearm_interval,
334
320
  SignalKind.IR,
335
321
  None,
336
- claim=holder,
322
+ claim=True,
337
323
  )
338
- holder.append(weakref.ref(gen))
339
- return gen
340
324
 
341
325
  async def _capture_loop(
342
326
  self,
@@ -348,18 +332,43 @@ class rmmini(Device):
348
332
  kind: SignalKind,
349
333
  frequency_mhz: float | None,
350
334
  *,
351
- claim: list | None = None,
335
+ claim: bool,
352
336
  ) -> AsyncGenerator[CapturedSignal]:
353
- # ``claim`` carries a weak reference to this generator (filled in by
354
- # the caller after creating it); None means the caller owns the
355
- # window claim, as capture_rf does for its inner loop.
337
+ # ``claim`` is False when the caller already holds the window, as
338
+ # capture_rf does for its inner loop.
356
339
  if window < 0:
357
340
  raise ValueError("window must be 0 (open-ended) or positive")
358
341
  if poll_interval <= 0 or rearm_interval <= 0:
359
342
  raise ValueError("poll_interval and rearm_interval must be positive")
360
343
  if claim:
361
- await self._claim_window(claim[0])
344
+ await self._claim_window()
345
+ try:
346
+ body = self._capture_body(
347
+ arm,
348
+ window,
349
+ stop_after_first,
350
+ poll_interval,
351
+ rearm_interval,
352
+ kind,
353
+ frequency_mhz,
354
+ )
355
+ async with contextlib.aclosing(body):
356
+ async for signal in body:
357
+ yield signal
358
+ finally:
359
+ if claim:
360
+ self._release_window()
362
361
 
362
+ async def _capture_body(
363
+ self,
364
+ arm: Callable[[], Awaitable[None]],
365
+ window: float,
366
+ stop_after_first: bool,
367
+ poll_interval: float,
368
+ rearm_interval: float,
369
+ kind: SignalKind,
370
+ frequency_mhz: float | None,
371
+ ) -> AsyncGenerator[CapturedSignal]:
363
372
  loop = asyncio.get_running_loop()
364
373
  deadline = loop.time() + window if window else None
365
374
  timeouts = 0
@@ -399,7 +408,7 @@ class rmmini(Device):
399
408
  except ValueError as err:
400
409
  # A packet the device returned but we cannot decode. Log
401
410
  # it, re-arm and keep the window open.
402
- _LOGGER.warning(
411
+ _LOGGER.debug(
403
412
  "%s: ignoring an undecodable capture (%s): %s",
404
413
  self.host[0],
405
414
  err,
@@ -475,18 +484,9 @@ class rmpro(rmmini):
475
484
  Each ``CapturedSignal`` carries the carrier in ``frequency_mhz``,
476
485
  which the packet itself does not record.
477
486
  """
478
- self._check_window()
479
- holder: list = []
480
- gen = self._capture_rf_loop(
481
- window,
482
- frequency,
483
- stop_after_first,
484
- poll_interval,
485
- rearm_interval,
486
- claim=holder,
487
+ return self._capture_rf_loop(
488
+ window, frequency, stop_after_first, poll_interval, rearm_interval
487
489
  )
488
- holder.append(weakref.ref(gen))
489
- return gen
490
490
 
491
491
  async def _capture_rf_loop(
492
492
  self,
@@ -495,13 +495,28 @@ class rmpro(rmmini):
495
495
  stop_after_first: bool,
496
496
  poll_interval: float,
497
497
  rearm_interval: float,
498
- *,
499
- claim: list,
500
498
  ) -> AsyncGenerator[CapturedSignal]:
501
499
  if window < 0 or poll_interval <= 0:
502
500
  raise ValueError("window must be 0 or positive, poll_interval positive")
503
- await self._claim_window(claim[0])
501
+ await self._claim_window()
502
+ try:
503
+ body = self._capture_rf_body(
504
+ window, frequency, stop_after_first, poll_interval, rearm_interval
505
+ )
506
+ async with contextlib.aclosing(body):
507
+ async for signal in body:
508
+ yield signal
509
+ finally:
510
+ self._release_window()
504
511
 
512
+ async def _capture_rf_body(
513
+ self,
514
+ window: float,
515
+ frequency: float | None,
516
+ stop_after_first: bool,
517
+ poll_interval: float,
518
+ rearm_interval: float,
519
+ ) -> AsyncGenerator[CapturedSignal]:
505
520
  loop = asyncio.get_running_loop()
506
521
  deadline = loop.time() + window if window else None
507
522
 
@@ -526,13 +541,11 @@ class rmpro(rmmini):
526
541
  rearm_interval,
527
542
  kind,
528
543
  frequency,
529
- claim=None,
544
+ claim=False,
530
545
  )
531
- try:
546
+ async with contextlib.aclosing(inner):
532
547
  async for signal in inner:
533
548
  yield signal
534
- finally:
535
- await inner.aclose()
536
549
 
537
550
  async def _sweep(self, deadline: float | None, poll_interval: float) -> float | None:
538
551
  """Sweep for the remote's carrier; return it in MHz, or None if the
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-broadlink
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Summary: Python API for controlling Broadlink devices
5
5
  Author: DAB-LABS
6
6
  Author-email: Matthew Garrett <mjg59@srcf.ucam.org>
@@ -236,6 +236,11 @@ async with device:
236
236
  await device.aclose()
237
237
  ```
238
238
 
239
+ A device belongs to the event loop it first talks on: its socket and its
240
+ locks are bound to that loop, so a script that runs several
241
+ `asyncio.run(...)` calls should create the device inside each one rather
242
+ than reuse it across them.
243
+
239
244
  The socket reopens by itself on the next call, so closing is cheap and
240
245
  safe to do at any time. A request that is in flight when `aclose()` runs
241
246
  fails with `EndpointClosedError`. A request that fails for a network
@@ -321,9 +326,8 @@ By default the window closes after the first signal. Pass
321
326
  because the device holds only one code per learning session. A universal
322
327
  remote has a single receiver, so only one capture window can be open on a
323
328
  device at a time: opening a second one raises `CaptureInProgressError`
324
- while the first is still held, either from the `capture()` call itself or
325
- from the new window's first iteration, depending on what the first window
326
- was doing at that moment. Always close a window you leave early
329
+ from the new window's first iteration while the first is still held.
330
+ Always close a window you leave early
327
331
  (`aclosing` above does it), otherwise it stays open until Python collects
328
332
  the generator.
329
333
 
@@ -1,19 +1,19 @@
1
- broadlink/__init__.py,sha256=CYuVoYuKsXTd69yx7X9lIYr3DEPFlUL88bE4rcZMqb4,10890
1
+ broadlink/__init__.py,sha256=zlf8v225DdjvT_yDELRPAsQcAC4e5rrdlP0nyTBL7ms,11145
2
2
  broadlink/alarm.py,sha256=tmeNI-AaPna5nbgoTqz_AGmuQVItGTmt7zyNiRLsSlQ,1260
3
3
  broadlink/climate.py,sha256=fciXwQ0P2_kOaJiXAprfXuDyhLdHU--mE6pCYkVWdF8,15592
4
4
  broadlink/const.py,sha256=Y0XSxYr1k27FKqHWBirJsUB0HGxQ1riIZ9rfVEJaIL4,120
5
5
  broadlink/cover.py,sha256=-wW1BPmf5Z8oeLuqDVEpUyU-PgId5CJ91M3g3RjgVAU,5448
6
- broadlink/device.py,sha256=-aAZfV_CSWqIeFowewQo5Dn3vvp2YclZ4E19qOc_hVQ,24883
6
+ broadlink/device.py,sha256=Noy3Uj_7XfFDhgH92C1GM4smdRK_2-VR-aP6f3PjKAY,26378
7
7
  broadlink/exceptions.py,sha256=S8xOGdzyFGd6ULXUlblDwE-MADoJIR49itEo0j_fZNU,5226
8
8
  broadlink/helpers.py,sha256=Zbw7lNRMB08P5FPAtKMJLExBjts_Gq64tJbR7-JwxX8,1294
9
9
  broadlink/hub.py,sha256=e78lDKcdtDNqSOZkQRqLMPMFR0PgT2g3E7VgULCq-QM,2869
10
10
  broadlink/light.py,sha256=fWCoaf6JmDfcc7ggRmehss9D3cp66evUDlcB50Iu2LI,7660
11
11
  broadlink/protocol.py,sha256=eRCm0fGdApVp5ko_fOP79lazQklda8N6x30ty6P0TiA,1848
12
- broadlink/remote.py,sha256=3UjIvyps9BmCCPemGiWCm5BgCsNpT2TjD3dKznQ93JI,22487
12
+ broadlink/remote.py,sha256=t0CBCrzbUuA1wX066odpPuQSnkppcPnJSBV6oDYDeMo,22462
13
13
  broadlink/sensor.py,sha256=Pa4fJnG28wPFIf7HSN4JQ3cIOfAdTCu5I0spnBG12Qg,2791
14
14
  broadlink/switch.py,sha256=0VurdoGf3CTrYBSQU4ahVSLC_FYHqIRWr3dLkBwIatI,14985
15
- python_broadlink-1.0.4.dist-info/licenses/LICENSE,sha256=h0N_LWq34pY0W-Ky3ayO6Re5ejG0XvutPlW-GL8eG5I,1111
16
- python_broadlink-1.0.4.dist-info/METADATA,sha256=5uZ80FW7Vd0OzTsttoPeI78P0oXJHbLWXRkh92Rb-vE,15819
17
- python_broadlink-1.0.4.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
18
- python_broadlink-1.0.4.dist-info/top_level.txt,sha256=_Qx7d8oNTOLeDkAN-HHDoq1FlRNlsiaMZlyMWgHXAVc,10
19
- python_broadlink-1.0.4.dist-info/RECORD,,
15
+ python_broadlink-1.0.6.dist-info/licenses/LICENSE,sha256=h0N_LWq34pY0W-Ky3ayO6Re5ejG0XvutPlW-GL8eG5I,1111
16
+ python_broadlink-1.0.6.dist-info/METADATA,sha256=B8Qv617UFdGn6gustQIUxRdlY2b1qSwHYHHnuiN5rSA,15948
17
+ python_broadlink-1.0.6.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
18
+ python_broadlink-1.0.6.dist-info/top_level.txt,sha256=_Qx7d8oNTOLeDkAN-HHDoq1FlRNlsiaMZlyMWgHXAVc,10
19
+ python_broadlink-1.0.6.dist-info/RECORD,,