Raise exceptions for errors#356
Merged
Danielhiversen merged 5 commits intomjg59:masterfrom May 7, 2020
Merged
Conversation
Collaborator
Author
|
@Danielhiversen @mjg59 What do you think about this? |
Collaborator
Author
|
I will move to draft until I make fine adjustments in Home Assistant. But you can review the code anyway, since I don't intend to change it anymore. |
Owner
|
This looks extremely good to me. |
Danielhiversen
approved these changes
May 2, 2020
Collaborator
Danielhiversen
left a comment
There was a problem hiding this comment.
@felipediel
You are doing a great job improving this library.
Thanks
Collaborator
|
Is it still a draft? |
Collaborator
Author
|
@Danielhiversen This code is ready, but we still have to update Home Assistant to handle these exceptions. I'm gonna mark it as ready and work on the updates tonight. |
Collaborator
Author
|
Here we go. |
20 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
Things are failing silently. When we send a request and an error occurs, the device sends an error code in the response. This is valuable information for dealing with the problem. But this is being ignored in the current implementation. Errors are passing by without leaving clues.
Let's take a practical example. We need authorization to send a command. If we send a command without being authorized we will receive an authorization error (0xfff9) in the response. It's simple to deal with this error, we just need to re-authenticate and send the command again. But it is currently not possible to catch this error, so we cannot deal with it.
Here is a real case. Once @AndroVet's device closes the connection for some reason, the device object breaks silently (because it doesn't know it has been logged out). @AndroVet is being logged out due to the large number of connections, probably. We are not detecting this error and his switches are failing silently. We should inform the integrations when an error occurs so they can deal with it.
Proposed changes
My suggestion is to raise an exception whenever the device informs an error. In this way, integrations will have material to decide how to respond. I found the error codes in the official documentation. I compared some with error codes that I captured in a controlled environment. They match.
These are the changes I propose:
Breaking changes
These are breaking changes because we are gonna start raising exceptions, we will no longer fail silently as before. Some minor changes will be needed in the integrations. I commit to bringing these changes to Home Assistant.