mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Improve Waze Travel Time import and naming logic (#49838)
This commit is contained in:
parent
183220008d
commit
6f36fcc427
@ -103,31 +103,28 @@ 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(
|
|
||||||
is_valid_config_entry,
|
if user_input:
|
||||||
self.hass,
|
|
||||||
_LOGGER,
|
|
||||||
user_input[CONF_ORIGIN],
|
|
||||||
user_input[CONF_DESTINATION],
|
|
||||||
user_input[CONF_REGION],
|
|
||||||
):
|
|
||||||
await self.async_set_unique_id(
|
await self.async_set_unique_id(
|
||||||
slugify(
|
slugify(
|
||||||
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
|
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_create_entry(
|
if (
|
||||||
title=(
|
self.source == config_entries.SOURCE_IMPORT
|
||||||
user_input.get(
|
or await self.hass.async_add_executor_job(
|
||||||
CONF_NAME,
|
is_valid_config_entry,
|
||||||
(
|
self.hass,
|
||||||
f"{DEFAULT_NAME}: {user_input[CONF_ORIGIN]} -> "
|
_LOGGER,
|
||||||
f"{user_input[CONF_DESTINATION]}"
|
user_input[CONF_ORIGIN],
|
||||||
),
|
user_input[CONF_DESTINATION],
|
||||||
|
user_input[CONF_REGION],
|
||||||
)
|
)
|
||||||
),
|
):
|
||||||
|
return self.async_create_entry(
|
||||||
|
title=user_input.get(CONF_NAME, DEFAULT_NAME),
|
||||||
data=user_input,
|
data=user_input,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -138,6 +135,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_ORIGIN): cv.string,
|
vol.Required(CONF_ORIGIN): cv.string,
|
||||||
vol.Required(CONF_DESTINATION): cv.string,
|
vol.Required(CONF_DESTINATION): cv.string,
|
||||||
vol.Required(CONF_REGION): vol.In(REGIONS),
|
vol.Required(CONF_REGION): vol.In(REGIONS),
|
||||||
|
@ -43,7 +43,6 @@ from .const import (
|
|||||||
DEFAULT_AVOID_FERRIES,
|
DEFAULT_AVOID_FERRIES,
|
||||||
DEFAULT_AVOID_SUBSCRIPTION_ROADS,
|
DEFAULT_AVOID_SUBSCRIPTION_ROADS,
|
||||||
DEFAULT_AVOID_TOLL_ROADS,
|
DEFAULT_AVOID_TOLL_ROADS,
|
||||||
DEFAULT_NAME,
|
|
||||||
DEFAULT_REALTIME,
|
DEFAULT_REALTIME,
|
||||||
DEFAULT_VEHICLE_TYPE,
|
DEFAULT_VEHICLE_TYPE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -117,10 +116,9 @@ async def async_setup_entry(
|
|||||||
CONF_AVOID_SUBSCRIPTION_ROADS: DEFAULT_AVOID_SUBSCRIPTION_ROADS,
|
CONF_AVOID_SUBSCRIPTION_ROADS: DEFAULT_AVOID_SUBSCRIPTION_ROADS,
|
||||||
CONF_AVOID_TOLL_ROADS: DEFAULT_AVOID_TOLL_ROADS,
|
CONF_AVOID_TOLL_ROADS: DEFAULT_AVOID_TOLL_ROADS,
|
||||||
}
|
}
|
||||||
name = None
|
|
||||||
if not config_entry.options:
|
if not config_entry.options:
|
||||||
new_data = config_entry.data.copy()
|
new_data = config_entry.data.copy()
|
||||||
name = new_data.pop(CONF_NAME, None)
|
|
||||||
options = {}
|
options = {}
|
||||||
for key in [
|
for key in [
|
||||||
CONF_INCL_FILTER,
|
CONF_INCL_FILTER,
|
||||||
@ -144,7 +142,7 @@ async def async_setup_entry(
|
|||||||
destination = config_entry.data[CONF_DESTINATION]
|
destination = config_entry.data[CONF_DESTINATION]
|
||||||
origin = config_entry.data[CONF_ORIGIN]
|
origin = config_entry.data[CONF_ORIGIN]
|
||||||
region = config_entry.data[CONF_REGION]
|
region = config_entry.data[CONF_REGION]
|
||||||
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, origin, destination, region
|
is_valid_config_entry, hass, _LOGGER, origin, destination, region
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
"user": {
|
"user": {
|
||||||
"description": "For Origin and Destination, enter the address or the GPS coordinates of the location (GPS coordinates has to be separated by a comma). You can also enter an entity id which provides this information in its state, an entity id with latitude and longitude attributes, or zone friendly name.",
|
"description": "For Origin and Destination, enter the address or the GPS coordinates of the location (GPS coordinates has to be separated by a comma). You can also enter an entity id which provides this information in its state, an entity id with latitude and longitude attributes, or zone friendly name.",
|
||||||
"data": {
|
"data": {
|
||||||
|
"name": "[%key:common::config_flow::data::name%]",
|
||||||
"origin": "Origin",
|
"origin": "Origin",
|
||||||
"destination": "Destination",
|
"destination": "Destination",
|
||||||
"region": "Region"
|
"region": "Region"
|
||||||
|
@ -14,7 +14,7 @@ from homeassistant.components.waze_travel_time.const import (
|
|||||||
DEFAULT_NAME,
|
DEFAULT_NAME,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_REGION, CONF_UNIT_SYSTEM_IMPERIAL
|
from homeassistant.const import CONF_NAME, CONF_REGION, CONF_UNIT_SYSTEM_IMPERIAL
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -38,8 +38,9 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
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_ORIGIN: "location1",
|
CONF_ORIGIN: "location1",
|
||||||
CONF_DESTINATION: "location2",
|
CONF_DESTINATION: "location2",
|
||||||
CONF_REGION: "US",
|
CONF_REGION: "US",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user