Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """Axis network device abstraction."""
2 
3 from asyncio import timeout
4 from types import MappingProxyType
5 from typing import Any
6 
7 import axis
8 from axis.models.configuration import Configuration
9 
10 from homeassistant.const import (
11  CONF_HOST,
12  CONF_PASSWORD,
13  CONF_PORT,
14  CONF_PROTOCOL,
15  CONF_USERNAME,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.httpx_client import get_async_client
19 
20 from ..const import LOGGER
21 from ..errors import AuthenticationRequired, CannotConnect
22 
23 
24 async def get_axis_api(
25  hass: HomeAssistant,
26  config: MappingProxyType[str, Any],
27 ) -> axis.AxisDevice:
28  """Create a Axis device API."""
29  session = get_async_client(hass, verify_ssl=False)
30 
31  api = axis.AxisDevice(
32  Configuration(
33  session,
34  config[CONF_HOST],
35  port=config[CONF_PORT],
36  username=config[CONF_USERNAME],
37  password=config[CONF_PASSWORD],
38  web_proto=config.get(CONF_PROTOCOL, "http"),
39  )
40  )
41 
42  try:
43  async with timeout(30):
44  await api.vapix.initialize()
45 
46  except axis.Unauthorized as err:
47  LOGGER.warning(
48  "Connected to device at %s but not registered", config[CONF_HOST]
49  )
50  raise AuthenticationRequired from err
51 
52  except (TimeoutError, axis.RequestError) as err:
53  LOGGER.error("Error connecting to the Axis device at %s", config[CONF_HOST])
54  raise CannotConnect from err
55 
56  except axis.AxisException as err:
57  LOGGER.exception("Unknown Axis communication error occurred")
58  raise AuthenticationRequired from err
59 
60  return api
axis.AxisDevice get_axis_api(HomeAssistant hass, MappingProxyType[str, Any] config)
Definition: api.py:27
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)
Definition: httpx_client.py:41