From 3b7f16f189e632b574f91c41f81543941119c992 Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Tue, 25 Jul 2017 00:05:47 -0400 Subject: [PATCH] Catch and log Lyft API errors (#8635) --- homeassistant/components/sensor/lyft.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/sensor/lyft.py b/homeassistant/components/sensor/lyft.py index c16fae9f5d5..11ca07f7fb8 100644 --- a/homeassistant/components/sensor/lyft.py +++ b/homeassistant/components/sensor/lyft.py @@ -45,20 +45,27 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Lyft sensor.""" from lyft_rides.auth import ClientCredentialGrant + from lyft_rides.errors import APIError auth_flow = ClientCredentialGrant(client_id=config.get(CONF_CLIENT_ID), client_secret=config.get( CONF_CLIENT_SECRET), scopes="public", is_sandbox_mode=False) - session = auth_flow.get_session() + try: + session = auth_flow.get_session() + + timeandpriceest = LyftEstimate( + session, config[CONF_START_LATITUDE], config[CONF_START_LONGITUDE], + config.get(CONF_END_LATITUDE), config.get(CONF_END_LONGITUDE)) + timeandpriceest.fetch_data() + except APIError as exc: + _LOGGER.error("Error setting up Lyft platform: %s", exc) + return False wanted_product_ids = config.get(CONF_PRODUCT_IDS) dev = [] - timeandpriceest = LyftEstimate( - session, config[CONF_START_LATITUDE], config[CONF_START_LONGITUDE], - config.get(CONF_END_LATITUDE), config.get(CONF_END_LONGITUDE)) for product_id, product in timeandpriceest.products.items(): if (wanted_product_ids is not None) and \ (product_id not in wanted_product_ids): @@ -188,14 +195,18 @@ class LyftEstimate(object): self.end_latitude = end_latitude self.end_longitude = end_longitude self.products = None - self.__real_update() @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest product info and estimates from the Lyft API.""" - self.__real_update() + from lyft_rides.errors import APIError + try: + self.fetch_data() + except APIError as exc: + _LOGGER.error("Error fetching Lyft data: %s", exc) - def __real_update(self): + def fetch_data(self): + """Get the latest product info and estimates from the Lyft API.""" from lyft_rides.client import LyftRidesClient client = LyftRidesClient(self._session)