Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cleanup leftover legacy settings logic #24947

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions lib/util/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,12 @@ function loadSettingsWithDefaults(): void {
status_topic: 'hass/status',
experimental_event_entities: false,
};
const sLegacy = {};

if (_settingsWithDefaults.advanced) {
for (const key of ['homeassistant_discovery_topic', 'homeassistant_status_topic']) {
// @ts-expect-error ignore typing
if (_settingsWithDefaults.advanced[key] !== undefined) {
// @ts-expect-error ignore typing
sLegacy[key.replace('homeassistant_', '')] = _settingsWithDefaults.advanced[key];
}
}
}

const s = typeof _settingsWithDefaults.homeassistant === 'object' ? _settingsWithDefaults.homeassistant : {};
// @ts-expect-error ignore typing
_settingsWithDefaults.homeassistant = {};

// @ts-expect-error ignore typing
objectAssignDeep(_settingsWithDefaults.homeassistant, defaults, sLegacy, s);
objectAssignDeep(_settingsWithDefaults.homeassistant, defaults, s);
}

if (_settingsWithDefaults.availability) {
Expand Down Expand Up @@ -338,7 +326,7 @@ function read(): Settings {
s[type] = {};
for (const file of files) {
const content = yaml.readIfExists(data.joinPath(file));
/* eslint-disable-line */ // @ts-expect-error
// @ts-expect-error noMutate not typed properly
s[type] = objectAssignDeep.noMutate(s[type], content);
}
}
Expand Down Expand Up @@ -370,23 +358,22 @@ function applyEnvironmentVariables(settings: Partial<Settings>): void {

if (type.indexOf('object') >= 0 || type.indexOf('array') >= 0) {
try {
// @ts-expect-error ignore typing
setting[key] = JSON.parse(envVariable);
setting[key as keyof Settings] = JSON.parse(envVariable);
} catch {
// @ts-expect-error ignore typing
setting[key] = envVariable;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = envVariable as any;
}
} else if (type.indexOf('number') >= 0) {
// @ts-expect-error ignore typing
setting[key] = (envVariable as unknown as number) * 1;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = ((envVariable as unknown as number) * 1) as any;
} else if (type.indexOf('boolean') >= 0) {
// @ts-expect-error ignore typing
setting[key] = envVariable.toLowerCase() === 'true';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = (envVariable.toLowerCase() === 'true') as any;
} else {
/* istanbul ignore else */
if (type.indexOf('string') >= 0) {
// @ts-expect-error ignore typing
setting[key] = envVariable;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = envVariable as any;
}
}
}
Expand Down Expand Up @@ -446,7 +433,7 @@ export function set(path: string[], value: string | number | boolean | KeyValue)

export function apply(settings: Record<string, unknown>): boolean {
getInternalSettings(); // Ensure _settings is initialized.
// @ts-expect-error ignore typing
// @ts-expect-error noMutate not typed properly
const newSettings = objectAssignDeep.noMutate(_settings, settings);
utils.removeNullPropertiesFromObject(newSettings, NULLABLE_SETTINGS);
ajvSetting(newSettings);
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/homeassistant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,8 @@ describe('Extension: HomeAssistant', () => {
});
});

it('Should discover devices with custom homeassistant_discovery_topic', async () => {
settings.set(['advanced', 'homeassistant_discovery_topic'], 'my_custom_discovery_topic');
it('Should discover devices with custom homeassistant.discovery_topic', async () => {
settings.set(['homeassistant'], {discovery_topic: 'my_custom_discovery_topic'});
await resetExtension();

const payload = {
Expand Down