-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and and or condition to conditional card (#18409)
- Loading branch information
Showing
7 changed files
with
221 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/panels/lovelace/editor/conditions/types/ha-card-condition-and.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { html, LitElement } from "lit"; | ||
import { customElement, property } from "lit/decorators"; | ||
import { any, array, assert, literal, object, optional } from "superstruct"; | ||
import { fireEvent } from "../../../../../common/dom/fire_event"; | ||
import "../../../../../components/ha-form/ha-form"; | ||
import type { HomeAssistant } from "../../../../../types"; | ||
import { | ||
AndCondition, | ||
Condition, | ||
StateCondition, | ||
} from "../../../common/validate-condition"; | ||
|
||
const andConditionStruct = object({ | ||
condition: literal("and"), | ||
conditions: optional(array(any())), | ||
}); | ||
|
||
@customElement("ha-card-condition-and") | ||
export class HaCardConditionNumericAnd extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property({ attribute: false }) public condition!: AndCondition; | ||
|
||
@property({ type: Boolean }) public disabled = false; | ||
|
||
public static get defaultConfig(): AndCondition { | ||
return { condition: "and", conditions: [] }; | ||
} | ||
|
||
protected static validateUIConfig(condition: StateCondition) { | ||
return assert(condition, andConditionStruct); | ||
} | ||
|
||
protected render() { | ||
return html` | ||
<ha-card-conditions-editor | ||
nested | ||
.hass=${this.hass} | ||
.conditions=${this.condition.conditions} | ||
@value-changed=${this._valueChanged} | ||
> | ||
</ha-card-conditions-editor> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent): void { | ||
ev.stopPropagation(); | ||
const conditions = ev.detail.value as Condition[]; | ||
const condition = { | ||
...this.condition, | ||
conditions, | ||
}; | ||
fireEvent(this, "value-changed", { value: condition }); | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"ha-card-condition-and": HaCardConditionNumericAnd; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/panels/lovelace/editor/conditions/types/ha-card-condition-or.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { html, LitElement } from "lit"; | ||
import { customElement, property } from "lit/decorators"; | ||
import { any, array, assert, literal, object, optional } from "superstruct"; | ||
import { fireEvent } from "../../../../../common/dom/fire_event"; | ||
import "../../../../../components/ha-form/ha-form"; | ||
import type { HomeAssistant } from "../../../../../types"; | ||
import { | ||
Condition, | ||
OrCondition, | ||
StateCondition, | ||
} from "../../../common/validate-condition"; | ||
|
||
const orConditionStruct = object({ | ||
condition: literal("or"), | ||
conditions: optional(array(any())), | ||
}); | ||
|
||
@customElement("ha-card-condition-or") | ||
export class HaCardConditionOr extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property({ attribute: false }) public condition!: OrCondition; | ||
|
||
@property({ type: Boolean }) public disabled = false; | ||
|
||
public static get defaultConfig(): OrCondition { | ||
return { condition: "or", conditions: [] }; | ||
} | ||
|
||
protected static validateUIConfig(condition: StateCondition) { | ||
return assert(condition, orConditionStruct); | ||
} | ||
|
||
protected render() { | ||
return html` | ||
<ha-card-conditions-editor | ||
nested | ||
.hass=${this.hass} | ||
.conditions=${this.condition.conditions} | ||
@value-changed=${this._valueChanged} | ||
> | ||
</ha-card-conditions-editor> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent): void { | ||
ev.stopPropagation(); | ||
const conditions = ev.detail.value as Condition[]; | ||
const condition = { | ||
...this.condition, | ||
conditions, | ||
}; | ||
fireEvent(this, "value-changed", { value: condition }); | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"ha-card-condition-or": HaCardConditionOr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4844,6 +4844,12 @@ | |
}, | ||
"user": { | ||
"label": "User" | ||
}, | ||
"or": { | ||
"label": "Or" | ||
}, | ||
"and": { | ||
"label": "And" | ||
} | ||
} | ||
}, | ||
|