Skip to content

Commit 19b2c6d

Browse files
authored
fix(amberelectric): add request timeouts (home-assistant#150613)
Signed-off-by: JP-Ellis <[email protected]>
1 parent 0b231ff commit 19b2c6d

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

homeassistant/components/amberelectric/config_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
SelectSelectorMode,
1717
)
1818

19-
from .const import CONF_SITE_ID, CONF_SITE_NAME, DOMAIN
19+
from .const import CONF_SITE_ID, CONF_SITE_NAME, DOMAIN, REQUEST_TIMEOUT
2020

2121
API_URL = "https://app.amber.com.au/developers"
2222

@@ -64,7 +64,9 @@ def _fetch_sites(self, token: str) -> list[Site] | None:
6464
api = amberelectric.AmberApi(api_client)
6565

6666
try:
67-
sites: list[Site] = filter_sites(api.get_sites())
67+
sites: list[Site] = filter_sites(
68+
api.get_sites(_request_timeout=REQUEST_TIMEOUT)
69+
)
6870
except amberelectric.ApiException as api_exception:
6971
if api_exception.status == 403:
7072
self._errors[CONF_API_TOKEN] = "invalid_api_token"

homeassistant/components/amberelectric/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
GENERAL_CHANNEL = "general"
2222
CONTROLLED_LOAD_CHANNEL = "controlled_load"
2323
FEED_IN_CHANNEL = "feed_in"
24+
25+
REQUEST_TIMEOUT = 15

homeassistant/components/amberelectric/coordinator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from homeassistant.core import HomeAssistant
1717
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
1818

19-
from .const import LOGGER
19+
from .const import LOGGER, REQUEST_TIMEOUT
2020
from .helpers import normalize_descriptor
2121

2222
type AmberConfigEntry = ConfigEntry[AmberUpdateCoordinator]
@@ -82,7 +82,11 @@ def update_price_data(self) -> dict[str, dict[str, Any]]:
8282
"grid": {},
8383
}
8484
try:
85-
data = self._api.get_current_prices(self.site_id, next=288)
85+
data = self._api.get_current_prices(
86+
self.site_id,
87+
next=288,
88+
_request_timeout=REQUEST_TIMEOUT,
89+
)
8690
intervals = [interval.actual_instance for interval in data]
8791
except ApiException as api_exception:
8892
raise UpdateFailed("Missing price data, skipping update") from api_exception

tests/components/amberelectric/test_coordinator.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
from dateutil import parser
1616
import pytest
1717

18-
from homeassistant.components.amberelectric.const import CONF_SITE_ID, CONF_SITE_NAME
18+
from homeassistant.components.amberelectric.const import (
19+
CONF_SITE_ID,
20+
CONF_SITE_NAME,
21+
REQUEST_TIMEOUT,
22+
)
1923
from homeassistant.components.amberelectric.coordinator import AmberUpdateCoordinator
2024
from homeassistant.const import CONF_API_TOKEN
2125
from homeassistant.core import HomeAssistant
@@ -104,7 +108,9 @@ async def test_fetch_general_site(hass: HomeAssistant, current_price_api: Mock)
104108
result = await data_service._async_update_data()
105109

106110
current_price_api.get_current_prices.assert_called_with(
107-
GENERAL_ONLY_SITE_ID, next=288
111+
GENERAL_ONLY_SITE_ID,
112+
next=288,
113+
_request_timeout=REQUEST_TIMEOUT,
108114
)
109115

110116
assert result["current"].get("general") == GENERAL_CHANNEL[0].actual_instance
@@ -136,7 +142,9 @@ async def test_fetch_no_general_site(
136142
await data_service._async_update_data()
137143

138144
current_price_api.get_current_prices.assert_called_with(
139-
GENERAL_ONLY_SITE_ID, next=288
145+
GENERAL_ONLY_SITE_ID,
146+
next=288,
147+
_request_timeout=REQUEST_TIMEOUT,
140148
)
141149

142150

@@ -150,7 +158,9 @@ async def test_fetch_api_error(hass: HomeAssistant, current_price_api: Mock) ->
150158
result = await data_service._async_update_data()
151159

152160
current_price_api.get_current_prices.assert_called_with(
153-
GENERAL_ONLY_SITE_ID, next=288
161+
GENERAL_ONLY_SITE_ID,
162+
next=288,
163+
_request_timeout=REQUEST_TIMEOUT,
154164
)
155165

156166
assert result["current"].get("general") == GENERAL_CHANNEL[0].actual_instance
@@ -201,7 +211,9 @@ async def test_fetch_general_and_controlled_load_site(
201211
result = await data_service._async_update_data()
202212

203213
current_price_api.get_current_prices.assert_called_with(
204-
GENERAL_AND_CONTROLLED_SITE_ID, next=288
214+
GENERAL_AND_CONTROLLED_SITE_ID,
215+
next=288,
216+
_request_timeout=REQUEST_TIMEOUT,
205217
)
206218

207219
assert result["current"].get("general") == GENERAL_CHANNEL[0].actual_instance
@@ -241,7 +253,9 @@ async def test_fetch_general_and_feed_in_site(
241253
result = await data_service._async_update_data()
242254

243255
current_price_api.get_current_prices.assert_called_with(
244-
GENERAL_AND_FEED_IN_SITE_ID, next=288
256+
GENERAL_AND_FEED_IN_SITE_ID,
257+
next=288,
258+
_request_timeout=REQUEST_TIMEOUT,
245259
)
246260

247261
assert result["current"].get("general") == GENERAL_CHANNEL[0].actual_instance

0 commit comments

Comments
 (0)