diff --git a/homeassistant/components/here_travel_time/config_flow.py b/homeassistant/components/here_travel_time/config_flow.py index 09faf95177d..54f164c43a7 100644 --- a/homeassistant/components/here_travel_time/config_flow.py +++ b/homeassistant/components/here_travel_time/config_flow.py @@ -15,6 +15,8 @@ from homeassistant.const import ( CONF_MODE, CONF_NAME, CONF_UNIT_SYSTEM, + CONF_UNIT_SYSTEM_IMPERIAL, + CONF_UNIT_SYSTEM_METRIC, ) from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult @@ -24,6 +26,7 @@ from homeassistant.helpers.selector import ( LocationSelector, TimeSelector, ) +from homeassistant.util.unit_system import IMPERIAL_SYSTEM from .const import ( CONF_ARRIVAL_TIME, @@ -88,13 +91,16 @@ def get_user_step_schema(data: dict[str, Any]) -> vol.Schema: def default_options(hass: HomeAssistant) -> dict[str, str | None]: """Get the default options.""" - return { + default = { CONF_TRAFFIC_MODE: TRAFFIC_MODE_ENABLED, CONF_ROUTE_MODE: ROUTE_MODE_FASTEST, CONF_ARRIVAL_TIME: None, CONF_DEPARTURE_TIME: None, - CONF_UNIT_SYSTEM: hass.config.units.name, + CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC, } + if hass.config.units is IMPERIAL_SYSTEM: + default[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_IMPERIAL + return default class HERETravelTimeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): @@ -255,24 +261,25 @@ class HERETravelTimeOptionsFlow(config_entries.OptionsFlow): menu_options=["departure_time", "no_time"], ) + defaults = default_options(self.hass) schema = vol.Schema( { vol.Optional( CONF_TRAFFIC_MODE, default=self.config_entry.options.get( - CONF_TRAFFIC_MODE, TRAFFIC_MODE_ENABLED + CONF_TRAFFIC_MODE, defaults[CONF_TRAFFIC_MODE] ), ): vol.In(TRAFFIC_MODES), vol.Optional( CONF_ROUTE_MODE, default=self.config_entry.options.get( - CONF_ROUTE_MODE, ROUTE_MODE_FASTEST + CONF_ROUTE_MODE, defaults[CONF_ROUTE_MODE] ), ): vol.In(ROUTE_MODES), vol.Optional( CONF_UNIT_SYSTEM, default=self.config_entry.options.get( - CONF_UNIT_SYSTEM, self.hass.config.units.name + CONF_UNIT_SYSTEM, defaults[CONF_UNIT_SYSTEM] ), ): vol.In(UNITS), } diff --git a/tests/components/here_travel_time/test_config_flow.py b/tests/components/here_travel_time/test_config_flow.py index b56f97a8053..1777258c6e9 100644 --- a/tests/components/here_travel_time/test_config_flow.py +++ b/tests/components/here_travel_time/test_config_flow.py @@ -31,6 +31,7 @@ from homeassistant.const import ( CONF_UNIT_SYSTEM_METRIC, ) from homeassistant.core import HomeAssistant +from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem from .const import ( API_KEY, @@ -227,10 +228,21 @@ async def test_step_destination_coordinates( @pytest.mark.usefixtures("valid_response") +@pytest.mark.parametrize( + "unit_system, expected_unit_option", + [ + (METRIC_SYSTEM, CONF_UNIT_SYSTEM_METRIC), + (IMPERIAL_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL), + ], +) async def test_step_destination_entity( - hass: HomeAssistant, origin_step_result: data_entry_flow.FlowResult + hass: HomeAssistant, + origin_step_result: data_entry_flow.FlowResult, + unit_system: UnitSystem, + expected_unit_option: str, ) -> None: """Test the origin coordinates step.""" + hass.config.units = unit_system menu_result = await hass.config_entries.flow.async_configure( origin_step_result["flow_id"], {"next_step_id": "destination_entity"} ) @@ -250,6 +262,13 @@ async def test_step_destination_entity( CONF_DESTINATION_ENTITY_ID: "zone.home", CONF_MODE: TRAVEL_MODE_CAR, } + assert entry.options == { + CONF_UNIT_SYSTEM: expected_unit_option, + CONF_ROUTE_MODE: ROUTE_MODE_FASTEST, + CONF_TRAFFIC_MODE: TRAFFIC_MODE_ENABLED, + CONF_ARRIVAL_TIME: None, + CONF_DEPARTURE_TIME: None, + } async def test_form_invalid_auth(hass: HomeAssistant) -> None: