From 6f36fcc42703a0b7225099ee42ede578e7b49b35 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Thu, 29 Apr 2021 09:49:16 -0400 Subject: [PATCH] Improve Waze Travel Time import and naming logic (#49838) --- .../waze_travel_time/config_flow.py | 46 +++++++++---------- .../components/waze_travel_time/sensor.py | 6 +-- .../components/waze_travel_time/strings.json | 1 + .../waze_travel_time/test_config_flow.py | 5 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/waze_travel_time/config_flow.py b/homeassistant/components/waze_travel_time/config_flow.py index 05dd372f9d9..4100284273e 100644 --- a/homeassistant/components/waze_travel_time/config_flow.py +++ b/homeassistant/components/waze_travel_time/config_flow.py @@ -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), diff --git a/homeassistant/components/waze_travel_time/sensor.py b/homeassistant/components/waze_travel_time/sensor.py index 8e7ca04fec6..02446849e98 100644 --- a/homeassistant/components/waze_travel_time/sensor.py +++ b/homeassistant/components/waze_travel_time/sensor.py @@ -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 diff --git a/homeassistant/components/waze_travel_time/strings.json b/homeassistant/components/waze_travel_time/strings.json index 082ee31db73..0417962ccbf 100644 --- a/homeassistant/components/waze_travel_time/strings.json +++ b/homeassistant/components/waze_travel_time/strings.json @@ -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" diff --git a/tests/components/waze_travel_time/test_config_flow.py b/tests/components/waze_travel_time/test_config_flow.py index f6f1614ca25..b6690be4d86 100644 --- a/tests/components/waze_travel_time/test_config_flow.py +++ b/tests/components/waze_travel_time/test_config_flow.py @@ -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",