Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aiocomfoconnect/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .const import VENTILATION_UNIT_PRODUCT_IDS
from .exceptions import (
AioComfoConnectNotConnected,
AioComfoConnectNotReachable,
AioComfoConnectTimeout,
ComfoConnectBadRequest,
ComfoConnectError,
Expand Down Expand Up @@ -189,8 +190,13 @@ async def _open_connection(self, uuid: str):
try:
self._reader, self._writer = await asyncio.wait_for(asyncio.open_connection(self.host, self.PORT), TIMEOUT)
except asyncio.TimeoutError as exc:
# Keep this before OSError, since TimeoutError is a subclass of it.
_LOGGER.warning("Timeout while connecting to bridge %s", self.host)
raise AioComfoConnectTimeout("Timeout while connecting to bridge") from exc
except OSError as exc:
# The bridge refused the connection, is gone from the network, or its hostname doesn't resolve.
_LOGGER.warning("Could not connect to bridge %s: %s", self.host, exc)
raise AioComfoConnectNotReachable(f"Could not connect to bridge: {exc}") from exc

self._reference = itertools.count(1)
self._local_uuid = uuid
Expand Down
5 changes: 5 additions & 0 deletions aiocomfoconnect/comfoconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from aiocomfoconnect.exceptions import (
AioComfoConnectNotConnected,
AioComfoConnectNotReachable,
AioComfoConnectTimeout,
ComfoConnectNotAllowed,
VentilationUnitNotFoundException,
Expand Down Expand Up @@ -180,6 +181,10 @@ async def _reconnect_loop(self, uuid: str):
_LOGGER.warning("Connection timeout, retrying in 5 seconds...")
await asyncio.sleep(5)

except AioComfoConnectNotReachable as exc:
_LOGGER.warning("%s. Retrying in 5 seconds...", exc)
await asyncio.sleep(5)

except VentilationUnitNotFoundException as exc:
_LOGGER.warning("%s, retrying in 5 seconds...", exc)
await asyncio.sleep(5)
Expand Down
4 changes: 4 additions & 0 deletions aiocomfoconnect/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class AioComfoConnectTimeout(Exception):
"""An error occurred because the bridge didn't reply in time."""


class AioComfoConnectNotReachable(Exception):
"""An error occurred because the bridge could not be reached, for example when it moved to another address."""


class BridgeNotFoundException(Exception):
"""Exception raised when no bridge is found."""

Expand Down
22 changes: 22 additions & 0 deletions tests/test_bridge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for the Bridge class."""

import asyncio
import socket
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import pytest
Expand All @@ -14,6 +15,7 @@

from aiocomfoconnect.exceptions import (
AioComfoConnectNotConnected,
AioComfoConnectNotReachable,
AioComfoConnectTimeout,
ComfoConnectNotAllowed,
VentilationUnitNotFoundException,
Expand Down Expand Up @@ -151,6 +153,26 @@ async def timeout_coro(*args, **kwargs):
await bridge.connect(LOCAL_UUID)
assert not bridge.is_connected()

@pytest.mark.asyncio
@pytest.mark.parametrize(
"error",
[
OSError(113, "No route to host"),
ConnectionRefusedError(111, "Connection refused"),
socket.gaierror("Name or service not known"),
],
)
async def test_connect_not_reachable(self, bridge, error):
"""Test that a bridge we can't reach doesn't raise a bare OSError."""

async def error_coro(*args, **kwargs):
raise error

with patch("asyncio.open_connection", side_effect=error_coro):
with pytest.raises(AioComfoConnectNotReachable, match="Could not connect to bridge"):
await bridge.connect(LOCAL_UUID)
assert not bridge.is_connected()

@pytest.mark.asyncio
async def test_connect_already_connected(self, bridge, mock_connection):
"""Test connecting when already connected."""
Expand Down
Loading