diff --git a/custom_components/google_home/__init__.py b/custom_components/google_home/__init__.py index acba39ff1..5f5304040 100644 --- a/custom_components/google_home/__init__.py +++ b/custom_components/google_home/__init__.py @@ -20,6 +20,7 @@ from .const import ( CONF_ANDROID_ID, CONF_MASTER_TOKEN, + CONF_UPDATE_INTERVAL, DATA_CLIENT, DATA_COORDINATOR, DOMAIN, @@ -42,6 +43,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: password = entry.data.get(CONF_PASSWORD) android_id = entry.data.get(CONF_ANDROID_ID) master_token = entry.data.get(CONF_MASTER_TOKEN) + update_interval = entry.options.get(CONF_UPDATE_INTERVAL, UPDATE_INTERVAL) + + _LOGGER.debug( + "Coordinator update_interval is: %s", timedelta(seconds=update_interval) + ) session = async_get_clientsession(hass, verify_ssl=False) @@ -61,7 +67,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: _LOGGER, name=SENSOR, update_method=glocaltokens_client.update_google_devices_information, - update_interval=timedelta(seconds=UPDATE_INTERVAL), + update_interval=timedelta(seconds=update_interval), ) await coordinator.async_refresh() diff --git a/custom_components/google_home/config_flow.py b/custom_components/google_home/config_flow.py index 8f8c5a276..77037bd35 100644 --- a/custom_components/google_home/config_flow.py +++ b/custom_components/google_home/config_flow.py @@ -1,6 +1,7 @@ """Adds config flow for Google Home""" from __future__ import annotations +from datetime import timedelta import logging from typing import Any @@ -18,8 +19,11 @@ CONF_DATA_COLLECTION, CONF_MASTER_TOKEN, CONF_PASSWORD, + CONF_UPDATE_INTERVAL, CONF_USERNAME, + DATA_COORDINATOR, DOMAIN, + UPDATE_INTERVAL, ) from .exceptions import InvalidMasterToken from .types import OptionsFlowDict @@ -74,7 +78,7 @@ def async_get_options_flow( async def _show_config_form( self, _user_input: dict[str, Any] | None ) -> dict[str, Any]: - """Show the configuration form to edit location data.""" + """Show the configuration form to edit login information.""" return self.async_show_form( step_id="user", data_schema=vol.Schema( @@ -112,12 +116,28 @@ async def async_step_init( """Manage the options.""" if user_input is not None: self.options.update(user_input) - return self.async_create_entry(title="", data=self.options) + coordinator = self.hass.data[DOMAIN][self.config_entry.entry_id][ + DATA_COORDINATOR + ] + update_interval = timedelta( + seconds=self.options.get(CONF_UPDATE_INTERVAL, UPDATE_INTERVAL) + ) + _LOGGER.debug("Updating coordinator, update_interval: %s", update_interval) + coordinator.update_interval = update_interval + return self.async_create_entry( + title=self.config_entry.data.get(CONF_USERNAME), data=self.options + ) return self.async_show_form( step_id="init", data_schema=vol.Schema( { + vol.Optional( + CONF_UPDATE_INTERVAL, + default=self.config_entry.options.get( + CONF_UPDATE_INTERVAL, UPDATE_INTERVAL + ), + ): int, vol.Optional( CONF_DATA_COLLECTION, default=self.config_entry.options.get( diff --git a/custom_components/google_home/const.py b/custom_components/google_home/const.py index fc062a9e5..63da80929 100644 --- a/custom_components/google_home/const.py +++ b/custom_components/google_home/const.py @@ -14,6 +14,7 @@ ATTRIBUTION: Final[str] = "json" ISSUE_URL: Final[str] = "https://github.com/leikoilja/ha-google-home/issues" CONF_DATA_COLLECTION: Final[str] = "data_collection" +CONF_UPDATE_INTERVAL: Final[str] = "update_interval" DATA_CLIENT: Final[str] = "client" DATA_COORDINATOR: Final[str] = "coordinator" @@ -84,7 +85,7 @@ # Access token only lives about 1 hour # Update often to fetch timers in timely manner -UPDATE_INTERVAL: Final[int] = 10 # sec +UPDATE_INTERVAL: Final[int] = 180 # sec # JSON parameter values when retrieving information from devices JSON_ALARM: Final[str] = "alarm" diff --git a/custom_components/google_home/translations/en.json b/custom_components/google_home/translations/en.json index f4d64d006..f6926be22 100644 --- a/custom_components/google_home/translations/en.json +++ b/custom_components/google_home/translations/en.json @@ -21,7 +21,8 @@ "step": { "init": { "data": { - "data_collection": "Allow anonymous diagnostic data collection (See https://github.com/leikoilja/ha-google-home#diagnostic-data-collection)." + "data_collection": "Allow anonymous diagnostic data collection (See https://github.com/leikoilja/ha-google-home#diagnostic-data-collection).", + "update_interval": "Change update interval. Increase this if you are suffering from devices timing out. Default: 180 (Seconds)" } } }