fix: Allow multiple overrides to operate independently (#2063)
- Closes #1954
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { isEqual } from 'lodash-es';
|
||||
import { ConditionsManager } from '../../conditions/conditions-manager.js';
|
||||
import { isConfigUpgradeable } from '../../config/management.js';
|
||||
import { setProfiles } from '../../config/profiles/set-profiles.js';
|
||||
import {
|
||||
@@ -12,11 +11,11 @@ import { localize } from '../../localize/localize.js';
|
||||
import { getParseErrorPaths } from '../../utils/zod.js';
|
||||
import { InitializationAspect } from '../initialization-manager.js';
|
||||
import { CardConfigAPI } from '../types.js';
|
||||
import { getOverriddenConfig } from './get-overridden-config.js';
|
||||
import { setAutomationsFromConfig } from './load-automations.js';
|
||||
import { setRemoteControlEntityFromConfig } from './load-control-entities.js';
|
||||
import { setFoldersFromConfig } from './load-folders.js';
|
||||
import { setKeyboardShortcutsFromConfig } from './load-keyboard-shortcuts.js';
|
||||
import { OverridesManager } from './overrides-manager.js';
|
||||
|
||||
export class ConfigManager {
|
||||
protected _api: CardConfigAPI;
|
||||
@@ -29,7 +28,9 @@ export class ConfigManager {
|
||||
protected _overriddenConfig: AdvancedCameraCardConfig | null = null;
|
||||
protected _rawConfig: RawAdvancedCameraCardConfig | null = null;
|
||||
protected _cardWideConfig: CardWideConfig | null = null;
|
||||
protected _overridesConditionsManager: ConditionsManager | null = null;
|
||||
protected _overridesManager = new OverridesManager(() =>
|
||||
this._processOverrideConfig(),
|
||||
);
|
||||
|
||||
constructor(api: CardConfigAPI) {
|
||||
this._api = api;
|
||||
@@ -89,14 +90,10 @@ export class ConfigManager {
|
||||
debug: config.debug,
|
||||
};
|
||||
|
||||
this._overridesConditionsManager?.destroy();
|
||||
this._overridesConditionsManager = this._config.overrides?.length
|
||||
? new ConditionsManager(
|
||||
this._config.overrides.map((override) => override.conditions).flat(),
|
||||
this._api.getConditionStateManager(),
|
||||
)
|
||||
: null;
|
||||
this._overridesConditionsManager?.addListener(() => this._processOverrideConfig());
|
||||
this._overridesManager.set(
|
||||
this._api.getConditionStateManager(),
|
||||
this._config.overrides,
|
||||
);
|
||||
|
||||
this._api.getConditionStateManager().setState({
|
||||
view: undefined,
|
||||
@@ -121,11 +118,6 @@ export class ConfigManager {
|
||||
}
|
||||
|
||||
protected _processOverrideConfig(): void {
|
||||
/* istanbul ignore if: No (current) way to reach this code -- @preserve */
|
||||
if (!this._config) {
|
||||
return;
|
||||
}
|
||||
|
||||
const overriddenConfig = this._getOverriddenConfig();
|
||||
|
||||
// Save on Lit re-rendering costs by only updating the configuration if it
|
||||
@@ -163,15 +155,13 @@ export class ConfigManager {
|
||||
}
|
||||
|
||||
protected _getOverriddenConfig(): AdvancedCameraCardConfig | null {
|
||||
if (!this._overridesConditionsManager || !this._config) {
|
||||
return this._config;
|
||||
/* istanbul ignore if: No (current) way to reach this code -- @preserve */
|
||||
if (!this._config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return getOverriddenConfig(this._overridesConditionsManager, this._config, {
|
||||
configOverrides: this._config.overrides,
|
||||
schema: advancedCameraCardConfigSchema,
|
||||
});
|
||||
return this._overridesManager.getConfig(this._config);
|
||||
} catch (ev) {
|
||||
this._api.getMessageManager().setErrorIfHigherPriority(ev);
|
||||
return null;
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
import { merge } from 'lodash-es';
|
||||
import { ZodType as ZodSchema } from 'zod';
|
||||
import { ConditionsManagerReadonlyInterface } from '../../conditions/types';
|
||||
import {
|
||||
copyConfig,
|
||||
deleteConfigValue,
|
||||
getConfigValue,
|
||||
setConfigValue,
|
||||
} from '../../config/management';
|
||||
import { Overrides } from '../../config/schema/overrides';
|
||||
import { RawAdvancedCameraCardConfig } from '../../config/types';
|
||||
import { localize } from '../../localize/localize';
|
||||
import { AdvancedCameraCardError } from '../../types';
|
||||
import { desparsifyArrays } from '../../utils/basic';
|
||||
|
||||
class OverrideConfigurationError extends AdvancedCameraCardError {}
|
||||
|
||||
export function getOverriddenConfig<RT extends RawAdvancedCameraCardConfig>(
|
||||
manager: ConditionsManagerReadonlyInterface,
|
||||
config: Readonly<RT>,
|
||||
options?: {
|
||||
configOverrides?: Readonly<Overrides>;
|
||||
schema?: ZodSchema;
|
||||
},
|
||||
): RT {
|
||||
if (!options?.configOverrides) {
|
||||
return config;
|
||||
}
|
||||
|
||||
let output = copyConfig(config);
|
||||
let overridden = false;
|
||||
for (const override of options.configOverrides) {
|
||||
if (manager.getEvaluation()?.result) {
|
||||
override.delete?.forEach((deletionKey) => {
|
||||
deleteConfigValue(output, deletionKey);
|
||||
});
|
||||
|
||||
Object.keys(override.set ?? {}).forEach((setKey) => {
|
||||
setConfigValue(output, setKey, override.set?.[setKey]);
|
||||
});
|
||||
|
||||
Object.keys(override.merge ?? {}).forEach((mergeKey) => {
|
||||
setConfigValue(
|
||||
output,
|
||||
mergeKey,
|
||||
merge({}, getConfigValue(output, mergeKey), override.merge?.[mergeKey]),
|
||||
);
|
||||
});
|
||||
|
||||
overridden = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!overridden) {
|
||||
// Return the same configuration object if it has not been overridden (to
|
||||
// reduce re-renders for a configuration that has not changed).
|
||||
return config;
|
||||
}
|
||||
|
||||
if (options?.configOverrides?.some((override) => override.delete?.length)) {
|
||||
// If anything was deleted during this override, empty undefined slots may
|
||||
// be left in arrays where values were unset. Desparsify them.
|
||||
output = desparsifyArrays(output);
|
||||
}
|
||||
|
||||
if (options?.schema) {
|
||||
const parseResult = options.schema.safeParse(output);
|
||||
if (!parseResult.success) {
|
||||
throw new OverrideConfigurationError(
|
||||
localize('error.invalid_configuration_override'),
|
||||
[parseResult.error.errors, output],
|
||||
);
|
||||
}
|
||||
return parseResult.data;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import { merge } from 'lodash-es';
|
||||
import { ConditionsManager } from '../../conditions/conditions-manager';
|
||||
import { ConditionStateManagerReadonlyInterface } from '../../conditions/types';
|
||||
import {
|
||||
copyConfig,
|
||||
deleteConfigValue,
|
||||
getConfigValue,
|
||||
setConfigValue,
|
||||
} from '../../config/management';
|
||||
import { Override } from '../../config/schema/overrides';
|
||||
import {
|
||||
AdvancedCameraCardConfig,
|
||||
advancedCameraCardConfigSchema,
|
||||
} from '../../config/schema/types';
|
||||
import { localize } from '../../localize/localize';
|
||||
import { AdvancedCameraCardError } from '../../types';
|
||||
import { desparsifyArrays } from '../../utils/basic.js';
|
||||
|
||||
type OverridesCallback = () => void;
|
||||
|
||||
class OverrideConfigurationError extends AdvancedCameraCardError {}
|
||||
|
||||
export class OverridesManager {
|
||||
private _overrides = new Map<Override, ConditionsManager>();
|
||||
private _callback: OverridesCallback;
|
||||
|
||||
constructor(callback: OverridesCallback) {
|
||||
this._callback = callback;
|
||||
}
|
||||
|
||||
private _clear(): void {
|
||||
this._overrides.forEach((manager) => manager.destroy());
|
||||
this._overrides.clear();
|
||||
}
|
||||
|
||||
public hasOverrides(): boolean {
|
||||
return !!this._overrides.size;
|
||||
}
|
||||
|
||||
public set(
|
||||
stateManager: ConditionStateManagerReadonlyInterface,
|
||||
overrides?: Override[],
|
||||
): void {
|
||||
this._clear();
|
||||
|
||||
overrides?.forEach((override) => {
|
||||
const manager = new ConditionsManager(override.conditions, stateManager);
|
||||
manager.addListener(this._callback);
|
||||
this._overrides.set(override, manager);
|
||||
});
|
||||
}
|
||||
|
||||
public getConfig(base: AdvancedCameraCardConfig): AdvancedCameraCardConfig {
|
||||
let output = copyConfig(base);
|
||||
let overridden = false;
|
||||
let desparsify = false;
|
||||
|
||||
for (const [override, manager] of this._overrides.entries()) {
|
||||
if (manager.getEvaluation()?.result) {
|
||||
override.delete?.forEach((deletionKey) => {
|
||||
deleteConfigValue(output, deletionKey);
|
||||
desparsify = true;
|
||||
});
|
||||
|
||||
Object.keys(override.set ?? {}).forEach((setKey) => {
|
||||
setConfigValue(output, setKey, override.set?.[setKey]);
|
||||
});
|
||||
|
||||
Object.keys(override.merge ?? {}).forEach((mergeKey) => {
|
||||
setConfigValue(
|
||||
output,
|
||||
mergeKey,
|
||||
merge({}, getConfigValue(output, mergeKey), override.merge?.[mergeKey]),
|
||||
);
|
||||
});
|
||||
|
||||
overridden = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!overridden) {
|
||||
// Return the same configuration object if it has not been overridden (to
|
||||
// reduce re-renders for a configuration that has not changed).
|
||||
return base;
|
||||
}
|
||||
|
||||
if (desparsify) {
|
||||
// If anything was deleted during this override, empty undefined slots may
|
||||
// be left in arrays where values were unset. Desparsify them.
|
||||
output = desparsifyArrays(output);
|
||||
}
|
||||
|
||||
const parseResult = advancedCameraCardConfigSchema.safeParse(output);
|
||||
if (!parseResult.success) {
|
||||
throw new OverrideConfigurationError(
|
||||
localize('error.invalid_configuration_override'),
|
||||
[parseResult.error.errors, output],
|
||||
);
|
||||
}
|
||||
return parseResult.data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user