@ajclarkson/homerun 0.0.1-edge.db0717b → 0.0.1-edge.efd408a
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 +8 -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 +78 -27
- package/dist/src/testing.d.ts +2 -0
- package/dist/src/testing.js +2 -2
- package/dist/src/types/automation.d.ts +1 -0
- package/dist/src/types/triggers.d.ts +1 -1
- package/package.json +1 -1
|
@@ -51,7 +51,8 @@ export class ApiServer {
|
|
|
51
51
|
id: a.id,
|
|
52
52
|
location: a.location,
|
|
53
53
|
subsystem: a.subsystem,
|
|
54
|
-
|
|
54
|
+
enabled: a.enabled ?? true,
|
|
55
|
+
triggers: a.triggers.map(serializeTrigger),
|
|
55
56
|
}));
|
|
56
57
|
json(res, 200, automations);
|
|
57
58
|
}
|
|
@@ -100,6 +101,12 @@ export class ApiServer {
|
|
|
100
101
|
req.on('close', unsubscribe);
|
|
101
102
|
}
|
|
102
103
|
}
|
|
104
|
+
function serializeTrigger(trigger) {
|
|
105
|
+
if (trigger.type === 'state_changed' && trigger.entity instanceof RegExp) {
|
|
106
|
+
return { ...trigger, entity: trigger.entity.toString() };
|
|
107
|
+
}
|
|
108
|
+
return trigger;
|
|
109
|
+
}
|
|
103
110
|
function json(res, status, body) {
|
|
104
111
|
const payload = JSON.stringify(body);
|
|
105
112
|
res.writeHead(status, { 'Content-Type': 'application/json' });
|
|
@@ -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;
|
|
@@ -1,49 +1,72 @@
|
|
|
1
|
-
const DOUBLE_PRESS_WINDOW_MS =
|
|
1
|
+
const DOUBLE_PRESS_WINDOW_MS = 250;
|
|
2
2
|
class ButtonGestureHandler {
|
|
3
3
|
entityId;
|
|
4
4
|
dispatch;
|
|
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
|
}
|
package/dist/src/testing.d.ts
CHANGED
package/dist/src/testing.js
CHANGED
|
@@ -9,8 +9,8 @@ export function testAutomation(automation, options) {
|
|
|
9
9
|
entity_id: entityId,
|
|
10
10
|
state: entry.state,
|
|
11
11
|
attributes: entry.attributes ?? {},
|
|
12
|
-
last_changed: '',
|
|
13
|
-
last_updated: '',
|
|
12
|
+
last_changed: entry.last_changed ?? '',
|
|
13
|
+
last_updated: entry.last_updated ?? '',
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
16
|
const haContext = {
|