Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Sensor to indicate whether the current day is a workday."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 
7 from holidays import HolidayBase, country_holidays
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_COUNTRY, CONF_LANGUAGE
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryError
13 from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
14 from homeassistant.setup import SetupPhases, async_pause_setup
15 
16 from .const import CONF_PROVINCE, DOMAIN, PLATFORMS
17 
18 
20  hass: HomeAssistant, entry: ConfigEntry, country: str | None, province: str | None
21 ) -> None:
22  """Validate country and province."""
23 
24  if not country:
25  return
26  try:
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(country_holidays, country)
32  except NotImplementedError as ex:
34  hass,
35  DOMAIN,
36  "bad_country",
37  is_fixable=True,
38  is_persistent=False,
39  severity=IssueSeverity.ERROR,
40  translation_key="bad_country",
41  translation_placeholders={"title": entry.title},
42  data={"entry_id": entry.entry_id, "country": None},
43  )
44  raise ConfigEntryError(f"Selected country {country} is not valid") from ex
45 
46  if not province:
47  return
48  try:
49  with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
50  # import executor job is used here because multiple integrations use
51  # the holidays library and it is not thread safe to import it in parallel
52  # https://github.com/python/cpython/issues/83065
53  await hass.async_add_import_executor_job(
54  partial(country_holidays, country, subdiv=province)
55  )
56  except NotImplementedError as ex:
58  hass,
59  DOMAIN,
60  "bad_province",
61  is_fixable=True,
62  is_persistent=False,
63  severity=IssueSeverity.ERROR,
64  translation_key="bad_province",
65  translation_placeholders={
66  CONF_COUNTRY: country,
67  "title": entry.title,
68  },
69  data={"entry_id": entry.entry_id, "country": country},
70  )
71  raise ConfigEntryError(
72  f"Selected province {province} for country {country} is not valid"
73  ) from ex
74 
75 
76 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
77  """Set up Workday from a config entry."""
78 
79  country: str | None = entry.options.get(CONF_COUNTRY)
80  province: str | None = entry.options.get(CONF_PROVINCE)
81 
82  await _async_validate_country_and_province(hass, entry, country, province)
83 
84  if country and CONF_LANGUAGE not in entry.options:
85  with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
86  # import executor job is used here because multiple integrations use
87  # the holidays library and it is not thread safe to import it in parallel
88  # https://github.com/python/cpython/issues/83065
89  cls: HolidayBase = await hass.async_add_import_executor_job(
90  partial(country_holidays, country, subdiv=province)
91  )
92  default_language = cls.default_language
93  new_options = entry.options.copy()
94  new_options[CONF_LANGUAGE] = default_language
95  hass.config_entries.async_update_entry(entry, options=new_options)
96 
97  entry.async_on_unload(entry.add_update_listener(async_update_listener))
98  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
99  return True
100 
101 
102 async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
103  """Update listener for options."""
104  await hass.config_entries.async_reload(entry.entry_id)
105 
106 
107 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
108  """Unload Workday config entry."""
109 
110  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:76
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:107
None _async_validate_country_and_province(HomeAssistant hass, ConfigEntry entry, str|None country, str|None province)
Definition: __init__.py:21
None async_update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:102
Generator[None] async_pause_setup(core.HomeAssistant hass, SetupPhases phase)
Definition: setup.py:691