mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Refactor exceptions to align on library (#169622)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -77,10 +77,8 @@ class IndevoltCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Fetch device info once on boot."""
|
||||
try:
|
||||
config_data = await self.api.get_config()
|
||||
except TimeoutError as err:
|
||||
raise ConfigEntryNotReady(
|
||||
f"Device config retrieval timed out: {err}"
|
||||
) from err
|
||||
except (ClientError, OSError) as err:
|
||||
raise ConfigEntryNotReady(f"Device config retrieval failed: {err}") from err
|
||||
|
||||
# Cache device information
|
||||
device_data = config_data.get("device", {})
|
||||
@@ -93,8 +91,8 @@ class IndevoltCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
|
||||
try:
|
||||
return await self.api.fetch_data(sensor_keys)
|
||||
except TimeoutError as err:
|
||||
raise UpdateFailed(f"Device update timed out: {err}") from err
|
||||
except (ClientError, OSError) as err:
|
||||
raise UpdateFailed(f"Device update failed: {err}") from err
|
||||
|
||||
async def async_push_data(self, sensor_key: str, value: Any) -> bool:
|
||||
"""Push/write data values to given key on the device."""
|
||||
@@ -102,7 +100,7 @@ class IndevoltCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
return await self.api.set_data(sensor_key, value)
|
||||
except TimeoutError as err:
|
||||
raise DeviceTimeoutError(f"Device push timed out: {err}") from err
|
||||
except (ClientError, ConnectionError, OSError) as err:
|
||||
except (ClientError, OSError) as err:
|
||||
raise DeviceConnectionError(f"Device push failed: {err}") from err
|
||||
|
||||
async def async_switch_energy_mode(
|
||||
|
||||
@@ -371,11 +371,11 @@ async def test_single_device_execution_failure(
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test that the original exception is re-raised for a single device execution failure."""
|
||||
"""Test that the exception is raised for a single device execution failure."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Simulate an API push failure
|
||||
mock_indevolt.set_data.side_effect = HomeAssistantError("Device push failed")
|
||||
mock_indevolt.set_data.side_effect = OSError("Device push failed")
|
||||
|
||||
# Mock call to start charging
|
||||
with pytest.raises(HomeAssistantError) as exc_info:
|
||||
@@ -391,8 +391,7 @@ async def test_single_device_execution_failure(
|
||||
)
|
||||
|
||||
# Verify correct translation key is used for the error (for single coordinator)
|
||||
assert str(exc_info.value) == "Device push failed"
|
||||
assert exc_info.value.translation_key is None
|
||||
assert exc_info.value.translation_key == "failed_to_switch_energy_mode"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
@@ -410,7 +409,7 @@ async def test_multi_device_execution_failure(
|
||||
await setup_integration(hass, alt_mock_config_entry)
|
||||
|
||||
# Simulate an API push failure (triggers for both coordinators)
|
||||
mock_indevolt.set_data.side_effect = HomeAssistantError("Device push failed")
|
||||
mock_indevolt.set_data.side_effect = OSError("Device push failed")
|
||||
|
||||
# Mock call to start charging both devices
|
||||
with pytest.raises(HomeAssistantError) as exc_info:
|
||||
|
||||
Reference in New Issue
Block a user