add timeout exception to _discover_device
This commit is contained in:
1 parent
138e7cf315
commit
d22a26d2dc
1 file changed
+19
-6
+19
-6
@@ -65,14 +65,27 @@ class SensorReading:
|
|||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
async def _discover_device(mac: str) -> Optional[str]:
|
async def _discover_device(mac: str) -> Optional[str]:
|
||||||
"""Scan once for the given MAC and return its address if found."""
|
"""Scan once for the given MAC and return its address if found."""
|
||||||
devices = await BleakScanner.discover(timeout=SCAN_TIMEOUT)
|
try:
|
||||||
for d in devices:
|
devices = await asyncio.wait_for(
|
||||||
if d.address.lower() == mac.lower():
|
BleakScanner.discover(timeout=SCAN_TIMEOUT),
|
||||||
log.info("Found device %s (%s)", d.name or d.address, d.address)
|
timeout=SCAN_TIMEOUT + 2.0, # give a small buffer over scan timeout
|
||||||
return d.address
|
)
|
||||||
log.warning("Device %s not found in scan", mac)
|
for d in devices:
|
||||||
|
if d.address.lower() == mac.lower():
|
||||||
|
log.info("Found device %s (%s)", d.name or d.address, d.address)
|
||||||
|
return d.address
|
||||||
|
log.warning("Device %s not found in scan", mac)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
log.warning("Discover call timed out after %.1fs for %s", SCAN_TIMEOUT + 2.0, mac)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
#for d in devices:
|
||||||
|
# if d.address.lower() == mac.lower():
|
||||||
|
# log.info("Found device %s (%s)", d.name or d.address, d.address)
|
||||||
|
# return d.address
|
||||||
|
#log.warning("Device %s not found in scan", mac)
|
||||||
|
#return None
|
||||||
|
|
||||||
|
|
||||||
def _parse_sensor_data(raw: bytes) -> (float, int):
|
def _parse_sensor_data(raw: bytes) -> (float, int):
|
||||||
"""Parse the 3-byte Mi packet into temperature & humidity."""
|
"""Parse the 3-byte Mi packet into temperature & humidity."""
|
||||||
|
|||||||
Reference in new issue
Block a user