2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Coinbase sensors."""
|
2021-06-28 14:38:12 +00:00
|
|
|
import logging
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2018-01-10 06:47:22 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
from .const import (
|
|
|
|
API_ACCOUNT_AMOUNT,
|
|
|
|
API_ACCOUNT_BALANCE,
|
|
|
|
API_ACCOUNT_CURRENCY,
|
|
|
|
API_ACCOUNT_ID,
|
|
|
|
API_ACCOUNT_NAME,
|
|
|
|
API_ACCOUNT_NATIVE_BALANCE,
|
2021-07-02 09:15:05 +00:00
|
|
|
API_RATES,
|
2021-08-06 16:34:21 +00:00
|
|
|
API_RESOURCE_TYPE,
|
|
|
|
API_TYPE_VAULT,
|
2021-06-28 14:38:12 +00:00
|
|
|
CONF_CURRENCIES,
|
|
|
|
CONF_EXCHANGE_RATES,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-01-28 17:04:40 +00:00
|
|
|
ATTR_NATIVE_BALANCE = "Balance in native currency"
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2018-11-04 08:50:00 +00:00
|
|
|
CURRENCY_ICONS = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"BTC": "mdi:currency-btc",
|
|
|
|
"ETH": "mdi:currency-eth",
|
|
|
|
"EUR": "mdi:currency-eur",
|
|
|
|
"LTC": "mdi:litecoin",
|
|
|
|
"USD": "mdi:currency-usd",
|
2018-11-04 08:50:00 +00:00
|
|
|
}
|
2019-02-14 21:09:22 +00:00
|
|
|
|
2020-07-14 20:17:14 +00:00
|
|
|
DEFAULT_COIN_ICON = "mdi:currency-usd-circle"
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by coinbase.com"
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2018-01-28 17:04:40 +00:00
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Coinbase sensor platform."""
|
|
|
|
instance = hass.data[DOMAIN][config_entry.entry_id]
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
entities = []
|
|
|
|
|
|
|
|
provided_currencies = [
|
2021-08-06 16:34:21 +00:00
|
|
|
account[API_ACCOUNT_CURRENCY]
|
|
|
|
for account in instance.accounts
|
|
|
|
if account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
2021-06-28 14:38:12 +00:00
|
|
|
]
|
|
|
|
|
2021-06-28 18:21:04 +00:00
|
|
|
desired_currencies = []
|
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
if CONF_CURRENCIES in config_entry.options:
|
|
|
|
desired_currencies = config_entry.options[CONF_CURRENCIES]
|
|
|
|
|
2021-07-15 04:50:23 +00:00
|
|
|
exchange_base_currency = instance.exchange_rates[API_ACCOUNT_CURRENCY]
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
for currency in desired_currencies:
|
|
|
|
if currency not in provided_currencies:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"The currency %s is no longer provided by your account, please check "
|
|
|
|
"your settings in Coinbase's developer tools",
|
|
|
|
currency,
|
|
|
|
)
|
2021-06-29 15:54:38 +00:00
|
|
|
continue
|
2021-06-28 14:38:12 +00:00
|
|
|
entities.append(AccountSensor(instance, currency))
|
|
|
|
|
|
|
|
if CONF_EXCHANGE_RATES in config_entry.options:
|
|
|
|
for rate in config_entry.options[CONF_EXCHANGE_RATES]:
|
|
|
|
entities.append(
|
|
|
|
ExchangeRateSensor(
|
|
|
|
instance,
|
|
|
|
rate,
|
2021-07-15 04:50:23 +00:00
|
|
|
exchange_base_currency,
|
2021-06-28 14:38:12 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
class AccountSensor(SensorEntity):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Representation of a Coinbase.com sensor."""
|
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
def __init__(self, coinbase_data, currency):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._coinbase_data = coinbase_data
|
2021-06-28 14:38:12 +00:00
|
|
|
self._currency = currency
|
|
|
|
for account in coinbase_data.accounts:
|
2021-08-06 16:34:21 +00:00
|
|
|
if (
|
|
|
|
account[API_ACCOUNT_CURRENCY] == currency
|
|
|
|
and account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
|
|
|
):
|
2021-06-28 14:38:12 +00:00
|
|
|
self._name = f"Coinbase {account[API_ACCOUNT_NAME]}"
|
2021-07-02 09:15:05 +00:00
|
|
|
self._id = (
|
|
|
|
f"coinbase-{account[API_ACCOUNT_ID]}-wallet-"
|
|
|
|
f"{account[API_ACCOUNT_CURRENCY]}"
|
|
|
|
)
|
2021-06-28 14:38:12 +00:00
|
|
|
self._state = account[API_ACCOUNT_BALANCE][API_ACCOUNT_AMOUNT]
|
|
|
|
self._unit_of_measurement = account[API_ACCOUNT_CURRENCY]
|
|
|
|
self._native_balance = account[API_ACCOUNT_NATIVE_BALANCE][
|
|
|
|
API_ACCOUNT_AMOUNT
|
|
|
|
]
|
|
|
|
self._native_currency = account[API_ACCOUNT_NATIVE_BALANCE][
|
|
|
|
API_ACCOUNT_CURRENCY
|
|
|
|
]
|
|
|
|
break
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the Unique ID of the sensor."""
|
|
|
|
return self._id
|
|
|
|
|
2018-01-10 06:47:22 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement this sensor expresses itself in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2018-11-04 08:50:00 +00:00
|
|
|
return CURRENCY_ICONS.get(self._unit_of_measurement, DEFAULT_COIN_ICON)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Return the state attributes of the sensor."""
|
|
|
|
return {
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2020-02-24 16:47:52 +00:00
|
|
|
ATTR_NATIVE_BALANCE: f"{self._native_balance} {self._native_currency}",
|
2018-01-10 06:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest state of the sensor."""
|
|
|
|
self._coinbase_data.update()
|
2021-05-22 17:31:49 +00:00
|
|
|
for account in self._coinbase_data.accounts:
|
2021-08-06 16:34:21 +00:00
|
|
|
if (
|
|
|
|
account[API_ACCOUNT_CURRENCY] == self._currency
|
|
|
|
and account[API_RESOURCE_TYPE] != API_TYPE_VAULT
|
|
|
|
):
|
2021-06-28 14:38:12 +00:00
|
|
|
self._state = 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
|
|
|
|
]
|
|
|
|
break
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
class ExchangeRateSensor(SensorEntity):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Representation of a Coinbase.com sensor."""
|
|
|
|
|
2021-07-15 04:50:23 +00:00
|
|
|
def __init__(self, coinbase_data, exchange_currency, exchange_base):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._coinbase_data = coinbase_data
|
|
|
|
self.currency = exchange_currency
|
2019-09-03 15:09:59 +00:00
|
|
|
self._name = f"{exchange_currency} Exchange Rate"
|
2021-07-02 09:15:05 +00:00
|
|
|
self._id = f"coinbase-{coinbase_data.user_id}-xe-{exchange_currency}"
|
2021-06-28 14:38:12 +00:00
|
|
|
self._state = round(
|
2021-07-02 09:15:05 +00:00
|
|
|
1 / float(self._coinbase_data.exchange_rates[API_RATES][self.currency]), 2
|
2021-06-28 14:38:12 +00:00
|
|
|
)
|
2021-07-15 04:50:23 +00:00
|
|
|
self._unit_of_measurement = exchange_base
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
2021-06-28 14:38:12 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique ID of the sensor."""
|
|
|
|
return self._id
|
|
|
|
|
2018-01-10 06:47:22 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement this sensor expresses itself in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2018-11-04 08:50:00 +00:00
|
|
|
return CURRENCY_ICONS.get(self.currency, DEFAULT_COIN_ICON)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Return the state attributes of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest state of the sensor."""
|
|
|
|
self._coinbase_data.update()
|
2021-06-28 14:38:12 +00:00
|
|
|
self._state = round(
|
|
|
|
1 / float(self._coinbase_data.exchange_rates.rates[self.currency]), 2
|
|
|
|
)
|