mirror of
https://github.com/DigiLive/mushroom-strategy.git
synced 2025-08-04 20:14:28 +02:00
Fix Stateful scenes support
- Stateful scene cards now replace classic scene cards when a stateful equivalent exists. - Stateful scene switches are hidden from the switch domain in all views, except for the Scene view. - Stateful scene switches are excluded from the switch chip count.
This commit is contained in:
@@ -244,6 +244,7 @@ class Registry {
|
|||||||
states.push(
|
states.push(
|
||||||
...new RegistryFilter(Registry.entities)
|
...new RegistryFilter(Registry.entities)
|
||||||
.whereDomain(domain)
|
.whereDomain(domain)
|
||||||
|
.where((entity) => !entity.entity_id.endsWith('_stateful_scene'))
|
||||||
.toList()
|
.toList()
|
||||||
.map((entity) => `states['${entity.entity_id}']`),
|
.map((entity) => `states['${entity.entity_id}']`),
|
||||||
);
|
);
|
||||||
|
@@ -3,28 +3,27 @@
|
|||||||
import { Registry } from '../Registry';
|
import { Registry } from '../Registry';
|
||||||
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
|
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
|
||||||
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
|
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
|
||||||
import { TemplateCardConfig } from '../types/lovelace-mushroom/cards/template-card-config';
|
|
||||||
import { isCallServiceActionConfig, isCallServiceActionTarget } from '../types/strategy/strategy-generics';
|
|
||||||
import AbstractCard from './AbstractCard';
|
import AbstractCard from './AbstractCard';
|
||||||
|
import SwitchCard from './SwitchCard';
|
||||||
|
import { isCallServiceActionConfig } from '../types/strategy/strategy-generics';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scene Card Class
|
* Scene Card Class
|
||||||
*
|
*
|
||||||
* Used to create a card configuration to control an entity of the scene domain.
|
* Used to create a card configuration to control an entity of the scene domain.
|
||||||
|
*
|
||||||
|
* Supports Stateful scenes from https://github.com/hugobloem/stateful_scenes.
|
||||||
|
* If the stateful scene entity is available, it will be used instead of the original scene entity.
|
||||||
*/
|
*/
|
||||||
class SceneCard extends AbstractCard {
|
class SceneCard extends AbstractCard {
|
||||||
/** Returns the default configuration object for the card. */
|
/** Returns the default configuration object for the card. */
|
||||||
static getDefaultConfig(): TemplateCardConfig {
|
static getDefaultConfig(): EntityCardConfig {
|
||||||
return {
|
return {
|
||||||
type: 'custom:mushroom-template-card',
|
type: 'custom:mushroom-entity-card',
|
||||||
icon: 'mdi:palette',
|
|
||||||
icon_color: 'disabled',
|
|
||||||
tap_action: {
|
tap_action: {
|
||||||
action: 'call-service',
|
action: 'perform-action',
|
||||||
perform_action: 'scene.turn_on',
|
perform_action: 'scene.turn_on',
|
||||||
target: {
|
target: {},
|
||||||
entity_id: undefined,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -36,31 +35,26 @@ class SceneCard extends AbstractCard {
|
|||||||
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
|
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
|
||||||
*/
|
*/
|
||||||
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
|
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
|
||||||
super(entity);
|
|
||||||
|
|
||||||
const sceneName = entity.entity_id.split('.').pop();
|
const sceneName = entity.entity_id.split('.').pop();
|
||||||
const configuration = SceneCard.getDefaultConfig();
|
const statefulScene = Registry.entities.find((entity) => entity.entity_id === `switch.${sceneName}_stateful_scene`);
|
||||||
|
|
||||||
|
super(statefulScene ?? entity);
|
||||||
|
|
||||||
|
// Stateful scene support.
|
||||||
|
if (statefulScene) {
|
||||||
|
this.configuration = new SwitchCard(statefulScene).getCard();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the default configuration.
|
// Initialize the default configuration.
|
||||||
configuration.primary = entity.name ?? entity.original_name ?? '?';
|
const configuration = SceneCard.getDefaultConfig();
|
||||||
|
|
||||||
if (
|
if (isCallServiceActionConfig(configuration.tap_action)) {
|
||||||
isCallServiceActionConfig(configuration.tap_action) &&
|
configuration.tap_action.target = { entity_id: entity.entity_id };
|
||||||
isCallServiceActionTarget(configuration.tap_action.target)
|
|
||||||
) {
|
|
||||||
configuration.tap_action.target.entity_id = entity.entity_id;
|
|
||||||
}
|
}
|
||||||
configuration.icon = Registry.hassStates[entity.entity_id]?.attributes.icon ?? configuration.icon;
|
|
||||||
|
|
||||||
// Stateful Scenes support. (https://github.com/hugobloem/stateful_scenes)
|
configuration.icon = Registry.hassStates[entity.entity_id]?.attributes.icon ?? configuration.icon;
|
||||||
configuration.icon_color = `
|
|
||||||
{% set state = states('switch.${sceneName}_stateful_scene') %}
|
|
||||||
{% if state == 'on' %}
|
|
||||||
blue
|
|
||||||
{% else %}
|
|
||||||
disabled
|
|
||||||
{% endif %}
|
|
||||||
`;
|
|
||||||
|
|
||||||
this.configuration = { ...this.configuration, ...configuration, ...customConfiguration };
|
this.configuration = { ...this.configuration, ...configuration, ...customConfiguration };
|
||||||
}
|
}
|
||||||
|
@@ -110,7 +110,11 @@ class MushroomStrategy extends HTMLTemplateElement {
|
|||||||
// Prepare promises for all supported domains
|
// Prepare promises for all supported domains
|
||||||
const domainCardPromises = exposedDomainNames.filter(isSupportedDomain).map(async (domain) => {
|
const domainCardPromises = exposedDomainNames.filter(isSupportedDomain).map(async (domain) => {
|
||||||
const moduleName = sanitizeClassName(domain + 'Card');
|
const moduleName = sanitizeClassName(domain + 'Card');
|
||||||
const entities = new RegistryFilter(areaEntities).whereDomain(domain).toList();
|
|
||||||
|
const entities = new RegistryFilter(areaEntities)
|
||||||
|
.whereDomain(domain)
|
||||||
|
.where((entity) => !(domain === 'switch' && entity.entity_id.endsWith('_stateful_scene')))
|
||||||
|
.toList();
|
||||||
|
|
||||||
if (!entities.length) {
|
if (!entities.length) {
|
||||||
return null;
|
return null;
|
||||||
|
@@ -54,9 +54,12 @@ abstract class AbstractView {
|
|||||||
*/
|
*/
|
||||||
protected async createCardConfigurations(): Promise<LovelaceCardConfig[]> {
|
protected async createCardConfigurations(): Promise<LovelaceCardConfig[]> {
|
||||||
const viewCards: LovelaceCardConfig[] = [];
|
const viewCards: LovelaceCardConfig[] = [];
|
||||||
const domainEntities = new RegistryFilter(Registry.entities).whereDomain(this.domain).toList();
|
|
||||||
const moduleName = sanitizeClassName(this.domain + 'Card');
|
const moduleName = sanitizeClassName(this.domain + 'Card');
|
||||||
const DomainCard = (await import(`../cards/${moduleName}`)).default;
|
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();
|
||||||
|
|
||||||
// Create card configurations for each area.
|
// Create card configurations for each area.
|
||||||
for (const area of Registry.areas) {
|
for (const area of Registry.areas) {
|
||||||
|
Reference in New Issue
Block a user