@ajclarkson/homerun 0.0.1-edge.f1d8985 → 0.0.1-edge.fcf7bc5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/framework/api-server.js +1 -0
- package/dist/src/framework/hot-reload.js +2 -1
- package/dist/src/framework/pipeline.js +5 -0
- package/dist/src/framework/trigger-engine.d.ts +1 -0
- package/dist/src/framework/trigger-engine.js +77 -26
- package/dist/src/types/automation.d.ts +1 -0
- package/dist/src/types/triggers.d.ts +1 -1
- package/package.json +1 -1
|
@@ -45,11 +45,12 @@ export function _deleteFile(filePath, registry, fileToIds = moduleFileToIds) {
|
|
|
45
45
|
}
|
|
46
46
|
fileToIds.delete(filePath);
|
|
47
47
|
}
|
|
48
|
+
const EXCLUDED_DIRS = new Set(['types', 'lib']);
|
|
48
49
|
const isAutomationFile = (f) => f.endsWith('.ts') &&
|
|
49
50
|
!f.endsWith('.test.ts') &&
|
|
50
51
|
!f.includes('node_modules') &&
|
|
51
52
|
!f.includes('.d.ts') &&
|
|
52
|
-
!f.split(path.sep).
|
|
53
|
+
!f.split(path.sep).some(seg => EXCLUDED_DIRS.has(seg));
|
|
53
54
|
export async function rescanAutomations(automationsDir, registry, fileToIds = moduleFileToIds, importer = defaultImporter) {
|
|
54
55
|
let files = [];
|
|
55
56
|
try {
|
|
@@ -11,6 +11,11 @@ export async function runPipeline(automation, event, haClient, deps) {
|
|
|
11
11
|
timestamp,
|
|
12
12
|
...(deps.dryRun ? { dry_run: true } : {}),
|
|
13
13
|
};
|
|
14
|
+
// Step 1: Enabled check
|
|
15
|
+
if (automation.enabled === false) {
|
|
16
|
+
deps.observability.publishDecision({ ...base, event_type: 'abort', reason: 'disabled' });
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
14
19
|
// Step 2: Context
|
|
15
20
|
let ctx;
|
|
16
21
|
try {
|
|
@@ -13,6 +13,7 @@ export declare class TriggerEngine {
|
|
|
13
13
|
private readonly onMatch;
|
|
14
14
|
private readonly mqttClient?;
|
|
15
15
|
private readonly buttonHandlers;
|
|
16
|
+
private regexButtonTriggers;
|
|
16
17
|
private readonly durationTimers;
|
|
17
18
|
constructor(registry: AutomationRegistry, haClient: HAClient, onMatch: (automation: Automation<unknown>, event: TriggerEvent) => void, mqttClient?: MqttClient | undefined);
|
|
18
19
|
private rebuildButtonHandlers;
|
|
@@ -5,45 +5,68 @@ class ButtonGestureHandler {
|
|
|
5
5
|
supportsDoublePress;
|
|
6
6
|
gestureState = 'idle';
|
|
7
7
|
resolveTimer = null;
|
|
8
|
+
holdFired = false;
|
|
9
|
+
// Set when 'release' fires a short press; suppresses the trailing confirmation
|
|
10
|
+
// event ('on', 'toggle', etc.) so the same physical press doesn't fire twice.
|
|
11
|
+
shortFired = false;
|
|
8
12
|
constructor(entityId, dispatch, supportsDoublePress) {
|
|
9
13
|
this.entityId = entityId;
|
|
10
14
|
this.dispatch = dispatch;
|
|
11
15
|
this.supportsDoublePress = supportsDoublePress;
|
|
12
16
|
}
|
|
13
17
|
handle(actionState, correlationId) {
|
|
18
|
+
if (actionState === '')
|
|
19
|
+
return;
|
|
14
20
|
const parsed = parseButtonAction(actionState);
|
|
15
21
|
if (!parsed)
|
|
16
22
|
return;
|
|
17
23
|
const { button, pressType } = parsed;
|
|
24
|
+
const isRelease = actionState.toLowerCase() === 'release';
|
|
25
|
+
if (pressType === 'hold') {
|
|
26
|
+
// Cancel any pending double-press window and fire hold exactly once per physical hold.
|
|
27
|
+
if (this.resolveTimer) {
|
|
28
|
+
clearTimeout(this.resolveTimer);
|
|
29
|
+
this.resolveTimer = null;
|
|
30
|
+
this.gestureState = 'idle';
|
|
31
|
+
}
|
|
32
|
+
if (!this.holdFired) {
|
|
33
|
+
this.holdFired = true;
|
|
34
|
+
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'hold', button, correlation_id: correlationId });
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
// pressType === 'short'
|
|
39
|
+
if (isRelease && this.holdFired) {
|
|
40
|
+
// Physical release after a hold — reset hold state, don't fire short press.
|
|
41
|
+
this.holdFired = false;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!isRelease && this.shortFired) {
|
|
45
|
+
// Confirmation event ('on', 'toggle', etc.) after 'release' already fired — suppress.
|
|
46
|
+
this.shortFired = false;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (isRelease)
|
|
50
|
+
this.shortFired = true;
|
|
18
51
|
if (this.gestureState === 'idle') {
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'single_press', button, correlation_id: correlationId });
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
this.gestureState = 'resolving';
|
|
25
|
-
this.resolveTimer = setTimeout(() => {
|
|
26
|
-
this.resolveTimer = null;
|
|
27
|
-
this.gestureState = 'idle';
|
|
28
|
-
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'single_press', button, correlation_id: correlationId });
|
|
29
|
-
}, DOUBLE_PRESS_WINDOW_MS);
|
|
30
|
-
}
|
|
52
|
+
if (!this.supportsDoublePress) {
|
|
53
|
+
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'single_press', button, correlation_id: correlationId });
|
|
31
54
|
}
|
|
32
55
|
else {
|
|
33
|
-
this.
|
|
56
|
+
this.gestureState = 'resolving';
|
|
57
|
+
this.resolveTimer = setTimeout(() => {
|
|
58
|
+
this.resolveTimer = null;
|
|
59
|
+
this.gestureState = 'idle';
|
|
60
|
+
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'single_press', button, correlation_id: correlationId });
|
|
61
|
+
}, DOUBLE_PRESS_WINDOW_MS);
|
|
34
62
|
}
|
|
35
63
|
}
|
|
36
64
|
else {
|
|
37
|
-
// resolving —
|
|
65
|
+
// resolving — second short press = double press
|
|
38
66
|
clearTimeout(this.resolveTimer);
|
|
39
67
|
this.resolveTimer = null;
|
|
40
68
|
this.gestureState = 'idle';
|
|
41
|
-
|
|
42
|
-
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'double_press', button, correlation_id: correlationId });
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'hold', button, correlation_id: correlationId });
|
|
46
|
-
}
|
|
69
|
+
this.dispatch({ type: 'button', entity_id: this.entityId, gesture: 'double_press', button, correlation_id: correlationId });
|
|
47
70
|
}
|
|
48
71
|
}
|
|
49
72
|
}
|
|
@@ -57,7 +80,7 @@ export function parseButtonAction(state) {
|
|
|
57
80
|
const action = prefixed ? prefixed[2] : s;
|
|
58
81
|
if (/long|hold/.test(action))
|
|
59
82
|
return { button, pressType: 'hold' };
|
|
60
|
-
if (/short|click|single/.test(action) || action === '
|
|
83
|
+
if (/short|click|single/.test(action) || action === 'on' || action === 'toggle' || action === 'release') {
|
|
61
84
|
return { button, pressType: 'short' };
|
|
62
85
|
}
|
|
63
86
|
return null;
|
|
@@ -69,6 +92,7 @@ export class TriggerEngine {
|
|
|
69
92
|
onMatch;
|
|
70
93
|
mqttClient;
|
|
71
94
|
buttonHandlers = new Map();
|
|
95
|
+
regexButtonTriggers = [];
|
|
72
96
|
durationTimers = new Map();
|
|
73
97
|
constructor(registry, haClient, onMatch, mqttClient) {
|
|
74
98
|
this.registry = registry;
|
|
@@ -80,26 +104,50 @@ export class TriggerEngine {
|
|
|
80
104
|
}
|
|
81
105
|
rebuildButtonHandlers() {
|
|
82
106
|
this.buttonHandlers.clear();
|
|
107
|
+
this.regexButtonTriggers = [];
|
|
83
108
|
const entityGestures = new Map();
|
|
109
|
+
const regexGestures = new Map();
|
|
84
110
|
for (const automation of this.registry.getAll()) {
|
|
85
111
|
for (const trigger of automation.triggers) {
|
|
86
112
|
if (trigger.type === 'button') {
|
|
87
|
-
if (
|
|
88
|
-
entityGestures.
|
|
113
|
+
if (typeof trigger.entity === 'string') {
|
|
114
|
+
if (!entityGestures.has(trigger.entity))
|
|
115
|
+
entityGestures.set(trigger.entity, new Set());
|
|
116
|
+
entityGestures.get(trigger.entity).add(trigger.gesture);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
if (!regexGestures.has(trigger.entity))
|
|
120
|
+
regexGestures.set(trigger.entity, new Set());
|
|
121
|
+
regexGestures.get(trigger.entity).add(trigger.gesture);
|
|
89
122
|
}
|
|
90
|
-
entityGestures.get(trigger.entity).add(trigger.gesture);
|
|
91
123
|
}
|
|
92
124
|
}
|
|
93
125
|
}
|
|
94
126
|
for (const [entity, gestures] of entityGestures) {
|
|
95
127
|
this.buttonHandlers.set(entity, new ButtonGestureHandler(entity, (e) => this.dispatch(e), gestures.has('double_press')));
|
|
96
128
|
}
|
|
129
|
+
for (const [pattern, gestures] of regexGestures) {
|
|
130
|
+
this.regexButtonTriggers.push({ pattern, gestures });
|
|
131
|
+
}
|
|
97
132
|
}
|
|
98
133
|
start() {
|
|
99
134
|
this.haClient.ready
|
|
100
135
|
.then(() => {
|
|
101
136
|
this.haClient.on('state_changed', (event) => {
|
|
102
|
-
|
|
137
|
+
let handler = this.buttonHandlers.get(event.entity_id);
|
|
138
|
+
if (!handler) {
|
|
139
|
+
const matchingGestures = new Set();
|
|
140
|
+
for (const { pattern, gestures } of this.regexButtonTriggers) {
|
|
141
|
+
if (pattern.test(event.entity_id)) {
|
|
142
|
+
for (const g of gestures)
|
|
143
|
+
matchingGestures.add(g);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (matchingGestures.size > 0) {
|
|
147
|
+
handler = new ButtonGestureHandler(event.entity_id, (e) => this.dispatch(e), matchingGestures.has('double_press'));
|
|
148
|
+
this.buttonHandlers.set(event.entity_id, handler);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
103
151
|
if (handler) {
|
|
104
152
|
handler.handle(event.new_state.state, event.correlation_id);
|
|
105
153
|
}
|
|
@@ -200,7 +248,10 @@ function matchesTrigger(trigger, event) {
|
|
|
200
248
|
case 'button': {
|
|
201
249
|
if (event.type !== 'button')
|
|
202
250
|
return false;
|
|
203
|
-
|
|
251
|
+
const entityMatch = typeof trigger.entity === 'string'
|
|
252
|
+
? trigger.entity === event.entity_id
|
|
253
|
+
: trigger.entity.test(event.entity_id);
|
|
254
|
+
return (entityMatch &&
|
|
204
255
|
trigger.gesture === event.gesture &&
|
|
205
256
|
(trigger.button === undefined || trigger.button === event.button));
|
|
206
257
|
}
|