-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add screen condition to conditional card. (#18041)
- Loading branch information
Showing
10 changed files
with
831 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function getAllCombinations<T>(arr: T[]) { | ||
return arr.reduce<T[][]>( | ||
(combinations, element) => | ||
combinations.concat( | ||
combinations.map((combination) => [...combination, element]) | ||
), | ||
[[]] | ||
); | ||
} |
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,7 @@ | ||
import { mdiResponsive, mdiStateMachine } from "@mdi/js"; | ||
import { Condition } from "./validate-condition"; | ||
|
||
export const ICON_CONDITION: Record<Condition["condition"], string> = { | ||
state: mdiStateMachine, | ||
screen: mdiResponsive, | ||
}; |
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 |
---|---|---|
@@ -1,29 +1,75 @@ | ||
import { UNAVAILABLE } from "../../../data/entity"; | ||
import { HomeAssistant } from "../../../types"; | ||
|
||
export interface Condition { | ||
entity: string; | ||
export type Condition = StateCondition | ScreenCondition; | ||
|
||
export type LegacyCondition = { | ||
entity?: string; | ||
state?: string; | ||
state_not?: string; | ||
}; | ||
|
||
export type StateCondition = { | ||
condition: "state"; | ||
entity?: string; | ||
state?: string; | ||
state_not?: string; | ||
}; | ||
|
||
export type ScreenCondition = { | ||
condition: "screen"; | ||
media_query?: string; | ||
}; | ||
|
||
function checkStateCondition(condition: StateCondition, hass: HomeAssistant) { | ||
const state = | ||
condition.entity && hass.states[condition.entity] | ||
? hass.states[condition.entity].state | ||
: UNAVAILABLE; | ||
|
||
return condition.state != null | ||
? state === condition.state | ||
: state !== condition.state_not; | ||
} | ||
|
||
function checkScreenCondition( | ||
condition: ScreenCondition, | ||
_hass: HomeAssistant | ||
) { | ||
return condition.media_query | ||
? matchMedia(condition.media_query).matches | ||
: false; | ||
} | ||
|
||
export function checkConditionsMet( | ||
conditions: Condition[], | ||
hass: HomeAssistant | ||
): boolean { | ||
return conditions.every((c) => { | ||
const state = hass.states[c.entity] | ||
? hass!.states[c.entity].state | ||
: UNAVAILABLE; | ||
if (c.condition === "screen") { | ||
return checkScreenCondition(c, hass); | ||
} | ||
|
||
return c.state != null ? state === c.state : state !== c.state_not; | ||
return checkStateCondition(c, hass); | ||
}); | ||
} | ||
|
||
export function validateConditionalConfig(conditions: Condition[]): boolean { | ||
return conditions.every( | ||
(c) => | ||
(c.entity && | ||
(c.state != null || c.state_not != null)) as unknown as boolean | ||
function valideStateCondition(condition: StateCondition) { | ||
return ( | ||
condition.entity != null && | ||
(condition.state != null || condition.state_not != null) | ||
); | ||
} | ||
|
||
function validateScreenCondition(condition: ScreenCondition) { | ||
return condition.media_query != null; | ||
} | ||
|
||
export function validateConditionalConfig(conditions: Condition[]): boolean { | ||
return conditions.every((c) => { | ||
if (c.condition === "screen") { | ||
return validateScreenCondition(c); | ||
} | ||
return valideStateCondition(c); | ||
}); | ||
} |
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
Oops, something went wrong.