mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Improve Google Travel Time import and naming logic (#49839)
This commit is contained in:
parent
5d421c9715
commit
4a95f6c2a3
@ -122,29 +122,27 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(self, user_input=None):
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
user_input = user_input or {}
|
||||||
if await self.hass.async_add_executor_job(
|
if user_input:
|
||||||
is_valid_config_entry,
|
await self.async_set_unique_id(
|
||||||
self.hass,
|
slugify(
|
||||||
_LOGGER,
|
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
|
||||||
user_input[CONF_API_KEY],
|
|
||||||
user_input[CONF_ORIGIN],
|
|
||||||
user_input[CONF_DESTINATION],
|
|
||||||
):
|
|
||||||
await self.async_set_unique_id(
|
|
||||||
slugify(
|
|
||||||
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self._abort_if_unique_id_configured()
|
)
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
if (
|
||||||
|
self.source == config_entries.SOURCE_IMPORT
|
||||||
|
or await self.hass.async_add_executor_job(
|
||||||
|
is_valid_config_entry,
|
||||||
|
self.hass,
|
||||||
|
_LOGGER,
|
||||||
|
user_input[CONF_API_KEY],
|
||||||
|
user_input[CONF_ORIGIN],
|
||||||
|
user_input[CONF_DESTINATION],
|
||||||
|
)
|
||||||
|
):
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input.get(
|
title=user_input.get(CONF_NAME, DEFAULT_NAME),
|
||||||
CONF_NAME,
|
|
||||||
(
|
|
||||||
f"{DEFAULT_NAME}: {user_input[CONF_ORIGIN]} -> "
|
|
||||||
f"{user_input[CONF_DESTINATION]}"
|
|
||||||
),
|
|
||||||
),
|
|
||||||
data=user_input,
|
data=user_input,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -155,6 +153,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema(
|
data_schema=vol.Schema(
|
||||||
{
|
{
|
||||||
|
vol.Required(
|
||||||
|
CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
|
||||||
|
): cv.string,
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
vol.Required(CONF_DESTINATION): cv.string,
|
vol.Required(CONF_DESTINATION): cv.string,
|
||||||
vol.Required(CONF_ORIGIN): cv.string,
|
vol.Required(CONF_ORIGIN): cv.string,
|
||||||
|
@ -40,7 +40,6 @@ from .const import (
|
|||||||
CONF_TRANSIT_ROUTING_PREFERENCE,
|
CONF_TRANSIT_ROUTING_PREFERENCE,
|
||||||
CONF_TRAVEL_MODE,
|
CONF_TRAVEL_MODE,
|
||||||
CONF_UNITS,
|
CONF_UNITS,
|
||||||
DEFAULT_NAME,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
TRACKABLE_DOMAINS,
|
TRACKABLE_DOMAINS,
|
||||||
TRANSIT_PREFS,
|
TRANSIT_PREFS,
|
||||||
@ -100,11 +99,9 @@ async def async_setup_entry(
|
|||||||
async_add_entities: Callable[[list[SensorEntity], bool], None],
|
async_add_entities: Callable[[list[SensorEntity], bool], None],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a Google travel time sensor entry."""
|
"""Set up a Google travel time sensor entry."""
|
||||||
name = None
|
|
||||||
if not config_entry.options:
|
if not config_entry.options:
|
||||||
new_data = config_entry.data.copy()
|
new_data = config_entry.data.copy()
|
||||||
options = new_data.pop(CONF_OPTIONS, {})
|
options = new_data.pop(CONF_OPTIONS, {})
|
||||||
name = new_data.pop(CONF_NAME, None)
|
|
||||||
|
|
||||||
if CONF_UNITS not in options:
|
if CONF_UNITS not in options:
|
||||||
options[CONF_UNITS] = hass.config.units.name
|
options[CONF_UNITS] = hass.config.units.name
|
||||||
@ -129,7 +126,7 @@ async def async_setup_entry(
|
|||||||
api_key = config_entry.data[CONF_API_KEY]
|
api_key = config_entry.data[CONF_API_KEY]
|
||||||
origin = config_entry.data[CONF_ORIGIN]
|
origin = config_entry.data[CONF_ORIGIN]
|
||||||
destination = config_entry.data[CONF_DESTINATION]
|
destination = config_entry.data[CONF_DESTINATION]
|
||||||
name = name or f"{DEFAULT_NAME}: {origin} -> {destination}"
|
name = config_entry.data[CONF_NAME]
|
||||||
|
|
||||||
if not await hass.async_add_executor_job(
|
if not await hass.async_add_executor_job(
|
||||||
is_valid_config_entry, hass, _LOGGER, api_key, origin, destination
|
is_valid_config_entry, hass, _LOGGER, api_key, origin, destination
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
"user": {
|
"user": {
|
||||||
"description": "When specifying the origin and destination, you can supply one or more locations separated by the pipe character, in the form of an address, latitude/longitude coordinates, or a Google place ID. When specifying the location using a Google place ID, the ID must be prefixed with `place_id:`.",
|
"description": "When specifying the origin and destination, you can supply one or more locations separated by the pipe character, in the form of an address, latitude/longitude coordinates, or a Google place ID. When specifying the location using a Google place ID, the ID must be prefixed with `place_id:`.",
|
||||||
"data": {
|
"data": {
|
||||||
|
"name": "[%key:common::config_flow::data::name%]",
|
||||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||||
"origin": "Origin",
|
"origin": "Origin",
|
||||||
"destination": "Destination"
|
"destination": "Destination"
|
||||||
|
@ -47,8 +47,9 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result2["title"] == f"{DEFAULT_NAME}: location1 -> location2"
|
assert result2["title"] == DEFAULT_NAME
|
||||||
assert result2["data"] == {
|
assert result2["data"] == {
|
||||||
|
CONF_NAME: DEFAULT_NAME,
|
||||||
CONF_API_KEY: "api_key",
|
CONF_API_KEY: "api_key",
|
||||||
CONF_ORIGIN: "location1",
|
CONF_ORIGIN: "location1",
|
||||||
CONF_DESTINATION: "location2",
|
CONF_DESTINATION: "location2",
|
||||||
@ -281,6 +282,7 @@ async def test_import_flow(hass, validate_config_entry, bypass_update):
|
|||||||
|
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
assert entry.data == {
|
assert entry.data == {
|
||||||
|
CONF_NAME: "test_name",
|
||||||
CONF_API_KEY: "api_key",
|
CONF_API_KEY: "api_key",
|
||||||
CONF_ORIGIN: "location1",
|
CONF_ORIGIN: "location1",
|
||||||
CONF_DESTINATION: "location2",
|
CONF_DESTINATION: "location2",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user