Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Holiday integration."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 
7 from holidays import country_holidays
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_COUNTRY, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.setup import SetupPhases, async_pause_setup
13 
14 from .const import CONF_PROVINCE
15 
16 PLATFORMS: list[Platform] = [Platform.CALENDAR]
17 
18 
19 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
20  """Set up Holiday from a config entry."""
21  country: str = entry.data[CONF_COUNTRY]
22  province: str | None = entry.data.get(CONF_PROVINCE)
23 
24  # We only import here to ensure that that its not imported later
25  # in the event loop since the platforms will call country_holidays
26  # which loads python code from disk.
27  with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
28  # import executor job is used here because multiple integrations use
29  # the holidays library and it is not thread safe to import it in parallel
30  # https://github.com/python/cpython/issues/83065
31  await hass.async_add_import_executor_job(
32  partial(country_holidays, country, subdiv=province)
33  )
34 
35  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
36 
37  return True
38 
39 
40 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
41  """Unload a config entry."""
42  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:19
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:40
Generator[None] async_pause_setup(core.HomeAssistant hass, SetupPhases phase)
Definition: setup.py:691