Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Utils for NextBus integration module."""
2 
3 from typing import Any, NamedTuple
4 
5 
6 def listify(maybe_list: Any) -> list[Any]:
7  """Return list version of whatever value is passed in.
8 
9  This is used to provide a consistent way of interacting with the JSON
10  results from the API. There are several attributes that will either missing
11  if there are no values, a single dictionary if there is only one value, and
12  a list if there are multiple.
13  """
14  if maybe_list is None:
15  return []
16  if isinstance(maybe_list, list):
17  return maybe_list
18  return [maybe_list]
19 
20 
21 def maybe_first(maybe_list: list[Any] | None) -> Any:
22  """Return the first item out of a list or returns back the input."""
23  if isinstance(maybe_list, list) and maybe_list:
24  return maybe_list[0]
25 
26  return maybe_list
27 
28 
29 class RouteStop(NamedTuple):
30  """NamedTuple for a route and stop combination."""
31 
32  route_id: str
33  stop_id: str
Any maybe_first(list[Any]|None maybe_list)
Definition: util.py:21
list[Any] listify(Any maybe_list)
Definition: util.py:6