mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Use LocationSelector in Airly config flow (#188122)
This commit is contained in:
@@ -12,9 +12,14 @@ from airly.exceptions import AirlyError
|
||||
import probatio
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_LATITUDE,
|
||||
CONF_LOCATION,
|
||||
CONF_LONGITUDE,
|
||||
)
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.selector import LocationSelector, LocationSelectorConfig
|
||||
|
||||
from .const import (
|
||||
CONF_USE_NEAREST,
|
||||
@@ -44,21 +49,22 @@ class AirlyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
use_nearest = False
|
||||
|
||||
if user_input is not None:
|
||||
location: dict[str, Any] = user_input[CONF_LOCATION]
|
||||
await self.async_set_unique_id(
|
||||
f"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
|
||||
f"{location[CONF_LATITUDE]}-{location[CONF_LONGITUDE]}"
|
||||
)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
location_valid, errors = await self.async_check_location(
|
||||
user_input[CONF_API_KEY],
|
||||
user_input[CONF_LATITUDE],
|
||||
user_input[CONF_LONGITUDE],
|
||||
location[CONF_LATITUDE],
|
||||
location[CONF_LONGITUDE],
|
||||
)
|
||||
if not location_valid and not errors:
|
||||
location_valid, errors = await self.async_check_location(
|
||||
user_input[CONF_API_KEY],
|
||||
user_input[CONF_LATITUDE],
|
||||
user_input[CONF_LONGITUDE],
|
||||
location[CONF_LATITUDE],
|
||||
location[CONF_LONGITUDE],
|
||||
use_nearest=True,
|
||||
)
|
||||
use_nearest = location_valid
|
||||
@@ -68,21 +74,32 @@ class AirlyFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_abort(reason="wrong_location")
|
||||
return self.async_create_entry(
|
||||
title=DEFAULT_NAME,
|
||||
data={**user_input, CONF_USE_NEAREST: use_nearest},
|
||||
data={
|
||||
CONF_API_KEY: user_input[CONF_API_KEY],
|
||||
CONF_LATITUDE: location[CONF_LATITUDE],
|
||||
CONF_LONGITUDE: location[CONF_LONGITUDE],
|
||||
CONF_USE_NEAREST: use_nearest,
|
||||
},
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_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,
|
||||
}
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
probatio.Schema(
|
||||
{
|
||||
probatio.Required(probatio.Secret(CONF_API_KEY)): str,
|
||||
probatio.Required(CONF_LOCATION): LocationSelector(
|
||||
LocationSelectorConfig(radius=False)
|
||||
),
|
||||
}
|
||||
),
|
||||
user_input
|
||||
or {
|
||||
CONF_LOCATION: {
|
||||
CONF_LATITUDE: self.hass.config.latitude,
|
||||
CONF_LONGITUDE: self.hass.config.longitude,
|
||||
}
|
||||
},
|
||||
),
|
||||
errors=errors,
|
||||
description_placeholders=DESCRIPTION_PLACEHOLDERS,
|
||||
|
||||
@@ -30,13 +30,11 @@
|
||||
"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%]"
|
||||
},
|
||||
"data_description": {
|
||||
"api_key": "To generate API key go to {developer_registration_url}.",
|
||||
"latitude": "The latitude of your location.",
|
||||
"longitude": "The longitude of your location."
|
||||
"location": "The location to get air quality data for."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,23 @@ from pytest_homeassistant.common import MockConfigEntry
|
||||
|
||||
from homeassistant.components.airly.const import CONF_USE_NEAREST, DEFAULT_NAME, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
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
|
||||
|
||||
CONFIG = {
|
||||
CONF_API_KEY: "foo",
|
||||
CONF_LATITUDE: 12.3,
|
||||
CONF_LONGITUDE: 45.6,
|
||||
TEST_API_KEY = "foo"
|
||||
TEST_LATITUDE = 12.3
|
||||
TEST_LONGITUDE = 45.6
|
||||
|
||||
|
||||
USER_INPUT = {
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
CONF_LOCATION: {CONF_LATITUDE: TEST_LATITUDE, CONF_LONGITUDE: TEST_LONGITUDE},
|
||||
}
|
||||
|
||||
UPDATE_API_KEY_FLOWS = [
|
||||
@@ -57,7 +66,7 @@ async def test_invalid_api_key(
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "invalid_api_key"}
|
||||
@@ -65,15 +74,15 @@ async def test_invalid_api_key(
|
||||
point_measurements.update.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == "12.3-45.6"
|
||||
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_API_KEY] == CONFIG[CONF_API_KEY]
|
||||
assert result["data"][CONF_LATITUDE] == TEST_LATITUDE
|
||||
assert result["data"][CONF_LONGITUDE] == TEST_LONGITUDE
|
||||
assert result["data"][CONF_API_KEY] == TEST_API_KEY
|
||||
assert result["data"][CONF_USE_NEAREST] is False
|
||||
|
||||
|
||||
@@ -99,7 +108,7 @@ async def test_invalid_location(
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "wrong_location"}
|
||||
@@ -109,15 +118,15 @@ async def test_invalid_location(
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == "12.3-45.6"
|
||||
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_API_KEY] == CONFIG[CONF_API_KEY]
|
||||
assert result["data"][CONF_LATITUDE] == TEST_LATITUDE
|
||||
assert result["data"][CONF_LONGITUDE] == TEST_LONGITUDE
|
||||
assert result["data"][CONF_API_KEY] == TEST_API_KEY
|
||||
assert result["data"][CONF_USE_NEAREST] is False
|
||||
|
||||
|
||||
@@ -142,7 +151,7 @@ async def test_invalid_location_for_point_and_nearest(
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
@@ -154,7 +163,15 @@ async def test_duplicate_error(
|
||||
mock_airly_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
MockConfigEntry(domain=DOMAIN, unique_id="12.3-45.6", data=CONFIG).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="12.3-45.6",
|
||||
data={
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
},
|
||||
).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
@@ -164,7 +181,7 @@ async def test_duplicate_error(
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
@@ -184,15 +201,15 @@ async def test_create_entry(
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == "12.3-45.6"
|
||||
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_API_KEY] == CONFIG[CONF_API_KEY]
|
||||
assert result["data"][CONF_LATITUDE] == TEST_LATITUDE
|
||||
assert result["data"][CONF_LONGITUDE] == TEST_LONGITUDE
|
||||
assert result["data"][CONF_API_KEY] == TEST_API_KEY
|
||||
assert result["data"][CONF_USE_NEAREST] is False
|
||||
|
||||
|
||||
@@ -214,15 +231,15 @@ async def test_create_entry_with_nearest_method(
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == "12.3-45.6"
|
||||
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_API_KEY] == CONFIG[CONF_API_KEY]
|
||||
assert result["data"][CONF_LATITUDE] == TEST_LATITUDE
|
||||
assert result["data"][CONF_LONGITUDE] == TEST_LONGITUDE
|
||||
assert result["data"][CONF_API_KEY] == TEST_API_KEY
|
||||
assert result["data"][CONF_USE_NEAREST] is True
|
||||
|
||||
|
||||
@@ -253,7 +270,7 @@ async def test_cannot_connect(
|
||||
point_measurements.update.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": error}
|
||||
@@ -263,7 +280,7 @@ async def test_cannot_connect(
|
||||
point_measurements.update.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
@@ -300,7 +317,7 @@ async def test_unknown_error(
|
||||
point_measurements.update.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": error}
|
||||
@@ -310,7 +327,7 @@ async def test_unknown_error(
|
||||
point_measurements.update.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG
|
||||
result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
@@ -342,7 +359,11 @@ async def test_update_api_key_successful(
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == abort_reason
|
||||
assert mock_config_entry.data == {**CONFIG, CONF_API_KEY: "new_api_key"}
|
||||
assert mock_config_entry.data == {
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
CONF_API_KEY: "new_api_key",
|
||||
}
|
||||
mock_airly_client.create_measurements_session_point.assert_called_once_with(
|
||||
latitude=12.3, longitude=45.6
|
||||
)
|
||||
@@ -363,7 +384,12 @@ async def test_update_api_key_with_nearest_method(
|
||||
domain=DOMAIN,
|
||||
title="Home",
|
||||
unique_id="12.3-45.6",
|
||||
data={**CONFIG, CONF_USE_NEAREST: True},
|
||||
data={
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
CONF_USE_NEAREST: True,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
@@ -379,7 +405,8 @@ async def test_update_api_key_with_nearest_method(
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == abort_reason
|
||||
assert entry.data == {
|
||||
**CONFIG,
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
CONF_API_KEY: "new_api_key",
|
||||
CONF_USE_NEAREST: True,
|
||||
}
|
||||
@@ -445,7 +472,11 @@ async def test_update_api_key_errors(
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == step_id
|
||||
assert result["errors"] == {"base": error}
|
||||
assert mock_config_entry.data == CONFIG
|
||||
assert mock_config_entry.data == {
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
}
|
||||
|
||||
point_measurements.update.side_effect = None
|
||||
|
||||
@@ -455,4 +486,8 @@ async def test_update_api_key_errors(
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == abort_reason
|
||||
assert mock_config_entry.data == {**CONFIG, CONF_API_KEY: "new_api_key"}
|
||||
assert mock_config_entry.data == {
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
CONF_API_KEY: "new_api_key",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user