mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Setup DarkSky platform when offline during init (#4919)
* Setup DarkSky platform when offline during init * Fail setup_platform if fetch was unsuccessful
This commit is contained in:
parent
103fffa0f4
commit
b9dcc2777b
@ -104,19 +104,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
else:
|
||||
units = 'us'
|
||||
|
||||
# Create a data fetcher to support all of the configured sensors. Then make
|
||||
# the first call to init the data and confirm we can connect.
|
||||
try:
|
||||
forecast_data = DarkSkyData(
|
||||
api_key=config.get(CONF_API_KEY, None),
|
||||
latitude=hass.config.latitude,
|
||||
longitude=hass.config.longitude,
|
||||
units=units,
|
||||
interval=config.get(CONF_UPDATE_INTERVAL))
|
||||
forecast_data.update()
|
||||
forecast_data.update_currently()
|
||||
except ValueError as error:
|
||||
_LOGGER.error(error)
|
||||
forecast_data = DarkSkyData(
|
||||
api_key=config.get(CONF_API_KEY, None),
|
||||
latitude=hass.config.latitude,
|
||||
longitude=hass.config.longitude,
|
||||
units=units,
|
||||
interval=config.get(CONF_UPDATE_INTERVAL))
|
||||
forecast_data.update()
|
||||
forecast_data.update_currently()
|
||||
|
||||
# If connection failed don't setup platform.
|
||||
if forecast_data.data is None:
|
||||
return False
|
||||
|
||||
name = config.get(CONF_NAME)
|
||||
@ -227,7 +225,10 @@ class DarkSkySensor(Entity):
|
||||
If the sensor type is unknown, the current state is returned.
|
||||
"""
|
||||
lookup_type = convert_to_camel(self.type)
|
||||
state = getattr(data, lookup_type, 0)
|
||||
state = getattr(data, lookup_type, None)
|
||||
|
||||
if state is None:
|
||||
return state
|
||||
|
||||
# Some state data needs to be rounded to whole values or converted to
|
||||
# percentages
|
||||
@ -284,21 +285,22 @@ class DarkSkyData(object):
|
||||
self.data = forecastio.load_forecast(
|
||||
self._api_key, self.latitude, self.longitude, units=self.units)
|
||||
except (ConnectError, HTTPError, Timeout, ValueError) as error:
|
||||
raise ValueError("Unable to init Dark Sky. %s", error)
|
||||
self.unit_system = self.data.json['flags']['units']
|
||||
_LOGGER.error("Unable to connect to Dark Sky. %s", error)
|
||||
self.data = None
|
||||
self.unit_system = self.data and self.data.json['flags']['units']
|
||||
|
||||
def _update_currently(self):
|
||||
"""Update currently data."""
|
||||
self.data_currently = self.data.currently()
|
||||
self.data_currently = self.data and self.data.currently()
|
||||
|
||||
def _update_minutely(self):
|
||||
"""Update minutely data."""
|
||||
self.data_minutely = self.data.minutely()
|
||||
self.data_minutely = self.data and self.data.minutely()
|
||||
|
||||
def _update_hourly(self):
|
||||
"""Update hourly data."""
|
||||
self.data_hourly = self.data.hourly()
|
||||
self.data_hourly = self.data and self.data.hourly()
|
||||
|
||||
def _update_daily(self):
|
||||
"""Update daily data."""
|
||||
self.data_daily = self.data.daily()
|
||||
self.data_daily = self.data and self.data.daily()
|
||||
|
@ -17,6 +17,15 @@ from tests.common import load_fixture, get_test_home_assistant
|
||||
class TestDarkSkySetup(unittest.TestCase):
|
||||
"""Test the Dark Sky platform."""
|
||||
|
||||
def add_entities(self, new_entities, update_before_add=False):
|
||||
"""Mock add entities."""
|
||||
if update_before_add:
|
||||
for entity in new_entities:
|
||||
entity.update()
|
||||
|
||||
for entity in new_entities:
|
||||
self.entities.append(entity)
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@ -30,6 +39,7 @@ class TestDarkSkySetup(unittest.TestCase):
|
||||
self.lon = -122.423
|
||||
self.hass.config.latitude = self.lat
|
||||
self.hass.config.longitude = self.lon
|
||||
self.entities = []
|
||||
|
||||
def test_setup_with_config(self):
|
||||
"""Test the platform setup with configuration."""
|
||||
@ -63,6 +73,7 @@ class TestDarkSkySetup(unittest.TestCase):
|
||||
r'(-?\d+\.?\d*),(-?\d+\.?\d*)')
|
||||
mock_req.get(re.compile(uri),
|
||||
text=load_fixture('darksky.json'))
|
||||
darksky.setup_platform(self.hass, self.config, MagicMock())
|
||||
darksky.setup_platform(self.hass, self.config, self.add_entities)
|
||||
self.assertTrue(mock_get_forecast.called)
|
||||
self.assertEqual(mock_get_forecast.call_count, 1)
|
||||
self.assertEqual(len(self.entities), 2)
|
||||
|
Loading…
x
Reference in New Issue
Block a user