mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Remove config name from IPMA config flow (#98576)
This commit is contained in:
parent
918d822ec7
commit
a2b0149677
@ -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],
|
|
||||||
CONF_LONGITUDE: user_input[CONF_LONGITUDE],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
try:
|
||||||
return await self._show_config_form(
|
location = await Location.get(
|
||||||
name=HOME_LOCATION_NAME,
|
api,
|
||||||
latitude=self.hass.config.latitude,
|
user_input[CONF_LATITUDE],
|
||||||
longitude=self.hass.config.longitude,
|
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(
|
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_LATITUDE): cv.latitude,
|
||||||
|
vol.Required(CONF_LONGITUDE): cv.longitude,
|
||||||
|
}
|
||||||
|
),
|
||||||
{
|
{
|
||||||
vol.Required(CONF_NAME, default=name): str,
|
CONF_LATITUDE: self.hass.config.latitude,
|
||||||
vol.Required(CONF_LATITUDE, default=latitude): cv.latitude,
|
CONF_LONGITUDE: self.hass.config.longitude,
|
||||||
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude,
|
},
|
||||||
}
|
|
||||||
),
|
),
|
||||||
errors=self._errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
@ -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%]"
|
||||||
}
|
}
|
||||||
|
@ -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["flow_id"],
|
||||||
|
test_data,
|
||||||
|
)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
result["flow_id"],
|
assert result["title"] == "HomeTown"
|
||||||
test_data,
|
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["type"] == "form"
|
||||||
assert result["title"] == "Home"
|
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()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user