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 -> unavailable
pull/27214/head
Josh 2019-10-04 22:28:55 -04:00 committed by Paulus Schoutsen
parent bbd2078986
commit 71a3516053
2 changed files with 22 additions and 1 deletions

View File

@ -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

View File

@ -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"