mirror of
https://github.com/DigiLive/mushroom-strategy.git
synced 2025-08-04 20:14:28 +02:00
Fix typo in JSDOC
This commit is contained in:
@@ -1,59 +1,59 @@
|
|||||||
import { Registry } from '../Registry';
|
import {Helper} from "../Helper";
|
||||||
import { LovelaceCardConfig } from '../types/homeassistant/data/lovelace/config/card';
|
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
|
||||||
import { AbstractCardConfig } from '../types/strategy/strategy-cards';
|
import {cards} from "../types/strategy/cards";
|
||||||
import { RegistryEntry } from '../types/strategy/strategy-generics';
|
import {generic} from "../types/strategy/generic";
|
||||||
import { logMessage, lvlFatal } from '../utilities/debug';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract Card Class
|
* Abstract Card Class
|
||||||
*
|
*
|
||||||
* To create a card configuration, this class should be extended by a child class.
|
* To create a new card, extend the new class with this one.
|
||||||
* Child classes should override the default configuration so the card correctly reflects the entity.
|
|
||||||
*
|
*
|
||||||
* @remarks
|
* @class
|
||||||
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
|
* @abstract
|
||||||
*/
|
*/
|
||||||
abstract class AbstractCard {
|
abstract class AbstractCard {
|
||||||
/** The registry entry this card represents. */
|
/**
|
||||||
readonly entity: RegistryEntry;
|
* Entity to create the card for.
|
||||||
|
*
|
||||||
|
* @type {generic.RegistryEntry}
|
||||||
|
*/
|
||||||
|
entity: generic.RegistryEntry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The card configuration for this entity.
|
* Configuration of the card.
|
||||||
*
|
*
|
||||||
* Child classes should override this property to reflect their own card type and options.
|
* @type {EntityCardConfig}
|
||||||
*/
|
*/
|
||||||
protected configuration: LovelaceCardConfig = {
|
config: EntityCardConfig = {
|
||||||
type: 'custom:mushroom-entity-card',
|
type: "custom:mushroom-entity-card",
|
||||||
icon: 'mdi:help-circle',
|
icon: "mdi:help-circle",
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor.
|
* Class constructor.
|
||||||
*
|
*
|
||||||
* @param {RegistryEntry} entity The registry entry to create a card configuration for.
|
* @param {generic.RegistryEntry} entity The hass entity to create a card for.
|
||||||
*
|
* @throws {Error} If the Helper module isn't initialized.
|
||||||
* @remarks
|
|
||||||
* Before this class can be used, the Registry module must be initialized by calling {@link Registry.initialize}.
|
|
||||||
*/
|
*/
|
||||||
protected constructor(entity: RegistryEntry) {
|
protected constructor(entity: generic.RegistryEntry) {
|
||||||
if (!Registry.initialized) {
|
if (!Helper.isInitialized()) {
|
||||||
logMessage(lvlFatal, 'Registry not initialized!');
|
throw new Error("The Helper module must be initialized before using this one.");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a card configuration.
|
* Get a card.
|
||||||
*
|
*
|
||||||
* The configuration should be set by any of the child classes so the card correctly reflects an entity.
|
* @returns {cards.AbstractCardConfig} A card object.
|
||||||
*/
|
*/
|
||||||
getCard(): AbstractCardConfig {
|
getCard(): cards.AbstractCardConfig {
|
||||||
return {
|
return {
|
||||||
...this.configuration,
|
...this.config,
|
||||||
entity: 'entity_id' in this.entity ? this.entity.entity_id : undefined,
|
entity: "entity_id" in this.entity ? this.entity.entity_id : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AbstractCard;
|
export {AbstractCard};
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import {cards} from "../types/strategy/cards";
|
|
||||||
import {LovelaceCardConfig} from "../types/homeassistant/data/lovelace";
|
|
||||||
import {HassServiceTarget} from "home-assistant-js-websocket";
|
import {HassServiceTarget} from "home-assistant-js-websocket";
|
||||||
|
import {LovelaceCardConfig} from "../types/homeassistant/data/lovelace";
|
||||||
import {StackCardConfig} from "../types/homeassistant/panels/lovelace/cards/types";
|
import {StackCardConfig} from "../types/homeassistant/panels/lovelace/cards/types";
|
||||||
|
import {cards} from "../types/strategy/cards";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller Card class.
|
* Controller Card class.
|
||||||
@@ -49,7 +49,7 @@ class ControllerCard {
|
|||||||
/**
|
/**
|
||||||
* Create a Controller card.
|
* Create a Controller card.
|
||||||
*
|
*
|
||||||
* @return {StackCardConfig} A Controller card.
|
* @returns {StackCardConfig} A Controller card.
|
||||||
*/
|
*/
|
||||||
createCard(): StackCardConfig {
|
createCard(): StackCardConfig {
|
||||||
const cards: LovelaceCardConfig[] = [
|
const cards: LovelaceCardConfig[] = [
|
||||||
|
60
src/localize.ts
Normal file
60
src/localize.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import * as de from "./translations/de.json";
|
||||||
|
import * as en from "./translations/en.json";
|
||||||
|
import * as es from "./translations/es.json";
|
||||||
|
import * as nl from "./translations/nl.json";
|
||||||
|
import {HomeAssistant} from "./types/homeassistant/types";
|
||||||
|
|
||||||
|
/* Registry of currently supported languages */
|
||||||
|
const languages: Record<string, unknown> = {
|
||||||
|
en,
|
||||||
|
es,
|
||||||
|
nl,
|
||||||
|
de,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* The fallback language if the user-defined language isn't defined */
|
||||||
|
const DEFAULT_LANG = "en";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a string by keyword and language.
|
||||||
|
*
|
||||||
|
* @param {string} key The keyword to look for in object notation (E.g. generic.home).
|
||||||
|
* @param {string} lang The language to get the string from (E.g. en).
|
||||||
|
*
|
||||||
|
* @returns {string | undefined} The requested string or undefined if the keyword doesn't exist/on error.
|
||||||
|
*/
|
||||||
|
function getTranslatedString(key: string, lang: string): string | undefined {
|
||||||
|
try {
|
||||||
|
return key
|
||||||
|
.split(".")
|
||||||
|
.reduce(
|
||||||
|
(o, i) => (o as Record<string, unknown>)[i],
|
||||||
|
languages[lang]
|
||||||
|
) as string;
|
||||||
|
} catch (_) {
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the localization.
|
||||||
|
*
|
||||||
|
* It reads the user-defined language with a fall-back to english and returns a function to get strings from
|
||||||
|
* language-files by keyword.
|
||||||
|
*
|
||||||
|
* If the keyword is undefined, or on error, the keyword itself is returned.
|
||||||
|
*
|
||||||
|
* @param {HomeAssistant} hass The Home Assistant object.
|
||||||
|
* @returns {(key: string) => string} The function to call for translating strings.
|
||||||
|
*/
|
||||||
|
export default function setupCustomLocalize(hass?: HomeAssistant): (key: string) => string {
|
||||||
|
return function (key: string) {
|
||||||
|
const lang = hass?.locale.language ?? DEFAULT_LANG;
|
||||||
|
|
||||||
|
let translated = getTranslatedString(key, lang);
|
||||||
|
if (!translated) translated = getTranslatedString(key, DEFAULT_LANG);
|
||||||
|
|
||||||
|
return translated ?? key;
|
||||||
|
};
|
||||||
|
}
|
@@ -68,7 +68,7 @@ abstract class AbstractView {
|
|||||||
/**
|
/**
|
||||||
* Create the cards to include in the view.
|
* Create the cards to include in the view.
|
||||||
*
|
*
|
||||||
* @return {Promise<(StackCardConfig | TitleCardConfig)[]>} An array of card objects.
|
* @returns {Promise<(StackCardConfig | TitleCardConfig)[]>} An array of card objects.
|
||||||
*/
|
*/
|
||||||
async createViewCards(): Promise<(StackCardConfig | TitleCardConfig)[]> {
|
async createViewCards(): Promise<(StackCardConfig | TitleCardConfig)[]> {
|
||||||
if (this.#domain === "home") {
|
if (this.#domain === "home") {
|
||||||
@@ -154,7 +154,7 @@ abstract class AbstractView {
|
|||||||
* Get a target of entity IDs for the given domain.
|
* Get a target of entity IDs for the given domain.
|
||||||
*
|
*
|
||||||
* @param {string} domain - The target domain to retrieve entity IDs from.
|
* @param {string} domain - The target domain to retrieve entity IDs from.
|
||||||
* @return {HassServiceTarget} - A target for a service call.
|
* @returns {HassServiceTarget} - A target for a service call.
|
||||||
*/
|
*/
|
||||||
targetDomain(domain: string): HassServiceTarget {
|
targetDomain(domain: string): HassServiceTarget {
|
||||||
return {
|
return {
|
||||||
|
@@ -49,7 +49,7 @@ class HomeView extends AbstractView {
|
|||||||
/**
|
/**
|
||||||
* Create the cards to include in the view.
|
* Create the cards to include in the view.
|
||||||
*
|
*
|
||||||
* @return {Promise<(StackCardConfig | TemplateCardConfig | ChipsCardConfig)[]>} Promise a View Card array.
|
* @returns {Promise<(StackCardConfig | TemplateCardConfig | ChipsCardConfig)[]>} Promise a View Card array.
|
||||||
* @override
|
* @override
|
||||||
*/
|
*/
|
||||||
async createViewCards(): Promise<(StackCardConfig | TemplateCardConfig | ChipsCardConfig)[]> {
|
async createViewCards(): Promise<(StackCardConfig | TemplateCardConfig | ChipsCardConfig)[]> {
|
||||||
@@ -123,7 +123,7 @@ class HomeView extends AbstractView {
|
|||||||
/**
|
/**
|
||||||
* Create the chips to include in the view.
|
* Create the chips to include in the view.
|
||||||
*
|
*
|
||||||
* @return {Promise<LovelaceChipConfig[]>} Promise a chip array.
|
* @returns {Promise<LovelaceChipConfig[]>} Promise a chip array.
|
||||||
*/
|
*/
|
||||||
async #createChips(): Promise<LovelaceChipConfig[]> {
|
async #createChips(): Promise<LovelaceChipConfig[]> {
|
||||||
if ((Helper.strategyOptions.home_view.hidden as string[]).includes("chips")) {
|
if ((Helper.strategyOptions.home_view.hidden as string[]).includes("chips")) {
|
||||||
@@ -185,7 +185,7 @@ class HomeView extends AbstractView {
|
|||||||
/**
|
/**
|
||||||
* Create the person cards to include in the view.
|
* Create the person cards to include in the view.
|
||||||
*
|
*
|
||||||
* @return {PersonCardConfig[]} A Person Card array.
|
* @returns {PersonCardConfig[]} A Person Card array.
|
||||||
*/
|
*/
|
||||||
#createPersonCards(): PersonCardConfig[] {
|
#createPersonCards(): PersonCardConfig[] {
|
||||||
if ((Helper.strategyOptions.home_view.hidden as string[]).includes("persons")) {
|
if ((Helper.strategyOptions.home_view.hidden as string[]).includes("persons")) {
|
||||||
@@ -214,7 +214,7 @@ class HomeView extends AbstractView {
|
|||||||
*
|
*
|
||||||
* Area cards are grouped into two areas per row.
|
* Area cards are grouped into two areas per row.
|
||||||
*
|
*
|
||||||
* @return {Promise<(TitleCardConfig | StackCardConfig)[]>} Promise an Area Card Section.
|
* @returns {Promise<(TitleCardConfig | StackCardConfig)[]>} Promise an Area Card Section.
|
||||||
*/
|
*/
|
||||||
async #createAreaSection(): Promise<(TitleCardConfig | StackCardConfig)[]> {
|
async #createAreaSection(): Promise<(TitleCardConfig | StackCardConfig)[]> {
|
||||||
if ((Helper.strategyOptions.home_view.hidden as string[]).includes("areas")) {
|
if ((Helper.strategyOptions.home_view.hidden as string[]).includes("areas")) {
|
||||||
|
Reference in New Issue
Block a user