Robert Svensson 03e4a20cdf
Rename get_deconz_session to get_deconz_api (#112826)
Move and rename get_deconz_session to get_deconz_api
2024-03-09 18:32:31 +01:00

40 lines
1.2 KiB
Python

"""deCONZ API representation."""
from __future__ import annotations
import asyncio
from types import MappingProxyType
from typing import Any
from pydeconz import DeconzSession, errors
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from ..const import LOGGER
from ..errors import AuthenticationRequired, CannotConnect
async def get_deconz_api(
hass: HomeAssistant, config: MappingProxyType[str, Any]
) -> DeconzSession:
"""Create a gateway object and verify configuration."""
session = aiohttp_client.async_get_clientsession(hass)
api = DeconzSession(
session, config[CONF_HOST], config[CONF_PORT], config[CONF_API_KEY]
)
try:
async with asyncio.timeout(10):
await api.refresh_state()
return api
except errors.Unauthorized as err:
LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
raise AuthenticationRequired from err
except (TimeoutError, errors.RequestError, errors.ResponseError) as err:
LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
raise CannotConnect from err