mirror of
https://github.com/home-assistant/core.git
synced 2026-09-24 23:41:48 -05:00
Add reconfiguration flow to NUT (#142127)
* Add reconfiguration flow * Check host/port/alias without comparing strings * Replace repeat strings with references
This commit is contained in:
@@ -6,6 +6,7 @@ from unittest.mock import patch
|
||||
from aionut import NUTError, NUTLoginError
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.nut.config_flow import PASSWORD_NOT_CHANGED
|
||||
from homeassistant.components.nut.const import DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_ALIAS,
|
||||
@@ -83,8 +84,8 @@ async def test_form_zeroconf(hass: HomeAssistant) -> None:
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_user_one_ups(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
async def test_form_user_one_alias(hass: HomeAssistant) -> None:
|
||||
"""Test we can configure a device with one alias."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
@@ -128,8 +129,8 @@ async def test_form_user_one_ups(hass: HomeAssistant) -> None:
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_user_multiple_ups(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
async def test_form_user_multiple_aliases(hass: HomeAssistant) -> None:
|
||||
"""Test we can configure device with multiple aliases."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
@@ -194,7 +195,7 @@ async def test_form_user_multiple_ups(hass: HomeAssistant) -> None:
|
||||
assert len(mock_setup_entry.mock_calls) == 2
|
||||
|
||||
|
||||
async def test_form_user_one_ups_with_ignored_entry(hass: HomeAssistant) -> None:
|
||||
async def test_form_user_one_alias_with_ignored_entry(hass: HomeAssistant) -> None:
|
||||
"""Test we can setup a new one when there is an ignored one."""
|
||||
ignored_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data={}, source=config_entries.SOURCE_IGNORE
|
||||
@@ -244,8 +245,8 @@ async def test_form_user_one_ups_with_ignored_entry(hass: HomeAssistant) -> None
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_no_upses_found(hass: HomeAssistant) -> None:
|
||||
"""Test we abort when the NUT server has not UPSes."""
|
||||
async def test_form_no_aliases_found(hass: HomeAssistant) -> None:
|
||||
"""Test we abort when the NUT server has no aliases."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
@@ -561,8 +562,8 @@ async def test_abort_duplicate_unique_ids(hass: HomeAssistant) -> None:
|
||||
assert result2["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_abort_multiple_ups_duplicate_unique_ids(hass: HomeAssistant) -> None:
|
||||
"""Test we abort on multiple devices if unique_id is already setup."""
|
||||
async def test_abort_multiple_aliases_duplicate_unique_ids(hass: HomeAssistant) -> None:
|
||||
"""Test we abort on multiple aliases if unique_id is already setup."""
|
||||
|
||||
list_vars = {
|
||||
"device.mfr": "Some manufacturer",
|
||||
@@ -670,3 +671,762 @@ async def test_abort_if_already_setup_alias(hass: HomeAssistant) -> None:
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_reconfigure_one_alias_successful(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure one alias successful."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "2.2.2.2",
|
||||
CONF_PORT: 456,
|
||||
CONF_USERNAME: "test-new-username",
|
||||
CONF_PASSWORD: "test-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "reconfigure_successful"
|
||||
|
||||
assert entry.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry.data[CONF_PORT] == 456
|
||||
assert entry.data[CONF_USERNAME] == "test-new-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-new-password"
|
||||
|
||||
|
||||
async def test_reconfigure_one_alias_nochange(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure one alias when there is no change."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: entry.data[CONF_HOST],
|
||||
CONF_PORT: int(entry.data[CONF_PORT]),
|
||||
CONF_USERNAME: entry.data[CONF_USERNAME],
|
||||
CONF_PASSWORD: entry.data[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "reconfigure_successful"
|
||||
|
||||
assert entry.data[CONF_HOST] == "1.1.1.1"
|
||||
assert entry.data[CONF_PORT] == 123
|
||||
assert entry.data[CONF_USERNAME] == "test-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-password"
|
||||
|
||||
|
||||
async def test_reconfigure_one_alias_password_nochange(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure one alias when there is no password change."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "2.2.2.2",
|
||||
CONF_PORT: 456,
|
||||
CONF_USERNAME: "test-new-username",
|
||||
CONF_PASSWORD: PASSWORD_NOT_CHANGED,
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "reconfigure_successful"
|
||||
|
||||
assert entry.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry.data[CONF_PORT] == 456
|
||||
assert entry.data[CONF_USERNAME] == "test-new-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-password"
|
||||
|
||||
|
||||
async def test_reconfigure_one_alias_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure when config changed to an existing host/port/alias."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
entry2 = await async_init_integration(
|
||||
hass,
|
||||
host="2.2.2.2",
|
||||
port=456,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry2.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: entry.data[CONF_HOST],
|
||||
CONF_PORT: int(entry.data[CONF_PORT]),
|
||||
CONF_USERNAME: entry.data[CONF_USERNAME],
|
||||
CONF_PASSWORD: entry.data[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "already_configured"
|
||||
|
||||
assert entry.data[CONF_HOST] == "1.1.1.1"
|
||||
assert entry.data[CONF_PORT] == 123
|
||||
assert entry.data[CONF_USERNAME] == "test-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-password"
|
||||
|
||||
assert entry2.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry2.data[CONF_PORT] == 456
|
||||
assert entry2.data[CONF_USERNAME] == "test-username"
|
||||
assert entry2.data[CONF_PASSWORD] == "test-password"
|
||||
|
||||
|
||||
async def test_reconfigure_one_alias_unique_id_change(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure when the unique ID is changed."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={
|
||||
"device.mfr": "Some manufacturer",
|
||||
"device.model": "Some model",
|
||||
"device.serial": "0000-1",
|
||||
},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={
|
||||
"device.mfr": "Another manufacturer",
|
||||
"device.model": "Another model",
|
||||
"device.serial": "0000-2",
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: entry.data[CONF_HOST],
|
||||
CONF_PORT: entry.data[CONF_PORT],
|
||||
CONF_USERNAME: entry.data[CONF_USERNAME],
|
||||
CONF_PASSWORD: entry.data[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "unique_id_mismatch"
|
||||
|
||||
|
||||
async def test_reconfigure_one_alias_duplicate_unique_ids(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure that results in a duplicate unique ID."""
|
||||
|
||||
list_vars = {
|
||||
"device.mfr": "Some manufacturer",
|
||||
"device.model": "Some model",
|
||||
"device.serial": "0000-1",
|
||||
}
|
||||
|
||||
await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars=list_vars,
|
||||
)
|
||||
|
||||
entry2 = await async_init_integration(
|
||||
hass,
|
||||
host="2.2.2.2",
|
||||
port=456,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups2": "UPS 2"},
|
||||
list_vars={
|
||||
"device.mfr": "Another manufacturer",
|
||||
"device.model": "Another model",
|
||||
"device.serial": "0000-2",
|
||||
},
|
||||
)
|
||||
|
||||
result = await entry2.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups2": "UPS 2"},
|
||||
list_vars=list_vars,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "3.3.3.3",
|
||||
CONF_PORT: 789,
|
||||
CONF_USERNAME: "test-new-username",
|
||||
CONF_PASSWORD: "test-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "unique_id_mismatch"
|
||||
|
||||
|
||||
async def test_reconfigure_multiple_aliases_successful(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure with multiple aliases is successful."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={
|
||||
"ups1": "UPS 1",
|
||||
"ups2": "UPS 2",
|
||||
},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "2.2.2.2",
|
||||
CONF_PORT: 456,
|
||||
CONF_USERNAME: "test-new-username",
|
||||
CONF_PASSWORD: "test-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "reconfigure_ups"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{CONF_ALIAS: "ups2"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "reconfigure_successful"
|
||||
|
||||
assert entry.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry.data[CONF_PORT] == 456
|
||||
assert entry.data[CONF_USERNAME] == "test-new-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-new-password"
|
||||
assert entry.data[CONF_ALIAS] == "ups2"
|
||||
|
||||
|
||||
async def test_reconfigure_multiple_aliases_nochange(hass: HomeAssistant) -> None:
|
||||
"""Test reconfigure with multiple aliases and no change."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={
|
||||
"ups1": "UPS 1",
|
||||
"ups2": "UPS 2",
|
||||
},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: entry.data[CONF_HOST],
|
||||
CONF_PORT: entry.data[CONF_PORT],
|
||||
CONF_USERNAME: entry.data[CONF_USERNAME],
|
||||
CONF_PASSWORD: entry.data[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "reconfigure_ups"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{CONF_ALIAS: "ups1"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "reconfigure_successful"
|
||||
|
||||
assert entry.data[CONF_HOST] == "1.1.1.1"
|
||||
assert entry.data[CONF_PORT] == 123
|
||||
assert entry.data[CONF_USERNAME] == "test-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-password"
|
||||
assert entry.data[CONF_ALIAS] == "ups1"
|
||||
|
||||
|
||||
async def test_reconfigure_multiple_aliases_password_nochange(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test reconfigure with multiple aliases when no password change."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={
|
||||
"ups1": "UPS 1",
|
||||
"ups2": "UPS 2",
|
||||
},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "2.2.2.2",
|
||||
CONF_PORT: 456,
|
||||
CONF_USERNAME: "test-new-username",
|
||||
CONF_PASSWORD: PASSWORD_NOT_CHANGED,
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "reconfigure_ups"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{CONF_ALIAS: "ups2"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "reconfigure_successful"
|
||||
|
||||
assert entry.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry.data[CONF_PORT] == 456
|
||||
assert entry.data[CONF_USERNAME] == "test-new-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-password"
|
||||
assert entry.data[CONF_ALIAS] == "ups2"
|
||||
|
||||
|
||||
async def test_reconfigure_multiple_aliases_already_configured(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test reconfigure multi aliases changed to existing host/port/alias."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
alias="ups1",
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1", "ups2": "UPS 2"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
entry2 = await async_init_integration(
|
||||
hass,
|
||||
host="2.2.2.2",
|
||||
port=456,
|
||||
alias="ups2",
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
assert entry2.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry2.data[CONF_PORT] == 456
|
||||
assert entry2.data[CONF_USERNAME] == "test-username"
|
||||
assert entry2.data[CONF_PASSWORD] == "test-password"
|
||||
|
||||
result = await entry2.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={
|
||||
"ups1": "UPS 1",
|
||||
"ups2": "UPS 2",
|
||||
},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: entry.data[CONF_HOST],
|
||||
CONF_PORT: entry.data[CONF_PORT],
|
||||
CONF_USERNAME: entry.data[CONF_USERNAME],
|
||||
CONF_PASSWORD: entry.data[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "reconfigure_ups"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{CONF_ALIAS: entry.data[CONF_ALIAS]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "already_configured"
|
||||
|
||||
assert entry.data[CONF_HOST] == "1.1.1.1"
|
||||
assert entry.data[CONF_PORT] == 123
|
||||
assert entry.data[CONF_USERNAME] == "test-username"
|
||||
assert entry.data[CONF_PASSWORD] == "test-password"
|
||||
assert entry.data[CONF_ALIAS] == "ups1"
|
||||
|
||||
assert entry2.data[CONF_HOST] == "2.2.2.2"
|
||||
assert entry2.data[CONF_PORT] == 456
|
||||
assert entry2.data[CONF_USERNAME] == "test-username"
|
||||
assert entry2.data[CONF_PASSWORD] == "test-password"
|
||||
assert entry2.data[CONF_ALIAS] == "ups2"
|
||||
|
||||
|
||||
async def test_reconfigure_multiple_aliases_unique_id_change(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test reconfigure with multiple aliases and the unique ID is changed."""
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
alias="ups1",
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1", "ups2": "UPS 2"},
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
)
|
||||
|
||||
result = await entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={
|
||||
"ups1": "UPS 1",
|
||||
"ups2": "UPS 2",
|
||||
},
|
||||
list_vars={
|
||||
"device.mfr": "Another manufacturer",
|
||||
"device.model": "Another model",
|
||||
"device.serial": "0000-2",
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: entry.data[CONF_HOST],
|
||||
CONF_PORT: entry.data[CONF_PORT],
|
||||
CONF_USERNAME: entry.data[CONF_USERNAME],
|
||||
CONF_PASSWORD: entry.data[CONF_PASSWORD],
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "reconfigure_ups"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{CONF_ALIAS: entry.data[CONF_ALIAS]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "unique_id_mismatch"
|
||||
|
||||
|
||||
async def test_reconfigure_multiple_aliases_duplicate_unique_ids(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test reconfigure multi aliases that results in duplicate unique ID."""
|
||||
|
||||
list_vars = {
|
||||
"device.mfr": "Some manufacturer",
|
||||
"device.model": "Some model",
|
||||
"device.serial": "0000-1",
|
||||
}
|
||||
|
||||
entry = await async_init_integration(
|
||||
hass,
|
||||
host="1.1.1.1",
|
||||
port=123,
|
||||
alias="ups1",
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1", "ups2": "UPS 2"},
|
||||
list_vars=list_vars,
|
||||
)
|
||||
|
||||
entry2 = await async_init_integration(
|
||||
hass,
|
||||
host="2.2.2.2",
|
||||
port=456,
|
||||
alias="ups2",
|
||||
username="test-username",
|
||||
password="test-password",
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
list_vars={
|
||||
"device.mfr": "Another manufacturer",
|
||||
"device.model": "Another model",
|
||||
"device.serial": "0000-2",
|
||||
},
|
||||
)
|
||||
|
||||
result = await entry2.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={
|
||||
"ups1": "UPS 1",
|
||||
"ups2": "UPS 2",
|
||||
},
|
||||
list_vars=list_vars,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "3.3.3.3",
|
||||
CONF_PORT: 789,
|
||||
CONF_USERNAME: "test-new-username",
|
||||
CONF_PASSWORD: "test-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "reconfigure_ups"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{CONF_ALIAS: entry.data[CONF_ALIAS]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.ABORT
|
||||
assert result3["reason"] == "unique_id_mismatch"
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
"""Tests for the nut integration."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from homeassistant.components.nut.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
||||
from homeassistant.const import (
|
||||
CONF_ALIAS,
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
@@ -35,8 +42,11 @@ def _get_mock_nutclient(
|
||||
async def async_init_integration(
|
||||
hass: HomeAssistant,
|
||||
ups_fixture: str | None = None,
|
||||
host: str = "mock",
|
||||
port: str = "mock",
|
||||
username: str = "mock",
|
||||
password: str = "mock",
|
||||
alias: str | None = None,
|
||||
list_ups: dict[str, str] | None = None,
|
||||
list_vars: dict[str, str] | None = None,
|
||||
list_commands_return_value: dict[str, str] | None = None,
|
||||
@@ -65,15 +75,24 @@ async def async_init_integration(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
extra_config_entry_data: dict[str, Any] = {}
|
||||
|
||||
if alias is not None:
|
||||
extra_config_entry_data = {
|
||||
CONF_ALIAS: alias,
|
||||
}
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_HOST: "mock",
|
||||
CONF_HOST: host,
|
||||
CONF_PASSWORD: password,
|
||||
CONF_PORT: "mock",
|
||||
CONF_PORT: port,
|
||||
CONF_USERNAME: username,
|
||||
},
|
||||
}
|
||||
| extra_config_entry_data,
|
||||
)
|
||||
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
||||
Reference in New Issue
Block a user