Skip to content

Commit

Permalink
Use unique_id in Plex config entries (home-assistant#32489)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Mar 5, 2020
1 parent 81f99ef commit 1615a5e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
5 changes: 5 additions & 0 deletions homeassistant/components/plex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 2 additions & 6 deletions homeassistant/components/plex/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,8 @@ async def async_step_server_validate(self, server_config):

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)
Expand Down
11 changes: 6 additions & 5 deletions tests/components/plex/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -393,18 +391,21 @@ 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(
"plexapi.server.PlexServer", return_value=mock_plex_server
), 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"
Expand Down

0 comments on commit 1615a5e

Please sign in to comment.