Convert nut to a multi step config flow (#33803)

* Convert nut to a multi step config flow

* Users can now choose the ups they want in
step 2 (or skipped if only one)

* Users can now select the resources they want
to monitor based on what is actually available
on the device

* CONF_NAME has been removed as we now get the
name from NUT

* Device classes have been added which allows
the battery charge state to be seen in the
devices UI

* Remove update_interval as its for a followup PR

* explict

* reduce

* fix bug, add tests for options flow

* Test for dupe import

* Test for dupe import

* Call step directly

* Update homeassistant/components/nut/config_flow.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
J. Nick KostonandMartin Hjelmare authored and GitHub committed 2020-04-08 11:45:45 -05:00
1 parent db9b6aba31
commit 7383e81609
7 files changed
+482 -160

No files matched your search

+144 -16
View File
@@ -1,18 +1,28 @@
"""Test the Network UPS Tools (NUT) config flow."""
from asynctest import MagicMock, patch
from homeassistant import config_entries, setup
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.nut.const import DOMAIN
from homeassistant.const import CONF_RESOURCES
from tests.common import MockConfigEntry
VALID_CONFIG = {
"host": "localhost",
"port": 123,
"name": "name",
"resources": ["battery.charge"],
}
def _get_mock_pynutclient(list_vars=None):
def _get_mock_pynutclient(list_vars=None, list_ups=None):
pynutclient = MagicMock()
type(pynutclient).list_ups = MagicMock(return_value=["ups1"])
type(pynutclient).list_ups = MagicMock(return_value=list_ups)
type(pynutclient).list_vars = MagicMock(return_value=list_vars)
return pynutclient
async def test_form(hass):
async def test_form_user_one_ups(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
@@ -21,7 +31,25 @@ async def test_form(hass):
assert result["type"] == "form"
assert result["errors"] == {}
mock_pynut = _get_mock_pynutclient(list_vars={"battery.voltage": "voltage"})
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "voltage"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
"port": 2222,
},
)
assert result2["step_id"] == "resources"
assert result2["type"] == "form"
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
@@ -30,6 +58,40 @@ async def test_form(hass):
) as mock_setup, patch(
"homeassistant.components.nut.async_setup_entry", return_value=True,
) as mock_setup_entry:
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"resources": ["battery.voltage"]},
)
assert result3["type"] == "create_entry"
assert result3["title"] == "1.1.1.1:2222"
assert result3["data"] == {
"host": "1.1.1.1",
"password": "test-password",
"port": 2222,
"resources": ["battery.voltage"],
"username": "test-username",
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_user_multiple_ups(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "voltage"}, list_ups=["ups1", "ups2"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
@@ -37,20 +99,41 @@ async def test_form(hass):
"username": "test-username",
"password": "test-password",
"port": 2222,
"alias": "ups1",
"resources": ["battery.charge"],
},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "ups1@1.1.1.1:2222"
assert result2["data"] == {
"alias": "ups1",
assert result2["step_id"] == "ups"
assert result2["type"] == "form"
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"alias": "ups2"},
)
assert result3["step_id"] == "resources"
assert result3["type"] == "form"
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
), patch(
"homeassistant.components.nut.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.nut.async_setup_entry", return_value=True,
) as mock_setup_entry:
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], {"resources": ["battery.voltage"]},
)
assert result4["type"] == "create_entry"
assert result4["title"] == "ups2@1.1.1.1:2222"
assert result4["data"] == {
"host": "1.1.1.1",
"name": "NUT UPS",
"password": "test-password",
"alias": "ups2",
"port": 2222,
"resources": ["battery.charge"],
"resources": ["battery.voltage"],
"username": "test-username",
}
await hass.async_block_till_done()
@@ -62,7 +145,9 @@ async def test_form_import(hass):
"""Test we get the form with import source."""
await setup.async_setup_component(hass, "persistent_notification", {})
mock_pynut = _get_mock_pynutclient(list_vars={"battery.voltage": "serial"})
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "serial"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
@@ -95,6 +180,20 @@ async def test_form_import(hass):
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_import_dupe(hass):
"""Test we get abort on duplicate import."""
await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "import"}, data=VALID_CONFIG
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
@@ -113,10 +212,39 @@ async def test_form_cannot_connect(hass):
"username": "test-username",
"password": "test-password",
"port": 2222,
"alias": "ups1",
"resources": ["battery.charge"],
},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_options_flow(hass):
"""Test config flow options."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcde12345",
data=VALID_CONFIG,
options={CONF_RESOURCES: ["battery.charge"]},
)
config_entry.add_to_hass(hass)
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "voltage"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient", return_value=mock_pynut,
), patch("homeassistant.components.nut.async_setup_entry", return_value=True):
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_RESOURCES: ["battery.voltage"]}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {CONF_RESOURCES: ["battery.voltage"]}