feat: allow disabling rig/device sensors

- via "rigs" and "devices" configuration options
- both default to false
This commit is contained in:
Brian Berg 2020-07-08 23:50:44 +00:00
parent d5f9f56f98
commit 1f33d3e05d
3 changed files with 77 additions and 52 deletions

View File

@ -18,6 +18,8 @@ from .const import (
CONF_API_SECRET, CONF_API_SECRET,
CONF_CURRENCY, CONF_CURRENCY,
CONF_ORGANIZATION_ID, CONF_ORGANIZATION_ID,
CONF_RIGS_ENABLED,
CONF_DEVICES_ENABLED,
CURRENCY_USD, CURRENCY_USD,
DOMAIN, DOMAIN,
STARTUP_MESSAGE, STARTUP_MESSAGE,
@ -38,6 +40,8 @@ CONFIG_SCHEMA = vol.Schema(
vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_API_SECRET): cv.string, vol.Required(CONF_API_SECRET): cv.string,
vol.Required(CONF_CURRENCY, default=CURRENCY_USD): cv.string, vol.Required(CONF_CURRENCY, default=CURRENCY_USD): cv.string,
vol.Required(CONF_RIGS_ENABLED, default=False): cv.boolean,
vol.Required(CONF_DEVICES_ENABLED, default=False): cv.boolean,
} }
) )
}, },
@ -52,15 +56,18 @@ async def async_setup(hass: HomeAssistant, config: Config):
_LOGGER.debug(STARTUP_MESSAGE) _LOGGER.debug(STARTUP_MESSAGE)
nicehash_config = config[DOMAIN] nicehash_config = config[DOMAIN]
# Configuration
organization_id = nicehash_config.get(CONF_ORGANIZATION_ID) organization_id = nicehash_config.get(CONF_ORGANIZATION_ID)
api_key = nicehash_config.get(CONF_API_KEY) api_key = nicehash_config.get(CONF_API_KEY)
api_secret = nicehash_config.get(CONF_API_SECRET) api_secret = nicehash_config.get(CONF_API_SECRET)
# Options
currency = nicehash_config.get(CONF_CURRENCY).upper() currency = nicehash_config.get(CONF_CURRENCY).upper()
rigs_enabled = nicehash_config.get(CONF_RIGS_ENABLED)
devices_enabled = nicehash_config.get(CONF_DEVICES_ENABLED)
client = NiceHashPrivateClient(organization_id, api_key, api_secret) client = NiceHashPrivateClient(organization_id, api_key, api_secret)
accounts_coordinator = NiceHashAccountsDataUpdateCoordinator(hass, client) accounts_coordinator = NiceHashAccountsDataUpdateCoordinator(hass, client)
rigs_coordinator = NiceHashMiningRigsDataUpdateCoordinator(hass, client)
await accounts_coordinator.async_refresh() await accounts_coordinator.async_refresh()
@ -68,16 +75,21 @@ async def async_setup(hass: HomeAssistant, config: Config):
_LOGGER.error("Unable to get NiceHash accounts") _LOGGER.error("Unable to get NiceHash accounts")
raise PlatformNotReady 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
if rigs_enabled or devices_enabled:
rigs_coordinator = NiceHashMiningRigsDataUpdateCoordinator(hass, client)
await rigs_coordinator.async_refresh() await rigs_coordinator.async_refresh()
if not rigs_coordinator.last_update_success: if not rigs_coordinator.last_update_success:
_LOGGER.error("Unable to get NiceHash mining rigs") _LOGGER.error("Unable to get NiceHash mining rigs")
raise PlatformNotReady raise PlatformNotReady
hass.data[DOMAIN]["organization_id"] = organization_id hass.data[DOMAIN]["rigs_enabled"] = rigs_enabled
hass.data[DOMAIN]["client"] = client hass.data[DOMAIN]["devices_enabled"] = devices_enabled
hass.data[DOMAIN]["currency"] = currency
hass.data[DOMAIN]["accounts_coordinator"] = accounts_coordinator
hass.data[DOMAIN]["rigs_coordinator"] = rigs_coordinator hass.data[DOMAIN]["rigs_coordinator"] = rigs_coordinator
await discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config) await discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config)

View File

@ -25,11 +25,12 @@ PLATFORMS = [SENSOR]
# Configuration and options # Configuration and options
CONF_ENABLED = "enabled"
CONF_API_KEY = "api_key" CONF_API_KEY = "api_key"
CONF_API_SECRET = "api_secret" CONF_API_SECRET = "api_secret"
CONF_ORGANIZATION_ID = "organization_id" CONF_ORGANIZATION_ID = "organization_id"
CONF_CURRENCY = "currency" CONF_CURRENCY = "currency"
CONF_RIGS_ENABLED = "rigs"
CONF_DEVICES_ENABLED = "devices"
# Defaults # Defaults

View File

@ -3,7 +3,6 @@ Sensor platform for NiceHash
""" """
import logging import logging
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import Config, HomeAssistant from homeassistant.core import Config, HomeAssistant
@ -46,28 +45,56 @@ async def async_setup_platform(
_LOGGER.debug("Creating new NiceHash sensor components") _LOGGER.debug("Creating new NiceHash sensor components")
data = hass.data[DOMAIN] data = hass.data[DOMAIN]
# Configuration
organization_id = data.get("organization_id") organization_id = data.get("organization_id")
client = data.get("client") client = data.get("client")
# Options
currency = data.get("currency") currency = data.get("currency")
accounts_coordinator = data.get("accounts_coordinator") rigs_enabled = data.get("rigs_enabled")
rigs_coordinator = data.get("rigs_coordinator") devices_enabled = data.get("devices_enabled")
print(f"rigs: {rigs_enabled}")
print(f"devices: {devices_enabled}")
# Add account balance sensors # Add account balance sensors
accounts_coordinator = data.get("accounts_coordinator")
balance_sensors = create_balance_sensors(
organization_id, currency, accounts_coordinator
)
async_add_entities(balance_sensors, True)
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")
# Add mining rig sensors if enabled
if rigs_enabled:
rig_sensors = create_rig_sensors(mining_rigs, rigs_coordinator)
async_add_entities(rig_sensors, True)
# Add device sensors if enabled
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):
balance_sensors = [ balance_sensors = [
NiceHashBalanceSensor( NiceHashBalanceSensor(
accounts_coordinator, coordinator,
organization_id, organization_id,
currency=CURRENCY_BTC, currency=CURRENCY_BTC,
balance_type=BALANCE_TYPE_AVAILABLE, balance_type=BALANCE_TYPE_AVAILABLE,
), ),
NiceHashBalanceSensor( NiceHashBalanceSensor(
accounts_coordinator, coordinator,
organization_id, organization_id,
currency=CURRENCY_BTC, currency=CURRENCY_BTC,
balance_type=BALANCE_TYPE_PENDING, balance_type=BALANCE_TYPE_PENDING,
), ),
NiceHashBalanceSensor( NiceHashBalanceSensor(
accounts_coordinator, coordinator,
organization_id, organization_id,
currency=CURRENCY_BTC, currency=CURRENCY_BTC,
balance_type=BALANCE_TYPE_TOTAL, balance_type=BALANCE_TYPE_TOTAL,
@ -76,7 +103,7 @@ async def async_setup_platform(
if currency == CURRENCY_USD or currency == CURRENCY_EUR: if currency == CURRENCY_USD or currency == CURRENCY_EUR:
balance_sensors.append( balance_sensors.append(
NiceHashBalanceSensor( NiceHashBalanceSensor(
accounts_coordinator, coordinator,
organization_id, organization_id,
currency=currency, currency=currency,
balance_type=BALANCE_TYPE_AVAILABLE, balance_type=BALANCE_TYPE_AVAILABLE,
@ -84,7 +111,7 @@ async def async_setup_platform(
) )
balance_sensors.append( balance_sensors.append(
NiceHashBalanceSensor( NiceHashBalanceSensor(
accounts_coordinator, coordinator,
organization_id, organization_id,
currency=currency, currency=currency,
balance_type=BALANCE_TYPE_PENDING, balance_type=BALANCE_TYPE_PENDING,
@ -92,7 +119,7 @@ async def async_setup_platform(
) )
balance_sensors.append( balance_sensors.append(
NiceHashBalanceSensor( NiceHashBalanceSensor(
accounts_coordinator, coordinator,
organization_id, organization_id,
currency=currency, currency=currency,
balance_type=BALANCE_TYPE_TOTAL, balance_type=BALANCE_TYPE_TOTAL,
@ -101,49 +128,34 @@ async def async_setup_platform(
else: else:
_LOGGER.warn("Invalid currency: must be EUR or USD") _LOGGER.warn("Invalid currency: must be EUR or USD")
async_add_entities(balance_sensors, True) return balance_sensors
# Add mining rig sensors
rig_data = await client.get_mining_rigs()
mining_rigs = rig_data.get("miningRigs")
# Add status sensors def create_rig_sensors(mining_rigs, coordinator):
async_add_entities( rig_sensors = []
[NiceHashRigStatusSensor(rigs_coordinator, rig) for rig in mining_rigs], True, for rig in mining_rigs:
) rig_sensors.append(NiceHashRigStatusSensor(coordinator, rig))
# Add temperature sensors rig_sensors.append(NiceHashRigTemperatureSensor(coordinator, rig))
async_add_entities( rig_sensors.append(NiceHashRigProfitabilitySensor(coordinator, rig))
[NiceHashRigTemperatureSensor(rigs_coordinator, rig) for rig in mining_rigs],
True, return rig_sensors
)
# Add profitability sensors
async_add_entities( def create_device_sensors(mining_rigs, coordinator):
[NiceHashRigProfitabilitySensor(rigs_coordinator, rig) for rig in mining_rigs],
True,
)
# Add device sensors
device_sensors = [] device_sensors = []
for rig in mining_rigs: for rig in mining_rigs:
devices = rig.get("devices") devices = rig.get("devices")
for i in range(len(devices)): for i in range(len(devices)):
device = devices[i] device = devices[i]
device_sensors.append( device_sensors.append(
NiceHashDeviceAlgorithmSensor(rigs_coordinator, rig, device) NiceHashDeviceAlgorithmSensor(coordinator, rig, device)
) )
device_sensors.append(NiceHashDeviceSpeedSensor(coordinator, rig, device))
device_sensors.append(NiceHashDeviceStatusSensor(coordinator, rig, device))
device_sensors.append( device_sensors.append(
NiceHashDeviceSpeedSensor(rigs_coordinator, rig, device) NiceHashDeviceTemperatureSensor(coordinator, rig, device)
)
device_sensors.append(
NiceHashDeviceStatusSensor(rigs_coordinator, rig, device)
)
device_sensors.append(
NiceHashDeviceTemperatureSensor(rigs_coordinator, rig, device)
)
device_sensors.append(
NiceHashDeviceLoadSensor(rigs_coordinator, rig, device)
)
device_sensors.append(
NiceHashDeviceRPMSensor(rigs_coordinator, rig, device)
) )
device_sensors.append(NiceHashDeviceLoadSensor(coordinator, rig, device))
device_sensors.append(NiceHashDeviceRPMSensor(coordinator, rig, device))
async_add_entities(device_sensors, True) return device_sensors