Files
mushroom-strategy/src/views/AbstractView.ts
T

167 lines
6.0 KiB
TypeScript
Raw Normal View History

2025-05-13 07:01:26 +02:00
import { HassServiceTarget } from 'home-assistant-js-websocket';
import HeaderCard from '../cards/HeaderCard';
import { Registry } from '../Registry';
import { LovelaceCardConfig } from '../types/homeassistant/data/lovelace/config/card';
import { LovelaceViewConfig } from '../types/homeassistant/data/lovelace/config/view';
import { StackCardConfig } from '../types/homeassistant/panels/lovelace/cards/types';
import { AbstractCardConfig, CustomHeaderCardConfig, StrategyHeaderCardConfig } from '../types/strategy/strategy-cards';
import { SupportedDomains } from '../types/strategy/strategy-generics';
import { ViewConfig, ViewConstructor } from '../types/strategy/strategy-views';
import { sanitizeClassName } from '../utilities/auxiliaries';
import { logMessage, lvlFatal } from '../utilities/debug';
import RegistryFilter from '../utilities/RegistryFilter';
2025-06-01 12:49:10 +02:00
import { stackHorizontal } from '../utilities/cardStacking';
2023-11-22 21:02:56 +01:00
/**
* Abstract View Class.
*
2025-05-13 07:01:26 +02:00
* To create a view configuration, this class should be extended by a child class.
* Child classes should override the default configuration so the view correctly reflects the entities of a domain.
2023-11-22 21:02:56 +01:00
*
2025-05-13 07:01:26 +02:00
* @remarks
* Before this class can be used, the Registry module must be initialized by calling {@link Registry.initialize}.
2023-11-22 21:02:56 +01:00
*/
abstract class AbstractView {
2025-05-13 07:01:26 +02:00
/** The base configuration of a view. */
2025-05-14 10:58:45 +02:00
protected baseConfiguration: ViewConfig = {
2025-05-13 07:01:26 +02:00
icon: 'mdi:view-dashboard',
2023-11-22 21:02:56 +01:00
subview: false,
};
2025-05-13 07:01:26 +02:00
/** A card configuration to control all entities in the view. */
private viewHeaderCardConfiguration: StackCardConfig = {
2023-11-22 21:02:56 +01:00
cards: [],
2025-05-13 07:01:26 +02:00
type: '',
2023-11-22 21:02:56 +01:00
};
2025-05-13 07:01:26 +02:00
protected get domain(): SupportedDomains | 'home' {
return (this.constructor as unknown as ViewConstructor).domain;
}
2023-11-22 21:02:56 +01:00
/**
* Class constructor.
*
2025-05-13 07:01:26 +02:00
* @remarks
* Before this class can be used, the Registry module must be initialized by calling {@link Registry.initialize}.
2023-11-22 21:02:56 +01:00
*/
2025-05-13 07:01:26 +02:00
protected constructor() {
if (!Registry.initialized) {
2025-06-01 12:49:10 +02:00
logMessage(lvlFatal, 'Registry is not initialized!');
2023-11-22 21:02:56 +01:00
}
}
/**
2025-05-13 07:01:26 +02:00
* Create the configuration of the cards to include in the view.
2023-11-22 21:02:56 +01:00
*/
2025-05-13 07:01:26 +02:00
protected async createCardConfigurations(): Promise<LovelaceCardConfig[]> {
2023-11-22 21:02:56 +01:00
const viewCards: LovelaceCardConfig[] = [];
2025-05-13 07:01:26 +02:00
const moduleName = sanitizeClassName(this.domain + 'Card');
const DomainCard = (await import(`../cards/${moduleName}`)).default;
const domainEntities = new RegistryFilter(Registry.entities)
.whereDomain(this.domain)
.where((entity) => !entity.entity_id.endsWith('_stateful_scene'))
.toList();
2023-11-22 21:02:56 +01:00
2025-05-13 07:01:26 +02:00
// Create card configurations for each area.
for (const area of Registry.areas) {
2025-06-01 12:49:10 +02:00
let areaCards: AbstractCardConfig[] = [];
2023-11-22 21:02:56 +01:00
2025-05-13 07:01:26 +02:00
// Set the target of the Header card to the current area.
2023-11-22 21:02:56 +01:00
let target: HassServiceTarget = {
area_id: [area.area_id],
};
2025-05-13 07:01:26 +02:00
const areaEntities = new RegistryFilter(domainEntities).whereAreaId(area.area_id).toList();
2023-11-22 21:02:56 +01:00
2025-05-13 07:01:26 +02:00
// Set the target of the Header card to entities without an area.
if (area.area_id === 'undisclosed') {
2023-11-22 21:02:56 +01:00
target = {
2025-05-13 07:01:26 +02:00
entity_id: areaEntities.map((entity) => entity.entity_id),
};
2023-11-22 21:02:56 +01:00
}
2025-05-13 07:01:26 +02:00
// Create a card configuration for each entity in the current area.
areaCards.push(
...areaEntities.map((entity) =>
new DomainCard(entity, Registry.strategyOptions.card_options?.[entity.entity_id]).getCard(),
),
);
2023-11-22 21:02:56 +01:00
2025-06-01 12:49:10 +02:00
// Stack the cards of the current area.
2023-11-22 21:02:56 +01:00
if (areaCards.length) {
2025-06-01 12:49:10 +02:00
areaCards = stackHorizontal(
areaCards,
Registry.strategyOptions.domains[this.domain as SupportedDomains].stack_count ??
Registry.strategyOptions.domains['_'].stack_count,
);
2025-05-13 07:01:26 +02:00
// Create and insert a Header card.
const areaHeaderCardOptions = (
'headerCardConfiguration' in this.baseConfiguration ? this.baseConfiguration.headerCardConfiguration : {}
) as CustomHeaderCardConfig;
2023-11-22 21:02:56 +01:00
2025-05-13 07:01:26 +02:00
areaCards.unshift(new HeaderCard(target, { title: area.name, ...areaHeaderCardOptions }).createCard());
2023-11-22 21:02:56 +01:00
2025-05-13 07:01:26 +02:00
viewCards.push({ type: 'vertical-stack', cards: areaCards });
2023-11-22 21:02:56 +01:00
}
}
2025-05-13 07:01:26 +02:00
// Add a Header Card to control all the entities in the view.
if (this.viewHeaderCardConfiguration.cards.length && viewCards.length) {
viewCards.unshift(this.viewHeaderCardConfiguration);
2023-11-22 21:02:56 +01:00
}
return viewCards;
}
/**
2025-05-13 07:01:26 +02:00
* Get a view configuration.
2023-11-22 21:02:56 +01:00
*
2025-05-13 07:01:26 +02:00
* The configuration includes the card configurations which are created by createCardConfigurations().
2023-11-22 21:02:56 +01:00
*/
async getView(): Promise<LovelaceViewConfig> {
return {
2025-05-13 07:01:26 +02:00
...this.baseConfiguration,
cards: await this.createCardConfigurations(),
2023-11-22 21:02:56 +01:00
};
}
/**
2025-05-13 07:01:26 +02:00
* Get the domain's entity ids to target for a HASS service call.
2023-11-22 21:02:56 +01:00
*/
2025-05-13 07:01:26 +02:00
private getDomainTargets(): HassServiceTarget {
2023-11-22 21:02:56 +01:00
return {
2025-05-13 07:01:26 +02:00
entity_id: Registry.entities
.filter((entity) => entity.entity_id.startsWith(this.domain + '.'))
.map((entity) => entity.entity_id),
2023-11-22 21:02:56 +01:00
};
}
2025-05-13 07:01:26 +02:00
/**
* Initialize the view configuration with defaults and custom settings.
*
* @param viewConfiguration The view's default configuration for the view.
* @param customConfiguration The view's custom configuration to apply.
* @param headerCardConfig The view's Header card configuration.
*/
protected initializeViewConfig(
viewConfiguration: ViewConfig,
customConfiguration: ViewConfig = {},
headerCardConfig: CustomHeaderCardConfig,
): void {
this.baseConfiguration = { ...this.baseConfiguration, ...viewConfiguration, ...customConfiguration };
2025-05-14 10:58:45 +02:00
this.baseConfiguration.headerCardConfiguration = {
2025-06-03 20:47:44 +02:00
...this.baseConfiguration.headerCardConfiguration,
2025-05-14 10:58:45 +02:00
showControls:
Registry.strategyOptions.domains[this.domain as Exclude<SupportedDomains, 'home'>]?.showControls ??
Registry.strategyOptions.domains['_'].showControls,
};
2025-05-13 07:01:26 +02:00
this.viewHeaderCardConfiguration = new HeaderCard(this.getDomainTargets(), {
2025-05-14 10:58:45 +02:00
...(this.baseConfiguration.headerCardConfiguration as StrategyHeaderCardConfig),
2025-05-13 07:01:26 +02:00
...headerCardConfig,
}).createCard();
}
2023-11-22 21:02:56 +01:00
}
2025-05-13 07:01:26 +02:00
export default AbstractView;