From 609a7ccda85a48e3d14c14f789a66c5445bdd733 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Fri, 21 Apr 2023 12:57:45 +0200 Subject: [PATCH] Use Selectors for waze_travel_time flows (#91778) * Use Selectors for waze_travel_time flows * Use correct selector option in tests * Remove duplicate option * Use suggested values --- .../waze_travel_time/config_flow.py | 64 +++++++++++++------ .../components/waze_travel_time/const.py | 2 +- .../components/waze_travel_time/strings.json | 24 +++++++ tests/components/waze_travel_time/const.py | 6 ++ .../waze_travel_time/test_config_flow.py | 10 +-- 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/waze_travel_time/config_flow.py b/homeassistant/components/waze_travel_time/config_flow.py index b885da3f37b..a743844659c 100644 --- a/homeassistant/components/waze_travel_time/config_flow.py +++ b/homeassistant/components/waze_travel_time/config_flow.py @@ -7,7 +7,13 @@ from homeassistant import config_entries from homeassistant.const import CONF_NAME, CONF_REGION from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.selector import ( + BooleanSelector, + SelectSelector, + SelectSelectorConfig, + SelectSelectorMode, + TextSelector, +) from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM from .const import ( @@ -33,14 +39,41 @@ from .helpers import is_valid_config_entry OPTIONS_SCHEMA = vol.Schema( { - vol.Optional(CONF_INCL_FILTER, default=""): cv.string, - vol.Optional(CONF_EXCL_FILTER, default=""): cv.string, - vol.Optional(CONF_REALTIME): cv.boolean, - vol.Optional(CONF_VEHICLE_TYPE): vol.In(VEHICLE_TYPES), - vol.Optional(CONF_UNITS): vol.In(UNITS), - vol.Optional(CONF_AVOID_TOLL_ROADS): cv.boolean, - vol.Optional(CONF_AVOID_SUBSCRIPTION_ROADS): cv.boolean, - vol.Optional(CONF_AVOID_FERRIES): cv.boolean, + vol.Optional(CONF_INCL_FILTER, default=""): TextSelector(), + vol.Optional(CONF_EXCL_FILTER, default=""): TextSelector(), + vol.Optional(CONF_REALTIME): BooleanSelector(), + vol.Required(CONF_VEHICLE_TYPE): SelectSelector( + SelectSelectorConfig( + options=sorted(VEHICLE_TYPES), + mode=SelectSelectorMode.DROPDOWN, + translation_key=CONF_VEHICLE_TYPE, + ) + ), + vol.Required(CONF_UNITS): SelectSelector( + SelectSelectorConfig( + options=sorted(UNITS), + mode=SelectSelectorMode.DROPDOWN, + translation_key=CONF_UNITS, + ) + ), + vol.Optional(CONF_AVOID_TOLL_ROADS): BooleanSelector(), + vol.Optional(CONF_AVOID_SUBSCRIPTION_ROADS): BooleanSelector(), + vol.Optional(CONF_AVOID_FERRIES): BooleanSelector(), + } +) + +CONFIG_SCHEMA = vol.Schema( + { + vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(), + vol.Required(CONF_ORIGIN): TextSelector(), + vol.Required(CONF_DESTINATION): TextSelector(), + vol.Required(CONF_REGION): SelectSelector( + SelectSelectorConfig( + options=sorted(REGIONS), + mode=SelectSelectorMode.DROPDOWN, + translation_key=CONF_REGION, + ) + ), } ) @@ -95,6 +128,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): user_input = user_input or {} if user_input: + user_input[CONF_REGION] = user_input[CONF_REGION].upper() if await self.hass.async_add_executor_job( is_valid_config_entry, self.hass, @@ -110,18 +144,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # If we get here, it's because we couldn't connect errors["base"] = "cannot_connect" + user_input[CONF_REGION] = user_input[CONF_REGION].lower() return self.async_show_form( 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), - } - ), + data_schema=self.add_suggested_values_to_schema(CONFIG_SCHEMA, user_input), errors=errors, ) diff --git a/homeassistant/components/waze_travel_time/const.py b/homeassistant/components/waze_travel_time/const.py index 1121519f8cd..698ba5a63b2 100644 --- a/homeassistant/components/waze_travel_time/const.py +++ b/homeassistant/components/waze_travel_time/const.py @@ -25,7 +25,7 @@ IMPERIAL_UNITS = "imperial" METRIC_UNITS = "metric" UNITS = [METRIC_UNITS, IMPERIAL_UNITS] -REGIONS = ["US", "NA", "EU", "IL", "AU"] +REGIONS = ["us", "na", "eu", "il", "au"] VEHICLE_TYPES = ["car", "taxi", "motorcycle"] DEFAULT_OPTIONS: dict[str, str | bool] = { diff --git a/homeassistant/components/waze_travel_time/strings.json b/homeassistant/components/waze_travel_time/strings.json index 9ed2ba8dfee..61b93f13f17 100644 --- a/homeassistant/components/waze_travel_time/strings.json +++ b/homeassistant/components/waze_travel_time/strings.json @@ -35,5 +35,29 @@ } } } + }, + "selector": { + "vehicle_type": { + "options": { + "car": "Car", + "taxi": "Taxi", + "motorcycle": "Motorcycle" + } + }, + "units": { + "options": { + "metric": "Metric System", + "imperial": "Imperial System" + } + }, + "region": { + "options": { + "us": "USA", + "na": "North America", + "eu": "Europe", + "il": "Israel", + "au": "Australia" + } + } } } diff --git a/tests/components/waze_travel_time/const.py b/tests/components/waze_travel_time/const.py index f56e8c5892e..314a24d23e4 100644 --- a/tests/components/waze_travel_time/const.py +++ b/tests/components/waze_travel_time/const.py @@ -11,3 +11,9 @@ MOCK_CONFIG = { CONF_DESTINATION: "location2", CONF_REGION: "US", } + +CONFIG_FLOW_USER_INPUT = { + CONF_ORIGIN: "location1", + CONF_DESTINATION: "location2", + CONF_REGION: "us", +} diff --git a/tests/components/waze_travel_time/test_config_flow.py b/tests/components/waze_travel_time/test_config_flow.py index d58f8d9a34d..f1fd3041d46 100644 --- a/tests/components/waze_travel_time/test_config_flow.py +++ b/tests/components/waze_travel_time/test_config_flow.py @@ -21,7 +21,7 @@ from homeassistant.components.waze_travel_time.const import ( from homeassistant.const import CONF_NAME, CONF_REGION from homeassistant.core import HomeAssistant -from .const import MOCK_CONFIG +from .const import CONFIG_FLOW_USER_INPUT, MOCK_CONFIG from tests.common import MockConfigEntry @@ -37,7 +37,7 @@ async def test_minimum_fields(hass: HomeAssistant) -> None: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - MOCK_CONFIG, + CONFIG_FLOW_USER_INPUT, ) await hass.async_block_till_done() @@ -116,7 +116,7 @@ async def test_dupe(hass: HomeAssistant) -> None: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - MOCK_CONFIG, + CONFIG_FLOW_USER_INPUT, ) await hass.async_block_till_done() @@ -131,7 +131,7 @@ async def test_dupe(hass: HomeAssistant) -> None: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - MOCK_CONFIG, + CONFIG_FLOW_USER_INPUT, ) await hass.async_block_till_done() @@ -150,7 +150,7 @@ async def test_invalid_config_entry( assert result["errors"] == {} result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - MOCK_CONFIG, + CONFIG_FLOW_USER_INPUT, ) assert result2["type"] == data_entry_flow.FlowResultType.FORM