Modernize WWLLN config flow (#32194)

* Modernize WWLLN config flow

* Code review

* Update tests
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-03-04 18:23:00 -08:00
1 parent 56cf4e54a9
commit 81810dd920
7 files changed
+152 -145

No files matched your search

+66 -36
View File
@@ -1,6 +1,4 @@
"""Define tests for the WWLLN config flow."""
from datetime import timedelta
from asynctest import patch
from homeassistant import data_entry_flow
@@ -9,34 +7,34 @@ from homeassistant.components.wwlln import (
DATA_CLIENT,
DOMAIN,
async_setup_entry,
config_flow,
)
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_RADIUS,
CONF_UNIT_SYSTEM,
)
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS
from tests.common import MockConfigEntry
async def test_duplicate_error(hass, config_entry):
"""Test that errors are shown when duplicates are added."""
conf = {CONF_LATITUDE: 39.128712, CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25}
config_entry.add_to_hass(hass)
flow = config_flow.WWLLNFlowHandler()
flow.hass = hass
MockConfigEntry(
domain=DOMAIN, unique_id="39.128712, -104.9812612", data=conf
).add_to_hass(hass)
result = await flow.async_step_user(user_input=conf)
assert result["errors"] == {"base": "identifier_exists"}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_show_form(hass):
"""Test that the form is served with no input."""
flow = config_flow.WWLLNFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=None)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
@@ -44,46 +42,79 @@ async def test_show_form(hass):
async def test_step_import(hass):
"""Test that the import step works."""
# `configuration.yaml` will always return a timedelta for the `window`
# parameter, FYI:
conf = {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_WINDOW: timedelta(minutes=10),
}
flow = config_flow.WWLLNFlowHandler()
flow.hass = hass
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)
result = await flow.async_step_import(import_config=conf)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "39.128712, -104.9812612"
assert result["data"] == {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_WINDOW: 600.0,
CONF_WINDOW: 3600.0,
}
async def test_step_import_too_small_window(hass):
"""Test that the import step with a too-small window is aborted."""
conf = {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_WINDOW: 60,
}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "window_too_small"
async def test_step_user(hass):
"""Test that the user step works."""
conf = {CONF_LATITUDE: 39.128712, CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25}
flow = config_flow.WWLLNFlowHandler()
flow.hass = hass
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "39.128712, -104.9812612"
assert result["data"] == {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_WINDOW: 3600.0,
}
async def test_different_unit_system(hass):
"""Test that the config flow picks up the HASS unit system."""
conf = {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
result = await flow.async_step_user(user_input=conf)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "39.128712, -104.9812612"
assert result["data"] == {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_WINDOW: 3600.0,
}
@@ -94,20 +125,19 @@ async def test_custom_window(hass):
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_WINDOW: timedelta(hours=2),
CONF_WINDOW: 7200,
}
flow = config_flow.WWLLNFlowHandler()
flow.hass = hass
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
result = await flow.async_step_user(user_input=conf)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "39.128712, -104.9812612"
assert result["data"] == {
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_WINDOW: 7200,
}