mirror of
https://github.com/home-assistant/frontend.git
synced 2026-04-15 23:24:53 +00:00
Compare commits
1 Commits
debug-devi
...
card-featu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cec07d0b3d |
@@ -0,0 +1,99 @@
|
||||
import type { HassEntity } from "home-assistant-js-websocket";
|
||||
import { html, LitElement, nothing } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import { computeDomain } from "../../../common/entity/compute_domain";
|
||||
import "../../../components/ha-control-button";
|
||||
import "../../../components/ha-control-button-group";
|
||||
import { triggerAutomationActions } from "../../../data/automation";
|
||||
import { isUnavailableState } from "../../../data/entity/entity";
|
||||
import type { HomeAssistant } from "../../../types";
|
||||
import type { LovelaceCardFeature } from "../types";
|
||||
import { cardFeatureStyles } from "./common/card-feature-styles";
|
||||
import type {
|
||||
AutomationTriggerCardFeatureConfig,
|
||||
LovelaceCardFeatureContext,
|
||||
} from "./types";
|
||||
|
||||
export const supportsAutomationTriggerCardFeature = (
|
||||
hass: HomeAssistant,
|
||||
context: LovelaceCardFeatureContext
|
||||
) => {
|
||||
const stateObj = context.entity_id
|
||||
? hass.states[context.entity_id]
|
||||
: undefined;
|
||||
if (!stateObj) return false;
|
||||
const domain = computeDomain(stateObj.entity_id);
|
||||
return domain === "automation";
|
||||
};
|
||||
|
||||
@customElement("hui-automation-trigger-card-feature")
|
||||
class HuiAutomationTriggerCardFeature
|
||||
extends LitElement
|
||||
implements LovelaceCardFeature
|
||||
{
|
||||
@property({ attribute: false }) public hass?: HomeAssistant;
|
||||
|
||||
@property({ attribute: false }) public context?: LovelaceCardFeatureContext;
|
||||
|
||||
@state() private _config?: AutomationTriggerCardFeatureConfig;
|
||||
|
||||
private get _stateObj() {
|
||||
if (!this.hass || !this.context || !this.context.entity_id) {
|
||||
return undefined;
|
||||
}
|
||||
return this.hass.states[this.context.entity_id!] as HassEntity | undefined;
|
||||
}
|
||||
|
||||
static getStubConfig(): AutomationTriggerCardFeatureConfig {
|
||||
return {
|
||||
type: "automation-trigger",
|
||||
};
|
||||
}
|
||||
|
||||
public setConfig(config: AutomationTriggerCardFeatureConfig): void {
|
||||
if (!config) {
|
||||
throw new Error("Invalid configuration");
|
||||
}
|
||||
this._config = config;
|
||||
}
|
||||
|
||||
protected render() {
|
||||
if (
|
||||
!this._config ||
|
||||
!this.hass ||
|
||||
!this.context ||
|
||||
!this._stateObj ||
|
||||
!supportsAutomationTriggerCardFeature(this.hass, this.context)
|
||||
) {
|
||||
return nothing;
|
||||
}
|
||||
|
||||
return html`
|
||||
<ha-control-button-group>
|
||||
<ha-control-button
|
||||
.disabled=${isUnavailableState(this._stateObj.state)}
|
||||
class="trigger-button"
|
||||
@click=${this._triggerAutomation}
|
||||
>
|
||||
${this._config.action_name ??
|
||||
this.hass.localize("ui.card.automation.trigger")}
|
||||
</ha-control-button>
|
||||
</ha-control-button-group>
|
||||
`;
|
||||
}
|
||||
|
||||
private _triggerAutomation() {
|
||||
if (!this.hass || !this._stateObj) {
|
||||
return;
|
||||
}
|
||||
triggerAutomationActions(this.hass, this._stateObj.entity_id);
|
||||
}
|
||||
|
||||
static styles = cardFeatureStyles;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"hui-automation-trigger-card-feature": HuiAutomationTriggerCardFeature;
|
||||
}
|
||||
}
|
||||
@@ -251,6 +251,11 @@ export interface AreaControlsCardFeatureConfig {
|
||||
controls?: AreaControl[];
|
||||
}
|
||||
|
||||
export interface AutomationTriggerCardFeatureConfig {
|
||||
type: "automation-trigger";
|
||||
action_name?: string;
|
||||
}
|
||||
|
||||
export interface BarGaugeCardFeatureConfig {
|
||||
type: "bar-gauge";
|
||||
min?: number;
|
||||
@@ -261,6 +266,8 @@ export type LovelaceCardFeaturePosition = "bottom" | "inline";
|
||||
|
||||
export type LovelaceCardFeatureConfig =
|
||||
| AlarmModesCardFeatureConfig
|
||||
| AreaControlsCardFeatureConfig
|
||||
| AutomationTriggerCardFeatureConfig
|
||||
| ButtonCardFeatureConfig
|
||||
| ClimateFanModesCardFeatureConfig
|
||||
| ClimateSwingModesCardFeatureConfig
|
||||
@@ -305,7 +312,6 @@ export type LovelaceCardFeatureConfig =
|
||||
| ValvePositionFavoriteCardFeatureConfig
|
||||
| ValvePositionCardFeatureConfig
|
||||
| WaterHeaterOperationModesCardFeatureConfig
|
||||
| AreaControlsCardFeatureConfig
|
||||
| BarGaugeCardFeatureConfig;
|
||||
|
||||
export interface LovelaceCardFeatureContext {
|
||||
|
||||
@@ -42,6 +42,7 @@ import "../card-features/hui-valve-position-favorite-card-feature";
|
||||
import "../card-features/hui-valve-position-card-feature";
|
||||
import "../card-features/hui-water-heater-operation-modes-card-feature";
|
||||
import "../card-features/hui-area-controls-card-feature";
|
||||
import "../card-features/hui-automation-trigger-card-feature";
|
||||
import "../card-features/hui-bar-gauge-card-feature";
|
||||
import "../card-features/hui-trend-graph-card-feature";
|
||||
|
||||
@@ -54,6 +55,7 @@ import {
|
||||
const TYPES = new Set<LovelaceCardFeatureConfig["type"]>([
|
||||
"alarm-modes",
|
||||
"area-controls",
|
||||
"automation-trigger",
|
||||
"bar-gauge",
|
||||
"button",
|
||||
"climate-fan-modes",
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
import type { HomeAssistant } from "../../../../types";
|
||||
import { supportsAlarmModesCardFeature } from "../../card-features/hui-alarm-modes-card-feature";
|
||||
import { supportsAreaControlsCardFeature } from "../../card-features/hui-area-controls-card-feature";
|
||||
import { supportsAutomationTriggerCardFeature } from "../../card-features/hui-automation-trigger-card-feature";
|
||||
import { supportsBarGaugeCardFeature } from "../../card-features/hui-bar-gauge-card-feature";
|
||||
import { supportsButtonCardFeature } from "../../card-features/hui-button-card-feature";
|
||||
import { supportsClimateFanModesCardFeature } from "../../card-features/hui-climate-fan-modes-card-feature";
|
||||
@@ -86,6 +87,7 @@ type SupportsFeature = (
|
||||
const UI_FEATURE_TYPES = [
|
||||
"alarm-modes",
|
||||
"area-controls",
|
||||
"automation-trigger",
|
||||
"bar-gauge",
|
||||
"button",
|
||||
"climate-fan-modes",
|
||||
@@ -166,6 +168,7 @@ const SUPPORTS_FEATURE_TYPES: Record<
|
||||
> = {
|
||||
"alarm-modes": supportsAlarmModesCardFeature,
|
||||
"area-controls": supportsAreaControlsCardFeature,
|
||||
"automation-trigger": supportsAutomationTriggerCardFeature,
|
||||
"bar-gauge": supportsBarGaugeCardFeature,
|
||||
button: supportsButtonCardFeature,
|
||||
"climate-fan-modes": supportsClimateFanModesCardFeature,
|
||||
|
||||
@@ -9971,6 +9971,9 @@
|
||||
},
|
||||
"no_compatible_controls": "No compatible controls available for this area"
|
||||
},
|
||||
"automation-trigger": {
|
||||
"label": "Trigger"
|
||||
},
|
||||
"bar-gauge": {
|
||||
"label": "Bar gauge",
|
||||
"min": "Minimum value",
|
||||
|
||||
Reference in New Issue
Block a user