Guard against network errors for Dark Sky (#27141)
* Guard against network errors for Dark Sky - Prevents network errors from throwing an exception during state updates for the Dark Sky weather component. * Implement `available` for Dark Sky component * unknown -> unavailablepull/27214/head
parent
bbd2078986
commit
71a3516053
|
@ -102,6 +102,11 @@ class DarkSkyWeather(WeatherEntity):
|
|||
self._ds_hourly = None
|
||||
self._ds_daily = None
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if weather data is available from Dark Sky."""
|
||||
return self._ds_data is not None
|
||||
|
||||
@property
|
||||
def attribution(self):
|
||||
"""Return the attribution."""
|
||||
|
@ -215,7 +220,8 @@ class DarkSkyWeather(WeatherEntity):
|
|||
self._dark_sky.update()
|
||||
|
||||
self._ds_data = self._dark_sky.data
|
||||
self._ds_currently = self._dark_sky.currently.d
|
||||
currently = self._dark_sky.currently
|
||||
self._ds_currently = currently.d if currently else {}
|
||||
self._ds_hourly = self._dark_sky.hourly
|
||||
self._ds_daily = self._dark_sky.daily
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ from unittest.mock import patch
|
|||
import forecastio
|
||||
import requests_mock
|
||||
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from homeassistant.components import weather
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||
from homeassistant.setup import setup_component
|
||||
|
@ -48,3 +50,16 @@ class TestDarkSky(unittest.TestCase):
|
|||
|
||||
state = self.hass.states.get("weather.test")
|
||||
assert state.state == "sunny"
|
||||
|
||||
@patch("forecastio.load_forecast", side_effect=ConnectionError())
|
||||
def test_failed_setup(self, mock_load_forecast):
|
||||
"""Test to ensure that a network error does not break component state."""
|
||||
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
weather.DOMAIN,
|
||||
{"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}},
|
||||
)
|
||||
|
||||
state = self.hass.states.get("weather.test")
|
||||
assert state.state == "unavailable"
|
||||
|
|
Loading…
Reference in New Issue