From 99e58f3c18939a12ec3777341913a141b4fa2c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9n?= Date: Sat, 22 May 2021 19:31:49 +0200 Subject: [PATCH] Fix coinbase response pagination (#50890) * Fix issue #50500 * next is a python keyword --- homeassistant/components/coinbase/__init__.py | 18 ++++++++++++++++-- homeassistant/components/coinbase/sensor.py | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/coinbase/__init__.py b/homeassistant/components/coinbase/__init__.py index 9fd99e993b6..5bcd330c9bb 100644 --- a/homeassistant/components/coinbase/__init__.py +++ b/homeassistant/components/coinbase/__init__.py @@ -57,7 +57,7 @@ def setup(hass, config): if not hasattr(coinbase_data, "accounts"): return False - for account in coinbase_data.accounts.data: + for account in coinbase_data.accounts: if account_currencies is None or account.currency in account_currencies: load_platform(hass, "sensor", DOMAIN, {"account": account}, config) for currency in exchange_currencies: @@ -90,7 +90,21 @@ class CoinbaseData: """Get the latest data from coinbase.""" try: - self.accounts = self.client.get_accounts() + response = self.client.get_accounts() + accounts = response["data"] + + # Most of Coinbase's API seems paginated now (25 items per page, but first page has 24). + # This API gives a 'next_starting_after' property to send back as a 'starting_after' param. + # Their API documentation is not up to date when writing these lines (2021-05-20) + next_starting_after = response.pagination.next_starting_after + + while next_starting_after: + response = self.client.get_accounts(starting_after=next_starting_after) + accounts = accounts + response["data"] + next_starting_after = response.pagination.next_starting_after + + self.accounts = accounts + self.exchange_rates = self.client.get_exchange_rates() except AuthenticationError as coinbase_error: _LOGGER.error( diff --git a/homeassistant/components/coinbase/sensor.py b/homeassistant/components/coinbase/sensor.py index e4e4e719c9e..3a0e689862f 100644 --- a/homeassistant/components/coinbase/sensor.py +++ b/homeassistant/components/coinbase/sensor.py @@ -81,7 +81,7 @@ class AccountSensor(SensorEntity): def update(self): """Get the latest state of the sensor.""" self._coinbase_data.update() - for account in self._coinbase_data.accounts["data"]: + for account in self._coinbase_data.accounts: if self._name == f"Coinbase {account['name']}": self._state = account["balance"]["amount"] self._native_balance = account["native_balance"]["amount"]