Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The jewish_calendar component."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 
7 from hdate import Location
8 
9 from homeassistant.const import (
10  CONF_ELEVATION,
11  CONF_LANGUAGE,
12  CONF_LATITUDE,
13  CONF_LONGITUDE,
14  CONF_TIME_ZONE,
15  Platform,
16 )
17 from homeassistant.core import HomeAssistant
18 
19 from .const import (
20  CONF_CANDLE_LIGHT_MINUTES,
21  CONF_DIASPORA,
22  CONF_HAVDALAH_OFFSET_MINUTES,
23  DEFAULT_CANDLE_LIGHT,
24  DEFAULT_DIASPORA,
25  DEFAULT_HAVDALAH_OFFSET_MINUTES,
26  DEFAULT_LANGUAGE,
27 )
28 from .entity import JewishCalendarConfigEntry, JewishCalendarData
29 
30 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
31 
32 
34  hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
35 ) -> bool:
36  """Set up a configuration entry for Jewish calendar."""
37  language = config_entry.data.get(CONF_LANGUAGE, DEFAULT_LANGUAGE)
38  diaspora = config_entry.data.get(CONF_DIASPORA, DEFAULT_DIASPORA)
39  candle_lighting_offset = config_entry.options.get(
40  CONF_CANDLE_LIGHT_MINUTES, DEFAULT_CANDLE_LIGHT
41  )
42  havdalah_offset = config_entry.options.get(
43  CONF_HAVDALAH_OFFSET_MINUTES, DEFAULT_HAVDALAH_OFFSET_MINUTES
44  )
45 
46  location = await hass.async_add_executor_job(
47  partial(
48  Location,
49  name=hass.config.location_name,
50  diaspora=diaspora,
51  latitude=config_entry.data.get(CONF_LATITUDE, hass.config.latitude),
52  longitude=config_entry.data.get(CONF_LONGITUDE, hass.config.longitude),
53  altitude=config_entry.data.get(CONF_ELEVATION, hass.config.elevation),
54  timezone=config_entry.data.get(CONF_TIME_ZONE, hass.config.time_zone),
55  )
56  )
57 
58  config_entry.runtime_data = JewishCalendarData(
59  language,
60  diaspora,
61  location,
62  candle_lighting_offset,
63  havdalah_offset,
64  )
65 
66  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
67 
68  async def update_listener(
69  hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
70  ) -> None:
71  # Trigger update of states for all platforms
72  await hass.config_entries.async_reload(config_entry.entry_id)
73 
74  config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
75  return True
76 
77 
79  hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
80 ) -> bool:
81  """Unload a config entry."""
82  return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, JewishCalendarConfigEntry config_entry)
Definition: __init__.py:80
bool async_setup_entry(HomeAssistant hass, JewishCalendarConfigEntry config_entry)
Definition: __init__.py:35
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30