Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """deCONZ API representation."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 
7 from pydeconz import DeconzSession, errors
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers import aiohttp_client
12 
13 from ..const import LOGGER
14 from ..errors import AuthenticationRequired, CannotConnect
15 from .config import DeconzConfig
16 
17 
18 async def get_deconz_api(
19  hass: HomeAssistant, config_entry: ConfigEntry
20 ) -> DeconzSession:
21  """Create a gateway object and verify configuration."""
22  session = aiohttp_client.async_get_clientsession(hass)
23 
24  config = DeconzConfig.from_config_entry(config_entry)
25  api = DeconzSession(session, config.host, config.port, config.api_key)
26  try:
27  async with asyncio.timeout(10):
28  await api.refresh_state()
29 
30  except errors.Unauthorized as err:
31  LOGGER.warning("Invalid key for deCONZ at %s", config.host)
32  raise AuthenticationRequired from err
33 
34  except (TimeoutError, errors.RequestError, errors.ResponseError) as err:
35  LOGGER.error("Error connecting to deCONZ gateway at %s", config.host)
36  raise CannotConnect from err
37  return api
DeconzSession get_deconz_api(HomeAssistant hass, ConfigEntry config_entry)
Definition: api.py:20