mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Use entity class attributes for alpha_vantage (#52520)
* Use entity class attributes for alpha_vantage * tweak * clean up
This commit is contained in:
parent
4463d50711
commit
e2b89e4650
@ -110,48 +110,27 @@ class AlphaVantageSensor(SensorEntity):
|
|||||||
def __init__(self, timeseries, symbol):
|
def __init__(self, timeseries, symbol):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._symbol = symbol[CONF_SYMBOL]
|
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._timeseries = timeseries
|
||||||
self.values = None
|
self._attr_unit_of_measurement = symbol.get(CONF_CURRENCY, self._symbol)
|
||||||
self._unit_of_measurement = symbol.get(CONF_CURRENCY, self._symbol)
|
self._attr_icon = ICONS.get(symbol.get(CONF_CURRENCY, "USD"))
|
||||||
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
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and updates the states."""
|
"""Get the latest data and updates the states."""
|
||||||
_LOGGER.debug("Requesting new data for symbol %s", self._symbol)
|
_LOGGER.debug("Requesting new data for symbol %s", self._symbol)
|
||||||
all_values, _ = self._timeseries.get_intraday(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)
|
_LOGGER.debug("Received new values for symbol %s", self._symbol)
|
||||||
|
|
||||||
|
|
||||||
@ -163,43 +142,13 @@ class AlphaVantageForeignExchange(SensorEntity):
|
|||||||
self._foreign_exchange = foreign_exchange
|
self._foreign_exchange = foreign_exchange
|
||||||
self._from_currency = config[CONF_FROM]
|
self._from_currency = config[CONF_FROM]
|
||||||
self._to_currency = config[CONF_TO]
|
self._to_currency = config[CONF_TO]
|
||||||
if CONF_NAME in config:
|
self._attr_name = (
|
||||||
self._name = config.get(CONF_NAME)
|
config.get(CONF_NAME)
|
||||||
else:
|
if CONF_NAME in config
|
||||||
self._name = f"{self._to_currency}/{self._from_currency}"
|
else f"{self._to_currency}/{self._from_currency}"
|
||||||
self._unit_of_measurement = self._to_currency
|
)
|
||||||
self._icon = ICONS.get(self._from_currency, "USD")
|
self._attr_icon = ICONS.get(self._from_currency, "USD")
|
||||||
self.values = None
|
self._attr_unit_of_measurement = self._to_currency
|
||||||
|
|
||||||
@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,
|
|
||||||
}
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and updates the states."""
|
"""Get the latest data and updates the states."""
|
||||||
@ -208,9 +157,20 @@ class AlphaVantageForeignExchange(SensorEntity):
|
|||||||
self._from_currency,
|
self._from_currency,
|
||||||
self._to_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
|
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(
|
_LOGGER.debug(
|
||||||
"Received new data for forex %s - %s",
|
"Received new data for forex %s - %s",
|
||||||
self._from_currency,
|
self._from_currency,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user