Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The caldav component."""
2 
3 import logging
4 
5 import caldav
6 from caldav.lib.error import AuthorizationError, DAVError
7 import requests
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import (
11  CONF_PASSWORD,
12  CONF_URL,
13  CONF_USERNAME,
14  CONF_VERIFY_SSL,
15  Platform,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
19 
20 type CalDavConfigEntry = ConfigEntry[caldav.DAVClient]
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
25 PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.TODO]
26 
27 
28 async def async_setup_entry(hass: HomeAssistant, entry: CalDavConfigEntry) -> bool:
29  """Set up CalDAV from a config entry."""
30  client = caldav.DAVClient(
31  entry.data[CONF_URL],
32  username=entry.data[CONF_USERNAME],
33  password=entry.data[CONF_PASSWORD],
34  ssl_verify_cert=entry.data[CONF_VERIFY_SSL],
35  timeout=30,
36  )
37  try:
38  await hass.async_add_executor_job(client.principal)
39  except AuthorizationError as err:
40  if err.reason == "Unauthorized":
41  raise ConfigEntryAuthFailed("Credentials error from CalDAV server") from err
42  # AuthorizationError can be raised if the url is incorrect or
43  # on some other unexpected server response.
44  _LOGGER.warning("Unexpected CalDAV server response: %s", err)
45  return False
46  except requests.ConnectionError as err:
47  raise ConfigEntryNotReady("Connection error from CalDAV server") from err
48  except DAVError as err:
49  raise ConfigEntryNotReady("CalDAV client error") from err
50 
51  entry.runtime_data = client
52 
53  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
54 
55  return True
56 
57 
58 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
59  """Unload a config entry."""
60  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, CalDavConfigEntry entry)
Definition: __init__.py:28
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:58