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 {LovelaceChipConfig} from "../types/lovelace-mushroom/utils/lovelace/chip/types";
import {Helper} from "../Helper";
import {generic} from "../types/strategy/generic";
import isCallServiceActionConfig = generic.isCallServiceActionConfig;
import { HassServiceTarget } from 'home-assistant-js-websocket';
import { Registry } from '../Registry';
import { LovelaceChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import { isCallServiceActionConfig } from '../types/strategy/strategy-generics';
import { logMessage, lvlFatal, lvlWarn } from '../utilities/debug';
/**
abstract class AbstractChip {
/**
* 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
* @abstract
* @remarks
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/
abstract class AbstractChip {
/**
* Configuration of the chip.
*
* @type {LovelaceChipConfig}
* Child classes should override this property to reflect their own card type and options.
*/
config: LovelaceChipConfig = {
type: "template"
configuration: LovelaceChipConfig = {
type: 'template',
};
/**
* Class Constructor.
*
* @remarks
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/
protected constructor() {
if (!Helper.isInitialized()) {
throw new Error("The Helper module must be initialized before using this one.");
if (!Registry.initialized) {
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 {
return this.config;
getChipConfiguration(): LovelaceChipConfig {
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) {
if ("tap_action" in this.config && isCallServiceActionConfig(this.config.tap_action)) {
this.config.tap_action.target = target;
if ('tap_action' in this.configuration && isCallServiceActionConfig(this.configuration.tap_action)) {
this.configuration.tap_action.target = target;
return;
}
if (Helper.debug) {
console.warn(
this.constructor.name
+ " - Target not set: Invalid target or tap action.");
}
logMessage(lvlWarn, '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.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/**
* 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 {
/**
* Default configuration of the chip.
*
* @type {TemplateChipConfig}
*
* @readonly
* @private
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:thermostat",
icon_color: "orange",
content: Helper.getCountTemplate("climate", "ne", "off"),
/** Returns the default configuration object for the chip. */
static getDefaultConfig(): TemplateChipConfig {
return {
type: 'template',
icon: 'mdi:thermostat',
icon_color: 'orange',
content: Registry.getCountTemplate('climate', 'ne', 'off'),
tap_action: {
action: "none",
action: 'none',
},
hold_action: {
action: "navigate",
navigation_path: "climates",
action: 'navigate',
navigation_path: 'climates',
},
};
}
/**
* Class Constructor.
*
* @param {chips.TemplateChipOptions} options The chip options.
* @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/
constructor(options: chips.TemplateChipOptions = {}) {
constructor(customConfiguration?: TemplateChipConfig) {
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.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/**
* 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 {
/**
* Default configuration of the chip.
*
* @type {TemplateChipConfig}
*
* @readonly
* @private
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:window-open",
icon_color: "cyan",
content: Helper.getCountTemplate("cover", "search", "(open|opening|closing)"),
/** Returns the default configuration object for the chip. */
static getDefaultConfig(): TemplateChipConfig {
return {
type: 'template',
icon: 'mdi:window-open',
icon_color: 'cyan',
content: Registry.getCountTemplate('cover', 'search', '(open|opening|closing)'),
tap_action: {
action: "none",
action: 'none',
},
hold_action: {
action: "navigate",
navigation_path: "covers",
action: 'navigate',
navigation_path: 'covers',
},
};
}
/**
* Class Constructor.
*
* @param {chips.TemplateChipOptions} options The chip options.
* @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/
constructor(options: chips.TemplateChipOptions = {}) {
constructor(customConfiguration?: TemplateChipConfig) {
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.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/**
* 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 {
/**
* Default configuration of the chip.
*
* @type {TemplateChipConfig}
*
* @readonly
* @private
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:fan",
icon_color: "green",
content: Helper.getCountTemplate("fan", "eq", "on"),
/** Returns the default configuration object for the chip. */
static getDefaultConfig(): TemplateChipConfig {
return {
type: 'template',
icon: 'mdi:fan',
icon_color: 'green',
content: Registry.getCountTemplate('fan', 'eq', 'on'),
tap_action: {
action: "call-service",
service: "fan.turn_off",
action: 'call-service',
perform_action: 'fan.turn_off',
},
hold_action: {
action: "navigate",
navigation_path: "fans",
action: 'navigate',
navigation_path: 'fans',
},
};
}
/**
* Class Constructor.
*
* @param {chips.TemplateChipOptions} options The chip options.
* @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/
constructor(options: chips.TemplateChipOptions = {}) {
constructor(customConfiguration?: TemplateChipConfig) {
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.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/**
* 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 {
/**
* Default configuration of the chip.
*
* @type {TemplateChipConfig}
*
* @readonly
* @private
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:lightbulb-group",
icon_color: "amber",
content: Helper.getCountTemplate("light", "eq", "on"),
/** Returns the default configuration object for the chip. */
static getDefaultConfig(): TemplateChipConfig {
return {
type: 'template',
icon: 'mdi:lightbulb-group',
icon_color: 'amber',
content: Registry.getCountTemplate('light', 'eq', 'on'),
tap_action: {
action: "call-service",
service: "light.turn_off",
action: 'call-service',
perform_action: 'light.turn_off',
},
hold_action: {
action: "navigate",
navigation_path: "lights",
action: 'navigate',
navigation_path: 'lights',
},
};
}
/**
* Class Constructor.
*
* @param {chips.TemplateChipOptions} options The chip options.
* @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/
constructor(options: chips.TemplateChipOptions = {}) {
constructor(customConfiguration?: TemplateChipConfig) {
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.
import { Registry } from '../Registry';
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/**
* 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 {
/**
* Default configuration of the chip.
*
* @type {TemplateChipConfig}
*
* @readonly
* @private
*/
readonly #defaultConfig: TemplateChipConfig = {
type: "template",
icon: "mdi:dip-switch",
icon_color: "blue",
content: Helper.getCountTemplate("switch", "eq", "on"),
/** Returns the default configuration object for the chip. */
static getDefaultConfig(): TemplateChipConfig {
return {
type: 'template',
icon: 'mdi:dip-switch',
icon_color: 'blue',
content: Registry.getCountTemplate('switch', 'eq', 'on'),
tap_action: {
action: "call-service",
service: "switch.turn_off",
action: 'call-service',
perform_action: 'switch.turn_off',
},
hold_action: {
action: "navigate",
navigation_path: "switches",
action: 'navigate',
navigation_path: 'switches',
},
};
}
/**
* Class Constructor.
*
* @param {chips.TemplateChipOptions} options The chip options.
* @param {TemplateChipConfig} [customConfiguration] Custom chip configuration.
*/
constructor(options: chips.TemplateChipOptions = {}) {
constructor(customConfiguration?: TemplateChipConfig) {
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.
import { WeatherChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
import AbstractChip from './AbstractChip';
/**
* 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 {
/**
* Default configuration of the chip.
*
* @private
* @readonly
*/
readonly #defaultConfig: WeatherChipConfig = {
type: "weather",
/** Returns the default configuration object for the chip. */
static getDefaultConfig(entityId: string): WeatherChipConfig {
return {
type: 'weather',
entity: entityId,
show_temperature: true,
show_conditions: true,
};
}
/**
* Class Constructor.
*
* @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();
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;