Skip to content

Commit

Permalink
Add timeout seconds config (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Feb 8, 2023
1 parent 42b480e commit b2269e2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ Recommend that you create a separate manager account with full permissions to ma

Turn off this config option if your browser gives you a warning that the SSL certificate is self-signed when you visit the router host IP in your browser.

### Timeout Secounds

How many seconds to wait until request times out. You can increase this if you get a lot of timeout errors from your router.

Note: The router also has its own timeout so increasing this may not help.

### Timeout Error Retry Count

How many times to retry timeout errors for one request. You can increase this if you get a lot of timeout errors from your router.
Expand Down Expand Up @@ -223,6 +229,7 @@ message: "device_tracker.amos_phone_wifi connected to main 5G through Guest Room
Some routers give a lot of timeout errors like `Timeout fetching tplink_deco`, which cause the devices to be unavailable. This is a problem with the router. Potential mitigations:

- Rebooting the router
- Increasing [timeout seconds](#timeout-seconds) in integration config
- Increasing [timeout error retry count](#timeout-error-retry-count) in integration config

## Contributions are welcome!
Expand Down
8 changes: 8 additions & 0 deletions custom_components/tplink_deco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
from .api import TplinkDecoApi
from .const import ATTR_DEVICE_TYPE
from .const import CONF_TIMEOUT_ERROR_RETRIES
from .const import CONF_TIMEOUT_SECONDS
from .const import CONF_VERIFY_SSL
from .const import COORDINATOR_CLIENTS_KEY
from .const import COORDINATOR_DECOS_KEY
from .const import DEFAULT_CONSIDER_HOME
from .const import DEFAULT_SCAN_INTERVAL
from .const import DEFAULT_TIMEOUT_ERROR_RETRIES
from .const import DEFAULT_TIMEOUT_SECONDS
from .const import DEVICE_TYPE_DECO
from .const import DOMAIN
from .const import PLATFORMS
Expand All @@ -64,6 +66,7 @@ async def async_create_and_refresh_coordinators(
username = config_data.get(CONF_USERNAME)
password = config_data.get(CONF_PASSWORD)
timeout_error_retries = config_data.get(CONF_TIMEOUT_ERROR_RETRIES)
timeout_seconds = config_data.get(CONF_TIMEOUT_SECONDS)
verify_ssl = config_data.get(CONF_VERIFY_SSL)
session = async_get_clientsession(hass)

Expand All @@ -74,6 +77,7 @@ async def async_create_and_refresh_coordinators(
password,
verify_ssl,
timeout_error_retries,
timeout_seconds,
)
deco_coordinator = TplinkDecoUpdateCoordinator(
hass, api, update_interval, deco_data
Expand Down Expand Up @@ -258,6 +262,10 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
config_entry.version = 3
new[CONF_TIMEOUT_ERROR_RETRIES] = DEFAULT_TIMEOUT_ERROR_RETRIES

if config_entry.version == 3:
config_entry.version = 4
new[CONF_TIMEOUT_SECONDS] = DEFAULT_TIMEOUT_SECONDS

hass.config_entries.async_update_entry(config_entry, data=new)

_LOGGER.info("Migration to version %s successful", config_entry.version)
Expand Down
10 changes: 6 additions & 4 deletions custom_components/tplink_deco/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers import modes

from .const import DEFAULT_TIMEOUT_ERROR_RETRIES
from .const import DEFAULT_TIMEOUT_SECONDS
from .exceptions import EmptyDataException
from .exceptions import ForbiddenException
from .exceptions import LoginForbiddenException
from .exceptions import LoginInvalidException
from .exceptions import TimeoutException
from .exceptions import UnexpectedApiException

TIMEOUT = 30

AES_KEY_BYTES = 16
MIN_AES_KEY = 10 ** (AES_KEY_BYTES - 1)
MAX_AES_KEY = (10**AES_KEY_BYTES) - 1
Expand Down Expand Up @@ -125,14 +125,16 @@ def __init__(
username: str,
password: str,
verify_ssl: bool,
timeout_error_retries: int = 1,
timeout_error_retries: int = DEFAULT_TIMEOUT_ERROR_RETRIES,
timeout_seconds: int = DEFAULT_TIMEOUT_SECONDS,
) -> None:
self._host = host
self._username = username
self._password = password
self._verify_ssl = verify_ssl
self._session = session
self._timeout_error_retries = timeout_error_retries
self._timeout_seconds = timeout_seconds
self._auth_errors = 0

self._aes_key = None
Expand Down Expand Up @@ -394,7 +396,7 @@ async def _async_post(
if self._cookie is not None:
headers[COOKIE] = self._cookie
try:
async with async_timeout.timeout(TIMEOUT):
async with async_timeout.timeout(self._timeout_seconds):
response = await self._session.post(
url,
params=params,
Expand Down
12 changes: 9 additions & 3 deletions custom_components/tplink_deco/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Adds config flow for TP-Link Deco."""
import asyncio
import logging
from typing import Any

Expand All @@ -19,11 +18,14 @@

from .__init__ import async_create_and_refresh_coordinators
from .const import CONF_TIMEOUT_ERROR_RETRIES
from .const import CONF_TIMEOUT_SECONDS
from .const import CONF_VERIFY_SSL
from .const import DEFAULT_CONSIDER_HOME
from .const import DEFAULT_SCAN_INTERVAL
from .const import DEFAULT_TIMEOUT_ERROR_RETRIES
from .const import DEFAULT_TIMEOUT_SECONDS
from .const import DOMAIN
from .exceptions import TimeoutException

_LOGGER: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -58,6 +60,10 @@ def _get_schema(data: dict[str:Any]):
default=data.get(
CONF_TIMEOUT_ERROR_RETRIES, DEFAULT_TIMEOUT_ERROR_RETRIES
),
): vol.All(vol.Coerce(int), vol.Range(min=5)),
vol.Required(
CONF_TIMEOUT_SECONDS,
default=data.get(CONF_TIMEOUT_SECONDS, DEFAULT_TIMEOUT_SECONDS),
): vol.All(vol.Coerce(int), vol.Range(min=0)),
vol.Required(
CONF_VERIFY_SSL,
Expand All @@ -73,7 +79,7 @@ async def _async_test_credentials(hass: HomeAssistant, data: dict[str:Any]):
try:
await async_create_and_refresh_coordinators(hass, data, consider_home_seconds=1)
return {}
except asyncio.TimeoutError:
except TimeoutException:
return {"base": "timeout_connect"}
except ConfigEntryAuthFailed as err:
_LOGGER.warning("Error authenticating credentials: %s", err)
Expand All @@ -86,7 +92,7 @@ async def _async_test_credentials(hass: HomeAssistant, data: dict[str:Any]):
class TplinkDecoFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for tplink_deco."""

VERSION = 3
VERSION = 4
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
reauth_entry: ConfigEntry = None

Expand Down
2 changes: 2 additions & 0 deletions custom_components/tplink_deco/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DEFAULT_CONSIDER_HOME = DEFAULT_CONSIDER_HOME_SPAN.total_seconds()
DEFAULT_SCAN_INTERVAL = 30
DEFAULT_TIMEOUT_ERROR_RETRIES = 1
DEFAULT_TIMEOUT_SECONDS = 30

DEVICE_TYPE_CLIENT = "client"
DEVICE_TYPE_DECO = "deco"
Expand All @@ -35,6 +36,7 @@

# Config
CONF_TIMEOUT_ERROR_RETRIES = "timeout_error_retries"
CONF_TIMEOUT_SECONDS = "timeout_seconds"
CONF_VERIFY_SSL = "verify_ssl"

# Signals
Expand Down
2 changes: 2 additions & 0 deletions custom_components/tplink_deco/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"password": "[%key:common::config_flow::data::password%]",
"interval_seconds": "Seconds between updates",
"consider_home": "Seconds to wait till marking a device tracker as not home after not being seen.",
"timeout_seconds": "Timeout seconds",
"timeout_error_retries": "Timeout error retry count",
"verify_ssl": "Verify SSL certificate is valid"
}
Expand Down Expand Up @@ -37,6 +38,7 @@
"password": "[%key:common::config_flow::data::password%]",
"interval_seconds": "Seconds between updates",
"consider_home": "Seconds to wait till marking a device tracker as not home after not being seen.",
"timeout_seconds": "Timeout seconds",
"timeout_error_retries": "Timeout error retry count",
"verify_ssl": "Verify SSL certificate is valid"
}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/tplink_deco/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"password": "Password",
"interval_seconds": "Seconds between updates",
"consider_home": "Seconds to wait till marking a device tracker as not home after not being seen.",
"timeout_seconds": "Timeout seconds",
"timeout_error_retries": "Timeout error retry count",
"verify_ssl": "Verify SSL certificate is valid"
}
Expand Down Expand Up @@ -40,6 +41,7 @@
"password": "Password",
"interval_seconds": "Seconds between updates",
"consider_home": "Seconds to wait till marking a device tracker as not home after not being seen.",
"timeout_seconds": "Timeout seconds",
"timeout_error_retries": "Timeout error retry count",
"verify_ssl": "Verify SSL certificate is valid"
}
Expand Down

0 comments on commit b2269e2

Please sign in to comment.