handle request exceptions

This commit is contained in:
ktdad
2019-05-30 20:50:42 -04:00
parent 7f71143b9c
commit 63055f949c

View File

@@ -58,6 +58,8 @@ CONDITION_CLASSES = OrderedDict([
('partlycloudy', ['few', 'sct']) ('partlycloudy', ['few', 'sct'])
]) ])
ERRORS = (aiohttp.ClientError, JSONDecodeError, asyncio.CancelledError)
FORECAST_CLASSES = { FORECAST_CLASSES = {
ATTR_FORECAST_DETAIL_DESCRIPTION: 'detailedForecast', ATTR_FORECAST_DETAIL_DESCRIPTION: 'detailedForecast',
ATTR_FORECAST_TEMP: 'temperature', ATTR_FORECAST_TEMP: 'temperature',
@@ -150,8 +152,7 @@ async def async_setup_platform(hass, config, async_add_entities,
try: try:
with async_timeout.timeout(10, loop=hass.loop): with async_timeout.timeout(10, loop=hass.loop):
stations = await nws.stations() stations = await nws.stations()
except (aiohttp.ClientError, JSONDecodeError, except ERRORS as status:
asyncio.CancelledError) as status:
_LOGGER.error("Error getting station list for %s: %s", _LOGGER.error("Error getting station list for %s: %s",
nws.latlon, status) nws.latlon, status)
raise PlatformNotReady raise PlatformNotReady
@@ -187,24 +188,33 @@ class NWSWeather(WeatherEntity):
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self): async def async_update(self):
"""Update Condition.""" """Update Condition."""
with async_timeout.timeout(10, loop=self.hass.loop): _LOGGER.debug("Updating station observations %s", self._nws.station)
_LOGGER.debug("Updating station observations %s", try:
self._nws.station) with async_timeout.timeout(10, loop=self.hass.loop):
obs = await self._nws.observations(limit=1)
obs = await self._nws.observations(limit=1) except ERRORS as status:
_LOGGER.error("Error updating observation from station %s: %s",
self._nws.station, status)
else:
self._observation = obs[0] self._observation = obs[0]
if 'rawMessage' in self._observation.keys(): if 'rawMessage' in self._observation.keys():
self._metar_obs = self._metar(self._observation['rawMessage']) self._metar_obs = self._metar(
self._observation['rawMessage'])
else: else:
self._metar_obs = None self._metar_obs = None
_LOGGER.debug("Observations: %s", self._observation)
_LOGGER.debug("Updating forecast") _LOGGER.debug("Updating forecast")
try:
if self._mode == 'daynight': if self._mode == 'daynight':
self._forecast = await self._nws.forecast() self._forecast = await self._nws.forecast()
elif self._mode == 'hourly': elif self._mode == 'hourly':
self._forecast = await self._nws.forecast_hourly() self._forecast = await self._nws.forecast_hourly()
_LOGGER.debug("Observations: %s", self._observation) except ERRORS as status:
_LOGGER.debug("Forecasts: %s", self._forecast) _LOGGER.error("Error updating forecast from station %s: %s",
self._nws.station, status)
else:
_LOGGER.debug("Forecasts: %s", self._forecast)
return
@property @property
def attribution(self): def attribution(self):