Automatically expand WWLLN window to 1 hour (if necessary) (#25357)

* Expand default window for WWLLN

* Fleshed out conditions

* Fixed tests

* Removed unused import

* Linting
pull/25365/head
Aaron Bach 2019-07-21 00:58:50 -06:00 committed by Martin Hjelmare
parent 95223cb9ea
commit aa27e22b17
4 changed files with 30 additions and 5 deletions

View File

@ -41,6 +41,11 @@ async def async_setup(hass, config):
if identifier in configured_instances(hass): if identifier in configured_instances(hass):
return True 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: if hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL:
unit_system = CONF_UNIT_SYSTEM_IMPERIAL unit_system = CONF_UNIT_SYSTEM_IMPERIAL
else: else:
@ -85,3 +90,23 @@ async def async_unload_entry(hass, config_entry):
config_entry, 'geo_location') config_entry, 'geo_location')
return True 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

View File

@ -25,7 +25,7 @@ def configured_instances(hass):
class WWLLNFlowHandler(config_entries.ConfigFlow): class WWLLNFlowHandler(config_entries.ConfigFlow):
"""Handle a WWLLN config flow.""" """Handle a WWLLN config flow."""
VERSION = 1 VERSION = 2
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
async def _show_form(self, errors=None): async def _show_form(self, errors=None):

View File

@ -8,4 +8,4 @@ CONF_WINDOW = 'window'
DATA_CLIENT = 'client' DATA_CLIENT = 'client'
DEFAULT_RADIUS = 25 DEFAULT_RADIUS = 25
DEFAULT_WINDOW = timedelta(minutes=10) DEFAULT_WINDOW = timedelta(hours=1)

View File

@ -83,7 +83,7 @@ async def test_step_user(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: 600.0, CONF_WINDOW: 3600.0,
} }
@ -93,7 +93,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: timedelta(hours=1) CONF_WINDOW: timedelta(hours=2)
} }
flow = config_flow.WWLLNFlowHandler() flow = config_flow.WWLLNFlowHandler()
@ -107,7 +107,7 @@ 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: 3600, CONF_WINDOW: 7200,
} }