-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import requests | ||
import logging | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class BankOfIsraelAPI: | ||
def __init__(self, hass, domain): | ||
self._hass = hass | ||
self._domain = domain | ||
self._base_url = "https://boi.org.il/PublicApi/GetExchangeRate?key=" | ||
|
||
async def get_exchange_rates(self): | ||
currencies = self._hass.data[self._domain]["currencies"] | ||
exchange_rates = {} | ||
|
||
for currency in currencies: | ||
try: | ||
response = await self._hass.async_add_executor_job( | ||
requests.get, self._base_url + currency | ||
) | ||
response.raise_for_status() | ||
data = response.json() | ||
exchange_rates[currency] = round(float(data["currentExchangeRate"]), 2) | ||
except requests.exceptions.RequestException as err: | ||
_LOGGER.error(f"Error fetching data for {currency}: {err}") | ||
except (ValueError, KeyError): | ||
_LOGGER.error(f"Error parsing data for {currency}") | ||
|
||
return exchange_rates | ||
|
||
async def get_available_currencies(self): | ||
try: | ||
response = await self._hass.async_add_executor_job( | ||
requests.get, "https://boi.org.il/PublicApi/GetExchangeRates" | ||
) | ||
response.raise_for_status() | ||
data = response.json() | ||
return {rate["key"]: rate["key"] for rate in data["exchangeRates"]} | ||
except requests.exceptions.RequestException as err: | ||
_LOGGER.error(f"Error fetching available currencies: {err}") | ||
return {} | ||
except (ValueError, KeyError): | ||
_LOGGER.error("Error parsing available currencies") | ||
return {} |