mirror of
https://github.com/home-assistant/core.git
synced 2026-09-05 10:05:52 -05:00
Add proper error handling for /actions endpoint for miele (#152290)
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio.timeouts
|
||||
import asyncio
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from aiohttp import ClientResponseError
|
||||
from pymiele import MieleAction, MieleAPI, MieleDevice
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@@ -66,7 +67,22 @@ class MieleDataUpdateCoordinator(DataUpdateCoordinator[MieleCoordinatorData]):
|
||||
self.devices = devices
|
||||
actions = {}
|
||||
for device_id in devices:
|
||||
actions_json = await self.api.get_actions(device_id)
|
||||
try:
|
||||
actions_json = await self.api.get_actions(device_id)
|
||||
except ClientResponseError as err:
|
||||
_LOGGER.debug(
|
||||
"Error fetching actions for device %s: Status: %s, Message: %s",
|
||||
device_id,
|
||||
err.status,
|
||||
err.message,
|
||||
)
|
||||
actions_json = {}
|
||||
except TimeoutError:
|
||||
_LOGGER.debug(
|
||||
"Timeout fetching actions for device %s",
|
||||
device_id,
|
||||
)
|
||||
actions_json = {}
|
||||
actions[device_id] = MieleAction(actions_json)
|
||||
return MieleCoordinatorData(devices=devices, actions=actions)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import http
|
||||
import time
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from aiohttp import ClientConnectionError
|
||||
from aiohttp import ClientConnectionError, ClientResponseError
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pymiele import OAUTH2_TOKEN
|
||||
import pytest
|
||||
@@ -210,3 +210,29 @@ async def test_setup_all_platforms(
|
||||
# Check a sample sensor for each new device
|
||||
assert hass.states.get("sensor.dishwasher").state == "in_use"
|
||||
assert hass.states.get("sensor.oven_temperature_2").state == "175.0"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"side_effect",
|
||||
[
|
||||
ClientResponseError("test", "Test"),
|
||||
TimeoutError,
|
||||
],
|
||||
ids=[
|
||||
"ClientResponseError",
|
||||
"TimeoutError",
|
||||
],
|
||||
)
|
||||
async def test_load_entry_with_action_error(
|
||||
hass: HomeAssistant,
|
||||
mock_miele_client: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
side_effect: Exception,
|
||||
) -> None:
|
||||
"""Test load with error from actions endpoint."""
|
||||
mock_miele_client.get_actions.side_effect = side_effect
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
entry = mock_config_entry
|
||||
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
assert mock_miele_client.get_actions.call_count == 5
|
||||
|
||||
Reference in New Issue
Block a user