Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to retrieve from Google #14

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())