mirror of
https://github.com/DigiLive/mushroom-strategy.git
synced 2025-08-04 20:14:28 +02:00
Add typeguard isTargetableChip
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { HassServiceTarget } from 'home-assistant-js-websocket';
|
import { HassServiceTarget } from 'home-assistant-js-websocket';
|
||||||
import { Registry } from '../Registry';
|
import { Registry } from '../Registry';
|
||||||
import { LovelaceChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
import { LovelaceChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||||
import { isCallServiceActionConfig } from '../types/strategy/strategy-generics';
|
import { isCallServiceActionConfig, isTargetableChip } from '../types/strategy/strategy-generics';
|
||||||
import { logMessage, lvlFatal, lvlWarn } from '../utilities/debug';
|
import { logMessage, lvlFatal, lvlWarn } from '../utilities/debug';
|
||||||
|
|
||||||
abstract class AbstractChip {
|
abstract class AbstractChip {
|
||||||
@@ -52,7 +52,7 @@ abstract class AbstractChip {
|
|||||||
* @param {HassServiceTarget} target Target of the tap action.
|
* @param {HassServiceTarget} target Target of the tap action.
|
||||||
*/
|
*/
|
||||||
setTapActionTarget(target: HassServiceTarget) {
|
setTapActionTarget(target: HassServiceTarget) {
|
||||||
if ('tap_action' in this.configuration && isCallServiceActionConfig(this.configuration.tap_action)) {
|
if (isTargetableChip(this.configuration) && isCallServiceActionConfig(this.configuration.tap_action)) {
|
||||||
this.configuration.tap_action.target = target;
|
this.configuration.tap_action.target = target;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
import { HassServiceTarget } from 'home-assistant-js-websocket';
|
|
||||||
import { AreaRegistryEntry } from '../homeassistant/data/area_registry';
|
import { AreaRegistryEntry } from '../homeassistant/data/area_registry';
|
||||||
import { DeviceRegistryEntry } from '../homeassistant/data/device_registry';
|
import { DeviceRegistryEntry } from '../homeassistant/data/device_registry';
|
||||||
import { EntityRegistryEntry } from '../homeassistant/data/entity_registry';
|
import { EntityRegistryEntry } from '../homeassistant/data/entity_registry';
|
||||||
@@ -7,7 +6,13 @@ import { LovelaceCardConfig } from '../homeassistant/data/lovelace/config/card';
|
|||||||
import { LovelaceConfig } from '../homeassistant/data/lovelace/config/types';
|
import { LovelaceConfig } from '../homeassistant/data/lovelace/config/types';
|
||||||
import { LovelaceViewConfig, LovelaceViewRawConfig } from '../homeassistant/data/lovelace/config/view';
|
import { LovelaceViewConfig, LovelaceViewRawConfig } from '../homeassistant/data/lovelace/config/view';
|
||||||
import { HomeAssistant } from '../homeassistant/types';
|
import { HomeAssistant } from '../homeassistant/types';
|
||||||
import { LovelaceChipConfig } from '../lovelace-mushroom/utils/lovelace/chip/types';
|
import {
|
||||||
|
BackChipConfig,
|
||||||
|
ConditionalChipConfig,
|
||||||
|
LovelaceChipConfig,
|
||||||
|
MenuChipConfig,
|
||||||
|
SpacerChipConfig,
|
||||||
|
} from '../lovelace-mushroom/utils/lovelace/chip/types';
|
||||||
import { StrategyHeaderCardConfig } from './strategy-cards';
|
import { StrategyHeaderCardConfig } from './strategy-cards';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -303,13 +308,24 @@ export function isCallServiceActionConfig(object?: ActionConfig): object is Call
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type guard to check if an object matches the HassServiceTarget interface.
|
* List of chip types that are not considered "targetable".
|
||||||
*
|
* These chips do not support tap_action or similar targeting logic.
|
||||||
* @param {any} [object] - The object to check.
|
|
||||||
* @returns {boolean} - True if the object represents a valid service action target.
|
|
||||||
*/
|
*/
|
||||||
export function isCallServiceActionTarget(object?: HassServiceTarget): object is HassServiceTarget {
|
const NON_TARGETABLE_CHIP_TYPES = ['back', 'menu', 'conditional', 'spacer'] as const;
|
||||||
return !!object && ['entity_id', 'device_id', 'area_id'].some((key) => key in object);
|
|
||||||
|
/**
|
||||||
|
* Type guard to check if a LovelaceChipConfig is "targetable".
|
||||||
|
*
|
||||||
|
* A targetable chip supports actions like tap_action.
|
||||||
|
* This function excludes chips of type "back", "menu", "conditional", and "spacer".
|
||||||
|
*
|
||||||
|
* @param object - The chip configuration object to check.
|
||||||
|
* @returns True if the chip is targetable; false otherwise.
|
||||||
|
*/
|
||||||
|
export function isTargetableChip(
|
||||||
|
object: LovelaceChipConfig,
|
||||||
|
): object is Exclude<LovelaceChipConfig, BackChipConfig | MenuChipConfig | ConditionalChipConfig | SpacerChipConfig> {
|
||||||
|
return !NON_TARGETABLE_CHIP_TYPES.includes(object.type as (typeof NON_TARGETABLE_CHIP_TYPES)[number]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user