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 class AbstractChip {
/** /**
* Abstract Chip class. * Abstract Chip class.
* *
* To create a new chip, extend this one. * 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.
* *
* @class * @remarks
* @abstract * Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/ */
abstract class AbstractChip {
/** /**
* 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'),
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:thermostat",
icon_color: "orange",
content: Helper.getCountTemplate("climate", "ne", "off"),
tap_action: { tap_action: {
action: "none", action: 'none',
}, },
hold_action: { hold_action: {
action: "navigate", action: 'navigate',
navigation_path: "climates", 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)'),
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:window-open",
icon_color: "cyan",
content: Helper.getCountTemplate("cover", "search", "(open|opening|closing)"),
tap_action: { tap_action: {
action: "none", action: 'none',
}, },
hold_action: { hold_action: {
action: "navigate", action: 'navigate',
navigation_path: "covers", 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'),
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:fan",
icon_color: "green",
content: Helper.getCountTemplate("fan", "eq", "on"),
tap_action: { tap_action: {
action: "call-service", action: 'call-service',
service: "fan.turn_off", perform_action: 'fan.turn_off',
}, },
hold_action: { hold_action: {
action: "navigate", action: 'navigate',
navigation_path: "fans", 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'),
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:lightbulb-group",
icon_color: "amber",
content: Helper.getCountTemplate("light", "eq", "on"),
tap_action: { tap_action: {
action: "call-service", action: 'call-service',
service: "light.turn_off", perform_action: 'light.turn_off',
}, },
hold_action: { hold_action: {
action: "navigate", action: 'navigate',
navigation_path: "lights", 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'),
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:dip-switch",
icon_color: "blue",
content: Helper.getCountTemplate("switch", "eq", "on"),
tap_action: { tap_action: {
action: "call-service", action: 'call-service',
service: "switch.turn_off", perform_action: 'switch.turn_off',
}, },
hold_action: { hold_action: {
action: "navigate", action: 'navigate',
navigation_path: "switches", 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,
*/
readonly #defaultConfig: WeatherChipConfig = {
type: "weather",
show_temperature: true, show_temperature: true,
show_conditions: 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;