From 109819e9cd1298fc31e80213a2b1846ce7ebf02b Mon Sep 17 00:00:00 2001 From: Maikel Punie Date: Mon, 23 Oct 2023 10:58:11 +0200 Subject: [PATCH] Only allow a single duotecno config entry (#102478) --- .../components/duotecno/config_flow.py | 3 +++ .../components/duotecno/strings.json | 3 ++- tests/components/duotecno/test_config_flow.py | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/duotecno/config_flow.py b/homeassistant/components/duotecno/config_flow.py index 37087d4ea1ad..6f08b0258353 100644 --- a/homeassistant/components/duotecno/config_flow.py +++ b/homeassistant/components/duotecno/config_flow.py @@ -34,6 +34,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle the initial step.""" + if self._async_current_entries(): + return self.async_abort(reason="single_instance_allowed") + errors: dict[str, str] = {} if user_input is not None: try: diff --git a/homeassistant/components/duotecno/strings.json b/homeassistant/components/duotecno/strings.json index a00647993a8b..93a545d31dc1 100644 --- a/homeassistant/components/duotecno/strings.json +++ b/homeassistant/components/duotecno/strings.json @@ -12,7 +12,8 @@ "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", - "unknown": "[%key:common::config_flow::error::unknown%]" + "unknown": "[%key:common::config_flow::error::unknown%]", + "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]" } }, "entity": { diff --git a/tests/components/duotecno/test_config_flow.py b/tests/components/duotecno/test_config_flow.py index a2dc265ae6eb..a02fea8008cb 100644 --- a/tests/components/duotecno/test_config_flow.py +++ b/tests/components/duotecno/test_config_flow.py @@ -9,6 +9,8 @@ from homeassistant.components.duotecno.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from tests.common import MockConfigEntry + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -87,3 +89,20 @@ async def test_invalid(hass: HomeAssistant, test_side_effect, test_error): "port": 1234, "password": "test-password2", } + + +async def test_already_setup(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: + """Test duoteco flow - already setup.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id="duotecno_1234", + data={}, + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == "single_instance_allowed"