-
-
Notifications
You must be signed in to change notification settings - Fork 37.9k
Improve netgear_lte logging when unconnected #18163
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 2 commits into
home-assistant:dev
from
amelchio:netgear-retry-log-once
Nov 4, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,11 +39,13 @@ | |
| class ModemData: | ||
| """Class for modem state.""" | ||
|
|
||
| host = attr.ib() | ||
| modem = attr.ib() | ||
|
|
||
| serial_number = attr.ib(init=False, default=None) | ||
| unread_count = attr.ib(init=False, default=None) | ||
| usage = attr.ib(init=False, default=None) | ||
| connected = attr.ib(init=False, default=True) | ||
|
|
||
| @Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
| async def async_update(self): | ||
|
|
@@ -54,7 +56,13 @@ async def async_update(self): | |
| self.serial_number = information.serial_number | ||
| self.unread_count = sum(1 for x in information.sms if x.unread) | ||
| self.usage = information.usage | ||
| if not self.connected: | ||
| _LOGGER.warning("Connected to %s", self.host) | ||
| self.connected = True | ||
| except eternalegypt.Error: | ||
| if self.connected: | ||
| _LOGGER.warning("Lost connection to %s", self.host) | ||
| self.connected = False | ||
| self.unread_count = None | ||
| self.usage = None | ||
|
|
||
|
|
@@ -90,34 +98,59 @@ async def async_setup(hass, config): | |
| return True | ||
|
|
||
|
|
||
| async def _setup_lte(hass, lte_config, delay=0): | ||
| async def _setup_lte(hass, lte_config): | ||
| """Set up a Netgear LTE modem.""" | ||
| import eternalegypt | ||
|
|
||
| if delay: | ||
| await asyncio.sleep(delay) | ||
|
|
||
| host = lte_config[CONF_HOST] | ||
| password = lte_config[CONF_PASSWORD] | ||
|
|
||
| websession = hass.data[DATA_KEY].websession | ||
|
|
||
| modem = eternalegypt.Modem(hostname=host, websession=websession) | ||
|
|
||
| modem_data = ModemData(host, modem) | ||
|
|
||
| try: | ||
| await modem.login(password=password) | ||
| await _login(hass, modem_data, password) | ||
| except eternalegypt.Error: | ||
| delay = max(15, min(2*delay, 300)) | ||
| _LOGGER.warning("Retrying %s in %d seconds", host, delay) | ||
| hass.loop.create_task(_setup_lte(hass, lte_config, delay)) | ||
| return | ||
| retry_task = hass.loop.create_task( | ||
| _retry_login(hass, modem_data, password)) | ||
|
|
||
| async def cleanup_retry(event): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a callback.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I changed that. |
||
| """Clean up retry task resources.""" | ||
| if not retry_task.done(): | ||
| retry_task.cancel() | ||
|
|
||
| modem_data = ModemData(modem) | ||
| hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup_retry) | ||
|
|
||
|
|
||
| async def _login(hass, modem_data, password): | ||
| """Log in and complete setup.""" | ||
| await modem_data.modem.login(password=password) | ||
| await modem_data.async_update() | ||
| hass.data[DATA_KEY].modem_data[host] = modem_data | ||
| hass.data[DATA_KEY].modem_data[modem_data.host] = modem_data | ||
|
|
||
| async def cleanup(event): | ||
| """Clean up resources.""" | ||
| await modem.logout() | ||
| await modem_data.modem.logout() | ||
|
|
||
| hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup) | ||
|
|
||
|
|
||
| async def _retry_login(hass, modem_data, password): | ||
| """Sleep and retry setup.""" | ||
| import eternalegypt | ||
|
|
||
| _LOGGER.warning( | ||
| "Could not connect to %s. Will keep trying.", modem_data.host) | ||
|
|
||
| modem_data.connected = False | ||
| delay = 15 | ||
|
|
||
| while not modem_data.connected: | ||
| await asyncio.sleep(delay) | ||
|
|
||
| try: | ||
| await _login(hass, modem_data, password) | ||
| except eternalegypt.Error: | ||
| delay = min(2*delay, 300) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe info level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should match the warnings for lost connections so it is clear that the problem is resolved even if the log file filters out info level logs.
(Note that it does not print during normal startup, only after connection failures.)