Remove config name from IPMA config flow (#98576)

This commit is contained in:
Joost Lekkerkerker 2023-08-23 11:35:04 +02:00 committed by GitHub
parent 918d822ec7
commit a2b0149677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 40 deletions

View File

@ -1,53 +1,64 @@
"""Config flow to configure IPMA component.""" """Config flow to configure IPMA component."""
import logging
from typing import Any
from pyipma import IPMAException
from pyipma.api import IPMA_API
from pyipma.location import Location
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from .const import DOMAIN, HOME_LOCATION_NAME from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
class IpmaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): class IpmaFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for IPMA component.""" """Config flow for IPMA component."""
VERSION = 1 VERSION = 1
def __init__(self): async def async_step_user(
"""Init IpmaFlowHandler.""" self, user_input: dict[str, Any] | None = None
self._errors = {} ) -> FlowResult:
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user.""" """Handle a flow initialized by the user."""
self._errors = {} errors = {}
if user_input is not None: if user_input is not None:
self._async_abort_entries_match( self._async_abort_entries_match(user_input)
{
CONF_LATITUDE: user_input[CONF_LATITUDE], api = IPMA_API(async_get_clientsession(self.hass))
CONF_LONGITUDE: user_input[CONF_LONGITUDE],
} try:
location = await Location.get(
api,
user_input[CONF_LATITUDE],
user_input[CONF_LONGITUDE],
) )
except IPMAException as err:
_LOGGER.exception(err)
errors["base"] = "unknown"
else:
return self.async_create_entry(title=location.name, data=user_input)
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
# default location is set hass configuration
return await self._show_config_form(
name=HOME_LOCATION_NAME,
latitude=self.hass.config.latitude,
longitude=self.hass.config.longitude,
)
async def _show_config_form(self, name=None, latitude=None, longitude=None):
"""Show the configuration form to edit location data."""
return self.async_show_form( return self.async_show_form(
step_id="user", step_id="user",
data_schema=vol.Schema( data_schema=self.add_suggested_values_to_schema(
vol.Schema(
{ {
vol.Required(CONF_NAME, default=name): str, vol.Required(CONF_LATITUDE): cv.latitude,
vol.Required(CONF_LATITUDE, default=latitude): cv.latitude, vol.Required(CONF_LONGITUDE): cv.longitude,
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude,
} }
), ),
errors=self._errors, {
CONF_LATITUDE: self.hass.config.latitude,
CONF_LONGITUDE: self.hass.config.longitude,
},
),
errors=errors,
) )

View File

@ -12,6 +12,9 @@
} }
} }
}, },
"error": {
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": { "abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_location%]" "already_configured": "[%key:common::config_flow::abort::already_configured_location%]"
} }

View File

@ -1,12 +1,16 @@
"""Tests for IPMA config flow.""" """Tests for IPMA config flow."""
from unittest.mock import patch from unittest.mock import patch
from pyipma import IPMAException
import pytest import pytest
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.ipma.const import DOMAIN from homeassistant.components.ipma.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.components.ipma import MockLocation
@pytest.fixture(name="ipma_setup", autouse=True) @pytest.fixture(name="ipma_setup", autouse=True)
@ -19,7 +23,7 @@ def ipma_setup_fixture(request):
async def test_config_flow(hass: HomeAssistant) -> None: async def test_config_flow(hass: HomeAssistant) -> None:
"""Test configuration form.""" """Test configuration form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
assert result["type"] == "form" assert result["type"] == "form"
@ -29,16 +33,59 @@ async def test_config_flow(hass: HomeAssistant) -> None:
CONF_LONGITUDE: 0, CONF_LONGITUDE: 0,
CONF_LATITUDE: 0, CONF_LATITUDE: 0,
} }
with patch(
"pyipma.location.Location.get",
return_value=MockLocation(),
):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
test_data, test_data,
) )
assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "Home" assert result["title"] == "HomeTown"
assert result["data"] == {
CONF_LONGITUDE: 0,
CONF_LATITUDE: 0,
}
async def test_config_flow_failures(hass: HomeAssistant) -> None:
"""Test config flow with failures."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == "form"
assert result["step_id"] == "user"
test_data = {
CONF_LONGITUDE: 0,
CONF_LATITUDE: 0,
}
with patch(
"pyipma.location.Location.get",
side_effect=IPMAException(),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
test_data,
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "unknown"}
with patch(
"pyipma.location.Location.get",
return_value=MockLocation(),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
test_data,
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "HomeTown"
assert result["data"] == { assert result["data"] == {
CONF_NAME: "Home",
CONF_LONGITUDE: 0, CONF_LONGITUDE: 0,
CONF_LATITUDE: 0, CONF_LATITUDE: 0,
} }
@ -57,7 +104,7 @@ async def test_flow_entry_already_exists(hass: HomeAssistant, config_entry) -> N
} }
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=test_data DOMAIN, context={"source": SOURCE_USER}, data=test_data
) )
await hass.async_block_till_done() await hass.async_block_till_done()