Filter Coinbase account wallets (#18167)

Only add sensor entities for accounts with the specified currencies.
This is a none breaking change.
If it's not specified then all account wallets will be loaded.
This commit is contained in:
Jorim Tielemans 2018-11-12 11:26:05 +01:00 committed by Fabian Affolter
parent afd9c44ffb
commit 9eac11dcbe

View File

@ -21,6 +21,7 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = 'coinbase' DOMAIN = 'coinbase'
CONF_API_SECRET = 'api_secret' CONF_API_SECRET = 'api_secret'
CONF_ACCOUNT_CURRENCIES = 'account_balance_currencies'
CONF_EXCHANGE_CURRENCIES = 'exchange_rate_currencies' CONF_EXCHANGE_CURRENCIES = 'exchange_rate_currencies'
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
@ -31,6 +32,8 @@ CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({ DOMAIN: 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.Optional(CONF_ACCOUNT_CURRENCIES):
vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_EXCHANGE_CURRENCIES, default=[]): vol.Optional(CONF_EXCHANGE_CURRENCIES, default=[]):
vol.All(cv.ensure_list, [cv.string]) vol.All(cv.ensure_list, [cv.string])
}) })
@ -45,6 +48,7 @@ def setup(hass, config):
""" """
api_key = config[DOMAIN].get(CONF_API_KEY) api_key = config[DOMAIN].get(CONF_API_KEY)
api_secret = config[DOMAIN].get(CONF_API_SECRET) api_secret = config[DOMAIN].get(CONF_API_SECRET)
account_currencies = config[DOMAIN].get(CONF_ACCOUNT_CURRENCIES)
exchange_currencies = config[DOMAIN].get(CONF_EXCHANGE_CURRENCIES) exchange_currencies = config[DOMAIN].get(CONF_EXCHANGE_CURRENCIES)
hass.data[DATA_COINBASE] = coinbase_data = CoinbaseData( hass.data[DATA_COINBASE] = coinbase_data = CoinbaseData(
@ -53,7 +57,13 @@ def setup(hass, config):
if not hasattr(coinbase_data, 'accounts'): if not hasattr(coinbase_data, 'accounts'):
return False return False
for account in coinbase_data.accounts.data: for account in coinbase_data.accounts.data:
load_platform(hass, 'sensor', DOMAIN, {'account': account}, config) if (account_currencies is None or
account.currency in account_currencies):
load_platform(hass,
'sensor',
DOMAIN,
{'account': account},
config)
for currency in exchange_currencies: for currency in exchange_currencies:
if currency not in coinbase_data.exchange_rates.rates: if currency not in coinbase_data.exchange_rates.rates:
_LOGGER.warning("Currency %s not found", currency) _LOGGER.warning("Currency %s not found", currency)