mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Add unique ID to elgato config entries (#30486)
This commit is contained in:
parent
4ea0754094
commit
3b14d9f375
@ -36,9 +36,8 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
return self._show_setup_form({"base": "connection_error"})
|
return self._show_setup_form({"base": "connection_error"})
|
||||||
|
|
||||||
# Check if already configured
|
# Check if already configured
|
||||||
if await self._device_already_configured(info):
|
await self.async_set_unique_id(info.serial_number)
|
||||||
# This serial number is already configured
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_abort(reason="already_configured")
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=info.serial_number,
|
title=info.serial_number,
|
||||||
@ -64,9 +63,8 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
return self.async_abort(reason="connection_error")
|
return self.async_abort(reason="connection_error")
|
||||||
|
|
||||||
# Check if already configured
|
# Check if already configured
|
||||||
if await self._device_already_configured(info):
|
await self.async_set_unique_id(info.serial_number)
|
||||||
# This serial number is already configured
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_abort(reason="already_configured")
|
|
||||||
|
|
||||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||||
self.context.update(
|
self.context.update(
|
||||||
@ -97,9 +95,8 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
return self.async_abort(reason="connection_error")
|
return self.async_abort(reason="connection_error")
|
||||||
|
|
||||||
# Check if already configured
|
# Check if already configured
|
||||||
if await self._device_already_configured(info):
|
await self.async_set_unique_id(info.serial_number)
|
||||||
# This serial number is already configured
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_abort(reason="already_configured")
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=self.context.get(CONF_SERIAL_NUMBER),
|
title=self.context.get(CONF_SERIAL_NUMBER),
|
||||||
@ -137,10 +134,3 @@ class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
elgato = Elgato(host, port=port, session=session,)
|
elgato = Elgato(host, port=port, session=session,)
|
||||||
return await elgato.info()
|
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
|
|
||||||
|
@ -33,6 +33,7 @@ async def init_integration(
|
|||||||
|
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
|
unique_id="CN11A1A00001",
|
||||||
data={
|
data={
|
||||||
CONF_HOST: "example.local",
|
CONF_HOST: "example.local",
|
||||||
CONF_PORT: 9123,
|
CONF_PORT: 9123,
|
||||||
|
@ -16,10 +16,9 @@ from tests.test_util.aiohttp import AiohttpClientMocker
|
|||||||
|
|
||||||
async def test_show_user_form(hass: HomeAssistant) -> None:
|
async def test_show_user_form(hass: HomeAssistant) -> None:
|
||||||
"""Test that the user set up form is served."""
|
"""Test that the user set up form is served."""
|
||||||
flow = config_flow.ElgatoFlowHandler()
|
result = await hass.config_entries.flow.async_init(
|
||||||
flow.hass = hass
|
config_flow.DOMAIN, context={"source": SOURCE_USER},
|
||||||
flow.context = {"source": SOURCE_USER}
|
)
|
||||||
result = await flow.async_step_user(user_input=None)
|
|
||||||
|
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
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
|
"http://example.local/elgato/accessory-info", exc=aiohttp.ClientError
|
||||||
)
|
)
|
||||||
|
|
||||||
flow = config_flow.ElgatoFlowHandler()
|
result = await hass.config_entries.flow.async_init(
|
||||||
flow.hass = hass
|
config_flow.DOMAIN,
|
||||||
flow.context = {"source": SOURCE_USER}
|
context={"source": SOURCE_USER},
|
||||||
result = await flow.async_step_user(
|
data={CONF_HOST: "example.local", CONF_PORT: 9123},
|
||||||
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["errors"] == {"base": "connection_error"}
|
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
|
"http://example.local/elgato/accessory-info", exc=aiohttp.ClientError
|
||||||
)
|
)
|
||||||
|
|
||||||
flow = config_flow.ElgatoFlowHandler()
|
result = await hass.config_entries.flow.async_init(
|
||||||
flow.hass = hass
|
config_flow.DOMAIN,
|
||||||
flow.context = {"source": SOURCE_ZEROCONF}
|
context={"source": SOURCE_ZEROCONF},
|
||||||
result = await flow.async_step_zeroconf(
|
data={"hostname": "example.local.", "port": 9123},
|
||||||
user_input={"hostname": "example.local.", "port": 9123}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["reason"] == "connection_error"
|
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."""
|
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
|
||||||
await init_integration(hass, aioclient_mock)
|
await init_integration(hass, aioclient_mock)
|
||||||
|
|
||||||
flow = config_flow.ElgatoFlowHandler()
|
result = await hass.config_entries.flow.async_init(
|
||||||
flow.hass = hass
|
config_flow.DOMAIN,
|
||||||
flow.context = {"source": SOURCE_USER}
|
context={"source": SOURCE_USER},
|
||||||
result = await flow.async_step_user({CONF_HOST: "example.local", CONF_PORT: 9123})
|
data={CONF_HOST: "example.local", CONF_PORT: 9123},
|
||||||
|
)
|
||||||
|
|
||||||
assert result["reason"] == "already_configured"
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
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."""
|
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
|
||||||
await init_integration(hass, aioclient_mock)
|
await init_integration(hass, aioclient_mock)
|
||||||
|
|
||||||
flow = config_flow.ElgatoFlowHandler()
|
result = await hass.config_entries.flow.async_init(
|
||||||
flow.hass = hass
|
config_flow.DOMAIN,
|
||||||
flow.context = {"source": SOURCE_ZEROCONF}
|
context={"source": SOURCE_ZEROCONF},
|
||||||
result = await flow.async_step_zeroconf(
|
data={"hostname": "example.local.", "port": 9123},
|
||||||
{"hostname": "example.local.", "port": 9123}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
|
||||||
flow.context = {"source": SOURCE_ZEROCONF, CONF_HOST: "example.local", "port": 9123}
|
result = await hass.config_entries.flow.async_init(
|
||||||
result = await flow.async_step_zeroconf_confirm(
|
config_flow.DOMAIN,
|
||||||
{"hostname": "example.local.", "port": 9123}
|
context={"source": SOURCE_ZEROCONF, CONF_HOST: "example.local", "port": 9123},
|
||||||
|
data={"hostname": "example.local.", "port": 9123},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
@ -186,23 +183,26 @@ async def test_full_user_flow_implementation(
|
|||||||
headers={"Content-Type": "application/json"},
|
headers={"Content-Type": "application/json"},
|
||||||
)
|
)
|
||||||
|
|
||||||
flow = config_flow.ElgatoFlowHandler()
|
result = await hass.config_entries.flow.async_init(
|
||||||
flow.hass = hass
|
config_flow.DOMAIN, context={"source": SOURCE_USER},
|
||||||
flow.context = {"source": SOURCE_USER}
|
)
|
||||||
result = await flow.async_step_user(user_input=None)
|
|
||||||
|
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
result = await flow.async_step_user(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
|
result["flow_id"], user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["data"][CONF_HOST] == "example.local"
|
assert result["data"][CONF_HOST] == "example.local"
|
||||||
assert result["data"][CONF_PORT] == 9123
|
assert result["data"][CONF_PORT] == 9123
|
||||||
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||||
assert result["title"] == "CN11A1A00001"
|
assert result["title"] == "CN11A1A00001"
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
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(
|
async def test_full_zeroconf_flow_implementation(
|
||||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
|
Loading…
x
Reference in New Issue
Block a user