Add support for all currencies
This commit is contained in:
parent
0e94842c9d
commit
6b85fe5e70
@ -102,7 +102,10 @@ class BalanceSensor(Entity):
|
|||||||
return ICON_CURRENCY_EUR
|
return ICON_CURRENCY_EUR
|
||||||
elif self.currency == CURRENCY_USD:
|
elif self.currency == CURRENCY_USD:
|
||||||
return ICON_CURRENCY_USD
|
return ICON_CURRENCY_USD
|
||||||
return ICON_CURRENCY_BTC
|
elif self.currency == CURRENCY_BTC:
|
||||||
|
return ICON_CURRENCY_BTC
|
||||||
|
|
||||||
|
return ICON_CURRENCY_USD
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
|
@ -58,6 +58,23 @@ NICEHASH_ATTRIBUTION = "Data provided by NiceHash"
|
|||||||
CURRENCY_BTC = "BTC"
|
CURRENCY_BTC = "BTC"
|
||||||
CURRENCY_USD = "USD"
|
CURRENCY_USD = "USD"
|
||||||
CURRENCY_EUR = "EUR"
|
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
|
||||||
BALANCE_TYPE_AVAILABLE = "available"
|
BALANCE_TYPE_AVAILABLE = "available"
|
||||||
BALANCE_TYPE_PENDING = "pending"
|
BALANCE_TYPE_PENDING = "pending"
|
||||||
|
@ -12,6 +12,7 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CURRENCY_BTC,
|
CURRENCY_BTC,
|
||||||
|
CURRENCY_USD,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
|
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
|
||||||
@ -41,13 +42,25 @@ class AccountsDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
accounts = await self._client.get_accounts()
|
accounts = await self._client.get_accounts()
|
||||||
exchange_rates = await NiceHashPublicClient().get_exchange_rates()
|
exchange_rates = await NiceHashPublicClient().get_exchange_rates()
|
||||||
rates_dict = dict()
|
rates_dict = dict()
|
||||||
|
|
||||||
|
# NiceHash supports BTC->USD and BTC->EUR only.
|
||||||
for rate in exchange_rates:
|
for rate in exchange_rates:
|
||||||
fromCurrency = rate.get("fromCurrency")
|
fromCurrency = rate.get("fromCurrency")
|
||||||
# Only care about the Bitcoin exchange rates
|
|
||||||
if fromCurrency == CURRENCY_BTC:
|
if fromCurrency == CURRENCY_BTC:
|
||||||
toCurrency = rate.get("toCurrency")
|
toCurrency = rate.get("toCurrency")
|
||||||
exchange_rate = float(rate.get("exchangeRate"))
|
exchange_rate = float(rate.get("exchangeRate"))
|
||||||
rates_dict[f"{fromCurrency}-{toCurrency}"] = exchange_rate
|
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 {
|
return {
|
||||||
"accounts": accounts,
|
"accounts": accounts,
|
||||||
"exchange_rates": rates_dict,
|
"exchange_rates": rates_dict,
|
||||||
|
@ -20,6 +20,7 @@ from .const import (
|
|||||||
DEVICE_RPM,
|
DEVICE_RPM,
|
||||||
DEVICE_SPEED_RATE,
|
DEVICE_SPEED_RATE,
|
||||||
DEVICE_SPEED_ALGORITHM,
|
DEVICE_SPEED_ALGORITHM,
|
||||||
|
SUPPORTED_CURRENCIES,
|
||||||
)
|
)
|
||||||
from .nicehash import (
|
from .nicehash import (
|
||||||
MiningRig,
|
MiningRig,
|
||||||
@ -122,7 +123,7 @@ def create_balance_sensors(organization_id, currency, coordinator):
|
|||||||
balance_type=BALANCE_TYPE_TOTAL,
|
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")
|
_LOGGER.debug(f"Creating {currency} account balance sensors")
|
||||||
balance_sensors.append(
|
balance_sensors.append(
|
||||||
BalanceSensor(
|
BalanceSensor(
|
||||||
@ -149,8 +150,7 @@ def create_balance_sensors(organization_id, currency, coordinator):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
_LOGGER.warn("Invalid currency: must be EUR or USD")
|
_LOGGER.warn("Currency is invalid.")
|
||||||
|
|
||||||
return balance_sensors
|
return balance_sensors
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user