Use entity class attributes for alpha_vantage (#52520)

* Use entity class attributes for alpha_vantage

* tweak

* clean up
pull/52829/head
Robert Hillis 2021-07-05 03:37:09 -04:00 committed by GitHub
parent 4463d50711
commit e2b89e4650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 74 deletions

View File

@ -110,48 +110,27 @@ class AlphaVantageSensor(SensorEntity):
def __init__(self, timeseries, symbol):
"""Initialize the sensor."""
self._symbol = symbol[CONF_SYMBOL]
self._name = symbol.get(CONF_NAME, self._symbol)
self._attr_name = symbol.get(CONF_NAME, self._symbol)
self._timeseries = timeseries
self.values = None
self._unit_of_measurement = symbol.get(CONF_CURRENCY, self._symbol)
self._icon = ICONS.get(symbol.get(CONF_CURRENCY, "USD"))
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def state(self):
"""Return the state of the sensor."""
return self.values["1. open"]
@property
def extra_state_attributes(self):
"""Return the state attributes."""
if self.values is not None:
return {
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_CLOSE: self.values["4. close"],
ATTR_HIGH: self.values["2. high"],
ATTR_LOW: self.values["3. low"],
}
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return self._icon
self._attr_unit_of_measurement = symbol.get(CONF_CURRENCY, self._symbol)
self._attr_icon = ICONS.get(symbol.get(CONF_CURRENCY, "USD"))
def update(self):
"""Get the latest data and updates the states."""
_LOGGER.debug("Requesting new data for symbol %s", self._symbol)
all_values, _ = self._timeseries.get_intraday(self._symbol)
self.values = next(iter(all_values.values()))
values = next(iter(all_values.values()))
self._attr_state = values["1. open"]
self._attr_extra_state_attributes = (
{
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_CLOSE: values["4. close"],
ATTR_HIGH: values["2. high"],
ATTR_LOW: values["3. low"],
}
if values is not None
else None
)
_LOGGER.debug("Received new values for symbol %s", self._symbol)
@ -163,43 +142,13 @@ class AlphaVantageForeignExchange(SensorEntity):
self._foreign_exchange = foreign_exchange
self._from_currency = config[CONF_FROM]
self._to_currency = config[CONF_TO]
if CONF_NAME in config:
self._name = config.get(CONF_NAME)
else:
self._name = f"{self._to_currency}/{self._from_currency}"
self._unit_of_measurement = self._to_currency
self._icon = ICONS.get(self._from_currency, "USD")
self.values = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def state(self):
"""Return the state of the sensor."""
return round(float(self.values["5. Exchange Rate"]), 4)
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return self._icon
@property
def extra_state_attributes(self):
"""Return the state attributes."""
if self.values is not None:
return {
ATTR_ATTRIBUTION: ATTRIBUTION,
CONF_FROM: self._from_currency,
CONF_TO: self._to_currency,
}
self._attr_name = (
config.get(CONF_NAME)
if CONF_NAME in config
else f"{self._to_currency}/{self._from_currency}"
)
self._attr_icon = ICONS.get(self._from_currency, "USD")
self._attr_unit_of_measurement = self._to_currency
def update(self):
"""Get the latest data and updates the states."""
@ -208,9 +157,20 @@ class AlphaVantageForeignExchange(SensorEntity):
self._from_currency,
self._to_currency,
)
self.values, _ = self._foreign_exchange.get_currency_exchange_rate(
values, _ = self._foreign_exchange.get_currency_exchange_rate(
from_currency=self._from_currency, to_currency=self._to_currency
)
self._attr_state = round(float(values["5. Exchange Rate"]), 4)
self._attr_extra_state_attributes = (
{
ATTR_ATTRIBUTION: ATTRIBUTION,
CONF_FROM: self._from_currency,
CONF_TO: self._to_currency,
}
if values is not None
else None
)
_LOGGER.debug(
"Received new data for forex %s - %s",
self._from_currency,