Fix window exception in WWLLN (#25100)

* Beta fix: handle window exception in WWLLN

* Fixed test

* Fix bug

* Member comments

* Removed unused import
This commit is contained in:
Aaron Bach 2019-07-12 09:41:47 -06:00 committed by Paulus Schoutsen
parent 60c2e5e2e2
commit 5eb7268ae7
2 changed files with 15 additions and 8 deletions

View file

@ -60,11 +60,14 @@ class WWLLNFlowHandler(config_entries.ConfigFlow):
else: else:
user_input[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_METRIC user_input[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_METRIC
# To simplify things, we don't allow users of the config flow to # When importing from `configuration.yaml`, we give the user
# input a window; instead, we make a sane assumption to use the # flexibility by allowing the `window` parameter to be any type
# default (stored as seconds, since timedelta's aren't # of time period. This will always return a timedelta; unfortunately,
# JSON-serializable): # timedeltas aren't JSON-serializable, so we can't store them in a
if CONF_WINDOW not in user_input: # 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() user_input[CONF_WINDOW] = DEFAULT_WINDOW.total_seconds()
return self.async_create_entry(title=identifier, data=user_input) return self.async_create_entry(title=identifier, data=user_input)

View file

@ -1,4 +1,6 @@
"""Define tests for the WWLLN config flow.""" """Define tests for the WWLLN config flow."""
from datetime import timedelta
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components.wwlln import CONF_WINDOW, DOMAIN, config_flow from homeassistant.components.wwlln import CONF_WINDOW, DOMAIN, config_flow
from homeassistant.const import ( from homeassistant.const import (
@ -36,12 +38,14 @@ async def test_show_form(hass):
async def test_step_import(hass): async def test_step_import(hass):
"""Test that the import step works.""" """Test that the import step works."""
# `configuration.yaml` will always return a timedelta for the `window`
# parameter, FYI:
conf = { conf = {
CONF_LATITUDE: 39.128712, CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612, CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25, CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: 'metric', CONF_UNIT_SYSTEM: 'metric',
CONF_WINDOW: 600.0, CONF_WINDOW: timedelta(minutes=10)
} }
flow = config_flow.WWLLNFlowHandler() flow = config_flow.WWLLNFlowHandler()
@ -88,7 +92,7 @@ async def test_custom_window(hass):
CONF_LATITUDE: 39.128712, CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612, CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25, CONF_RADIUS: 25,
CONF_WINDOW: 300 CONF_WINDOW: timedelta(hours=1)
} }
flow = config_flow.WWLLNFlowHandler() flow = config_flow.WWLLNFlowHandler()
@ -102,5 +106,5 @@ async def test_custom_window(hass):
CONF_LONGITUDE: -104.9812612, CONF_LONGITUDE: -104.9812612,
CONF_RADIUS: 25, CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: 'metric', CONF_UNIT_SYSTEM: 'metric',
CONF_WINDOW: 300, CONF_WINDOW: 3600,
} }