mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix checks for duplicated config entries in IPMA (#98319)
* fix unique_id * old unique id detection * update tests * match entry not unique_id * address review
This commit is contained in:
parent
ed2f067c52
commit
abf065ed76
@ -22,14 +22,14 @@ class IpmaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
self._errors = {}
|
self._errors = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
if user_input[CONF_NAME] not in self.hass.config_entries.async_entries(
|
self._async_abort_entries_match(
|
||||||
DOMAIN
|
{
|
||||||
):
|
CONF_LATITUDE: user_input[CONF_LATITUDE],
|
||||||
return self.async_create_entry(
|
CONF_LONGITUDE: user_input[CONF_LONGITUDE],
|
||||||
title=user_input[CONF_NAME], data=user_input
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self._errors[CONF_NAME] = "name_exists"
|
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
|
||||||
|
|
||||||
# default location is set hass configuration
|
# default location is set hass configuration
|
||||||
return await self._show_config_form(
|
return await self._show_config_form(
|
||||||
|
@ -12,7 +12,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": { "name_exists": "Name already exists" }
|
"abort": {
|
||||||
|
"already_configured": "[%key:common::config_flow::abort::already_configured_location%]"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"system_health": {
|
"system_health": {
|
||||||
"info": {
|
"info": {
|
||||||
|
36
tests/components/ipma/conftest.py
Normal file
36
tests/components/ipma/conftest.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
"""Define test fixtures for IPMA."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.ipma import DOMAIN
|
||||||
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config_entry")
|
||||||
|
def config_entry_fixture(hass, config):
|
||||||
|
"""Define a config entry fixture."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data=config,
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config")
|
||||||
|
def config_fixture():
|
||||||
|
"""Define a config entry data fixture."""
|
||||||
|
return {
|
||||||
|
CONF_NAME: "Home",
|
||||||
|
CONF_LATITUDE: 0,
|
||||||
|
CONF_LONGITUDE: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="setup_config_entry")
|
||||||
|
async def setup_config_entry_fixture(hass, config_entry):
|
||||||
|
"""Define a fixture to set up ipma."""
|
||||||
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
@ -1,116 +1,65 @@
|
|||||||
"""Tests for IPMA config flow."""
|
"""Tests for IPMA config flow."""
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.ipma import config_flow
|
import pytest
|
||||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
|
||||||
|
from homeassistant import config_entries, data_entry_flow
|
||||||
|
from homeassistant.components.ipma.const import DOMAIN
|
||||||
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
|
||||||
async def test_show_config_form() -> None:
|
@pytest.fixture(name="ipma_setup", autouse=True)
|
||||||
"""Test show configuration form."""
|
def ipma_setup_fixture(request):
|
||||||
hass = Mock()
|
"""Patch ipma setup entry."""
|
||||||
flow = config_flow.IpmaFlowHandler()
|
with patch("homeassistant.components.ipma.async_setup_entry", return_value=True):
|
||||||
flow.hass = hass
|
yield
|
||||||
|
|
||||||
result = await flow._show_config_form()
|
|
||||||
|
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}
|
||||||
|
)
|
||||||
|
|
||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
|
test_data = {
|
||||||
|
CONF_LONGITUDE: 0,
|
||||||
|
CONF_LATITUDE: 0,
|
||||||
|
}
|
||||||
|
|
||||||
async def test_show_config_form_default_values() -> None:
|
result = await hass.config_entries.flow.async_configure(
|
||||||
"""Test show configuration form."""
|
result["flow_id"],
|
||||||
hass = Mock()
|
test_data,
|
||||||
flow = config_flow.IpmaFlowHandler()
|
)
|
||||||
flow.hass = hass
|
|
||||||
|
|
||||||
result = await flow._show_config_form(name="test", latitude="0", longitude="0")
|
assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||||
|
assert result["title"] == "Home"
|
||||||
assert result["type"] == "form"
|
assert result["data"] == {
|
||||||
assert result["step_id"] == "user"
|
CONF_NAME: "Home",
|
||||||
|
CONF_LONGITUDE: 0,
|
||||||
|
CONF_LATITUDE: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_with_home_location(hass: HomeAssistant) -> None:
|
async def test_flow_entry_already_exists(hass: HomeAssistant, config_entry) -> None:
|
||||||
"""Test config flow .
|
"""Test user input for config_entry that already exists.
|
||||||
|
|
||||||
Tests the flow when a default location is configured
|
Test when the form should show when user puts existing location
|
||||||
then it should return a form with default values
|
in the config gui. Then the form should show with error.
|
||||||
"""
|
"""
|
||||||
flow = config_flow.IpmaFlowHandler()
|
test_data = {
|
||||||
flow.hass = hass
|
CONF_NAME: "Home",
|
||||||
|
CONF_LONGITUDE: 0,
|
||||||
|
CONF_LATITUDE: 0,
|
||||||
|
}
|
||||||
|
|
||||||
hass.config.location_name = "Home"
|
result = await hass.config_entries.flow.async_init(
|
||||||
hass.config.latitude = 1
|
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=test_data
|
||||||
hass.config.longitude = 1
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
result = await flow.async_step_user()
|
assert result["type"] == "abort"
|
||||||
assert result["type"] == "form"
|
assert result["reason"] == "already_configured"
|
||||||
assert result["step_id"] == "user"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_show_form() -> None:
|
|
||||||
"""Test show form scenarios first time.
|
|
||||||
|
|
||||||
Test when the form should show when no configurations exists
|
|
||||||
"""
|
|
||||||
hass = Mock()
|
|
||||||
flow = config_flow.IpmaFlowHandler()
|
|
||||||
flow.hass = hass
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.ipma.config_flow.IpmaFlowHandler._show_config_form"
|
|
||||||
) as config_form:
|
|
||||||
await flow.async_step_user()
|
|
||||||
assert len(config_form.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_entry_created_from_user_input() -> None:
|
|
||||||
"""Test that create data from user input.
|
|
||||||
|
|
||||||
Test when the form should show when no configurations exists
|
|
||||||
"""
|
|
||||||
hass = Mock()
|
|
||||||
flow = config_flow.IpmaFlowHandler()
|
|
||||||
flow.hass = hass
|
|
||||||
|
|
||||||
test_data = {"name": "home", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"}
|
|
||||||
|
|
||||||
# Test that entry created when user_input name not exists
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.ipma.config_flow.IpmaFlowHandler._show_config_form"
|
|
||||||
) as config_form, patch.object(
|
|
||||||
flow.hass.config_entries,
|
|
||||||
"async_entries",
|
|
||||||
return_value=[],
|
|
||||||
) as config_entries:
|
|
||||||
result = await flow.async_step_user(user_input=test_data)
|
|
||||||
|
|
||||||
assert result["type"] == "create_entry"
|
|
||||||
assert result["data"] == test_data
|
|
||||||
assert len(config_entries.mock_calls) == 1
|
|
||||||
assert not config_form.mock_calls
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_entry_config_entry_already_exists() -> None:
|
|
||||||
"""Test that create data from user input and config_entry already exists.
|
|
||||||
|
|
||||||
Test when the form should show when user puts existing name
|
|
||||||
in the config gui. Then the form should show with error
|
|
||||||
"""
|
|
||||||
hass = Mock()
|
|
||||||
flow = config_flow.IpmaFlowHandler()
|
|
||||||
flow.hass = hass
|
|
||||||
|
|
||||||
test_data = {"name": "home", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"}
|
|
||||||
|
|
||||||
# Test that entry created when user_input name not exists
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.ipma.config_flow.IpmaFlowHandler._show_config_form"
|
|
||||||
) as config_form, patch.object(
|
|
||||||
flow.hass.config_entries, "async_entries", return_value={"home": test_data}
|
|
||||||
) as config_entries:
|
|
||||||
await flow.async_step_user(user_input=test_data)
|
|
||||||
|
|
||||||
assert len(config_form.mock_calls) == 1
|
|
||||||
assert len(config_entries.mock_calls) == 1
|
|
||||||
assert len(flow._errors) == 1
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user