1 """Provide an object to communicate with UniFi Network application."""
3 from __future__
import annotations
7 from types
import MappingProxyType
8 from typing
import Any, Literal
10 from aiohttp
import CookieJar
12 from aiounifi.models.configuration
import Configuration
24 from ..const
import CONF_SITE_ID, LOGGER
25 from ..errors
import AuthenticationRequired, CannotConnect
30 config: MappingProxyType[str, Any],
31 ) -> aiounifi.Controller:
32 """Create a aiounifi object and verify authentication."""
33 ssl_context: ssl.SSLContext | Literal[
False] =
False
35 if verify_ssl := config.get(CONF_VERIFY_SSL):
36 session = aiohttp_client.async_get_clientsession(hass)
37 if isinstance(verify_ssl, str):
38 ssl_context = ssl.create_default_context(cafile=verify_ssl)
40 session = aiohttp_client.async_create_clientsession(
41 hass, verify_ssl=
False, cookie_jar=CookieJar(unsafe=
True)
44 api = aiounifi.Controller(
47 host=config[CONF_HOST],
48 username=config[CONF_USERNAME],
49 password=config[CONF_PASSWORD],
50 port=config[CONF_PORT],
51 site=config[CONF_SITE_ID],
52 ssl_context=ssl_context,
57 async
with asyncio.timeout(10):
60 except aiounifi.Unauthorized
as err:
62 "Connected to UniFi Network at %s but not registered: %s",
66 raise AuthenticationRequired
from err
72 aiounifi.ServiceUnavailable,
73 aiounifi.RequestError,
74 aiounifi.ResponseError,
77 "Error connecting to the UniFi Network at %s: %s", config[CONF_HOST], err
79 raise CannotConnect
from err
81 except aiounifi.LoginRequired
as err:
83 "Connected to UniFi Network at %s but login required: %s",
87 raise AuthenticationRequired
from err
89 except aiounifi.AiounifiException
as err:
90 LOGGER.exception(
"Unknown UniFi Network communication error occurred: %s", err)
91 raise AuthenticationRequired
from err
aiounifi.Controller get_unifi_api(HomeAssistant hass, MappingProxyType[str, Any] config)