1 """Support for Coinbase sensors."""
3 from __future__
import annotations
13 from .
import CoinbaseData
22 CONF_EXCHANGE_PRECISION,
23 CONF_EXCHANGE_PRECISION_DEFAULT,
28 _LOGGER = logging.getLogger(__name__)
30 ATTR_NATIVE_BALANCE =
"Balance in native currency"
31 ATTR_API_VERSION =
"API Version"
34 "BTC":
"mdi:currency-btc",
35 "ETH":
"mdi:currency-eth",
36 "EUR":
"mdi:currency-eur",
37 "LTC":
"mdi:litecoin",
38 "USD":
"mdi:currency-usd",
41 DEFAULT_COIN_ICON =
"mdi:cash"
43 ATTRIBUTION =
"Data provided by coinbase.com"
48 config_entry: ConfigEntry,
49 async_add_entities: AddEntitiesCallback,
51 """Set up Coinbase sensor platform."""
52 instance: CoinbaseData = hass.data[DOMAIN][config_entry.entry_id]
54 entities: list[SensorEntity] = []
56 provided_currencies: list[str] = [
57 account[API_ACCOUNT_CURRENCY]
58 for account
in instance.accounts
59 if not account[ACCOUNT_IS_VAULT]
62 desired_currencies: list[str] = []
64 if CONF_CURRENCIES
in config_entry.options:
65 desired_currencies = config_entry.options[CONF_CURRENCIES]
67 exchange_base_currency: str = instance.exchange_rates[API_ACCOUNT_CURRENCY]
69 exchange_precision: int = config_entry.options.get(
70 CONF_EXCHANGE_PRECISION, CONF_EXCHANGE_PRECISION_DEFAULT
73 for currency
in desired_currencies:
75 "Attempting to set up %s account sensor with %s API",
79 if currency
not in provided_currencies:
82 "The currency %s is no longer provided by your account, please"
83 " check your settings in Coinbase's developer tools"
90 if CONF_EXCHANGE_RATES
in config_entry.options:
91 for rate
in config_entry.options[CONF_EXCHANGE_RATES]:
93 "Attempting to set up %s account sensor with %s API",
99 instance, rate, exchange_base_currency, exchange_precision
107 """Representation of a Coinbase.com sensor."""
109 _attr_attribution = ATTRIBUTION
111 def __init__(self, coinbase_data: CoinbaseData, currency: str) ->
None:
112 """Initialize the sensor."""
115 for account
in coinbase_data.accounts:
116 if account[API_ACCOUNT_CURRENCY] != currency
or account[ACCOUNT_IS_VAULT]:
118 self.
_attr_name_attr_name = f
"Coinbase {account[API_ACCOUNT_NAME]}"
120 f
"coinbase-{account[API_ACCOUNT_ID]}-wallet-"
121 f
"{account[API_ACCOUNT_CURRENCY]}"
126 account[API_ACCOUNT_CURRENCY],
130 float(account[API_ACCOUNT_AMOUNT])
131 /
float(coinbase_data.exchange_rates[API_RATES][currency]),
138 configuration_url=
"https://www.coinbase.com/settings/api",
139 entry_type=DeviceEntryType.SERVICE,
140 identifiers={(DOMAIN, self.
_coinbase_data_coinbase_data.user_id)},
141 manufacturer=
"Coinbase.com",
142 name=f
"Coinbase {self._coinbase_data.user_id[-4:]}",
147 """Return the state attributes of the sensor."""
149 ATTR_NATIVE_BALANCE: f
"{self._native_balance} {self._coinbase_data.exchange_base}",
154 """Get the latest state of the sensor."""
156 "Updating %s account sensor with %s API",
163 account[API_ACCOUNT_CURRENCY] != self.
_currency_currency
164 or account[ACCOUNT_IS_VAULT]
169 float(account[API_ACCOUNT_AMOUNT])
177 """Representation of a Coinbase.com sensor."""
179 _attr_attribution = ATTRIBUTION
183 coinbase_data: CoinbaseData,
184 exchange_currency: str,
188 """Initialize the sensor."""
191 self.
_attr_name_attr_name = f
"{exchange_currency} Exchange Rate"
193 f
"coinbase-{coinbase_data.user_id}-xe-{exchange_currency}"
196 self.
_attr_icon_attr_icon = CURRENCY_ICONS.get(exchange_currency, DEFAULT_COIN_ICON)
198 1 /
float(coinbase_data.exchange_rates[API_RATES][exchange_currency]),
204 configuration_url=
"https://www.coinbase.com/settings/api",
205 entry_type=DeviceEntryType.SERVICE,
206 identifiers={(DOMAIN, self.
_coinbase_data_coinbase_data.user_id)},
207 manufacturer=
"Coinbase.com",
208 name=f
"Coinbase {self._coinbase_data.user_id[-4:]}",
212 """Get the latest state of the sensor."""
214 "Updating %s rate sensor with %s API",
_attr_native_unit_of_measurement
None __init__(self, CoinbaseData coinbase_data, str currency)
dict[str, str] extra_state_attributes(self)
_attr_native_unit_of_measurement
None __init__(self, CoinbaseData coinbase_data, str exchange_currency, str exchange_base, int precision)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)