Skip to content

Commit

Permalink
Create __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugulator authored Jul 1, 2024
1 parent 853a044 commit 9ade542
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions custom_components/boi_exchange_rates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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
hass.async_create_task(
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)

0 comments on commit 9ade542

Please sign in to comment.