core/tests/components/harmony/test_config_flow.py

227 lines
7.3 KiB
Python
Raw Normal View History

"""Test the Logitech Harmony Hub config flow."""
Generate switches for harmony activities automatically (#42331) * Adding switch code for harmony activities * Working on-off * Removing poll code for now * Async updates for current activity * Update our state based on events * Notifications we got connected or disconnected * Remove unncessary constructor arg * Initial switch tests * Additional tests for switch transitions * Test transitions for availability * Testing switch state changes * Tests passing * Final tests * Updating manifest. * Correctly mock the return value from a call to the library * Adding new subscriber classes * Update class name and location * Got the refactor working locally. * Tests passing * Tracking state changes * Remove write_to_config_file - this appears to never be read. It was added far back in the past to account for a harmony library change, but nothing ever reads that path. Removing that side effect from tests is a pain - avoid the side effect completely. * Connection changes tested * Clean up temporary code * Update .coveragerc for harmony component Specifically exclude untested files instead of the whole module * Fix linting * test sending activity change commands by id * Improving coverage * Testing channel change commands * Splitting subscriber logic into it's own class * Improve coverage and tighten up .coveragerc * Test cleanups. * re-add config file writing for harmony remote * Create fixture for the mock harmonyclient * Reduce duplication in subscription callbacks * use async_run_job to call callbacks * Adding some tests for async behaviors with subscribers. * async_call_later for delay in marking remote unavailable * Test disconnection handling in harmony remote * Early exit if activity not specified * Use connection state mixin * Lint fix after rebase * Fix isort * super init for ConnectionStateMixin * Adding @mkeesey to harmony CODEOWNERS
2021-01-04 23:21:14 +00:00
from unittest.mock import AsyncMock, MagicMock, patch
2021-01-01 21:31:56 +00:00
import aiohttp
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import ssdp
from homeassistant.components.harmony.config_flow import CannotConnect
from homeassistant.components.harmony.const import DOMAIN, PREVIOUS_ACTIVE_ACTIVITY
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
def _get_mock_harmonyapi(connect=None, close=None):
harmonyapi_mock = MagicMock()
type(harmonyapi_mock).connect = AsyncMock(return_value=connect)
type(harmonyapi_mock).close = AsyncMock(return_value=close)
return harmonyapi_mock
async def test_user_form(hass: HomeAssistant) -> None:
"""Test we get the user form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
harmonyapi = _get_mock_harmonyapi(connect=True)
with (
patch(
"homeassistant.components.harmony.util.HarmonyAPI",
return_value=harmonyapi,
),
patch(
"homeassistant.components.harmony.async_setup_entry",
return_value=True,
) as mock_setup_entry,
):
result2 = await hass.config_entries.flow.async_configure(
2020-08-27 11:56:20 +00:00
result["flow_id"],
{"host": "1.2.3.4", "name": "friend"},
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["title"] == "friend"
assert result2["data"] == {"host": "1.2.3.4", "name": "friend"}
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_ssdp(hass: HomeAssistant) -> None:
"""Test we get the form with ssdp source."""
with patch(
"homeassistant.components.harmony.config_flow.HubConnector.get_remote_id",
return_value=1234,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_SSDP},
data=ssdp.SsdpServiceInfo(
ssdp_usn="mock_usn",
ssdp_st="mock_st",
ssdp_location="http://192.168.1.12:8088/description",
upnp={
"friendlyName": "Harmony Hub",
},
),
)
assert result["type"] == "form"
assert result["step_id"] == "link"
assert result["errors"] == {}
assert result["description_placeholders"] == {
"host": "Harmony Hub",
"name": "192.168.1.12",
}
2021-03-08 23:44:55 +00:00
progress = hass.config_entries.flow.async_progress()
assert len(progress) == 1
assert progress[0]["flow_id"] == result["flow_id"]
assert progress[0]["context"]["confirm_only"] is True
harmonyapi = _get_mock_harmonyapi(connect=True)
with (
patch(
"homeassistant.components.harmony.util.HarmonyAPI",
return_value=harmonyapi,
),
patch(
"homeassistant.components.harmony.async_setup_entry",
return_value=True,
) as mock_setup_entry,
):
2020-08-27 11:56:20 +00:00
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{},
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["title"] == "Harmony Hub"
assert result2["data"] == {"host": "192.168.1.12", "name": "Harmony Hub"}
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_ssdp_fails_to_get_remote_id(hass: HomeAssistant) -> None:
"""Test we abort if we cannot get the remote id."""
with patch(
"homeassistant.components.harmony.config_flow.HubConnector.get_remote_id",
side_effect=aiohttp.ClientError,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_SSDP},
data=ssdp.SsdpServiceInfo(
ssdp_usn="mock_usn",
ssdp_st="mock_st",
ssdp_location="http://192.168.1.12:8088/description",
upnp={
"friendlyName": "Harmony Hub",
},
),
)
assert result["type"] == "abort"
assert result["reason"] == "cannot_connect"
async def test_form_ssdp_aborts_before_checking_remoteid_if_host_known(
hass: HomeAssistant,
) -> None:
"""Test we abort without connecting if the host is already known."""
config_entry = MockConfigEntry(
2020-08-27 11:56:20 +00:00
domain=DOMAIN,
data={"host": "2.2.2.2", "name": "any"},
)
config_entry.add_to_hass(hass)
2020-08-27 11:56:20 +00:00
config_entry_without_host = MockConfigEntry(
domain=DOMAIN,
data={"name": "other"},
)
config_entry_without_host.add_to_hass(hass)
harmonyapi = _get_mock_harmonyapi(connect=True)
with patch(
2020-08-27 11:56:20 +00:00
"homeassistant.components.harmony.util.HarmonyAPI",
return_value=harmonyapi,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_SSDP},
data=ssdp.SsdpServiceInfo(
ssdp_usn="mock_usn",
ssdp_st="mock_st",
ssdp_location="http://2.2.2.2:8088/description",
upnp={
"friendlyName": "Harmony Hub",
},
),
)
assert result["type"] == "abort"
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
2020-08-27 11:56:20 +00:00
"homeassistant.components.harmony.util.HarmonyAPI",
side_effect=CannotConnect,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.2.3.4",
"name": "friend",
"activity": "Watch TV",
"delay_secs": 0.2,
},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_options_flow(hass: HomeAssistant, mock_hc, mock_write_config) -> None:
"""Test config flow options."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcde12345",
data={CONF_HOST: "1.2.3.4", CONF_NAME: "Guest Room"},
options={"activity": "Watch TV", "delay_secs": 0.5},
)
Generate switches for harmony activities automatically (#42331) * Adding switch code for harmony activities * Working on-off * Removing poll code for now * Async updates for current activity * Update our state based on events * Notifications we got connected or disconnected * Remove unncessary constructor arg * Initial switch tests * Additional tests for switch transitions * Test transitions for availability * Testing switch state changes * Tests passing * Final tests * Updating manifest. * Correctly mock the return value from a call to the library * Adding new subscriber classes * Update class name and location * Got the refactor working locally. * Tests passing * Tracking state changes * Remove write_to_config_file - this appears to never be read. It was added far back in the past to account for a harmony library change, but nothing ever reads that path. Removing that side effect from tests is a pain - avoid the side effect completely. * Connection changes tested * Clean up temporary code * Update .coveragerc for harmony component Specifically exclude untested files instead of the whole module * Fix linting * test sending activity change commands by id * Improving coverage * Testing channel change commands * Splitting subscriber logic into it's own class * Improve coverage and tighten up .coveragerc * Test cleanups. * re-add config file writing for harmony remote * Create fixture for the mock harmonyclient * Reduce duplication in subscription callbacks * use async_run_job to call callbacks * Adding some tests for async behaviors with subscribers. * async_call_later for delay in marking remote unavailable * Test disconnection handling in harmony remote * Early exit if activity not specified * Use connection state mixin * Lint fix after rebase * Fix isort * super init for ConnectionStateMixin * Adding @mkeesey to harmony CODEOWNERS
2021-01-04 23:21:14 +00:00
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
assert await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"activity": PREVIOUS_ACTIVE_ACTIVITY, "delay_secs": 0.4},
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {
"activity": PREVIOUS_ACTIVE_ACTIVITY,
"delay_secs": 0.4,
}