mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +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."""
|
||||
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)
|
||||
|
||||
api = IPMA_API(async_get_clientsession(self.hass))
|
||||
|
||||
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(
|
||||
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, default=latitude): cv.latitude,
|
||||
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude,
|
||||
vol.Required(CONF_LATITUDE): cv.latitude,
|
||||
vol.Required(CONF_LONGITUDE): cv.longitude,
|
||||
}
|
||||
),
|
||||
errors=self._errors,
|
||||
{
|
||||
CONF_LATITUDE: self.hass.config.latitude,
|
||||
CONF_LONGITUDE: self.hass.config.longitude,
|
||||
},
|
||||
),
|
||||
errors=errors,
|
||||
)
|
||||
|
@ -12,6 +12,9 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_location%]"
|
||||
}
|
||||
|
@ -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,
|
||||
)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Home"
|
||||
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"] == "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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user