Fix nws platform setup and data update. (#34106)

* add test and fix tests for update logging.

* Fix update logging.

* Remove assert is True

* guard against platform config and use async_write_ha_state

* fix discovery info passing to platform

* Improve testing for entities in init
This commit is contained in:
MatthewFlamm authored and GitHub committed 2020-04-12 17:19:22 -05:00
1 parent ad5a396c10
commit e6a6c3ceb6
4 files changed
+74 -38

No files matched your search

+12 -8
View File
@@ -78,7 +78,7 @@ async def async_setup(hass: HomeAssistant, config: dict):
for component in PLATFORMS:
hass.async_create_task(
discovery.async_load_platform(hass, component, DOMAIN, {}, config)
discovery.async_load_platform(hass, component, DOMAIN, entry, config)
)
return True
@@ -129,18 +129,21 @@ class NwsData:
return self.nws.forecast_hourly
@staticmethod
async def _async_update_item(update_call, update_type, station_name, success):
async def _async_update_item(
update_call, update_type, station_name, previous_success
):
"""Update item and handle logging."""
try:
_LOGGER.debug("Updating %s for station %s", update_type, station_name)
await update_call()
if success:
if not previous_success:
_LOGGER.warning(
"Success updating %s for station %s", update_type, station_name
)
success = True
success = True
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
if success:
if previous_success:
_LOGGER.warning(
"Error updating %s for station %s: %s",
update_type,
@@ -148,23 +151,24 @@ class NwsData:
err,
)
success = False
return success
async def async_update(self, now=None):
"""Update all data."""
await self._async_update_item(
self.update_observation_success = await self._async_update_item(
self.nws.update_observation,
"observation",
self.station,
self.update_observation_success,
)
await self._async_update_item(
self.update_forecast_success = await self._async_update_item(
self.nws.update_forecast,
"forecast",
self.station,
self.update_forecast_success,
)
await self._async_update_item(
self.update_forecast_hourly_success = await self._async_update_item(
self.nws.update_forecast_hourly,
"forecast_hourly",
self.station,