hass-core/homeassistant/components/nextbus/util.py
Ian 3e8d3083ac
Refactor NextBus integration to use new API (#121133)
* Refactor NextBus integration to use new API

This removes the `messages`, `directions`, and `attribution` attributes
from the sensor. Those may be added back in the future with additional
refactoring.

Some existing sensors may be broken today because of deprecated Agency
names. This patch will not migrate them as the migration path is
ambiguous. Setting up again should work though.

* Move result indexing outside of try/except
2024-07-24 18:18:21 +02:00

33 lines
948 B
Python

"""Utils for NextBus integration module."""
from typing import Any, NamedTuple
def listify(maybe_list: Any) -> list[Any]:
"""Return list version of whatever value is passed in.
This is used to provide a consistent way of interacting with the JSON
results from the API. There are several attributes that will either missing
if there are no values, a single dictionary if there is only one value, and
a list if there are multiple.
"""
if maybe_list is None:
return []
if isinstance(maybe_list, list):
return maybe_list
return [maybe_list]
def maybe_first(maybe_list: list[Any] | None) -> Any:
"""Return the first item out of a list or returns back the input."""
if isinstance(maybe_list, list) and maybe_list:
return maybe_list[0]
return maybe_list
class RouteStop(NamedTuple):
"""NamedTuple for a route and stop combination."""
route_id: str
stop_id: str