From 8901508df6b4f6bc44c1709eb8ba084f111c7b82 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Sat, 11 Apr 2020 23:33:22 +0200 Subject: [PATCH] Handle incorrect config for Nederlandse Spoorwegen integration (#31623) * Improved error handling * Removed notification * Simplified error messages * Added custom excpetion support * Fixed custom exception catch * Removed broad exception catch * Raise PlatformNotReady --- homeassistant/components/nederlandse_spoorwegen/sensor.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/nederlandse_spoorwegen/sensor.py b/homeassistant/components/nederlandse_spoorwegen/sensor.py index 45413d4a15a..2db46b02db4 100644 --- a/homeassistant/components/nederlandse_spoorwegen/sensor.py +++ b/homeassistant/components/nederlandse_spoorwegen/sensor.py @@ -3,11 +3,13 @@ from datetime import datetime, timedelta import logging import ns_api +from ns_api import RequestParametersError import requests import voluptuous as vol from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY, CONF_NAME +from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.util import Throttle @@ -47,13 +49,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the departure sensor.""" nsapi = ns_api.NSAPI(config[CONF_API_KEY]) + try: stations = nsapi.get_stations() except ( requests.exceptions.ConnectionError, requests.exceptions.HTTPError, ) as error: - _LOGGER.error("Couldn't fetch stations, API key correct?: %s", error) + _LOGGER.error("Could not connect to the internet: %s", error) + raise PlatformNotReady() + except RequestParametersError as error: + _LOGGER.error("Could not fetch stations, please check configuration: %s", error) return sensors = []