ble - trying updated read_data from the intern

This commit is contained in:
matt committed 2026-10-23 00:56:27 -07:00
1 parent 75a9ac32da
commit 6eaa22417c
1 file changed
+23 -32
+23 -32
View File
@@ -181,7 +181,6 @@ def _is_match(mac: str, name: str) -> bool:
# This is the actual function that does the magic # This is the actual function that does the magic
#################################################### ####################################################
async def read_data(mac: str) -> Optional[SensorReading]: async def read_data(mac: str) -> Optional[SensorReading]:
try: try:
address = await asyncio.wait_for( address = await asyncio.wait_for(
_discover_device(mac), _discover_device(mac),
@@ -193,43 +192,35 @@ async def read_data(mac: str) -> Optional[SensorReading]:
if not address: if not address:
return None return None
client: BleakClient | None = None
try: try:
async with BleakClient(address, timeout=CONNECT_TIMEOUT) as client: client = BleakClient(address, timeout=CONNECT_TIMEOUT)
# NEW BLOCK ---------------------------------------------- await client.connect()
try: # service discovery
await asyncio.wait_for(client.get_services(), await asyncio.wait_for(client.get_services(), timeout=SERVICE_DISCOVERY_TIMEOUT)
timeout=SERVICE_DISCOVERY_TIMEOUT)
except asyncio.TimeoutError:
log.warning("Service discovery timed out for %s", mac)
return None
except Exception as e:
log.error(f"Discovery failed - {e}")
#################################################### if MI_SERVICE_UUID.lower() not in (s.uuid.lower() for s in client.services):
# If we get here, services are available. log.error("Mi service not found on %s", mac)
#################################################### return None
if MI_SERVICE_UUID.lower() not in (
s.uuid.lower() for s in client.services
):
log.error("Mi service not found on %s", mac)
return None
raw = await asyncio.wait_for( raw = await asyncio.wait_for(client.read_gatt_char(TEMP_CHAR_UUID), timeout=READ_TIMEOUT)
client.read_gatt_char(TEMP_CHAR_UUID), temp, hum = _parse_sensor_data(raw)
timeout=READ_TIMEOUT, log.info("Read from %s: %.1f°C, %d%%", mac, temp, hum)
) return SensorReading(mac=mac, temperature=temp, humidity=hum)
temp, hum = _parse_sensor_data(raw)
log.info("Read from %s: %.1f°C, %d%%", mac, temp, hum)
return SensorReading(mac=mac, temperature=temp, humidity=hum)
except asyncio.TimeoutError:
# This is the *connect* timeout (BleakClient)
log.error("Connection to %s timed out after %.1fs", mac, CONNECT_TIMEOUT)
except Exception as exc: except Exception as exc:
log.exception("Error reading from %s: %s", mac, exc) log.exception("Error reading from %s: %s", mac, exc)
return None return None
finally:
# Safe disconnect – swallow any EOFError caused by a dropped DBus socket
if client is not None:
try:
await client.disconnect()
except Exception as e:
# Usually EOFError, just log and ignore
log.debug("Disconnect error (ignored): %s", e)
######################################################################################################## ########################################################################################################
### Test Function Section ### Test Function Section