-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
68 lines (52 loc) · 2.01 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import asyncio
from datetime import timedelta
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .api import BankOfIsraelAPI
_LOGGER = logging.getLogger(__name__)
DOMAIN = "boi_exchange_rates"
PLATFORMS = ["sensor"]
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Bank of Israel Exchange Rates component."""
hass.data[DOMAIN] = {}
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up Bank of Israel Exchange Rates from a config entry."""
currencies = entry.options.get("currencies", [])
hass.data[DOMAIN] = {"currencies": currencies}
api = BankOfIsraelAPI(hass, DOMAIN)
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name="exchange_rate",
update_method=api.get_exchange_rates,
update_interval=timedelta(hours=3),
)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = {
"coordinator": coordinator,
"currencies": currencies,
"sensors": [], # Initialize empty sensor list
}
entry.async_on_unload(entry.add_update_listener(update_listener))
# Setup sensor platform
await hass.config_entries.async_forward_entry_setup(entry, "sensor")
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def update_listener(hass, entry):
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)