Skip to content

Commit

Permalink
Merge pull request #179 from Snuffy2/Add-User-Agent-details-to-OSM-Qu…
Browse files Browse the repository at this point in the history
…eries

Add User-Agent Header to OSM Queries
  • Loading branch information
Snuffy2 authored May 20, 2023
2 parents a118501 + 0b9b935 commit 5f31304
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
3 changes: 1 addition & 2 deletions custom_components/places/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)

DOMAIN = "places"
VERSION = "v2.4"
EVENT_TYPE = DOMAIN + "_state_update"
ENTITY_ID_FORMAT = Platform.SENSOR + ".{}"

Expand Down Expand Up @@ -152,7 +153,6 @@
ATTR_STREET_NUMBER,
ATTR_STREET,
ATTR_STREET_REF,
# ATTR_UPDATES_SKIPPED,
ATTR_WIKIDATA_DICT,
ATTR_WIKIDATA_ID,
]
Expand Down Expand Up @@ -212,7 +212,6 @@
ATTR_LOCATION_CURRENT,
ATTR_LOCATION_PREVIOUS,
ATTR_PREVIOUS_STATE,
# ATTR_UPDATES_SKIPPED,
]
JSON_ATTRIBUTE_LIST = [
ATTR_CITY,
Expand Down
26 changes: 23 additions & 3 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from homeassistant.util.location import distance
from urllib3.exceptions import NewConnectionError

from .const import ( # ATTR_UPDATES_SKIPPED,
from .const import (
ATTR_CITY,
ATTR_CITY_CLEAN,
ATTR_COUNTRY,
Expand Down Expand Up @@ -139,6 +139,7 @@
RESET_ATTRIBUTE_LIST,
TRACKING_DOMAINS,
TRACKING_DOMAINS_NEED_LATLONG,
VERSION,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -886,8 +887,27 @@ def get_dict_from_url(self, url, name):
get_dict = {}
_LOGGER.info(f"({self.get_attr(CONF_NAME)}) Requesting data for {name}")
_LOGGER.debug(f"({self.get_attr(CONF_NAME)}) {name} URL: {url}")
headers = {"user-agent": f"Mozilla/5.0 (Home Assistant) {DOMAIN}/{VERSION}"}
try:
get_response = requests.get(url)
get_response = requests.get(url, headers=headers)
except requests.exceptions.RetryError as e:
get_response = None
_LOGGER.warning(
f"({self.get_attr(CONF_NAME)}) Retry Error connecting to {name} [Error: {e}]: {url}"
)
return {}
except requests.exceptions.ConnectionError as e:
get_response = None
_LOGGER.warning(
f"({self.get_attr(CONF_NAME)}) Connection Error connecting to {name} [Error: {e}]: {url}"
)
return {}
except requests.exceptions.HTTPError as e:
get_response = None
_LOGGER.warning(
f"({self.get_attr(CONF_NAME)}) HTTP Error connecting to {name} [Error: {e}]: {url}"
)
return {}
except requests.exceptions.Timeout as e:
get_response = None
_LOGGER.warning(
Expand All @@ -905,7 +925,7 @@ def get_dict_from_url(self, url, name):
except NewConnectionError as e:
get_response = None
_LOGGER.warning(
f"({self.get_attr(CONF_NAME)}) Connection Error connecting to {name} "
f"({self.get_attr(CONF_NAME)}) New Connection Error connecting to {name} "
+ f"[Error: {e}]: {url}"
)
return {}
Expand Down

0 comments on commit 5f31304

Please sign in to comment.