From 2a36adae460c6a0c4680e4d3300cdb96f77915be Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 24 Mar 2020 19:09:24 -0500 Subject: [PATCH] Abort myq homekit config when one is already setup. (#33218) We can see myq on the network to tell them to configure it, but since the device will not give up the account it is bound to and there can be multiple myq gateways on a single account, we avoid showing the device as discovered once they already have one configured as they can always add a new one via "+" --- homeassistant/components/myq/config_flow.py | 8 +++++++ homeassistant/components/myq/strings.json | 2 +- tests/components/myq/test_config_flow.py | 24 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/myq/config_flow.py b/homeassistant/components/myq/config_flow.py index baa7aad4cffff7..07d57921e35d0b 100644 --- a/homeassistant/components/myq/config_flow.py +++ b/homeassistant/components/myq/config_flow.py @@ -67,6 +67,14 @@ async def async_step_user(self, user_input=None): async def async_step_homekit(self, homekit_info): """Handle HomeKit discovery.""" + if self._async_current_entries(): + # We can see myq on the network to tell them to configure + # it, but since the device will not give up the account it is + # bound to and there can be multiple myq gateways on a single + # account, we avoid showing the device as discovered once + # they already have one configured as they can always + # add a new one via "+" + return self.async_abort(reason="already_configured") return await self.async_step_user() async def async_step_import(self, user_input): diff --git a/homeassistant/components/myq/strings.json b/homeassistant/components/myq/strings.json index c31162b28949f4..2aa0eab328e474 100644 --- a/homeassistant/components/myq/strings.json +++ b/homeassistant/components/myq/strings.json @@ -19,4 +19,4 @@ "already_configured": "MyQ is already configured" } } -} \ No newline at end of file +} diff --git a/tests/components/myq/test_config_flow.py b/tests/components/myq/test_config_flow.py index c0bae8c5225223..9fb3b34ca6318f 100644 --- a/tests/components/myq/test_config_flow.py +++ b/tests/components/myq/test_config_flow.py @@ -4,6 +4,9 @@ from homeassistant import config_entries, setup from homeassistant.components.myq.const import DOMAIN +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME + +from tests.common import MockConfigEntry async def test_form_user(hass): @@ -101,3 +104,24 @@ async def test_form_cannot_connect(hass): assert result2["type"] == "form" assert result2["errors"] == {"base": "cannot_connect"} + + +async def test_form_homekit(hass): + """Test that we abort from homekit if myq is already setup.""" + await setup.async_setup_component(hass, "persistent_notification", {}) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "homekit"} + ) + assert result["type"] == "form" + assert result["errors"] == {} + + entry = MockConfigEntry( + domain=DOMAIN, data={CONF_USERNAME: "mock", CONF_PASSWORD: "mock"} + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "homekit"} + ) + assert result["type"] == "abort"