From 9f146a3954453c0a1d87f74f75a9bfb7d68b17e5 Mon Sep 17 00:00:00 2001 From: cgtobi Date: Fri, 26 Oct 2018 20:18:14 +0200 Subject: [PATCH] Raise PlatformNotReady for RMVtransport if API not available (#17635) * Raise PlatformNotReady if API not available * Delete whitespaces * Revert unwanted breaking changes * Revert deleted line * Update homeassistant/components/sensor/rmvtransport.py * Use await asyncio.wait --- .../components/sensor/rmvtransport.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/sensor/rmvtransport.py b/homeassistant/components/sensor/rmvtransport.py index f9bd65c1a74..7835b74ac98 100644 --- a/homeassistant/components/sensor/rmvtransport.py +++ b/homeassistant/components/sensor/rmvtransport.py @@ -4,16 +4,18 @@ Support for real-time departure information for Rhein-Main public transport. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.rmvtransport/ """ +import asyncio import logging from datetime import timedelta import voluptuous as vol -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.helpers.entity import Entity from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION) +from homeassistant.exceptions import PlatformNotReady +from homeassistant.helpers.aiohttp_client import async_get_clientsession +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity import Entity from homeassistant.util import Throttle REQUIREMENTS = ['PyRMVtransport==0.1.3'] @@ -90,7 +92,14 @@ async def async_setup_platform(hass, config, async_add_entities, next_departure.get(CONF_MAX_JOURNEYS), next_departure.get(CONF_NAME), timeout)) - async_add_entities(sensors, True) + + tasks = [sensor.async_update() for sensor in sensors] + if tasks: + await asyncio.wait(tasks) + if not all(sensor.data.departures for sensor in sensors): + raise PlatformNotReady + + async_add_entities(sensors) class RMVDepartureSensor(Entity): @@ -185,14 +194,16 @@ class RMVDepartureData: @Throttle(SCAN_INTERVAL) async def async_update(self): """Update the connection data.""" + from RMVtransport.rmvtransport import RMVtransportApiConnectionError + try: _data = await self.rmv.get_departures(self._station_id, products=self._products, directionId=self._direction, maxJourneys=50) - except ValueError: + except RMVtransportApiConnectionError: self.departures = [] - _LOGGER.warning("Returned data not understood") + _LOGGER.warning("Could not retrive data from rmv.de") return self.station = _data.get('station') _deps = []