Skip to content

Commit dd197d7

Browse files
authored
Raise InvalidHostError if URL build fails (#838)
1 parent a39d40d commit dd197d7

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

aioshelly/common.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from .exceptions import (
2424
DeviceConnectionError,
2525
DeviceConnectionTimeoutError,
26+
InvalidHostError,
2627
MacAddressMismatchError,
28+
ShellyError,
2729
)
2830

2931
_LOGGER = logging.getLogger(__name__)
@@ -80,9 +82,7 @@ async def get_info(
8082
port: int = DEFAULT_HTTP_PORT,
8183
) -> dict[str, Any]:
8284
"""Get info from device through REST call."""
83-
error: (
84-
DeviceConnectionError | DeviceConnectionTimeoutError | MacAddressMismatchError
85-
)
85+
error: ShellyError
8686
try:
8787
async with aiohttp_session.get(
8888
URL.build(scheme="http", host=ip_address, port=port, path="/shelly"),
@@ -94,6 +94,10 @@ async def get_info(
9494
error = DeviceConnectionTimeoutError(err)
9595
_LOGGER.debug("host %s:%s: timeout error: %r", ip_address, port, error)
9696
raise error from err
97+
except ValueError as err:
98+
error = InvalidHostError(err)
99+
_LOGGER.debug("host %s is invalid: %r", ip_address, error)
100+
raise error from err
97101
except CONNECT_ERRORS as err:
98102
error = DeviceConnectionError(err)
99103
_LOGGER.debug("host %s:%s: error: %r", ip_address, port, error)

aioshelly/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class InvalidAuthError(ShellyError):
4343
"""Raised to indicate invalid or missing authentication error."""
4444

4545

46+
class InvalidHostError(ShellyError):
47+
"""Raised to indicate invalid host error."""
48+
49+
4650
class MacAddressMismatchError(ShellyError):
4751
"""Raised if input MAC address does not match the device MAC address."""
4852

tests/test_common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from aioshelly.exceptions import (
1818
DeviceConnectionError,
1919
DeviceConnectionTimeoutError,
20+
InvalidHostError,
2021
MacAddressMismatchError,
2122
)
2223

@@ -139,3 +140,16 @@ async def test_get_info_exc(exc: Exception, expected_exc: Exception) -> None:
139140
await get_info(session, ip_address, "AABBCCDDEEFF")
140141

141142
await session.close()
143+
144+
145+
@pytest.mark.asyncio
146+
async def test_get_info_invalid_error() -> None:
147+
"""Test get_info function with an invalid host exception."""
148+
session = ClientSession()
149+
150+
with pytest.raises(
151+
InvalidHostError, match="Host 'http://10.10.10.10' cannot contain ':'"
152+
):
153+
await get_info(session, "http://10.10.10.10", "AABBCCDDEEFF")
154+
155+
await session.close()

0 commit comments

Comments
 (0)