1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

Improve Waze Travel Time import and naming logic (#49838)

This commit is contained in:
Raman Gupta 2021-04-29 09:49:16 -04:00 committed by GitHub
parent 183220008d
commit 6f36fcc427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 29 deletions

View File

@ -103,31 +103,28 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
if user_input is not None:
if await self.hass.async_add_executor_job(
is_valid_config_entry,
self.hass,
_LOGGER,
user_input[CONF_ORIGIN],
user_input[CONF_DESTINATION],
user_input[CONF_REGION],
):
await self.async_set_unique_id(
slugify(
f"{DOMAIN}_{user_input[CONF_ORIGIN]}_{user_input[CONF_DESTINATION]}"
)
user_input = user_input or {}
if user_input:
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_ORIGIN],
user_input[CONF_DESTINATION],
user_input[CONF_REGION],
)
):
return self.async_create_entry(
title=(
user_input.get(
CONF_NAME,
(
f"{DEFAULT_NAME}: {user_input[CONF_ORIGIN]} -> "
f"{user_input[CONF_DESTINATION]}"
),
)
),
title=user_input.get(CONF_NAME, DEFAULT_NAME),
data=user_input,
)
@ -138,6 +135,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
step_id="user",
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_DESTINATION): cv.string,
vol.Required(CONF_REGION): vol.In(REGIONS),

View File

@ -43,7 +43,6 @@ from .const import (
DEFAULT_AVOID_FERRIES,
DEFAULT_AVOID_SUBSCRIPTION_ROADS,
DEFAULT_AVOID_TOLL_ROADS,
DEFAULT_NAME,
DEFAULT_REALTIME,
DEFAULT_VEHICLE_TYPE,
DOMAIN,
@ -117,10 +116,9 @@ async def async_setup_entry(
CONF_AVOID_SUBSCRIPTION_ROADS: DEFAULT_AVOID_SUBSCRIPTION_ROADS,
CONF_AVOID_TOLL_ROADS: DEFAULT_AVOID_TOLL_ROADS,
}
name = None
if not config_entry.options:
new_data = config_entry.data.copy()
name = new_data.pop(CONF_NAME, None)
options = {}
for key in [
CONF_INCL_FILTER,
@ -144,7 +142,7 @@ async def async_setup_entry(
destination = config_entry.data[CONF_DESTINATION]
origin = config_entry.data[CONF_ORIGIN]
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(
is_valid_config_entry, hass, _LOGGER, origin, destination, region

View File

@ -5,6 +5,7 @@
"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.",
"data": {
"name": "[%key:common::config_flow::data::name%]",
"origin": "Origin",
"destination": "Destination",
"region": "Region"

View File

@ -14,7 +14,7 @@ from homeassistant.components.waze_travel_time.const import (
DEFAULT_NAME,
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
@ -38,8 +38,9 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
await hass.async_block_till_done()
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"] == {
CONF_NAME: DEFAULT_NAME,
CONF_ORIGIN: "location1",
CONF_DESTINATION: "location2",
CONF_REGION: "US",