From cb24a06009ad07806eb4650db533b58e6f4be2fe Mon Sep 17 00:00:00 2001 From: Manu Date: Sun, 25 Oct 2026 21:57:33 +0100 Subject: [PATCH] Use `LocationSelector` and `LanguageSelector` in Environment Canada integration (#188416) --- .../components/environment_canada/__init__.py | 2 +- .../environment_canada/config_flow.py | 81 +++++++++++-------- .../environment_canada/strings.json | 10 ++- .../environment_canada/test_config_flow.py | 75 +++++++---------- 4 files changed, 86 insertions(+), 82 deletions(-) diff --git a/homeassistant/components/environment_canada/__init__.py b/homeassistant/components/environment_canada/__init__.py index afd59e501dff..b19144f2084c 100644 --- a/homeassistant/components/environment_canada/__init__.py +++ b/homeassistant/components/environment_canada/__init__.py @@ -59,7 +59,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ECConfigEntry) -> lat = config_entry.data.get(CONF_LATITUDE) lon = config_entry.data.get(CONF_LONGITUDE) station = config_entry.data.get(CONF_STATION) - lang = config_entry.data.get(CONF_LANGUAGE, "English") + lang = config_entry.data.get(CONF_LANGUAGE, "english") errors = 0 diff --git a/homeassistant/components/environment_canada/config_flow.py b/homeassistant/components/environment_canada/config_flow.py index 96375d54afc3..55d75b0e2317 100644 --- a/homeassistant/components/environment_canada/config_flow.py +++ b/homeassistant/components/environment_canada/config_flow.py @@ -15,12 +15,20 @@ from homeassistant.config_entries import ( ConfigFlowResult, OptionsFlowWithReload, ) -from homeassistant.const import CONF_LANGUAGE, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.const import ( + CONF_LANGUAGE, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, +) from homeassistant.core import callback from homeassistant.data_entry_flow import section -from homeassistant.helpers import config_validation as cv from homeassistant.helpers.selector import ( BooleanSelector, + LanguageSelector, + LanguageSelectorConfig, + LocationSelector, + LocationSelectorConfig, NumberSelector, NumberSelectorConfig, NumberSelectorMode, @@ -64,13 +72,15 @@ from .const import ( _LOGGER = logging.getLogger(__name__) +MAP_LANGUAGES = {"en": "english", "fr": "french"} + async def validate_input(data): """Validate the user input allows us to connect.""" - lat = data.get(CONF_LATITUDE) - lon = data.get(CONF_LONGITUDE) + lat = data[CONF_LOCATION][CONF_LATITUDE] + lon = data[CONF_LOCATION][CONF_LONGITUDE] station = data.get(CONF_STATION) - lang = data.get(CONF_LANGUAGE).lower() + lang = MAP_LANGUAGES[data[CONF_LANGUAGE]] if station: # When station is provided, use it and get the coordinates from ECWeather @@ -89,6 +99,7 @@ async def validate_input(data): CONF_STATION: weather_data.station_id, CONF_LATITUDE: lat, CONF_LONGITUDE: lon, + CONF_LANGUAGE: lang, } @@ -136,43 +147,45 @@ class EnvironmentCanadaConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "unknown" if not errors: - user_input[CONF_STATION] = info[CONF_STATION] - user_input[CONF_LATITUDE] = info[CONF_LATITUDE] - user_input[CONF_LONGITUDE] = info[CONF_LONGITUDE] - # The combination of station and language are # unique for all EC weather reporting await self.async_set_unique_id( - f"{user_input[CONF_STATION]}-{user_input[CONF_LANGUAGE].lower()}" + f"{info[CONF_STATION]}-{info[CONF_LANGUAGE]}" ) self._abort_if_unique_id_configured() - return self.async_create_entry(title=info[CONF_TITLE], data=user_input) + return self.async_create_entry(title=info.pop(CONF_TITLE), data=info) station_codes = await self._get_station_codes() - data_schema = probatio.Schema( - { - probatio.Optional(CONF_STATION): SelectSelector( - SelectSelectorConfig( - options=[ - SelectOptionDict( - value=station["value"], label=station["label"] - ) - for station in station_codes - ], - mode=SelectSelectorMode.DROPDOWN, - ) - ), - probatio.Optional( - CONF_LATITUDE, default=self.hass.config.latitude - ): cv.latitude, - probatio.Optional( - CONF_LONGITUDE, default=self.hass.config.longitude - ): cv.longitude, - probatio.Required(CONF_LANGUAGE, default="English"): probatio.In( - ["English", "French"] - ), - } + data_schema = self.add_suggested_values_to_schema( + probatio.Schema( + { + probatio.Optional(CONF_STATION): SelectSelector( + SelectSelectorConfig( + options=[ + SelectOptionDict( + value=station["value"], label=station["label"] + ) + for station in station_codes + ], + mode=SelectSelectorMode.DROPDOWN, + ) + ), + probatio.Required(CONF_LOCATION): LocationSelector( + LocationSelectorConfig(radius=False) + ), + probatio.Required(CONF_LANGUAGE, default="en"): LanguageSelector( + LanguageSelectorConfig(languages=["en", "fr"]) + ), + } + ), + user_input + or { + CONF_LOCATION: { + CONF_LATITUDE: self.hass.config.latitude, + CONF_LONGITUDE: self.hass.config.longitude, + } + }, ) return self.async_show_form( diff --git a/homeassistant/components/environment_canada/strings.json b/homeassistant/components/environment_canada/strings.json index 06bef9c71059..3ba17c846cef 100644 --- a/homeassistant/components/environment_canada/strings.json +++ b/homeassistant/components/environment_canada/strings.json @@ -14,11 +14,15 @@ "user": { "data": { "language": "Weather information language", - "latitude": "[%key:common::config_flow::data::latitude%]", - "longitude": "[%key:common::config_flow::data::longitude%]", + "location": "[%key:common::config_flow::data::location%]", "station": "Weather station" }, - "description": "Select a weather station from the dropdown, or specify coordinates to use the closest station. The default coordinates are from your Home Assistant installation. Weather information can be retrieved in English or French.", + "data_description": { + "language": "Weather information is available in English and French.", + "location": "Used to find the nearest weather station when no station is selected. Ignored when a station is selected. Defaults to the home location configured in Home Assistant.", + "station": "Select a specific weather station, or leave unselected to use the nearest station based on the specified coordinates." + }, + "description": "Select a weather station, or specify coordinates to use the closest station.", "title": "Environment Canada: weather location and language" } } diff --git a/tests/components/environment_canada/test_config_flow.py b/tests/components/environment_canada/test_config_flow.py index 35a7f822499b..d2893158d9a4 100644 --- a/tests/components/environment_canada/test_config_flow.py +++ b/tests/components/environment_canada/test_config_flow.py @@ -38,17 +38,34 @@ from homeassistant.components.environment_canada.const import ( SECTION_RADAR, SECTION_TIME, ) -from homeassistant.const import CONF_LANGUAGE, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.const import ( + CONF_LANGUAGE, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, +) from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from . import build_mocks, init_integration +TEST_STATION = "123" +TEST_LATITUDE = 42.42 +TEST_LONGITUDE = -42.42 + FAKE_CONFIG = { - CONF_STATION: "123", - CONF_LANGUAGE: "English", - CONF_LATITUDE: 42.42, - CONF_LONGITUDE: -42.42, + CONF_STATION: TEST_STATION, + CONF_LANGUAGE: "english", + CONF_LATITUDE: TEST_LATITUDE, + CONF_LONGITUDE: TEST_LONGITUDE, +} +USER_INPUT = { + CONF_STATION: TEST_STATION, + CONF_LANGUAGE: "en", + CONF_LOCATION: { + CONF_LATITUDE: TEST_LATITUDE, + CONF_LONGITUDE: TEST_LONGITUDE, + }, } FAKE_TITLE = "Universal title!" FAKE_STATIONS = [ @@ -97,7 +114,7 @@ async def test_create_entry(hass: HomeAssistant) -> None: DOMAIN, context={"source": config_entries.SOURCE_USER} ) result = await hass.config_entries.flow.async_configure( - flow["flow_id"], FAKE_CONFIG + flow["flow_id"], user_input=USER_INPUT ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY @@ -127,7 +144,7 @@ async def test_create_same_entry_twice(hass: HomeAssistant) -> None: DOMAIN, context={"source": config_entries.SOURCE_USER} ) result = await hass.config_entries.flow.async_configure( - flow["flow_id"], FAKE_CONFIG + flow["flow_id"], user_input=USER_INPUT ) await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT @@ -160,8 +177,7 @@ async def test_exception_handling( DOMAIN, context={"source": config_entries.SOURCE_USER} ) result = await hass.config_entries.flow.async_configure( - flow["flow_id"], - {}, + flow["flow_id"], user_input=USER_INPUT ) await hass.async_block_till_done() assert result["type"] is FlowResultType.FORM @@ -176,43 +192,12 @@ async def test_exception_handling( ), ): result = await hass.config_entries.flow.async_configure( - flow["flow_id"], FAKE_CONFIG + flow["flow_id"], user_input=USER_INPUT ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY -async def test_lat_lon_not_specified(hass: HomeAssistant) -> None: - """Test that the import step works when coordinates are not specified.""" - with ( - mocked_ec(), - mocked_stations(), - patch( - "homeassistant.components.environment_canada.async_setup_entry", - return_value=None, - ), - ): - fake_config = dict(FAKE_CONFIG) - del fake_config[CONF_LATITUDE] - del fake_config[CONF_LONGITUDE] - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input=fake_config, - ) - await hass.async_block_till_done() - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"] == FAKE_CONFIG - assert result["title"] == FAKE_TITLE - assert result["result"].unique_id == "123-english" - - async def test_coordinates_without_station(hass: HomeAssistant) -> None: """Test setup with coordinates but no station ID.""" with ( @@ -225,9 +210,11 @@ async def test_coordinates_without_station(hass: HomeAssistant) -> None: ): # Config with coordinates but no station config_no_station = { - CONF_LANGUAGE: "English", - CONF_LATITUDE: 42.42, - CONF_LONGITUDE: -42.42, + CONF_LANGUAGE: "en", + CONF_LOCATION: { + CONF_LATITUDE: 42.42, + CONF_LONGITUDE: -42.42, + }, } flow = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}