2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Coinbase."""
|
2018-01-10 06:47:22 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2018-01-28 17:04:40 +00:00
|
|
|
|
2019-10-18 22:05:42 +00:00
|
|
|
from coinbase.wallet.client import Client
|
|
|
|
from coinbase.wallet.error import AuthenticationError
|
2018-01-10 06:47:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_API_KEY
|
2018-01-28 17:04:40 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-01-10 06:47:22 +00:00
|
|
|
from homeassistant.helpers.discovery import load_platform
|
2018-01-28 17:04:40 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2018-01-10 06:47:22 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "coinbase"
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_API_SECRET = "api_secret"
|
|
|
|
CONF_ACCOUNT_CURRENCIES = "account_balance_currencies"
|
|
|
|
CONF_EXCHANGE_CURRENCIES = "exchange_rate_currencies"
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_COINBASE = "coinbase_cache"
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_API_SECRET): cv.string,
|
|
|
|
vol.Optional(CONF_ACCOUNT_CURRENCIES): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_EXCHANGE_CURRENCIES, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Set up the Coinbase component.
|
|
|
|
|
|
|
|
Will automatically setup sensors to support
|
|
|
|
wallets discovered on the network.
|
|
|
|
"""
|
2020-04-14 18:38:55 +00:00
|
|
|
api_key = config[DOMAIN][CONF_API_KEY]
|
|
|
|
api_secret = config[DOMAIN][CONF_API_SECRET]
|
2018-11-12 10:26:05 +00:00
|
|
|
account_currencies = config[DOMAIN].get(CONF_ACCOUNT_CURRENCIES)
|
2020-04-14 18:38:55 +00:00
|
|
|
exchange_currencies = config[DOMAIN][CONF_EXCHANGE_CURRENCIES]
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DATA_COINBASE] = coinbase_data = CoinbaseData(api_key, api_secret)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if not hasattr(coinbase_data, "accounts"):
|
2018-01-10 06:47:22 +00:00
|
|
|
return False
|
|
|
|
for account in coinbase_data.accounts.data:
|
2019-07-31 19:25:30 +00:00
|
|
|
if account_currencies is None or account.currency in account_currencies:
|
|
|
|
load_platform(hass, "sensor", DOMAIN, {"account": account}, config)
|
2018-01-10 06:47:22 +00:00
|
|
|
for currency in exchange_currencies:
|
|
|
|
if currency not in coinbase_data.exchange_rates.rates:
|
|
|
|
_LOGGER.warning("Currency %s not found", currency)
|
|
|
|
continue
|
|
|
|
native = coinbase_data.exchange_rates.currency
|
2019-07-31 19:25:30 +00:00
|
|
|
load_platform(
|
|
|
|
hass,
|
|
|
|
"sensor",
|
|
|
|
DOMAIN,
|
|
|
|
{"native_currency": native, "exchange_currency": currency},
|
|
|
|
config,
|
|
|
|
)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class CoinbaseData:
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Get the latest data and update the states."""
|
|
|
|
|
|
|
|
def __init__(self, api_key, api_secret):
|
|
|
|
"""Init the coinbase data object."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-01-10 06:47:22 +00:00
|
|
|
self.client = Client(api_key, api_secret)
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from coinbase."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-01-10 06:47:22 +00:00
|
|
|
try:
|
|
|
|
self.accounts = self.client.get_accounts()
|
|
|
|
self.exchange_rates = self.client.get_exchange_rates()
|
|
|
|
except AuthenticationError as coinbase_error:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
2020-01-02 19:17:10 +00:00
|
|
|
"Authentication error connecting to coinbase: %s", coinbase_error
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|