Fix Coinbase for new API Structure (#103930)
parent
56298b2c88
commit
a5a8d38d08
|
@ -17,6 +17,7 @@ import homeassistant.helpers.config_validation as cv
|
|||
from . import get_accounts
|
||||
from .const import (
|
||||
API_ACCOUNT_CURRENCY,
|
||||
API_ACCOUNT_CURRENCY_CODE,
|
||||
API_RATES,
|
||||
API_RESOURCE_TYPE,
|
||||
API_TYPE_VAULT,
|
||||
|
@ -81,7 +82,7 @@ async def validate_options(
|
|||
accounts = await hass.async_add_executor_job(get_accounts, client)
|
||||
|
||||
accounts_currencies = [
|
||||
account[API_ACCOUNT_CURRENCY]
|
||||
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE]
|
||||
for account in accounts
|
||||
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
||||
]
|
||||
|
|
|
@ -12,14 +12,16 @@ DOMAIN = "coinbase"
|
|||
API_ACCOUNT_AMOUNT = "amount"
|
||||
API_ACCOUNT_BALANCE = "balance"
|
||||
API_ACCOUNT_CURRENCY = "currency"
|
||||
API_ACCOUNT_CURRENCY_CODE = "code"
|
||||
API_ACCOUNT_ID = "id"
|
||||
API_ACCOUNT_NATIVE_BALANCE = "native_balance"
|
||||
API_ACCOUNT_NATIVE_BALANCE = "balance"
|
||||
API_ACCOUNT_NAME = "name"
|
||||
API_ACCOUNTS_DATA = "data"
|
||||
API_RATES = "rates"
|
||||
API_RESOURCE_PATH = "resource_path"
|
||||
API_RESOURCE_TYPE = "type"
|
||||
API_TYPE_VAULT = "vault"
|
||||
API_USD = "USD"
|
||||
|
||||
WALLETS = {
|
||||
"1INCH": "1INCH",
|
||||
|
|
|
@ -14,9 +14,9 @@ from .const import (
|
|||
API_ACCOUNT_AMOUNT,
|
||||
API_ACCOUNT_BALANCE,
|
||||
API_ACCOUNT_CURRENCY,
|
||||
API_ACCOUNT_CURRENCY_CODE,
|
||||
API_ACCOUNT_ID,
|
||||
API_ACCOUNT_NAME,
|
||||
API_ACCOUNT_NATIVE_BALANCE,
|
||||
API_RATES,
|
||||
API_RESOURCE_TYPE,
|
||||
API_TYPE_VAULT,
|
||||
|
@ -55,7 +55,7 @@ async def async_setup_entry(
|
|||
entities: list[SensorEntity] = []
|
||||
|
||||
provided_currencies: list[str] = [
|
||||
account[API_ACCOUNT_CURRENCY]
|
||||
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE]
|
||||
for account in instance.accounts
|
||||
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
||||
]
|
||||
|
@ -106,26 +106,28 @@ class AccountSensor(SensorEntity):
|
|||
self._currency = currency
|
||||
for account in coinbase_data.accounts:
|
||||
if (
|
||||
account[API_ACCOUNT_CURRENCY] != currency
|
||||
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE] != currency
|
||||
or account[API_RESOURCE_TYPE] == API_TYPE_VAULT
|
||||
):
|
||||
continue
|
||||
self._attr_name = f"Coinbase {account[API_ACCOUNT_NAME]}"
|
||||
self._attr_unique_id = (
|
||||
f"coinbase-{account[API_ACCOUNT_ID]}-wallet-"
|
||||
f"{account[API_ACCOUNT_CURRENCY]}"
|
||||
f"{account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE]}"
|
||||
)
|
||||
self._attr_native_value = account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT]
|
||||
self._attr_native_unit_of_measurement = account[API_ACCOUNT_CURRENCY]
|
||||
self._attr_native_unit_of_measurement = account[API_ACCOUNT_CURRENCY][
|
||||
API_ACCOUNT_CURRENCY_CODE
|
||||
]
|
||||
self._attr_icon = CURRENCY_ICONS.get(
|
||||
account[API_ACCOUNT_CURRENCY], DEFAULT_COIN_ICON
|
||||
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE],
|
||||
DEFAULT_COIN_ICON,
|
||||
)
|
||||
self._native_balance = round(
|
||||
float(account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT])
|
||||
/ float(coinbase_data.exchange_rates[API_RATES][currency]),
|
||||
2,
|
||||
)
|
||||
self._native_balance = account[API_ACCOUNT_NATIVE_BALANCE][
|
||||
API_ACCOUNT_AMOUNT
|
||||
]
|
||||
self._native_currency = account[API_ACCOUNT_NATIVE_BALANCE][
|
||||
API_ACCOUNT_CURRENCY
|
||||
]
|
||||
break
|
||||
|
||||
self._attr_state_class = SensorStateClass.TOTAL
|
||||
|
@ -141,7 +143,7 @@ class AccountSensor(SensorEntity):
|
|||
def extra_state_attributes(self) -> dict[str, str]:
|
||||
"""Return the state attributes of the sensor."""
|
||||
return {
|
||||
ATTR_NATIVE_BALANCE: f"{self._native_balance} {self._native_currency}",
|
||||
ATTR_NATIVE_BALANCE: f"{self._native_balance} {self._coinbase_data.exchange_base}",
|
||||
}
|
||||
|
||||
def update(self) -> None:
|
||||
|
@ -149,17 +151,17 @@ class AccountSensor(SensorEntity):
|
|||
self._coinbase_data.update()
|
||||
for account in self._coinbase_data.accounts:
|
||||
if (
|
||||
account[API_ACCOUNT_CURRENCY] != self._currency
|
||||
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE]
|
||||
!= self._currency
|
||||
or account[API_RESOURCE_TYPE] == API_TYPE_VAULT
|
||||
):
|
||||
continue
|
||||
self._attr_native_value = account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT]
|
||||
self._native_balance = account[API_ACCOUNT_NATIVE_BALANCE][
|
||||
API_ACCOUNT_AMOUNT
|
||||
]
|
||||
self._native_currency = account[API_ACCOUNT_NATIVE_BALANCE][
|
||||
API_ACCOUNT_CURRENCY
|
||||
]
|
||||
self._native_balance = round(
|
||||
float(account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT])
|
||||
/ float(self._coinbase_data.exchange_rates[API_RATES][self._currency]),
|
||||
2,
|
||||
)
|
||||
break
|
||||
|
||||
|
||||
|
|
|
@ -6,7 +6,12 @@ from homeassistant.components.coinbase.const import (
|
|||
)
|
||||
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN
|
||||
|
||||
from .const import GOOD_EXCHANGE_RATE, GOOD_EXCHANGE_RATE_2, MOCK_ACCOUNTS_RESPONSE
|
||||
from .const import (
|
||||
GOOD_CURRENCY_2,
|
||||
GOOD_EXCHANGE_RATE,
|
||||
GOOD_EXCHANGE_RATE_2,
|
||||
MOCK_ACCOUNTS_RESPONSE,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -60,7 +65,11 @@ def mock_get_exchange_rates():
|
|||
"""Return a heavily reduced mock list of exchange rates for testing."""
|
||||
return {
|
||||
"currency": "USD",
|
||||
"rates": {GOOD_EXCHANGE_RATE_2: "0.109", GOOD_EXCHANGE_RATE: "0.00002"},
|
||||
"rates": {
|
||||
GOOD_CURRENCY_2: "1.0",
|
||||
GOOD_EXCHANGE_RATE_2: "0.109",
|
||||
GOOD_EXCHANGE_RATE: "0.00002",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -12,26 +12,23 @@ BAD_EXCHANGE_RATE = "ETH"
|
|||
MOCK_ACCOUNTS_RESPONSE = [
|
||||
{
|
||||
"balance": {"amount": "0.00001", "currency": GOOD_CURRENCY},
|
||||
"currency": GOOD_CURRENCY,
|
||||
"currency": {"code": GOOD_CURRENCY},
|
||||
"id": "123456789",
|
||||
"name": "BTC Wallet",
|
||||
"native_balance": {"amount": "100.12", "currency": GOOD_CURRENCY_2},
|
||||
"type": "wallet",
|
||||
},
|
||||
{
|
||||
"balance": {"amount": "100.00", "currency": GOOD_CURRENCY},
|
||||
"currency": GOOD_CURRENCY,
|
||||
"currency": {"code": GOOD_CURRENCY},
|
||||
"id": "abcdefg",
|
||||
"name": "BTC Vault",
|
||||
"native_balance": {"amount": "100.12", "currency": GOOD_CURRENCY_2},
|
||||
"type": "vault",
|
||||
},
|
||||
{
|
||||
"balance": {"amount": "9.90", "currency": GOOD_CURRENCY_2},
|
||||
"currency": "USD",
|
||||
"currency": {"code": GOOD_CURRENCY_2},
|
||||
"id": "987654321",
|
||||
"name": "USD Wallet",
|
||||
"native_balance": {"amount": "9.90", "currency": GOOD_CURRENCY_2},
|
||||
"type": "fiat",
|
||||
},
|
||||
]
|
||||
|
|
|
@ -7,13 +7,11 @@
|
|||
'amount': '**REDACTED**',
|
||||
'currency': 'BTC',
|
||||
}),
|
||||
'currency': 'BTC',
|
||||
'currency': dict({
|
||||
'code': 'BTC',
|
||||
}),
|
||||
'id': '**REDACTED**',
|
||||
'name': 'BTC Wallet',
|
||||
'native_balance': dict({
|
||||
'amount': '**REDACTED**',
|
||||
'currency': 'USD',
|
||||
}),
|
||||
'type': 'wallet',
|
||||
}),
|
||||
dict({
|
||||
|
@ -21,13 +19,11 @@
|
|||
'amount': '**REDACTED**',
|
||||
'currency': 'BTC',
|
||||
}),
|
||||
'currency': 'BTC',
|
||||
'currency': dict({
|
||||
'code': 'BTC',
|
||||
}),
|
||||
'id': '**REDACTED**',
|
||||
'name': 'BTC Vault',
|
||||
'native_balance': dict({
|
||||
'amount': '**REDACTED**',
|
||||
'currency': 'USD',
|
||||
}),
|
||||
'type': 'vault',
|
||||
}),
|
||||
dict({
|
||||
|
@ -35,13 +31,11 @@
|
|||
'amount': '**REDACTED**',
|
||||
'currency': 'USD',
|
||||
}),
|
||||
'currency': 'USD',
|
||||
'currency': dict({
|
||||
'code': 'USD',
|
||||
}),
|
||||
'id': '**REDACTED**',
|
||||
'name': 'USD Wallet',
|
||||
'native_balance': dict({
|
||||
'amount': '**REDACTED**',
|
||||
'currency': 'USD',
|
||||
}),
|
||||
'type': 'fiat',
|
||||
}),
|
||||
]),
|
||||
|
|
Loading…
Reference in New Issue