Move default option handling to config_flow for google_travel_time (#80607)
Move default option handling to config_flow
This commit is contained in:
parent
60b3d6816b
commit
b35cfe711a
4 changed files with 26 additions and 63 deletions
|
@ -5,9 +5,10 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_API_KEY, CONF_MODE, CONF_NAME
|
||||
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.util.unit_system import IMPERIAL_SYSTEM
|
||||
|
||||
from .const import (
|
||||
ALL_LANGUAGES,
|
||||
|
@ -34,10 +35,22 @@ from .const import (
|
|||
TRAVEL_MODE,
|
||||
TRAVEL_MODEL,
|
||||
UNITS,
|
||||
UNITS_IMPERIAL,
|
||||
UNITS_METRIC,
|
||||
)
|
||||
from .helpers import InvalidApiKeyException, UnknownException, validate_config_entry
|
||||
|
||||
|
||||
def default_options(hass: HomeAssistant) -> dict[str, str | None]:
|
||||
"""Get the default options."""
|
||||
return {
|
||||
CONF_MODE: "driving",
|
||||
CONF_UNITS: (
|
||||
UNITS_IMPERIAL if hass.config.units is IMPERIAL_SYSTEM else UNITS_METRIC
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
class GoogleOptionsFlow(config_entries.OptionsFlow):
|
||||
"""Handle an options flow for Google Travel Time."""
|
||||
|
||||
|
@ -135,6 +148,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
return self.async_create_entry(
|
||||
title=user_input.get(CONF_NAME, DEFAULT_NAME),
|
||||
data=user_input,
|
||||
options=default_options(self.hass),
|
||||
)
|
||||
except InvalidApiKeyException:
|
||||
errors["base"] = "invalid_auth"
|
||||
|
|
|
@ -11,7 +11,6 @@ from homeassistant.components.sensor import SensorEntity
|
|||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_MODE,
|
||||
CONF_NAME,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
TIME_MINUTES,
|
||||
|
@ -22,21 +21,15 @@ from homeassistant.helpers.entity import DeviceInfo
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.location import find_coordinates
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||
|
||||
from .const import (
|
||||
ATTRIBUTION,
|
||||
CONF_ARRIVAL_TIME,
|
||||
CONF_DEPARTURE_TIME,
|
||||
CONF_DESTINATION,
|
||||
CONF_OPTIONS,
|
||||
CONF_ORIGIN,
|
||||
CONF_TRAVEL_MODE,
|
||||
CONF_UNITS,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
UNITS_IMPERIAL,
|
||||
UNITS_METRIC,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -60,32 +53,6 @@ async def async_setup_entry(
|
|||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a Google travel time sensor entry."""
|
||||
if not config_entry.options:
|
||||
new_data = config_entry.data.copy()
|
||||
options = new_data.pop(CONF_OPTIONS, {})
|
||||
|
||||
if CONF_UNITS not in options:
|
||||
options[CONF_UNITS] = UNITS_METRIC
|
||||
if hass.config.units is US_CUSTOMARY_SYSTEM:
|
||||
options[CONF_UNITS] = UNITS_IMPERIAL
|
||||
|
||||
if CONF_TRAVEL_MODE in new_data:
|
||||
wstr = (
|
||||
"Google Travel Time: travel_mode is deprecated, please "
|
||||
"add mode to the options dictionary instead!"
|
||||
)
|
||||
_LOGGER.warning(wstr)
|
||||
travel_mode = new_data.pop(CONF_TRAVEL_MODE)
|
||||
if CONF_MODE not in options:
|
||||
options[CONF_MODE] = travel_mode
|
||||
|
||||
if CONF_MODE not in options:
|
||||
options[CONF_MODE] = "driving"
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry, data=new_data, options=options
|
||||
)
|
||||
|
||||
api_key = config_entry.data[CONF_API_KEY]
|
||||
origin = config_entry.data[CONF_ORIGIN]
|
||||
destination = config_entry.data[CONF_DESTINATION]
|
||||
|
|
|
@ -141,7 +141,6 @@ async def test_malformed_api_key(hass):
|
|||
MOCK_CONFIG,
|
||||
{
|
||||
CONF_MODE: "driving",
|
||||
CONF_ARRIVAL_TIME: "test",
|
||||
CONF_UNITS: UNITS_IMPERIAL,
|
||||
},
|
||||
)
|
||||
|
@ -198,7 +197,15 @@ async def test_options_flow(hass, mock_config):
|
|||
|
||||
@pytest.mark.parametrize(
|
||||
"data,options",
|
||||
[(MOCK_CONFIG, {})],
|
||||
[
|
||||
(
|
||||
MOCK_CONFIG,
|
||||
{
|
||||
CONF_MODE: "driving",
|
||||
CONF_UNITS: UNITS_IMPERIAL,
|
||||
},
|
||||
)
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("validate_config_entry")
|
||||
async def test_options_flow_departure_time(hass, mock_config):
|
||||
|
|
|
@ -4,10 +4,10 @@ from unittest.mock import patch
|
|||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.google_travel_time.config_flow import default_options
|
||||
from homeassistant.components.google_travel_time.const import (
|
||||
CONF_ARRIVAL_TIME,
|
||||
CONF_DEPARTURE_TIME,
|
||||
CONF_TRAVEL_MODE,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC
|
||||
|
@ -202,31 +202,6 @@ async def test_sensor_arrival_time_custom_timestamp(hass):
|
|||
assert hass.states.get("sensor.google_travel_time").state == "27"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_update")
|
||||
async def test_sensor_deprecation_warning(hass, caplog):
|
||||
"""Test that sensor setup prints a deprecating warning for old configs.
|
||||
|
||||
The mock_config fixture does not work with caplog.
|
||||
"""
|
||||
data = MOCK_CONFIG.copy()
|
||||
data[CONF_TRAVEL_MODE] = "driving"
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=data,
|
||||
entry_id="test",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("sensor.google_travel_time").state == "27"
|
||||
wstr = (
|
||||
"Google Travel Time: travel_mode is deprecated, please "
|
||||
"add mode to the options dictionary instead!"
|
||||
)
|
||||
assert wstr in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"unit_system, expected_unit_option",
|
||||
[
|
||||
|
@ -245,7 +220,7 @@ async def test_sensor_unit_system(
|
|||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=MOCK_CONFIG,
|
||||
options={},
|
||||
options=default_options(hass),
|
||||
entry_id="test",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
|
Loading…
Add table
Reference in a new issue