Move default options to config_flow for waze_travel_time (#80681)

Move default options to config_flow

Signed-off-by: Kevin Stillhammer <kevin.stillhammer@gmail.com>

Signed-off-by: Kevin Stillhammer <kevin.stillhammer@gmail.com>
pull/80735/head
Kevin Stillhammer 2022-10-21 17:02:41 +02:00 committed by GitHub
parent fe67703e13
commit c70614fd7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 52 deletions

View File

@ -5,8 +5,10 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_NAME, CONF_REGION
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 (
CONF_AVOID_FERRIES,
@ -20,7 +22,9 @@ from .const import (
CONF_UNITS,
CONF_VEHICLE_TYPE,
DEFAULT_NAME,
DEFAULT_OPTIONS,
DOMAIN,
IMPERIAL_UNITS,
REGIONS,
UNITS,
VEHICLE_TYPES,
@ -28,6 +32,14 @@ from .const import (
from .helpers import is_valid_config_entry
def default_options(hass: HomeAssistant) -> dict[str, str | bool]:
"""Get the default options."""
defaults = DEFAULT_OPTIONS.copy()
if hass.config.units is IMPERIAL_SYSTEM:
defaults[CONF_UNITS] = IMPERIAL_UNITS
return defaults
class WazeOptionsFlow(config_entries.OptionsFlow):
"""Handle an options flow for Waze Travel Time."""
@ -35,7 +47,7 @@ class WazeOptionsFlow(config_entries.OptionsFlow):
"""Initialize waze options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
async def async_step_init(self, user_input=None) -> FlowResult:
"""Handle the initial step."""
if user_input is not None:
return self.async_create_entry(
@ -99,7 +111,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Get the options flow for this handler."""
return WazeOptionsFlow(config_entry)
async def async_step_user(self, user_input=None):
async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle the initial step."""
errors = {}
user_input = user_input or {}
@ -115,6 +127,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),
)
# If we get here, it's because we couldn't connect

View File

@ -1,4 +1,6 @@
"""Constants for waze_travel_time."""
from __future__ import annotations
DOMAIN = "waze_travel_time"
CONF_DESTINATION = "destination"
@ -25,3 +27,12 @@ UNITS = [METRIC_UNITS, IMPERIAL_UNITS]
REGIONS = ["US", "NA", "EU", "IL", "AU"]
VEHICLE_TYPES = ["car", "taxi", "motorcycle"]
DEFAULT_OPTIONS: dict[str, str | bool] = {
CONF_REALTIME: DEFAULT_REALTIME,
CONF_VEHICLE_TYPE: DEFAULT_VEHICLE_TYPE,
CONF_UNITS: METRIC_UNITS,
CONF_AVOID_FERRIES: DEFAULT_AVOID_FERRIES,
CONF_AVOID_SUBSCRIPTION_ROADS: DEFAULT_AVOID_SUBSCRIPTION_ROADS,
CONF_AVOID_TOLL_ROADS: DEFAULT_AVOID_TOLL_ROADS,
}

View File

@ -26,7 +26,6 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.location import find_coordinates
from homeassistant.util.unit_conversion import DistanceConverter
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from .const import (
CONF_AVOID_FERRIES,
@ -39,15 +38,9 @@ from .const import (
CONF_REALTIME,
CONF_UNITS,
CONF_VEHICLE_TYPE,
DEFAULT_AVOID_FERRIES,
DEFAULT_AVOID_SUBSCRIPTION_ROADS,
DEFAULT_AVOID_TOLL_ROADS,
DEFAULT_NAME,
DEFAULT_REALTIME,
DEFAULT_VEHICLE_TYPE,
DOMAIN,
IMPERIAL_UNITS,
METRIC_UNITS,
)
_LOGGER = logging.getLogger(__name__)
@ -61,39 +54,6 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Waze travel time sensor entry."""
defaults = {
CONF_REALTIME: DEFAULT_REALTIME,
CONF_VEHICLE_TYPE: DEFAULT_VEHICLE_TYPE,
CONF_UNITS: METRIC_UNITS,
CONF_AVOID_FERRIES: DEFAULT_AVOID_FERRIES,
CONF_AVOID_SUBSCRIPTION_ROADS: DEFAULT_AVOID_SUBSCRIPTION_ROADS,
CONF_AVOID_TOLL_ROADS: DEFAULT_AVOID_TOLL_ROADS,
}
if hass.config.units is US_CUSTOMARY_SYSTEM:
defaults[CONF_UNITS] = IMPERIAL_UNITS
if not config_entry.options:
new_data = config_entry.data.copy()
options = {}
for key in (
CONF_INCL_FILTER,
CONF_EXCL_FILTER,
CONF_REALTIME,
CONF_VEHICLE_TYPE,
CONF_AVOID_TOLL_ROADS,
CONF_AVOID_SUBSCRIPTION_ROADS,
CONF_AVOID_FERRIES,
CONF_UNITS,
):
if key in new_data:
options[key] = new_data.pop(key)
elif key in defaults:
options[key] = defaults[key]
hass.config_entries.async_update_entry(
config_entry, data=new_data, options=options
)
destination = config_entry.data[CONF_DESTINATION]
origin = config_entry.data[CONF_ORIGIN]
region = config_entry.data[CONF_REGION]

View File

@ -14,9 +14,11 @@ from homeassistant.components.waze_travel_time.const import (
CONF_UNITS,
CONF_VEHICLE_TYPE,
DEFAULT_NAME,
DEFAULT_OPTIONS,
DOMAIN,
IMPERIAL_UNITS,
)
from homeassistant.const import CONF_NAME, CONF_REGION, CONF_UNIT_SYSTEM_IMPERIAL
from homeassistant.const import CONF_NAME, CONF_REGION
from .const import MOCK_CONFIG
@ -53,6 +55,7 @@ async def test_options(hass):
entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_CONFIG,
options=DEFAULT_OPTIONS,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -72,7 +75,7 @@ async def test_options(hass):
CONF_EXCL_FILTER: "exclude",
CONF_INCL_FILTER: "include",
CONF_REALTIME: False,
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: IMPERIAL_UNITS,
CONF_VEHICLE_TYPE: "taxi",
},
)
@ -85,7 +88,7 @@ async def test_options(hass):
CONF_EXCL_FILTER: "exclude",
CONF_INCL_FILTER: "include",
CONF_REALTIME: False,
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: IMPERIAL_UNITS,
CONF_VEHICLE_TYPE: "taxi",
}
@ -96,7 +99,7 @@ async def test_options(hass):
CONF_EXCL_FILTER: "exclude",
CONF_INCL_FILTER: "include",
CONF_REALTIME: False,
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: IMPERIAL_UNITS,
CONF_VEHICLE_TYPE: "taxi",
}

View File

@ -10,9 +10,10 @@ from homeassistant.components.waze_travel_time.const import (
CONF_REALTIME,
CONF_UNITS,
CONF_VEHICLE_TYPE,
DEFAULT_OPTIONS,
DOMAIN,
IMPERIAL_UNITS,
)
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL
from .const import MOCK_CONFIG
@ -51,7 +52,7 @@ def mock_update_keyerror_fixture(mock_wrc):
@pytest.mark.parametrize(
"data,options",
[(MOCK_CONFIG, {})],
[(MOCK_CONFIG, DEFAULT_OPTIONS)],
)
@pytest.mark.usefixtures("mock_update", "mock_config")
async def test_sensor(hass):
@ -84,7 +85,7 @@ async def test_sensor(hass):
(
MOCK_CONFIG,
{
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNITS: IMPERIAL_UNITS,
CONF_REALTIME: True,
CONF_VEHICLE_TYPE: "car",
CONF_AVOID_TOLL_ROADS: True,
@ -105,7 +106,9 @@ async def test_imperial(hass):
@pytest.mark.usefixtures("mock_update_wrcerror")
async def test_sensor_failed_wrcerror(hass, caplog):
"""Test that sensor update fails with log message."""
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry = MockConfigEntry(
domain=DOMAIN, data=MOCK_CONFIG, options=DEFAULT_OPTIONS, 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()
@ -117,7 +120,9 @@ async def test_sensor_failed_wrcerror(hass, caplog):
@pytest.mark.usefixtures("mock_update_keyerror")
async def test_sensor_failed_keyerror(hass, caplog):
"""Test that sensor update fails with log message."""
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry = MockConfigEntry(
domain=DOMAIN, data=MOCK_CONFIG, options=DEFAULT_OPTIONS, 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()