From 1615a5ee81fde04e0722da2842059b81126a0250 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 4 Mar 2020 21:42:48 -0800 Subject: [PATCH] Use unique_id in Plex config entries (#32489) --- homeassistant/components/plex/__init__.py | 5 +++++ homeassistant/components/plex/config_flow.py | 8 ++------ tests/components/plex/test_config_flow.py | 11 ++++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/plex/__init__.py b/homeassistant/components/plex/__init__.py index 101704013c11..9d74ed8cb757 100644 --- a/homeassistant/components/plex/__init__.py +++ b/homeassistant/components/plex/__init__.py @@ -116,6 +116,11 @@ async def async_setup_entry(hass, entry): """Set up Plex from a config entry.""" server_config = entry.data[PLEX_SERVER_CONFIG] + if entry.unique_id is None: + hass.config_entries.async_update_entry( + entry, unique_id=entry.data[CONF_SERVER_IDENTIFIER] + ) + if MP_DOMAIN not in entry.options: options = dict(entry.options) options.setdefault( diff --git a/homeassistant/components/plex/config_flow.py b/homeassistant/components/plex/config_flow.py index 6e4dce3e9145..d61da8609a9a 100644 --- a/homeassistant/components/plex/config_flow.py +++ b/homeassistant/components/plex/config_flow.py @@ -125,12 +125,8 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): server_id = plex_server.machine_identifier - for entry in self._async_current_entries(): - if entry.data[CONF_SERVER_IDENTIFIER] == server_id: - _LOGGER.debug( - "Plex server already configured: %s", entry.data[CONF_SERVER] - ) - return self.async_abort(reason="already_configured") + await self.async_set_unique_id(server_id) + self._abort_if_unique_id_configured() url = plex_server.url_in_use token = server_config.get(CONF_TOKEN) diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index 4e7af44c5a48..da4c95c145fc 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -383,8 +383,6 @@ async def test_already_configured(hass): mock_plex_server = MockPlexServer() - flow = init_config_flow(hass) - flow.context = {"source": "import"} MockConfigEntry( domain=config_flow.DOMAIN, data={ @@ -393,6 +391,7 @@ async def test_already_configured(hass): config_flow.CONF_SERVER_IDENTIFIER ], }, + unique_id=MOCK_SERVERS[0][config_flow.CONF_SERVER_IDENTIFIER], ).add_to_hass(hass) with patch( @@ -400,11 +399,13 @@ async def test_already_configured(hass): ), asynctest.patch("plexauth.PlexAuth.initiate_auth"), asynctest.patch( "plexauth.PlexAuth.token", return_value=MOCK_TOKEN ): - result = await flow.async_step_import( - { + result = await hass.config_entries.flow.async_init( + config_flow.DOMAIN, + context={"source": "import"}, + data={ CONF_TOKEN: MOCK_TOKEN, CONF_URL: f"http://{MOCK_SERVERS[0][CONF_HOST]}:{MOCK_SERVERS[0][CONF_PORT]}", - } + }, ) assert result["type"] == "abort" assert result["reason"] == "already_configured"