Skip to content

Commit 73251fb

Browse files
Handle additional errors in Nord Pool (home-assistant#147937)
1 parent 7ff90ca commit 73251fb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

homeassistant/components/nordpool/coordinator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from datetime import datetime, timedelta
77
from typing import TYPE_CHECKING
88

9+
import aiohttp
910
from pynordpool import (
1011
Currency,
1112
DeliveryPeriodData,
@@ -91,6 +92,8 @@ async def api_call(self, retry: int = 3) -> DeliveryPeriodsData | None:
9192
except (
9293
NordPoolResponseError,
9394
NordPoolError,
95+
TimeoutError,
96+
aiohttp.ClientError,
9497
) as error:
9598
LOGGER.debug("Connection error: %s", error)
9699
self.async_set_update_error(error)

tests/components/nordpool/test_coordinator.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from datetime import timedelta
66
from unittest.mock import patch
77

8+
import aiohttp
89
from freezegun.api import FrozenDateTimeFactory
910
from pynordpool import (
1011
NordPoolAuthenticationError,
@@ -90,6 +91,36 @@ async def test_coordinator(
9091
assert state.state == STATE_UNAVAILABLE
9192
assert "Empty response" in caplog.text
9293

94+
with (
95+
patch(
96+
"homeassistant.components.nordpool.coordinator.NordPoolClient.async_get_delivery_period",
97+
side_effect=aiohttp.ClientError("error"),
98+
) as mock_data,
99+
):
100+
assert "Response error" not in caplog.text
101+
freezer.tick(timedelta(hours=1))
102+
async_fire_time_changed(hass)
103+
await hass.async_block_till_done(wait_background_tasks=True)
104+
assert mock_data.call_count == 1
105+
state = hass.states.get("sensor.nord_pool_se3_current_price")
106+
assert state.state == STATE_UNAVAILABLE
107+
assert "error" in caplog.text
108+
109+
with (
110+
patch(
111+
"homeassistant.components.nordpool.coordinator.NordPoolClient.async_get_delivery_period",
112+
side_effect=TimeoutError("error"),
113+
) as mock_data,
114+
):
115+
assert "Response error" not in caplog.text
116+
freezer.tick(timedelta(hours=1))
117+
async_fire_time_changed(hass)
118+
await hass.async_block_till_done(wait_background_tasks=True)
119+
assert mock_data.call_count == 1
120+
state = hass.states.get("sensor.nord_pool_se3_current_price")
121+
assert state.state == STATE_UNAVAILABLE
122+
assert "error" in caplog.text
123+
93124
with (
94125
patch(
95126
"homeassistant.components.nordpool.coordinator.NordPoolClient.async_get_delivery_period",
@@ -109,4 +140,4 @@ async def test_coordinator(
109140
async_fire_time_changed(hass)
110141
await hass.async_block_till_done()
111142
state = hass.states.get("sensor.nord_pool_se3_current_price")
112-
assert state.state == "1.81645"
143+
assert state.state == "1.81983"

0 commit comments

Comments
 (0)