feat: Add event-based automation triggers (#2537)
This commit is contained in:
committed by
dermotduffy
parent
b701366762
commit
a31816c168
@@ -1,8 +1,15 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mock } from 'vitest-mock-extended';
|
||||
import { ActionsExecutionRequest } from '../../src/card-controller/actions/types.js';
|
||||
import { AutomationsManager } from '../../src/card-controller/automations-manager.js';
|
||||
import { EventWatcherSubscriptionInterface } from '../../src/card-controller/hass/event-watcher.js';
|
||||
import { ConditionStateManager } from '../../src/condition-trigger/conditions/state-manager.js';
|
||||
import { createCardAPI, flushPromises } from '../test-utils.js';
|
||||
import {
|
||||
createCardAPI,
|
||||
createHASS,
|
||||
createHASSEvent,
|
||||
flushPromises,
|
||||
} from '../test-utils.js';
|
||||
|
||||
describe('AutomationsManager', () => {
|
||||
const actions = [
|
||||
@@ -264,6 +271,40 @@ describe('AutomationsManager', () => {
|
||||
expect(api.getActionsManager().executeActions).toBeCalledTimes(10);
|
||||
});
|
||||
|
||||
it('should execute actions on a matching HA bus event trigger', () => {
|
||||
const api = createCardAPI();
|
||||
const eventWatcher = mock<EventWatcherSubscriptionInterface>();
|
||||
vi.mocked(eventWatcher.subscribe).mockResolvedValue();
|
||||
vi.mocked(eventWatcher.unsubscribe).mockResolvedValue();
|
||||
vi.mocked(api.getHASSManager().hasHASS).mockReturnValue(true);
|
||||
vi.mocked(api.getHASSManager().getHASS).mockReturnValue(createHASS());
|
||||
vi.mocked(api.getHASSManager().getEventWatcher).mockReturnValue(eventWatcher);
|
||||
vi.mocked(api.getInitializationManager().isInitializedMandatory).mockReturnValue(
|
||||
true,
|
||||
);
|
||||
const stateManager = new ConditionStateManager();
|
||||
vi.mocked(api.getConditionStateManager).mockReturnValue(stateManager);
|
||||
|
||||
const automationsManager = new AutomationsManager(api);
|
||||
automationsManager.addAutomations([
|
||||
{
|
||||
triggers: [{ trigger: 'event' as const, event_type: 'zha_event' }],
|
||||
actions: actions,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(eventWatcher.subscribe).toBeCalledTimes(1);
|
||||
|
||||
// Simulate an event arrival.
|
||||
const event = createHASSEvent('zha_event', { command: 'press' });
|
||||
vi.mocked(eventWatcher.subscribe).mock.calls[0][0].callback(event);
|
||||
|
||||
expect(api.getActionsManager().executeActions).toBeCalledTimes(1);
|
||||
expect(
|
||||
vi.mocked(api.getActionsManager().executeActions).mock.calls[0][0].triggerData,
|
||||
).toEqual({ platform: 'event', event });
|
||||
});
|
||||
|
||||
it('should delete automations', () => {
|
||||
const api = createCardAPI();
|
||||
vi.mocked(api.getHASSManager().hasHASS).mockReturnValue(true);
|
||||
|
||||
Reference in New Issue
Block a user