@camstack/types 1.2.547 → 1.2.548
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/CHANGELOG.md +8 -0
- package/dist/automation/condition-eval.d.ts +24 -0
- package/dist/automation/hold.d.ts +11 -0
- package/dist/automation/index.d.ts +6 -4
- package/dist/automation/recipe.d.ts +5 -0
- package/dist/automation/step-condition.d.ts +7 -2
- package/dist/automation/trace.d.ts +6 -0
- package/dist/automation/trigger-variables.d.ts +3 -1
- package/dist/automation/when-site.d.ts +27 -0
- package/dist/automation/when.d.ts +107 -1
- package/dist/capabilities/actions.cap.d.ts +2 -0
- package/dist/capabilities/core-blocks.cap.d.ts +6 -0
- package/dist/capabilities/schedulers.cap.d.ts +2 -0
- package/dist/generated/addon-api.d.ts +20 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +508 -42
- package/dist/index.mjs +497 -43
- package/dist/time/index.d.ts +1 -0
- package/dist/time/time-spec.d.ts +5 -0
- package/dist/time/time-window.d.ts +33 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -32304,7 +32304,7 @@ function weekdayOfLocalDate(localDate) {
|
|
|
32304
32304
|
const { y, m, d } = parseLocalDate(localDate);
|
|
32305
32305
|
return WEEKDAY_BY_INDEX[new Date(Date.UTC(y, m - 1, d)).getUTCDay()] ?? "sun";
|
|
32306
32306
|
}
|
|
32307
|
-
function addDays(localDate, days) {
|
|
32307
|
+
function addDays$1(localDate, days) {
|
|
32308
32308
|
const { y, m, d } = parseLocalDate(localDate);
|
|
32309
32309
|
return new Date(Date.UTC(y, m - 1, d + days)).toISOString().slice(0, 10);
|
|
32310
32310
|
}
|
|
@@ -32368,9 +32368,9 @@ function nextOccurrence(spec, afterMs, site, options = {}) {
|
|
|
32368
32368
|
const at = occurrenceOn(spec, own, site);
|
|
32369
32369
|
return at !== null && at > afterMs ? at : null;
|
|
32370
32370
|
}
|
|
32371
|
-
const first = addDays(localDateOf(afterMs, site.timeZone), -1);
|
|
32371
|
+
const first = addDays$1(localDateOf(afterMs, site.timeZone), -1);
|
|
32372
32372
|
for (let i = 0; i < 400; i += 1) {
|
|
32373
|
-
const localDate = addDays(first, i);
|
|
32373
|
+
const localDate = addDays$1(first, i);
|
|
32374
32374
|
if (days !== void 0 && !days.includes(weekdayOfLocalDate(localDate))) continue;
|
|
32375
32375
|
const at = occurrenceOn(spec, localDate, site);
|
|
32376
32376
|
if (at !== null && at > afterMs) return at;
|
|
@@ -32414,7 +32414,8 @@ function zoneOnlySite(timeZone) {
|
|
|
32414
32414
|
return {
|
|
32415
32415
|
lat: 0,
|
|
32416
32416
|
lon: 0,
|
|
32417
|
-
timeZone
|
|
32417
|
+
timeZone,
|
|
32418
|
+
zoneOnly: true
|
|
32418
32419
|
};
|
|
32419
32420
|
}
|
|
32420
32421
|
//#endregion
|
|
@@ -32607,6 +32608,8 @@ var BecomesWatch = class {
|
|
|
32607
32608
|
spec;
|
|
32608
32609
|
deps;
|
|
32609
32610
|
previous = void 0;
|
|
32611
|
+
/** The value the field left at its last change — a re-report does not move it. */
|
|
32612
|
+
from = void 0;
|
|
32610
32613
|
seeded = false;
|
|
32611
32614
|
core;
|
|
32612
32615
|
handle = void 0;
|
|
@@ -32651,13 +32654,15 @@ var BecomesWatch = class {
|
|
|
32651
32654
|
const previous = this.previous;
|
|
32652
32655
|
const first = !this.seeded;
|
|
32653
32656
|
this.seeded = true;
|
|
32657
|
+
if (!first && value !== previous) this.from = previous;
|
|
32654
32658
|
this.previous = value;
|
|
32655
32659
|
const holds = predicateOf(this.spec, value);
|
|
32656
32660
|
if (this.core === void 0) {
|
|
32657
32661
|
if (first) return;
|
|
32658
32662
|
if (holds === void 0 ? value !== previous : holds && predicateOf(this.spec, previous) !== true) this.deps.emit({
|
|
32659
32663
|
kind: "fire",
|
|
32660
|
-
value
|
|
32664
|
+
value,
|
|
32665
|
+
from: this.from
|
|
32661
32666
|
});
|
|
32662
32667
|
return;
|
|
32663
32668
|
}
|
|
@@ -32713,6 +32718,7 @@ var BecomesWatch = class {
|
|
|
32713
32718
|
this.deps.emit({
|
|
32714
32719
|
kind: "fire",
|
|
32715
32720
|
value: this.previous,
|
|
32721
|
+
from: this.from,
|
|
32716
32722
|
hold: this.trace("fired", reading)
|
|
32717
32723
|
});
|
|
32718
32724
|
}
|
|
@@ -32775,6 +32781,13 @@ var AutomationConditionOperatorSchema = zod.z.enum([
|
|
|
32775
32781
|
"in"
|
|
32776
32782
|
]);
|
|
32777
32783
|
/**
|
|
32784
|
+
* The stable name of an event leaf. A path (`when.0.1`) moves when a row is
|
|
32785
|
+
* reordered; the id does not, so a `triggered-by` leaf keeps pointing at the
|
|
32786
|
+
* same row. Optional: a leaf nothing refers to needs none.
|
|
32787
|
+
*/
|
|
32788
|
+
var EVENT_LEAF_ID_RE = /^[A-Za-z0-9_-]{1,40}$/;
|
|
32789
|
+
var eventLeafId = { id: zod.z.string().regex(EVENT_LEAF_ID_RE).optional() };
|
|
32790
|
+
/**
|
|
32778
32791
|
* D8 compliance, and it is the reason a "becomes" leaf is not merely an event
|
|
32779
32792
|
* subscription: it evaluates against the **state mirror**, which is reconciled,
|
|
32780
32793
|
* and an event only WAKES the evaluation. A dropped event therefore DELAYS a
|
|
@@ -32782,6 +32795,7 @@ var AutomationConditionOperatorSchema = zod.z.enum([
|
|
|
32782
32795
|
*/
|
|
32783
32796
|
var DeviceBecomesLeafSchema = zod.z.object({
|
|
32784
32797
|
kind: zod.z.literal("device-becomes"),
|
|
32798
|
+
...eventLeafId,
|
|
32785
32799
|
deviceId: zod.z.number().int().nonnegative(),
|
|
32786
32800
|
cap: zod.z.string().min(1),
|
|
32787
32801
|
fieldPath: zod.z.string().min(1),
|
|
@@ -32834,6 +32848,7 @@ var DeviceBecomesLeafSchema = zod.z.object({
|
|
|
32834
32848
|
});
|
|
32835
32849
|
var DeviceEventLeafSchema = zod.z.object({
|
|
32836
32850
|
kind: zod.z.literal("device-event"),
|
|
32851
|
+
...eventLeafId,
|
|
32837
32852
|
/** An `EventCategory` value. */
|
|
32838
32853
|
category: zod.z.string().min(1),
|
|
32839
32854
|
deviceId: zod.z.number().int().nonnegative().optional()
|
|
@@ -32846,18 +32861,24 @@ var DeviceEventLeafSchema = zod.z.object({
|
|
|
32846
32861
|
*/
|
|
32847
32862
|
var ScheduleLeafSchema = zod.z.object({
|
|
32848
32863
|
kind: zod.z.literal("schedule"),
|
|
32864
|
+
...eventLeafId,
|
|
32849
32865
|
at: TimeSpecSchema.optional(),
|
|
32850
32866
|
days: zod.z.array(WeekdayKeySchema).min(1).max(7).optional(),
|
|
32851
32867
|
/** @deprecated legacy cron — new schedules use `at`. */
|
|
32852
32868
|
cron: zod.z.string().min(1).max(120).optional()
|
|
32853
32869
|
}).refine((leaf) => leaf.at === void 0 !== (leaf.cron === void 0), { message: "a schedule names exactly one of `at` (a time spec) and `cron` (legacy)" }).refine((leaf) => leaf.days === void 0 || leaf.at !== void 0, { message: "`days` goes with `at`; a cron expression carries its own days" });
|
|
32854
|
-
var ManualLeafSchema = zod.z.object({
|
|
32870
|
+
var ManualLeafSchema = zod.z.object({
|
|
32871
|
+
kind: zod.z.literal("manual"),
|
|
32872
|
+
...eventLeafId
|
|
32873
|
+
});
|
|
32855
32874
|
var IntervalLeafSchema = zod.z.object({
|
|
32856
32875
|
kind: zod.z.literal("interval"),
|
|
32876
|
+
...eventLeafId,
|
|
32857
32877
|
minutes: zod.z.number().int().min(1).max(1440)
|
|
32858
32878
|
});
|
|
32859
32879
|
var DetectionLeafSchema = zod.z.object({
|
|
32860
32880
|
kind: zod.z.literal("detection"),
|
|
32881
|
+
...eventLeafId,
|
|
32861
32882
|
/** One macro class each, optionally narrowed by a label; a track of any listed subject fires. */
|
|
32862
32883
|
subjects: zod.z.array(DetectionSubjectSchema).min(1),
|
|
32863
32884
|
deviceIds: zod.z.array(zod.z.number().int().nonnegative()).min(1),
|
|
@@ -32868,6 +32889,7 @@ var DetectionLeafSchema = zod.z.object({
|
|
|
32868
32889
|
});
|
|
32869
32890
|
var StationaryLeafSchema = zod.z.object({
|
|
32870
32891
|
kind: zod.z.literal("stationary"),
|
|
32892
|
+
...eventLeafId,
|
|
32871
32893
|
deviceIds: zod.z.array(zod.z.number().int().nonnegative()).min(1),
|
|
32872
32894
|
subjects: zod.z.array(StationarySubjectSchema).min(1).optional(),
|
|
32873
32895
|
zones: zod.z.array(zod.z.string().min(1)).min(1).optional(),
|
|
@@ -32913,6 +32935,43 @@ var StationaryPresentLeafSchema = zod.z.object({
|
|
|
32913
32935
|
minCount: zod.z.number().int().positive().optional()
|
|
32914
32936
|
});
|
|
32915
32937
|
/**
|
|
32938
|
+
* True while the site-local clock is inside a daily window and/or on one of
|
|
32939
|
+
* the named days. `days` names the local day a window STARTS on, so
|
|
32940
|
+
* 22:00-06:00 on weekdays is still inside at 01:00 Saturday. A one-date spec
|
|
32941
|
+
* (`kind: 'date'`) is refused: only a clock time or a sun event makes a window
|
|
32942
|
+
* that repeats every day.
|
|
32943
|
+
*/
|
|
32944
|
+
var TimeIsLeafSchema = zod.z.object({
|
|
32945
|
+
kind: zod.z.literal("time-is"),
|
|
32946
|
+
between: zod.z.object({
|
|
32947
|
+
from: TimeSpecSchema,
|
|
32948
|
+
to: TimeSpecSchema
|
|
32949
|
+
}).optional(),
|
|
32950
|
+
days: zod.z.array(WeekdayKeySchema).min(1).max(7).optional()
|
|
32951
|
+
}).superRefine((leaf, ctx) => {
|
|
32952
|
+
if (leaf.between === void 0 && leaf.days === void 0) ctx.addIssue({
|
|
32953
|
+
code: "custom",
|
|
32954
|
+
message: "a time-is leaf needs a between window, days, or both"
|
|
32955
|
+
});
|
|
32956
|
+
for (const end of ["from", "to"]) if (leaf.between?.[end].kind === "date") ctx.addIssue({
|
|
32957
|
+
code: "custom",
|
|
32958
|
+
message: "a one-date time cannot bound a daily window: use a clock time or a sun event",
|
|
32959
|
+
path: ["between", end]
|
|
32960
|
+
});
|
|
32961
|
+
});
|
|
32962
|
+
/** How many event leaves one `triggered-by` names. */
|
|
32963
|
+
var TRIGGERED_BY_MAX_IDS = 20;
|
|
32964
|
+
/**
|
|
32965
|
+
* True for the run that one of the named event leaves started. It reads the
|
|
32966
|
+
* run, not a device: in the tree it is judged against the leaf that fired, in
|
|
32967
|
+
* a step guard against the trigger the run carries. A run no event leaf
|
|
32968
|
+
* started (a state-only tree becoming true) is false.
|
|
32969
|
+
*/
|
|
32970
|
+
var TriggeredByLeafSchema = zod.z.object({
|
|
32971
|
+
kind: zod.z.literal("triggered-by"),
|
|
32972
|
+
leafIds: zod.z.array(zod.z.string().regex(EVENT_LEAF_ID_RE)).min(1).max(20)
|
|
32973
|
+
});
|
|
32974
|
+
/**
|
|
32916
32975
|
* The escape hatch: the bounded expression engine over named device bindings.
|
|
32917
32976
|
* Declared as a plain object so a discriminated union can read `kind`; the
|
|
32918
32977
|
* author-time validation is the SAME function `ExpressionSourceSchema` runs.
|
|
@@ -32958,7 +33017,9 @@ var AutomationWhenSchema = zod.z.lazy(() => zod.z.discriminatedUnion("kind", [
|
|
|
32958
33017
|
StationaryLeafSchema,
|
|
32959
33018
|
DeviceIsLeafSchema,
|
|
32960
33019
|
StationaryPresentLeafSchema,
|
|
32961
|
-
ExpressionLeafSchema
|
|
33020
|
+
ExpressionLeafSchema,
|
|
33021
|
+
TimeIsLeafSchema,
|
|
33022
|
+
TriggeredByLeafSchema
|
|
32962
33023
|
]));
|
|
32963
33024
|
var EVENT_KINDS = new Set([
|
|
32964
33025
|
"device-becomes",
|
|
@@ -33012,6 +33073,46 @@ function hasEventLeaves(node) {
|
|
|
33012
33073
|
});
|
|
33013
33074
|
return found;
|
|
33014
33075
|
}
|
|
33076
|
+
/** The event leaves of the tree that carry an id, in tree order. */
|
|
33077
|
+
function identifiedEventLeaves(when) {
|
|
33078
|
+
const found = [];
|
|
33079
|
+
forEachLeaf(when, (leaf, path) => {
|
|
33080
|
+
if (isEventLeaf(leaf) && leaf.id !== void 0) found.push({
|
|
33081
|
+
id: leaf.id,
|
|
33082
|
+
path,
|
|
33083
|
+
leaf
|
|
33084
|
+
});
|
|
33085
|
+
});
|
|
33086
|
+
return found;
|
|
33087
|
+
}
|
|
33088
|
+
/** The id of the event leaf at `path` (`when.0.1`); null when the leaf has none or the path names no event leaf. */
|
|
33089
|
+
function eventLeafIdAt(when, path) {
|
|
33090
|
+
return identifiedEventLeaves(when).find((entry) => entry.path === path)?.id ?? null;
|
|
33091
|
+
}
|
|
33092
|
+
/** Does the tree hold a `triggered-by` leaf? */
|
|
33093
|
+
function hasTriggeredByLeaves(when) {
|
|
33094
|
+
let found = false;
|
|
33095
|
+
forEachLeaf(when, (leaf) => {
|
|
33096
|
+
if (leaf.kind === "triggered-by") found = true;
|
|
33097
|
+
});
|
|
33098
|
+
return found;
|
|
33099
|
+
}
|
|
33100
|
+
/** Why a `triggered-by` leaf is refused outside an automation: there is no trigger row to name. */
|
|
33101
|
+
var TRIGGERED_BY_NEEDS_AUTOMATION = "\"triggered by\" reads which trigger started an automation — an action or a scheduler has no trigger rows, so it could never be true here";
|
|
33102
|
+
/**
|
|
33103
|
+
* What is wrong with the `triggered-by` leaves of `tree`, given the ids the
|
|
33104
|
+
* automation's event leaves carry: an id nothing carries can never be true.
|
|
33105
|
+
* `where` names the tree in the message (`the rules`, `step 2`).
|
|
33106
|
+
*/
|
|
33107
|
+
function triggeredByIssue(tree, knownIds, where) {
|
|
33108
|
+
let issue = null;
|
|
33109
|
+
forEachLeaf(tree, (leaf) => {
|
|
33110
|
+
if (issue !== null || leaf.kind !== "triggered-by") return;
|
|
33111
|
+
const missing = leaf.leafIds.find((id) => !knownIds.has(id));
|
|
33112
|
+
if (missing !== void 0) issue = `${where}: "triggered by" names the trigger "${missing}", and no trigger row of this automation has that id — pick one of its trigger rows`;
|
|
33113
|
+
});
|
|
33114
|
+
return issue;
|
|
33115
|
+
}
|
|
33015
33116
|
function hasEmptyContainer(node) {
|
|
33016
33117
|
if (isLeaf(node)) return false;
|
|
33017
33118
|
if (node.kind === "not") return hasEmptyContainer(node.child);
|
|
@@ -33070,6 +33171,11 @@ function validateWhenTree(when) {
|
|
|
33070
33171
|
if (depth > 4) return `the rules nest ${depth} levels deep — the limit is 4. "not (any of …)" is the same as "all of (not …)", which is one level shallower`;
|
|
33071
33172
|
const leaves = countWhenLeaves(when);
|
|
33072
33173
|
if (leaves > 32) return `the rules use ${leaves} rows — the limit is 32`;
|
|
33174
|
+
const seen = /* @__PURE__ */ new Set();
|
|
33175
|
+
for (const entry of identifiedEventLeaves(when)) {
|
|
33176
|
+
if (seen.has(entry.id)) return `two trigger rows share the id "${entry.id}" — an id names one row`;
|
|
33177
|
+
seen.add(entry.id);
|
|
33178
|
+
}
|
|
33073
33179
|
const need = eventNeed(when, "when", false);
|
|
33074
33180
|
if ("error" in need) return need.error;
|
|
33075
33181
|
const [first, second] = need.paths;
|
|
@@ -33405,7 +33511,18 @@ var GuardedCapStepSchema = AutomationCapStepBaseSchema.extend(stepGuardShape).re
|
|
|
33405
33511
|
message: "a domain target needs the vocabulary action it applies to (actionId)",
|
|
33406
33512
|
path: ["actionId"]
|
|
33407
33513
|
}).refine((step) => step.else === void 0 || step.if !== void 0, elseNeedsIf);
|
|
33408
|
-
|
|
33514
|
+
/**
|
|
33515
|
+
* A step of a STANDALONE list (an action, a scheduler point, a preset): the
|
|
33516
|
+
* same two step kinds an automation runs, minus a guard that reads the
|
|
33517
|
+
* automation's trigger, which such a list does not have.
|
|
33518
|
+
*/
|
|
33519
|
+
var ActionStepSchema = zod.z.discriminatedUnion("kind", [GuardedWaitStepSchema, GuardedCapStepSchema]).superRefine((step, ctx) => {
|
|
33520
|
+
if (step.if !== void 0 && hasTriggeredByLeaves(step.if)) ctx.addIssue({
|
|
33521
|
+
code: "custom",
|
|
33522
|
+
message: TRIGGERED_BY_NEEDS_AUTOMATION,
|
|
33523
|
+
path: ["if"]
|
|
33524
|
+
});
|
|
33525
|
+
});
|
|
33409
33526
|
var AutomationActionSchema = zod.z.discriminatedUnion("kind", [
|
|
33410
33527
|
GuardedWaitStepSchema,
|
|
33411
33528
|
GuardedCapStepSchema,
|
|
@@ -33501,7 +33618,104 @@ var MAX_CONDITION_LEAVES = 32;
|
|
|
33501
33618
|
*/
|
|
33502
33619
|
function validateRecipeBounds(recipe) {
|
|
33503
33620
|
if (recipe.actions.length === 0) return "an automation needs at least one action — a rule that does nothing is not a rule";
|
|
33504
|
-
|
|
33621
|
+
const tree = validateWhenTree(recipe.when);
|
|
33622
|
+
if (tree !== null) return tree;
|
|
33623
|
+
const ids = new Set(identifiedEventLeaves(recipe.when).map((entry) => entry.id));
|
|
33624
|
+
const inRules = triggeredByIssue(recipe.when, ids, "the rules");
|
|
33625
|
+
if (inRules !== null) return inRules;
|
|
33626
|
+
for (const [index, action] of recipe.actions.entries()) {
|
|
33627
|
+
if (action.kind !== "wait" && action.kind !== "cap" || action.if === void 0) continue;
|
|
33628
|
+
const issue = triggeredByIssue(action.if, ids, `step ${index + 1}`);
|
|
33629
|
+
if (issue !== null) return issue;
|
|
33630
|
+
}
|
|
33631
|
+
return null;
|
|
33632
|
+
}
|
|
33633
|
+
//#endregion
|
|
33634
|
+
//#region src/time/time-window.ts
|
|
33635
|
+
/**
|
|
33636
|
+
* A daily window as a STATE: is this instant inside it, and when does that
|
|
33637
|
+
* next change? Pure — the answer is built per local date from
|
|
33638
|
+
* {@link occurrenceOn}, so a DST change needs no special case.
|
|
33639
|
+
*
|
|
33640
|
+
* Rules:
|
|
33641
|
+
* - A window whose `to` is not after its `from` on the same local date WRAPS:
|
|
33642
|
+
* it runs from `from` on day D to `to` on D+1.
|
|
33643
|
+
* - `days` names the local day a window STARTS on. 22:00-06:00 on weekdays is
|
|
33644
|
+
* inside at 01:00 Saturday (it started Friday). With `days` and no
|
|
33645
|
+
* `between` the window is the whole local day.
|
|
33646
|
+
* - A sun event that does not happen on a day (polar day or night) gives that
|
|
33647
|
+
* day no window. A sun event on a site WITHOUT a location (or an unknown
|
|
33648
|
+
* zone) cannot be judged at all: the result is `unknown`, never a guess.
|
|
33649
|
+
*/
|
|
33650
|
+
var MIDNIGHT = {
|
|
33651
|
+
kind: "clock",
|
|
33652
|
+
hh: 0,
|
|
33653
|
+
mm: 0
|
|
33654
|
+
};
|
|
33655
|
+
/** Start dates looked at for a boundary: yesterday .. the day after tomorrow. */
|
|
33656
|
+
var LOOKAHEAD_DAYS = 2;
|
|
33657
|
+
function addDays(localDate, days) {
|
|
33658
|
+
const [y, m, d] = localDate.split("-").map(Number);
|
|
33659
|
+
return new Date(Date.UTC(y ?? 0, (m ?? 1) - 1, (d ?? 1) + days)).toISOString().slice(0, 10);
|
|
33660
|
+
}
|
|
33661
|
+
function unresolvable(window, site) {
|
|
33662
|
+
if (!isKnownTimeZone(site.timeZone)) return `the site time zone "${site.timeZone}" is not a known IANA zone`;
|
|
33663
|
+
if (window.between !== void 0 && (window.between.from.kind === "sun" || window.between.to.kind === "sun") && site.zoneOnly === true) return "a sun time needs the site location, and none is set";
|
|
33664
|
+
return null;
|
|
33665
|
+
}
|
|
33666
|
+
/** The interval that STARTS on local day `startDate`, or null when that day has none. */
|
|
33667
|
+
function intervalStartingOn(window, startDate, site) {
|
|
33668
|
+
if (window.days !== void 0 && !window.days.includes(weekdayOfLocalDate(startDate))) return null;
|
|
33669
|
+
if (window.between === void 0) {
|
|
33670
|
+
const start = occurrenceOn(MIDNIGHT, startDate, site);
|
|
33671
|
+
const end = occurrenceOn(MIDNIGHT, addDays(startDate, 1), site);
|
|
33672
|
+
return start === null || end === null ? null : {
|
|
33673
|
+
start,
|
|
33674
|
+
end
|
|
33675
|
+
};
|
|
33676
|
+
}
|
|
33677
|
+
const start = occurrenceOn(window.between.from, startDate, site);
|
|
33678
|
+
const sameDayEnd = occurrenceOn(window.between.to, startDate, site);
|
|
33679
|
+
if (start === null || sameDayEnd === null) return null;
|
|
33680
|
+
if (sameDayEnd > start) return {
|
|
33681
|
+
start,
|
|
33682
|
+
end: sameDayEnd
|
|
33683
|
+
};
|
|
33684
|
+
const end = occurrenceOn(window.between.to, addDays(startDate, 1), site);
|
|
33685
|
+
return end === null || end <= start ? null : {
|
|
33686
|
+
start,
|
|
33687
|
+
end
|
|
33688
|
+
};
|
|
33689
|
+
}
|
|
33690
|
+
function judgeTimeWindow(window, nowMs, site) {
|
|
33691
|
+
const blocked = unresolvable(window, site);
|
|
33692
|
+
if (blocked !== null) return {
|
|
33693
|
+
inside: false,
|
|
33694
|
+
unknown: true,
|
|
33695
|
+
nextChangeAtMs: null,
|
|
33696
|
+
why: `cannot be judged: ${blocked}`
|
|
33697
|
+
};
|
|
33698
|
+
const today = localDateOf(nowMs, site.timeZone);
|
|
33699
|
+
let inside = false;
|
|
33700
|
+
let next = null;
|
|
33701
|
+
for (let offset = -1; offset <= LOOKAHEAD_DAYS; offset += 1) {
|
|
33702
|
+
const interval = intervalStartingOn(window, addDays(today, offset), site);
|
|
33703
|
+
if (interval === null) continue;
|
|
33704
|
+
if (interval.start <= nowMs && nowMs < interval.end) inside = true;
|
|
33705
|
+
for (const boundary of [interval.start, interval.end]) if (boundary > nowMs && (next === null || boundary < next)) next = boundary;
|
|
33706
|
+
}
|
|
33707
|
+
if (window.between === void 0) {
|
|
33708
|
+
const midnight = occurrenceOn(MIDNIGHT, addDays(today, 1), site);
|
|
33709
|
+
if (midnight !== null && midnight > nowMs && (next === null || midnight < next)) next = midnight;
|
|
33710
|
+
}
|
|
33711
|
+
const days = window.days === void 0 ? "" : ` on ${window.days.join(", ")}`;
|
|
33712
|
+
const span = window.between === void 0 ? "the whole day" : "the window";
|
|
33713
|
+
return {
|
|
33714
|
+
inside,
|
|
33715
|
+
unknown: false,
|
|
33716
|
+
nextChangeAtMs: next,
|
|
33717
|
+
why: inside ? `inside ${span}${days}` : `outside ${span}${days}`
|
|
33718
|
+
};
|
|
33505
33719
|
}
|
|
33506
33720
|
//#endregion
|
|
33507
33721
|
//#region src/automation/condition-eval.ts
|
|
@@ -33577,6 +33791,8 @@ function collectInto(cond, out) {
|
|
|
33577
33791
|
...binding.addonId !== void 0 ? { addonId: binding.addonId } : {}
|
|
33578
33792
|
});
|
|
33579
33793
|
return;
|
|
33794
|
+
case "time-is": return;
|
|
33795
|
+
case "triggered-by": return;
|
|
33580
33796
|
default: return;
|
|
33581
33797
|
}
|
|
33582
33798
|
}
|
|
@@ -33593,6 +33809,32 @@ function collectWhenMirrorSources(cond) {
|
|
|
33593
33809
|
return true;
|
|
33594
33810
|
});
|
|
33595
33811
|
}
|
|
33812
|
+
/** Does the tree hold a `time-is` leaf? */
|
|
33813
|
+
function hasTimeLeaves(when) {
|
|
33814
|
+
let found = false;
|
|
33815
|
+
forEachLeaf(when, (leaf) => {
|
|
33816
|
+
if (leaf.kind === "time-is") found = true;
|
|
33817
|
+
});
|
|
33818
|
+
return found;
|
|
33819
|
+
}
|
|
33820
|
+
/**
|
|
33821
|
+
* The soonest ANY `time-is` leaf changes sides (ms from `now`), over every
|
|
33822
|
+
* leaf of the tree — a short-circuit that skipped a leaf in the last
|
|
33823
|
+
* evaluation must not hide its boundary. Undefined: no time leaf, no site, or
|
|
33824
|
+
* no boundary within reach.
|
|
33825
|
+
*/
|
|
33826
|
+
function nextTimeBoundaryInMs(when, now, site, rootPath = "when") {
|
|
33827
|
+
if (site === void 0) return void 0;
|
|
33828
|
+
let due;
|
|
33829
|
+
forEachLeaf(when, (leaf) => {
|
|
33830
|
+
if (leaf.kind !== "time-is") return;
|
|
33831
|
+
const next = judgeTimeWindow(leaf, now, site).nextChangeAtMs;
|
|
33832
|
+
if (next === null) return;
|
|
33833
|
+
const ms = Math.max(0, next - now);
|
|
33834
|
+
due = due === void 0 ? ms : Math.min(due, ms);
|
|
33835
|
+
}, rootPath);
|
|
33836
|
+
return due;
|
|
33837
|
+
}
|
|
33596
33838
|
/** Legacy name, for blocks generated before the single tree: an old condition tree. */
|
|
33597
33839
|
function collectMirrorSources(cond) {
|
|
33598
33840
|
return collectWhenMirrorSources(cond === void 0 ? void 0 : convertLegacyCondition(cond));
|
|
@@ -33928,6 +34170,53 @@ function evalNode(cond, path, reader, now, out, state) {
|
|
|
33928
34170
|
});
|
|
33929
34171
|
return ok;
|
|
33930
34172
|
}
|
|
34173
|
+
case "time-is": {
|
|
34174
|
+
if (state.site === void 0) {
|
|
34175
|
+
state.unknown = true;
|
|
34176
|
+
out.push({
|
|
34177
|
+
path,
|
|
34178
|
+
kind: cond.kind,
|
|
34179
|
+
outcome: "fail",
|
|
34180
|
+
reason: "the site clock is not known here → fail"
|
|
34181
|
+
});
|
|
34182
|
+
return false;
|
|
34183
|
+
}
|
|
34184
|
+
const verdict = judgeTimeWindow(cond, now, state.site);
|
|
34185
|
+
if (verdict.unknown) state.unknown = true;
|
|
34186
|
+
if (verdict.nextChangeAtMs !== null) {
|
|
34187
|
+
const due = Math.max(0, verdict.nextChangeAtMs - now);
|
|
34188
|
+
state.timeDueInMs = state.timeDueInMs === void 0 ? due : Math.min(state.timeDueInMs, due);
|
|
34189
|
+
}
|
|
34190
|
+
out.push({
|
|
34191
|
+
path,
|
|
34192
|
+
kind: cond.kind,
|
|
34193
|
+
outcome: verdict.inside ? "pass" : "fail",
|
|
34194
|
+
reason: `${verdict.why} → ${verdict.inside ? "pass" : "fail"}`
|
|
34195
|
+
});
|
|
34196
|
+
return verdict.inside;
|
|
34197
|
+
}
|
|
34198
|
+
case "triggered-by": {
|
|
34199
|
+
const named = cond.leafIds.map((id) => `"${id}"`).join(", ");
|
|
34200
|
+
if (state.trigger === "unknown") {
|
|
34201
|
+
state.unknown = true;
|
|
34202
|
+
out.push({
|
|
34203
|
+
path,
|
|
34204
|
+
kind: cond.kind,
|
|
34205
|
+
outcome: "fail",
|
|
34206
|
+
reason: `what started this run is not known here, so "triggered by ${named}" cannot be judged → fail`
|
|
34207
|
+
});
|
|
34208
|
+
return false;
|
|
34209
|
+
}
|
|
34210
|
+
const fired = state.trigger.leafId;
|
|
34211
|
+
const ok = fired !== null && cond.leafIds.includes(fired);
|
|
34212
|
+
out.push({
|
|
34213
|
+
path,
|
|
34214
|
+
kind: cond.kind,
|
|
34215
|
+
outcome: ok ? "pass" : "fail",
|
|
34216
|
+
reason: `${fired === null ? "no named trigger started this run" : `started by "${fired}"`}, wanted ${named} → ${ok ? "pass" : "fail"}`
|
|
34217
|
+
});
|
|
34218
|
+
return ok;
|
|
34219
|
+
}
|
|
33931
34220
|
case "not": {
|
|
33932
34221
|
const at = out.length;
|
|
33933
34222
|
out.push({
|
|
@@ -33986,13 +34275,17 @@ function evaluateWhenTree(when, reader, now, options = {}) {
|
|
|
33986
34275
|
firedPath: options.firedPath,
|
|
33987
34276
|
holds: options.holds,
|
|
33988
34277
|
unknown: false,
|
|
33989
|
-
holdDueInMs: void 0
|
|
34278
|
+
holdDueInMs: void 0,
|
|
34279
|
+
site: options.site,
|
|
34280
|
+
timeDueInMs: void 0,
|
|
34281
|
+
trigger: options.trigger ?? { leafId: options.firedPath === void 0 ? null : eventLeafIdAt(when, options.firedPath) }
|
|
33990
34282
|
};
|
|
33991
34283
|
return {
|
|
33992
34284
|
pass: evalNode(when, options.rootPath ?? "when", reader, now, nodes, state),
|
|
33993
34285
|
unknown: state.unknown,
|
|
33994
34286
|
nodes,
|
|
33995
|
-
...state.holdDueInMs === void 0 ? {} : { holdDueInMs: state.holdDueInMs }
|
|
34287
|
+
...state.holdDueInMs === void 0 ? {} : { holdDueInMs: state.holdDueInMs },
|
|
34288
|
+
...state.timeDueInMs === void 0 ? {} : { timeDueInMs: state.timeDueInMs }
|
|
33996
34289
|
};
|
|
33997
34290
|
}
|
|
33998
34291
|
/** Does any state leaf of the tree carry a `for`? */
|
|
@@ -34016,7 +34309,10 @@ function observeHoldLeaves(when, reader, now, holds, rootPath = "when") {
|
|
|
34016
34309
|
firedPath: void 0,
|
|
34017
34310
|
holds,
|
|
34018
34311
|
unknown: false,
|
|
34019
|
-
holdDueInMs: void 0
|
|
34312
|
+
holdDueInMs: void 0,
|
|
34313
|
+
site: void 0,
|
|
34314
|
+
timeDueInMs: void 0,
|
|
34315
|
+
trigger: "unknown"
|
|
34020
34316
|
};
|
|
34021
34317
|
evalNode(leaf, path, reader, now, [], state);
|
|
34022
34318
|
if (state.holdDueInMs !== void 0) due = due === void 0 ? state.holdDueInMs : Math.min(due, state.holdDueInMs);
|
|
@@ -34048,6 +34344,11 @@ var STATIONARY_DESCRIPTIONS = {
|
|
|
34048
34344
|
phase: "appeared or departed",
|
|
34049
34345
|
entryId: "the stopped object"
|
|
34050
34346
|
};
|
|
34347
|
+
var BECOMES_DESCRIPTIONS = {
|
|
34348
|
+
deviceId: "the device that changed",
|
|
34349
|
+
from: "the value before the change, empty when it is not known",
|
|
34350
|
+
to: "the value it changed to"
|
|
34351
|
+
};
|
|
34051
34352
|
function describe$1(descriptions) {
|
|
34052
34353
|
return Object.entries(descriptions).map(([name, description]) => ({
|
|
34053
34354
|
name,
|
|
@@ -34056,10 +34357,21 @@ function describe$1(descriptions) {
|
|
|
34056
34357
|
}
|
|
34057
34358
|
var DETECTION_VARIABLES = describe$1(DETECTION_DESCRIPTIONS);
|
|
34058
34359
|
var STATIONARY_VARIABLES = describe$1(STATIONARY_DESCRIPTIONS);
|
|
34360
|
+
var BECOMES_VARIABLES = describe$1(BECOMES_DESCRIPTIONS).map((v) => v.name === "deviceId" ? {
|
|
34361
|
+
...v,
|
|
34362
|
+
hintKey: "becomesDeviceId"
|
|
34363
|
+
} : v);
|
|
34364
|
+
/** A name two trigger kinds describe differently, said for both (a camera, or a device that changed). */
|
|
34365
|
+
var SHARED_VARIABLES = { deviceId: {
|
|
34366
|
+
name: "deviceId",
|
|
34367
|
+
description: "the device that started the run",
|
|
34368
|
+
hintKey: "triggerDeviceId"
|
|
34369
|
+
} };
|
|
34059
34370
|
/** What one event row kind provides; kinds that provide none are absent. */
|
|
34060
34371
|
function triggerLeafVariables(kind) {
|
|
34061
34372
|
if (kind === "detection") return DETECTION_VARIABLES;
|
|
34062
34373
|
if (kind === "stationary") return STATIONARY_VARIABLES;
|
|
34374
|
+
if (kind === "device-becomes") return BECOMES_VARIABLES;
|
|
34063
34375
|
return [];
|
|
34064
34376
|
}
|
|
34065
34377
|
/**
|
|
@@ -34074,7 +34386,9 @@ function triggerVariablesOf(when) {
|
|
|
34074
34386
|
});
|
|
34075
34387
|
const [first, ...rest] = perEvent;
|
|
34076
34388
|
if (first === void 0) return [];
|
|
34077
|
-
return first.filter((v) => rest.every((other) => other.some((o) => o.name === v.name)))
|
|
34389
|
+
return first.filter((v) => rest.every((other) => other.some((o) => o.name === v.name))).map((v) => {
|
|
34390
|
+
return rest.every((other) => other.some((o) => o.name === v.name && o.description === v.description)) ? v : SHARED_VARIABLES[v.name] ?? v;
|
|
34391
|
+
});
|
|
34078
34392
|
}
|
|
34079
34393
|
//#endregion
|
|
34080
34394
|
//#region src/automation/codegen.ts
|
|
@@ -34202,6 +34516,7 @@ function emitLeaf(trigger, path) {
|
|
|
34202
34516
|
cap: ${j(trigger.cap)},
|
|
34203
34517
|
field: ${j(trigger.fieldPath)},
|
|
34204
34518
|
to: toTraceScalar(effect.value),
|
|
34519
|
+
...(effect.kind === 'fire' ? { from: toTraceScalar(effect.from) } : {}),
|
|
34205
34520
|
...(effect.hold === undefined ? {} : { hold: effect.hold }),
|
|
34206
34521
|
}
|
|
34207
34522
|
if (effect.kind === 'fire') {
|
|
@@ -34325,7 +34640,7 @@ function emitGuard(action, index) {
|
|
|
34325
34640
|
const body = emitLeafBody(el);
|
|
34326
34641
|
return `{ meta: ${j(stepMetaOf(el))}, run: async (scope) => {\n${body ?? ""}\n } }`;
|
|
34327
34642
|
}).join(", ");
|
|
34328
|
-
return ` guard: () => evaluateCondition(${j(action.if)}, ${j(`actions.${index}.if`)}),${otherwise === "" ? "" : `\n otherwise: [${otherwise}],`}
|
|
34643
|
+
return ` guard: () => evaluateCondition(${j(action.if)}, ${j(`actions.${index}.if`)}, vars),${otherwise === "" ? "" : `\n otherwise: [${otherwise}],`}
|
|
34329
34644
|
`;
|
|
34330
34645
|
}
|
|
34331
34646
|
function emitAction(action, index) {
|
|
@@ -34375,6 +34690,7 @@ function generateAutomationBlock(input) {
|
|
|
34375
34690
|
path: `actions.${i}.if`,
|
|
34376
34691
|
when: a.if
|
|
34377
34692
|
}] : []);
|
|
34693
|
+
const hasTime = hasTimeLeaves(recipe.when) || recipe.actions.some((a) => (a.kind === "wait" || a.kind === "cap") && a.if !== void 0 && hasTimeLeaves(a.if));
|
|
34378
34694
|
const eventLeaves = [];
|
|
34379
34695
|
forEachLeaf(recipe.when, (leaf, path) => {
|
|
34380
34696
|
if (isEventLeaf(leaf)) eventLeaves.push({
|
|
@@ -34392,6 +34708,7 @@ function generateAutomationBlock(input) {
|
|
|
34392
34708
|
AutomationStepTrace,
|
|
34393
34709
|
RunnableStep,
|
|
34394
34710
|
StepScope,
|
|
34711
|
+
TimeSite,
|
|
34395
34712
|
TimeSpec,
|
|
34396
34713
|
WeekdayKey,
|
|
34397
34714
|
} from '@camstack/types'
|
|
@@ -34410,9 +34727,11 @@ import {
|
|
|
34410
34727
|
describeHold,
|
|
34411
34728
|
evaluateWhenTree,
|
|
34412
34729
|
evaluateStationaryTrigger,
|
|
34730
|
+
eventLeafIdAt,
|
|
34413
34731
|
isSkippedTrace,
|
|
34414
34732
|
mergeSkipTraces,
|
|
34415
34733
|
nextOccurrence,
|
|
34734
|
+
nextTimeBoundaryInMs,
|
|
34416
34735
|
observeHoldLeaves,
|
|
34417
34736
|
stableIdMapOf,
|
|
34418
34737
|
resolveStepArgs,
|
|
@@ -34420,6 +34739,7 @@ import {
|
|
|
34420
34739
|
runForDevices,
|
|
34421
34740
|
evaluateStepCondition,
|
|
34422
34741
|
runSteps,
|
|
34742
|
+
siteFor,
|
|
34423
34743
|
validateTimeSpecForSite,
|
|
34424
34744
|
zoneOnlySite,
|
|
34425
34745
|
} from '@camstack/types'
|
|
@@ -34461,6 +34781,9 @@ const HAS_DOMAIN_TARGET = ${hasDomainTarget}
|
|
|
34461
34781
|
/** A state leaf of the tree carries a \`for\`: its clock is fed on every mirror update, so it counts while nothing fires. */
|
|
34462
34782
|
const HAS_HOLD = ${hasHoldLeaves(recipe.when)}
|
|
34463
34783
|
|
|
34784
|
+
/** A \`time-is\` leaf (in the tree or a guard) reads the site clock: the site is kept in memory and a timer wakes the tree at each boundary. */
|
|
34785
|
+
const HAS_TIME = ${hasTime}
|
|
34786
|
+
|
|
34464
34787
|
/** Guards (a step's \`if\`) with a \`for\`: they keep their own mirror and clocks, fed the same way. */
|
|
34465
34788
|
const GUARD_HOLD_TREES: ReadonlyArray<{ path: string; when: AutomationWhen }> = ${j(guardHoldTrees)}
|
|
34466
34789
|
|
|
@@ -34526,7 +34849,9 @@ export default function block(ctx: AddonContext) {
|
|
|
34526
34849
|
)
|
|
34527
34850
|
const guardHolds = new HoldTracker()
|
|
34528
34851
|
let holdTimer: ReturnType<typeof setTimeout> | undefined
|
|
34529
|
-
|
|
34852
|
+
${hasTime ? ` /** The site clock a \`time-is\` leaf reads: memory only at evaluation time (D49), refreshed at start and on the reconcile. */
|
|
34853
|
+
let site: TimeSite | undefined
|
|
34854
|
+
` : ""} const BLOCK_ID = coreBlockIdFromAddonId(ctx.id)
|
|
34530
34855
|
/** The cooldown stamp: memory is the authority at fire time (D49); the hub ledger is read once at start. */
|
|
34531
34856
|
let lastRunStartedAt: number | null = null
|
|
34532
34857
|
/** True when the stamp could not be read at start — the first run is let through and says so. */
|
|
@@ -34623,7 +34948,19 @@ export default function block(ctx: AddonContext) {
|
|
|
34623
34948
|
void send({ trace })
|
|
34624
34949
|
}
|
|
34625
34950
|
|
|
34626
|
-
|
|
34951
|
+
${hasTime ? ` /** Read the site into memory. A failed read keeps the previous value (D49). */
|
|
34952
|
+
async function refreshSite(): Promise<void> {
|
|
34953
|
+
try {
|
|
34954
|
+
const reading = await ctx.api.system.getSiteLocation.query()
|
|
34955
|
+
site = siteFor(reading) ?? zoneOnlySite(reading.timeZone)
|
|
34956
|
+
} catch (error) {
|
|
34957
|
+
ctx.logger.warn('site clock not refreshed — keeping the previous one', {
|
|
34958
|
+
error: error instanceof Error ? error.message : String(error),
|
|
34959
|
+
})
|
|
34960
|
+
}
|
|
34961
|
+
}
|
|
34962
|
+
|
|
34963
|
+
` : ""} // ── the condition mirror (D49): memory only at fire time ──
|
|
34627
34964
|
async function refreshMirror(): Promise<void> {
|
|
34628
34965
|
const report = await mirror.refresh({
|
|
34629
34966
|
fetchSlice: (deviceId, cap) => ctx.api.deviceState.getCapSlice.query({ deviceId, capName: cap }),
|
|
@@ -34662,7 +34999,10 @@ export default function block(ctx: AddonContext) {
|
|
|
34662
34999
|
const due = observeHoldLeaves(tree.when, guardMirror, now, guardHolds, tree.path)
|
|
34663
35000
|
if (due !== undefined) dues.push(due)
|
|
34664
35001
|
}
|
|
34665
|
-
|
|
35002
|
+
${hasTime ? ` // a time-is leaf changes sides at a boundary with no device event: wake the tree there
|
|
35003
|
+
const timeDue = nextTimeBoundaryInMs(WHEN, now, site)
|
|
35004
|
+
if (timeDue !== undefined) dues.push(timeDue)
|
|
35005
|
+
` : ""} if (holdTimer !== undefined) clearTimeout(holdTimer)
|
|
34666
35006
|
holdTimer = undefined
|
|
34667
35007
|
if (dues.length === 0) return
|
|
34668
35008
|
holdTimer = setTimeout(() => {
|
|
@@ -34835,11 +35175,14 @@ export default function block(ctx: AddonContext) {
|
|
|
34835
35175
|
}
|
|
34836
35176
|
|
|
34837
35177
|
/** A step's \`if\` (#472), read on a mirror filled for exactly its leaves, NOW (D49: unread = unknown). */
|
|
34838
|
-
function evaluateCondition(when: AutomationWhen, path: string) {
|
|
35178
|
+
function evaluateCondition(when: AutomationWhen, path: string, vars: Record<string, unknown>) {
|
|
35179
|
+
// \`triggered-by\` reads the run: the id of the leaf that started it, by the path the trigger carries.
|
|
35180
|
+
const firedLeaf = vars['leaf']
|
|
35181
|
+
const trigger = { leafId: typeof firedLeaf === 'string' ? eventLeafIdAt(WHEN, firedLeaf) : null }
|
|
34839
35182
|
const history = GUARD_HOLD_TREES.some((t) => t.path === path)
|
|
34840
35183
|
? { mirror: guardMirror, holds: guardHolds, rootPath: path }
|
|
34841
35184
|
: undefined
|
|
34842
|
-
return evaluateStepCondition(when, mirrorFetchers(), Date.now, history)
|
|
35185
|
+
return evaluateStepCondition(when, mirrorFetchers(), Date.now, history, ${hasTime ? "site" : "undefined"}, trigger)
|
|
34843
35186
|
}
|
|
34844
35187
|
|
|
34845
35188
|
/** The THEN half, through the ONE shared step runner: stops at the first
|
|
@@ -34926,7 +35269,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
34926
35269
|
try {
|
|
34927
35270
|
const firedLeaf = trigger['leaf']
|
|
34928
35271
|
const evaluation = evaluateWhenTree(WHEN, mirror, Date.now(), {
|
|
34929
|
-
holds,
|
|
35272
|
+
holds,${hasTime ? "\n ...(site === undefined ? {} : { site })," : ""}
|
|
34930
35273
|
...(typeof firedLeaf === 'string' ? { firedPath: firedLeaf } : {}),
|
|
34931
35274
|
})
|
|
34932
35275
|
nodes = [...evaluation.nodes]
|
|
@@ -35040,7 +35383,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
35040
35383
|
|
|
35041
35384
|
const ready: Promise<void> = (async () => {
|
|
35042
35385
|
await loadState()
|
|
35043
|
-
await refreshMirror()
|
|
35386
|
+
${hasTime ? " await refreshSite()\n" : ""} await refreshMirror()
|
|
35044
35387
|
feedHolds()
|
|
35045
35388
|
stateWatching = true
|
|
35046
35389
|
evaluateStateTree()
|
|
@@ -35056,7 +35399,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
35056
35399
|
let stateWatching = false
|
|
35057
35400
|
function evaluateStateTree(): void {
|
|
35058
35401
|
if (!STATE_ONLY || !stateWatching || stopped) return
|
|
35059
|
-
const evaluation = evaluateWhenTree(WHEN, mirror, Date.now(), { holds })
|
|
35402
|
+
const evaluation = evaluateWhenTree(WHEN, mirror, Date.now(), { holds${hasTime ? ", ...(site === undefined ? {} : { site })" : ""} })
|
|
35060
35403
|
if (evaluation.unknown) return
|
|
35061
35404
|
const was = treeWasTrue
|
|
35062
35405
|
treeWasTrue = evaluation.pass
|
|
@@ -35065,7 +35408,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
35065
35408
|
}
|
|
35066
35409
|
}
|
|
35067
35410
|
|
|
35068
|
-
if (MIRROR_SOURCES.length > 0 || GUARD_HOLD_TREES.length > 0) {
|
|
35411
|
+
if (MIRROR_SOURCES.length > 0 || GUARD_HOLD_TREES.length > 0 || HAS_TIME) {
|
|
35069
35412
|
disposers.push(
|
|
35070
35413
|
ctx.eventBus.subscribe({ category: EventCategory.DeviceStateChanged }, (event) => {
|
|
35071
35414
|
const data = event.data
|
|
@@ -35080,7 +35423,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
35080
35423
|
}),
|
|
35081
35424
|
)
|
|
35082
35425
|
const mirrorReconcile = setInterval(() => {
|
|
35083
|
-
void refreshMirror().then(() => {
|
|
35426
|
+
void ${hasTime ? "Promise.all([refreshSite(), refreshMirror()])" : "refreshMirror()"}.then(() => {
|
|
35084
35427
|
feedHolds()
|
|
35085
35428
|
evaluateStateTree()
|
|
35086
35429
|
})
|
|
@@ -35411,6 +35754,40 @@ var RunGate = class {
|
|
|
35411
35754
|
}
|
|
35412
35755
|
};
|
|
35413
35756
|
//#endregion
|
|
35757
|
+
//#region src/automation/when-site.ts
|
|
35758
|
+
/**
|
|
35759
|
+
* Save-time check of a `when` tree against the SITE: a `time-is` window
|
|
35760
|
+
* bounded by a sun event needs the site location, and any window needs a known
|
|
35761
|
+
* time zone. Refused before the save with a typed reason — at run time the
|
|
35762
|
+
* same leaf can only read `unknown`, and a rule that never decides is a rule
|
|
35763
|
+
* the operator believes is working. Pure: the site reading is handed in.
|
|
35764
|
+
*/
|
|
35765
|
+
var WINDOW_ENDS = ["from", "to"];
|
|
35766
|
+
/** Every `time-is` window end of the tree this site cannot place. `rootPath` replaces the leading `when`. */
|
|
35767
|
+
function whenSiteIssues(when, reading, rootPath = "when") {
|
|
35768
|
+
const issues = [];
|
|
35769
|
+
forEachLeaf(when, (leaf, path) => {
|
|
35770
|
+
if (leaf.kind !== "time-is" || leaf.between === void 0) return;
|
|
35771
|
+
for (const end of WINDOW_ENDS) {
|
|
35772
|
+
const checked = validateTimeSpecForSite(leaf.between[end], reading);
|
|
35773
|
+
if (!checked.ok) issues.push({
|
|
35774
|
+
path: `${path}.between.${end}`,
|
|
35775
|
+
reason: checked.reason,
|
|
35776
|
+
message: checked.message
|
|
35777
|
+
});
|
|
35778
|
+
}
|
|
35779
|
+
}, rootPath);
|
|
35780
|
+
return issues;
|
|
35781
|
+
}
|
|
35782
|
+
/** The same check over the guard (`if`) of every step of a list, reported under `<rootPath>.<i>.if`. */
|
|
35783
|
+
function stepGuardSiteIssues(steps, reading, rootPath) {
|
|
35784
|
+
return steps.flatMap((step, i) => step.if === void 0 ? [] : whenSiteIssues(step.if, reading, `${rootPath}.${i}.if`));
|
|
35785
|
+
}
|
|
35786
|
+
/** The whole recipe: its `when` tree and every step guard. */
|
|
35787
|
+
function recipeSiteIssues(recipe, reading) {
|
|
35788
|
+
return [...whenSiteIssues(recipe.when, reading), ...stepGuardSiteIssues(recipe.actions, reading, "actions")];
|
|
35789
|
+
}
|
|
35790
|
+
//#endregion
|
|
35414
35791
|
//#region src/automation/scheduler.ts
|
|
35415
35792
|
/**
|
|
35416
35793
|
* Schedulers (D983) — an entity that performs actions over the day.
|
|
@@ -35435,6 +35812,13 @@ var SchedulerStateTreeSchema = AutomationWhenSchema.superRefine((when, ctx) => {
|
|
|
35435
35812
|
});
|
|
35436
35813
|
return;
|
|
35437
35814
|
}
|
|
35815
|
+
if (hasTriggeredByLeaves(when)) {
|
|
35816
|
+
ctx.addIssue({
|
|
35817
|
+
code: "custom",
|
|
35818
|
+
message: TRIGGERED_BY_NEEDS_AUTOMATION
|
|
35819
|
+
});
|
|
35820
|
+
return;
|
|
35821
|
+
}
|
|
35438
35822
|
const problem = validateWhenTree(when);
|
|
35439
35823
|
if (problem !== null) ctx.addIssue({
|
|
35440
35824
|
code: "custom",
|
|
@@ -35526,10 +35910,20 @@ function holdRefusalOf(step) {
|
|
|
35526
35910
|
*/
|
|
35527
35911
|
function validateSchedulerInput(input, reading) {
|
|
35528
35912
|
const issues = [];
|
|
35913
|
+
const siteIssues = (found) => {
|
|
35914
|
+
for (const issue of found) issues.push({
|
|
35915
|
+
path: issue.path,
|
|
35916
|
+
message: issue.message
|
|
35917
|
+
});
|
|
35918
|
+
};
|
|
35919
|
+
if (input.activeWhen !== void 0) siteIssues(whenSiteIssues(input.activeWhen, reading, "activeWhen"));
|
|
35529
35920
|
input.profiles.forEach((profile, pi) => {
|
|
35530
35921
|
const ids = /* @__PURE__ */ new Set();
|
|
35922
|
+
if (profile.activeWhen !== void 0) siteIssues(whenSiteIssues(profile.activeWhen, reading, `profiles.${pi}.activeWhen`));
|
|
35531
35923
|
profile.points.forEach((point, qi) => {
|
|
35532
35924
|
const base = `profiles.${pi}.points.${qi}`;
|
|
35925
|
+
if (point.conditions !== void 0) siteIssues(whenSiteIssues(point.conditions, reading, `${base}.conditions`));
|
|
35926
|
+
siteIssues(stepGuardSiteIssues(point.steps, reading, `${base}.steps`));
|
|
35533
35927
|
if (ids.has(point.id)) issues.push({
|
|
35534
35928
|
path: `${base}.id`,
|
|
35535
35929
|
message: `point id "${point.id}" is used twice`
|
|
@@ -35755,6 +36149,8 @@ var AutomationConditionTraceNodeSchema = zod.z.object({
|
|
|
35755
36149
|
"device-is",
|
|
35756
36150
|
"expression",
|
|
35757
36151
|
"stationary-present",
|
|
36152
|
+
"time-is",
|
|
36153
|
+
"triggered-by",
|
|
35758
36154
|
"device-becomes",
|
|
35759
36155
|
"device-event",
|
|
35760
36156
|
"schedule",
|
|
@@ -36217,21 +36613,18 @@ var ActionRunChangedSchema = zod.z.object({
|
|
|
36217
36613
|
});
|
|
36218
36614
|
//#endregion
|
|
36219
36615
|
//#region src/automation/step-condition.ts
|
|
36220
|
-
/**
|
|
36221
|
-
* The guard of a conditional step (#472): the step's `if` is the ONE `when`
|
|
36222
|
-
* tree (D980), read on a {@link ConditionMirror} filled for exactly the leaves
|
|
36223
|
-
* the tree names, at the moment the step is reached. A state that cannot be
|
|
36224
|
-
* read is UNKNOWN, and unknown runs neither side (D49: a failed read changes
|
|
36225
|
-
* nothing, and never decides a branch).
|
|
36226
|
-
*/
|
|
36227
36616
|
/** Fill a mirror for the tree's leaves from `fetchers` and judge it NOW. */
|
|
36228
|
-
async function evaluateStepCondition(when, fetchers, now = Date.now, history) {
|
|
36617
|
+
async function evaluateStepCondition(when, fetchers, now = Date.now, history, site, trigger) {
|
|
36229
36618
|
const mirror = history?.mirror ?? new ConditionMirror(collectWhenMirrorSources(when));
|
|
36230
36619
|
await mirror.refresh(fetchers);
|
|
36231
|
-
const evaluation = evaluateWhenTree(when, mirror, now(), {
|
|
36232
|
-
|
|
36233
|
-
|
|
36234
|
-
|
|
36620
|
+
const evaluation = evaluateWhenTree(when, mirror, now(), {
|
|
36621
|
+
...history === void 0 ? {} : {
|
|
36622
|
+
holds: history.holds,
|
|
36623
|
+
rootPath: history.rootPath
|
|
36624
|
+
},
|
|
36625
|
+
...site === void 0 ? {} : { site },
|
|
36626
|
+
trigger: trigger ?? "unknown"
|
|
36627
|
+
});
|
|
36235
36628
|
const first = evaluation.nodes.filter((n) => n.outcome !== "pass").map((n) => n.reason)[0];
|
|
36236
36629
|
if (evaluation.unknown) return {
|
|
36237
36630
|
result: "unknown",
|
|
@@ -37205,6 +37598,60 @@ var AUTOMATION_LEAF_CATALOG = {
|
|
|
37205
37598
|
deviceId: 0
|
|
37206
37599
|
})
|
|
37207
37600
|
},
|
|
37601
|
+
"time-is": {
|
|
37602
|
+
kind: "time-is",
|
|
37603
|
+
category: "time",
|
|
37604
|
+
role: "state",
|
|
37605
|
+
allowedIn: STATE_CONTEXTS,
|
|
37606
|
+
i18nKey: "automation.leafCatalog.time-is",
|
|
37607
|
+
keywords: [
|
|
37608
|
+
"time",
|
|
37609
|
+
"hour",
|
|
37610
|
+
"window",
|
|
37611
|
+
"between",
|
|
37612
|
+
"night",
|
|
37613
|
+
"day",
|
|
37614
|
+
"weekday",
|
|
37615
|
+
"sunset",
|
|
37616
|
+
"sunrise"
|
|
37617
|
+
],
|
|
37618
|
+
needsDeviceValue: false,
|
|
37619
|
+
seed: () => ({
|
|
37620
|
+
kind: "time-is",
|
|
37621
|
+
between: {
|
|
37622
|
+
from: {
|
|
37623
|
+
kind: "clock",
|
|
37624
|
+
hh: 22,
|
|
37625
|
+
mm: 0
|
|
37626
|
+
},
|
|
37627
|
+
to: {
|
|
37628
|
+
kind: "clock",
|
|
37629
|
+
hh: 6,
|
|
37630
|
+
mm: 0
|
|
37631
|
+
}
|
|
37632
|
+
}
|
|
37633
|
+
})
|
|
37634
|
+
},
|
|
37635
|
+
"triggered-by": {
|
|
37636
|
+
kind: "triggered-by",
|
|
37637
|
+
category: "logic",
|
|
37638
|
+
role: "state",
|
|
37639
|
+
allowedIn: STATE_CONTEXTS,
|
|
37640
|
+
i18nKey: "automation.leafCatalog.triggered-by",
|
|
37641
|
+
keywords: [
|
|
37642
|
+
"trigger",
|
|
37643
|
+
"triggered",
|
|
37644
|
+
"started",
|
|
37645
|
+
"which",
|
|
37646
|
+
"branch",
|
|
37647
|
+
"fired"
|
|
37648
|
+
],
|
|
37649
|
+
needsDeviceValue: false,
|
|
37650
|
+
seed: () => ({
|
|
37651
|
+
kind: "triggered-by",
|
|
37652
|
+
leafIds: []
|
|
37653
|
+
})
|
|
37654
|
+
},
|
|
37208
37655
|
expression: {
|
|
37209
37656
|
kind: "expression",
|
|
37210
37657
|
category: "logic",
|
|
@@ -37239,6 +37686,8 @@ var AUTOMATION_LEAF_KINDS = [
|
|
|
37239
37686
|
"manual",
|
|
37240
37687
|
"device-is",
|
|
37241
37688
|
"stationary-present",
|
|
37689
|
+
"time-is",
|
|
37690
|
+
"triggered-by",
|
|
37242
37691
|
"expression"
|
|
37243
37692
|
];
|
|
37244
37693
|
function leafCatalogEntry(kind) {
|
|
@@ -93972,6 +94421,13 @@ function describeTarget(site, ref) {
|
|
|
93972
94421
|
nativeCaps: new Map(device.caps.map((c) => [c, device.addonId]))
|
|
93973
94422
|
};
|
|
93974
94423
|
}
|
|
94424
|
+
/** The site as the time checks read it. */
|
|
94425
|
+
function readingOf$1(site) {
|
|
94426
|
+
return {
|
|
94427
|
+
location: site.location,
|
|
94428
|
+
timeZone: site.timeZone
|
|
94429
|
+
};
|
|
94430
|
+
}
|
|
93975
94431
|
function validateAutomation(site, json) {
|
|
93976
94432
|
const parsed = CurrentAutomationRecipeSchema.safeParse(json);
|
|
93977
94433
|
if (!parsed.success) return {
|
|
@@ -93983,7 +94439,8 @@ function validateAutomation(site, json) {
|
|
|
93983
94439
|
const issues = [
|
|
93984
94440
|
...bounds === null ? [] : [`when: ${bounds}`],
|
|
93985
94441
|
...leafIssues(site, recipe.when, "when"),
|
|
93986
|
-
...recipe.actions.flatMap((a, i) => stepIssues(site, a, `actions.${i}`))
|
|
94442
|
+
...recipe.actions.flatMap((a, i) => stepIssues(site, a, `actions.${i}`)),
|
|
94443
|
+
...recipeSiteIssues(recipe, readingOf$1(site)).map((i) => `${i.path}: ${i.message}`)
|
|
93987
94444
|
];
|
|
93988
94445
|
if (issues.length > 0) return {
|
|
93989
94446
|
ok: false,
|
|
@@ -94004,7 +94461,7 @@ function validateAction(site, json) {
|
|
|
94004
94461
|
ok: false,
|
|
94005
94462
|
issues: zodIssues(parsed.error)
|
|
94006
94463
|
};
|
|
94007
|
-
const issues = parsed.data.steps.flatMap((s, i) => stepIssues(site, s, `steps.${i}`));
|
|
94464
|
+
const issues = [...parsed.data.steps.flatMap((s, i) => stepIssues(site, s, `steps.${i}`)), ...stepGuardSiteIssues(parsed.data.steps, readingOf$1(site), "steps").map((i) => `${i.path}: ${i.message}`)];
|
|
94008
94465
|
return issues.length > 0 ? {
|
|
94009
94466
|
ok: false,
|
|
94010
94467
|
issues
|
|
@@ -94020,10 +94477,7 @@ function validateScheduler(site, json) {
|
|
|
94020
94477
|
issues: zodIssues(parsed.error)
|
|
94021
94478
|
};
|
|
94022
94479
|
const input = parsed.data;
|
|
94023
|
-
const verdict = validateSchedulerInput(input,
|
|
94024
|
-
location: site.location,
|
|
94025
|
-
timeZone: site.timeZone
|
|
94026
|
-
});
|
|
94480
|
+
const verdict = validateSchedulerInput(input, readingOf$1(site));
|
|
94027
94481
|
const issues = [
|
|
94028
94482
|
...verdict.ok ? [] : verdict.issues.map((i) => `${i.path}: ${i.message}`),
|
|
94029
94483
|
...input.activeWhen === void 0 ? [] : leafIssues(site, input.activeWhen, "activeWhen"),
|
|
@@ -136986,6 +137440,7 @@ exports.EVENT_DENSITY_BATCH_MAX = EVENT_DENSITY_BATCH_MAX;
|
|
|
136986
137440
|
exports.EVENT_KIND_BY_CAP = EVENT_KIND_BY_CAP;
|
|
136987
137441
|
exports.EVENT_KIND_LABELS = EVENT_KIND_LABELS;
|
|
136988
137442
|
exports.EVENT_KIND_LABEL_KEYS = EVENT_KIND_LABEL_KEYS;
|
|
137443
|
+
exports.EVENT_LEAF_ID_RE = EVENT_LEAF_ID_RE;
|
|
136989
137444
|
exports.EVENT_OWNER_TYPES = EVENT_OWNER_TYPES;
|
|
136990
137445
|
exports.EVENT_PAD_MS = EVENT_PAD_MS;
|
|
136991
137446
|
exports.EVENT_TAXONOMY = EVENT_TAXONOMY;
|
|
@@ -138535,6 +138990,7 @@ exports.TRAINING_JOB_SCHEMA = TRAINING_JOB_SCHEMA;
|
|
|
138535
138990
|
exports.TRAINING_RESULT_SCHEMA = TRAINING_RESULT_SCHEMA;
|
|
138536
138991
|
exports.TRANSCODE_DOWN_MAX_BITRATE_KBPS = TRANSCODE_DOWN_MAX_BITRATE_KBPS;
|
|
138537
138992
|
exports.TRANSCODE_DOWN_MAX_HEIGHT = TRANSCODE_DOWN_MAX_HEIGHT;
|
|
138993
|
+
exports.TRIGGERED_BY_MAX_IDS = TRIGGERED_BY_MAX_IDS;
|
|
138538
138994
|
exports.TamperStatusSchema = TamperStatusSchema;
|
|
138539
138995
|
exports.TargetKindCapsSchema = TargetKindCapsSchema;
|
|
138540
138996
|
exports.TargetKindLevelSchema = TargetKindLevelSchema;
|
|
@@ -139179,6 +139635,7 @@ exports.evaluateZoneRules = evaluateZoneRules;
|
|
|
139179
139635
|
exports.event = require_sleep.event;
|
|
139180
139636
|
exports.eventEmitterCapability = eventEmitterCapability;
|
|
139181
139637
|
exports.eventKindLabel = eventKindLabel;
|
|
139638
|
+
exports.eventLeafIdAt = eventLeafIdAt;
|
|
139182
139639
|
exports.eventOverlapsClipWindow = eventOverlapsClipWindow;
|
|
139183
139640
|
exports.evictionPolicyForMode = evictionPolicyForMode;
|
|
139184
139641
|
exports.evictionPolicyOfLocation = evictionPolicyOfLocation;
|
|
@@ -139275,6 +139732,8 @@ exports.hasHoldLeaves = hasHoldLeaves;
|
|
|
139275
139732
|
exports.hasModelBoundOverrides = hasModelBoundOverrides;
|
|
139276
139733
|
exports.hasMotionTrigger = hasMotionTrigger;
|
|
139277
139734
|
exports.hasOwnRunningPowerDefaults = hasOwnRunningPowerDefaults;
|
|
139735
|
+
exports.hasTimeLeaves = hasTimeLeaves;
|
|
139736
|
+
exports.hasTriggeredByLeaves = hasTriggeredByLeaves;
|
|
139278
139737
|
exports.hasUrlScheme = hasUrlScheme;
|
|
139279
139738
|
exports.heatPumpControlCapability = heatPumpControlCapability;
|
|
139280
139739
|
exports.heatPumpInstantCop = heatPumpInstantCop;
|
|
@@ -139302,6 +139761,7 @@ exports.hydrateSchema = require_sleep.hydrateSchema;
|
|
|
139302
139761
|
exports.iconCategoryLabel = iconCategoryLabel;
|
|
139303
139762
|
exports.iconEntry = iconEntry;
|
|
139304
139763
|
exports.iconsInCategory = iconsInCategory;
|
|
139764
|
+
exports.identifiedEventLeaves = identifiedEventLeaves;
|
|
139305
139765
|
exports.imageCapability = imageCapability;
|
|
139306
139766
|
exports.imageModesForKind = imageModesForKind;
|
|
139307
139767
|
exports.imageSettingsCapability = imageSettingsCapability;
|
|
@@ -139437,6 +139897,7 @@ exports.isVoidInput = isVoidInput;
|
|
|
139437
139897
|
exports.isZodSecret = require_sleep.isZodSecret;
|
|
139438
139898
|
exports.ituDbfsFromPreEpoch = ituDbfsFromPreEpoch;
|
|
139439
139899
|
exports.jobKindSchema = jobKindSchema;
|
|
139900
|
+
exports.judgeTimeWindow = judgeTimeWindow;
|
|
139440
139901
|
exports.kebabToCamel = kebabToCamel;
|
|
139441
139902
|
exports.keychainAdminCapability = keychainAdminCapability;
|
|
139442
139903
|
exports.keychainCapability = keychainCapability;
|
|
@@ -139550,6 +140011,7 @@ exports.networkLinkCapability = networkLinkCapability;
|
|
|
139550
140011
|
exports.networkQualityCapability = networkQualityCapability;
|
|
139551
140012
|
exports.nextLocalMidnight = require_implemented_methods.nextLocalMidnight;
|
|
139552
140013
|
exports.nextOccurrence = nextOccurrence;
|
|
140014
|
+
exports.nextTimeBoundaryInMs = nextTimeBoundaryInMs;
|
|
139553
140015
|
exports.nodeBenchmarkRefusals = nodeBenchmarkRefusals;
|
|
139554
140016
|
exports.nodeCertificatesCapability = nodeCertificatesCapability;
|
|
139555
140017
|
exports.nodeIdOfScopedKey = require_sleep.nodeIdOfScopedKey;
|
|
@@ -139734,6 +140196,7 @@ exports.readUseRootModel = readUseRootModel;
|
|
|
139734
140196
|
exports.readinessKey = require_sleep.readinessKey;
|
|
139735
140197
|
exports.readingMeta = require_sleep.readingMeta;
|
|
139736
140198
|
exports.rebootCapability = rebootCapability;
|
|
140199
|
+
exports.recipeSiteIssues = recipeSiteIssues;
|
|
139737
140200
|
exports.recommendNode = recommendNode;
|
|
139738
140201
|
exports.reconcileBlockDevices = reconcileBlockDevices;
|
|
139739
140202
|
exports.reconcileRemovedDevices = reconcileRemovedDevices;
|
|
@@ -139974,6 +140437,7 @@ exports.stateVocabularyFor = stateVocabularyFor;
|
|
|
139974
140437
|
exports.statisticCadenceMs = require_implemented_methods.statisticCadenceMs;
|
|
139975
140438
|
exports.statisticDraft = statisticDraft;
|
|
139976
140439
|
exports.statusInstanceOf = statusInstanceOf;
|
|
140440
|
+
exports.stepGuardSiteIssues = stepGuardSiteIssues;
|
|
139977
140441
|
exports.stepModelRunsOnFormat = stepModelRunsOnFormat;
|
|
139978
140442
|
exports.stepPulse = stepPulse;
|
|
139979
140443
|
exports.stepSettingSource = stepSettingSource;
|
|
@@ -140079,6 +140543,7 @@ exports.transcodeBody = transcodeBody;
|
|
|
140079
140543
|
exports.transformSecretWrite = require_sleep.transformSecretWrite;
|
|
140080
140544
|
exports.triggerLeafVariables = triggerLeafVariables;
|
|
140081
140545
|
exports.triggerVariablesOf = triggerVariablesOf;
|
|
140546
|
+
exports.triggeredByIssue = triggeredByIssue;
|
|
140082
140547
|
exports.tryConvertDelta = require_convert.tryConvertDelta;
|
|
140083
140548
|
exports.tryConvertUnit = require_convert.tryConvertUnit;
|
|
140084
140549
|
exports.turnProviderCapability = turnProviderCapability;
|
|
@@ -140130,6 +140595,7 @@ exports.weeklyMaskIndex = weeklyMaskIndex;
|
|
|
140130
140595
|
exports.weeklyMaskIsUniform = weeklyMaskIsUniform;
|
|
140131
140596
|
exports.weeklyMaskToWindows = weeklyMaskToWindows;
|
|
140132
140597
|
exports.whenDepth = whenDepth;
|
|
140598
|
+
exports.whenSiteIssues = whenSiteIssues;
|
|
140133
140599
|
exports.widenDeviceConstructor = widenDeviceConstructor;
|
|
140134
140600
|
exports.windowsToWeeklyMask = windowsToWeeklyMask;
|
|
140135
140601
|
exports.wiringAddonHealthSchema = wiringAddonHealthSchema;
|