From d4d233536f46b5d37df1e940c2b2a037a8c78a85 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Thu, 11 Jun 2020 17:45:00 -0500 Subject: [PATCH] Fix missing options in Plex config entry (#36683) --- homeassistant/components/plex/__init__.py | 6 +++ tests/components/plex/test_config_flow.py | 50 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/homeassistant/components/plex/__init__.py b/homeassistant/components/plex/__init__.py index e460115ef0b..89a3570dd10 100644 --- a/homeassistant/components/plex/__init__.py +++ b/homeassistant/components/plex/__init__.py @@ -9,6 +9,7 @@ from plexwebsocket import PlexWebsocket import requests.exceptions import voluptuous as vol +from homeassistant.components.media_player import DOMAIN as MP_DOMAIN from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_TYPE, @@ -65,6 +66,11 @@ async def async_setup_entry(hass, entry): entry, unique_id=entry.data[CONF_SERVER_IDENTIFIER] ) + if MP_DOMAIN not in entry.options: + options = dict(entry.options) + options.setdefault(MP_DOMAIN, {}) + hass.config_entries.async_update_entry(entry, options=options) + plex_server = PlexServer( hass, server_config, entry.data[CONF_SERVER_IDENTIFIER], entry.options ) diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index c51c0670525..4ffea576514 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -405,6 +405,56 @@ async def test_option_flow(hass): } +async def test_missing_option_flow(hass): + """Test config options flow selection when no options stored.""" + mock_plex_server = MockPlexServer() + + entry = MockConfigEntry( + domain=DOMAIN, + data=DEFAULT_DATA, + options=None, + unique_id=DEFAULT_DATA["server_id"], + ) + + with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch( + "homeassistant.components.plex.PlexWebsocket.listen" + ) as mock_listen: + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert mock_listen.called + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + assert entry.state == ENTRY_STATE_LOADED + + result = await hass.config_entries.options.async_init( + entry.entry_id, context={"source": "test"}, data=None + ) + assert result["type"] == "form" + assert result["step_id"] == "plex_mp_settings" + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={ + CONF_USE_EPISODE_ART: True, + CONF_IGNORE_NEW_SHARED_USERS: True, + CONF_MONITORED_USERS: list(mock_plex_server.accounts), + }, + ) + assert result["type"] == "create_entry" + assert result["data"] == { + MP_DOMAIN: { + CONF_USE_EPISODE_ART: True, + CONF_IGNORE_NEW_SHARED_USERS: True, + CONF_MONITORED_USERS: { + user: {"enabled": True} for user in mock_plex_server.accounts + }, + CONF_IGNORE_PLEX_WEB_CLIENTS: False, + } + } + + async def test_option_flow_new_users_available(hass, caplog): """Test config options multiselect defaults when new Plex users are seen."""