Fix Coinbase for new API Structure (#103930)
This commit is contained in:
parent
0eafc8f2cd
commit
2557e41ec0
6 changed files with 50 additions and 45 deletions
|
@ -17,6 +17,7 @@ import homeassistant.helpers.config_validation as cv
|
||||||
from . import get_accounts
|
from . import get_accounts
|
||||||
from .const import (
|
from .const import (
|
||||||
API_ACCOUNT_CURRENCY,
|
API_ACCOUNT_CURRENCY,
|
||||||
|
API_ACCOUNT_CURRENCY_CODE,
|
||||||
API_RATES,
|
API_RATES,
|
||||||
API_RESOURCE_TYPE,
|
API_RESOURCE_TYPE,
|
||||||
API_TYPE_VAULT,
|
API_TYPE_VAULT,
|
||||||
|
@ -81,7 +82,7 @@ async def validate_options(
|
||||||
accounts = await hass.async_add_executor_job(get_accounts, client)
|
accounts = await hass.async_add_executor_job(get_accounts, client)
|
||||||
|
|
||||||
accounts_currencies = [
|
accounts_currencies = [
|
||||||
account[API_ACCOUNT_CURRENCY]
|
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE]
|
||||||
for account in accounts
|
for account in accounts
|
||||||
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
||||||
]
|
]
|
||||||
|
|
|
@ -12,14 +12,16 @@ DOMAIN = "coinbase"
|
||||||
API_ACCOUNT_AMOUNT = "amount"
|
API_ACCOUNT_AMOUNT = "amount"
|
||||||
API_ACCOUNT_BALANCE = "balance"
|
API_ACCOUNT_BALANCE = "balance"
|
||||||
API_ACCOUNT_CURRENCY = "currency"
|
API_ACCOUNT_CURRENCY = "currency"
|
||||||
|
API_ACCOUNT_CURRENCY_CODE = "code"
|
||||||
API_ACCOUNT_ID = "id"
|
API_ACCOUNT_ID = "id"
|
||||||
API_ACCOUNT_NATIVE_BALANCE = "native_balance"
|
API_ACCOUNT_NATIVE_BALANCE = "balance"
|
||||||
API_ACCOUNT_NAME = "name"
|
API_ACCOUNT_NAME = "name"
|
||||||
API_ACCOUNTS_DATA = "data"
|
API_ACCOUNTS_DATA = "data"
|
||||||
API_RATES = "rates"
|
API_RATES = "rates"
|
||||||
API_RESOURCE_PATH = "resource_path"
|
API_RESOURCE_PATH = "resource_path"
|
||||||
API_RESOURCE_TYPE = "type"
|
API_RESOURCE_TYPE = "type"
|
||||||
API_TYPE_VAULT = "vault"
|
API_TYPE_VAULT = "vault"
|
||||||
|
API_USD = "USD"
|
||||||
|
|
||||||
WALLETS = {
|
WALLETS = {
|
||||||
"1INCH": "1INCH",
|
"1INCH": "1INCH",
|
||||||
|
|
|
@ -14,9 +14,9 @@ from .const import (
|
||||||
API_ACCOUNT_AMOUNT,
|
API_ACCOUNT_AMOUNT,
|
||||||
API_ACCOUNT_BALANCE,
|
API_ACCOUNT_BALANCE,
|
||||||
API_ACCOUNT_CURRENCY,
|
API_ACCOUNT_CURRENCY,
|
||||||
|
API_ACCOUNT_CURRENCY_CODE,
|
||||||
API_ACCOUNT_ID,
|
API_ACCOUNT_ID,
|
||||||
API_ACCOUNT_NAME,
|
API_ACCOUNT_NAME,
|
||||||
API_ACCOUNT_NATIVE_BALANCE,
|
|
||||||
API_RATES,
|
API_RATES,
|
||||||
API_RESOURCE_TYPE,
|
API_RESOURCE_TYPE,
|
||||||
API_TYPE_VAULT,
|
API_TYPE_VAULT,
|
||||||
|
@ -55,7 +55,7 @@ async def async_setup_entry(
|
||||||
entities: list[SensorEntity] = []
|
entities: list[SensorEntity] = []
|
||||||
|
|
||||||
provided_currencies: list[str] = [
|
provided_currencies: list[str] = [
|
||||||
account[API_ACCOUNT_CURRENCY]
|
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE]
|
||||||
for account in instance.accounts
|
for account in instance.accounts
|
||||||
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
||||||
]
|
]
|
||||||
|
@ -106,26 +106,28 @@ class AccountSensor(SensorEntity):
|
||||||
self._currency = currency
|
self._currency = currency
|
||||||
for account in coinbase_data.accounts:
|
for account in coinbase_data.accounts:
|
||||||
if (
|
if (
|
||||||
account[API_ACCOUNT_CURRENCY] != currency
|
account[API_ACCOUNT_CURRENCY][API_ACCOUNT_CURRENCY_CODE] != currency
|
||||||
or account[API_RESOURCE_TYPE] == API_TYPE_VAULT
|
or account[API_RESOURCE_TYPE] == API_TYPE_VAULT
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
self._attr_name = f"Coinbase {account[API_ACCOUNT_NAME]}"
|
self._attr_name = f"Coinbase {account[API_ACCOUNT_NAME]}"
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f"coinbase-{account[API_ACCOUNT_ID]}-wallet-"
|
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_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(
|
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
|
break
|
||||||
|
|
||||||
self._attr_state_class = SensorStateClass.TOTAL
|
self._attr_state_class = SensorStateClass.TOTAL
|
||||||
|
@ -141,7 +143,7 @@ class AccountSensor(SensorEntity):
|
||||||
def extra_state_attributes(self) -> dict[str, str]:
|
def extra_state_attributes(self) -> dict[str, str]:
|
||||||
"""Return the state attributes of the sensor."""
|
"""Return the state attributes of the sensor."""
|
||||||
return {
|
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:
|
def update(self) -> None:
|
||||||
|
@ -149,17 +151,17 @@ class AccountSensor(SensorEntity):
|
||||||
self._coinbase_data.update()
|
self._coinbase_data.update()
|
||||||
for account in self._coinbase_data.accounts:
|
for account in self._coinbase_data.accounts:
|
||||||
if (
|
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
|
or account[API_RESOURCE_TYPE] == API_TYPE_VAULT
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
self._attr_native_value = account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT]
|
self._attr_native_value = account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT]
|
||||||
self._native_balance = account[API_ACCOUNT_NATIVE_BALANCE][
|
self._native_balance = round(
|
||||||
API_ACCOUNT_AMOUNT
|
float(account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT])
|
||||||
]
|
/ float(self._coinbase_data.exchange_rates[API_RATES][self._currency]),
|
||||||
self._native_currency = account[API_ACCOUNT_NATIVE_BALANCE][
|
2,
|
||||||
API_ACCOUNT_CURRENCY
|
)
|
||||||
]
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,12 @@ from homeassistant.components.coinbase.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN
|
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
|
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 a heavily reduced mock list of exchange rates for testing."""
|
||||||
return {
|
return {
|
||||||
"currency": "USD",
|
"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 = [
|
MOCK_ACCOUNTS_RESPONSE = [
|
||||||
{
|
{
|
||||||
"balance": {"amount": "0.00001", "currency": GOOD_CURRENCY},
|
"balance": {"amount": "0.00001", "currency": GOOD_CURRENCY},
|
||||||
"currency": GOOD_CURRENCY,
|
"currency": {"code": GOOD_CURRENCY},
|
||||||
"id": "123456789",
|
"id": "123456789",
|
||||||
"name": "BTC Wallet",
|
"name": "BTC Wallet",
|
||||||
"native_balance": {"amount": "100.12", "currency": GOOD_CURRENCY_2},
|
|
||||||
"type": "wallet",
|
"type": "wallet",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"balance": {"amount": "100.00", "currency": GOOD_CURRENCY},
|
"balance": {"amount": "100.00", "currency": GOOD_CURRENCY},
|
||||||
"currency": GOOD_CURRENCY,
|
"currency": {"code": GOOD_CURRENCY},
|
||||||
"id": "abcdefg",
|
"id": "abcdefg",
|
||||||
"name": "BTC Vault",
|
"name": "BTC Vault",
|
||||||
"native_balance": {"amount": "100.12", "currency": GOOD_CURRENCY_2},
|
|
||||||
"type": "vault",
|
"type": "vault",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"balance": {"amount": "9.90", "currency": GOOD_CURRENCY_2},
|
"balance": {"amount": "9.90", "currency": GOOD_CURRENCY_2},
|
||||||
"currency": "USD",
|
"currency": {"code": GOOD_CURRENCY_2},
|
||||||
"id": "987654321",
|
"id": "987654321",
|
||||||
"name": "USD Wallet",
|
"name": "USD Wallet",
|
||||||
"native_balance": {"amount": "9.90", "currency": GOOD_CURRENCY_2},
|
|
||||||
"type": "fiat",
|
"type": "fiat",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
|
@ -7,13 +7,11 @@
|
||||||
'amount': '**REDACTED**',
|
'amount': '**REDACTED**',
|
||||||
'currency': 'BTC',
|
'currency': 'BTC',
|
||||||
}),
|
}),
|
||||||
'currency': 'BTC',
|
'currency': dict({
|
||||||
|
'code': 'BTC',
|
||||||
|
}),
|
||||||
'id': '**REDACTED**',
|
'id': '**REDACTED**',
|
||||||
'name': 'BTC Wallet',
|
'name': 'BTC Wallet',
|
||||||
'native_balance': dict({
|
|
||||||
'amount': '**REDACTED**',
|
|
||||||
'currency': 'USD',
|
|
||||||
}),
|
|
||||||
'type': 'wallet',
|
'type': 'wallet',
|
||||||
}),
|
}),
|
||||||
dict({
|
dict({
|
||||||
|
@ -21,13 +19,11 @@
|
||||||
'amount': '**REDACTED**',
|
'amount': '**REDACTED**',
|
||||||
'currency': 'BTC',
|
'currency': 'BTC',
|
||||||
}),
|
}),
|
||||||
'currency': 'BTC',
|
'currency': dict({
|
||||||
|
'code': 'BTC',
|
||||||
|
}),
|
||||||
'id': '**REDACTED**',
|
'id': '**REDACTED**',
|
||||||
'name': 'BTC Vault',
|
'name': 'BTC Vault',
|
||||||
'native_balance': dict({
|
|
||||||
'amount': '**REDACTED**',
|
|
||||||
'currency': 'USD',
|
|
||||||
}),
|
|
||||||
'type': 'vault',
|
'type': 'vault',
|
||||||
}),
|
}),
|
||||||
dict({
|
dict({
|
||||||
|
@ -35,13 +31,11 @@
|
||||||
'amount': '**REDACTED**',
|
'amount': '**REDACTED**',
|
||||||
'currency': 'USD',
|
'currency': 'USD',
|
||||||
}),
|
}),
|
||||||
'currency': 'USD',
|
'currency': dict({
|
||||||
|
'code': 'USD',
|
||||||
|
}),
|
||||||
'id': '**REDACTED**',
|
'id': '**REDACTED**',
|
||||||
'name': 'USD Wallet',
|
'name': 'USD Wallet',
|
||||||
'native_balance': dict({
|
|
||||||
'amount': '**REDACTED**',
|
|
||||||
'currency': 'USD',
|
|
||||||
}),
|
|
||||||
'type': 'fiat',
|
'type': 'fiat',
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue