-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Fix connection problems in the Broadlink integration #34670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinHjelmare
merged 13 commits into
home-assistant:dev
from
felipediel:fix_connection
May 13, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3ec4be8
Use helper functions for exception handling
felipediel 41e69a7
Create a separate class to handle communication with the device
felipediel 8894c54
Update manifest
felipediel 87b0400
Use coroutine for service setup
felipediel 2441eb5
Fix sensor update
felipediel 7a748f6
Update tests
felipediel 206ec90
Fix MP1 switch
felipediel f074a6a
Add device.py to .coveragerc
felipediel b95ab0c
Remove unnecessary blocking from test_learn_timeout
felipediel 2726532
Change access method for entries with default values
felipediel cd47c1b
Make the changes suggested by MartinHjelmare
felipediel c7cd86f
Remove dot from debug message
felipediel 49924e9
Use underscore for unused variable
felipediel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """Support for Broadlink devices.""" | ||
| from functools import partial | ||
| import logging | ||
|
|
||
| from broadlink.exceptions import ( | ||
| AuthorizationError, | ||
| BroadlinkException, | ||
| ConnectionClosedError, | ||
| DeviceOfflineError, | ||
| ) | ||
|
|
||
| from .const import DEFAULT_RETRY | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class BroadlinkDevice: | ||
| """Manages a Broadlink device.""" | ||
|
|
||
| def __init__(self, hass, api): | ||
| """Initialize the device.""" | ||
| self.hass = hass | ||
| self.api = api | ||
| self.available = None | ||
|
|
||
| async def async_connect(self): | ||
| """Connect to the device.""" | ||
| try: | ||
| await self.hass.async_add_executor_job(self.api.auth) | ||
| except BroadlinkException as err_msg: | ||
| if self.available: | ||
| self.available = False | ||
| _LOGGER.warning( | ||
| "Disconnected from device at %s: %s", self.api.host[0], err_msg | ||
| ) | ||
| return False | ||
| else: | ||
| if not self.available: | ||
| if self.available is not None: | ||
| _LOGGER.warning("Connected to device at %s", self.api.host[0]) | ||
| self.available = True | ||
| return True | ||
|
|
||
| async def async_request(self, function, *args, **kwargs): | ||
| """Send a request to the device.""" | ||
| partial_function = partial(function, *args, **kwargs) | ||
| for attempt in range(DEFAULT_RETRY): | ||
|
felipediel marked this conversation as resolved.
|
||
| try: | ||
| result = await self.hass.async_add_executor_job(partial_function) | ||
| except (AuthorizationError, ConnectionClosedError, DeviceOfflineError): | ||
| if attempt == DEFAULT_RETRY - 1 or not await self.async_connect(): | ||
| raise | ||
| else: | ||
| if not self.available: | ||
| self.available = True | ||
| _LOGGER.warning("Connected to device at %s", self.api.host[0]) | ||
| return result | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.