From aa27e22b1741c1cdd7448458be7dc852d7f8113d Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sun, 21 Jul 2019 00:58:50 -0600 Subject: [PATCH] Automatically expand WWLLN window to 1 hour (if necessary) (#25357) * Expand default window for WWLLN * Fleshed out conditions * Fixed tests * Removed unused import * Linting --- homeassistant/components/wwlln/__init__.py | 25 +++++++++++++++++++ homeassistant/components/wwlln/config_flow.py | 2 +- homeassistant/components/wwlln/const.py | 2 +- tests/components/wwlln/test_config_flow.py | 6 ++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/wwlln/__init__.py b/homeassistant/components/wwlln/__init__.py index fe5061f6ed4..f302dbb081d 100644 --- a/homeassistant/components/wwlln/__init__.py +++ b/homeassistant/components/wwlln/__init__.py @@ -41,6 +41,11 @@ async def async_setup(hass, config): if identifier in configured_instances(hass): return True + if conf[CONF_WINDOW] < DEFAULT_WINDOW: + _LOGGER.warning( + 'Setting a window smaller than %s seconds may cause Home Assistant \ + to miss events', DEFAULT_WINDOW.total_seconds()) + if hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL: unit_system = CONF_UNIT_SYSTEM_IMPERIAL else: @@ -85,3 +90,23 @@ async def async_unload_entry(hass, config_entry): config_entry, 'geo_location') return True + + +async def async_migrate_entry(hass, config_entry): + """Migrate the config entry upon new versions.""" + version = config_entry.version + data = config_entry.data + + default_total_seconds = DEFAULT_WINDOW.total_seconds() + + _LOGGER.debug('Migrating from version %s', version) + + # 1 -> 2: Expanding the default window to 1 hour (if needed): + if version == 1: + if data[CONF_WINDOW] < default_total_seconds: + data[CONF_WINDOW] = default_total_seconds + version = config_entry.version = 2 + hass.config_entries.async_update_entry(config_entry, data=data) + _LOGGER.info('Migration to version %s successful', version) + + return True diff --git a/homeassistant/components/wwlln/config_flow.py b/homeassistant/components/wwlln/config_flow.py index 81992794d2a..57525cf1649 100644 --- a/homeassistant/components/wwlln/config_flow.py +++ b/homeassistant/components/wwlln/config_flow.py @@ -25,7 +25,7 @@ def configured_instances(hass): class WWLLNFlowHandler(config_entries.ConfigFlow): """Handle a WWLLN config flow.""" - VERSION = 1 + VERSION = 2 CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL async def _show_form(self, errors=None): diff --git a/homeassistant/components/wwlln/const.py b/homeassistant/components/wwlln/const.py index e712f7f68a4..1c7b033b77d 100644 --- a/homeassistant/components/wwlln/const.py +++ b/homeassistant/components/wwlln/const.py @@ -8,4 +8,4 @@ CONF_WINDOW = 'window' DATA_CLIENT = 'client' DEFAULT_RADIUS = 25 -DEFAULT_WINDOW = timedelta(minutes=10) +DEFAULT_WINDOW = timedelta(hours=1) diff --git a/tests/components/wwlln/test_config_flow.py b/tests/components/wwlln/test_config_flow.py index 34d46bbd7c6..eac5598c138 100644 --- a/tests/components/wwlln/test_config_flow.py +++ b/tests/components/wwlln/test_config_flow.py @@ -83,7 +83,7 @@ async def test_step_user(hass): CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25, CONF_UNIT_SYSTEM: 'metric', - CONF_WINDOW: 600.0, + CONF_WINDOW: 3600.0, } @@ -93,7 +93,7 @@ async def test_custom_window(hass): CONF_LATITUDE: 39.128712, CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25, - CONF_WINDOW: timedelta(hours=1) + CONF_WINDOW: timedelta(hours=2) } flow = config_flow.WWLLNFlowHandler() @@ -107,7 +107,7 @@ async def test_custom_window(hass): CONF_LONGITUDE: -104.9812612, CONF_RADIUS: 25, CONF_UNIT_SYSTEM: 'metric', - CONF_WINDOW: 3600, + CONF_WINDOW: 7200, }