1 """The Coinbase integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from coinbase.rest
import RESTClient
9 from coinbase.rest.rest_base
import HTTPError
10 from coinbase.wallet.client
import Client
as LegacyClient
11 from coinbase.wallet.error
import AuthenticationError
22 API_ACCOUNT_AVALIABLE,
25 API_ACCOUNT_CURRENCY_CODE,
43 _LOGGER = logging.getLogger(__name__)
45 PLATFORMS = [Platform.SENSOR]
50 """Set up Coinbase from a config entry."""
52 instance = await hass.async_add_executor_job(create_and_update_instance, entry)
54 entry.async_on_unload(entry.add_update_listener(update_listener))
56 hass.data.setdefault(DOMAIN, {})
58 hass.data[DOMAIN][entry.entry_id] = instance
60 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
66 """Unload a config entry."""
67 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
69 hass.data[DOMAIN].pop(entry.entry_id)
75 """Create and update a Coinbase Data instance."""
76 if "organizations" not in entry.data[CONF_API_KEY]:
77 client = LegacyClient(entry.data[CONF_API_KEY], entry.data[CONF_API_TOKEN])
81 api_key=entry.data[CONF_API_KEY], api_secret=entry.data[CONF_API_TOKEN]
84 base_rate = entry.options.get(CONF_EXCHANGE_BASE,
"USD")
90 async
def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) ->
None:
91 """Handle options update."""
93 await hass.config_entries.async_reload(config_entry.entry_id)
95 registry = er.async_get(hass)
96 entities = er.async_entries_for_config_entry(registry, config_entry.entry_id)
99 for entity
in entities:
100 currency = entity.unique_id.split(
"-")[-1]
102 "xe" in entity.unique_id
103 and currency
not in config_entry.options.get(CONF_EXCHANGE_RATES, [])
104 or "wallet" in entity.unique_id
105 and currency
not in config_entry.options.get(CONF_CURRENCIES, [])
107 registry.async_remove(entity.entity_id)
111 """Handle paginated accounts."""
112 response = client.get_accounts()
114 accounts = response[API_DATA]
115 next_starting_after = response.pagination.next_starting_after
117 while next_starting_after:
118 response = client.get_accounts(starting_after=next_starting_after)
119 accounts += response[API_DATA]
120 next_starting_after = response.pagination.next_starting_after
124 API_ACCOUNT_ID: account[API_ACCOUNT_ID],
125 API_ACCOUNT_NAME: account[API_ACCOUNT_NAME],
126 API_ACCOUNT_CURRENCY: account[API_ACCOUNT_CURRENCY][
127 API_ACCOUNT_CURRENCY_CODE
129 API_ACCOUNT_AMOUNT: account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT],
130 ACCOUNT_IS_VAULT: account[API_RESOURCE_TYPE] == API_TYPE_VAULT,
132 for account
in accounts
135 accounts = response[API_ACCOUNTS]
136 while response[
"has_next"]:
137 response = client.get_accounts(cursor=response[
"cursor"])
138 accounts += response[
"accounts"]
142 API_ACCOUNT_ID: account[API_V3_ACCOUNT_ID],
143 API_ACCOUNT_NAME: account[API_ACCOUNT_NAME],
144 API_ACCOUNT_CURRENCY: account[API_ACCOUNT_CURRENCY],
145 API_ACCOUNT_AMOUNT: account[API_ACCOUNT_AVALIABLE][API_ACCOUNT_VALUE]
146 + account[API_ACCOUNT_HOLD][API_ACCOUNT_VALUE],
147 ACCOUNT_IS_VAULT: account[API_RESOURCE_TYPE] == API_V3_TYPE_VAULT,
149 for account
in accounts
154 """Get the latest data and update the states."""
156 def __init__(self, client, exchange_base, version):
157 """Init the coinbase data object."""
167 "v3_" + client.get_portfolios()[
"portfolios"][0][API_V3_ACCOUNT_ID]
171 @Throttle(MIN_TIME_BETWEEN_UPDATES)
173 """Get the latest data from coinbase."""
183 "/v2/exchange-rates",
184 params={API_RATES_CURRENCY: self.
exchange_baseexchange_base},
186 except (AuthenticationError, HTTPError)
as coinbase_error:
188 "Authentication error connecting to coinbase: %s", coinbase_error
def __init__(self, client, exchange_base, version)
None update_listener(HomeAssistant hass, ConfigEntry config_entry)
def get_accounts(client, version)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
CoinbaseData create_and_update_instance(ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
web.Response get(self, web.Request request, str config_key)