Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option for changing update interval. #302

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion custom_components/google_home/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .const import (
CONF_ANDROID_ID,
CONF_MASTER_TOKEN,
CONF_UPDATE_INTERVAL,
DATA_CLIENT,
DATA_COORDINATOR,
DOMAIN,
Expand All @@ -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)

Expand All @@ -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()
Expand Down
24 changes: 22 additions & 2 deletions custom_components/google_home/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Adds config flow for Google Home"""
from __future__ import annotations

from datetime import timedelta
import logging
from typing import Any

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion custom_components/google_home/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion custom_components/google_home/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}
}
Expand Down