Add openexchangerates config flow (#76390)

This commit is contained in:
Martin Hjelmare 2022-08-07 23:45:32 +02:00 committed by GitHub
parent ceecab9559
commit d1ab93fbaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 661 additions and 82 deletions

View file

@ -1,19 +1,22 @@
"""Provide an OpenExchangeRates data coordinator."""
from __future__ import annotations
import asyncio
from datetime import timedelta
from aiohttp import ClientSession
from aioopenexchangerates import Client, Latest, OpenExchangeRatesClientError
from aioopenexchangerates import (
Client,
Latest,
OpenExchangeRatesAuthError,
OpenExchangeRatesClientError,
)
import async_timeout
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN, LOGGER
TIMEOUT = 10
from .const import CLIENT_TIMEOUT, DOMAIN, LOGGER
class OpenexchangeratesCoordinator(DataUpdateCoordinator[Latest]):
@ -33,14 +36,15 @@ class OpenexchangeratesCoordinator(DataUpdateCoordinator[Latest]):
)
self.base = base
self.client = Client(api_key, session)
self.setup_lock = asyncio.Lock()
async def _async_update_data(self) -> Latest:
"""Update data from Open Exchange Rates."""
try:
async with async_timeout.timeout(TIMEOUT):
async with async_timeout.timeout(CLIENT_TIMEOUT):
latest = await self.client.get_latest(base=self.base)
except (OpenExchangeRatesClientError) as err:
except OpenExchangeRatesAuthError as err:
raise ConfigEntryAuthFailed(err) from err
except OpenExchangeRatesClientError as err:
raise UpdateFailed(err) from err
LOGGER.debug("Result: %s", latest)