feat: Align automations with Home Assistant triggers and conditions (#2527)
Split automations into HA-style `triggers`, ongoing `conditions`, and
`actions`, with compatibility migrations for existing Advanced Camera
Card configs.
## Summary
At a glance (details below):
- **Added** `triggers:` -- a required, HA-shaped block: stock `state` /
`numeric_state` / `template` plus card-specific triggers (`camera`,
`view`, `fullscreen`, ...).
- **Added** the HA-native `if` / `then` / `else` action.
- **Removed** `actions_not` (replaced by `if` / `then` / `else`).
- **Removed** the ambient `advanced_camera_card` template namespace (use
`acc` instead).
- **Changed** the trigger template surface to a top-level `trigger.*`
variable (as in HA); the nested `acc.trigger.*` paths are removed.
- **Changed** `conditions:` to ongoing gates only -- they no longer wake
an automation, and change-only forms (`config`, valueless `camera` /
`view` / `state`) become triggers, not conditions.
- **Changed** action templates to render per step, so a later action
sees state an earlier one changed.
- **Compatibility:** HA-shaped YAML is accepted (singular keys,
single-or-list, `and` / `or` / `not` shorthand, `entity` / `entity_id`).
- **Migration:** existing configs upgrade automatically; anything that
cannot be converted faithfully is recorded under `__UPGRADE_FAILURE__`
for manual fixup.
## Breaking Changes
### 1. Automations now require triggers
Before this PR, `automations[].conditions` served two roles:
- They decided whether the automation should run.
- They also acted as the thing that woke the automation up.
After this PR:
- `triggers` wake the automation.
- `conditions` only gate it at the instant a trigger fires.
Most existing automations are migrated automatically from `conditions:`
to `triggers:`.
### 2. `actions_not` is retired
Legacy `actions_not` is replaced by an HA-style `if` action with `then`
/ `else`.
Faithful conversions are automatic. Cases that cannot be faithfully
converted are recorded under `__UPGRADE_FAILURE__.automations` and must
be migrated manually.
### 3. Template surface aligned with Home Assistant
Two related template changes, both auto-migrated:
- **Top-level `trigger.*`.** Automation actions now receive a top-level
`trigger` template variable, like Home Assistant. Legacy nested paths
such as `acc.trigger.state.to` and
`advanced_camera_card.trigger.camera.to` are migrated automatically when
they appear inside template strings.
- **The ambient `advanced_camera_card` template namespace is removed.**
The long-form ambient namespace (`advanced_camera_card.camera`,
`advanced_camera_card.view`, `advanced_camera_card.config`) is retired
in favour of its shorter `acc` alias -- supported since v7.1.0, and the
only spelling the new trigger surface uses. Existing templates are
migrated automatically by rewriting the `advanced_camera_card.` prefix
to `acc.`.
### 4. Trigger-only condition forms are no longer valid conditions
Some legacy "conditions" were really change detectors. These are now
triggers only:
- `condition: config`
- valueless `camera`
- valueless `view`
- valueless `state` / picture-elements state condition with neither
`state` nor `state_not`
These are automatically promoted in automations and stripped from
overrides/elements where they would no longer be meaningful as ongoing
conditions.
### 5. Template truthiness now follows Home Assistant behavior
Template conditions and template triggers intentionally use different
truthiness rules, matching HA:
- A template condition passes only when the rendered value is `true`
(case-insensitive), matching HA's `condition.py`.
- A template trigger uses HA's broader `result_as_boolean` coercion: a
non-zero number, or `1` / `true` / `yes` / `on` / `enable`
(case-insensitive), counts as true.
### 6. Action templates render when each action executes
Action templates are now rendered per action step, not once for the
whole sequence. This means a later action can see card-local state
changed by an earlier action in the same sequence.
The `trigger` context is fixed for the automation run. HA entity state
updates still depend on the frontend receiving updated HASS state over
the websocket.
## Automatic Migrations
### Automation `conditions:` to `triggers:`
Simple legacy automation:
```yaml
# Before
automations:
- conditions:
- condition: fullscreen
fullscreen: true
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: substream_on
```
```yaml
# After, automatic
automations:
- triggers:
- trigger: fullscreen
fullscreen: true
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: substream_on
```
State conditions become HA-style state triggers:
```yaml
# Before
automations:
- conditions:
- condition: state
entity_id: binary_sensor.front_door
state: 'on'
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: live
```
```yaml
# After, automatic
automations:
- triggers:
- trigger: state
entity_id: binary_sensor.front_door
to: 'on'
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: live
```
Multiple conditions become both triggers and ongoing conditions:
```yaml
# Before
automations:
- conditions:
- condition: camera
cameras: [front_door]
- condition: fullscreen
fullscreen: true
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: substream_on
```
```yaml
# After, automatic
automations:
- triggers:
- trigger: camera
cameras: [front_door]
- trigger: fullscreen
fullscreen: true
conditions:
- condition: camera
cameras: [front_door]
- condition: fullscreen
fullscreen: true
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: substream_on
```
The flattened trigger list is an implicit OR. The retained `conditions:`
list is an implicit AND checked when any trigger fires.
### Trigger-only legacy conditions
Legacy `config` conditions become `config` triggers:
```yaml
# Before
automations:
- conditions:
- condition: config
paths: [menu.style]
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: status_bar
```
```yaml
# After, automatic
automations:
- triggers:
- trigger: config
paths: [menu.style]
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: status_bar
```
Trigger-only leaves are removed from retained `conditions:` blocks
because they no longer describe an ongoing state.
### `actions_not` to `if` / `then` / `else`
```yaml
# Before
automations:
- conditions:
- condition: state
entity_id: input_boolean.camera_alerts
state: 'on'
actions:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: live
actions_not:
- action: none
```
```yaml
# After, automatic
automations:
- triggers:
- trigger: state
entity_id: input_boolean.camera_alerts
actions:
- if:
- condition: state
entity_id: input_boolean.camera_alerts
state: 'on'
then:
- action: custom:advanced-camera-card-action
advanced_camera_card_action: live
else:
- action: none
```
If the legacy automation had no conditions, or only trigger-only
conditions, `actions_not` is dropped because the old `else` branch could
not be reproduced as an ongoing predicate.
### Trigger template paths
```yaml
# Before
message: 'Door is {{ acc.trigger.state.to }} from {{ acc.trigger.state.from }}'
```
```yaml
# After, automatic
message: 'Door is {{ trigger.to_state.state }} from {{ trigger.from_state.state }}'
```
Path rewrites performed automatically:
| Old path | New path |
| -------------------------- | -------------------------- |
| `acc.trigger.state.entity` | `trigger.entity_id` |
| `acc.trigger.state.from` | `trigger.from_state.state` |
| `acc.trigger.state.to` | `trigger.to_state.state` |
| `acc.trigger.camera.from` | `trigger.from_acc.camera` |
| `acc.trigger.camera.to` | `trigger.to_acc.camera` |
| `acc.trigger.view.from` | `trigger.from_acc.view` |
| `acc.trigger.view.to` | `trigger.to_acc.view` |
| `acc.trigger.config.from` | `trigger.from_acc.config` |
| `acc.trigger.config.to` | `trigger.to_acc.config` |
The same rewrites are applied for the older
`advanced_camera_card.trigger.*` namespace.
### Ambient template namespace
Any remaining long-form ambient `advanced_camera_card.*` references
(outside the trigger surface) are rewritten to the `acc.*` alias:
```yaml
# Before
title: 'Now viewing {{ advanced_camera_card.camera }}'
```
```yaml
# After, automatic
title: 'Now viewing {{ acc.camera }}'
```
## Manual Migration Cases
### `__UPGRADE_FAILURE__.automations`
If a legacy automation cannot be converted faithfully, the original
automation is recorded under:
```yaml
__UPGRADE_FAILURE__:
automations:
- ...
```
These entries require manual migration.
The main known case is legacy `actions_not` with a condition whose
trigger can only fire on a rising edge, such as:
- `condition: template`
- `condition: screen`
- `condition: numeric_state` without an entity-backed state to watch
Those conditions can start the `then` branch, but cannot reliably start
the `else` branch when they stop matching.
### Unsupported HA conditions and triggers
This PR aligns the card with HA where supported, but it is not a full HA
automation engine.
Unsupported HA condition families include:
- `time`
- `zone`
- `sun`
- `location`
- `device`
- `condition: trigger`
Unsupported HA trigger platforms include:
- `event`
- `time`
- `time_pattern`
- `sun`
- `zone`
- `calendar`
- `webhook`
- `tag`
- `device`
- `mqtt`
The card-specific camera `triggers:` feature (which auto-selects and
wakes the card on camera events such as motion) is a separate feature
from automation `triggers:`, despite the shared word.
### Trigger IDs and variables
HA keys such as `id`, `alias`, and `variables` are accepted so pasted HA
YAML validates, but they are ignored by the card. There is no
`trigger.id` support in this PR.
## New Compatibility Features
This PR also makes card config more forgiving for HA-style YAML:
- `trigger`, `condition`, and `action` singular keys are accepted and
normalized to `triggers`, `conditions`, and `actions`.
- Single trigger, condition, and action objects are accepted where lists
are expected.
- `if`, `then`, and `else` accept a single item or a list.
- Composite condition shorthand is accepted:
- `{ and: [...] }`
- `{ or: [...] }`
- `{ not: [...] }`
- `{ condition: [...] }` as an implicit AND
- State conditions resolve expected state values that name another
entity, matching HA/Lovelace behavior.
- Both `entity` and `entity_id` are accepted on state and numeric
conditions and triggers (a superset of HA's two dialects), so there is
no forced rename.
- `state_not` remains supported as a card/Lovelace-friendly extension.
## Trigger Payloads
Automation action templates receive a top-level `trigger` object.
For stock `state` and `numeric_state` triggers:
```yaml
trigger.platform
trigger.entity_id
trigger.entity
trigger.from_state
trigger.to_state
```
For template triggers:
```yaml
trigger.platform
```
For card-specific triggers:
```yaml
trigger.platform # "acc"
trigger.type
trigger.from_acc
trigger.to_acc
```
The card does not currently expose HA's `id`, `idx`, `for`, `attribute`,
`above`, `below`, or `alias` trigger fields.
BREAKING CHANGE: Automations now follow Home Assistant's `triggers:` /
`conditions:` / `actions:` model. Automations require a `triggers:`
block and `conditions:` no longer wake an automation; `actions_not` is
removed in favour of an `if` / `then` / `else` action; the nested
`acc.trigger.*` template paths and the ambient `advanced_camera_card`
template namespace are removed (use the top-level `trigger.*` surface
and the `acc` alias); trigger-only condition forms (`config`, valueless
`camera` / `view` / `state`) are no longer valid conditions; and
template-condition vs template-trigger truthiness now follow HA.
Existing configs are upgraded automatically where a faithful conversion
exists; anything that cannot be converted is recorded under
`__UPGRADE_FAILURE__` for manual migration.
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
committed by
dermotduffy
co-authored by
Claude Opus 4.8
parent
209c873c58
commit
b701366762
@@ -0,0 +1,297 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ConditionsManager } from '../../../src/condition-trigger/conditions/conditions-manager';
|
||||
import { ConditionStateManager } from '../../../src/condition-trigger/conditions/state-manager';
|
||||
import { createHASS, createStateEntity } from '../../test-utils';
|
||||
|
||||
// Per-condition-type evaluation is covered by tests/conditions/conditions/<type>.test.ts.
|
||||
// This file covers the manager's own orchestration: building/destroying evaluators,
|
||||
// listener management, trigger-data merging, and notifying (or not) on changes.
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('ConditionsManager', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should not call listeners for HA state changes without a relevant condition', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[
|
||||
{
|
||||
condition: 'fullscreen' as const,
|
||||
fullscreen: true,
|
||||
},
|
||||
],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '11' }) }),
|
||||
});
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should forward the triggering state change to listeners', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'fullscreen' as const, fullscreen: true }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
// The change that prompted the evaluation is passed through verbatim.
|
||||
expect(listener).toHaveBeenLastCalledWith(expect.anything(), {
|
||||
old: {},
|
||||
change: { fullscreen: true },
|
||||
new: { fullscreen: true },
|
||||
});
|
||||
});
|
||||
|
||||
it('should re-evaluate and notify when a subscribed condition source changes', () => {
|
||||
const addEventListener = vi.fn();
|
||||
const removeEventListener = vi.fn();
|
||||
vi.spyOn(window, 'matchMedia')
|
||||
.mockReturnValueOnce({
|
||||
addEventListener: addEventListener,
|
||||
removeEventListener: removeEventListener,
|
||||
} as unknown as MediaQueryList)
|
||||
.mockReturnValueOnce({
|
||||
matches: false,
|
||||
} as unknown as MediaQueryList)
|
||||
.mockReturnValueOnce({
|
||||
matches: true,
|
||||
} as unknown as MediaQueryList);
|
||||
|
||||
const manager = new ConditionsManager([
|
||||
{ condition: 'screen' as const, media_query: 'whatever' },
|
||||
]);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
|
||||
// Fire the media-query change; the manager re-evaluates and notifies.
|
||||
addEventListener.mock.calls[0][1]();
|
||||
expect(listener).toBeCalledWith({ result: true }, undefined);
|
||||
|
||||
// Destroy tears the subscription down.
|
||||
manager.destroy();
|
||||
expect(removeEventListener).toBeCalled();
|
||||
});
|
||||
|
||||
describe('should handle listeners correctly', () => {
|
||||
it('should add listener', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'fullscreen' as const, fullscreen: true }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).toBeCalledWith({ result: true }, expect.anything());
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
|
||||
stateManager.setState({ fullscreen: false });
|
||||
expect(listener).toBeCalledWith({ result: false }, expect.anything());
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
|
||||
// Re-add the same listener (will still only be called once).
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).toBeCalledWith({ result: true }, expect.anything());
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
});
|
||||
|
||||
it('should remove listener', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'fullscreen' as const, fullscreen: true }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
manager.removeListener(listener);
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should remove listener on destroy', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'fullscreen' as const, fullscreen: true }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
manager.destroy();
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should not call listeners when the condition result does not change', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'view' as const, views: ['live'] }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
const listener = vi.fn();
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({ view: 'live' });
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
|
||||
stateManager.setState({ view: 'clip' });
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
|
||||
stateManager.setState({ view: 'clip' });
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
|
||||
stateManager.setState({ view: 'live' });
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
|
||||
stateManager.setState({ view: 'live' });
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('enabled', () => {
|
||||
const ENABLED_TEMPLATE = '{{ is_state("binary_sensor.flag", "on") }}';
|
||||
|
||||
it('should ignore a disabled condition', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'fullscreen' as const, fullscreen: true, enabled: false }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
// The disabled condition is ignored, so with no remaining conditions the
|
||||
// result is true even though fullscreen does not match.
|
||||
stateManager.setState({ fullscreen: false });
|
||||
|
||||
expect(manager.getEvaluation()).toEqual({ result: true });
|
||||
});
|
||||
|
||||
it('should evaluate an enabled condition normally', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[{ condition: 'fullscreen' as const, fullscreen: true, enabled: true }],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
stateManager.setState({ fullscreen: false });
|
||||
expect(manager.getEvaluation()).toEqual({ result: false });
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
expect(manager.getEvaluation()).toEqual({ result: true });
|
||||
});
|
||||
|
||||
it('should drop a condition whose enabled template does not render true', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
stateManager.setState({
|
||||
hass: createHASS({ 'binary_sensor.flag': createStateEntity({ state: 'off' }) }),
|
||||
});
|
||||
const manager = new ConditionsManager(
|
||||
[
|
||||
{
|
||||
condition: 'fullscreen' as const,
|
||||
fullscreen: true,
|
||||
enabled: ENABLED_TEMPLATE,
|
||||
},
|
||||
],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
stateManager.setState({ fullscreen: false });
|
||||
|
||||
expect(manager.getEvaluation()).toEqual({ result: true });
|
||||
});
|
||||
|
||||
it('should evaluate a condition whose enabled template renders true', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
stateManager.setState({
|
||||
hass: createHASS({ 'binary_sensor.flag': createStateEntity({ state: 'on' }) }),
|
||||
});
|
||||
const manager = new ConditionsManager(
|
||||
[
|
||||
{
|
||||
condition: 'fullscreen' as const,
|
||||
fullscreen: true,
|
||||
enabled: ENABLED_TEMPLATE,
|
||||
},
|
||||
],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
stateManager.setState({ fullscreen: false });
|
||||
|
||||
expect(manager.getEvaluation()).toEqual({ result: false });
|
||||
});
|
||||
|
||||
it('should keep a condition enabled when its template cannot render without hass', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
const manager = new ConditionsManager(
|
||||
[
|
||||
{
|
||||
condition: 'fullscreen' as const,
|
||||
fullscreen: true,
|
||||
enabled: ENABLED_TEMPLATE,
|
||||
},
|
||||
],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
stateManager.setState({ fullscreen: false });
|
||||
|
||||
// With no hass the enabled template cannot render, so the gate stays
|
||||
// enabled and the condition is evaluated (here: fullscreen is false).
|
||||
expect(manager.getEvaluation()).toEqual({ result: false });
|
||||
});
|
||||
|
||||
it('should re-evaluate the enabled template on each evaluation', () => {
|
||||
const stateManager = new ConditionStateManager();
|
||||
stateManager.setState({
|
||||
hass: createHASS({ 'binary_sensor.flag': createStateEntity({ state: 'off' }) }),
|
||||
fullscreen: false,
|
||||
});
|
||||
const manager = new ConditionsManager(
|
||||
[
|
||||
{
|
||||
condition: 'fullscreen' as const,
|
||||
fullscreen: true,
|
||||
enabled: ENABLED_TEMPLATE,
|
||||
},
|
||||
],
|
||||
stateManager,
|
||||
);
|
||||
|
||||
// Flag off: the condition is disabled and ignored, so the result is true.
|
||||
expect(manager.getEvaluation()).toEqual({ result: true });
|
||||
|
||||
// Flag on: the condition is now active and fullscreen does not match.
|
||||
stateManager.setState({
|
||||
hass: createHASS({ 'binary_sensor.flag': createStateEntity({ state: 'on' }) }),
|
||||
});
|
||||
expect(manager.getEvaluation()).toEqual({ result: false });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('and condition', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should evaluate a simple and condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'and' as const,
|
||||
conditions: [
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
{ condition: 'expand' as const, expand: true },
|
||||
],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ fullscreen: true }).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ fullscreen: true, expand: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ fullscreen: false, expand: true }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should expose its children external invalidation sources', () => {
|
||||
// The `screen` child contributes an external source; the `fullscreen` child
|
||||
// contributes none -- the union must include only the former.
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'and' as const,
|
||||
conditions: [
|
||||
{ condition: 'screen' as const, media_query: 'whatever' },
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.externalSources).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { callConditionSchema } from '../../../../src/config/schema/condition-trigger/conditions/custom/call';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('call condition', () => {
|
||||
it('should require a value', () => {
|
||||
expect(() => callConditionSchema.parse({ condition: 'call' })).toThrow();
|
||||
});
|
||||
|
||||
it('should match when call is true', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'call' as const, call: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ call: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ call: false }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match when call is false', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'call' as const, call: false },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// With no state.call published, the bare condition matches `false`,
|
||||
// so `call: false` is satisfied.
|
||||
expect(evaluator.evaluate({}).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ call: true }).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ call: false }).result).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('camera condition', () => {
|
||||
it('should match a named camera', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'camera' as const, cameras: ['bar'] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ camera: 'bar' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ camera: 'will-not-match' }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match any of several named cameras', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'camera' as const, cameras: ['foo', 'bar'] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({ camera: 'bar' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ camera: 'foo' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ camera: 'baz' }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match a selected camera when cameras is omitted', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'camera' as const },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({ camera: 'bar' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match no selected camera for an empty list', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'camera' as const, cameras: [] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ camera: 'bar' }).result).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('display mode condition', () => {
|
||||
it('should match a display mode condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'display_mode' as const, display_mode: 'grid' as const },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ displayMode: 'grid' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ displayMode: 'single' }).result).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { expandConditionSchema } from '../../../../src/config/schema/condition-trigger/conditions/custom/expand';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('expand condition', () => {
|
||||
it('should match an expand condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'expand' as const, expand: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ expand: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ expand: false }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require a value', () => {
|
||||
expect(() => expandConditionSchema.parse({ condition: 'expand' })).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { fullscreenConditionSchema } from '../../../../src/config/schema/condition-trigger/conditions/custom/fullscreen';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('fullscreen condition', () => {
|
||||
it('should match a fullscreen condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ fullscreen: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ fullscreen: false }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require a value', () => {
|
||||
expect(() => fullscreenConditionSchema.parse({ condition: 'fullscreen' })).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('initialized condition', () => {
|
||||
it('should match an initialized condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'initialized' as const },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ initialized: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ initialized: false }).result).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { interactionConditionSchema } from '../../../../src/config/schema/condition-trigger/conditions/custom/interaction';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('interaction condition', () => {
|
||||
it('should match an interaction condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'interaction' as const, interaction: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ interaction: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ interaction: false }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require a value', () => {
|
||||
expect(() =>
|
||||
interactionConditionSchema.parse({ condition: 'interaction' }),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isTemplateTrue } from '../../../../src/condition-trigger/conditions/conditions/is-template-true';
|
||||
|
||||
describe('condition isTemplateTrue', () => {
|
||||
it('should accept boolean true', () => {
|
||||
expect(isTemplateTrue(true)).toBe(true);
|
||||
});
|
||||
|
||||
it('should accept the string "true" case-insensitively', () => {
|
||||
expect(isTemplateTrue('true')).toBe(true);
|
||||
expect(isTemplateTrue('TRUE')).toBe(true);
|
||||
});
|
||||
|
||||
it('should reject other truthy-looking values for HA symmetry', () => {
|
||||
for (const value of ['yes', 'on', '1', 'enable', false, 0, null, undefined]) {
|
||||
expect(isTemplateTrue(value)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('key condition', () => {
|
||||
it('should match a simple keypress', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'key' as const, key: 'a' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
keys: {
|
||||
a: { state: 'down', ctrl: false, shift: false, alt: false, meta: false },
|
||||
},
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
keys: {
|
||||
a: { state: 'up', ctrl: false, shift: false, alt: false, meta: false },
|
||||
},
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match a keypress with modifiers', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'key' as const,
|
||||
key: 'a',
|
||||
state: 'down' as const,
|
||||
ctrl: true,
|
||||
shift: true,
|
||||
alt: true,
|
||||
meta: true,
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
keys: {
|
||||
a: { state: 'down', ctrl: false, shift: false, alt: false, meta: false },
|
||||
},
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
keys: {
|
||||
a: { state: 'down', ctrl: true, shift: true, alt: true, meta: false },
|
||||
},
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
keys: {
|
||||
a: { state: 'down', ctrl: true, shift: true, alt: true, meta: true },
|
||||
},
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { mediaLoadedConditionSchema } from '../../../../src/config/schema/condition-trigger/conditions/custom/media-loaded';
|
||||
import { createMediaLoadedInfo } from '../../../test-utils';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('media loaded condition', () => {
|
||||
it('should match a media loaded condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'media_loaded' as const, media_loaded: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({ mediaLoadedInfo: createMediaLoadedInfo() }).result,
|
||||
).toBeTruthy();
|
||||
expect(evaluator.evaluate({ mediaLoadedInfo: null }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should require a value', () => {
|
||||
expect(() =>
|
||||
mediaLoadedConditionSchema.parse({ condition: 'media_loaded' }),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { MicrophoneState } from '../../../../src/card-controller/types';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('microphone condition', () => {
|
||||
const createMicrophoneState = (state: Partial<MicrophoneState>): MicrophoneState => {
|
||||
return {
|
||||
connected: false,
|
||||
muted: false,
|
||||
forbidden: false,
|
||||
...state,
|
||||
};
|
||||
};
|
||||
|
||||
it('should match when muted is true', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'microphone' as const, muted: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({ microphone: createMicrophoneState({ muted: true }) }).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({ microphone: createMicrophoneState({ muted: false }) }).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match when muted is false', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'microphone' as const, muted: false },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({ microphone: createMicrophoneState({ muted: true }) }).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({ microphone: createMicrophoneState({ muted: false }) }).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('not condition', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should evaluate a not condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'not' as const,
|
||||
conditions: [
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
{ condition: 'expand' as const, expand: true },
|
||||
],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// Neither sub-condition is true, so `not` passes.
|
||||
expect(evaluator.evaluate({})).toEqual({ result: true });
|
||||
|
||||
// Any sub-condition being true means `not` fails.
|
||||
expect(evaluator.evaluate({ fullscreen: true }).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ expand: true }).result).toBeFalsy();
|
||||
|
||||
// Both sub-conditions false again -- `not` passes.
|
||||
expect(evaluator.evaluate({ fullscreen: false, expand: false }).result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should expose its children external invalidation sources', () => {
|
||||
// The `screen` child contributes an external source; the `fullscreen` child
|
||||
// contributes none -- the union must include only the former.
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'not' as const,
|
||||
conditions: [
|
||||
{ condition: 'screen' as const, media_query: 'whatever' },
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.externalSources).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,227 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createHASS, createStateEntity } from '../../../test-utils';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('numeric state condition', () => {
|
||||
it('should match above a threshold', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'numeric_state' as const, entity_id: 'sensor.foo', above: 10 },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '11' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: '9' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match below a threshold', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'numeric_state' as const, entity_id: 'sensor.foo', below: 10 },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '11' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '9' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should accept the entity field', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'numeric_state' as const, entity: 'sensor.foo', above: 10 },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '11' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should match a list of entities only when all of them match', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'numeric_state' as const,
|
||||
entity_id: ['sensor.a', 'sensor.b'],
|
||||
above: 10,
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// Both above -> match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.a': createStateEntity({ state: '11' }),
|
||||
'sensor.b': createStateEntity({ state: '12' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
// One below -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.a': createStateEntity({ state: '11' }),
|
||||
'sensor.b': createStateEntity({ state: '9' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
// One absent -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.a': createStateEntity({ state: '11' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match against an attribute instead of the state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'numeric_state' as const,
|
||||
entity_id: 'sensor.foo',
|
||||
attribute: 'battery',
|
||||
below: 20,
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.foo': createStateEntity({ state: 'on', attributes: { battery: 15 } }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.foo': createStateEntity({ state: 'on', attributes: { battery: 50 } }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should compare the rendered value_template instead of the state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'numeric_state' as const,
|
||||
entity_id: 'sensor.foo',
|
||||
value_template: '{{ 11 }}',
|
||||
above: 10,
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// The entity state (0) would fail; the template value (11) passes.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '0' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should not match when the value is not numeric', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'numeric_state' as const, entity_id: 'sensor.foo', above: 10 },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: 'unavailable' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not match without a condition state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'numeric_state' as const, entity_id: 'sensor.foo', above: 10 },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate().result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not match when the entity is absent', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'numeric_state' as const, entity_id: 'sensor.missing', above: 10 },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '11' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should resolve an entity-id reference as the above/below threshold', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'numeric_state' as const,
|
||||
entity_id: 'sensor.foo',
|
||||
above: 'input_number.limit',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// sensor.foo (20) > input_number.limit (10) -> true.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.foo': createStateEntity({ state: '20' }),
|
||||
'input_number.limit': createStateEntity({ state: '10' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
// sensor.foo (5) > input_number.limit (10) -> false.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.foo': createStateEntity({ state: '5' }),
|
||||
'input_number.limit': createStateEntity({ state: '10' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not match when a threshold entity is unresolvable', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'numeric_state' as const,
|
||||
entity_id: 'sensor.foo',
|
||||
below: 'input_number.limit',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'sensor.foo': createStateEntity({ state: '5' }),
|
||||
'input_number.limit': createStateEntity({ state: 'unavailable' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('or condition', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should evaluate a simple or condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'or' as const,
|
||||
conditions: [
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
{ condition: 'expand' as const, expand: true },
|
||||
],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ fullscreen: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ expand: true }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ fullscreen: false, expand: false }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should expose its children external invalidation sources', () => {
|
||||
// The `screen` child contributes an external source; the `fullscreen` child
|
||||
// contributes none -- the union must include only the former.
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'or' as const,
|
||||
conditions: [
|
||||
{ condition: 'screen' as const, media_query: 'whatever' },
|
||||
{ condition: 'fullscreen' as const, fullscreen: true },
|
||||
],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.externalSources).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('screen condition', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should evaluate the media query', () => {
|
||||
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
||||
matches: true,
|
||||
} as unknown as MediaQueryList);
|
||||
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'screen' as const, media_query: 'whatever' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
expect(evaluator.evaluate().result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should expose the media query as an external invalidation source', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'screen' as const, media_query: 'whatever' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
expect(evaluator.externalSources).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should not match or expose a source without a media query', () => {
|
||||
const matchMedia = vi.spyOn(window, 'matchMedia');
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'screen' as const },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate().result).toBeFalsy();
|
||||
expect(evaluator.externalSources).toHaveLength(0);
|
||||
expect(matchMedia).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,472 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createHASS, createStateEntity } from '../../../test-utils';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('state condition', () => {
|
||||
it('should match any transition when neither state nor state_not is set', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, entity_id: 'binary_sensor.foo' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate(
|
||||
{
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'on' }) }),
|
||||
},
|
||||
{},
|
||||
),
|
||||
).toEqual({ result: true });
|
||||
|
||||
expect(
|
||||
evaluator.evaluate(
|
||||
{
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
},
|
||||
{
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'on' }) }),
|
||||
},
|
||||
),
|
||||
).toEqual({ result: true });
|
||||
});
|
||||
|
||||
it('should match a single positive state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, entity_id: 'binary_sensor.foo', state: 'on' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity() }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match multiple positive states', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state: ['active', 'on'],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity() }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'active' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match a single negative state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, entity_id: 'binary_sensor.foo', state_not: 'on' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity() }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should match multiple negative states', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state_not: ['active', 'on'],
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity() }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'active' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should match an implicit state condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ entity_id: 'binary_sensor.foo', state: 'on' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity() }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not match when no entity is set', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, state: 'on' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
const result = evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'on' }) }),
|
||||
});
|
||||
expect(result.result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should accept the entity field', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, entity: 'binary_sensor.foo', state: 'on' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'on' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'binary_sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match a list of entities only when all match by default', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: ['binary_sensor.foo', 'binary_sensor.bar'],
|
||||
state: 'on',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// Both on -> match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'on' }),
|
||||
'binary_sensor.bar': createStateEntity({ state: 'on' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
// One off -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'on' }),
|
||||
'binary_sensor.bar': createStateEntity({ state: 'off' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
// One absent -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'on' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match a list of entities when any matches and match is any', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: ['binary_sensor.foo', 'binary_sensor.bar'],
|
||||
state: 'on',
|
||||
match: 'any' as const,
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// One on -> match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'on' }),
|
||||
'binary_sensor.bar': createStateEntity({ state: 'off' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
// None on -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'off' }),
|
||||
'binary_sensor.bar': createStateEntity({ state: 'off' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match against an attribute instead of the state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
attribute: 'device_class',
|
||||
state: 'door',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({
|
||||
state: 'on',
|
||||
attributes: { device_class: 'door' },
|
||||
}),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({
|
||||
state: 'on',
|
||||
attributes: { device_class: 'window' },
|
||||
}),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
// Attribute absent on the entity -> no value -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'on', attributes: {} }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
describe('for', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date('2026-06-05T22:56:56Z'));
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should not match until the state has been held long enough', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state: 'on',
|
||||
for: '00:00:05',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// Held 2s (< 5s) -> no match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({
|
||||
state: 'on',
|
||||
last_changed: '2026-06-05T22:56:54Z',
|
||||
}),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
|
||||
// Held 8s (>= 5s) -> match.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({
|
||||
state: 'on',
|
||||
last_changed: '2026-06-05T22:56:48Z',
|
||||
}),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should render a templated "for" before comparing', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state: 'on',
|
||||
for: "{{ states('input_number.delay') }}",
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
const evaluateHeldSince = (lastChanged: string): boolean =>
|
||||
!!evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({
|
||||
state: 'on',
|
||||
last_changed: lastChanged,
|
||||
}),
|
||||
'input_number.delay': createStateEntity({ state: '5' }),
|
||||
}),
|
||||
}).result;
|
||||
|
||||
// `for` renders to 5s: held 2s -> no match, held 8s -> match.
|
||||
expect(evaluateHeldSince('2026-06-05T22:56:54Z')).toBe(false);
|
||||
expect(evaluateHeldSince('2026-06-05T22:56:48Z')).toBe(true);
|
||||
});
|
||||
|
||||
it('should not match when last_changed is unavailable', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state: 'on',
|
||||
for: '00:00:05',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'on', last_changed: '' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not match when for is unparseable', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state: 'on',
|
||||
for: 'not-a-duration',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({
|
||||
state: 'on',
|
||||
last_changed: '2026-06-05T22:56:46Z',
|
||||
}),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
it('should resolve an expected state that names another entity', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'state' as const,
|
||||
entity_id: 'binary_sensor.foo',
|
||||
state: 'input_text.expected',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// The entity's state matches the resolved state of `input_text.expected`.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'armed' }),
|
||||
'input_text.expected': createStateEntity({ state: 'armed' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
|
||||
// It does not match when the resolved state differs.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'disarmed' }),
|
||||
'input_text.expected': createStateEntity({ state: 'armed' }),
|
||||
}),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match a state_not against an empty-string entity state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, entity_id: 'sensor.foo', state_not: 'on' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should match an empty-string expected state', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'state' as const, entity_id: 'sensor.foo', state: '' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
// An entity state of "" matches the configured empty `state`.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
|
||||
// A non-empty entity state does not.
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: 'on' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createHASS, createStateEntity } from '../../../test-utils';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('template condition', () => {
|
||||
it('should evaluate true when template evalutes to true', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'template' as const,
|
||||
value_template: '{{ is_state("sensor.foo", "on") }}',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: 'on' }) }),
|
||||
}).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: 'off' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should evaluate false when template evalutes to non-boolean', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'template' as const,
|
||||
// This does not result in a boolean.
|
||||
value_template: '{{ hass.states["light.office"].state }}',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({
|
||||
hass: createHASS({ 'light.office': createStateEntity({ state: 'on' }) }),
|
||||
}).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should accept a template rendering the string "true" for HA symmetry', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'template' as const, value_template: '{{ "true" }}' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({ hass: createHASS({}) }).result).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
import { TemplateRenderer } from '../../../../src/card-controller/templates';
|
||||
import { EvaluatorContext } from '../../../../src/condition-trigger/conditions/conditions/types';
|
||||
|
||||
export const createEvaluatorContext = (): EvaluatorContext => ({
|
||||
templateRenderer: new TemplateRenderer(),
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('triggered condition', () => {
|
||||
it('should match any triggered camera when no list is given', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'triggered' as const },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ triggered: new Set() }).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ triggered: new Set(['camera_1']) }).result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should match when one of the listed cameras is triggered', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'triggered' as const, triggered: ['camera_1', 'camera_2'] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ triggered: new Set(['camera_1']) }).result).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({ triggered: new Set(['camera_2', 'camera_1', 'camera_3']) })
|
||||
.result,
|
||||
).toBeTruthy();
|
||||
expect(evaluator.evaluate({ triggered: new Set(['camera_3']) }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match when no camera is triggered for an empty list', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'triggered' as const, triggered: [] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ triggered: new Set() }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ triggered: new Set(['camera_1']) }).result).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('user agent condition', () => {
|
||||
const userAgent =
|
||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
||||
|
||||
it('should match exact user agent', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'user_agent' as const, user_agent: userAgent },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ userAgent: userAgent }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ userAgent: 'Something else' }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match user agent regex', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'user_agent' as const, user_agent_re: 'Chrome/' },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ userAgent: userAgent }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ userAgent: 'Something else' }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match casting', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'user_agent' as const, casting: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ userAgent: 'CrKey/1.0' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ userAgent: userAgent }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match companion app', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'user_agent' as const, companion: true },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ userAgent: 'Home Assistant/' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ userAgent: userAgent }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match multiple parameters', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{
|
||||
condition: 'user_agent' as const,
|
||||
companion: true,
|
||||
user_agent: 'Home Assistant/',
|
||||
user_agent_re: 'Home.Assistant',
|
||||
},
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ userAgent: 'Home Assistant/' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ userAgent: 'Something else' }).result).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createHASS, createUser } from '../../../test-utils';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('user condition', () => {
|
||||
it('should match a user condition', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'user' as const, users: ['user_1', 'user_2'] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(
|
||||
evaluator.evaluate({ hass: createHASS({}, createUser({ id: 'user_1' })) }).result,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
evaluator.evaluate({ hass: createHASS({}, createUser({ id: 'user_WRONG' })) })
|
||||
.result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not match when no users are set', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'user' as const },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(
|
||||
evaluator.evaluate({ hass: createHASS({}, createUser({ id: 'user_1' })) }).result,
|
||||
).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createConditionEvaluator } from '../../../../src/condition-trigger/conditions/factory';
|
||||
import { createEvaluatorContext } from './test-utils';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('view condition', () => {
|
||||
it('should match a named view', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'view' as const, views: ['live'] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({}).result).toBeFalsy();
|
||||
expect(evaluator.evaluate({ view: 'live' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ view: 'clips' }).result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match any of several named views', () => {
|
||||
const evaluator = createConditionEvaluator(
|
||||
{ condition: 'view' as const, views: ['live', 'clips'] },
|
||||
createEvaluatorContext(),
|
||||
);
|
||||
|
||||
expect(evaluator.evaluate({ view: 'live' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ view: 'clips' }).result).toBeTruthy();
|
||||
expect(evaluator.evaluate({ view: 'timeline' }).result).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { CallConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/call';
|
||||
import { CameraConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/camera';
|
||||
import { DisplayModeConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/display-mode';
|
||||
import { ExpandConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/expand';
|
||||
import { FullscreenConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/fullscreen';
|
||||
import { InteractionConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/interaction';
|
||||
import { KeyConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/key';
|
||||
import { MediaLoadedConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/media-loaded';
|
||||
import { MicrophoneConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/microphone';
|
||||
import { TriggeredConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/triggered';
|
||||
import { ConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/types';
|
||||
import { ViewConditionEvaluator } from '../../../src/condition-trigger/conditions/conditions/view';
|
||||
import { createConditionEvaluatorForTrigger } from '../../../src/condition-trigger/conditions/factory';
|
||||
import { Trigger } from '../../../src/config/schema/condition-trigger/triggers/types';
|
||||
|
||||
type ConditionEvaluatorConstructor = new (...args: never[]) => ConditionEvaluator;
|
||||
|
||||
describe('createConditionEvaluatorForTrigger', () => {
|
||||
it.each<[Trigger, ConditionEvaluatorConstructor]>([
|
||||
[{ trigger: 'call', call: true }, CallConditionEvaluator],
|
||||
[{ trigger: 'camera', cameras: ['front'] }, CameraConditionEvaluator],
|
||||
[{ trigger: 'display_mode', display_mode: 'single' }, DisplayModeConditionEvaluator],
|
||||
[{ trigger: 'expand', expand: true }, ExpandConditionEvaluator],
|
||||
[{ trigger: 'fullscreen', fullscreen: true }, FullscreenConditionEvaluator],
|
||||
[{ trigger: 'interaction', interaction: true }, InteractionConditionEvaluator],
|
||||
[{ trigger: 'key', key: 'a' }, KeyConditionEvaluator],
|
||||
[{ trigger: 'media_loaded', media_loaded: true }, MediaLoadedConditionEvaluator],
|
||||
[{ trigger: 'microphone', muted: true }, MicrophoneConditionEvaluator],
|
||||
[{ trigger: 'triggered', triggered: ['front'] }, TriggeredConditionEvaluator],
|
||||
[{ trigger: 'view', views: ['live'] }, ViewConditionEvaluator],
|
||||
])(
|
||||
'should reuse the matching condition for a valued %o trigger',
|
||||
(trigger, expected) => {
|
||||
expect(createConditionEvaluatorForTrigger(trigger)).toBeInstanceOf(expected);
|
||||
},
|
||||
);
|
||||
|
||||
it.each<[string, Trigger]>([
|
||||
['a valueless trigger fires on any change', { trigger: 'fullscreen' }],
|
||||
['config has no matching condition', { trigger: 'config', paths: ['menu.style'] }],
|
||||
[
|
||||
'stock triggers evaluate themselves',
|
||||
{ trigger: 'state', entity_id: 'binary_sensor.x' },
|
||||
],
|
||||
])('should have no condition when %s', (_description, trigger) => {
|
||||
expect(createConditionEvaluatorForTrigger(trigger)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { mock } from 'vitest-mock-extended';
|
||||
import { ConditionStateManager } from '../../../src/condition-trigger/conditions/state-manager';
|
||||
import {
|
||||
ConditionStateManagerGetEvent,
|
||||
getConditionStateManagerViaEvent,
|
||||
} from '../../../src/condition-trigger/conditions/state-manager-via-event';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('getConditionStateManagerViaEvent', () => {
|
||||
it('should dispatch event and retrieve state manager', () => {
|
||||
const element = document.createElement('div');
|
||||
const stateManager = mock<ConditionStateManager>();
|
||||
|
||||
const handler = vi.fn().mockImplementation((ev: ConditionStateManagerGetEvent) => {
|
||||
ev.conditionStateManager = stateManager;
|
||||
});
|
||||
element.addEventListener(
|
||||
'advanced-camera-card:condition-state-manager:get',
|
||||
handler,
|
||||
);
|
||||
|
||||
expect(getConditionStateManagerViaEvent(element)).toBe(stateManager);
|
||||
});
|
||||
|
||||
it('should dispatch event and retrieve state manager', () => {
|
||||
const element = document.createElement('div');
|
||||
|
||||
const handler = vi.fn();
|
||||
element.addEventListener(
|
||||
'advanced-camera-card:condition-state-manager:get',
|
||||
handler,
|
||||
);
|
||||
|
||||
expect(getConditionStateManagerViaEvent(element)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,159 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ConditionStateManager } from '../../../src/condition-trigger/conditions/state-manager';
|
||||
import {
|
||||
ConditionState,
|
||||
ConditionStateChange,
|
||||
} from '../../../src/condition-trigger/conditions/types';
|
||||
import { createHASS, createStateEntity } from '../../test-utils';
|
||||
|
||||
describe('ConditionStateManager', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should get state', () => {
|
||||
const state = { fullscreen: true };
|
||||
|
||||
const manager = new ConditionStateManager();
|
||||
manager.setState(state);
|
||||
expect(manager.getState()).toEqual(state);
|
||||
});
|
||||
|
||||
describe('should set state', () => {
|
||||
it('should set and be able to get it again', () => {
|
||||
const state = {
|
||||
fullscreen: true,
|
||||
};
|
||||
|
||||
const manager = new ConditionStateManager();
|
||||
|
||||
manager.setState(state);
|
||||
expect(manager.getState()).toEqual(state);
|
||||
});
|
||||
|
||||
it('should set but only trigger when necessary', () => {
|
||||
const listener = vi.fn();
|
||||
const manager = new ConditionStateManager();
|
||||
manager.addListener(listener);
|
||||
|
||||
const state = {
|
||||
fullscreen: true,
|
||||
};
|
||||
|
||||
expect(manager.setState(state)).toBe(true);
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
|
||||
expect(manager.setState(state)).toBe(false);
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
|
||||
expect(manager.setState({ ...state })).toBe(false);
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
|
||||
expect(
|
||||
manager.setState({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity(),
|
||||
}),
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
|
||||
expect(manager.setState({ fullscreen: true })).toBe(false);
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
|
||||
expect(
|
||||
manager.setState({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity(),
|
||||
}),
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
|
||||
expect(manager.setState({ fullscreen: false })).toBe(true);
|
||||
expect(listener).toBeCalledTimes(4);
|
||||
|
||||
expect(manager.setState({ fullscreen: false })).toBe(false);
|
||||
expect(listener).toBeCalledTimes(4);
|
||||
|
||||
expect(
|
||||
manager.setState({
|
||||
hass: createHASS({
|
||||
'binary_sensor.foo': createStateEntity({ state: 'off' }),
|
||||
}),
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(listener).toBeCalledTimes(5);
|
||||
});
|
||||
});
|
||||
|
||||
it('should serialize a state change made from within a listener', () => {
|
||||
const manager = new ConditionStateManager();
|
||||
const seen: ConditionStateChange[] = [];
|
||||
let reentrantReturn: boolean | null | undefined = undefined;
|
||||
let stateDuringDispatch: ConditionState | undefined;
|
||||
|
||||
manager.addListener((change) => {
|
||||
seen.push(change);
|
||||
|
||||
// Reentrantly flip the value the originating change just set, capturing
|
||||
// what is observable mid-dispatch so it can be asserted outside the
|
||||
// callback (where a failure cannot be swallowed).
|
||||
if (reentrantReturn === undefined) {
|
||||
reentrantReturn = manager.setState({ fullscreen: false });
|
||||
stateDuringDispatch = manager.getState();
|
||||
}
|
||||
});
|
||||
|
||||
expect(manager.setState({ fullscreen: true })).toBe(true);
|
||||
|
||||
// The reentrant change is deferred: it returns null and, crucially, is NOT
|
||||
// applied while the originating dispatch is still in flight -- fullscreen is
|
||||
// still true despite the reentrant call to set it false.
|
||||
expect(reentrantReturn).toBeNull();
|
||||
expect(stateDuringDispatch).toEqual({ fullscreen: true });
|
||||
|
||||
// It is dispatched only after the originating change, seeing that change's
|
||||
// result as its own `old`: a coherent edge, not a torn state. Only now does
|
||||
// fullscreen become false.
|
||||
expect(seen).toEqual([
|
||||
{ old: {}, change: { fullscreen: true }, new: { fullscreen: true } },
|
||||
{
|
||||
old: { fullscreen: true },
|
||||
change: { fullscreen: false },
|
||||
new: { fullscreen: false },
|
||||
},
|
||||
]);
|
||||
expect(manager.getState()).toEqual({ fullscreen: false });
|
||||
});
|
||||
|
||||
it('should add listener', () => {
|
||||
const listener = vi.fn();
|
||||
const manager = new ConditionStateManager();
|
||||
|
||||
manager.setState({ fullscreen: true });
|
||||
|
||||
manager.addListener(listener);
|
||||
|
||||
manager.setState({ expand: true });
|
||||
|
||||
expect(listener).toBeCalledWith({
|
||||
old: { fullscreen: true },
|
||||
change: { expand: true },
|
||||
new: { fullscreen: true, expand: true },
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove listener', () => {
|
||||
const listener = vi.fn();
|
||||
const manager = new ConditionStateManager();
|
||||
|
||||
manager.addListener(listener);
|
||||
manager.removeListener(listener);
|
||||
|
||||
const state = { fullscreen: true };
|
||||
manager.setState(state);
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user