Skip to content

Commit

Permalink
Change API login default for HA integration
Browse files Browse the repository at this point in the history
Improve error handling on login
  • Loading branch information
Alberto Geniola committed May 29, 2022
1 parent aa31fe2 commit 2084b1d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class Constants {
public static final String LOG_TAG = "Custom Meross Pairer";
public static final String MEROSS_CLOUD_EP = "https://iot.meross.com";

public static final String HA_ADDON_DEFAULT_EMAIL = "meross@local";
public static final String HA_ADDON_DEFAULT_PASSWORD = "Changeme!";
public static final String HA_ADDON_DEFAULT_EMAIL = "";
public static final String HA_ADDON_DEFAULT_PASSWORD = "";

public static final long PAIRING_VERIFY_TIMEOUT_MILLISECONDS = 30000;
public static final long MDNS_DISCOVERY_TIMEOUT_MILLISEOONDS = 5000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ private abstract static class HttpClientTask<T> extends AsyncTask<MerossHttpClie

public interface Callback<T> {
public void onSuccess(T result);
public void onFailure(Exception result);
public void onFailure(Exception exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.albertogeniola.merossconf.model.HttpClientManager;
import com.albertogeniola.merossconf.ui.MainActivityViewModel;
import com.albertogeniola.merosslib.model.http.ApiCredentials;
import com.albertogeniola.merosslib.model.http.ErrorCodes;
import com.albertogeniola.merosslib.model.http.exceptions.HttpApiException;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.progressindicator.CircularProgressIndicator;
import com.google.android.material.progressindicator.CircularProgressIndicatorSpec;
Expand Down Expand Up @@ -160,7 +162,7 @@ public void onClick(View view) {
mRequiresWifiLocation = args.getBoolean(Args.REQUIRES_WIFI_LOCATION, false);
mDiscoveryEnabled = args.getBoolean(Args.ENABLE_BROKER_DISCOVERY, false);

mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, ""));
mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, "http://homeassistant.local:2002"));
mHttpUsernameEditText.setText(args.getString(Args.HTTP_BROKER_EMAIL, ""));
mHttpPasswordEditText.setText(args.getString(Args.HTTP_BROKER_PASSWORD, ""));
mDiscoveryButton.setEnabled(mDiscoveryEnabled);
Expand Down Expand Up @@ -302,9 +304,13 @@ public void onSuccess(ApiCredentials creds) {
}

@Override
public void onFailure(Exception result) {
public void onFailure(Exception exception) {
dialog.dismiss();
Snackbar.make(mLoginButton, "An error while executing the request.", Snackbar.LENGTH_LONG).show();
String errorMessage = "An error occurred while executing the request.";
if (exception instanceof HttpApiException) {
errorMessage = ((HttpApiException)exception).getErrorMessage();
}
Snackbar.make(mLoginButton,errorMessage , Snackbar.LENGTH_LONG).show();
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/fragment_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"
/>
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
Expand All @@ -68,6 +67,7 @@
android:layout_height="wrap_content"
android:ems="10"
android:hint="Username / email"
android:autofillHints="emailAddress"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -91,6 +91,7 @@
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:autofillHints="password"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,34 @@ public int getCode()
return this.value;
}

public String getMessage() {
if (value==CODE_NO_ERROR.value)
return "All OK";
else if (value==CODE_MISSING_PASSWORD.value)
return "Missing password";
else if (value==CODE_UNEXISTING_ACCOUNT.value)
return "Account does not exist";
else if (value==CODE_DISABLED_OR_DELETED_ACCOUNT.value)
return "Account has been disabled or deleted";
else if (value==CODE_WRONG_CREDENTIALS.value)
return "Bad/wrong credentials";
else if (value==CODE_INVALID_EMAIL.value)
return "Invalid email address";
else if (value==CODE_BAD_PASSWORD_FORMAT.value)
return "Invalid password format";
else if (value==CODE_WRONG_EMAIL.value)
return "Wrong email";
else if (value==CODE_TOKEN_INVALID.value)
return "The token is invalid";
else if (value==CODE_TOKEN_EXPIRED.value)
return "Token has expired";
else if (value==CODE_TOKEN_ERROR.value)
return "Token error";
else if (value==CODE_TOO_MANY_TOKENS.value)
return "Too many tokens have been released.";
else if (value==CODE_GENERIC_ERROR.value)
return "Generic error";
else
return "Unknown error code";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public HttpApiException(ErrorCodes code, String message) {
super(message);
this.code = code;
}

public String getErrorMessage() {
return code.getMessage();
}
}

0 comments on commit 2084b1d

Please sign in to comment.