From 6b85fe5e70eeddb95973d30b828a9a31cdb7d10f Mon Sep 17 00:00:00 2001 From: Cathal O Cuimin Date: Sun, 15 May 2022 20:45:47 -0700 Subject: [PATCH] Add support for all currencies --- custom_components/nicehash/account_sensors.py | 5 ++++- custom_components/nicehash/const.py | 17 +++++++++++++++++ custom_components/nicehash/coordinators.py | 15 ++++++++++++++- custom_components/nicehash/sensor.py | 6 +++--- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/custom_components/nicehash/account_sensors.py b/custom_components/nicehash/account_sensors.py index ce335ea..7833b61 100644 --- a/custom_components/nicehash/account_sensors.py +++ b/custom_components/nicehash/account_sensors.py @@ -102,7 +102,10 @@ class BalanceSensor(Entity): return ICON_CURRENCY_EUR elif self.currency == CURRENCY_USD: return ICON_CURRENCY_USD - return ICON_CURRENCY_BTC + elif self.currency == CURRENCY_BTC: + return ICON_CURRENCY_BTC + + return ICON_CURRENCY_USD @property def unit_of_measurement(self): diff --git a/custom_components/nicehash/const.py b/custom_components/nicehash/const.py index fb4c2c6..10f43b7 100644 --- a/custom_components/nicehash/const.py +++ b/custom_components/nicehash/const.py @@ -58,6 +58,23 @@ NICEHASH_ATTRIBUTION = "Data provided by NiceHash" CURRENCY_BTC = "BTC" CURRENCY_USD = "USD" CURRENCY_EUR = "EUR" +SUPPORTED_CURRENCIES = [ + "ERN","HKD","GGP","RSD","SHP","USD","MYR","PYG","RON","DOP","TWD","AWG", + "CVE","BND","RUB","NGN","XCD","JEP","ZWL","HNL","NZD","AFN","MUR","DKK", + "CNY","JOD","CHF","COP","XAF","XAG","ZMK","GNF","ZMW","GIP","BTC","MKD", + "WST","IDR","IQD","BHD","YER","MAD","KGS","PHP","PEN","BMD","DJF","MVR", + "QAR","JPY","SCR","IMP","KRW","HRK","SOS","VUV","NIO","KYD","LAK","ISK", + "BOB","IRR","NPR","EGP","BBD","CAD","XAU","CUP","SDG","PKR","UZS","CLF", + "CUC","STD","MGA","FJD","DZD","TJS","EURKM","SZL","THB","SRD","BDT", + "BTN","CZK","AMD","UGX","TRY","AUD","UAH","HUF","SLL","VND","RWF","LBP", + "ANG","SAR","LVL","KHR","BYR","TTD","OMR","LTL","GTQ","ALL","MRO","MWK", + "LSL","SBD","BGN","LRD","JMD","CRC","ETB","NAD","GYD","LKR","INR","SEK", + "KES","KMF","VEF","ARS","HTG","BAM","BWP","GEL","KZT","AED","KWD","XDR", + "EUR","TND","MDL","LYD","BSD","GHS","MOP","PAB","ZAR","AZN","TOP","SVC", + "KPW","TMT","BZD","GMD","XOF","UYU","MNT","NOK","XPF","BIF","BYN","FKP", + "GBP","MXN","SYP","PGK","MZN","PLN","MMK","SGD","AOA","ILS","CLP","TZS", + "CDF","BRL" +] # Balance type BALANCE_TYPE_AVAILABLE = "available" BALANCE_TYPE_PENDING = "pending" diff --git a/custom_components/nicehash/coordinators.py b/custom_components/nicehash/coordinators.py index 9877356..8716aba 100644 --- a/custom_components/nicehash/coordinators.py +++ b/custom_components/nicehash/coordinators.py @@ -12,6 +12,7 @@ from homeassistant.helpers.update_coordinator import ( from .const import ( CURRENCY_BTC, + CURRENCY_USD, DOMAIN, ) from .nicehash import NiceHashPrivateClient, NiceHashPublicClient @@ -41,13 +42,25 @@ class AccountsDataUpdateCoordinator(DataUpdateCoordinator): accounts = await self._client.get_accounts() exchange_rates = await NiceHashPublicClient().get_exchange_rates() rates_dict = dict() + + # NiceHash supports BTC->USD and BTC->EUR only. for rate in exchange_rates: fromCurrency = rate.get("fromCurrency") - # Only care about the Bitcoin exchange rates if fromCurrency == CURRENCY_BTC: toCurrency = rate.get("toCurrency") exchange_rate = float(rate.get("exchangeRate")) rates_dict[f"{fromCurrency}-{toCurrency}"] = exchange_rate + + # For non-USD/EUR currencies, get exchange rate using USD as an intermediary. e.g. BTC->USD->CAD + btc_to_usd_rate = rates_dict[f"BTC-USD"] + for rate in exchange_rates: + fromCurrency = rate.get("fromCurrency") + if fromCurrency == CURRENCY_USD: + toCurrency = rate.get("toCurrency") + exchange_rate = float(rate.get("exchangeRate")) * btc_to_usd_rate + if f"{CURRENCY_BTC}-{toCurrency}" not in rates_dict: + rates_dict[f"{CURRENCY_BTC}-{toCurrency}"] = exchange_rate + return { "accounts": accounts, "exchange_rates": rates_dict, diff --git a/custom_components/nicehash/sensor.py b/custom_components/nicehash/sensor.py index ca235e4..0b37bfb 100644 --- a/custom_components/nicehash/sensor.py +++ b/custom_components/nicehash/sensor.py @@ -20,6 +20,7 @@ from .const import ( DEVICE_RPM, DEVICE_SPEED_RATE, DEVICE_SPEED_ALGORITHM, + SUPPORTED_CURRENCIES, ) from .nicehash import ( MiningRig, @@ -122,7 +123,7 @@ def create_balance_sensors(organization_id, currency, coordinator): balance_type=BALANCE_TYPE_TOTAL, ), ] - if currency == CURRENCY_USD or currency == CURRENCY_EUR: + if currency in SUPPORTED_CURRENCIES: _LOGGER.debug(f"Creating {currency} account balance sensors") balance_sensors.append( BalanceSensor( @@ -149,8 +150,7 @@ def create_balance_sensors(organization_id, currency, coordinator): ) ) else: - _LOGGER.warn("Invalid currency: must be EUR or USD") - + _LOGGER.warn("Currency is invalid.") return balance_sensors