From 61d32673a0f99cb1a8a7123d6c302e42b04a44ef Mon Sep 17 00:00:00 2001 From: Emanuele Palazzetti Date: Thu, 7 Sep 2023 12:47:02 +0000 Subject: [PATCH] fix(draft): support delayed scan interval --- custom_components/elmo_iess_alarm/__init__.py | 6 ++++-- custom_components/elmo_iess_alarm/config_flow.py | 8 ++++++++ custom_components/elmo_iess_alarm/const.py | 4 +++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/custom_components/elmo_iess_alarm/__init__.py b/custom_components/elmo_iess_alarm/__init__.py index 9b88e1a..47f1426 100644 --- a/custom_components/elmo_iess_alarm/__init__.py +++ b/custom_components/elmo_iess_alarm/__init__.py @@ -14,13 +14,14 @@ from .const import ( CONF_DOMAIN, + CONF_SCAN_INTERVAL, CONF_SYSTEM_URL, DOMAIN, KEY_COORDINATOR, KEY_DEVICE, KEY_UNSUBSCRIBER, POLLING_TIMEOUT, - SCAN_INTERVAL, + SCAN_INTERVAL_DEFAULT, ) from .devices import AlarmDevice @@ -89,11 +90,12 @@ async def async_update_data(): await hass.async_add_executor_job(device.connect, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) _LOGGER.info("Token was invalid or expired, re-authentication executed.") + scan_interval = entry.options.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL_DEFAULT) coordinator = DataUpdateCoordinator( hass, _LOGGER, name="elmo_iess_alarm", - update_interval=timedelta(seconds=SCAN_INTERVAL), + update_interval=timedelta(seconds=scan_interval), update_method=async_update_data, ) diff --git a/custom_components/elmo_iess_alarm/config_flow.py b/custom_components/elmo_iess_alarm/config_flow.py index b0e25b3..9c8bf06 100644 --- a/custom_components/elmo_iess_alarm/config_flow.py +++ b/custom_components/elmo_iess_alarm/config_flow.py @@ -14,8 +14,10 @@ CONF_AREAS_ARM_NIGHT, CONF_AREAS_ARM_VACATION, CONF_DOMAIN, + CONF_SCAN_INTERVAL, CONF_SYSTEM_URL, DOMAIN, + SCAN_INTERVAL_DEFAULT, SUPPORTED_SYSTEMS, ) from .exceptions import InvalidAreas @@ -126,6 +128,7 @@ async def async_step_init(self, user_input=None): suggest_arm_vacation = user_input.get(CONF_AREAS_ARM_VACATION) or self.config_entry.options.get( CONF_AREAS_ARM_VACATION ) + suggest_scan_interval = user_input.get(CONF_SCAN_INTERVAL) or self.config_entry.options.get(CONF_SCAN_INTERVAL) return self.async_show_form( step_id="init", data_schema=vol.Schema( @@ -142,6 +145,11 @@ async def async_step_init(self, user_input=None): CONF_AREAS_ARM_VACATION, description={"suggested_value": suggest_arm_vacation}, ): str, + vol.Required( + CONF_SCAN_INTERVAL, + default=SCAN_INTERVAL_DEFAULT, + description={"suggested_value": suggest_scan_interval}, + ): int, } ), errors=errors, diff --git a/custom_components/elmo_iess_alarm/const.py b/custom_components/elmo_iess_alarm/const.py index b727878..a791dfd 100644 --- a/custom_components/elmo_iess_alarm/const.py +++ b/custom_components/elmo_iess_alarm/const.py @@ -10,11 +10,13 @@ CONF_AREAS_ARM_HOME = "areas_arm_home" CONF_AREAS_ARM_NIGHT = "areas_arm_night" CONF_AREAS_ARM_VACATION = "areas_arm_vacation" +CONF_SCAN_INTERVAL = "scan_interval" DOMAIN = "elmo_iess_alarm" KEY_DEVICE = "device" KEY_COORDINATOR = "coordinator" KEY_UNSUBSCRIBER = "options_unsubscriber" # Fast scanning is fine because long-polling is used # and lasts 15 seconds -SCAN_INTERVAL = 5 +# TODO: update this comment explaining the default value +SCAN_INTERVAL_DEFAULT = 5 POLLING_TIMEOUT = 20