Add unique ID to elgato config entries (#30486)

This commit is contained in:
Franck Nijhof 2020-01-04 22:45:11 +01:00 committed by Paulus Schoutsen
parent 4ea0754094
commit 3b14d9f375
3 changed files with 40 additions and 49 deletions

View file

@ -36,9 +36,8 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
return self._show_setup_form({"base": "connection_error"})
# Check if already configured
if await self._device_already_configured(info):
# This serial number is already configured
return self.async_abort(reason="already_configured")
await self.async_set_unique_id(info.serial_number)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=info.serial_number,
@ -64,9 +63,8 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="connection_error")
# Check if already configured
if await self._device_already_configured(info):
# This serial number is already configured
return self.async_abort(reason="already_configured")
await self.async_set_unique_id(info.serial_number)
self._abort_if_unique_id_configured()
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
self.context.update(
@ -97,9 +95,8 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="connection_error")
# Check if already configured
if await self._device_already_configured(info):
# This serial number is already configured
return self.async_abort(reason="already_configured")
await self.async_set_unique_id(info.serial_number)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=self.context.get(CONF_SERIAL_NUMBER),
@ -137,10 +134,3 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
session = async_get_clientsession(self.hass)
elgato = Elgato(host, port=port, session=session,)
return await elgato.info()
async def _device_already_configured(self, info: Info) -> bool:
"""Return if a Elgato Key Light is already configured."""
for entry in self._async_current_entries():
if entry.data[CONF_SERIAL_NUMBER] == info.serial_number:
return True
return False

View file

@ -33,6 +33,7 @@ async def init_integration(
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="CN11A1A00001",
data={
CONF_HOST: "example.local",
CONF_PORT: 9123,

View file

@ -16,10 +16,9 @@ from tests.test_util.aiohttp import AiohttpClientMocker
async def test_show_user_form(hass: HomeAssistant) -> None:
"""Test that the user set up form is served."""
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=None)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": SOURCE_USER},
)
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
@ -70,11 +69,10 @@ async def test_connection_error(
"http://example.local/elgato/accessory-info", exc=aiohttp.ClientError
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
context={"source": SOURCE_USER},
data={CONF_HOST: "example.local", CONF_PORT: 9123},
)
assert result["errors"] == {"base": "connection_error"}
@ -90,11 +88,10 @@ async def test_zeroconf_connection_error(
"http://example.local/elgato/accessory-info", exc=aiohttp.ClientError
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf(
user_input={"hostname": "example.local.", "port": 9123}
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={"hostname": "example.local.", "port": 9123},
)
assert result["reason"] == "connection_error"
@ -142,12 +139,12 @@ async def test_user_device_exists_abort(
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
await init_integration(hass, aioclient_mock)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user({CONF_HOST: "example.local", CONF_PORT: 9123})
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
context={"source": SOURCE_USER},
data={CONF_HOST: "example.local", CONF_PORT: 9123},
)
assert result["reason"] == "already_configured"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
@ -157,19 +154,19 @@ async def test_zeroconf_device_exists_abort(
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
await init_integration(hass, aioclient_mock)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf(
{"hostname": "example.local.", "port": 9123}
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
context={"source": SOURCE_ZEROCONF},
data={"hostname": "example.local.", "port": 9123},
)
assert result["reason"] == "already_configured"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
flow.context = {"source": SOURCE_ZEROCONF, CONF_HOST: "example.local", "port": 9123}
result = await flow.async_step_zeroconf_confirm(
{"hostname": "example.local.", "port": 9123}
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
context={"source": SOURCE_ZEROCONF, CONF_HOST: "example.local", "port": 9123},
data={"hostname": "example.local.", "port": 9123},
)
assert result["reason"] == "already_configured"
@ -186,23 +183,26 @@ async def test_full_user_flow_implementation(
headers={"Content-Type": "application/json"},
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=None)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": SOURCE_USER},
)
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await flow.async_step_user(
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
)
assert result["data"][CONF_HOST] == "example.local"
assert result["data"][CONF_PORT] == 9123
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
assert result["title"] == "CN11A1A00001"
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entries = hass.config_entries.async_entries(config_flow.DOMAIN)
assert entries[0].unique_id == "CN11A1A00001"
async def test_full_zeroconf_flow_implementation(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker