2020-06-16 04:21:14 +02:00
|
|
|
"""
|
|
|
|
Sensor platform for NiceHash
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-06-17 03:00:44 +02:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2020-06-16 04:21:14 +02:00
|
|
|
from homeassistant.core import Config, HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from .const import (
|
2020-06-17 02:58:47 +02:00
|
|
|
BALANCE_TYPE_AVAILABLE,
|
|
|
|
BALANCE_TYPE_PENDING,
|
|
|
|
BALANCE_TYPE_TOTAL,
|
2020-06-16 04:21:14 +02:00
|
|
|
CURRENCY_BTC,
|
2020-06-16 06:12:50 +02:00
|
|
|
CURRENCY_EUR,
|
|
|
|
CURRENCY_USD,
|
2020-06-16 04:21:14 +02:00
|
|
|
DOMAIN,
|
2020-07-05 20:02:46 +02:00
|
|
|
DEVICE_LOAD,
|
|
|
|
DEVICE_RPM,
|
|
|
|
DEVICE_SPEED_RATE,
|
|
|
|
DEVICE_SPEED_ALGORITHM,
|
2020-06-16 04:21:14 +02:00
|
|
|
)
|
|
|
|
from .nicehash import NiceHashPrivateClient, NiceHashPublicClient
|
2020-07-09 02:31:02 +02:00
|
|
|
from .account_sensors import BalanceSensor
|
2020-07-11 20:40:42 +02:00
|
|
|
from .payout_sensors import RecentMiningPayoutSensor
|
2020-07-09 02:05:11 +02:00
|
|
|
from .rig_sensors import (
|
2020-07-09 02:31:02 +02:00
|
|
|
RigStatusSensor,
|
|
|
|
RigTemperatureSensor,
|
|
|
|
RigProfitabilitySensor,
|
2020-07-09 02:05:11 +02:00
|
|
|
)
|
|
|
|
from .device_sensors import (
|
2020-07-09 02:31:02 +02:00
|
|
|
DeviceAlgorithmSensor,
|
|
|
|
DeviceSpeedSensor,
|
|
|
|
DeviceStatusSensor,
|
|
|
|
DeviceLoadSensor,
|
|
|
|
DeviceRPMSensor,
|
|
|
|
DeviceTemperatureSensor,
|
2020-06-17 05:18:24 +02:00
|
|
|
)
|
2020-06-16 04:21:14 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant, config: Config, async_add_entities, discovery_info=None
|
|
|
|
):
|
|
|
|
"""Setup NiceHash sensor platform"""
|
|
|
|
_LOGGER.debug("Creating new NiceHash sensor components")
|
|
|
|
|
2020-06-16 06:12:50 +02:00
|
|
|
data = hass.data[DOMAIN]
|
2020-07-09 01:50:44 +02:00
|
|
|
# Configuration
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id = data.get("organization_id")
|
2020-06-16 06:12:50 +02:00
|
|
|
client = data.get("client")
|
2020-07-09 01:50:44 +02:00
|
|
|
# Options
|
2020-06-16 06:12:50 +02:00
|
|
|
currency = data.get("currency")
|
2020-07-11 20:40:42 +02:00
|
|
|
payouts_enabled = data.get("payouts_enabled")
|
2020-07-09 01:50:44 +02:00
|
|
|
rigs_enabled = data.get("rigs_enabled")
|
|
|
|
devices_enabled = data.get("devices_enabled")
|
|
|
|
|
2020-06-17 02:58:47 +02:00
|
|
|
# Add account balance sensors
|
2020-07-09 01:50:44 +02:00
|
|
|
accounts_coordinator = data.get("accounts_coordinator")
|
|
|
|
balance_sensors = create_balance_sensors(
|
|
|
|
organization_id, currency, accounts_coordinator
|
|
|
|
)
|
|
|
|
async_add_entities(balance_sensors, True)
|
|
|
|
|
2020-07-11 20:40:42 +02:00
|
|
|
# Payout sensors
|
|
|
|
if payouts_enabled:
|
|
|
|
payouts_coordinator = data.get("payouts_coordinator")
|
|
|
|
payout_sensors = create_payout_sensors(organization_id, payouts_coordinator)
|
|
|
|
async_add_entities(payout_sensors)
|
|
|
|
|
|
|
|
# Mining rig and device sensors
|
2020-07-09 01:50:44 +02:00
|
|
|
if rigs_enabled or devices_enabled:
|
|
|
|
rigs_coordinator = data.get("rigs_coordinator")
|
|
|
|
rig_data = await client.get_mining_rigs()
|
|
|
|
mining_rigs = rig_data.get("miningRigs")
|
|
|
|
|
|
|
|
if rigs_enabled:
|
|
|
|
rig_sensors = create_rig_sensors(mining_rigs, rigs_coordinator)
|
|
|
|
async_add_entities(rig_sensors, True)
|
|
|
|
|
|
|
|
if devices_enabled:
|
|
|
|
device_sensors = create_device_sensors(mining_rigs, rigs_coordinator)
|
|
|
|
async_add_entities(device_sensors, True)
|
|
|
|
|
|
|
|
|
|
|
|
def create_balance_sensors(organization_id, currency, coordinator):
|
2020-06-17 02:58:47 +02:00
|
|
|
balance_sensors = [
|
2020-07-09 02:31:02 +02:00
|
|
|
BalanceSensor(
|
2020-07-09 01:50:44 +02:00
|
|
|
coordinator,
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id,
|
|
|
|
currency=CURRENCY_BTC,
|
|
|
|
balance_type=BALANCE_TYPE_AVAILABLE,
|
|
|
|
),
|
2020-07-09 02:31:02 +02:00
|
|
|
BalanceSensor(
|
2020-07-09 01:50:44 +02:00
|
|
|
coordinator,
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id,
|
|
|
|
currency=CURRENCY_BTC,
|
|
|
|
balance_type=BALANCE_TYPE_PENDING,
|
|
|
|
),
|
2020-07-09 02:31:02 +02:00
|
|
|
BalanceSensor(
|
2020-07-09 01:50:44 +02:00
|
|
|
coordinator,
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id,
|
|
|
|
currency=CURRENCY_BTC,
|
|
|
|
balance_type=BALANCE_TYPE_TOTAL,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
if currency == CURRENCY_USD or currency == CURRENCY_EUR:
|
|
|
|
balance_sensors.append(
|
2020-07-09 02:31:02 +02:00
|
|
|
BalanceSensor(
|
2020-07-09 01:50:44 +02:00
|
|
|
coordinator,
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id,
|
|
|
|
currency=currency,
|
|
|
|
balance_type=BALANCE_TYPE_AVAILABLE,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
balance_sensors.append(
|
2020-07-09 02:31:02 +02:00
|
|
|
BalanceSensor(
|
2020-07-09 01:50:44 +02:00
|
|
|
coordinator,
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id,
|
|
|
|
currency=currency,
|
|
|
|
balance_type=BALANCE_TYPE_PENDING,
|
|
|
|
)
|
2020-06-16 06:12:50 +02:00
|
|
|
)
|
2020-06-17 02:58:47 +02:00
|
|
|
balance_sensors.append(
|
2020-07-09 02:31:02 +02:00
|
|
|
BalanceSensor(
|
2020-07-09 01:50:44 +02:00
|
|
|
coordinator,
|
2020-06-17 02:58:47 +02:00
|
|
|
organization_id,
|
|
|
|
currency=currency,
|
|
|
|
balance_type=BALANCE_TYPE_TOTAL,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
_LOGGER.warn("Invalid currency: must be EUR or USD")
|
|
|
|
|
2020-07-09 01:50:44 +02:00
|
|
|
return balance_sensors
|
2020-06-16 04:21:14 +02:00
|
|
|
|
2020-07-05 20:02:46 +02:00
|
|
|
|
2020-07-11 20:40:42 +02:00
|
|
|
def create_payout_sensors(organization_id, coordinator):
|
|
|
|
payout_sensors = []
|
|
|
|
payout_sensors.append(RecentMiningPayoutSensor(coordinator, organization_id))
|
|
|
|
|
|
|
|
return payout_sensors
|
|
|
|
|
|
|
|
|
2020-07-09 01:50:44 +02:00
|
|
|
def create_rig_sensors(mining_rigs, coordinator):
|
|
|
|
rig_sensors = []
|
|
|
|
for rig in mining_rigs:
|
2020-07-09 02:31:02 +02:00
|
|
|
rig_sensors.append(RigStatusSensor(coordinator, rig))
|
|
|
|
rig_sensors.append(RigTemperatureSensor(coordinator, rig))
|
|
|
|
rig_sensors.append(RigProfitabilitySensor(coordinator, rig))
|
2020-07-09 01:50:44 +02:00
|
|
|
|
|
|
|
return rig_sensors
|
|
|
|
|
|
|
|
|
|
|
|
def create_device_sensors(mining_rigs, coordinator):
|
2020-07-05 20:02:46 +02:00
|
|
|
device_sensors = []
|
|
|
|
for rig in mining_rigs:
|
|
|
|
devices = rig.get("devices")
|
2020-07-09 04:20:55 +02:00
|
|
|
for device in devices:
|
2020-07-09 02:31:02 +02:00
|
|
|
device_sensors.append(DeviceAlgorithmSensor(coordinator, rig, device))
|
|
|
|
device_sensors.append(DeviceSpeedSensor(coordinator, rig, device))
|
|
|
|
device_sensors.append(DeviceStatusSensor(coordinator, rig, device))
|
|
|
|
device_sensors.append(DeviceTemperatureSensor(coordinator, rig, device))
|
|
|
|
device_sensors.append(DeviceLoadSensor(coordinator, rig, device))
|
|
|
|
device_sensors.append(DeviceRPMSensor(coordinator, rig, device))
|
2020-07-05 20:02:46 +02:00
|
|
|
|
2020-07-09 01:50:44 +02:00
|
|
|
return device_sensors
|