diff --git a/homeassistant/components/wwlln/config_flow.py b/homeassistant/components/wwlln/config_flow.py index 35b6ce6c8a0..81992794d2a 100644 --- a/homeassistant/components/wwlln/config_flow.py +++ b/homeassistant/components/wwlln/config_flow.py @@ -60,11 +60,14 @@ class WWLLNFlowHandler(config_entries.ConfigFlow): else: user_input[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_METRIC - # To simplify things, we don't allow users of the config flow to - # input a window; instead, we make a sane assumption to use the - # default (stored as seconds, since timedelta's aren't - # JSON-serializable): - if CONF_WINDOW not in user_input: + # When importing from `configuration.yaml`, we give the user + # flexibility by allowing the `window` parameter to be any type + # of time period. This will always return a timedelta; unfortunately, + # timedeltas aren't JSON-serializable, so we can't store them in a + # config entry as-is; instead, we save the total seconds as an int: + if CONF_WINDOW in user_input: + user_input[CONF_WINDOW] = user_input[CONF_WINDOW].total_seconds() + else: user_input[CONF_WINDOW] = DEFAULT_WINDOW.total_seconds() return self.async_create_entry(title=identifier, data=user_input) diff --git a/tests/components/wwlln/test_config_flow.py b/tests/components/wwlln/test_config_flow.py index 9751f5d5c9c..349dc19dce4 100644 --- a/tests/components/wwlln/test_config_flow.py +++ b/tests/components/wwlln/test_config_flow.py @@ -1,4 +1,6 @@ """Define tests for the WWLLN config flow.""" +from datetime import timedelta + from homeassistant import data_entry_flow from homeassistant.components.wwlln import CONF_WINDOW, DOMAIN, config_flow from homeassistant.const import ( @@ -36,12 +38,14 @@ 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: 600.0, + CONF_WINDOW: timedelta(minutes=10) } flow = config_flow.WWLLNFlowHandler() @@ -88,7 +92,7 @@ async def test_custom_window(hass): CONF_LATITUDE: 39.128712, CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25, - CONF_WINDOW: 300 + CONF_WINDOW: timedelta(hours=1) } flow = config_flow.WWLLNFlowHandler() @@ -102,5 +106,5 @@ async def test_custom_window(hass): CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25, CONF_UNIT_SYSTEM: 'metric', - CONF_WINDOW: 300, + CONF_WINDOW: 3600, }