Use unique_id in Plex config entries (#32489)
This commit is contained in:
parent
81f99efda1
commit
1615a5ee81
3 changed files with 13 additions and 11 deletions
|
@ -116,6 +116,11 @@ async def async_setup_entry(hass, entry):
|
||||||
"""Set up Plex from a config entry."""
|
"""Set up Plex from a config entry."""
|
||||||
server_config = entry.data[PLEX_SERVER_CONFIG]
|
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:
|
if MP_DOMAIN not in entry.options:
|
||||||
options = dict(entry.options)
|
options = dict(entry.options)
|
||||||
options.setdefault(
|
options.setdefault(
|
||||||
|
|
|
@ -125,12 +125,8 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
server_id = plex_server.machine_identifier
|
server_id = plex_server.machine_identifier
|
||||||
|
|
||||||
for entry in self._async_current_entries():
|
await self.async_set_unique_id(server_id)
|
||||||
if entry.data[CONF_SERVER_IDENTIFIER] == server_id:
|
self._abort_if_unique_id_configured()
|
||||||
_LOGGER.debug(
|
|
||||||
"Plex server already configured: %s", entry.data[CONF_SERVER]
|
|
||||||
)
|
|
||||||
return self.async_abort(reason="already_configured")
|
|
||||||
|
|
||||||
url = plex_server.url_in_use
|
url = plex_server.url_in_use
|
||||||
token = server_config.get(CONF_TOKEN)
|
token = server_config.get(CONF_TOKEN)
|
||||||
|
|
|
@ -383,8 +383,6 @@ async def test_already_configured(hass):
|
||||||
|
|
||||||
mock_plex_server = MockPlexServer()
|
mock_plex_server = MockPlexServer()
|
||||||
|
|
||||||
flow = init_config_flow(hass)
|
|
||||||
flow.context = {"source": "import"}
|
|
||||||
MockConfigEntry(
|
MockConfigEntry(
|
||||||
domain=config_flow.DOMAIN,
|
domain=config_flow.DOMAIN,
|
||||||
data={
|
data={
|
||||||
|
@ -393,6 +391,7 @@ async def test_already_configured(hass):
|
||||||
config_flow.CONF_SERVER_IDENTIFIER
|
config_flow.CONF_SERVER_IDENTIFIER
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
unique_id=MOCK_SERVERS[0][config_flow.CONF_SERVER_IDENTIFIER],
|
||||||
).add_to_hass(hass)
|
).add_to_hass(hass)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
|
@ -400,11 +399,13 @@ async def test_already_configured(hass):
|
||||||
), asynctest.patch("plexauth.PlexAuth.initiate_auth"), asynctest.patch(
|
), asynctest.patch("plexauth.PlexAuth.initiate_auth"), asynctest.patch(
|
||||||
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
"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_TOKEN: MOCK_TOKEN,
|
||||||
CONF_URL: f"http://{MOCK_SERVERS[0][CONF_HOST]}:{MOCK_SERVERS[0][CONF_PORT]}",
|
CONF_URL: f"http://{MOCK_SERVERS[0][CONF_HOST]}:{MOCK_SERVERS[0][CONF_PORT]}",
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == "abort"
|
assert result["type"] == "abort"
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue