Skip to content

Commit

Permalink
Further fix RT issues with header, improve error handling on rt pull …
Browse files Browse the repository at this point in the history
…request
  • Loading branch information
vingerha committed Nov 13, 2024
1 parent a41b91c commit f9d4685
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions custom_components/gtfs2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ async def async_step_real_time(
vol.Optional(CONF_API_KEY, default=self.config_entry.options.get(CONF_API_KEY)) : str,
vol.Optional(CONF_API_KEY_NAME, default=self.config_entry.options.get(CONF_API_KEY_NAME,DEFAULT_API_KEY_NAME)) : str,
vol.Required(CONF_API_KEY_LOCATION, default=self.config_entry.options.get(CONF_API_KEY_LOCATION,DEFAULT_API_KEY_LOCATION)) : selector.SelectSelector(selector.SelectSelectorConfig(options=ATTR_API_KEY_LOCATIONS, translation_key="api_key_location")),
vol.Optional(CONF_ACCEPT_HEADER_PB, default = False): selector.BooleanSelector(),
vol.Optional(CONF_ACCEPT_HEADER_PB, default = self.config_entry.options.get(CONF_ACCEPT_HEADER_PB,False)): selector.BooleanSelector(),
},
),
errors=errors,
Expand All @@ -541,7 +541,7 @@ async def async_step_real_time(
vol.Optional(CONF_API_KEY, default=self.config_entry.options.get(CONF_API_KEY)) : str,
vol.Optional(CONF_API_KEY_NAME, default=self.config_entry.options.get(CONF_API_KEY_NAME,DEFAULT_API_KEY_NAME)) : str,
vol.Required(CONF_API_KEY_LOCATION, default=self.config_entry.options.get(CONF_API_KEY_LOCATION,DEFAULT_API_KEY_LOCATION)) : selector.SelectSelector(selector.SelectSelectorConfig(options=ATTR_API_KEY_LOCATIONS, translation_key="api_key_location")),
vol.Optional(CONF_ACCEPT_HEADER_PB, default = False): selector.BooleanSelector(),
vol.Optional(CONF_ACCEPT_HEADER_PB, default = self.config_entry.options.get(CONF_ACCEPT_HEADER_PB,False)): selector.BooleanSelector(),
},
),
errors=errors,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/gtfs2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ async def _async_update_data(self) -> dict[str, str]:
self._headers = {options[CONF_API_KEY_NAME]: options[CONF_API_KEY]}
self._headers[CONF_API_KEY_LOCATION] = options.get(CONF_API_KEY_LOCATION,None)
self._headers[CONF_API_KEY_NAME] = options.get(CONF_API_KEY_NAME, None)
if options.get(CONF_ACCEPT_HEADER_PB, False):
self._headers["Accept"] = "application/x-protobuf"
self._headers[CONF_API_KEY] = options.get(CONF_API_KEY, None)
self._headers[CONF_ACCEPT_HEADER_PB] = options.get(CONF_ACCEPT_HEADER_PB, False)
_LOGGER.debug("RT header: %s", self._headers)
self._pygtfs = get_gtfs(
self.hass, DEFAULT_PATH, data, False
Expand Down
2 changes: 1 addition & 1 deletion custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ def get_local_stops_next_departures(self):
# check if local file created
if check != "ok":
_LOGGER.error("Could not download RT data from: %s", self._trip_update_url)
return False
return {}
else:
# use local file created as new url
self._trip_update_url = "file://" + DEFAULT_PATH_RT + "/" + self._data["name"] + "_localstop.rt"
Expand Down
5 changes: 5 additions & 0 deletions custom_components/gtfs2/gtfs_rt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,14 @@ def get_gtfs_rt(hass, path, data):
try:
r = requests.get(url, headers = _headers , allow_redirects=True)
open(os.path.join(gtfs_dir, file), "wb").write(r.content)
if r.status_code != 200:
_LOGGER.error("Ìssues with downloading GTFS RT data, error: %s, content: %s", r.status_code, r.content)
return "no_rt_data_file"
except Exception as ex: # pylint: disable=broad-except
_LOGGER.error("Ìssues with downloading GTFS RT data to: %s", os.path.join(gtfs_dir, file))
return "no_rt_data_file"


if data.get("debug_output", False):
try:
data_out = ""
Expand Down

0 comments on commit f9d4685

Please sign in to comment.