Move nextbus constants and utils (#92211)

* NextBus: Move general components to const and util

* Remove not yet used code
This commit is contained in:
Ian 2023-05-11 09:28:29 -07:00 committed by GitHub
parent aee3f115d6
commit 0fcf8f968b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 29 deletions

View File

@ -0,0 +1,6 @@
"""NextBus Constants."""
DOMAIN = "nextbus"
CONF_AGENCY = "agency"
CONF_ROUTE = "route"
CONF_STOP = "stop"

View File

@ -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}

View File

@ -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