Handle unhandled IQVIA data update exception (#32144)

* Handle unhandled IQVIA data update exception

* Cleanup

* Ask for forgiveness, not permission

* Use warning-level logs

* Fix log messages
This commit is contained in:
Aaron Bach 2020-02-24 13:02:33 -07:00 committed by GitHub
parent 9801810552
commit db40b2fc32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -134,21 +134,34 @@ class IndexSensor(IQVIAEntity):
async def async_update(self): async def async_update(self):
"""Update the sensor.""" """Update the sensor."""
if not self._iqvia.data: if not self._iqvia.data:
_LOGGER.warning(
"IQVIA didn't return data for %s; trying again later", self.name
)
return return
data = {} try:
if self._type in (TYPE_ALLERGY_TODAY, TYPE_ALLERGY_TOMORROW): if self._type in (TYPE_ALLERGY_TODAY, TYPE_ALLERGY_TOMORROW):
data = self._iqvia.data[TYPE_ALLERGY_INDEX].get("Location") data = self._iqvia.data[TYPE_ALLERGY_INDEX].get("Location")
elif self._type in (TYPE_ASTHMA_TODAY, TYPE_ASTHMA_TOMORROW): elif self._type in (TYPE_ASTHMA_TODAY, TYPE_ASTHMA_TOMORROW):
data = self._iqvia.data[TYPE_ASTHMA_INDEX].get("Location") data = self._iqvia.data[TYPE_ASTHMA_INDEX].get("Location")
elif self._type == TYPE_DISEASE_TODAY: elif self._type == TYPE_DISEASE_TODAY:
data = self._iqvia.data[TYPE_DISEASE_INDEX].get("Location") data = self._iqvia.data[TYPE_DISEASE_INDEX].get("Location")
except KeyError:
if not data: _LOGGER.warning(
"IQVIA didn't return data for %s; trying again later", self.name
)
return return
key = self._type.split("_")[-1].title() key = self._type.split("_")[-1].title()
try:
[period] = [p for p in data["periods"] if p["Type"] == key] [period] = [p for p in data["periods"] if p["Type"] == key]
except ValueError:
_LOGGER.warning(
"IQVIA didn't return data for %s; trying again later", self.name
)
return
[rating] = [ [rating] = [
i["label"] i["label"]
for i in RATING_MAPPING for i in RATING_MAPPING