Cleanup config flow and tests for here_travel_time (#72364)

pull/72453/head
Kevin Stillhammer 2022-05-23 16:50:05 +02:00 committed by GitHub
parent d0556e6dd1
commit 967f4efc56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 27 deletions

View File

@ -41,11 +41,9 @@ from .const import (
CONF_ORIGIN_LATITUDE,
CONF_ORIGIN_LONGITUDE,
CONF_ROUTE_MODE,
CONF_TRAFFIC_MODE,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
NO_ROUTE_ERROR_MESSAGE,
ROUTE_MODE_FASTEST,
TRAFFIC_MODE_ENABLED,
TRAVEL_MODES_VEHICLE,
)
@ -60,7 +58,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
"""Set up HERE Travel Time from a config entry."""
api_key = config_entry.data[CONF_API_KEY]
here_client = RoutingApi(api_key)
setup_options(hass, config_entry)
arrival = (
dt.parse_time(config_entry.options[CONF_ARRIVAL_TIME])
@ -98,21 +95,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True
def setup_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Set up options for a config entry if not set."""
if not config_entry.options:
hass.config_entries.async_update_entry(
config_entry,
options={
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,
},
)
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(

View File

@ -15,7 +15,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_UNIT_SYSTEM,
)
from homeassistant.core import callback
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 (
@ -128,12 +128,25 @@ 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 {
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,
}
class HERETravelTimeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for HERE Travel Time."""
VERSION = 1
_config: dict[str, Any] = {}
def __init__(self) -> None:
"""Init Config Flow."""
self._config: dict[str, Any] = {}
@staticmethod
@callback
@ -211,7 +224,9 @@ class HERETravelTimeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"longitude"
]
return self.async_create_entry(
title=self._config[CONF_NAME], data=self._config
title=self._config[CONF_NAME],
data=self._config,
options=default_options(self.hass),
)
schema = vol.Schema(
{"destination": selector({LocationSelector.selector_type: {}})}
@ -230,7 +245,9 @@ class HERETravelTimeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
CONF_DESTINATION_ENTITY_ID
]
return self.async_create_entry(
title=self._config[CONF_NAME], data=self._config
title=self._config[CONF_NAME],
data=self._config,
options=default_options(self.hass),
)
schema = vol.Schema(
{CONF_DESTINATION_ENTITY_ID: selector({EntitySelector.selector_type: {}})}
@ -301,11 +318,10 @@ class HERETravelTimeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class HERETravelTimeOptionsFlow(config_entries.OptionsFlow):
"""Handle HERE Travel Time options."""
_config: dict[str, Any] = {}
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize HERE Travel Time options flow."""
self.config_entry = config_entry
self._config: dict[str, Any] = {}
async def async_step_init(
self, user_input: dict[str, Any] | None = None

View File

@ -50,6 +50,16 @@ from .const import (
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
def bypass_setup_fixture():
"""Prevent setup."""
with patch(
"homeassistant.components.here_travel_time.async_setup_entry",
return_value=True,
):
yield
@pytest.fixture(name="user_step_result")
async def user_step_result_fixture(hass: HomeAssistant) -> data_entry_flow.FlowResult:
"""Provide the result of a completed user step."""
@ -65,7 +75,7 @@ async def user_step_result_fixture(hass: HomeAssistant) -> data_entry_flow.FlowR
},
)
await hass.async_block_till_done()
yield user_step_result
return user_step_result
@pytest.fixture(name="option_init_result")
@ -96,7 +106,7 @@ async def option_init_result_fixture(hass: HomeAssistant) -> data_entry_flow.Flo
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC,
},
)
yield result
return result
@pytest.fixture(name="origin_step_result")
@ -118,7 +128,7 @@ async def origin_step_result_fixture(
}
},
)
yield location_selector_result
return location_selector_result
@pytest.mark.parametrize(

View File

@ -2,6 +2,7 @@
import pytest
from homeassistant.components.here_travel_time.config_flow import default_options
from homeassistant.components.here_travel_time.const import (
CONF_DESTINATION_LATITUDE,
CONF_DESTINATION_LONGITUDE,
@ -39,6 +40,7 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
CONF_MODE: TRAVEL_MODE_CAR,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)

View File

@ -5,6 +5,7 @@ from herepy.here_enum import RouteMode
from herepy.routing_api import NoRouteFoundError
import pytest
from homeassistant.components.here_travel_time.config_flow import default_options
from homeassistant.components.here_travel_time.const import (
ATTR_DESTINATION,
ATTR_DESTINATION_NAME,
@ -224,6 +225,7 @@ async def test_circular_ref(hass: HomeAssistant, caplog):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -250,6 +252,7 @@ async def test_no_attribution(hass: HomeAssistant):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -293,6 +296,7 @@ async def test_entity_ids(hass: HomeAssistant, valid_response: MagicMock):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -332,6 +336,7 @@ async def test_destination_entity_not_found(hass: HomeAssistant, caplog):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -357,6 +362,7 @@ async def test_origin_entity_not_found(hass: HomeAssistant, caplog):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -386,6 +392,7 @@ async def test_invalid_destination_entity_state(hass: HomeAssistant, caplog):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -415,6 +422,7 @@ async def test_invalid_origin_entity_state(hass: HomeAssistant, caplog):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -444,6 +452,7 @@ async def test_route_not_found(hass: HomeAssistant, caplog):
CONF_MODE: TRAVEL_MODE_TRUCK,
CONF_NAME: "test",
},
options=default_options(hass),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)