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

Make number of derpartures configurable #6

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions custom_components/trias/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async def async_step_stops(
description={"suggested_value": stop_id_dict},
): selector.ObjectSelector(),
vol.Optional("add_stop", default=False): bool,
vol.Optional("departure_limit_config", default=5): int,
}
),
)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/trias/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(

self.client: trias.Client = trias.Client(url=self._url, api_key=self._api_key)
self.stop_ids: list[str] = entry.options.get("stop_ids", [])
self.departure_limit: str = entry.options.get("departure_limit_config")
self.stops: dict[dict] = {}

self.trip_list: list[str] = entry.options.get(
Expand Down Expand Up @@ -204,7 +205,7 @@ async def _async_update_data(self) -> dict:
self.stops[stop_id]["prevestly_ok"] = self.stops[stop_id]["ok"]
try:
departures = await self._hass.async_add_executor_job(
self.client.get_departures, stop_id
self.client.get_departures, stop_id, self.departure_limit
)
except ApiError as err:
self.stops[stop_id]["ok"] = False
Expand Down
4 changes: 3 additions & 1 deletion custom_components/trias/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def __init__(self, stop, coordinator):
@property
def native_value(self):
"""Return the state of the device."""
return self.coordinator.stops[self._stop_id]["data"].get("next_departure", None)
stop = self.coordinator.stops[self._stop_id]
self._attr_extra_state_attributes.update(stop["attrs"])
return stop["data"].get("next_departure", None)


class TripSensor(TriasCoordinatorEntity, SensorEntity):
Expand Down
3 changes: 2 additions & 1 deletion custom_components/trias/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"description": "Stops that will be created as Sensors",
"data": {
"stop_id_dict": "Stop List",
"add_stop": "Add new Stop"
"add_stop": "Add new Stop",
"departure_limit_config": "Maximum number of Departures"
},
"data_description": {
"stop_id_dict": "Remove key to remove Sensor",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/trias/trias_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def location_information_request(

return result["LocationInformationResponse"]

def get_departures(self, location_name, number_results=1, dt=None):
def get_departures(self, location_name, number_results, dt=None):
"""
Get formatted departures from a station.

Expand Down
6 changes: 3 additions & 3 deletions custom_components/trias/trias_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def parse_duration(duration):
return str(parsed_duration)


def get_timedelta(start_dt, end_dt):
def get_timedelta(start_dt: datetime.datetime, end_dt: datetime.datetime):
if not start_dt or not end_dt:
return None
return "none"

# Calculate the timedelta
delta = end_dt - start_dt

return delta
return str(delta)
Loading