diff --git a/homeassistant/components/aemet/config_flow.py b/homeassistant/components/aemet/config_flow.py index fa59b23867df..567e00bb9902 100644 --- a/homeassistant/components/aemet/config_flow.py +++ b/homeassistant/components/aemet/config_flow.py @@ -7,13 +7,19 @@ from aemet_opendata.interface import AEMET, ConnectionOptions import probatio from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult -from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.const import ( + CONF_API_KEY, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, +) from homeassistant.core import callback -from homeassistant.helpers import aiohttp_client, config_validation as cv +from homeassistant.helpers import aiohttp_client from homeassistant.helpers.schema_config_entry_flow import ( SchemaFlowFormStep, SchemaOptionsFlowHandler, ) +from homeassistant.helpers.selector import LocationSelector, LocationSelectorConfig from .const import CONF_RADAR_UPDATES, CONF_STATION_UPDATES, DEFAULT_NAME, DOMAIN @@ -39,8 +45,9 @@ class AemetConfigFlow(ConfigFlow, domain=DOMAIN): errors = {} if user_input is not None: - latitude = user_input[CONF_LATITUDE] - longitude = user_input[CONF_LONGITUDE] + location = user_input[CONF_LOCATION] + latitude = location[CONF_LATITUDE] + longitude = location[CONF_LONGITUDE] await self.async_set_unique_id(f"{latitude}-{longitude}") self._abort_if_unique_id_configured() @@ -53,23 +60,36 @@ class AemetConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "invalid_api_key" if not errors: - return self.async_create_entry(title=DEFAULT_NAME, data=user_input) + return self.async_create_entry( + title=DEFAULT_NAME, + data={ + CONF_API_KEY: user_input[CONF_API_KEY], + CONF_LATITUDE: latitude, + CONF_LONGITUDE: longitude, + }, + ) schema = probatio.Schema( { probatio.Required(probatio.Secret(CONF_API_KEY)): str, - 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_LOCATION): LocationSelector( + LocationSelectorConfig(radius=False) + ), } ) return self.async_show_form( step_id="user", - data_schema=schema, + data_schema=self.add_suggested_values_to_schema( + schema, + user_input + or { + CONF_LOCATION: { + CONF_LATITUDE: self.hass.config.latitude, + CONF_LONGITUDE: self.hass.config.longitude, + } + }, + ), errors=errors, description_placeholders={ "api_key_url": "https://opendata.aemet.es/centrodedescargas/altaUsuario" diff --git a/homeassistant/components/aemet/strings.json b/homeassistant/components/aemet/strings.json index 58863cb64527..c38aa01d1287 100644 --- a/homeassistant/components/aemet/strings.json +++ b/homeassistant/components/aemet/strings.json @@ -10,8 +10,7 @@ "user": { "data": { "api_key": "[%key:common::config_flow::data::api_key%]", - "latitude": "[%key:common::config_flow::data::latitude%]", - "longitude": "[%key:common::config_flow::data::longitude%]", + "location": "[%key:common::config_flow::data::location%]", "name": "Name of the integration" }, "description": "To generate API key go to {api_key_url}" diff --git a/tests/components/aemet/test_config_flow.py b/tests/components/aemet/test_config_flow.py index db76a243eb77..d57e4bf6cbaa 100644 --- a/tests/components/aemet/test_config_flow.py +++ b/tests/components/aemet/test_config_flow.py @@ -14,7 +14,12 @@ from homeassistant.components.aemet.const import ( DOMAIN, ) from homeassistant.config_entries import SOURCE_USER, ConfigEntryState -from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.const import ( + CONF_API_KEY, + CONF_LATITUDE, + CONF_LOCATION, + CONF_LONGITUDE, +) from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -24,8 +29,10 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry") CONFIG = { CONF_API_KEY: "foo", - CONF_LATITUDE: 40.30403754, - CONF_LONGITUDE: -3.72935236, + CONF_LOCATION: { + CONF_LATITUDE: 40.30403754, + CONF_LONGITUDE: -3.72935236, + }, } @@ -56,8 +63,8 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == DEFAULT_NAME - assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE] - assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE] + assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LOCATION][CONF_LATITUDE] + assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LOCATION][CONF_LONGITUDE] assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY] assert result["result"].unique_id == "40.30403754--3.72935236"