Fix IQVIA failing to start if any API call fails (#50615)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/50628/head^2
Aaron Bach 2021-05-14 15:23:16 -06:00 committed by GitHub
parent 960ed13f94
commit 9c5f1b4406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -9,6 +9,7 @@ from pyiqvia.errors import IQVIAError
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
@ -74,9 +75,14 @@ async def async_setup_entry(hass, entry):
update_interval=DEFAULT_SCAN_INTERVAL,
update_method=partial(async_get_data_from_api, api_coro),
)
init_data_update_tasks.append(coordinator.async_config_entry_first_refresh())
init_data_update_tasks.append(coordinator.async_refresh())
await asyncio.gather(*init_data_update_tasks)
results = await asyncio.gather(*init_data_update_tasks, return_exceptions=True)
if all(isinstance(result, Exception) for result in results):
# The IQVIA API can be selectively flaky, meaning that any number of the setup
# API calls could fail. We only retry integration setup if *all* of the initial
# API calls fail:
raise ConfigEntryNotReady()
hass.data[DOMAIN].setdefault(DATA_COORDINATOR, {})[entry.entry_id] = coordinators
hass.config_entries.async_setup_platforms(entry, PLATFORMS)

View File

@ -104,9 +104,12 @@ class ForecastSensor(IQVIAEntity):
@callback
def update_from_latest_data(self):
"""Update the sensor."""
data = self.coordinator.data.get("Location")
if not self.coordinator.data:
return
if not data or not data.get("periods"):
data = self.coordinator.data.get("Location", {})
if not data.get("periods"):
return
indices = [p["Index"] for p in data["periods"]]