mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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:
parent
8de863ecf1
commit
9a53240759
@ -61,6 +61,10 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
||||||
"""Initialize config entry which represents the HEOS controller."""
|
"""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]
|
host = entry.data[CONF_HOST]
|
||||||
# Setting all_progress_events=False ensures that we only receive a
|
# Setting all_progress_events=False ensures that we only receive a
|
||||||
# media position update upon start of playback or when media changes
|
# media position update upon start of playback or when media changes
|
||||||
|
@ -33,12 +33,16 @@ class HeosFlowHandler(config_entries.ConfigFlow):
|
|||||||
# Abort if other flows in progress or an entry already exists
|
# Abort if other flows in progress or an entry already exists
|
||||||
if self._async_in_progress() or self._async_current_entries():
|
if self._async_in_progress() or self._async_current_entries():
|
||||||
return self.async_abort(reason="already_setup")
|
return self.async_abort(reason="already_setup")
|
||||||
|
await self.async_set_unique_id(DOMAIN)
|
||||||
# Show selection form
|
# Show selection form
|
||||||
return self.async_show_form(step_id="user")
|
return self.async_show_form(step_id="user")
|
||||||
|
|
||||||
async def async_step_import(self, user_input=None):
|
async def async_step_import(self, user_input=None):
|
||||||
"""Occurs when an entry is setup through config."""
|
"""Occurs when an entry is setup through config."""
|
||||||
host = user_input[CONF_HOST]
|
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})
|
return self.async_create_entry(title=format_title(host), data={CONF_HOST: host})
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(self, user_input=None):
|
||||||
|
@ -6,7 +6,8 @@ from pyheos import HeosError
|
|||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components import heos, ssdp
|
from homeassistant.components import heos, ssdp
|
||||||
from homeassistant.components.heos.config_flow import HeosFlowHandler
|
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 homeassistant.const import CONF_HOST
|
||||||
|
|
||||||
from tests.async_mock import patch
|
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
|
heos.DOMAIN, context={"source": "user"}, data=data
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
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["title"] == "Controller (127.0.0.1)"
|
||||||
assert result["data"] == data
|
assert result["data"] == data
|
||||||
assert controller.connect.call_count == 1
|
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
|
heos.DOMAIN, context={"source": "user"}, data=data
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
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["title"] == "Controller (127.0.0.1)"
|
||||||
assert result["data"] == {CONF_HOST: "127.0.0.1"}
|
assert result["data"] == {CONF_HOST: "127.0.0.1"}
|
||||||
assert controller.connect.call_count == 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):
|
async def test_discovery_shows_create_form(hass, controller, discovery_data):
|
||||||
"""Test discovery shows form to confirm setup and subsequent abort."""
|
"""Test discovery shows form to confirm setup and subsequent abort."""
|
||||||
|
|
||||||
await hass.config_entries.flow.async_init(
|
await hass.config_entries.flow.async_init(
|
||||||
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
|
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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"}
|
assert hass.data[DATA_DISCOVERED_HOSTS] == {"Office (127.0.0.1)": "127.0.0.1"}
|
||||||
|
|
||||||
port = urlparse(discovery_data[ssdp.ATTR_SSDP_LOCATION]).port
|
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_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
|
||||||
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
|
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
|
||||||
|
|
||||||
await hass.config_entries.flow.async_init(
|
await hass.config_entries.flow.async_init(
|
||||||
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
|
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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] == {
|
assert hass.data[DATA_DISCOVERED_HOSTS] == {
|
||||||
"Office (127.0.0.1)": "127.0.0.1",
|
"Office (127.0.0.1)": "127.0.0.1",
|
||||||
"Bedroom (127.0.0.2)": "127.0.0.2",
|
"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
|
hass, controller, discovery_data, config_entry
|
||||||
):
|
):
|
||||||
"""Test discovery flow aborts when entry already setup."""
|
"""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)
|
result = await flow.async_step_ssdp(discovery_data)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "already_setup"
|
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
|
||||||
|
@ -31,6 +31,7 @@ async def test_async_setup_creates_entry(hass, config):
|
|||||||
entry = entries[0]
|
entry = entries[0]
|
||||||
assert entry.title == "Controller (127.0.0.1)"
|
assert entry.title == "Controller (127.0.0.1)"
|
||||||
assert entry.data == {CONF_HOST: "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):
|
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]
|
entry = entries[0]
|
||||||
assert entry.title == "Controller (127.0.0.2)"
|
assert entry.title == "Controller (127.0.0.2)"
|
||||||
assert entry.data == {CONF_HOST: "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):
|
async def test_async_setup_returns_true(hass, config_entry, config):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user