-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Improve error handling for Powerwall #34580
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
Merged
Changes from all commits
Commits
Show all changes
5 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 |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import logging | ||
|
|
||
| import requests | ||
| from tesla_powerwall import APIError, Powerwall, PowerwallUnreachableError | ||
| from tesla_powerwall import APIChangedError, Powerwall, PowerwallUnreachableError | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry | ||
|
|
@@ -13,10 +13,11 @@ | |
| from homeassistant.exceptions import ConfigEntryNotReady | ||
| from homeassistant.helpers import entity_registry | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
| from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
|
||
| from .const import ( | ||
| DOMAIN, | ||
| POWERWALL_API_CHANGED, | ||
| POWERWALL_API_CHARGE, | ||
| POWERWALL_API_DEVICE_TYPE, | ||
| POWERWALL_API_GRID_STATUS, | ||
|
|
@@ -64,7 +65,7 @@ async def _migrate_old_unique_ids(hass, entry_id, powerwall_data): | |
| @callback | ||
| def _async_migrator(entity_entry: entity_registry.RegistryEntry): | ||
| parts = entity_entry.unique_id.split("_") | ||
| # Check if the unique_id starts with the serial_numbers of the powerwakks | ||
| # Check if the unique_id starts with the serial_numbers of the powerwalls | ||
| if parts[0 : len(serial_numbers)] != serial_numbers: | ||
| # The old unique_id ended with the nomianal_system_engery_kWh so we can use that | ||
| # to find the old base unique_id and extract the device_suffix. | ||
|
|
@@ -87,6 +88,17 @@ def _async_migrator(entity_entry: entity_registry.RegistryEntry): | |
| await entity_registry.async_migrate_entries(hass, entry_id, _async_migrator) | ||
|
|
||
|
|
||
| async def _async_handle_api_changed_error(hass: HomeAssistant, error: APIChangedError): | ||
| # The error might include some important information about what exactly changed. | ||
| _LOGGER.error(str(error)) | ||
| hass.components.persistent_notification.async_create( | ||
| "It seems like your powerwall uses an unsupported version. " | ||
| "Please update the software of your powerwall or if it is" | ||
| "already the newest consider reporting this issue.\nSee logs for more information", | ||
| title="Unknown powerwall software version", | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
| """Set up Tesla Powerwall from a config entry.""" | ||
|
|
||
|
|
@@ -97,16 +109,37 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | |
| power_wall = Powerwall(entry.data[CONF_IP_ADDRESS], http_session=http_session) | ||
| try: | ||
| await hass.async_add_executor_job(power_wall.detect_and_pin_version) | ||
| await hass.async_add_executor_job(_fetch_powerwall_data, power_wall) | ||
| powerwall_data = await hass.async_add_executor_job(call_base_info, power_wall) | ||
| except (PowerwallUnreachableError, APIError, ConnectionError): | ||
| except PowerwallUnreachableError: | ||
| http_session.close() | ||
| raise ConfigEntryNotReady | ||
| except APIChangedError as err: | ||
| http_session.close() | ||
| await _async_handle_api_changed_error(hass, err) | ||
| return False | ||
|
|
||
| await _migrate_old_unique_ids(hass, entry_id, powerwall_data) | ||
|
|
||
| async def async_update_data(): | ||
| """Fetch data from API endpoint.""" | ||
| return await hass.async_add_executor_job(_fetch_powerwall_data, power_wall) | ||
| # Check if we had an error before | ||
| _LOGGER.info("Checking if update failed") | ||
|
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 should be debug level. |
||
| if not hass.data[DOMAIN][entry.entry_id][POWERWALL_API_CHANGED]: | ||
| _LOGGER.info("Updating data") | ||
|
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. Here too. |
||
| try: | ||
| return await hass.async_add_executor_job( | ||
| _fetch_powerwall_data, power_wall | ||
| ) | ||
| except PowerwallUnreachableError: | ||
| raise UpdateFailed("Unable to fetch data from powerwall") | ||
| except APIChangedError as err: | ||
| await _async_handle_api_changed_error(hass, err) | ||
| hass.data[DOMAIN][entry.entry_id][POWERWALL_API_CHANGED] = True | ||
| # Returns the cached data. This data can also be None | ||
| return hass.data[DOMAIN][entry.entry_id][POWERWALL_COORDINATOR].data | ||
| else: | ||
| return hass.data[DOMAIN][entry.entry_id][POWERWALL_COORDINATOR].data | ||
|
|
||
| coordinator = DataUpdateCoordinator( | ||
| hass, | ||
|
|
@@ -122,6 +155,7 @@ async def async_update_data(): | |
| POWERWALL_OBJECT: power_wall, | ||
| POWERWALL_COORDINATOR: coordinator, | ||
| POWERWALL_HTTP_SESSION: http_session, | ||
| POWERWALL_API_CHANGED: False, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
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
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
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.