Skip to content

Commit

Permalink
Facilitate stop selecting when trip not found
Browse files Browse the repository at this point in the history
Reduce logging
  • Loading branch information
vingerha committed Sep 13, 2024
1 parent 1f4498c commit 65c89d4
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 10 deletions.
39 changes: 37 additions & 2 deletions custom_components/gtfs2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,48 @@ async def async_step_stops(self, user_input: dict | None = None) -> FlowResult:
_LOGGER.debug(f"UserInputs Stops: {self._user_inputs}")
check_config = await self._check_config(self._user_inputs)
if check_config:
errors["base"] = check_config
return self.async_abort(reason=check_config)
return await self.async_step_stops_retry()
else:
return self.async_create_entry(
title=user_input[CONF_NAME], data=self._user_inputs
)

async def async_step_stops_retry(self, user_input: dict | None = None) -> FlowResult:
"""Handle the route step."""
errors: dict[str, str] = {}
if user_input is None:
try:
stops = get_stop_list(
self._pygtfs,
self._user_inputs[CONF_ROUTE].split(": ")[0],
self._user_inputs[CONF_DIRECTION],
)
last_stop = stops[-1:][0]
return self.async_show_form(
step_id="stops_retry",
data_schema=vol.Schema(
{
vol.Required(CONF_ORIGIN, default = self._user_inputs[CONF_ORIGIN]): vol.In(stops),
vol.Required(CONF_DESTINATION, default = self._user_inputs[CONF_DESTINATION]): vol.In(stops),
vol.Required(CONF_NAME): str,
vol.Optional(CONF_INCLUDE_TOMORROW, default = False): selector.BooleanSelector(),
},
),
errors=errors,
)
except:
_LOGGER.debug(f"Likely no stops for this route: {[CONF_ROUTE]}")
return self.async_abort(reason="no_stops")
self._user_inputs.update(user_input)
_LOGGER.debug(f"UserInputs Stops: {self._user_inputs}")
check_config = await self._check_config(self._user_inputs)
if check_config:
return await self.async_step_stops_retry()
else:
return self.async_create_entry(
title=user_input[CONF_NAME], data=self._user_inputs
)

async def async_step_stops_train(self, user_input: dict | None = None) -> FlowResult:
"""Handle the stops when train, as often impossible to select ID"""
errors: dict[str, str] = {}
Expand Down
5 changes: 2 additions & 3 deletions custom_components/gtfs2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def _async_update_data(self) -> dict[str, str]:
}

if check_extracting(self.hass, self._data['gtfs_dir'],self._data['file']):
_LOGGER.warning("Cannot update this sensor as still unpacking: %s", self._data["file"])
_LOGGER.debug("Cannot update this sensor as still unpacking: %s", self._data["file"])
previous_data["extracting"] = True
return previous_data

Expand Down Expand Up @@ -221,8 +221,7 @@ async def _async_update_data(self) -> dict[str, str]:
self._data["gtfs_updated_at"] = dt_util.utcnow().isoformat()

if check_extracting(self.hass, self._data['gtfs_dir'],self._data['file']):
_LOGGER.warning("Cannot update this sensor as still unpacking: %s", self._data["file"])
previous_data={}
_LOGGER.debug("Cannot update this sensor as still unpacking: %s", self._data["file"])
previous_data["extracting"] = True
return previous_data
try:
Expand Down
4 changes: 2 additions & 2 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
def get_next_departure(self):
_LOGGER.debug("Get next departure with data: %s", self._data)
if check_extracting(self.hass, self._data['gtfs_dir'],self._data['file']):
_LOGGER.warning("Cannot get next depurtures on this datasource as still unpacking: %s", self._data["file"])
_LOGGER.debug("Cannot get next depurtures on this datasource as still unpacking: %s", self._data["file"])
return {}

"""Get next departures from data."""
Expand Down Expand Up @@ -396,7 +396,7 @@ def get_gtfs(hass, path, data, update=False):
check_source_dates = data.get("check_source_dates", False)
journal = os.path.join(gtfs_dir, filename + ".sqlite-journal")
if check_extracting(hass, gtfs_dir,filename) and not update :
_LOGGER.warning("Cannot use this datasource as still unpacking: %s", filename)
_LOGGER.debug("Cannot use this datasource as still unpacking: %s", filename)
return "extracting"
if update and data["extract_from"] == "url" and os.path.exists(os.path.join(gtfs_dir, file)):
remove_datasource(hass, path, filename, True)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/gtfs2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def icon(self) -> str:
def _update_attrs(self): # noqa: C901 PLR0911
_LOGGER.debug("SENSOR update attr data: %s", self.coordinator.data)
if self.coordinator.data["extracting"]:
_LOGGER.warning("Extracting datasource")
_LOGGER.warning("Extracting datasource: %s ,for sensor: %s", self.coordinator.data["file"], self._name)
self._attr_native_value = None
return
self._pygtfs = self.coordinator.data["schedule"]
Expand Down Expand Up @@ -510,7 +510,7 @@ def _update_attrs(self): # noqa: C901 PLR0911
self._state: str | None = None
# if no data or extracting, stop
if self.coordinator.data["extracting"]:
_LOGGER.warning("Extracting datasource")
_LOGGER.warning("Extracting datasource: %s ,for sensor: %s", self.coordinator.data["file"], self._name)
self._attr_native_value = None
return

Expand Down
15 changes: 14 additions & 1 deletion custom_components/gtfs2/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"data": {
"file": "New datasource name",
"url": "external url to gtfs data (zip) file",
"api_key": "API key, if required",
"api_key_name": "API key name",
"api_key_location": "the location where the key is applied",
"extract_from": "Extract data from:"
},
"description": "NOTE: with a new url/zip, this may take quite a bit of time, \n depending on file-size and system performance [(docu)](https://github.com/vingerha/gtfs2/wiki/1.-Initial-setup:-the-static-data-source)"
Expand Down Expand Up @@ -60,6 +63,15 @@
},
"description": "Select from the options below [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_retry": {
"data": {
"origin": "Origin Stop",
"destination": "Destination Stop",
"name": "Name of the route",
"include_tomorrow": "Include tomorrow"
},
"description": "No trip found between selected stops, try again [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_train": {
"data": {
"origin": "Enter the city of departure",
Expand Down Expand Up @@ -108,6 +120,7 @@
"real_time": "Setup Realtime integration? \n (needs data from the same source)",
"local_stop_refresh_interval": "Data refresh interval (in minutes)",
"timerange": "Checking for departures in future from 'now' (in minutes: between 15 and 120)",
"check_source_dates": "experimental: avoid using new zip/source contain having no current information",
"radius": "Radius to search for stops (in meters between 50 and 500)"
}
},
Expand All @@ -118,7 +131,7 @@
"vehicle_position_url": "URL to vehicle position",
"alerts_url": "URL to alerts",
"api_key": "API key, if required",
"api_key_name": "API key name, default api_key",
"api_key_name": "API key name",
"api_key_location": "the location where the key is applied",
"accept": "Add Accept:application/x-protobuf to the header"
}
Expand Down
9 changes: 9 additions & 0 deletions custom_components/gtfs2/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
},
"description": "Wählen Sie aus den Optionen unten [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_retry": {
"data": {
"origin": "Origin Stop",
"destination": "Zielhaltestelle",
"name": "Name der Route",
"include_tomorrow": "Inklusive morgen"
},
"description": "Kein Trip gefunden, bitte Haltestelle(n) ändern [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_train": {
"data": {
"origin": "Wählen sie die Stadt ihrer Abfahrt",
Expand Down
9 changes: 9 additions & 0 deletions custom_components/gtfs2/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
},
"description": "Select from the options below [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_retry": {
"data": {
"origin": "Origin Stop",
"destination": "Destination Stop",
"name": "Name of the route",
"include_tomorrow": "Include tomorrow"
},
"description": "No trip found between selected stops, try again [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_train": {
"data": {
"origin": "Enter the city of departure",
Expand Down
9 changes: 9 additions & 0 deletions custom_components/gtfs2/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
},
"description": "Seleccione una de las siguientes opciones [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_retry": {
"data": {
"origin": "Parada de origen",
"destination": "Parada de destino",
"name": "Nombre de la ruta",
"include_tomorrow": "Incluir mañana"
},
"description": "Viaje no encontrado, Inténtalo de nuevo con otra parada(s). [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_train": {
"data": {
"origin": "Introduzca la ciudad de salida",
Expand Down
9 changes: 9 additions & 0 deletions custom_components/gtfs2/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
},
"description": "Sélectionnez parmi les options [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_retry": {
"data": {
"origin": "Arrêt d'origine",
"destination": "Arrêt de destination",
"name": "Nom de la ligne",
"include_tomorrow": "Inclure le lendemain?"
},
"description": "Échoué, svp re-essayer avec d'autre arrêt(s) [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_train": {
"data": {
"origin": "Ville de départ",
Expand Down
9 changes: 9 additions & 0 deletions custom_components/gtfs2/translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
},
"description": "Selecione uma das opções abaixo [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_retry": {
"data": {
"origin": "Paragem de origem",
"destination": "Paragem de destino",
"name": "Nome da rota",
"include_tomorrow": "Incluir amanhã"
},
"description": "Viagem não encontrada, tente novamente com outra paragem(s) [(docu)](https://github.com/vingerha/gtfs2/wiki/2.-Setup-a-new-route)"
},
"stops_train": {
"data": {
"origin": "Introduza a cidade de partida",
Expand Down

0 comments on commit 65c89d4

Please sign in to comment.