Handle missing values in alpha_vantage sensor (#63632)

* Update sensor.py

catch error condition if returned values does not contain expected values

* Update sensor.py

fixed smaller code problems

* Update sensor.py

* Update sensor.py

fix case when values is None

* Update sensor.py

check if values is a dict

* Update sensor.py

removed comment

* Update sensor.py

simplified check for dict
This commit is contained in:
Martin 2022-01-20 14:49:42 +01:00 committed by GitHub
parent c741412808
commit b8ecb98fd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -131,7 +131,10 @@ class AlphaVantageSensor(SensorEntity):
_LOGGER.debug("Requesting new data for symbol %s", self._symbol)
all_values, _ = self._timeseries.get_intraday(self._symbol)
values = next(iter(all_values.values()))
self._attr_native_value = values["1. open"]
if isinstance(values, dict) and "1. open" in values:
self._attr_native_value = values["1. open"]
else:
self._attr_native_value = None
self._attr_extra_state_attributes = (
{
ATTR_ATTRIBUTION: ATTRIBUTION,
@ -139,7 +142,7 @@ class AlphaVantageSensor(SensorEntity):
ATTR_HIGH: values["2. high"],
ATTR_LOW: values["3. low"],
}
if values is not None
if isinstance(values, dict)
else None
)
_LOGGER.debug("Received new values for symbol %s", self._symbol)
@ -171,7 +174,10 @@ class AlphaVantageForeignExchange(SensorEntity):
values, _ = self._foreign_exchange.get_currency_exchange_rate(
from_currency=self._from_currency, to_currency=self._to_currency
)
self._attr_native_value = round(float(values["5. Exchange Rate"]), 4)
if isinstance(values, dict) and "5. Exchange Rate" in values:
self._attr_native_value = round(float(values["5. Exchange Rate"]), 4)
else:
self._attr_native_value = None
self._attr_extra_state_attributes = (
{
ATTR_ATTRIBUTION: ATTRIBUTION,