Skip to content

Commit f9a1357

Browse files
committed
Add a BLEError 'kind' field and branch on it instead of string matching
1 parent 1fc2407 commit f9a1357

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

meshtastic/__main__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,8 +1374,7 @@ def common():
13741374
timeout=args.timeout,
13751375
)
13761376
except BLEInterface.BLEError as e:
1377-
msg = str(e)
1378-
if "No Meshtastic BLE peripheral" in msg:
1377+
if e.kind == BLEInterface.BLEError.DEVICE_NOT_FOUND:
13791378
meshtastic.util.our_exit(
13801379
"BLE device not found.\n\n"
13811380
"Possible causes:\n"
@@ -1387,15 +1386,15 @@ def common():
13871386
" - Run 'meshtastic --ble-scan' to see available devices",
13881387
1,
13891388
)
1390-
elif "More than one" in msg:
1389+
elif e.kind == BLEInterface.BLEError.MULTIPLE_DEVICES:
13911390
meshtastic.util.our_exit(
13921391
"Multiple Meshtastic BLE devices found.\n\n"
13931392
"Please specify which device to connect to:\n"
13941393
" - Run 'meshtastic --ble-scan' to list devices\n"
13951394
" - Use 'meshtastic --ble <name_or_address>' to connect",
13961395
1,
13971396
)
1398-
elif "Error writing BLE" in msg:
1397+
elif e.kind == BLEInterface.BLEError.WRITE_ERROR:
13991398
meshtastic.util.our_exit(
14001399
"Failed to write to BLE device.\n\n"
14011400
"Possible causes:\n"
@@ -1407,7 +1406,7 @@ def common():
14071406
" - Reset the Meshtastic device",
14081407
1,
14091408
)
1410-
elif "Error reading BLE" in msg:
1409+
elif e.kind == BLEInterface.BLEError.READ_ERROR:
14111410
meshtastic.util.our_exit(
14121411
"Failed to read from BLE device.\n\n"
14131412
"The device may have disconnected unexpectedly.\n\n"

meshtastic/ble_interface.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class BLEInterface(MeshInterface):
3333
class BLEError(Exception):
3434
"""An exception class for BLE errors."""
3535

36+
DEVICE_NOT_FOUND = "device_not_found"
37+
MULTIPLE_DEVICES = "multiple_devices"
38+
READ_ERROR = "read_error"
39+
WRITE_ERROR = "write_error"
40+
UNKNOWN = "unknown"
41+
42+
def __init__(self, message: str, kind: str = UNKNOWN):
43+
super().__init__(message)
44+
self.kind = kind
45+
3646
def __init__( # pylint: disable=R0917
3747
self,
3848
address: Optional[str],
@@ -157,11 +167,13 @@ def find_device(self, address: Optional[str]) -> BLEDevice:
157167

158168
if len(addressed_devices) == 0:
159169
raise BLEInterface.BLEError(
160-
f"No Meshtastic BLE peripheral with identifier or address '{address}' found. Try --ble-scan to find it."
170+
f"No Meshtastic BLE peripheral with identifier or address '{address}' found. Try --ble-scan to find it.",
171+
BLEInterface.BLEError.DEVICE_NOT_FOUND,
161172
)
162173
if len(addressed_devices) > 1:
163174
raise BLEInterface.BLEError(
164-
f"More than one Meshtastic BLE peripheral with identifier or address '{address}' found."
175+
f"More than one Meshtastic BLE peripheral with identifier or address '{address}' found.",
176+
BLEInterface.BLEError.MULTIPLE_DEVICES,
165177
)
166178
return addressed_devices[0]
167179

@@ -204,7 +216,10 @@ def _receiveFromRadioImpl(self) -> None:
204216
logger.debug(f"Device disconnected, shutting down {e}")
205217
self._want_receive = False
206218
else:
207-
raise BLEInterface.BLEError("Error reading BLE") from e
219+
raise BLEInterface.BLEError(
220+
"Error reading BLE",
221+
BLEInterface.BLEError.READ_ERROR,
222+
) from e
208223
if not b:
209224
if retries < 5:
210225
time.sleep(0.1)
@@ -227,7 +242,8 @@ def _sendToRadioImpl(self, toRadio) -> None:
227242
# search Bleak src for org.bluez.Error.InProgress
228243
except Exception as e:
229244
raise BLEInterface.BLEError(
230-
"Error writing BLE (are you in the 'bluetooth' user group? did you enter the pairing PIN on your computer?)"
245+
"Error writing BLE (are you in the 'bluetooth' user group? did you enter the pairing PIN on your computer?)",
246+
BLEInterface.BLEError.WRITE_ERROR,
231247
) from e
232248
# Allow to propagate and then make sure we read
233249
time.sleep(0.01)

0 commit comments

Comments
 (0)