diff --git a/homeassistant/components/nextbus/const.py b/homeassistant/components/nextbus/const.py new file mode 100644 index 00000000000..9d9d0a5262f --- /dev/null +++ b/homeassistant/components/nextbus/const.py @@ -0,0 +1,6 @@ +"""NextBus Constants.""" +DOMAIN = "nextbus" + +CONF_AGENCY = "agency" +CONF_ROUTE = "route" +CONF_STOP = "stop" diff --git a/homeassistant/components/nextbus/sensor.py b/homeassistant/components/nextbus/sensor.py index 4f24a7aa7f3..02f5d8695ca 100644 --- a/homeassistant/components/nextbus/sensor.py +++ b/homeassistant/components/nextbus/sensor.py @@ -19,14 +19,11 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util.dt import utc_from_timestamp +from .const import CONF_AGENCY, CONF_ROUTE, CONF_STOP +from .util import listify, maybe_first + _LOGGER = logging.getLogger(__name__) -DOMAIN = "nextbus" - -CONF_AGENCY = "agency" -CONF_ROUTE = "route" -CONF_STOP = "stop" - PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_AGENCY): cv.string, @@ -37,29 +34,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( ) -def listify(maybe_list): - """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): - """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 - - def validate_value(value_name, value, value_list): """Validate tag value is in the list of items and logs error if not.""" valid_values = {v["tag"]: v["title"] for v in value_list} diff --git a/homeassistant/components/nextbus/util.py b/homeassistant/components/nextbus/util.py new file mode 100644 index 00000000000..c753c452546 --- /dev/null +++ b/homeassistant/components/nextbus/util.py @@ -0,0 +1,25 @@ +"""Utils for NextBus integration module.""" +from typing import Any + + +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]) -> 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