Skip to content

Commit 617d297

Browse files
committed
Refactor Device class to set first report IDs at initialization
1 parent 266ca15 commit 617d297

File tree

1 file changed

+14
-34
lines changed
  • adafruit_ble/services/standard

1 file changed

+14
-34
lines changed

adafruit_ble/services/standard/hid.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ class Device:
260260
def __init__(self, usage_page, usage):
261261
self._usage_page = usage_page
262262
self._usage = usage
263-
# Maintain insertion order of report_ids
264-
self._report_id_order = [] # list of report_id in insertion order
263+
self._first_report_in_id = None
264+
self._first_report_out_id = None
265265
self._report_ins = {} # report_id -> ReportIn
266266
self._report_outs = {} # report_id -> ReportOut
267267

@@ -275,50 +275,30 @@ def usage(self):
275275

276276
def add_report_in(self, report_id: int, report_in: ReportIn) -> None:
277277
"""Register a ReportIn instance for a report_id."""
278-
if report_id not in self._report_id_order:
279-
self._report_id_order.append(report_id)
278+
if self._first_report_in_id is None:
279+
self._first_report_in_id = report_id
280280
self._report_ins[report_id] = report_in
281281

282282
def add_report_out(self, report_id: int, report_out: ReportOut) -> None:
283283
"""Register a ReportOut instance for a report_id."""
284-
if report_id not in self._report_id_order:
285-
self._report_id_order.append(report_id)
284+
if self._first_report_out_id is None:
285+
self._first_report_out_id = report_id
286286
self._report_outs[report_id] = report_out
287287

288-
def _first_report_in_id(self):
289-
"""Return the first report_id that has an input (ReportIn)."""
290-
for rid in self._report_id_order:
291-
if rid in self._report_ins:
292-
return rid
293-
return None
294-
295-
def _first_report_out_id(self):
296-
"""Return the first report_id that has an output (ReportOut)."""
297-
for rid in self._report_id_order:
298-
if rid in self._report_outs:
299-
return rid
300-
return None
301-
302288
def send_report(self, report: Dict, report_id: int | None = None) -> None:
303-
"""Send a report via the ReportIn class.
304-
305-
If report_id is None, uses the first-added report_id for this device.
306-
Raises RuntimeError if no matching ReportIn exists.
307-
"""
289+
"""Send a report via the ReportIn class."""
290+
if report_id is None and self._first_report_in_id is not None:
291+
report_id = self._first_report_in_id
308292
if report_id is None:
309-
report_id = self._first_report_in_id()
310-
if report_id is None or report_id not in self._report_ins:
311-
raise RuntimeError(f"No input report available for report_id {report_id}")
293+
raise RuntimeError("No input report available")
312294
self._report_ins[report_id].send_report(report)
313295

314296
def get_last_received_report(self, report_id: int | None = None):
315-
"""Return the last OUT report received.
316-
If report_id is None, uses the first-added report_id for this device.
317-
"""
297+
"""Return the last OUT report received."""
298+
if report_id is None and self._first_report_out_id is not None:
299+
report_id = self._first_report_out_id
318300
if report_id is None:
319-
report_id = self._first_report_out_id()
320-
if report_id is None or report_id not in self._report_outs:
321-
return None
301+
raise RuntimeError("No output report available")
322302
return self._report_outs[report_id].report
323303

324304

0 commit comments

Comments
 (0)