Skip to content

Commit bbb67db

Browse files
astrandbfrenck
authored andcommitted
Add proper error handling for /actions endpoint for miele (home-assistant#152290)
1 parent 265f5da commit bbb67db

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

homeassistant/components/miele/coordinator.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
from __future__ import annotations
44

5-
import asyncio.timeouts
5+
import asyncio
66
from collections.abc import Callable
77
from dataclasses import dataclass
88
from datetime import timedelta
99
import logging
1010

11+
from aiohttp import ClientResponseError
1112
from pymiele import MieleAction, MieleAPI, MieleDevice
1213

1314
from homeassistant.config_entries import ConfigEntry
@@ -66,7 +67,22 @@ async def _async_update_data(self) -> MieleCoordinatorData:
6667
self.devices = devices
6768
actions = {}
6869
for device_id in devices:
69-
actions_json = await self.api.get_actions(device_id)
70+
try:
71+
actions_json = await self.api.get_actions(device_id)
72+
except ClientResponseError as err:
73+
_LOGGER.debug(
74+
"Error fetching actions for device %s: Status: %s, Message: %s",
75+
device_id,
76+
err.status,
77+
err.message,
78+
)
79+
actions_json = {}
80+
except TimeoutError:
81+
_LOGGER.debug(
82+
"Timeout fetching actions for device %s",
83+
device_id,
84+
)
85+
actions_json = {}
7086
actions[device_id] = MieleAction(actions_json)
7187
return MieleCoordinatorData(devices=devices, actions=actions)
7288

tests/components/miele/test_init.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from unittest.mock import MagicMock
77

8-
from aiohttp import ClientConnectionError
8+
from aiohttp import ClientConnectionError, ClientResponseError
99
from freezegun.api import FrozenDateTimeFactory
1010
from pymiele import OAUTH2_TOKEN
1111
import pytest
@@ -210,3 +210,29 @@ async def test_setup_all_platforms(
210210
# Check a sample sensor for each new device
211211
assert hass.states.get("sensor.dishwasher").state == "in_use"
212212
assert hass.states.get("sensor.oven_temperature_2").state == "175.0"
213+
214+
215+
@pytest.mark.parametrize(
216+
"side_effect",
217+
[
218+
ClientResponseError("test", "Test"),
219+
TimeoutError,
220+
],
221+
ids=[
222+
"ClientResponseError",
223+
"TimeoutError",
224+
],
225+
)
226+
async def test_load_entry_with_action_error(
227+
hass: HomeAssistant,
228+
mock_miele_client: MagicMock,
229+
mock_config_entry: MockConfigEntry,
230+
side_effect: Exception,
231+
) -> None:
232+
"""Test load with error from actions endpoint."""
233+
mock_miele_client.get_actions.side_effect = side_effect
234+
await setup_integration(hass, mock_config_entry)
235+
entry = mock_config_entry
236+
237+
assert entry.state is ConfigEntryState.LOADED
238+
assert mock_miele_client.get_actions.call_count == 5

0 commit comments

Comments
 (0)