mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add Plugwise scan_interval config option (#37229)
Co-authored-by: Tom Scholten <tom@sue.nl> Co-authored-by: Tom Scholten <git@scholten.nu>
This commit is contained in:
@@ -2,10 +2,28 @@
|
||||
from Plugwise_Smile.Smile import Smile
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.plugwise.const import DOMAIN
|
||||
from homeassistant import config_entries, data_entry_flow, setup
|
||||
from homeassistant.components.plugwise import config_flow
|
||||
from homeassistant.components.plugwise.const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_SCAN_INTERVAL
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.async_mock import MagicMock, patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
TEST_HOST = "1.1.1.1"
|
||||
TEST_HOSTNAME = "smileabcdef"
|
||||
TEST_PASSWORD = "test_password"
|
||||
TEST_DISCOVERY = {
|
||||
"host": TEST_HOST,
|
||||
"hostname": f"{TEST_HOSTNAME}.local.",
|
||||
"server": f"{TEST_HOSTNAME}.local.",
|
||||
"properties": {
|
||||
"product": "smile",
|
||||
"version": "1.2.3",
|
||||
"hostname": f"{TEST_HOSTNAME}.local.",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_smile")
|
||||
@@ -25,9 +43,9 @@ async def test_form(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}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
@@ -42,15 +60,56 @@ async def test_form(hass):
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"host": "1.1.1.1", "password": "test-password"},
|
||||
{"host": TEST_HOST, "password": TEST_PASSWORD},
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result2["data"] == {
|
||||
"host": "1.1.1.1",
|
||||
"password": "test-password",
|
||||
"host": TEST_HOST,
|
||||
"password": TEST_PASSWORD,
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["errors"] == {}
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_zeroconf_form(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": SOURCE_ZEROCONF},
|
||||
data=TEST_DISCOVERY,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.plugwise.config_flow.Smile.connect",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.plugwise.async_setup",
|
||||
return_value=True,
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.plugwise.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"password": TEST_PASSWORD},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result2["data"] == {
|
||||
"host": TEST_HOST,
|
||||
"password": TEST_PASSWORD,
|
||||
}
|
||||
|
||||
assert result["errors"] == {}
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
@@ -66,10 +125,10 @@ async def test_form_invalid_auth(hass, mock_smile):
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"host": "1.1.1.1", "password": "test-password"},
|
||||
{"host": TEST_HOST, "password": TEST_PASSWORD},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
@@ -84,8 +143,93 @@ async def test_form_cannot_connect(hass, mock_smile):
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"host": "1.1.1.1", "password": "test-password"},
|
||||
{"host": TEST_HOST, "password": TEST_PASSWORD},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_other_problem(hass, mock_smile):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_smile.connect.side_effect = TimeoutError
|
||||
mock_smile.gateway_id = "0a636a4fc1704ab4a24e4f7e37fb187a"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"host": TEST_HOST, "password": TEST_PASSWORD},
|
||||
)
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_show_zeroconf_form(hass, mock_smile) -> None:
|
||||
"""Test that the zeroconf confirmation form is served."""
|
||||
flow = config_flow.PlugwiseConfigFlow()
|
||||
flow.hass = hass
|
||||
flow.context = {"source": SOURCE_ZEROCONF}
|
||||
result = await flow.async_step_zeroconf(TEST_DISCOVERY)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert flow.context["title_placeholders"][CONF_HOST] == TEST_HOST
|
||||
assert flow.context["title_placeholders"]["name"] == "P1 DSMR v1.2.3"
|
||||
|
||||
assert result["step_id"] == "user"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
|
||||
async def test_options_flow_power(hass, mock_smile) -> None:
|
||||
"""Test config flow options DSMR environments."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=CONF_NAME,
|
||||
data={CONF_HOST: TEST_HOST, CONF_PASSWORD: TEST_PASSWORD},
|
||||
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
||||
)
|
||||
|
||||
hass.data[DOMAIN] = {entry.entry_id: {"api": MagicMock(smile_type="power")}}
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.options.async_init(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_SCAN_INTERVAL: 10}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_SCAN_INTERVAL: 10,
|
||||
}
|
||||
|
||||
|
||||
async def test_options_flow_thermo(hass, mock_smile) -> None:
|
||||
"""Test config flow options for thermostatic environments."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=CONF_NAME,
|
||||
data={CONF_HOST: TEST_HOST, CONF_PASSWORD: TEST_PASSWORD},
|
||||
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
||||
)
|
||||
|
||||
hass.data[DOMAIN] = {entry.entry_id: {"api": MagicMock(smile_type="thermostat")}}
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.options.async_init(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_SCAN_INTERVAL: 60}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_SCAN_INTERVAL: 60,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user