diff --git a/homeassistant/components/teslemetry/__init__.py b/homeassistant/components/teslemetry/__init__.py index 3ffd16c5ba5..765f345898c 100644 --- a/homeassistant/components/teslemetry/__init__.py +++ b/homeassistant/components/teslemetry/__init__.py @@ -102,7 +102,11 @@ async def _get_access_token(oauth_session: OAuth2Session) -> str: async def async_setup_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) -> bool: """Set up Teslemetry config.""" - session = async_get_clientsession(hass) + if "token" not in entry.data: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="token_data_malformed", + ) try: implementation = await async_get_config_entry_implementation(hass, entry) @@ -113,6 +117,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) - ) from err oauth_session = OAuth2Session(hass, entry, implementation) + session = async_get_clientsession(hass) + # Create API connection access_token = partial(_get_access_token, oauth_session) teslemetry = Teslemetry( diff --git a/tests/components/teslemetry/test_init.py b/tests/components/teslemetry/test_init.py index 18e8dc48039..c84653d16a3 100644 --- a/tests/components/teslemetry/test_init.py +++ b/tests/components/teslemetry/test_init.py @@ -649,6 +649,26 @@ async def test_live_status_generic_error( assert entry.state is ConfigEntryState.LOADED +async def test_missing_token_data(hass: HomeAssistant) -> None: + """Test that missing token data in config entry triggers auth failure.""" + mock_entry = MockConfigEntry( + domain=DOMAIN, + version=2, + unique_id=UNIQUE_ID, + data={ + "auth_implementation": DOMAIN, + # token is intentionally missing + }, + ) + mock_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_entry.entry_id) + await hass.async_block_till_done() + + entry = hass.config_entries.async_get_entry(mock_entry.entry_id) + assert entry is not None + assert entry.state is ConfigEntryState.SETUP_ERROR + + async def test_vehicle_streaming_version_update( hass: HomeAssistant, device_registry: dr.DeviceRegistry,