mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Add startup exception handling to nordpool (#131104)
This commit is contained in:
parent
80e8b8d61b
commit
926689ee4f
@ -4,9 +4,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import PLATFORMS
|
from .const import DOMAIN, PLATFORMS
|
||||||
from .coordinator import NordPoolDataUpdateCoordinator
|
from .coordinator import NordPoolDataUpdateCoordinator
|
||||||
|
|
||||||
type NordPoolConfigEntry = ConfigEntry[NordPoolDataUpdateCoordinator]
|
type NordPoolConfigEntry = ConfigEntry[NordPoolDataUpdateCoordinator]
|
||||||
@ -17,6 +18,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: NordPoolConfigEntry) ->
|
|||||||
|
|
||||||
coordinator = NordPoolDataUpdateCoordinator(hass, entry)
|
coordinator = NordPoolDataUpdateCoordinator(hass, entry)
|
||||||
await coordinator.fetch_data(dt_util.utcnow())
|
await coordinator.fetch_data(dt_util.utcnow())
|
||||||
|
if not coordinator.last_update_success:
|
||||||
|
raise ConfigEntryNotReady(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="initial_update_failed",
|
||||||
|
translation_placeholders={"error": str(coordinator.last_exception)},
|
||||||
|
)
|
||||||
entry.runtime_data = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
@ -61,5 +61,10 @@
|
|||||||
"name": "Daily average"
|
"name": "Daily average"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"exceptions": {
|
||||||
|
"initial_update_failed": {
|
||||||
|
"message": "Initial update failed on startup with error {error}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,14 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from pynordpool import DeliveryPeriodData
|
from pynordpool import (
|
||||||
|
DeliveryPeriodData,
|
||||||
|
NordPoolConnectionError,
|
||||||
|
NordPoolEmptyResponseError,
|
||||||
|
NordPoolError,
|
||||||
|
NordPoolResponseError,
|
||||||
|
)
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.nordpool.const import DOMAIN
|
from homeassistant.components.nordpool.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
||||||
@ -37,3 +44,35 @@ async def test_unload_entry(hass: HomeAssistant, get_data: DeliveryPeriodData) -
|
|||||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("error"),
|
||||||
|
[
|
||||||
|
(NordPoolConnectionError),
|
||||||
|
(NordPoolEmptyResponseError),
|
||||||
|
(NordPoolError),
|
||||||
|
(NordPoolResponseError),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_initial_startup_fails(
|
||||||
|
hass: HomeAssistant, get_data: DeliveryPeriodData, error: Exception
|
||||||
|
) -> None:
|
||||||
|
"""Test load and unload an entry."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
source=SOURCE_USER,
|
||||||
|
data=ENTRY_CONFIG,
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.nordpool.coordinator.NordPoolClient.async_get_delivery_period",
|
||||||
|
side_effect=error,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
Loading…
x
Reference in New Issue
Block a user