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-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
|
|
|
|
from .data_coordinators import (
|
|
|
|
NiceHashAccountsDataUpdateCoordinator,
|
|
|
|
NiceHashMiningRigsDataUpdateCoordinator,
|
|
|
|
)
|
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-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]
|
|
|
|
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-06-17 02:58:47 +02:00
|
|
|
currency = nicehash_config.get(CONF_CURRENCY).upper()
|
2020-06-16 04:21:14 +02:00
|
|
|
|
|
|
|
client = NiceHashPrivateClient(organization_id, api_key, api_secret)
|
|
|
|
|
2020-06-16 06:12:50 +02:00
|
|
|
accounts_coordinator = NiceHashAccountsDataUpdateCoordinator(hass, client)
|
|
|
|
rigs_coordinator = NiceHashMiningRigsDataUpdateCoordinator(hass, client)
|
|
|
|
|
|
|
|
await accounts_coordinator.async_refresh()
|
|
|
|
|
|
|
|
if not accounts_coordinator.last_update_success:
|
|
|
|
_LOGGER.error("Unable to get NiceHash accounts")
|
|
|
|
raise PlatformNotReady
|
|
|
|
|
|
|
|
await rigs_coordinator.async_refresh()
|
|
|
|
|
|
|
|
if not rigs_coordinator.last_update_success:
|
|
|
|
_LOGGER.error("Unable to get NiceHash mining rigs")
|
|
|
|
raise PlatformNotReady
|
|
|
|
|
2020-06-17 02:58:47 +02:00
|
|
|
hass.data[DOMAIN]["organization_id"] = organization_id
|
2020-06-16 06:12:50 +02:00
|
|
|
hass.data[DOMAIN]["client"] = client
|
|
|
|
hass.data[DOMAIN]["currency"] = currency
|
|
|
|
hass.data[DOMAIN]["accounts_coordinator"] = accounts_coordinator
|
|
|
|
hass.data[DOMAIN]["rigs_coordinator"] = rigs_coordinator
|
|
|
|
|
|
|
|
await discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config)
|
|
|
|
|
|
|
|
return True
|