Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for all currencies #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion custom_components/nicehash/account_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ def icon(self):
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):
Expand Down
17 changes: 17 additions & 0 deletions custom_components/nicehash/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@
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"
Expand Down
15 changes: 14 additions & 1 deletion custom_components/nicehash/coordinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from .const import (
CURRENCY_BTC,
CURRENCY_USD,
DOMAIN,
)
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
Expand Down Expand Up @@ -41,13 +42,25 @@ async def _async_update_data(self):
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,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/nicehash/rig_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def icon(self):
@property
def unit_of_measurement(self):
"""Sensor unit of measurement"""
return None
return f"{self._unit}"

@property
def device_state_attributes(self):
Expand Down
6 changes: 3 additions & 3 deletions custom_components/nicehash/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
DEVICE_RPM,
DEVICE_SPEED_RATE,
DEVICE_SPEED_ALGORITHM,
SUPPORTED_CURRENCIES,
)
from .nicehash import (
MiningRig,
Expand Down Expand Up @@ -121,7 +122,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(
Expand All @@ -148,8 +149,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


Expand Down