2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Coinbase sensors."""
|
2018-01-10 06:47:22 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2018-01-28 17:04:40 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2018-01-10 06:47:22 +00:00
|
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_COINBASE = "coinbase_cache"
|
2018-01-28 17:04:40 +00:00
|
|
|
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-01-10 06:47:22 +00:00
|
|
|
"""Set up the Coinbase sensors."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2019-07-31 19:25:30 +00:00
|
|
|
if "account" in discovery_info:
|
|
|
|
account = discovery_info["account"]
|
2018-01-28 17:04:40 +00:00
|
|
|
sensor = AccountSensor(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DATA_COINBASE], account["name"], account["balance"]["currency"]
|
|
|
|
)
|
|
|
|
if "exchange_currency" in discovery_info:
|
2018-01-28 17:04:40 +00:00
|
|
|
sensor = ExchangeRateSensor(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DATA_COINBASE],
|
|
|
|
discovery_info["exchange_currency"],
|
|
|
|
discovery_info["native_currency"],
|
|
|
|
)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([sensor], True)
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountSensor(Entity):
|
|
|
|
"""Representation of a Coinbase.com sensor."""
|
|
|
|
|
|
|
|
def __init__(self, coinbase_data, name, currency):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._coinbase_data = coinbase_data
|
2019-09-03 15:09:59 +00:00
|
|
|
self._name = f"Coinbase {name}"
|
2018-01-10 06:47:22 +00:00
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = currency
|
|
|
|
self._native_balance = None
|
|
|
|
self._native_currency = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@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
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""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()
|
2019-07-31 19:25:30 +00:00
|
|
|
for account in self._coinbase_data.accounts["data"]:
|
2020-04-05 15:48:55 +00:00
|
|
|
if self._name == f"Coinbase {account['name']}":
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = account["balance"]["amount"]
|
|
|
|
self._native_balance = account["native_balance"]["amount"]
|
|
|
|
self._native_currency = account["native_balance"]["currency"]
|
2018-01-10 06:47:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRateSensor(Entity):
|
|
|
|
"""Representation of a Coinbase.com sensor."""
|
|
|
|
|
|
|
|
def __init__(self, coinbase_data, exchange_currency, native_currency):
|
|
|
|
"""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"
|
2018-01-10 06:47:22 +00:00
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = native_currency
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@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
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""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()
|
|
|
|
rate = self._coinbase_data.exchange_rates.rates[self.currency]
|
|
|
|
self._state = round(1 / float(rate), 2)
|