Move components to folders (#20774)
* Move all components into folders * Move component tests into folders * Fix init moving * Move tests * Lint * Update coverage * Fix service descriptions * Update CODEOWNERS
This commit is contained in:
1 parent
d13b2ca6ef
commit
b8cc547fa3
275 files changed
+512
-684
No files matched your search
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
Support for Coinbase.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/coinbase/
|
||||
"""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.discovery import load_platform
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
REQUIREMENTS = ['coinbase==2.1.0']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = 'coinbase'
|
||||
|
||||
CONF_API_SECRET = 'api_secret'
|
||||
CONF_ACCOUNT_CURRENCIES = 'account_balance_currencies'
|
||||
CONF_EXCHANGE_CURRENCIES = 'exchange_rate_currencies'
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Coinbase component.
|
||||
|
||||
Will automatically setup sensors to support
|
||||
wallets discovered on the network.
|
||||
"""
|
||||
api_key = config[DOMAIN].get(CONF_API_KEY)
|
||||
api_secret = config[DOMAIN].get(CONF_API_SECRET)
|
||||
account_currencies = config[DOMAIN].get(CONF_ACCOUNT_CURRENCIES)
|
||||
exchange_currencies = config[DOMAIN].get(CONF_EXCHANGE_CURRENCIES)
|
||||
|
||||
hass.data[DATA_COINBASE] = coinbase_data = CoinbaseData(
|
||||
api_key, api_secret)
|
||||
|
||||
if not hasattr(coinbase_data, 'accounts'):
|
||||
return False
|
||||
for account in coinbase_data.accounts.data:
|
||||
if (account_currencies is None or
|
||||
account.currency in account_currencies):
|
||||
load_platform(hass,
|
||||
'sensor',
|
||||
DOMAIN,
|
||||
{'account': account},
|
||||
config)
|
||||
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
|
||||
load_platform(hass,
|
||||
'sensor',
|
||||
DOMAIN,
|
||||
{'native_currency': native,
|
||||
'exchange_currency': currency},
|
||||
config)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class CoinbaseData:
|
||||
"""Get the latest data and update the states."""
|
||||
|
||||
def __init__(self, api_key, api_secret):
|
||||
"""Init the coinbase data object."""
|
||||
from coinbase.wallet.client import Client
|
||||
self.client = Client(api_key, api_secret)
|
||||
self.update()
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Get the latest data from coinbase."""
|
||||
from coinbase.wallet.error import AuthenticationError
|
||||
try:
|
||||
self.accounts = self.client.get_accounts()
|
||||
self.exchange_rates = self.client.get_exchange_rates()
|
||||
except AuthenticationError as coinbase_error:
|
||||
_LOGGER.error("Authentication error connecting"
|
||||
" to coinbase: %s", coinbase_error)
|
||||
Reference in new issue
Block a user