Skip to content

Commit d52908b

Browse files
committed
Silly type hinting
1 parent 1ddfc57 commit d52908b

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

homeassistant_api/errors.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,29 @@ class HomeassistantAPIError(Exception):
1010
class RequestError(HomeassistantAPIError):
1111
"""Error raised when an issue occurs when requesting to Homeassistant."""
1212

13-
def __init__(self, data: str, /, url: str) -> None:
14-
if data is None:
13+
def __init__(
14+
self, data: Optional[str], /, url: str, message: Optional[str] = None
15+
) -> None:
16+
if message is not None:
17+
super().__init__(
18+
message
19+
+ f" {url!r}"
20+
+ (f" with data: {data!r}" if data is not None else "")
21+
)
22+
elif data is None:
1523
super().__init__(f"An error occurred while making the request to {url!r}")
1624
else:
1725
super().__init__(
1826
f"An error occurred while making the request to {url!r} with data: {data!r}"
1927
)
2028

29+
2130
class RequestTimeoutError(RequestError):
2231
"""Error raised when a request times out."""
2332

33+
def __init__(self, message: str, url: str) -> None:
34+
super().__init__(None, url, message)
35+
2436

2537
class ResponseError(HomeassistantAPIError):
2638
"""Error raised when an issue occurs in a response from Homeassistant."""

homeassistant_api/rawasyncclient.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ async def async_request(
112112
)
113113
except asyncio.exceptions.TimeoutError as err:
114114
raise RequestTimeoutError(
115-
f'Home Assistant did not respond in time (timeout: {kwargs.get("timeout", 300)} sec)'
115+
f'Home Assistant did not respond in time (timeout: {kwargs.get("timeout", 300)} sec)',
116+
self.endpoint(path) + f"?{params}" * bool(params),
116117
) from err
117118

118119
@staticmethod
@@ -145,7 +146,9 @@ async def async_get_logbook_entries(
145146
:code:`GET /api/logbook/<timestamp>`
146147
"""
147148
params, url = self.prepare_get_logbook_entry_params(*args, **kwargs)
148-
data = await self.async_request(url, params=params)
149+
data = await self.async_request(
150+
url, params=self.construct_params(cast(Dict[str, Optional[str]], params))
151+
)
149152
for entry in data:
150153
yield LogbookEntry.model_validate(entry)
151154

homeassistant_api/rawbaseclient.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def construct_params(params: Dict[str, Optional[str]]) -> str:
7070
For keys with corresponding non-None values, the query string will be key-value pairs (i.e. :code:`?key1=value1&key2=value2`).
7171
To have an empty value use an empty string :code:`""` (i.e. :code:`?key1=&key2=value2`).
7272
"""
73-
return "&".join([k if v is None else f"{k}={quote_plus(v)}" for k, v in params.items()])
73+
return "&".join(
74+
[k if v is None else f"{k}={quote_plus(v)}" for k, v in params.items()]
75+
)
7476

7577
@staticmethod
7678
def prepare_get_entity_histories_params(

homeassistant_api/rawclient.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def request(
107107
)
108108
except requests.exceptions.Timeout as err:
109109
raise RequestTimeoutError(
110-
f'Home Assistant did not respond in time (timeout: {kwargs.get("timeout", 300)} sec)'
110+
f'Home Assistant did not respond in time (timeout: {kwargs.get("timeout", 300)} sec)',
111+
url=self.endpoint(path) + f"?{params}" * bool(params),
111112
) from err
112113
return self.response_logic(response=resp, decode_bytes=decode_bytes)
113114

@@ -141,7 +142,9 @@ def get_logbook_entries(
141142
:code:`GET /api/logbook/<timestamp>`
142143
"""
143144
params, url = self.prepare_get_logbook_entry_params(*args, **kwargs)
144-
data = self.request(url, params=params)
145+
data = self.request(
146+
url, params=self.construct_params(cast(Dict[str, Optional[str]], params))
147+
)
145148
for entry in data:
146149
yield LogbookEntry.model_validate(entry)
147150

0 commit comments

Comments
 (0)