Skip to content
Open
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"oauthlib~=3.1",
"requests-oauthlib>=1.3,<3.0",
"requests~=2.24",
"tenacity~=9.1",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand Down
30 changes: 29 additions & 1 deletion src/pyatmo/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,34 @@
ClientTimeout,
ContentTypeError,
)
from tenacity import (
before_sleep_log,
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)

from pyatmo.const import (
AUTHORIZATION_HEADER,
CONCURRENCY_ERROR_CODE,
DEFAULT_BASE_URL,
ERRORS,
FORBIDDEN_ERROR_CODE,
THROTTLING_ERROR_CODE,
TOO_MANY_REQUESTS_ERROR_CODE,
WEBHOOK_URL_ADD_ENDPOINT,
WEBHOOK_URL_DROP_ENDPOINT,
)
from pyatmo.exceptions import ApiError, ApiThrottlingError
from pyatmo.exceptions import ApiError, ApiThrottlingError, ApiTooManyRequestError

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

# Retries to official API
MAX_RETRIES = 5
INITIAL_BACKOFF = 1 # in seconds
MULTIPLIER = 1


class AbstractAsyncAuth(ABC):
"""Abstract class to make authenticated requests."""
Expand Down Expand Up @@ -77,6 +91,13 @@ async def async_get_image(
msg = f"{resp.status} - invalid content-type in response when accessing '{url}'"
raise ApiError(msg)

@retry(
retry=retry_if_exception_type(ApiTooManyRequestError),
stop=stop_after_attempt(MAX_RETRIES),
wait=wait_exponential(multiplier=MULTIPLIER, min=INITIAL_BACKOFF),
before_sleep=before_sleep_log(LOG, logging.DEBUG),
reraise=True,
)
async def async_post_api_request(
self,
endpoint: str,
Expand Down Expand Up @@ -161,6 +182,13 @@ async def handle_error_response(
f"when accessing '{url}'"
)

if (
resp_status == TOO_MANY_REQUESTS_ERROR_CODE
and resp_json["error"]["code"] == CONCURRENCY_ERROR_CODE
):
raise ApiTooManyRequestError(message)

LOG.debug("The Netatmo API returned %s", message)
if (
resp_status == FORBIDDEN_ERROR_CODE
and resp_json["error"]["code"] == THROTTLING_ERROR_CODE
Expand Down
2 changes: 2 additions & 0 deletions src/pyatmo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@
UNKNOWN = "unknown"

# Error codes
CONCURRENCY_ERROR_CODE = 11
THROTTLING_ERROR_CODE = 26
FORBIDDEN_ERROR_CODE = 403
TOO_MANY_REQUESTS_ERROR_CODE = 429

# Location constants
GPS_COORDINATES_COUNT = 2
4 changes: 4 additions & 0 deletions src/pyatmo/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ class ApiHomeReachabilityError(ApiError):

class InvalidStateError(Exception):
"""Raised when an invalid state is encountered."""


class ApiTooManyRequestError(ApiError):
"""Raised when API returned 429 code 11."""
4 changes: 3 additions & 1 deletion src/pyatmo/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,9 @@ async def async_update_measures(

delta_range = MEASURE_INTERVAL_TO_SECONDS.get(interval, 0) // 2

filters, raw_data = await self._energy_api_calls(start_time, end_time, interval)
_filters, raw_data = await self._energy_api_calls(
start_time, end_time, interval
)

hist_good_vals = await self._get_aligned_energy_values_and_mode(
start_time,
Expand Down
19 changes: 12 additions & 7 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.