Skip to content

Commit

Permalink
Add timeout to retrieve from Google
Browse files Browse the repository at this point in the history
Adding timeout of 5 seconds to retrieve the information from Google. Without a timeout it is possible for the integration to "hang" indefinitely.
Further putting it in between try-except to catch timeout (and any other potential errors).
  • Loading branch information
ehendrix23 authored and gregoryduckworth committed Jun 23, 2021
1 parent 44b38b5 commit f294b6c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions custom_components/google_geocode/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ def update(self):
else:
url = "https://maps.googleapis.com/maps/api/geocode/json?language=" + self._google_language + "&region=" + self._google_region + "&latlng=" + lat + "&key=" + self._api_key
_LOGGER.debug("Google request sent: " + url)
response = get(url)
try:
response = get(url, timeout=5)
response.raise_for_status()
except requests.exceptions.RequestException as err:
_LOGGER.error("Failed to retrieve geocode from Google. Error: %s", err)
return
json_input = response.text
decoded = json.loads(json_input)
street_number = ''
Expand Down Expand Up @@ -331,4 +336,4 @@ def _get_gravatar_for_email(self, email: str):
def _get_image_from_url(self, url: str):
"""Return an image from a given url. Async friendly."""
import hashlib
return url.format(hashlib.md5(url.encode('utf-8').lower()).hexdigest())
return url.format(hashlib.md5(url.encode('utf-8').lower()).hexdigest())

0 comments on commit f294b6c

Please sign in to comment.