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
pull/34069/head
Yarmo Mackenbach 2020-04-11 23:33:22 +02:00 committed by GitHub
parent 9f713dac7f
commit 8901508df6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 1 deletions

View File

@ -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 = []