Fix Growatt login invalid auth response (#57071)

This commit is contained in:
indykoning 2021-10-05 21:31:23 +02:00 committed by GitHub
parent 1a7a4c52f1
commit f76cb12945
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 5 deletions

View file

@ -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"]

View file

@ -16,3 +16,5 @@ DEFAULT_URL = SERVER_URLS[0]
DOMAIN = "growatt_server"
PLATFORMS = ["sensor"]
LOGIN_INVALID_AUTH_CODE = "502"

View file

@ -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"]

View file

@ -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