Optimize Chips

- Make use of the debug module.
- Make use of the Registry module.
- Align code across the chip modules.
- Make configurations immutable.
- Make use of the refactored types.
This commit is contained in:
DigiLive
2025-04-23 07:46:49 +02:00
parent 9a081a32b6
commit ffce32d670
7 changed files with 184 additions and 216 deletions

View File

@@ -1,64 +1,64 @@
import {HassServiceTarget} from "home-assistant-js-websocket"; import { HassServiceTarget } from 'home-assistant-js-websocket';
import {LovelaceChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types"; import { Registry } from '../Registry';
import {Helper} from "../Helper"; import { LovelaceChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import {generic} from "../types/strategy/generic"; import { isCallServiceActionConfig } from '../types/strategy/strategy-generics';
import isCallServiceActionConfig = generic.isCallServiceActionConfig; import { logMessage, lvlFatal, lvlWarn } from '../utilities/debug';
/**
* Abstract Chip class.
*
* To create a new chip, extend this one.
*
* @class
* @abstract
*/
abstract class AbstractChip { abstract class AbstractChip {
/**
* Abstract Chip class.
*
* To create a chip configuration, this class should be extended by a child class.
* Child classes should override the default configuration so the chip correctly reflects the entity.
*
* @remarks
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/
/** /**
* Configuration of the chip. * Configuration of the chip.
* *
* @type {LovelaceChipConfig} * Child classes should override this property to reflect their own card type and options.
*/ */
config: LovelaceChipConfig = { configuration: LovelaceChipConfig = {
type: "template" type: 'template',
}; };
/** /**
* Class Constructor. * Class Constructor.
*
* @remarks
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/ */
protected constructor() { protected constructor() {
if (!Helper.isInitialized()) { if (!Registry.initialized) {
throw new Error("The Helper module must be initialized before using this one."); logMessage(lvlFatal, 'Registry not initialized!');
} }
} }
// noinspection JSUnusedGlobalSymbols Method is called on dymanically imported classes.
/** /**
* Get the chip. * Get a chip configuration.
* *
* @returns {LovelaceChipConfig} A chip. * The configuration should be set by any of the child classes so the chip correctly reflects an entity.
*/ */
getChip(): LovelaceChipConfig { getChipConfiguration(): LovelaceChipConfig {
return this.config; return this.configuration;
} }
/** /**
* Set the target to switch. * Set the target for the tap action.
* *
* @param {HassServiceTarget} target Target to switch. * @param {HassServiceTarget} target Target of the tap action.
*/ */
setTapActionTarget(target: HassServiceTarget) { setTapActionTarget(target: HassServiceTarget) {
if ("tap_action" in this.config && isCallServiceActionConfig(this.config.tap_action)) { if ('tap_action' in this.configuration && isCallServiceActionConfig(this.configuration.tap_action)) {
this.config.tap_action.target = target; this.configuration.tap_action.target = target;
return; return;
} }
if (Helper.debug) { logMessage(lvlWarn, 'Target not set: Invalid target or tap action.');
console.warn(
this.constructor.name
+ " - Target not set: Invalid target or tap action.");
}
} }
} }
export {AbstractChip}; export default AbstractChip;

View File

@@ -1,47 +1,42 @@
import {Helper} from "../Helper";
import {AbstractChip} from "./AbstractChip";
import {chips} from "../types/strategy/chips";
import {TemplateChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported. // noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/** /**
* Climate Chip class. * Climate Chip class.
* *
* Used to create a chip to indicate how many climates are operating. * Used to create a chip configuration to indicate how many climates are operating.
*/ */
class ClimateChip extends AbstractChip { class ClimateChip extends AbstractChip {
/** /** Returns the default configuration object for the chip. */
* Default configuration of the chip. static getDefaultConfig(): TemplateChipConfig {
* return {
* @type {TemplateChipConfig} type: 'template',
* icon: 'mdi:thermostat',
* @readonly icon_color: 'orange',
* @private content: Registry.getCountTemplate('climate', 'ne', 'off'),
*/ tap_action: {
readonly #defaultConfig: TemplateChipConfig = { action: 'none',
type: "template", },
icon: "mdi:thermostat", hold_action: {
icon_color: "orange", action: 'navigate',
content: Helper.getCountTemplate("climate", "ne", "off"), navigation_path: 'climates',
tap_action: { },
action: "none", };
}, }
hold_action: {
action: "navigate",
navigation_path: "climates",
},
};
/** /**
* Class Constructor. * Class Constructor.
* *
* @param {chips.TemplateChipOptions} options The chip options. * @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/ */
constructor(options: chips.TemplateChipOptions = {}) { constructor(customConfiguration?: TemplateChipConfig) {
super(); super();
this.config = Object.assign(this.config, this.#defaultConfig, options); this.configuration = { ...this.configuration, ...ClimateChip.getDefaultConfig(), ...customConfiguration };
} }
} }
export {ClimateChip}; export default ClimateChip;

View File

@@ -1,47 +1,42 @@
import {Helper} from "../Helper";
import {chips} from "../types/strategy/chips";
import {AbstractChip} from "./AbstractChip";
import {TemplateChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported. // noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/** /**
* Cover Chip class. * Cover Chip class.
* *
* Used to create a chip to indicate how many covers aren't closed. * Used to create a chip configuration to indicate how many covers aren't closed.
*/ */
class CoverChip extends AbstractChip { class CoverChip extends AbstractChip {
/** /** Returns the default configuration object for the chip. */
* Default configuration of the chip. static getDefaultConfig(): TemplateChipConfig {
* return {
* @type {TemplateChipConfig} type: 'template',
* icon: 'mdi:window-open',
* @readonly icon_color: 'cyan',
* @private content: Registry.getCountTemplate('cover', 'search', '(open|opening|closing)'),
*/ tap_action: {
readonly #defaultConfig: TemplateChipConfig = { action: 'none',
type: "template", },
icon: "mdi:window-open", hold_action: {
icon_color: "cyan", action: 'navigate',
content: Helper.getCountTemplate("cover", "search", "(open|opening|closing)"), navigation_path: 'covers',
tap_action: { },
action: "none", };
}, }
hold_action: {
action: "navigate",
navigation_path: "covers",
},
};
/** /**
* Class Constructor. * Class Constructor.
* *
* @param {chips.TemplateChipOptions} options The chip options. * @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/ */
constructor(options: chips.TemplateChipOptions = {}) { constructor(customConfiguration?: TemplateChipConfig) {
super(); super();
this.config = Object.assign(this.config, this.#defaultConfig, options); this.configuration = { ...this.configuration, ...CoverChip.getDefaultConfig(), ...customConfiguration };
} }
} }
export {CoverChip}; export default CoverChip;

View File

@@ -1,48 +1,43 @@
import {Helper} from "../Helper";
import {chips} from "../types/strategy/chips";
import {AbstractChip} from "./AbstractChip";
import {TemplateChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported. // noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/** /**
* Fan Chip class. * Fan Chip class.
* *
* Used to create a chip to indicate how many fans are on and to turn all off. * Used to create a chip to indicate how many fans are on and to switch them all off.
*/ */
class FanChip extends AbstractChip { class FanChip extends AbstractChip {
/** /** Returns the default configuration object for the chip. */
* Default configuration of the chip. static getDefaultConfig(): TemplateChipConfig {
* return {
* @type {TemplateChipConfig} type: 'template',
* icon: 'mdi:fan',
* @readonly icon_color: 'green',
* @private content: Registry.getCountTemplate('fan', 'eq', 'on'),
*/ tap_action: {
readonly #defaultConfig: TemplateChipConfig = { action: 'call-service',
type: "template", perform_action: 'fan.turn_off',
icon: "mdi:fan", },
icon_color: "green", hold_action: {
content: Helper.getCountTemplate("fan", "eq", "on"), action: 'navigate',
tap_action: { navigation_path: 'fans',
action: "call-service", },
service: "fan.turn_off", };
}, }
hold_action: {
action: "navigate",
navigation_path: "fans",
},
};
/** /**
* Class Constructor. * Class Constructor.
* *
* @param {chips.TemplateChipOptions} options The chip options. * @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/ */
constructor(options: chips.TemplateChipOptions = {}) { constructor(customConfiguration?: TemplateChipConfig) {
super(); super();
this.config = Object.assign(this.config, this.#defaultConfig, options); this.configuration = { ...this.configuration, ...FanChip.getDefaultConfig(), ...customConfiguration };
} }
} }
export {FanChip}; export default FanChip;

View File

@@ -1,48 +1,43 @@
import {Helper} from "../Helper";
import {chips} from "../types/strategy/chips";
import {AbstractChip} from "./AbstractChip";
import {TemplateChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported. // noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/** /**
* Light Chip class. * Light Chip class.
* *
* Used to create a chip to indicate how many lights are on and to turn all off. * Used to create a chip configuration to indicate how many lights are on and to switch them all off.
*/ */
class LightChip extends AbstractChip { class LightChip extends AbstractChip {
/** /** Returns the default configuration object for the chip. */
* Default configuration of the chip. static getDefaultConfig(): TemplateChipConfig {
* return {
* @type {TemplateChipConfig} type: 'template',
* icon: 'mdi:lightbulb-group',
* @readonly icon_color: 'amber',
* @private content: Registry.getCountTemplate('light', 'eq', 'on'),
*/ tap_action: {
readonly #defaultConfig: TemplateChipConfig = { action: 'call-service',
type: "template", perform_action: 'light.turn_off',
icon: "mdi:lightbulb-group", },
icon_color: "amber", hold_action: {
content: Helper.getCountTemplate("light", "eq", "on"), action: 'navigate',
tap_action: { navigation_path: 'lights',
action: "call-service", },
service: "light.turn_off", };
}, }
hold_action: {
action: "navigate",
navigation_path: "lights",
},
};
/** /**
* Class Constructor. * Class Constructor.
* *
* @param {chips.TemplateChipOptions} options The chip options. * @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/ */
constructor(options: chips.TemplateChipOptions = {}) { constructor(customConfiguration?: TemplateChipConfig) {
super(); super();
this.config = Object.assign(this.config, this.#defaultConfig, options); this.configuration = { ...this.configuration, ...LightChip.getDefaultConfig(), ...customConfiguration };
} }
} }
export {LightChip}; export default LightChip;

View File

@@ -1,48 +1,43 @@
import {Helper} from "../Helper";
import {chips} from "../types/strategy/chips";
import {AbstractChip} from "./AbstractChip";
import {TemplateChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported. // noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/** /**
* Switch Chip class. * Switch Chip class.
* *
* Used to create a chip to indicate how many switches are on and to turn all off. * Used to create a chip configuration to indicate how many switches are on and to switch them all off.
*/ */
class SwitchChip extends AbstractChip { class SwitchChip extends AbstractChip {
/** /** Returns the default configuration object for the chip. */
* Default configuration of the chip. static getDefaultConfig(): TemplateChipConfig {
* return {
* @type {TemplateChipConfig} type: 'template',
* icon: 'mdi:dip-switch',
* @readonly icon_color: 'blue',
* @private content: Registry.getCountTemplate('switch', 'eq', 'on'),
*/ tap_action: {
readonly #defaultConfig: TemplateChipConfig = { action: 'call-service',
type: "template", perform_action: 'switch.turn_off',
icon: "mdi:dip-switch", },
icon_color: "blue", hold_action: {
content: Helper.getCountTemplate("switch", "eq", "on"), action: 'navigate',
tap_action: { navigation_path: 'switches',
action: "call-service", },
service: "switch.turn_off", };
}, }
hold_action: {
action: "navigate",
navigation_path: "switches",
},
};
/** /**
* Class Constructor. * Class Constructor.
* *
* @param {chips.TemplateChipOptions} options The chip options. * @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/ */
constructor(options: chips.TemplateChipOptions = {}) { constructor(customConfiguration?: TemplateChipConfig) {
super(); super();
this.config = Object.assign(this.config, this.#defaultConfig, options); this.configuration = { ...this.configuration, ...SwitchChip.getDefaultConfig(), ...customConfiguration };
} }
} }
export {SwitchChip}; export default SwitchChip;

View File

@@ -1,42 +1,35 @@
import {chips} from "../types/strategy/chips";
import {WeatherChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
import {AbstractChip} from "./AbstractChip";
// noinspection JSUnusedGlobalSymbols False positive. // noinspection JSUnusedGlobalSymbols False positive.
import { WeatherChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/** /**
* Weather Chip class. * Weather Chip class.
* *
* Used to create a chip for showing the weather. * Used to create a chip configuration to indicate the current weather.
*/ */
class WeatherChip extends AbstractChip { class WeatherChip extends AbstractChip {
/** /** Returns the default configuration object for the chip. */
* Default configuration of the chip. static getDefaultConfig(entityId: string): WeatherChipConfig {
* return {
* @private type: 'weather',
* @readonly entity: entityId,
*/ show_temperature: true,
readonly #defaultConfig: WeatherChipConfig = { show_conditions: true,
type: "weather", };
show_temperature: true, }
show_conditions: true,
};
/** /**
* Class Constructor. * Class Constructor.
* *
* @param {string} entityId Id of a weather entity. * @param {string} entityId Id of a weather entity.
* @param {chips.WeatherChipOptions} options Weather Chip options. * @param {WeatherChipConfig} [customConfiguration] Custom chip configuration.
*/ */
constructor(entityId: string, options: chips.WeatherChipOptions = {}) { constructor(entityId: string, customConfiguration?: WeatherChipConfig) {
super(); super();
this.#defaultConfig = {
...this.#defaultConfig,
...{entity: entityId},
...options,
};
this.config = Object.assign(this.config, this.#defaultConfig, options); this.configuration = { ...this.configuration, ...WeatherChip.getDefaultConfig(entityId), ...customConfiguration };
} }
} }
export {WeatherChip}; export default WeatherChip;