mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Abort Minecraft Server config flow if device is already configured (#116852)
* Abort config flow if device is already configured * Fix review findings * Rename newly added test case
This commit is contained in:
parent
673bbc1372
commit
2e52a7c4c0
@ -32,6 +32,9 @@ class MinecraftServerConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
if user_input:
|
||||
address = user_input[CONF_ADDRESS]
|
||||
|
||||
# Abort config flow if service is already configured.
|
||||
self._async_abort_entries_match({CONF_ADDRESS: address})
|
||||
|
||||
# Prepare config entry data.
|
||||
config_data = {
|
||||
CONF_NAME: user_input[CONF_NAME],
|
||||
|
@ -10,6 +10,9 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Failed to connect to server. Please check the address and try again. If a port was provided, it must be within a valid range. If you are running a Minecraft Java Edition server, ensure that it is at least version 1.7."
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ from tests.common import MockConfigEntry
|
||||
|
||||
@pytest.fixture
|
||||
def java_mock_config_entry() -> MockConfigEntry:
|
||||
"""Create YouTube entry in Home Assistant."""
|
||||
"""Create Java Edition mock config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=None,
|
||||
@ -29,7 +29,7 @@ def java_mock_config_entry() -> MockConfigEntry:
|
||||
|
||||
@pytest.fixture
|
||||
def bedrock_mock_config_entry() -> MockConfigEntry:
|
||||
"""Create YouTube entry in Home Assistant."""
|
||||
"""Create Bedrock Edition mock config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=None,
|
||||
|
@ -19,6 +19,8 @@ from .const import (
|
||||
TEST_PORT,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
USER_INPUT = {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_ADDRESS: TEST_ADDRESS,
|
||||
@ -35,6 +37,29 @@ async def test_show_config_form(hass: HomeAssistant) -> None:
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_service_already_configured(
|
||||
hass: HomeAssistant, bedrock_mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test config flow abort if service is already configured."""
|
||||
bedrock_mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
return_value=BedrockServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.async_status",
|
||||
return_value=TEST_BEDROCK_STATUS_RESPONSE,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_address_validation_failure(hass: HomeAssistant) -> None:
|
||||
"""Test error in case of a failed connection."""
|
||||
with (
|
||||
|
Loading…
x
Reference in New Issue
Block a user