mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
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
This commit is contained in:
parent
3b0068bc85
commit
609a7ccda8
@ -7,7 +7,13 @@ from homeassistant import config_entries
|
|||||||
from homeassistant.const import CONF_NAME, CONF_REGION
|
from homeassistant.const import CONF_NAME, CONF_REGION
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
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 homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -33,14 +39,41 @@ from .helpers import is_valid_config_entry
|
|||||||
|
|
||||||
OPTIONS_SCHEMA = vol.Schema(
|
OPTIONS_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_INCL_FILTER, default=""): cv.string,
|
vol.Optional(CONF_INCL_FILTER, default=""): TextSelector(),
|
||||||
vol.Optional(CONF_EXCL_FILTER, default=""): cv.string,
|
vol.Optional(CONF_EXCL_FILTER, default=""): TextSelector(),
|
||||||
vol.Optional(CONF_REALTIME): cv.boolean,
|
vol.Optional(CONF_REALTIME): BooleanSelector(),
|
||||||
vol.Optional(CONF_VEHICLE_TYPE): vol.In(VEHICLE_TYPES),
|
vol.Required(CONF_VEHICLE_TYPE): SelectSelector(
|
||||||
vol.Optional(CONF_UNITS): vol.In(UNITS),
|
SelectSelectorConfig(
|
||||||
vol.Optional(CONF_AVOID_TOLL_ROADS): cv.boolean,
|
options=sorted(VEHICLE_TYPES),
|
||||||
vol.Optional(CONF_AVOID_SUBSCRIPTION_ROADS): cv.boolean,
|
mode=SelectSelectorMode.DROPDOWN,
|
||||||
vol.Optional(CONF_AVOID_FERRIES): cv.boolean,
|
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 {}
|
user_input = user_input or {}
|
||||||
|
|
||||||
if user_input:
|
if user_input:
|
||||||
|
user_input[CONF_REGION] = user_input[CONF_REGION].upper()
|
||||||
if await self.hass.async_add_executor_job(
|
if await self.hass.async_add_executor_job(
|
||||||
is_valid_config_entry,
|
is_valid_config_entry,
|
||||||
self.hass,
|
self.hass,
|
||||||
@ -110,18 +144,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
# If we get here, it's because we couldn't connect
|
# If we get here, it's because we couldn't connect
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
user_input[CONF_REGION] = user_input[CONF_REGION].lower()
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema(
|
data_schema=self.add_suggested_values_to_schema(CONFIG_SCHEMA, user_input),
|
||||||
{
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
),
|
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
@ -25,7 +25,7 @@ IMPERIAL_UNITS = "imperial"
|
|||||||
METRIC_UNITS = "metric"
|
METRIC_UNITS = "metric"
|
||||||
UNITS = [METRIC_UNITS, IMPERIAL_UNITS]
|
UNITS = [METRIC_UNITS, IMPERIAL_UNITS]
|
||||||
|
|
||||||
REGIONS = ["US", "NA", "EU", "IL", "AU"]
|
REGIONS = ["us", "na", "eu", "il", "au"]
|
||||||
VEHICLE_TYPES = ["car", "taxi", "motorcycle"]
|
VEHICLE_TYPES = ["car", "taxi", "motorcycle"]
|
||||||
|
|
||||||
DEFAULT_OPTIONS: dict[str, str | bool] = {
|
DEFAULT_OPTIONS: dict[str, str | bool] = {
|
||||||
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,3 +11,9 @@ MOCK_CONFIG = {
|
|||||||
CONF_DESTINATION: "location2",
|
CONF_DESTINATION: "location2",
|
||||||
CONF_REGION: "US",
|
CONF_REGION: "US",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CONFIG_FLOW_USER_INPUT = {
|
||||||
|
CONF_ORIGIN: "location1",
|
||||||
|
CONF_DESTINATION: "location2",
|
||||||
|
CONF_REGION: "us",
|
||||||
|
}
|
||||||
|
@ -21,7 +21,7 @@ from homeassistant.components.waze_travel_time.const import (
|
|||||||
from homeassistant.const import CONF_NAME, CONF_REGION
|
from homeassistant.const import CONF_NAME, CONF_REGION
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import MOCK_CONFIG
|
from .const import CONFIG_FLOW_USER_INPUT, MOCK_CONFIG
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
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(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
MOCK_CONFIG,
|
CONFIG_FLOW_USER_INPUT,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
MOCK_CONFIG,
|
CONFIG_FLOW_USER_INPUT,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
MOCK_CONFIG,
|
CONFIG_FLOW_USER_INPUT,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -150,7 +150,7 @@ async def test_invalid_config_entry(
|
|||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
MOCK_CONFIG,
|
CONFIG_FLOW_USER_INPUT,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
|
Loading…
x
Reference in New Issue
Block a user