Skip to content

Commit b6ebfc8

Browse files
committed
Add API test script
1 parent eeaa3cb commit b6ebfc8

4 files changed

Lines changed: 85 additions & 5 deletions

File tree

custom_components/tunnelflight/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2-
from homeassistant.helpers import config_validation as cv
32
from homeassistant.config_entries import ConfigEntry
43
from homeassistant.core import HomeAssistant
4+
from homeassistant.exceptions import ConfigEntryNotReady
5+
from homeassistant.helpers import config_validation as cv
6+
from homeassistant.helpers.aiohttp_client import async_get_clientsession
57

8+
from .api import TunnelflightApi
69
from .const import DOMAIN
710
from .logbook_service import async_setup_services, async_unload_services
811

@@ -36,7 +39,21 @@ async def async_setup(hass, config):
3639
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3740
"""Set up IBA Tunnelflight from a config entry."""
3841
hass.data.setdefault(DOMAIN, {})
39-
hass.data[DOMAIN][entry.entry_id] = entry.data
42+
43+
session = async_get_clientsession(hass)
44+
api = TunnelflightApi(
45+
entry.data.get("username"), entry.data.get("password"), session
46+
)
47+
48+
try:
49+
login_success = await api.login()
50+
except Exception as err:
51+
raise ConfigEntryNotReady("Unable to connect to Tunnelflight API") from err
52+
53+
if not login_success:
54+
raise ConfigEntryNotReady("Unable to authenticate with Tunnelflight API")
55+
56+
hass.data[DOMAIN][entry.entry_id] = {"api": api, **entry.data}
4057

4158
# Set up platforms
4259
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

custom_components/tunnelflight/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/B-Hartley/tunnelflight/issues",
1010
"requirements": [],
11-
"version": "1.3.5"
11+
"version": "1.3.6"
1212
}

custom_components/tunnelflight/sensor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ async def async_setup_entry(
3636
password = config[CONF_PASSWORD]
3737
name = config.get(CONF_NAME, DEFAULT_NAME)
3838

39-
session = async_get_clientsession(hass)
40-
api = TunnelflightApi(username, password, session)
39+
stored = hass.data.get(DOMAIN, {}).get(entry.entry_id, {})
40+
api = stored.get("api")
41+
if not api:
42+
session = async_get_clientsession(hass)
43+
api = TunnelflightApi(username, password, session)
4144

4245
# Create a data coordinator to handle updates
4346
coordinator = TunnelflightCoordinator(hass, api)

test_api_calls.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
param(
2+
[string]$Username,
3+
[string]$Password,
4+
[string]$LogFile = "tunnelflight_api_test.log"
5+
)
6+
7+
function Write-Log {
8+
param([string]$Message)
9+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
10+
"$timestamp`t$Message" | Out-File -FilePath $LogFile -Append
11+
}
12+
13+
$headers = @{
14+
"User-Agent" = "Mozilla/5.0 (Windows NT; Win64; x64)"
15+
"Content-Type" = "application/json"
16+
}
17+
18+
try {
19+
Write-Log "Starting login request"
20+
$loginBody = @{
21+
username = $Username.ToLower()
22+
password = $Password
23+
passcode = ""
24+
enable2fa = $false
25+
checkTwoFactor = $true
26+
passcodeOption = "email"
27+
} | ConvertTo-Json
28+
$loginResponse = Invoke-RestMethod -Uri "https://www.tunnelflight.com/login" -Method Post -Headers $headers -Body $loginBody
29+
Write-Log "Login response: $($loginResponse | ConvertTo-Json -Compress)"
30+
$token = $loginResponse.token
31+
} catch {
32+
Write-Log "Login error: $_"
33+
return
34+
}
35+
36+
if (-not $token) {
37+
Write-Log "No token received. Aborting further tests."
38+
return
39+
}
40+
41+
$authHeaders = $headers.Clone()
42+
$authHeaders["Authorization"] = "Bearer $token"
43+
44+
$endpoints = @(
45+
"https://www.tunnelflight.com/user/module-type/flyer-card/",
46+
"https://www.tunnelflight.com/user/module-type/flyer-charts/",
47+
"https://www.tunnelflight.com/account/logbook/tunnels/"
48+
)
49+
50+
foreach ($endpoint in $endpoints) {
51+
try {
52+
Write-Log "Requesting $endpoint"
53+
$response = Invoke-RestMethod -Uri $endpoint -Method Get -Headers $authHeaders
54+
Write-Log "Response from $endpoint: $($response | ConvertTo-Json -Compress)"
55+
} catch {
56+
Write-Log "Error calling $endpoint: $_"
57+
}
58+
}
59+
60+
Write-Log "API test script finished"

0 commit comments

Comments
 (0)