Skip to content

Commit

Permalink
Improve Waze Travel Time import and naming logic (home-assistant#49838)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 authored Apr 29, 2021
1 parent 1832200 commit 6f36fcc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
46 changes: 23 additions & 23 deletions homeassistant/components/waze_travel_time/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,28 @@ def async_get_options_flow(
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,
)

Expand All @@ -138,6 +135,9 @@ async def async_step_user(self, user_input=None):
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),
Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/waze_travel_time/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
DEFAULT_AVOID_FERRIES,
DEFAULT_AVOID_SUBSCRIPTION_ROADS,
DEFAULT_AVOID_TOLL_ROADS,
DEFAULT_NAME,
DEFAULT_REALTIME,
DEFAULT_VEHICLE_TYPE,
DOMAIN,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/waze_travel_time/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions tests/components/waze_travel_time/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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

Expand All @@ -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",
Expand Down

0 comments on commit 6f36fcc

Please sign in to comment.