Skip to content

Commit c22823f

Browse files
authored
Use growattServer library error code constants in growatt_server (home-assistant#172771)
1 parent a21212a commit c22823f

6 files changed

Lines changed: 20 additions & 30 deletions

File tree

homeassistant/components/growatt_server/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
1111
Open API V1 (API token):
1212
- Stateless — no login call, token is sent as a Bearer header on every request.
13-
- Auth failure is signalled by raising GrowattV1ApiError with error_code=10011
14-
(V1_API_ERROR_NO_PRIVILEGE). The library NEVER returns a failure silently;
13+
- Auth failure is signalled by raising GrowattV1ApiError with
14+
error_code=GrowattV1ApiErrorCode.NO_PRIVILEGE. The library NEVER returns a failure silently;
1515
any non-zero error_code raises an exception via _process_response().
1616
- Because the library always raises on error, return-value validation after a
1717
successful V1 API call is unnecessary — if it returned, the token was valid.
1818
1919
Error handling pattern for reauth:
2020
- Classic API: check NOT login_response["success"] and msg == LOGIN_INVALID_AUTH_CODE
2121
→ raise ConfigEntryAuthFailed
22-
- V1 API: catch GrowattV1ApiError with error_code == V1_API_ERROR_NO_PRIVILEGE
22+
- V1 API: catch GrowattV1ApiError with error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE
2323
→ raise ConfigEntryAuthFailed
2424
- All other errors → ConfigEntryError (setup) or UpdateFailed (coordinator)
2525
"""
@@ -30,6 +30,7 @@
3030
import logging
3131

3232
import growattServer
33+
from growattServer import GrowattV1ApiErrorCode
3334
from requests import RequestException
3435

3536
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_URL, CONF_USERNAME
@@ -58,8 +59,6 @@
5859
LOGIN_INVALID_AUTH_CODE,
5960
PLATFORMS,
6061
SUPPORTED_DEVICE_TYPES,
61-
V1_API_ERROR_NO_PRIVILEGE,
62-
V1_API_ERROR_RATE_LIMITED,
6362
V1_DEVICE_TYPES,
6463
)
6564
from .coordinator import GrowattConfigEntry, GrowattCoordinator
@@ -265,11 +264,11 @@ def get_device_list_v1(
265264
try:
266265
devices_dict = api.device_list(plant_id)
267266
except growattServer.GrowattV1ApiError as e:
268-
if e.error_code == V1_API_ERROR_NO_PRIVILEGE:
267+
if e.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
269268
raise ConfigEntryAuthFailed(
270269
f"Authentication failed for Growatt API: {e.error_msg or str(e)}"
271270
) from e
272-
if e.error_code == V1_API_ERROR_RATE_LIMITED:
271+
if e.error_code == GrowattV1ApiErrorCode.RATE_LIMITED:
273272
raise ConfigEntryNotReady(
274273
f"Growatt API rate limited, will retry: {e.error_msg or str(e)}"
275274
) from e

homeassistant/components/growatt_server/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any
66

77
import growattServer
8+
from growattServer import GrowattV1ApiErrorCode
89
import requests
910
import voluptuous as vol
1011

@@ -32,7 +33,6 @@
3233
ERROR_INVALID_AUTH,
3334
LOGIN_INVALID_AUTH_CODE,
3435
SERVER_URLS_NAMES,
35-
V1_API_ERROR_NO_PRIVILEGE,
3636
)
3737

3838
_URL_TO_REGION = {v: k for k, v in SERVER_URLS_NAMES.items()}
@@ -148,7 +148,7 @@ async def _async_step_credentials(
148148
_LOGGER.debug("Network error during credential update: %s", ex)
149149
errors["base"] = ERROR_CANNOT_CONNECT
150150
except growattServer.GrowattV1ApiError as err:
151-
if err.error_code == V1_API_ERROR_NO_PRIVILEGE:
151+
if err.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
152152
errors["base"] = ERROR_INVALID_AUTH
153153
else:
154154
_LOGGER.debug(
@@ -301,7 +301,7 @@ async def async_step_token_auth(
301301
e.error_msg or str(e),
302302
e.error_code,
303303
)
304-
if e.error_code == V1_API_ERROR_NO_PRIVILEGE:
304+
if e.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
305305
return self._async_show_token_form({"base": ERROR_INVALID_AUTH})
306306
return self._async_show_token_form({"base": ERROR_CANNOT_CONNECT})
307307
except (ValueError, KeyError, TypeError, AttributeError) as ex:

homeassistant/components/growatt_server/const.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@
4242
# Growatt Classic API error codes
4343
LOGIN_INVALID_AUTH_CODE = "502"
4444

45-
# Growatt Open API V1 error codes
46-
# Reference: https://www.showdoc.com.cn/262556420217021/1494055648380019
47-
V1_API_ERROR_WRONG_DOMAIN = -1 # Use correct regional domain
48-
V1_API_ERROR_NO_PRIVILEGE = 10011 # No privilege access — invalid or expired token
49-
V1_API_ERROR_RATE_LIMITED = 10012 # Access frequency limit (5 minutes per call)
50-
V1_API_ERROR_PAGE_SIZE = 10013 # Page size cannot exceed 100
51-
V1_API_ERROR_PAGE_COUNT = 10014 # Page count cannot exceed 250
5245

5346
# Config flow error types (also used as abort reasons)
5447
ERROR_CANNOT_CONNECT = "cannot_connect" # Used for both form errors and aborts

homeassistant/components/growatt_server/coordinator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import TYPE_CHECKING, Any
77

88
import growattServer
9+
from growattServer import GrowattV1ApiErrorCode
910
from requests import RequestException
1011

1112
from homeassistant.components.sensor import SensorStateClass
@@ -27,7 +28,6 @@
2728
DEFAULT_URL,
2829
DOMAIN,
2930
LOGIN_INVALID_AUTH_CODE,
30-
V1_API_ERROR_NO_PRIVILEGE,
3131
V1_DEVICE_TYPES,
3232
)
3333
from .models import GrowattRuntimeData
@@ -113,7 +113,7 @@ def _sync_fetch_device_list(self) -> None:
113113
if device.get("type") in V1_DEVICE_TYPES
114114
]
115115
except growattServer.GrowattV1ApiError as err:
116-
if err.error_code == V1_API_ERROR_NO_PRIVILEGE:
116+
if err.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
117117
raise ConfigEntryAuthFailed(
118118
f"Authentication failed for Growatt API: {err.error_msg or str(err)}"
119119
) from err
@@ -179,7 +179,7 @@ def _sync_update_data(self) -> dict[str, Any]:
179179
try:
180180
total_info = self.api.plant_energy_overview(self.plant_id)
181181
except growattServer.GrowattV1ApiError as err:
182-
if err.error_code == V1_API_ERROR_NO_PRIVILEGE:
182+
if err.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
183183
raise ConfigEntryAuthFailed(
184184
"Authentication failed for Growatt API:"
185185
f" {err.error_msg or str(err)}"
@@ -212,7 +212,7 @@ def _sync_update_data(self) -> dict[str, Any]:
212212
min_settings = self.api.min_settings(self.device_id)
213213
min_energy = self.api.min_energy(self.device_id)
214214
except growattServer.GrowattV1ApiError as err:
215-
if err.error_code == V1_API_ERROR_NO_PRIVILEGE:
215+
if err.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
216216
raise ConfigEntryAuthFailed(
217217
"Authentication failed for Growatt API:"
218218
f" {err.error_msg or str(err)}"
@@ -240,7 +240,7 @@ def _sync_update_data(self) -> dict[str, Any]:
240240
sph_detail = self.api.sph_detail(self.device_id)
241241
sph_energy = self.api.sph_energy(self.device_id)
242242
except growattServer.GrowattV1ApiError as err:
243-
if err.error_code == V1_API_ERROR_NO_PRIVILEGE:
243+
if err.error_code == GrowattV1ApiErrorCode.NO_PRIVILEGE:
244244
raise ConfigEntryAuthFailed(
245245
"Authentication failed for Growatt API:"
246246
f" {err.error_msg or str(err)}"

tests/components/growatt_server/test_config_flow.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from unittest.mock import MagicMock
77

88
import growattServer
9+
from growattServer import GrowattV1ApiErrorCode
910
import pytest
1011
import requests
1112
import voluptuous as vol
@@ -23,8 +24,6 @@
2324
ERROR_INVALID_AUTH,
2425
LOGIN_INVALID_AUTH_CODE,
2526
SERVER_URLS_NAMES,
26-
V1_API_ERROR_NO_PRIVILEGE,
27-
V1_API_ERROR_RATE_LIMITED,
2827
)
2928
from homeassistant.const import (
3029
CONF_NAME,
@@ -355,8 +354,8 @@ async def test_password_auth_multiple_plants(
355354
@pytest.mark.parametrize(
356355
("error_code", "expected_error"),
357356
[
358-
(V1_API_ERROR_NO_PRIVILEGE, ERROR_INVALID_AUTH),
359-
(V1_API_ERROR_RATE_LIMITED, ERROR_CANNOT_CONNECT),
357+
(GrowattV1ApiErrorCode.NO_PRIVILEGE, ERROR_INVALID_AUTH),
358+
(GrowattV1ApiErrorCode.RATE_LIMITED, ERROR_CANNOT_CONNECT),
360359
],
361360
)
362361
@pytest.mark.usefixtures("mock_setup_entry")

tests/components/growatt_server/test_init.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from freezegun.api import FrozenDateTimeFactory
77
import growattServer
8+
from growattServer import GrowattV1ApiErrorCode
89
import pytest
910
import requests
1011
from syrupy.assertion import SnapshotAssertion
@@ -19,8 +20,6 @@
1920
DEVICE_SCAN_INTERVAL,
2021
DOMAIN,
2122
LOGIN_INVALID_AUTH_CODE,
22-
V1_API_ERROR_RATE_LIMITED,
23-
V1_API_ERROR_WRONG_DOMAIN,
2423
)
2524
from homeassistant.config_entries import ConfigEntryState
2625
from homeassistant.const import (
@@ -70,7 +69,7 @@ async def test_device_info(
7069
(
7170
growattServer.GrowattV1ApiError(
7271
message="API Error",
73-
error_code=V1_API_ERROR_WRONG_DOMAIN,
72+
error_code=GrowattV1ApiErrorCode.WRONG_DOMAIN,
7473
error_msg="Invalid JSON",
7574
),
7675
ConfigEntryState.SETUP_ERROR,
@@ -82,7 +81,7 @@ async def test_device_info(
8281
(
8382
growattServer.GrowattV1ApiError(
8483
message="Rate limited",
85-
error_code=V1_API_ERROR_RATE_LIMITED,
84+
error_code=GrowattV1ApiErrorCode.RATE_LIMITED,
8685
error_msg="Access frequency limit",
8786
),
8887
ConfigEntryState.SETUP_RETRY,

0 commit comments

Comments
 (0)