From a2b01496776d4f9631113b3d65345d6dfd66717e Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 23 Aug 2023 11:35:04 +0200 Subject: [PATCH] Remove config name from IPMA config flow (#98576) --- homeassistant/components/ipma/config_flow.py | 73 +++++++++++--------- homeassistant/components/ipma/strings.json | 3 + tests/components/ipma/test_config_flow.py | 65 ++++++++++++++--- 3 files changed, 101 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/ipma/config_flow.py b/homeassistant/components/ipma/config_flow.py index d7b8b8cc003..cdea88bdbc0 100644 --- a/homeassistant/components/ipma/config_flow.py +++ b/homeassistant/components/ipma/config_flow.py @@ -1,53 +1,64 @@ """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 -from homeassistant import config_entries -from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME +from homeassistant.config_entries import ConfigFlow +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 -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.""" VERSION = 1 - def __init__(self): - """Init IpmaFlowHandler.""" - self._errors = {} - - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle a flow initialized by the user.""" - self._errors = {} + errors = {} if user_input is not None: - self._async_abort_entries_match( - { - CONF_LATITUDE: user_input[CONF_LATITUDE], - CONF_LONGITUDE: user_input[CONF_LONGITUDE], - } - ) + self._async_abort_entries_match(user_input) - return self.async_create_entry(title=user_input[CONF_NAME], data=user_input) + api = IPMA_API(async_get_clientsession(self.hass)) - # 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, - ) + 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) - 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( step_id="user", - data_schema=vol.Schema( + data_schema=self.add_suggested_values_to_schema( + vol.Schema( + { + vol.Required(CONF_LATITUDE): cv.latitude, + vol.Required(CONF_LONGITUDE): cv.longitude, + } + ), { - vol.Required(CONF_NAME, default=name): str, - vol.Required(CONF_LATITUDE, default=latitude): cv.latitude, - vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude, - } + CONF_LATITUDE: self.hass.config.latitude, + CONF_LONGITUDE: self.hass.config.longitude, + }, ), - errors=self._errors, + errors=errors, ) diff --git a/homeassistant/components/ipma/strings.json b/homeassistant/components/ipma/strings.json index 012550d8bd1..b9b672e77d9 100644 --- a/homeassistant/components/ipma/strings.json +++ b/homeassistant/components/ipma/strings.json @@ -12,6 +12,9 @@ } } }, + "error": { + "unknown": "[%key:common::config_flow::error::unknown%]" + }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_location%]" } diff --git a/tests/components/ipma/test_config_flow.py b/tests/components/ipma/test_config_flow.py index 18b68a5a44d..aff8af16bc3 100644 --- a/tests/components/ipma/test_config_flow.py +++ b/tests/components/ipma/test_config_flow.py @@ -1,12 +1,16 @@ """Tests for IPMA config flow.""" from unittest.mock import patch +from pyipma import IPMAException import pytest -from homeassistant import config_entries, data_entry_flow 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.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from tests.components.ipma import MockLocation @pytest.fixture(name="ipma_setup", autouse=True) @@ -19,7 +23,7 @@ def ipma_setup_fixture(request): async def test_config_flow(hass: HomeAssistant) -> None: """Test configuration form.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] == "form" @@ -29,16 +33,59 @@ async def test_config_flow(hass: HomeAssistant) -> None: CONF_LONGITUDE: 0, CONF_LATITUDE: 0, } + with patch( + "pyipma.location.Location.get", + return_value=MockLocation(), + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + test_data, + ) - 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"] == { + 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"] is data_entry_flow.FlowResultType.CREATE_ENTRY - assert result["title"] == "Home" + 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"] == { - CONF_NAME: "Home", CONF_LONGITUDE: 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( - DOMAIN, context={"source": config_entries.SOURCE_USER}, data=test_data + DOMAIN, context={"source": SOURCE_USER}, data=test_data ) await hass.async_block_till_done()