diff --git a/homeassistant/components/waze_travel_time/config_flow.py b/homeassistant/components/waze_travel_time/config_flow.py index d040f595fd6..45aeada2a7a 100644 --- a/homeassistant/components/waze_travel_time/config_flow.py +++ b/homeassistant/components/waze_travel_time/config_flow.py @@ -1,14 +1,11 @@ """Config flow for Waze Travel Time integration.""" from __future__ import annotations -import logging -from typing import Any - import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_NAME, CONF_REGION -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from .const import ( @@ -22,12 +19,7 @@ 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, REGIONS, UNITS, @@ -35,52 +27,6 @@ from .const import ( ) from .helpers import is_valid_config_entry -_LOGGER = logging.getLogger(__name__) - - -def is_dupe_import( - hass: HomeAssistant, entry: config_entries.ConfigEntry, user_input: dict[str, Any] -) -> bool: - """Return whether imported config already exists.""" - entry_data = {**entry.data, **entry.options} - defaults = { - CONF_REALTIME: DEFAULT_REALTIME, - CONF_VEHICLE_TYPE: DEFAULT_VEHICLE_TYPE, - CONF_UNITS: hass.config.units.name, - CONF_AVOID_FERRIES: DEFAULT_AVOID_FERRIES, - CONF_AVOID_SUBSCRIPTION_ROADS: DEFAULT_AVOID_SUBSCRIPTION_ROADS, - CONF_AVOID_TOLL_ROADS: DEFAULT_AVOID_TOLL_ROADS, - } - - for key in ( - CONF_ORIGIN, - CONF_DESTINATION, - CONF_REGION, - CONF_INCL_FILTER, - CONF_EXCL_FILTER, - CONF_REALTIME, - CONF_VEHICLE_TYPE, - CONF_UNITS, - CONF_AVOID_FERRIES, - CONF_AVOID_SUBSCRIPTION_ROADS, - CONF_AVOID_TOLL_ROADS, - ): - # If the key is present the check is simple - if key in user_input and user_input[key] != entry_data[key]: - return False - - # If the key is not present, then we have to check if the key has a default and - # if the default is in the options. If it doesn't have a default, we have to check - # if the key is in the options - if key not in user_input: - if key in defaults and defaults[key] != entry_data[key]: - return False - - if key not in defaults and key in entry_data: - return False - - return True - class WazeOptionsFlow(config_entries.OptionsFlow): """Handle an options flow for Waze Travel Time.""" @@ -159,23 +105,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): user_input = user_input or {} if user_input: - # We need to prevent duplicate imports - if self.source == config_entries.SOURCE_IMPORT and any( - is_dupe_import(self.hass, entry, user_input) - for entry in self.hass.config_entries.async_entries(DOMAIN) - if entry.source == config_entries.SOURCE_IMPORT - ): - return self.async_abort(reason="already_configured") - - if ( - self.source == config_entries.SOURCE_IMPORT - or await self.hass.async_add_executor_job( - is_valid_config_entry, - self.hass, - user_input[CONF_ORIGIN], - user_input[CONF_DESTINATION], - user_input[CONF_REGION], - ) + if await self.hass.async_add_executor_job( + is_valid_config_entry, + self.hass, + user_input[CONF_ORIGIN], + user_input[CONF_DESTINATION], + user_input[CONF_REGION], ): return self.async_create_entry( title=user_input.get(CONF_NAME, DEFAULT_NAME), diff --git a/homeassistant/components/waze_travel_time/sensor.py b/homeassistant/components/waze_travel_time/sensor.py index 3c6dba586d1..c983be49765 100644 --- a/homeassistant/components/waze_travel_time/sensor.py +++ b/homeassistant/components/waze_travel_time/sensor.py @@ -6,27 +6,22 @@ import logging import re from WazeRouteCalculator import WazeRouteCalculator, WRCError -import voluptuous as vol -from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry +from homeassistant.components.sensor import SensorEntity +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_ATTRIBUTION, - CONF_ENTITY_NAMESPACE, CONF_NAME, CONF_REGION, - CONF_SCAN_INTERVAL, CONF_UNIT_SYSTEM_IMPERIAL, EVENT_HOMEASSISTANT_STARTED, TIME_MINUTES, ) -from homeassistant.core import Config, CoreState, HomeAssistant -import homeassistant.helpers.config_validation as cv +from homeassistant.core import CoreState, HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.location import find_coordinates -from homeassistant.helpers.typing import DiscoveryInfoType from .const import ( CONF_AVOID_FERRIES, @@ -47,65 +42,12 @@ from .const import ( DEFAULT_VEHICLE_TYPE, DOMAIN, ENTITY_ID_PATTERN, - REGIONS, - UNITS, - VEHICLE_TYPES, ) _LOGGER = logging.getLogger(__name__) SCAN_INTERVAL = timedelta(minutes=5) -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( - { - vol.Required(CONF_ORIGIN): cv.string, - vol.Required(CONF_DESTINATION): cv.string, - vol.Required(CONF_REGION): vol.In(REGIONS), - vol.Optional(CONF_NAME): cv.string, - vol.Optional(CONF_INCL_FILTER): cv.string, - vol.Optional(CONF_EXCL_FILTER): cv.string, - vol.Optional(CONF_REALTIME, default=DEFAULT_REALTIME): cv.boolean, - vol.Optional(CONF_VEHICLE_TYPE, default=DEFAULT_VEHICLE_TYPE): vol.In( - VEHICLE_TYPES - ), - vol.Optional(CONF_UNITS): vol.In(UNITS), - vol.Optional( - CONF_AVOID_TOLL_ROADS, default=DEFAULT_AVOID_TOLL_ROADS - ): cv.boolean, - vol.Optional( - CONF_AVOID_SUBSCRIPTION_ROADS, default=DEFAULT_AVOID_SUBSCRIPTION_ROADS - ): cv.boolean, - vol.Optional(CONF_AVOID_FERRIES, default=DEFAULT_AVOID_FERRIES): cv.boolean, - # Remove options to exclude from import - vol.Remove(CONF_ENTITY_NAMESPACE): cv.string, - vol.Remove(CONF_SCAN_INTERVAL): cv.time_period, - }, - extra=vol.REMOVE_EXTRA, -) - - -async def async_setup_platform( - hass: HomeAssistant, - config: Config, - async_add_entities: AddEntitiesCallback, - discovery_info: DiscoveryInfoType | None = None, -) -> None: - """Set up the Waze travel time sensor platform.""" - - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data=config, - ) - ) - - _LOGGER.warning( - "Your Waze configuration has been imported into the UI; " - "please remove it from configuration.yaml as support for it " - "will be removed in a future release" - ) - async def async_setup_entry( hass: HomeAssistant, diff --git a/tests/components/waze_travel_time/test_config_flow.py b/tests/components/waze_travel_time/test_config_flow.py index f0f8a0f3bde..015850ba1b8 100644 --- a/tests/components/waze_travel_time/test_config_flow.py +++ b/tests/components/waze_travel_time/test_config_flow.py @@ -168,100 +168,6 @@ async def _setup_dupe_import(hass, mock_update): await hass.async_block_till_done() -async def test_dupe_import(hass, mock_update): - """Test duplicate import.""" - await _setup_dupe_import(hass, mock_update) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - CONF_ORIGIN: "location1", - CONF_DESTINATION: "location2", - CONF_REGION: "US", - CONF_AVOID_FERRIES: True, - CONF_AVOID_SUBSCRIPTION_ROADS: True, - CONF_AVOID_TOLL_ROADS: True, - CONF_EXCL_FILTER: "exclude", - CONF_INCL_FILTER: "include", - CONF_REALTIME: False, - CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL, - CONF_VEHICLE_TYPE: "taxi", - }, - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT - assert result["reason"] == "already_configured" - - -async def test_dupe_import_false_check_different_options_value(hass, mock_update): - """Test false duplicate import check when options value differs.""" - await _setup_dupe_import(hass, mock_update) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - CONF_ORIGIN: "location1", - CONF_DESTINATION: "location2", - CONF_REGION: "US", - CONF_AVOID_FERRIES: True, - CONF_AVOID_SUBSCRIPTION_ROADS: True, - CONF_AVOID_TOLL_ROADS: True, - CONF_EXCL_FILTER: "exclude", - CONF_INCL_FILTER: "include", - CONF_REALTIME: False, - CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL, - CONF_VEHICLE_TYPE: "car", - }, - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - - -async def test_dupe_import_false_check_default_option(hass, mock_update): - """Test false duplicate import check when option with a default is missing.""" - await _setup_dupe_import(hass, mock_update) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - CONF_ORIGIN: "location1", - CONF_DESTINATION: "location2", - CONF_REGION: "US", - CONF_AVOID_FERRIES: True, - CONF_AVOID_SUBSCRIPTION_ROADS: True, - CONF_AVOID_TOLL_ROADS: True, - CONF_EXCL_FILTER: "exclude", - CONF_INCL_FILTER: "include", - CONF_REALTIME: False, - CONF_VEHICLE_TYPE: "taxi", - }, - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - - -async def test_dupe_import_false_check_no_default_option(hass, mock_update): - """Test false duplicate import check option when option with no default is miissing.""" - await _setup_dupe_import(hass, mock_update) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - CONF_ORIGIN: "location1", - CONF_DESTINATION: "location2", - CONF_REGION: "US", - CONF_AVOID_FERRIES: True, - CONF_AVOID_SUBSCRIPTION_ROADS: True, - CONF_AVOID_TOLL_ROADS: True, - CONF_EXCL_FILTER: "exclude", - CONF_REALTIME: False, - CONF_VEHICLE_TYPE: "taxi", - }, - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - - async def test_dupe(hass, validate_config_entry, bypass_setup): """Test setting up the same entry data twice is OK.""" result = await hass.config_entries.flow.async_init(