|
7 | 7 | from typing import Any, Dict, Optional |
8 | 8 |
|
9 | 9 | import serial |
| 10 | +from zigpy.config import CONF_DEVICE_PATH, SCHEMA_DEVICE |
10 | 11 | from zigpy.exceptions import APIException, DeliveryError |
11 | 12 | import zigpy.types as t |
12 | 13 |
|
13 | 14 | import zigpy_xbee |
14 | | -from zigpy_xbee.config import CONF_DEVICE_BAUDRATE, CONF_DEVICE_PATH, SCHEMA_DEVICE |
15 | 15 | from zigpy_xbee.exceptions import ( |
16 | 16 | ATCommandError, |
17 | 17 | ATCommandException, |
@@ -287,7 +287,6 @@ def __init__(self, device_config: Dict[str, Any]) -> None: |
287 | 287 | self._awaiting = {} |
288 | 288 | self._app = None |
289 | 289 | self._cmd_mode_future: Optional[asyncio.Future] = None |
290 | | - self._conn_lost_task: Optional[asyncio.Task] = None |
291 | 290 | self._reset: asyncio.Event = asyncio.Event() |
292 | 291 | self._running: asyncio.Event = asyncio.Event() |
293 | 292 |
|
@@ -323,64 +322,15 @@ async def connect(self) -> None: |
323 | 322 | assert self._uart is None |
324 | 323 | self._uart = await uart.connect(self._config, self) |
325 | 324 |
|
326 | | - def reconnect(self): |
327 | | - """Reconnect using saved parameters.""" |
328 | | - LOGGER.debug( |
329 | | - "Reconnecting '%s' serial port using %s", |
330 | | - self._config[CONF_DEVICE_PATH], |
331 | | - self._config[CONF_DEVICE_BAUDRATE], |
332 | | - ) |
333 | | - return self.connect() |
334 | | - |
335 | 325 | def connection_lost(self, exc: Exception) -> None: |
336 | 326 | """Lost serial connection.""" |
337 | | - LOGGER.warning( |
338 | | - "Serial '%s' connection lost unexpectedly: %s", |
339 | | - self._config[CONF_DEVICE_PATH], |
340 | | - exc, |
341 | | - ) |
342 | | - self._uart = None |
343 | | - if self._conn_lost_task and not self._conn_lost_task.done(): |
344 | | - self._conn_lost_task.cancel() |
345 | | - self._conn_lost_task = asyncio.create_task(self._connection_lost()) |
346 | | - |
347 | | - async def _connection_lost(self) -> None: |
348 | | - """Reconnect serial port.""" |
349 | | - try: |
350 | | - await self._reconnect_till_done() |
351 | | - except asyncio.CancelledError: |
352 | | - LOGGER.debug("Cancelling reconnection attempt") |
353 | | - raise |
354 | | - |
355 | | - async def _reconnect_till_done(self) -> None: |
356 | | - attempt = 1 |
357 | | - while True: |
358 | | - try: |
359 | | - await asyncio.wait_for(self.reconnect(), timeout=10) |
360 | | - break |
361 | | - except (asyncio.TimeoutError, OSError) as exc: |
362 | | - wait = 2 ** min(attempt, 5) |
363 | | - attempt += 1 |
364 | | - LOGGER.debug( |
365 | | - "Couldn't re-open '%s' serial port, retrying in %ss: %s", |
366 | | - self._config[CONF_DEVICE_PATH], |
367 | | - wait, |
368 | | - str(exc), |
369 | | - ) |
370 | | - await asyncio.sleep(wait) |
| 327 | + if self._app is not None: |
| 328 | + self._app.connection_lost(exc) |
371 | 329 |
|
372 | | - LOGGER.debug( |
373 | | - "Reconnected '%s' serial port after %s attempts", |
374 | | - self._config[CONF_DEVICE_PATH], |
375 | | - attempt, |
376 | | - ) |
| 330 | + self.close() |
377 | 331 |
|
378 | 332 | def close(self): |
379 | 333 | """Close the connection.""" |
380 | | - if self._conn_lost_task: |
381 | | - self._conn_lost_task.cancel() |
382 | | - self._conn_lost_task = None |
383 | | - |
384 | 334 | if self._uart: |
385 | 335 | self._uart.close() |
386 | 336 | self._uart = None |
|
0 commit comments