Skip to content

Commit 2532802

Browse files
committed
"Blind" fix for possible duplicate domain prefixes in entity-ids.
1 parent b4382da commit 2532802

File tree

2 files changed

+83
-9
lines changed

2 files changed

+83
-9
lines changed

src/models/settings/settings.ts

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import streamDeck from '@elgato/streamdeck'
2+
13
interface SettingsV1 {
24
version: 1
35
domain: string
@@ -88,13 +90,29 @@ interface SettingsV4 {
8890
rotationTickBucketSizeMs: number
8991
}
9092

91-
export enum IconSettings {
92-
HIDE = 'HIDE',
93-
PREFER_HA = 'PREFER_HA',
94-
PREFER_PLUGIN = 'PREFER_PLUGIN'
93+
export type SettingsV5 = {
94+
version: 5
95+
display: {
96+
entityId: string
97+
useCustomTitle: boolean
98+
buttonTitle: string
99+
enableServiceIndicator: boolean
100+
useCustomButtonLabels: boolean
101+
buttonLabels: string
102+
iconSettings: IconSettings
103+
}
104+
button: {
105+
serviceShortPress: ActionSettings
106+
serviceLongPress: ActionSettings
107+
serviceRotation: ActionSettings
108+
serviceTap: ActionSettings
109+
}
110+
rotationTickMultiplier: number
111+
rotationTickBucketSizeMs: number
95112
}
113+
96114
export type Settings = {
97-
version: 5
115+
version: 6
98116
display: {
99117
entityId: string
100118
useCustomTitle: boolean
@@ -120,9 +138,16 @@ export type ActionSettings = {
120138
serviceData?: Nullable<string>
121139
}
122140

123-
export type LegacySettings = SettingsV1 | SettingsV2 | SettingsV3 | SettingsV4
141+
export enum IconSettings {
142+
HIDE = 'HIDE',
143+
PREFER_HA = 'PREFER_HA',
144+
PREFER_PLUGIN = 'PREFER_PLUGIN'
145+
}
146+
147+
export type LegacySettings = SettingsV1 | SettingsV2 | SettingsV3 | SettingsV4 | SettingsV5
148+
149+
export const latestSettingsVersion = 6
124150

125-
export const latestSettingsVersion = 5
126151
export function migrateSettings(settings: LegacySettings | Settings): Settings {
127152
if (settings.version === undefined || settings.version == 1) {
128153
const settingsV2: SettingsV2 = {
@@ -241,7 +266,7 @@ export function migrateSettings(settings: LegacySettings | Settings): Settings {
241266
}
242267

243268
if (settings.version === 4) {
244-
const settingsV5: Settings = {
269+
const settingsV5: SettingsV5 = {
245270
...settings,
246271
version: 5,
247272
display: {
@@ -253,5 +278,54 @@ export function migrateSettings(settings: LegacySettings | Settings): Settings {
253278
return migrateSettings(settingsV5)
254279
}
255280

281+
if (settings.version === 5) {
282+
const settingsV6: Settings = {
283+
...settings,
284+
version: 6,
285+
display: {
286+
...settings.display,
287+
entityId: fixDuplicateDomain(settings.display.entityId) || ''
288+
},
289+
button: {
290+
serviceShortPress: {
291+
...settings.button.serviceShortPress,
292+
entityId: fixDuplicateDomain(settings.button.serviceShortPress?.entityId)
293+
},
294+
serviceLongPress: {
295+
...settings.button.serviceLongPress,
296+
entityId: fixDuplicateDomain(settings.button.serviceLongPress?.entityId)
297+
},
298+
serviceRotation: {
299+
...settings.button.serviceRotation,
300+
entityId: fixDuplicateDomain(settings.button.serviceRotation?.entityId)
301+
},
302+
serviceTap: {
303+
...settings.button.serviceTap,
304+
entityId: fixDuplicateDomain(settings.button.serviceTap?.entityId)
305+
}
306+
}
307+
}
308+
309+
return settingsV6
310+
}
311+
256312
return settings
257313
}
314+
315+
/**
316+
* Fixes duplicate domains in entityId strings.
317+
* For example, "light.light.foobar" becomes "light.foobar".
318+
*/
319+
function fixDuplicateDomain(entityId?: string | null): string | undefined | null {
320+
if (!entityId) {
321+
return entityId
322+
}
323+
324+
const parts = entityId.split('.')
325+
if (parts.length == 3 && parts[0] === parts[1]) {
326+
streamDeck.logger.warn(`Fixing duplicate domain in entityId: ${entityId}`)
327+
return parts[0] + '.' + parts.slice(2).join('.')
328+
}
329+
330+
return entityId
331+
}

test/testUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ export function buildTestSettings(): Settings {
3838
},
3939
rotationTickBucketSizeMs: 0,
4040
rotationTickMultiplier: 0,
41-
version: 5
41+
version: 6
4242
}
4343
}

0 commit comments

Comments
 (0)