mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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
This commit is contained in:
parent
bbd2078986
commit
71a3516053
@ -102,6 +102,11 @@ class DarkSkyWeather(WeatherEntity):
|
|||||||
self._ds_hourly = None
|
self._ds_hourly = None
|
||||||
self._ds_daily = 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
|
@property
|
||||||
def attribution(self):
|
def attribution(self):
|
||||||
"""Return the attribution."""
|
"""Return the attribution."""
|
||||||
@ -215,7 +220,8 @@ class DarkSkyWeather(WeatherEntity):
|
|||||||
self._dark_sky.update()
|
self._dark_sky.update()
|
||||||
|
|
||||||
self._ds_data = self._dark_sky.data
|
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_hourly = self._dark_sky.hourly
|
||||||
self._ds_daily = self._dark_sky.daily
|
self._ds_daily = self._dark_sky.daily
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ from unittest.mock import patch
|
|||||||
import forecastio
|
import forecastio
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
|
||||||
|
from requests.exceptions import ConnectionError
|
||||||
|
|
||||||
from homeassistant.components import weather
|
from homeassistant.components import weather
|
||||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import setup_component
|
||||||
@ -48,3 +50,16 @@ class TestDarkSky(unittest.TestCase):
|
|||||||
|
|
||||||
state = self.hass.states.get("weather.test")
|
state = self.hass.states.get("weather.test")
|
||||||
assert state.state == "sunny"
|
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…
x
Reference in New Issue
Block a user