From 3c64648b7031fb42b89d5072786d0c1270695fe4 Mon Sep 17 00:00:00 2001 From: "Freek Laan (DutchWorkz BV)" Date: Tue, 19 Nov 2019 21:33:31 +0100 Subject: [PATCH] Added error handling --- custom_components/populartimes/sensor.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/custom_components/populartimes/sensor.py b/custom_components/populartimes/sensor.py index 4380419..139edd0 100644 --- a/custom_components/populartimes/sensor.py +++ b/custom_components/populartimes/sensor.py @@ -5,9 +5,12 @@ from homeassistant.helpers.entity import Entity from requests.exceptions import ConnectionError as ConnectError, HTTPError, Timeout import homeassistant.helpers.config_validation as cv +import logging import populartimes import voluptuous as vol +_LOGGER = logging.getLogger(__name__) + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_API_KEY): cv.string, @@ -69,12 +72,6 @@ def update(self): self._id ) - try: - current_popularity = result["current_popularity"] - self._state = current_popularity - except: - self._state = 0 - self._attributes['address'] = result["address"] self._attributes['maps_name'] = result["name"] self._attributes['popularity_monday'] = result["populartimes"][0]["data"] @@ -84,7 +81,9 @@ def update(self): self._attributes['popularity_friday'] = result["populartimes"][4]["data"] self._attributes['popularity_saturday'] = result["populartimes"][5]["data"] self._attributes['popularity_sunday'] = result["populartimes"][6]["data"] - except (ConnectError, HTTPError, Timeout, ValueError) as error: - _LOGGER.error( - "Unable to connect to Google Place API: %s", error) - self._state = None + + popularity = result.get('current_popularity') + self._state = popularity + + except: + _LOGGER.error("No popularity info is returned by the populartimes library.") \ No newline at end of file