mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Raise a Teslemetry repair when Tesla reports missing scopes (#193059)
This commit is contained in:
@@ -10,6 +10,7 @@ from typing import TYPE_CHECKING, Any, override
|
||||
|
||||
from tesla_fleet_api.const import VehicleDataEndpoint
|
||||
from tesla_fleet_api.exceptions import (
|
||||
Forbidden,
|
||||
GatewayTimeout,
|
||||
InsufficientCredits,
|
||||
InvalidResponse,
|
||||
@@ -84,6 +85,11 @@ WALL_CONNECTOR_TOTALS = "totals"
|
||||
# since entities and calendars read them as whole structures.
|
||||
TARIFF_SKIP_KEYS = ["daily_charges", "demand_charges", "energy_charges", "seasons"]
|
||||
|
||||
# Teslemetry's reauth keeps a valid Tesla grant, so only a fresh Tesla login
|
||||
# that re-asks for declined scopes can restore them.
|
||||
ISSUE_MISSING_SCOPES = "missing_scopes"
|
||||
MISSING_SCOPES_URL = "https://teslemetry.com/login?prompt_missing_scopes=1"
|
||||
|
||||
# Insufficient credits will not resolve themselves quickly, so back off polling
|
||||
# instead of hammering the API at the coordinator's normal interval.
|
||||
INSUFFICIENT_CREDITS_RETRY_AFTER = timedelta(hours=1).total_seconds()
|
||||
@@ -184,6 +190,21 @@ class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
translation_key="update_failed_insufficient_credits",
|
||||
retry_after=INSUFFICIENT_CREDITS_RETRY_AFTER,
|
||||
) from e
|
||||
except Forbidden as e:
|
||||
ir.async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
f"{ISSUE_MISSING_SCOPES}_{self.config_entry.entry_id}",
|
||||
config_entry_id=self.config_entry.entry_id,
|
||||
is_fixable=False,
|
||||
severity=ir.IssueSeverity.ERROR,
|
||||
translation_key=ISSUE_MISSING_SCOPES,
|
||||
learn_more_url=MISSING_SCOPES_URL,
|
||||
)
|
||||
raise UpdateFailed(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="update_failed_missing_scopes",
|
||||
) from e
|
||||
except RETRY_EXCEPTIONS as e:
|
||||
raise UpdateFailed(
|
||||
translation_domain=DOMAIN,
|
||||
@@ -198,6 +219,9 @@ class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
translation_placeholders={"message": e.message},
|
||||
) from e
|
||||
|
||||
ir.async_delete_issue(
|
||||
self.hass, DOMAIN, f"{ISSUE_MISSING_SCOPES}_{self.config_entry.entry_id}"
|
||||
)
|
||||
data = flatten(data)
|
||||
if version := data.get("vehicle_state_car_version"):
|
||||
# Consume firmware opportunistically rather than through a listener
|
||||
|
||||
@@ -1393,6 +1393,9 @@
|
||||
"update_failed_insufficient_credits": {
|
||||
"message": "Teslemetry account has insufficient command credits, pausing updates until credits are added"
|
||||
},
|
||||
"update_failed_missing_scopes": {
|
||||
"message": "Tesla authorization is missing the permissions to read vehicle data"
|
||||
},
|
||||
"wake_up_failed": {
|
||||
"message": "Failed to wake up vehicle: {message}"
|
||||
},
|
||||
@@ -1441,6 +1444,10 @@
|
||||
},
|
||||
"title": "Pair the virtual key for {vehicle}"
|
||||
},
|
||||
"missing_scopes": {
|
||||
"description": "Tesla is refusing to share vehicle data with Teslemetry because your Tesla authorization is missing the required permissions. Select **Learn more** to sign in with Tesla again and grant the missing permissions, including Vehicle Information, then reload the integration.",
|
||||
"title": "Tesla authorization is missing permissions"
|
||||
},
|
||||
"streaming_toggle": {
|
||||
"fix_flow": {
|
||||
"error": {
|
||||
|
||||
@@ -183,6 +183,7 @@ CONNECTION_ERRORS = [
|
||||
VEHICLE_ERRORS = [
|
||||
*ERRORS,
|
||||
(InsufficientCredits, ConfigEntryState.SETUP_RETRY),
|
||||
(Forbidden, ConfigEntryState.SETUP_RETRY),
|
||||
]
|
||||
|
||||
|
||||
@@ -1259,6 +1260,39 @@ async def test_insufficient_credits_backs_off_polling(
|
||||
assert coordinator.last_exception.retry_after == INSUFFICIENT_CREDITS_RETRY_AFTER
|
||||
|
||||
|
||||
async def test_vehicle_data_missing_scopes_repair(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
mock_vehicle_data: AsyncMock,
|
||||
mock_legacy: AsyncMock,
|
||||
) -> None:
|
||||
"""Missing scopes raise a repair that clears once vehicle data is readable."""
|
||||
entry = await setup_platform(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
issue_id = f"missing_scopes_{entry.entry_id}"
|
||||
|
||||
mock_vehicle_data.side_effect = Forbidden
|
||||
freezer.tick(VEHICLE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
assert hass.states.get("sensor.test_battery_level").state == STATE_UNAVAILABLE
|
||||
issue = issue_registry.async_get_issue(DOMAIN, issue_id)
|
||||
assert issue is not None
|
||||
assert (
|
||||
issue.learn_more_url == "https://teslemetry.com/login?prompt_missing_scopes=1"
|
||||
)
|
||||
|
||||
mock_vehicle_data.side_effect = None
|
||||
freezer.tick(VEHICLE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert issue_registry.async_get_issue(DOMAIN, issue_id) is None
|
||||
|
||||
|
||||
def _oauth_session(hass: HomeAssistant, entry: MockConfigEntry) -> OAuth2Session:
|
||||
"""Build an OAuth2Session for directly exercising _get_access_token."""
|
||||
return OAuth2Session(hass, entry, TeslemetryImplementation(hass, DOMAIN, CLIENT_ID))
|
||||
|
||||
Reference in New Issue
Block a user