mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Use LocationSelector in AEMET integration (#188114)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot Autofix powered by AI
parent
5550c7c25b
commit
cd38033858
@@ -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"
|
||||
|
||||
@@ -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}"
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user