@@ -25,7 +25,7 @@ import type { HaWebSocket as LibHaWebSocket } from 'home-assistant-js-websocket/
2525import * as messages from 'home-assistant-js-websocket/dist/messages.js'
2626import { atLeastHaVersion } from 'home-assistant-js-websocket/dist/util.js'
2727import WebSocket from 'ws'
28- import { hassUrl , hassToken } from './const.js'
28+ import { hassUrl , hassToken , SERVER_PORT } from './const.js'
2929import { loadPresets } from './devices.js'
3030import type { PresetsConfig } from './types/domain.js'
3131import { uiLogger } from './lib/logger.js'
@@ -85,6 +85,8 @@ interface UIConfig {
8585 connectionStatus : string
8686 /** Timestamp when HA data was last fetched (for cache age display) */
8787 cachedAt : number | null
88+ /** Server port for constructing Fetch URLs (10000 for add-on, configurable for standalone) */
89+ serverPort : number
8890}
8991
9092// =============================================================================
@@ -134,7 +136,7 @@ function extractHostname(url: string): string | null {
134136 * Returns detailed diagnostic info for error logging
135137 */
136138async function checkDnsResolution (
137- hostname : string
139+ hostname : string ,
138140) : Promise < { resolved : boolean ; ip ?: string ; error ?: string } > {
139141 try {
140142 const result = await lookup ( hostname )
@@ -160,7 +162,7 @@ async function checkDnsResolution(
160162 * than the browser's WebSocket, but they're compatible enough for our use case.
161163 */
162164function createSocketWithSslBypass (
163- options : ConnectionOptions
165+ options : ConnectionOptions ,
164166) : Promise < LibHaWebSocket > {
165167 if ( ! options . auth ) {
166168 throw new Error ( 'Auth is required for WebSocket connection' )
@@ -177,7 +179,7 @@ function createSocketWithSslBypass(
177179 function connect (
178180 triesLeft : number ,
179181 promResolve : ( socket : LibHaWebSocket ) => void ,
180- promReject : ( err : unknown ) => void
182+ promReject : ( err : unknown ) => void ,
181183 ) {
182184 log . debug `WebSocket connection attempt (retries left: ${ triesLeft } )`
183185
@@ -193,7 +195,7 @@ function createSocketWithSslBypass(
193195 let invalidAuth = false
194196
195197 const closeMessage = (
196- event ?: WebSocket . CloseEvent | WebSocket . ErrorEvent
198+ event ?: WebSocket . CloseEvent | WebSocket . ErrorEvent ,
197199 ) => {
198200 socket . removeEventListener ( 'close' , closeMessage )
199201
@@ -306,7 +308,7 @@ function createSocketWithSslBypass(
306308function sendHtmlResponse (
307309 response : ServerResponse ,
308310 html : string ,
309- statusCode : number = 200
311+ statusCode : number = 200 ,
310312) : void {
311313 response . writeHead ( statusCode , {
312314 'Content-Type' : 'text/html' ,
@@ -328,12 +330,12 @@ const HA_CONNECTION_TIMEOUT = 5000
328330function withTimeout < T > (
329331 promise : Promise < T > ,
330332 ms : number ,
331- message : string
333+ message : string ,
332334) : Promise < T > {
333335 return Promise . race ( [
334336 promise ,
335337 new Promise < T > ( ( _ , reject ) =>
336- setTimeout ( ( ) => reject ( new Error ( message ) ) , ms )
338+ setTimeout ( ( ) => reject ( new Error ( message ) ) , ms ) ,
337339 ) ,
338340 ] )
339341}
@@ -399,7 +401,7 @@ async function fetchHomeAssistantData(): Promise<HomeAssistantData> {
399401 createSocket : createSocketWithSslBypass ,
400402 } ) ,
401403 HA_CONNECTION_TIMEOUT ,
402- `HA connection timeout after ${ HA_CONNECTION_TIMEOUT } ms`
404+ `HA connection timeout after ${ HA_CONNECTION_TIMEOUT } ms` ,
403405 )
404406
405407 log . debug `WebSocket connected, fetching HA data...`
@@ -491,7 +493,7 @@ async function getCachedOrFetch(forceRefresh: boolean): Promise<{
491493 // Return cached data if available and not forcing refresh
492494 if ( cachedHassData && ! forceRefresh ) {
493495 log . debug `Using cached HA data (age: ${ Math . round (
494- ( Date . now ( ) - cacheTimestamp ) / 1000
496+ ( Date . now ( ) - cacheTimestamp ) / 1000 ,
495497 ) } s)`
496498 return { data : cachedHassData , cachedAt : cacheTimestamp }
497499 }
@@ -520,7 +522,7 @@ async function getCachedOrFetch(forceRefresh: boolean): Promise<{
520522 */
521523export async function handleUIRequest (
522524 response : ServerResponse ,
523- requestUrl ?: URL
525+ requestUrl ?: URL ,
524526) : Promise < void > {
525527 try {
526528 // Check for forced refresh via query param
@@ -581,6 +583,7 @@ export async function handleUIRequest(
581583 tokenPreview,
582584 connectionStatus,
583585 cachedAt,
586+ serverPort : SERVER_PORT ,
584587 }
585588
586589 const htmlPath = join ( HTML_DIR , 'index.html' )
0 commit comments