diff --git a/homeassistant/components/growatt_server/config_flow.py b/homeassistant/components/growatt_server/config_flow.py index c4a97a81f0a..11f082f1eab 100644 --- a/homeassistant/components/growatt_server/config_flow.py +++ b/homeassistant/components/growatt_server/config_flow.py @@ -6,7 +6,13 @@ from homeassistant import config_entries from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_URL, CONF_USERNAME from homeassistant.core import callback -from .const import CONF_PLANT_ID, DEFAULT_URL, DOMAIN, SERVER_URLS +from .const import ( + CONF_PLANT_ID, + DEFAULT_URL, + DOMAIN, + LOGIN_INVALID_AUTH_CODE, + SERVER_URLS, +) class GrowattServerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): @@ -45,7 +51,10 @@ class GrowattServerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self.api.login, user_input[CONF_USERNAME], user_input[CONF_PASSWORD] ) - if not login_response["success"] and login_response["errCode"] == "102": + if ( + not login_response["success"] + and login_response["msg"] == LOGIN_INVALID_AUTH_CODE + ): return self._async_show_user_form({"base": "invalid_auth"}) self.user_id = login_response["user"]["id"] diff --git a/homeassistant/components/growatt_server/const.py b/homeassistant/components/growatt_server/const.py index e0297de5eff..5425e26c806 100644 --- a/homeassistant/components/growatt_server/const.py +++ b/homeassistant/components/growatt_server/const.py @@ -16,3 +16,5 @@ DEFAULT_URL = SERVER_URLS[0] DOMAIN = "growatt_server" PLATFORMS = ["sensor"] + +LOGIN_INVALID_AUTH_CODE = "502" diff --git a/homeassistant/components/growatt_server/sensor.py b/homeassistant/components/growatt_server/sensor.py index 9f0fa509105..804d4157543 100644 --- a/homeassistant/components/growatt_server/sensor.py +++ b/homeassistant/components/growatt_server/sensor.py @@ -37,7 +37,7 @@ from homeassistant.const import ( ) from homeassistant.util import Throttle, dt -from .const import CONF_PLANT_ID, DEFAULT_PLANT_ID, DEFAULT_URL +from .const import CONF_PLANT_ID, DEFAULT_PLANT_ID, DEFAULT_URL, LOGIN_INVALID_AUTH_CODE _LOGGER = logging.getLogger(__name__) @@ -876,7 +876,10 @@ def get_device_list(api, config): # Log in to api and fetch first plant if no plant id is defined. login_response = api.login(config[CONF_USERNAME], config[CONF_PASSWORD]) - if not login_response["success"] and login_response["errCode"] == "102": + if ( + not login_response["success"] + and login_response["msg"] == LOGIN_INVALID_AUTH_CODE + ): _LOGGER.error("Username, Password or URL may be incorrect!") return user_id = login_response["user"]["id"] diff --git a/tests/components/growatt_server/test_config_flow.py b/tests/components/growatt_server/test_config_flow.py index db46ed36911..ba52e09296c 100644 --- a/tests/components/growatt_server/test_config_flow.py +++ b/tests/components/growatt_server/test_config_flow.py @@ -7,6 +7,7 @@ from homeassistant.components.growatt_server.const import ( CONF_PLANT_ID, DEFAULT_URL, DOMAIN, + LOGIN_INVALID_AUTH_CODE, ) from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME @@ -61,7 +62,7 @@ async def test_incorrect_login(hass): with patch( "growattServer.GrowattApi.login", - return_value={"errCode": "102", "success": False}, + return_value={"msg": LOGIN_INVALID_AUTH_CODE, "success": False}, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], FIXTURE_USER_INPUT