2020-06-16 04:21:14 +02:00
|
|
|
"""
|
|
|
|
Integrates NiceHash with Home Assistant
|
|
|
|
|
|
|
|
For more details about this integration, please refer to
|
|
|
|
https://github.com/brianberg/ha-nicehash
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_DEVICES, CONF_TIMEOUT
|
|
|
|
from homeassistant.core import Config, HomeAssistant
|
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-06-16 06:12:50 +02:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2020-06-16 04:21:14 +02:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_API_SECRET,
|
|
|
|
CONF_CURRENCY,
|
|
|
|
CONF_ORGANIZATION_ID,
|
2020-07-09 01:50:44 +02:00
|
|
|
CONF_RIGS_ENABLED,
|
|
|
|
CONF_DEVICES_ENABLED,
|
2020-07-11 20:40:42 +02:00
|
|
|
CONF_PAYOUTS_ENABLED,
|
2020-06-17 02:58:47 +02:00
|
|
|
CURRENCY_USD,
|
2020-06-16 04:21:14 +02:00
|
|
|
DOMAIN,
|
|
|
|
STARTUP_MESSAGE,
|
|
|
|
)
|
2020-06-17 03:46:53 +02:00
|
|
|
from .nicehash import NiceHashPrivateClient
|
2020-07-11 20:56:56 +02:00
|
|
|
from .coordinators import (
|
2020-07-11 18:17:02 +02:00
|
|
|
AccountsDataUpdateCoordinator,
|
2020-07-11 20:40:42 +02:00
|
|
|
MiningPayoutsDataUpdateCoordinator,
|
2020-07-11 18:17:02 +02:00
|
|
|
MiningRigsDataUpdateCoordinator,
|
2020-06-17 03:46:53 +02:00
|
|
|
)
|
2020-06-16 04:21:14 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ORGANIZATION_ID): cv.string,
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_API_SECRET): cv.string,
|
2020-06-17 02:58:47 +02:00
|
|
|
vol.Required(CONF_CURRENCY, default=CURRENCY_USD): cv.string,
|
2020-07-09 01:50:44 +02:00
|
|
|
vol.Required(CONF_RIGS_ENABLED, default=False): cv.boolean,
|
|
|
|
vol.Required(CONF_DEVICES_ENABLED, default=False): cv.boolean,
|
2020-07-11 20:40:42 +02:00
|
|
|
vol.Required(CONF_PAYOUTS_ENABLED, default=False): cv.boolean,
|
2020-06-16 04:21:14 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: Config):
|
|
|
|
"""Set up this integration"""
|
|
|
|
if hass.data.get(DOMAIN) is None:
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
_LOGGER.debug(STARTUP_MESSAGE)
|
|
|
|
|
|
|
|
nicehash_config = config[DOMAIN]
|
2020-07-09 01:50:44 +02:00
|
|
|
# Configuration
|
2020-06-16 04:21:14 +02:00
|
|
|
organization_id = nicehash_config.get(CONF_ORGANIZATION_ID)
|
|
|
|
api_key = nicehash_config.get(CONF_API_KEY)
|
|
|
|
api_secret = nicehash_config.get(CONF_API_SECRET)
|
2020-07-09 01:50:44 +02:00
|
|
|
# Options
|
2020-06-17 02:58:47 +02:00
|
|
|
currency = nicehash_config.get(CONF_CURRENCY).upper()
|
2020-07-09 01:50:44 +02:00
|
|
|
rigs_enabled = nicehash_config.get(CONF_RIGS_ENABLED)
|
|
|
|
devices_enabled = nicehash_config.get(CONF_DEVICES_ENABLED)
|
2020-07-11 20:40:42 +02:00
|
|
|
payouts_enabled = nicehash_config.get(CONF_PAYOUTS_ENABLED)
|
2020-06-16 04:21:14 +02:00
|
|
|
|
|
|
|
client = NiceHashPrivateClient(organization_id, api_key, api_secret)
|
|
|
|
|
2020-07-11 20:40:42 +02:00
|
|
|
hass.data[DOMAIN]["organization_id"] = organization_id
|
|
|
|
hass.data[DOMAIN]["client"] = client
|
|
|
|
hass.data[DOMAIN]["currency"] = currency
|
|
|
|
hass.data[DOMAIN]["rigs_enabled"] = rigs_enabled
|
|
|
|
hass.data[DOMAIN]["devices_enabled"] = devices_enabled
|
|
|
|
hass.data[DOMAIN]["payouts_enabled"] = payouts_enabled
|
2020-06-16 06:12:50 +02:00
|
|
|
|
2020-07-11 20:40:42 +02:00
|
|
|
# Accounts
|
|
|
|
accounts_coordinator = AccountsDataUpdateCoordinator(hass, client)
|
2020-06-16 06:12:50 +02:00
|
|
|
await accounts_coordinator.async_refresh()
|
|
|
|
|
|
|
|
if not accounts_coordinator.last_update_success:
|
|
|
|
_LOGGER.error("Unable to get NiceHash accounts")
|
|
|
|
raise PlatformNotReady
|
|
|
|
|
|
|
|
hass.data[DOMAIN]["accounts_coordinator"] = accounts_coordinator
|
2020-07-09 01:50:44 +02:00
|
|
|
|
2020-07-11 20:40:42 +02:00
|
|
|
# Payouts
|
|
|
|
if payouts_enabled:
|
|
|
|
payouts_coordinator = MiningPayoutsDataUpdateCoordinator(hass, client)
|
|
|
|
await payouts_coordinator.async_refresh()
|
|
|
|
|
|
|
|
if not payouts_coordinator.last_update_success:
|
|
|
|
_LOGGER.error("Unable to get NiceHash mining payouts")
|
|
|
|
raise PlatformNotReady
|
|
|
|
|
|
|
|
hass.data[DOMAIN]["payouts_coordinator"] = payouts_coordinator
|
|
|
|
|
|
|
|
# Rigs
|
2020-07-09 01:50:44 +02:00
|
|
|
if rigs_enabled or devices_enabled:
|
2020-07-11 18:17:02 +02:00
|
|
|
rigs_coordinator = MiningRigsDataUpdateCoordinator(hass, client)
|
2020-07-09 01:50:44 +02:00
|
|
|
await rigs_coordinator.async_refresh()
|
|
|
|
|
|
|
|
if not rigs_coordinator.last_update_success:
|
|
|
|
_LOGGER.error("Unable to get NiceHash mining rigs")
|
|
|
|
raise PlatformNotReady
|
|
|
|
|
|
|
|
hass.data[DOMAIN]["rigs_coordinator"] = rigs_coordinator
|
2020-06-16 06:12:50 +02:00
|
|
|
|
|
|
|
await discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config)
|
|
|
|
|
|
|
|
return True
|