Support user-defined base currency for Coinbase exchange rate sensors (#52879)
This commit is contained in:
parent
f152369944
commit
db97fd3d5b
6 changed files with 30 additions and 17 deletions
|
@ -20,6 +20,7 @@ from .const import (
|
|||
API_ACCOUNT_ID,
|
||||
API_ACCOUNTS_DATA,
|
||||
CONF_CURRENCIES,
|
||||
CONF_EXCHANGE_BASE,
|
||||
CONF_EXCHANGE_RATES,
|
||||
CONF_YAML_API_TOKEN,
|
||||
DOMAIN,
|
||||
|
@ -67,9 +68,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Coinbase from a config entry."""
|
||||
|
||||
instance = await hass.async_add_executor_job(
|
||||
create_and_update_instance, entry.data[CONF_API_KEY], entry.data[CONF_API_TOKEN]
|
||||
)
|
||||
instance = await hass.async_add_executor_job(create_and_update_instance, entry)
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(update_listener))
|
||||
|
||||
|
@ -91,10 +90,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
return unload_ok
|
||||
|
||||
|
||||
def create_and_update_instance(api_key, api_token):
|
||||
def create_and_update_instance(entry: ConfigEntry) -> CoinbaseData:
|
||||
"""Create and update a Coinbase Data instance."""
|
||||
client = Client(api_key, api_token)
|
||||
instance = CoinbaseData(client)
|
||||
client = Client(entry.data[CONF_API_KEY], entry.data[CONF_API_TOKEN])
|
||||
base_rate = entry.options.get(CONF_EXCHANGE_BASE, "USD")
|
||||
instance = CoinbaseData(client, base_rate)
|
||||
instance.update()
|
||||
return instance
|
||||
|
||||
|
@ -139,11 +139,12 @@ def get_accounts(client):
|
|||
class CoinbaseData:
|
||||
"""Get the latest data and update the states."""
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client, exchange_base):
|
||||
"""Init the coinbase data object."""
|
||||
|
||||
self.client = client
|
||||
self.accounts = None
|
||||
self.exchange_base = exchange_base
|
||||
self.exchange_rates = None
|
||||
self.user_id = self.client.get_current_user()[API_ACCOUNT_ID]
|
||||
|
||||
|
@ -153,7 +154,9 @@ class CoinbaseData:
|
|||
|
||||
try:
|
||||
self.accounts = get_accounts(self.client)
|
||||
self.exchange_rates = self.client.get_exchange_rates()
|
||||
self.exchange_rates = self.client.get_exchange_rates(
|
||||
currency=self.exchange_base
|
||||
)
|
||||
except AuthenticationError as coinbase_error:
|
||||
_LOGGER.error(
|
||||
"Authentication error connecting to coinbase: %s", coinbase_error
|
||||
|
|
|
@ -15,6 +15,7 @@ from .const import (
|
|||
API_ACCOUNT_CURRENCY,
|
||||
API_RATES,
|
||||
CONF_CURRENCIES,
|
||||
CONF_EXCHANGE_BASE,
|
||||
CONF_EXCHANGE_RATES,
|
||||
CONF_OPTIONS,
|
||||
CONF_YAML_API_TOKEN,
|
||||
|
@ -156,6 +157,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||
errors = {}
|
||||
default_currencies = self.config_entry.options.get(CONF_CURRENCIES, [])
|
||||
default_exchange_rates = self.config_entry.options.get(CONF_EXCHANGE_RATES, [])
|
||||
default_exchange_base = self.config_entry.options.get(CONF_EXCHANGE_BASE, "USD")
|
||||
|
||||
if user_input is not None:
|
||||
# Pass back user selected options, even if bad
|
||||
|
@ -165,6 +167,9 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||
if CONF_EXCHANGE_RATES in user_input:
|
||||
default_exchange_rates = user_input[CONF_EXCHANGE_RATES]
|
||||
|
||||
if CONF_EXCHANGE_RATES in user_input:
|
||||
default_exchange_base = user_input[CONF_EXCHANGE_BASE]
|
||||
|
||||
try:
|
||||
await validate_options(self.hass, self.config_entry, user_input)
|
||||
except CurrencyUnavaliable:
|
||||
|
@ -189,6 +194,10 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||
CONF_EXCHANGE_RATES,
|
||||
default=default_exchange_rates,
|
||||
): cv.multi_select(RATES),
|
||||
vol.Optional(
|
||||
CONF_EXCHANGE_BASE,
|
||||
default=default_exchange_base,
|
||||
): vol.In(WALLETS),
|
||||
}
|
||||
),
|
||||
errors=errors,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""Constants used for Coinbase."""
|
||||
|
||||
CONF_CURRENCIES = "account_balance_currencies"
|
||||
CONF_EXCHANGE_BASE = "exchange_base"
|
||||
CONF_EXCHANGE_RATES = "exchange_rate_currencies"
|
||||
CONF_OPTIONS = "options"
|
||||
DOMAIN = "coinbase"
|
||||
|
|
|
@ -49,7 +49,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
if CONF_CURRENCIES in config_entry.options:
|
||||
desired_currencies = config_entry.options[CONF_CURRENCIES]
|
||||
|
||||
exchange_native_currency = instance.exchange_rates[API_ACCOUNT_CURRENCY]
|
||||
exchange_base_currency = instance.exchange_rates[API_ACCOUNT_CURRENCY]
|
||||
|
||||
for currency in desired_currencies:
|
||||
if currency not in provided_currencies:
|
||||
|
@ -67,7 +67,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
ExchangeRateSensor(
|
||||
instance,
|
||||
rate,
|
||||
exchange_native_currency,
|
||||
exchange_base_currency,
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -149,7 +149,7 @@ class AccountSensor(SensorEntity):
|
|||
class ExchangeRateSensor(SensorEntity):
|
||||
"""Representation of a Coinbase.com sensor."""
|
||||
|
||||
def __init__(self, coinbase_data, exchange_currency, native_currency):
|
||||
def __init__(self, coinbase_data, exchange_currency, exchange_base):
|
||||
"""Initialize the sensor."""
|
||||
self._coinbase_data = coinbase_data
|
||||
self.currency = exchange_currency
|
||||
|
@ -158,7 +158,7 @@ class ExchangeRateSensor(SensorEntity):
|
|||
self._state = round(
|
||||
1 / float(self._coinbase_data.exchange_rates[API_RATES][self.currency]), 2
|
||||
)
|
||||
self._unit_of_measurement = native_currency
|
||||
self._unit_of_measurement = exchange_base
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
|
|
@ -25,7 +25,8 @@
|
|||
"description": "Adjust Coinbase Options",
|
||||
"data": {
|
||||
"account_balance_currencies": "Wallet balances to report.",
|
||||
"exchange_rate_currencies": "Exchange rates to report."
|
||||
"exchange_rate_currencies": "Exchange rates to report.",
|
||||
"exchange_base": "Base currency for exchange rate sensors."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -12,9 +12,7 @@
|
|||
"user": {
|
||||
"data": {
|
||||
"api_key": "API Key",
|
||||
"api_token": "API Secret",
|
||||
"currencies": "Account Balance Currencies",
|
||||
"exchange_rates": "Exchange Rates"
|
||||
"api_token": "API Secret"
|
||||
},
|
||||
"description": "Please enter the details of your API key as provided by Coinbase.",
|
||||
"title": "Coinbase API Key Details"
|
||||
|
@ -31,6 +29,7 @@
|
|||
"init": {
|
||||
"data": {
|
||||
"account_balance_currencies": "Wallet balances to report.",
|
||||
"exchange_base": "Base currency for exchange rate sensors.",
|
||||
"exchange_rate_currencies": "Exchange rates to report."
|
||||
},
|
||||
"description": "Adjust Coinbase Options"
|
||||
|
|
Loading…
Add table
Reference in a new issue