Skip to content

Commit 66f9ac4

Browse files
committed
Refactor Home Assistant connection logic for better validation
Ensure `serverUrl` and `accessToken` are present before attempting to connect to Home Assistant. Updated `GlobalSettings` model to make these properties optional and added logging for missing configuration.
1 parent 7d7af11 commit 66f9ac4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/models/settings/globalSettings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type GlobalSettings = {
2-
serverUrl: string
3-
accessToken: string
2+
serverUrl?: string
3+
accessToken?: string
44
displayConfiguration?: {
55
url: string
66
urlOverride: string

src/plugin.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ await streamDeck.connect()
2626
// Reconnect to Home Assistant when the global settings are changed.
2727
await streamDeck.settings.getGlobalSettings<GlobalSettings>().then(async (settings) => {
2828
// Convert the old WebSocket URL format to the new format required by the Home Assistant library.
29-
if (settings.serverUrl.startsWith('wss://') || settings.serverUrl.startsWith('ws://')) {
29+
if (settings.serverUrl && (settings.serverUrl.startsWith('wss://') || settings.serverUrl.startsWith('ws://'))) {
3030
settings.serverUrl = settings.serverUrl
3131
.replace('ws://', 'http://')
3232
.replace('wss://', 'https://')
3333
.replace('/api/websocket', '')
3434
await streamDeck.settings.setGlobalSettings(settings)
3535
}
3636

37-
await homeAssistant.connect(settings.serverUrl, settings.accessToken)
37+
if (settings.serverUrl && settings.accessToken) {
38+
streamDeck.logger.info('Connecting to Home Assistant...')
39+
await homeAssistant.connect(settings.serverUrl, settings.accessToken)
40+
} else {
41+
streamDeck.logger.info('No Home Assistant server URL or access token set, not connecting to Home Assistant.')
42+
}
3843
await entityConfigFactory.setDisplayConfigurationUrl(settings.displayConfiguration?.urlOverride)
3944
})
4045

0 commit comments

Comments
 (0)