feat(nicehash): add more acount balance sensors
- add total and pending sensors for BTC - add total and pending sensors for currency - default currency to USD - only support EUR or USD currency - in addition to BTC - ignore non-BTC exchange rates
This commit is contained in:
parent
2ef6ad9ac1
commit
8456fa4c34
@ -36,6 +36,7 @@ Platform | Description
|
||||
organization_id: <org_id>
|
||||
api_key: <api_key_code>
|
||||
api_secret: <api_secret_key_code>
|
||||
currency: "EUR" (default = "USD")
|
||||
```
|
||||
1. Restart Home Assistant
|
||||
|
||||
|
@ -22,6 +22,7 @@ from .const import (
|
||||
CONF_CURRENCY,
|
||||
CONF_ORGANIZATION_ID,
|
||||
CURRENCY_BTC,
|
||||
CURRENCY_USD,
|
||||
DOMAIN,
|
||||
STARTUP_MESSAGE,
|
||||
)
|
||||
@ -39,7 +40,7 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
vol.Required(CONF_ORGANIZATION_ID): cv.string,
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_API_SECRET): cv.string,
|
||||
vol.Required(CONF_CURRENCY, default=CURRENCY_BTC): cv.string,
|
||||
vol.Required(CONF_CURRENCY, default=CURRENCY_USD): cv.string,
|
||||
}
|
||||
)
|
||||
},
|
||||
@ -57,7 +58,7 @@ async def async_setup(hass: HomeAssistant, config: Config):
|
||||
organization_id = nicehash_config.get(CONF_ORGANIZATION_ID)
|
||||
api_key = nicehash_config.get(CONF_API_KEY)
|
||||
api_secret = nicehash_config.get(CONF_API_SECRET)
|
||||
currency = nicehash_config.get(CONF_CURRENCY)
|
||||
currency = nicehash_config.get(CONF_CURRENCY).upper()
|
||||
|
||||
client = NiceHashPrivateClient(organization_id, api_key, api_secret)
|
||||
|
||||
@ -76,6 +77,7 @@ async def async_setup(hass: HomeAssistant, config: Config):
|
||||
_LOGGER.error("Unable to get NiceHash mining rigs")
|
||||
raise PlatformNotReady
|
||||
|
||||
hass.data[DOMAIN]["organization_id"] = organization_id
|
||||
hass.data[DOMAIN]["client"] = client
|
||||
hass.data[DOMAIN]["currency"] = currency
|
||||
hass.data[DOMAIN]["accounts_coordinator"] = accounts_coordinator
|
||||
@ -91,7 +93,7 @@ class NiceHashAccountsDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
|
||||
def __init__(self, hass: HomeAssistant, client: NiceHashPrivateClient):
|
||||
"""Initialize"""
|
||||
self.name = f"{DOMAIN}_Accounts_Coordinator"
|
||||
self.name = f"{DOMAIN}_accounts_coordinator"
|
||||
self._client = client
|
||||
|
||||
super().__init__(
|
||||
@ -106,6 +108,8 @@ class NiceHashAccountsDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
rates_dict = dict()
|
||||
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
|
||||
@ -122,7 +126,7 @@ class NiceHashMiningRigsDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
|
||||
def __init__(self, hass: HomeAssistant, client: NiceHashPrivateClient):
|
||||
"""Initialize"""
|
||||
self.name = f"{DOMAIN}_Mining_Rigs_Coordinator"
|
||||
self.name = f"{DOMAIN}_mining_rigs_coordinator"
|
||||
self._client = client
|
||||
|
||||
super().__init__(
|
||||
|
@ -11,8 +11,9 @@ ISSUE_URL = "https://github.com/brianberg/ha-nicehash/issues"
|
||||
|
||||
# Icons
|
||||
ICON = "mdi:pickaxe"
|
||||
ICON_CURRENCY_USD = "mdi:currency-usd"
|
||||
ICON_CURRENCY_BTC = "mdi:currency-btc"
|
||||
ICON_CURRENCY_EUR = "mdi:currency-eur"
|
||||
ICON_CURRENCY_USD = "mdi:currency-usd"
|
||||
ICON_TEMPERATURE = "mdi:thermometer"
|
||||
|
||||
# Platforms
|
||||
@ -47,3 +48,6 @@ NICEHASH_API_URL = "https://api2.nicehash.com"
|
||||
CURRENCY_BTC = "BTC"
|
||||
CURRENCY_USD = "USD"
|
||||
CURRENCY_EUR = "EUR"
|
||||
BALANCE_TYPE_AVAILABLE = "available"
|
||||
BALANCE_TYPE_PENDING = "pending"
|
||||
BALANCE_TYPE_TOTAL = "total"
|
||||
|
@ -12,12 +12,17 @@ from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
from .const import (
|
||||
BALANCE_TYPE_AVAILABLE,
|
||||
BALANCE_TYPE_PENDING,
|
||||
BALANCE_TYPE_TOTAL,
|
||||
CURRENCY_BTC,
|
||||
CURRENCY_EUR,
|
||||
CURRENCY_USD,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
ICON_CURRENCY_BTC,
|
||||
ICON_CURRENCY_EUR,
|
||||
ICON_CURRENCY_USD,
|
||||
ICON_TEMPERATURE,
|
||||
)
|
||||
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
|
||||
@ -36,27 +41,62 @@ async def async_setup_platform(
|
||||
_LOGGER.debug("Creating new NiceHash sensor components")
|
||||
|
||||
data = hass.data[DOMAIN]
|
||||
organization_id = data.get("organization_id")
|
||||
client = data.get("client")
|
||||
currency = data.get("currency")
|
||||
accounts_coordinator = data.get("accounts_coordinator")
|
||||
rigs_coordinator = data.get("rigs_coordinator")
|
||||
|
||||
# Add account balance sensor(s)
|
||||
btc_balance_sensor = NiceHashBalanceSensor(
|
||||
accounts_coordinator, client.organization_id, CURRENCY_BTC
|
||||
)
|
||||
if currency == CURRENCY_BTC:
|
||||
async_add_entities([btc_balance_sensor], True)
|
||||
else:
|
||||
async_add_entities(
|
||||
[
|
||||
btc_balance_sensor,
|
||||
# Add account balance sensors
|
||||
balance_sensors = [
|
||||
NiceHashBalanceSensor(
|
||||
accounts_coordinator, client.organization_id, currency
|
||||
accounts_coordinator,
|
||||
organization_id,
|
||||
currency=CURRENCY_BTC,
|
||||
balance_type=BALANCE_TYPE_AVAILABLE,
|
||||
),
|
||||
],
|
||||
True,
|
||||
NiceHashBalanceSensor(
|
||||
accounts_coordinator,
|
||||
organization_id,
|
||||
currency=CURRENCY_BTC,
|
||||
balance_type=BALANCE_TYPE_PENDING,
|
||||
),
|
||||
NiceHashBalanceSensor(
|
||||
accounts_coordinator,
|
||||
organization_id,
|
||||
currency=CURRENCY_BTC,
|
||||
balance_type=BALANCE_TYPE_TOTAL,
|
||||
),
|
||||
]
|
||||
if currency == CURRENCY_USD or currency == CURRENCY_EUR:
|
||||
balance_sensors.append(
|
||||
NiceHashBalanceSensor(
|
||||
accounts_coordinator,
|
||||
organization_id,
|
||||
currency=currency,
|
||||
balance_type=BALANCE_TYPE_AVAILABLE,
|
||||
)
|
||||
)
|
||||
balance_sensors.append(
|
||||
NiceHashBalanceSensor(
|
||||
accounts_coordinator,
|
||||
organization_id,
|
||||
currency=currency,
|
||||
balance_type=BALANCE_TYPE_PENDING,
|
||||
)
|
||||
)
|
||||
balance_sensors.append(
|
||||
NiceHashBalanceSensor(
|
||||
accounts_coordinator,
|
||||
organization_id,
|
||||
currency=currency,
|
||||
balance_type=BALANCE_TYPE_TOTAL,
|
||||
)
|
||||
)
|
||||
else:
|
||||
_LOGGER.warn("Invalid currency: must be EUR or USD")
|
||||
|
||||
async_add_entities(balance_sensors, True)
|
||||
|
||||
# Add mining rig sensors
|
||||
rig_data = await client.get_mining_rigs()
|
||||
@ -71,12 +111,19 @@ async def async_setup_platform(
|
||||
class NiceHashBalanceSensor(Entity):
|
||||
"""NiceHash Account Balance Sensor"""
|
||||
|
||||
def __init__(self, coordinator, organization_id, currency):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator,
|
||||
organization_id,
|
||||
currency,
|
||||
balance_type=BALANCE_TYPE_AVAILABLE,
|
||||
):
|
||||
"""Initialize the sensor"""
|
||||
_LOGGER.debug(f"Account Balance Sensor: {currency}")
|
||||
_LOGGER.debug(f"Account Balance Sensor: {balance_type} {currency}")
|
||||
self.coordinator = coordinator
|
||||
self.currency = currency
|
||||
self._organization_id = organization_id
|
||||
self.organization_id = organization_id
|
||||
self.balance_type = balance_type
|
||||
self._available = 0.00
|
||||
self._pending = 0.00
|
||||
self._total_balance = 0.00
|
||||
@ -85,12 +132,13 @@ class NiceHashBalanceSensor(Entity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Sensor name"""
|
||||
return f"{DEFAULT_NAME} Account Balance {self.currency}"
|
||||
balance_type = self.balance_type[0].upper() + self.balance_type[1:]
|
||||
return f"{DEFAULT_NAME} {balance_type} Account Balance {self.currency}"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Unique entity id"""
|
||||
return f"{self._organization_id}:{self.currency}"
|
||||
return f"{self.organization_id}:{self.currency}:{self.balance_type}"
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
@ -110,16 +158,28 @@ class NiceHashBalanceSensor(Entity):
|
||||
self._pending = float(total.get("pending"))
|
||||
self._available = float(total.get("available"))
|
||||
self._total_balance = float(total.get("totalBalance"))
|
||||
|
||||
if self.balance_type == BALANCE_TYPE_TOTAL:
|
||||
balance = self._total_balance
|
||||
elif self.balance_type == BALANCE_TYPE_PENDING:
|
||||
balance = self._pending
|
||||
else:
|
||||
balance = self._available
|
||||
|
||||
if self.currency == CURRENCY_BTC:
|
||||
return self._available
|
||||
return balance
|
||||
else:
|
||||
exchange_rates = self.coordinator.data.get("exchange_rates")
|
||||
self._exchange_rate = exchange_rates.get(f"{CURRENCY_BTC}-{self.currency}")
|
||||
return round(self._available * self._exchange_rate, 2)
|
||||
return round(balance * self._exchange_rate, 2)
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Sensor icon"""
|
||||
if self.currency == CURRENCY_EUR:
|
||||
return ICON_CURRENCY_EUR
|
||||
elif self.currency == CURRENCY_USD:
|
||||
return ICON_CURRENCY_USD
|
||||
return ICON_CURRENCY_BTC
|
||||
|
||||
@property
|
||||
|
Loading…
Reference in New Issue
Block a user