From ffce32d670df3067102782a17254c21ba8e7db70 Mon Sep 17 00:00:00 2001 From: DigiLive Date: Wed, 23 Apr 2025 07:46:49 +0200 Subject: [PATCH] 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. --- src/chips/AbstractChip.ts | 66 +++++++++++++++++++-------------------- src/chips/ClimateChip.ts | 57 +++++++++++++++------------------ src/chips/CoverChip.ts | 57 +++++++++++++++------------------ src/chips/FanChip.ts | 59 ++++++++++++++++------------------ src/chips/LightChip.ts | 59 ++++++++++++++++------------------ src/chips/SwitchChip.ts | 59 ++++++++++++++++------------------ src/chips/WeatherChip.ts | 43 +++++++++++-------------- 7 files changed, 184 insertions(+), 216 deletions(-) diff --git a/src/chips/AbstractChip.ts b/src/chips/AbstractChip.ts index 0af89f5..1ec488f 100644 --- a/src/chips/AbstractChip.ts +++ b/src/chips/AbstractChip.ts @@ -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 Chip class. - * - * To create a new chip, extend this one. - * - * @class - * @abstract - */ 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. * - * @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; diff --git a/src/chips/ClimateChip.ts b/src/chips/ClimateChip.ts index 5cb6357..fb1b4df 100644 --- a/src/chips/ClimateChip.ts +++ b/src/chips/ClimateChip.ts @@ -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"), - tap_action: { - action: "none", - }, - hold_action: { - action: "navigate", - navigation_path: "climates", - }, - }; + /** 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', + }, + hold_action: { + 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; diff --git a/src/chips/CoverChip.ts b/src/chips/CoverChip.ts index 5dc43f7..96b1f4c 100644 --- a/src/chips/CoverChip.ts +++ b/src/chips/CoverChip.ts @@ -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)"), - tap_action: { - action: "none", - }, - hold_action: { - action: "navigate", - navigation_path: "covers", - }, - }; + /** 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', + }, + hold_action: { + 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; diff --git a/src/chips/FanChip.ts b/src/chips/FanChip.ts index 5d0b0ec..59ed226 100644 --- a/src/chips/FanChip.ts +++ b/src/chips/FanChip.ts @@ -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"), - tap_action: { - action: "call-service", - service: "fan.turn_off", - }, - hold_action: { - action: "navigate", - navigation_path: "fans", - }, - }; + /** 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', + perform_action: 'fan.turn_off', + }, + hold_action: { + 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; diff --git a/src/chips/LightChip.ts b/src/chips/LightChip.ts index 9cf596a..81cd19d 100644 --- a/src/chips/LightChip.ts +++ b/src/chips/LightChip.ts @@ -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"), - tap_action: { - action: "call-service", - service: "light.turn_off", - }, - hold_action: { - action: "navigate", - navigation_path: "lights", - }, - }; + /** 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', + perform_action: 'light.turn_off', + }, + hold_action: { + 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; diff --git a/src/chips/SwitchChip.ts b/src/chips/SwitchChip.ts index 26a18ab..4abac14 100644 --- a/src/chips/SwitchChip.ts +++ b/src/chips/SwitchChip.ts @@ -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"), - tap_action: { - action: "call-service", - service: "switch.turn_off", - }, - hold_action: { - action: "navigate", - navigation_path: "switches", - }, - }; + /** 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', + perform_action: 'switch.turn_off', + }, + hold_action: { + 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; diff --git a/src/chips/WeatherChip.ts b/src/chips/WeatherChip.ts index 2a78fad..1cb5490 100644 --- a/src/chips/WeatherChip.ts +++ b/src/chips/WeatherChip.ts @@ -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", - show_temperature: true, - show_conditions: true, - }; + /** 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;