Add ability to ignore heos discovery (#34653)

* Add ability to ignore heos discovery

* Fetch player_id, update tests

* Handle failure state

* Update tests as there are two players in the mock now

* Adjust and add more tests

* Strip out player id lookup

* reverts per review

* one more revert
This commit is contained in:
J. Nick Koston 2020-05-26 10:51:50 -05:00 committed by GitHub
parent 8de863ecf1
commit 9a53240759
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 4 deletions

View File

@ -61,6 +61,10 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Initialize config entry which represents the HEOS controller."""
# For backwards compat
if entry.unique_id is None:
hass.config_entries.async_update_entry(entry, unique_id=DOMAIN)
host = entry.data[CONF_HOST]
# Setting all_progress_events=False ensures that we only receive a
# media position update upon start of playback or when media changes

View File

@ -33,12 +33,16 @@ class HeosFlowHandler(config_entries.ConfigFlow):
# Abort if other flows in progress or an entry already exists
if self._async_in_progress() or self._async_current_entries():
return self.async_abort(reason="already_setup")
await self.async_set_unique_id(DOMAIN)
# Show selection form
return self.async_show_form(step_id="user")
async def async_step_import(self, user_input=None):
"""Occurs when an entry is setup through config."""
host = user_input[CONF_HOST]
# raise_on_progress is False here in case ssdp discovers
# heos first which would block the import
await self.async_set_unique_id(DOMAIN, raise_on_progress=False)
return self.async_create_entry(title=format_title(host), data={CONF_HOST: host})
async def async_step_user(self, user_input=None):

View File

@ -6,7 +6,8 @@ from pyheos import HeosError
from homeassistant import data_entry_flow
from homeassistant.components import heos, ssdp
from homeassistant.components.heos.config_flow import HeosFlowHandler
from homeassistant.components.heos.const import DATA_DISCOVERED_HOSTS
from homeassistant.components.heos.const import DATA_DISCOVERED_HOSTS, DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_SSDP
from homeassistant.const import CONF_HOST
from tests.async_mock import patch
@ -55,6 +56,7 @@ async def test_create_entry_when_host_valid(hass, controller):
heos.DOMAIN, context={"source": "user"}, data=data
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["result"].unique_id == DOMAIN
assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == data
assert controller.connect.call_count == 1
@ -70,6 +72,7 @@ async def test_create_entry_when_friendly_name_valid(hass, controller):
heos.DOMAIN, context={"source": "user"}, data=data
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["result"].unique_id == DOMAIN
assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == {CONF_HOST: "127.0.0.1"}
assert controller.connect.call_count == 1
@ -79,28 +82,34 @@ async def test_create_entry_when_friendly_name_valid(hass, controller):
async def test_discovery_shows_create_form(hass, controller, discovery_data):
"""Test discovery shows form to confirm setup and subsequent abort."""
await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
)
await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1
flows_in_progress = hass.config_entries.flow.async_progress()
assert flows_in_progress[0]["context"]["unique_id"] == DOMAIN
assert len(flows_in_progress) == 1
assert hass.data[DATA_DISCOVERED_HOSTS] == {"Office (127.0.0.1)": "127.0.0.1"}
port = urlparse(discovery_data[ssdp.ATTR_SSDP_LOCATION]).port
discovery_data[ssdp.ATTR_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
)
await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1
flows_in_progress = hass.config_entries.flow.async_progress()
assert flows_in_progress[0]["context"]["unique_id"] == DOMAIN
assert len(flows_in_progress) == 1
assert hass.data[DATA_DISCOVERED_HOSTS] == {
"Office (127.0.0.1)": "127.0.0.1",
"Bedroom (127.0.0.2)": "127.0.0.2",
}
async def test_disovery_flow_aborts_already_setup(
async def test_discovery_flow_aborts_already_setup(
hass, controller, discovery_data, config_entry
):
"""Test discovery flow aborts when entry already setup."""
@ -110,3 +119,34 @@ async def test_disovery_flow_aborts_already_setup(
result = await flow.async_step_ssdp(discovery_data)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_setup"
async def test_discovery_sets_the_unique_id(hass, controller, discovery_data):
"""Test discovery sets the unique id."""
port = urlparse(discovery_data[ssdp.ATTR_SSDP_LOCATION]).port
discovery_data[ssdp.ATTR_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": SOURCE_SSDP}, data=discovery_data
)
await hass.async_block_till_done()
flows_in_progress = hass.config_entries.flow.async_progress()
assert flows_in_progress[0]["context"]["unique_id"] == DOMAIN
assert len(flows_in_progress) == 1
assert hass.data[DATA_DISCOVERED_HOSTS] == {"Bedroom (127.0.0.2)": "127.0.0.2"}
async def test_import_sets_the_unique_id(hass, controller):
"""Test import sets the unique id."""
with patch("homeassistant.components.heos.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
heos.DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_HOST: "127.0.0.2"},
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["result"].unique_id == DOMAIN

View File

@ -31,6 +31,7 @@ async def test_async_setup_creates_entry(hass, config):
entry = entries[0]
assert entry.title == "Controller (127.0.0.1)"
assert entry.data == {CONF_HOST: "127.0.0.1"}
assert entry.unique_id == DOMAIN
async def test_async_setup_updates_entry(hass, config_entry, config, controller):
@ -44,6 +45,7 @@ async def test_async_setup_updates_entry(hass, config_entry, config, controller)
entry = entries[0]
assert entry.title == "Controller (127.0.0.2)"
assert entry.data == {CONF_HOST: "127.0.0.2"}
assert entry.unique_id == DOMAIN
async def test_async_setup_returns_true(hass, config_entry, config):