homeyform 0.0.0-stage → 0.3.0

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/CONTRIBUTING.md +96 -0
  3. package/LICENSE +18 -0
  4. package/README.md +566 -2
  5. package/SECURITY.md +25 -0
  6. package/dist/src/core/address.js +42 -0
  7. package/dist/src/core/ai-guide.js +88 -0
  8. package/dist/src/core/alarms.js +46 -0
  9. package/dist/src/core/apply-order.js +86 -0
  10. package/dist/src/core/card-keys.js +62 -0
  11. package/dist/src/core/cards.js +135 -0
  12. package/dist/src/core/check.js +137 -0
  13. package/dist/src/core/config.js +57 -0
  14. package/dist/src/core/context.js +137 -0
  15. package/dist/src/core/devices.js +68 -0
  16. package/dist/src/core/drivers.js +14 -0
  17. package/dist/src/core/export-tree.js +147 -0
  18. package/dist/src/core/file-name.js +47 -0
  19. package/dist/src/core/flow-path.js +5 -0
  20. package/dist/src/core/flow-values.js +170 -0
  21. package/dist/src/core/flows.js +178 -0
  22. package/dist/src/core/grid.js +9 -0
  23. package/dist/src/core/headers.js +11 -0
  24. package/dist/src/core/hidden-input.js +28 -0
  25. package/dist/src/core/init-files.js +139 -0
  26. package/dist/src/core/json.js +41 -0
  27. package/dist/src/core/literal-secrets.js +158 -0
  28. package/dist/src/core/plan/compare-flow.js +133 -0
  29. package/dist/src/core/plan/compute-plan.js +153 -0
  30. package/dist/src/core/plan/diff.js +83 -0
  31. package/dist/src/core/plan/plan-alarms.js +72 -0
  32. package/dist/src/core/plan/plan-devices.js +267 -0
  33. package/dist/src/core/plan/plan-flows.js +237 -0
  34. package/dist/src/core/plan/plan-folders.js +79 -0
  35. package/dist/src/core/plan/plan-scripts.js +125 -0
  36. package/dist/src/core/plan/plan-variables.js +74 -0
  37. package/dist/src/core/plan/plan-zones.js +121 -0
  38. package/dist/src/core/plan/prune-guard.js +55 -0
  39. package/dist/src/core/plan/references.js +86 -0
  40. package/dist/src/core/plan/render.js +52 -0
  41. package/dist/src/core/plan/resolve-flows.js +156 -0
  42. package/dist/src/core/plan/selector.js +56 -0
  43. package/dist/src/core/plan/step.js +23 -0
  44. package/dist/src/core/plan/types.js +3 -0
  45. package/dist/src/core/raw.js +2 -0
  46. package/dist/src/core/redact.js +11 -0
  47. package/dist/src/core/repo.js +2 -0
  48. package/dist/src/core/result.js +20 -0
  49. package/dist/src/core/scopes.js +116 -0
  50. package/dist/src/core/scripts.js +24 -0
  51. package/dist/src/core/secret-env.js +45 -0
  52. package/dist/src/core/secret-rules.js +53 -0
  53. package/dist/src/core/secret-suggestions.js +133 -0
  54. package/dist/src/core/secret.js +11 -0
  55. package/dist/src/core/tokens.js +42 -0
  56. package/dist/src/core/variables.js +38 -0
  57. package/dist/src/core/write-policy.js +110 -0
  58. package/dist/src/core/yaml.js +129 -0
  59. package/dist/src/core/zones.js +54 -0
  60. package/dist/src/shell/check-command.js +59 -0
  61. package/dist/src/shell/commands.js +185 -0
  62. package/dist/src/shell/errors.js +8 -0
  63. package/dist/src/shell/execute-plan.js +357 -0
  64. package/dist/src/shell/export-messages.js +90 -0
  65. package/dist/src/shell/init-command.js +337 -0
  66. package/dist/src/shell/main.js +244 -0
  67. package/dist/src/shell/prompt.js +51 -0
  68. package/dist/src/shell/read-client.js +122 -0
  69. package/dist/src/shell/repo-files.js +169 -0
  70. package/dist/src/shell/write-client.js +58 -0
  71. package/package.json +66 -4
  72. package/templates/AI_GUIDE.md +81 -0
package/README.md CHANGED
@@ -1,3 +1,567 @@
1
- # Temporary Holding Version
1
+ # homeyform
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Keep a [Homey](https://homey.app)'s logic as files: Flows, Advanced Flows, Logic variables, zones,
4
+ flow folders, virtual devices, device groups, alarms and HomeyScript scripts. `homeyform export`
5
+ writes them as readable YAML (scripts as JavaScript), you edit and review them like code,
6
+ `homeyform plan` shows what would change on the Homey, and `homeyform apply` makes it so.
7
+
8
+ ```
9
+ homeyform export the Homey -> files
10
+ homeyform plan files vs the Homey: a diff, nothing written
11
+ homeyform apply files -> the Homey, after you confirm
12
+ ```
13
+
14
+ It talks to the Homey's local Web API on your network. Paired hardware is never changed: devices
15
+ are only referred to, by zone and name.
16
+
17
+ **Status:** a hobby project, tested on one Homey Self-Hosted Server so far. Homey Pro (2023) has
18
+ the same local API and is expected to work, but has not been tried. Read "A safe first run" before
19
+ you apply anything.
20
+
21
+ ## Why homeyform?
22
+
23
+ - **History and review.** A Flow is a file with readable diffs, so changes can be reviewed like
24
+ code, made in the app or in the files. Run `export` on a schedule and commit the result: the git
25
+ history shows what changed in the app, and when.
26
+ - **Previewed changes.** `plan` shows exactly what `apply` would write, in the files' terms.
27
+ Nothing is written without your confirmation, and if any reference in the files does not resolve
28
+ (a device, a variable, a Flow that does not exist), apply stops before the first write and lists
29
+ every problem.
30
+ - **Names, not ids.** In the files a device is its zone and name, a variable or a Flow its name.
31
+ If you re-pair a device (it gets a new id, and every Flow that used the old one breaks) and give
32
+ the new one the same name and zone, `apply` points the Flows back at it. This works when the
33
+ files were exported before the re-pair, the old device is gone (deleted, not just unreachable),
34
+ and the new device offers the same flow cards.
35
+ - **A head start on a new Homey.** A backup does not restore on another model; these files carry
36
+ the logic over: zones, variables, alarms, virtual devices, groups, scripts and Flows. Re-pair the
37
+ devices there first, with the same names and zones, install the same apps, and name the
38
+ top-level zone and the users as before. Devices themselves, app settings, moods, webhook URLs
39
+ (they contain the Homey's id) and favourites do not carry over.
40
+
41
+ What it does **not** do:
42
+
43
+ - **It does not know which flow cards exist or what arguments they take.** A card id or an
44
+ argument that is wrong passes `plan` and fails at `apply`, when the Homey refuses it.
45
+ - **`apply` is not a transaction.** Changes are written one by one; if a write fails halfway, the
46
+ ones before it stay. Run `plan` again to see what is left.
47
+ - **It has seen one Homey.** Tested on one Homey Self-Hosted Server so far.
48
+
49
+ ## Requirements
50
+
51
+ - Node.js 22 or newer.
52
+ - A Homey with the local Web API: **Homey Pro** or **Homey Self-Hosted Server**. Homey Bridge
53
+ (Homey Cloud) has no local API and is not supported.
54
+
55
+ ## Install
56
+
57
+ ```sh
58
+ npm install --global homeyform
59
+ ```
60
+
61
+ or into the repository that holds your Homey's files, with `npm install --save-dev homeyform` and
62
+ `npx homeyform …`.
63
+
64
+ ## Getting started with init
65
+
66
+ `homeyform init` sets up a new repository for your Homey in one go:
67
+
68
+ ```sh
69
+ homeyform init my-homey # a new or empty directory
70
+ ```
71
+
72
+ It goes in four numbered steps: the Homey's address; an API key to create in the Homey web app
73
+ (it shows only the boxes a first run needs: each read-only scope, and _Apps_ only if you use
74
+ HomeyScript -- the full table is under "API key" and in `homeyform init --help`); the key, pasted
75
+ at the prompt without being shown; and then check, export and plan. The check reads every kind
76
+ before anything is written. Then init writes `homey.yml` (every setting as a commented example)
77
+ and a `.gitignore`, runs the first export, offers to `git init` and commit it, and runs `plan`,
78
+ which must say `No changes`.
79
+
80
+ - `--address URL`: the address, instead of the question.
81
+ - **From the shell:** on a terminal, a `HOMEY_ADDRESS` or `HOMEY_TOKEN` already set is a question
82
+ ("HOMEY_TOKEN is set in this shell. Use it? [Y/n]"); `n` asks for a new one. Without a terminal
83
+ they are used as they are. The key is never a command-line flag.
84
+ - **A key the Homey refuses on every request** (401) is one line: mistyped, deleted or expired.
85
+ - **Literal secrets:** when the first export refuses a credential in clear (see "Secrets"), init
86
+ shows where (never the value) and the `homey.yml` rules that hide it, and asks whether to add
87
+ them; yes appends them to `homey.yml`, its comments kept, and exports again. Without a terminal,
88
+ or when no rule can fix a finding, it stops with the instructions.
89
+ - **Run it again to continue:** a directory that holds only files init writes (`homey.yml`,
90
+ `.gitignore`, the AI guide) and `.env` or `.git` is continued, not refused; an existing
91
+ `homey.yml` is kept as it is. A directory with anything else, an export's files included, is
92
+ refused (to export into an existing repository, run `homeyform export` there).
93
+ - `--ai claude|cursor|agents|none`: whether an AI assistant will edit Flows here; on a terminal
94
+ init asks. The guide (`templates/AI_GUIDE.md`, see "Using an AI assistant to edit Flows") is
95
+ written where the assistant reads it: `CLAUDE.md` (Claude Code), `.cursor/rules/homeyform.mdc`
96
+ (Cursor), `AGENTS.md` (another).
97
+ - `--git`: git init and commit without asking. git's own identity is used; init never sets one.
98
+ - Without a terminal, init needs `--address` (or `HOMEY_ADDRESS`) and `HOMEY_TOKEN`.
99
+
100
+ **The key for later runs.** init never writes the key. Every command reads it, and the address,
101
+ from the environment:
102
+
103
+ ```sh
104
+ # in this shell: paste the key, then Enter (not shown, not in the shell history)
105
+ read -rs HOMEY_TOKEN && export HOMEY_TOKEN HOMEY_ADDRESS=<the address>
106
+ ```
107
+
108
+ Or keep them in a `.env` file in the repository (init's `.gitignore` keeps it out of git):
109
+
110
+ ```sh
111
+ export HOMEY_ADDRESS=<the address>
112
+ export HOMEY_TOKEN=<the key>
113
+ ```
114
+
115
+ Then `chmod 600 .env`, and load it with `. ./.env` before a run. Each line needs `export`; a file
116
+ of plain `NAME=value` lines loads with `set -a; . ./.env; set +a`. The key is then in clear on
117
+ disk: whoever can read the file can use it. A password manager avoids that (see "Secrets").
118
+
119
+ Make a backup of the Homey before you apply anything. The steps below are the same first run by
120
+ hand.
121
+
122
+ ## A safe first run
123
+
124
+ 1. **Make a backup of the Homey** in the Homey app before you apply anything.
125
+ 2. **The address.** homeyform needs the Homey's local address as `http://<ip>` (add `:<port>` if
126
+ it is not 80). Use `http`, not `https`: the Homey's certificate is not valid for its IP address,
127
+ and the request fails. The IP address is shown in the Homey app's settings, or in your router's
128
+ list of connected devices.
129
+ 3. **A read-only API key.** In the Homey web app (my.homey.app): _Settings → API keys → New API
130
+ key_, with the read-only scopes listed under "API key". If you use HomeyScript, add the full
131
+ _Apps_ scope too: without it, plan reports every Flow that runs a script as an error. `export`
132
+ and `plan` cannot write with any key (their client sends nothing but GET requests), but a key
133
+ that may not write is the safe start.
134
+ 4. **The environment:**
135
+
136
+ ```sh
137
+ export HOMEY_ADDRESS=http://<ip> # http, not https; with :port if not 80
138
+ read -s HOMEY_TOKEN && export HOMEY_TOKEN # paste the key; it stays out of shell history
139
+ ```
140
+
141
+ 5. **Export into a new directory** (a git repository is the point):
142
+
143
+ ```sh
144
+ mkdir my-homey && cd my-homey && git init
145
+ homeyform export
146
+ git add . && git commit -m "First export"
147
+ ```
148
+
149
+ 6. **Plan:** `homeyform plan` must say `No changes: the Homey matches the files.` If it does not,
150
+ the files do not round-trip for your Homey: please open an issue (see "Contributing") rather
151
+ than apply.
152
+
153
+ From there: edit a file (for example `enabled: false` in a Flow), run `homeyform plan` to see the
154
+ change, and `homeyform apply` with a key that may write (below) to make it. Run `homeyform export`
155
+ again whenever the Homey was changed in the app; the diff in git is what changed.
156
+
157
+ ## Commands
158
+
159
+ ```
160
+ homeyform export [--out DIR] [--values]
161
+ homeyform plan [--dir DIR] [paths…] [--prune] [--prune-many]
162
+ homeyform apply [--dir DIR] [paths…] [--yes] [--prune] [--prune-many]
163
+ homeyform check [--dir DIR]
164
+ homeyform init [dir] [--address URL] [--ai claude|cursor|agents|none] [--git] [--help]
165
+ ```
166
+
167
+ - `check`: can this address and key export and plan? One read per kind (zones, devices, Flows,
168
+ Advanced Flows, flow folders, Logic variables, alarms, users, HomeyScript scripts), with OK, the
169
+ scope a refusal means, or why the Homey is not reachable; a key refused on every request (401)
170
+ is one line instead (mistyped, deleted or expired). A key that may not read HomeyScript is
171
+ a note, not a failure, unless the repository in `--dir` has script files or a Flow that runs a
172
+ script: then export still works (it skips scripts) but plan does not, and check says so on a
173
+ line each. It never writes, so it cannot check apply's scopes: it lists them. Exit 0 when export
174
+ and plan would work, 1 otherwise.
175
+ - `init`: see "Getting started with init".
176
+
177
+ - `--out`, `--dir`: the repository's root (default: the current directory; `check` reads no
178
+ repository without `--dir`).
179
+ - `--values`: export writes the variables' _current_ values as `initial`. Without it, a variable
180
+ already in `variables.yml` keeps its `initial` (a value is runtime state, not configuration).
181
+ - `paths…`: limit plan and apply to files or directories, relative to `--dir`: `flows`,
182
+ `flows/Lights/Evening.advancedflow.yml`, `variables.yml`. A path that selects nothing is an
183
+ error, never "no changes".
184
+ - `--prune`: delete what is on the Homey but has no file (Flows, flow folders, variables, virtual
185
+ devices, groups, alarms, HomeyScript scripts, zones). Without it, those are listed as delete
186
+ candidates and left alone. With paths, only what lies inside them can be deleted. A kind whose
187
+ file or directory is missing (`flows/` included) is not managed: nothing of it is deleted. apply
188
+ refuses `--prune` in a directory with neither `zones.yml` nor `flows.lock.yml`.
189
+ - `--prune-many`: `--prune`, also when it would delete more than 10 objects, or more than a
190
+ quarter of a kind; without it apply refuses such a run (plan says so).
191
+ - `--yes`: apply without the question. Without a terminal and without `--yes`, apply refuses.
192
+
193
+ Exit codes: plan 0 (nothing to apply), 2 (changes), 1 (error); apply 0 (applied, or nothing to
194
+ do), 3 (declined), 1 (error); check 0 (ready), 1 (not); init 0 (plan says no changes), 1
195
+ (anything else); 64 for a missing or unknown command, and for init's wrong arguments. An unknown
196
+ option is an error (1).
197
+
198
+ > **Careful with `--prune`.** It deletes everything on the Homey that has no file, including a
199
+ > Flow, variable or alarm someone created in the app since your last export. Run `homeyform
200
+ > export` first (and review the diff), then `plan --prune`, and read every delete line before you
201
+ > apply.
202
+
203
+ ## API key
204
+
205
+ Homey API keys have scopes. In my.homey.app each scope is offered by its name, with a read-only
206
+ variant; homeyform uses one key, in `HOMEY_TOKEN`, for every command.
207
+
208
+ | In the app | Scope id | export / plan | apply |
209
+ | ---------- | -------------------------------------------- | ------------- | ------ |
210
+ | Zones | `homey.zone.readonly` / `homey.zone` | read-only | full |
211
+ | Devices | `homey.device.readonly` / `homey.device` | read-only | full |
212
+ | Flows | `homey.flow.readonly` / `homey.flow` | read-only | full |
213
+ | Logic | `homey.logic.readonly` / `homey.logic` | read-only | full |
214
+ | Alarms | `homey.alarm.readonly` / `homey.alarm` | read-only | full |
215
+ | Users | `homey.user.readonly` | read-only | read-only |
216
+ | Apps | `homey.app` | full (HomeyScript only) | full (HomeyScript only) |
217
+
218
+ - **HomeyScript needs the full _Apps_ scope (`homey.app`), even to read:** the Homey refuses
219
+ `homey.app.readonly` for scripts. Without it, export skips scripts with a warning; plan and apply
220
+ then report script files, and Flows that run a script (`@script(…)`), as errors rather than
221
+ compare them. Leave it out if you do not use HomeyScript.
222
+ - A full scope includes reading. A key with only the write scopes cannot read users, which plan
223
+ needs: keep _Users_ read-only in it.
224
+ - A refused write names the scope the key lacks.
225
+
226
+ ## The files
227
+
228
+ ```
229
+ homey.yml optional settings
230
+ zones.yml the zone tree
231
+ variables.yml Logic variables
232
+ devices.yml virtual devices and device groups (configuration, unlike paired hardware)
233
+ devices.lock.yml every device: zone path + name -> id, class, driver (written by export)
234
+ alarms.yml alarms
235
+ flows/<folder path>/<name>.flow.yml a standard Flow
236
+ flows/<folder path>/<name>.advancedflow.yml an Advanced Flow
237
+ flows.lock.yml per Flow: its id on the Homey; per Advanced Flow: card key -> the card's id
238
+ scripts/<name>.js a HomeyScript script, its code (the directory is homey.yml's scriptsDir)
239
+ ```
240
+
241
+ - **Export owns these files.** It writes them, and deletes the files of Flows and scripts that no
242
+ longer exist. Comments are not kept: each file but the Flows and scripts gets a fixed header, and a comment
243
+ written by hand is gone at the next export. Write notes into a Flow as a note card instead.
244
+ - **An empty kind is `[]`**: "none", not "not managed". A kind whose file does not exist is not
245
+ managed at all.
246
+ - **File names** are the Flow's (or folder's, or script's) name with `/`, `\`, control characters
247
+ and `:*?"<>|` replaced by `_`, leading spaces and trailing spaces and dots trimmed, and a name
248
+ Windows reserves followed by `_` (`CON` → `CON_`), so the files work on Linux, macOS and
249
+ Windows. A Flow's exact name is its `name:`; a script's name is its file name. There is one Flow
250
+ per name: `X.flow.yml` beside `X.advancedflow.yml` is an error. Two names whose files differ
251
+ only in case or Unicode normalisation are an export error (one file on macOS and Windows).
252
+ - **Flow folders are directories.** Moving a file to another directory moves the Flow to that
253
+ folder; a missing folder is created. A renamed directory is a new folder (the old one is pruned
254
+ with `--prune` once it is empty).
255
+ - **Values are runtime state.** A variable's current value and an alarm's on/off switch are never
256
+ compared or written. Card positions (`x`, `y`) are exported, rounded to a 20 px grid, but plan
257
+ ignores them: the layout belongs to the app.
258
+ - Keys are written in a fixed order and lists sorted, so the Homey's own ordering is never a diff.
259
+
260
+ ### zones.yml
261
+
262
+ ```yaml
263
+ - name: House
264
+ icon: home
265
+ zones:
266
+ - name: Garden
267
+ icon: garden
268
+ - name: Ground Floor
269
+ icon: default
270
+ zones:
271
+ - name: Kitchen
272
+ icon: kitchen
273
+ - name: Lounge
274
+ icon: livingRoom
275
+ ```
276
+
277
+ A zone is matched by its path (`House/Ground Floor/Kitchen`). apply creates zones and changes an
278
+ icon; it never renames a zone and cannot create a second top-level zone.
279
+
280
+ ### variables.yml
281
+
282
+ ```yaml
283
+ - name: dashboardAuthHeader
284
+ type: string
285
+ initial: !secret DASHBOARD_AUTH_HEADER
286
+ - name: morningBrightness
287
+ type: number
288
+ initial: 0.8
289
+ - name: vacationMode
290
+ type: boolean
291
+ initial: false
292
+ ```
293
+
294
+ `initial` is the value a new variable is created with. An existing variable's value is never
295
+ written; a change of `type` is an error.
296
+
297
+ ### devices.yml
298
+
299
+ ```yaml
300
+ - name: Guest Mode
301
+ zone: House
302
+ driver: homey:virtualdrivervirtualsocket:driver
303
+ class: socket
304
+ - name: Lounge Lights
305
+ zone: House/Ground Floor/Lounge
306
+ group: { class: light, members: [ Floor Lamp, Lounge Lamp ] }
307
+ - name: Nap Time
308
+ zone: House/Upper Floor/Nursery
309
+ driver: homey:virtualdrivervirtualbutton:driver
310
+ class: button
311
+ ```
312
+
313
+ Only virtual devices (sockets, buttons) and device groups: they are configuration. A group's
314
+ members are named alone, since they must be in the group's zone; a `light` group takes devices of
315
+ class `light` or with `virtualClass: light`. An entry at a paired device's zone and name is an
316
+ error. `devices.lock.yml` lists every device with its id, for a reader; nothing reads it back.
317
+
318
+ ### alarms.yml
319
+
320
+ ```yaml
321
+ - name: Wake Up
322
+ time: "06:30"
323
+ weekdays: [monday, tuesday, wednesday, thursday, friday]
324
+ ```
325
+
326
+ ### An Advanced Flow
327
+
328
+ ```yaml
329
+ name: Lounge Motion Light
330
+ enabled: true
331
+ cards:
332
+ c1:
333
+ type: action
334
+ card: "on"
335
+ device: { zone: House/Ground Floor/Lounge, name: Lounge Lamp }
336
+ x: 800
337
+ y: 0
338
+ c2:
339
+ type: condition
340
+ card: homey:app:com.athom.homeyscript:run
341
+ args: { script: "@script(Is Dark Outside)" }
342
+ x: 400
343
+ y: 0
344
+ outputTrue: [c1]
345
+ c3:
346
+ type: trigger
347
+ card: alarm_motion_true
348
+ device: { zone: House/Ground Floor/Lounge, name: Motion Sensor }
349
+ x: 0
350
+ y: 0
351
+ outputSuccess: [c2]
352
+ ```
353
+
354
+ - **Card keys** `c1`, `c2`, … stand for the cards' ids on the Homey. A key is kept for good
355
+ (`flows.lock.yml`), so a card moved in the app changes its `x`/`y` and nothing else. A card you
356
+ add needs a key that is not in the lock yet (the lock's `next` is a safe choice); a removed
357
+ card's key is never handed out again.
358
+ - **A card's fields**, in this order: `type` (`trigger`, `condition`, `action`, `delay`, `any`,
359
+ `all`, `start`, `note`), `card`, `device` or `zone` (the card's owner), `inverted`, `droptoken`,
360
+ `input`, `args`, `delay`, `duration`, `color`, `value`, `x`, `y`, and the outputs.
361
+ - **Outputs** list the cards that follow: `outputSuccess`, `outputTrue` / `outputFalse` (a
362
+ condition), `outputError` (when the card fails). An `all` card waits for its inputs:
363
+ `input: [c4::outputSuccess, c5::outputSuccess]`.
364
+ - **`card`:** a device's or zone's card is the card's own id (`"on"`, `dim`, `alarm_motion_true`)
365
+ with `device:` or `zone:` beside it. Homey's own cards keep their full id
366
+ (`homey:manager:cron:sunset`), and so do apps' cards (`homey:app:<app id>:…`), which are the same
367
+ on every Homey with the app.
368
+ - **A Start block** (`type: start`, no `card`) is what lets another Flow start this one: begin an
369
+ Advanced Flow that other Flows start with a Start block whose `outputSuccess` is wired, not with
370
+ a _This Flow is started_ trigger card (the Homey does not start such a Flow from another one).
371
+ - **A note** is `type: note` with `color` and `value` (the text).
372
+
373
+ ```yaml
374
+ c13:
375
+ type: delay
376
+ args: { delay: { multiplier: 60, number: "30" } }
377
+ x: 1200
378
+ y: 0
379
+ outputSuccess: [c2]
380
+ c14:
381
+ type: note
382
+ color: green
383
+ value: Turns the lounge lamp up slowly at sunrise, unless vacationMode is on.
384
+ x: -260
385
+ y: 400
386
+ c15:
387
+ type: start
388
+ x: 0
389
+ y: 0
390
+ outputSuccess: [c12]
391
+ ```
392
+
393
+ ### A standard Flow
394
+
395
+ ```yaml
396
+ name: Wind Down
397
+ enabled: true
398
+ trigger:
399
+ card: homey:manager:flow:programmatic_trigger
400
+ conditions:
401
+ - card: onoff
402
+ device: { zone: House, name: Guest Mode }
403
+ group: group1
404
+ inverted: true
405
+ actions:
406
+ - card: "off"
407
+ device: { zone: House, name: Guest Mode }
408
+ group: then
409
+ delay:
410
+ multiplier: 1
411
+ number: "5"
412
+ - card: homey:manager:notifications:create_notification
413
+ group: else
414
+ args: { text: Already off }
415
+ ```
416
+
417
+ A standard Flow keeps the order of its conditions and actions. `group` is the condition's group
418
+ (`group1`, …) or the action's branch (`then`, `else`).
419
+
420
+ ### References
421
+
422
+ Every id is replaced by a name, and resolved back on plan and apply:
423
+
424
+ | What | In the file |
425
+ | -------------------------------------- | -------------------------------------------------------------------- |
426
+ | a device (a card's owner) | `device: { zone: House/Ground Floor/Kitchen, name: Kitchen Spots }` |
427
+ | a zone (a card's owner) | `zone: House/Ground Floor` |
428
+ | a Logic variable (arg `variable`) | `variable: "@var(vacationMode)"` |
429
+ | a user (arg `user`) | `user: "@user(Test User)"` |
430
+ | an alarm (arg `alarm`) | `alarm: "@alarm(Wake Up)"` |
431
+ | a HomeyScript script (arg `script`) | `script: "@script(Is Dark Outside)"` |
432
+ | a Flow to start (arg `flow`) | `flow: Mark Done` (its name, of either type) |
433
+ | a variable inside text | `[[var:dashboardUrl]]` |
434
+ | a device capability inside text | `[[device:House/Ground Floor/Kitchen\|Kettle\|onoff]]` |
435
+ | a card's own token inside text | `[[trigger:c3:tag]]`, `[[card:c1:error]]` |
436
+ | a droptoken: a variable | `droptoken: { variable: vacationMode }` |
437
+ | a droptoken: a device capability | `droptoken: { device: { zone: House, name: Guest Mode }, capability: onoff }` |
438
+ | a droptoken: a card's token | `droptoken: trigger:c1:tag` |
439
+
440
+ - A device is identified by its zone path **and** name, since names repeat across zones (a
441
+ _Motion Sensor_ in every room). The zone path is the full path from the top-level zone.
442
+ - `@var(…)`, `@user(…)` and a bare Flow name are read only in args named `variable`, `user` and
443
+ `flow` of Homey's own cards; `@alarm(…)` only in the alarm cards' `alarm` arg; `@script(…)` only
444
+ in HomeyScript's `run` and `runWithArg` cards. Elsewhere such text is left as it is.
445
+ - **A rename in the app breaks the reference:** plan reports it as unresolved rather than guess.
446
+ Rename in the files and apply, or rename in the app and export.
447
+ - A reference to something the same apply creates (a new variable, virtual device, alarm, script
448
+ or Flow) resolves.
449
+
450
+ ### Lock files
451
+
452
+ `flows.lock.yml` holds, per Flow file, the Flow's id on the Homey, and per Advanced Flow file
453
+ also each card key's id on the Homey and `next`, the first key never used. plan uses a Flow's id
454
+ to notice a Flow renamed in the Homey app since the last export. `devices.lock.yml` is the map
455
+ from zone and name to device id. Both are written by homeyform (export, and apply for
456
+ `flows.lock.yml`) and committed with the files. **Never edit them by hand.** Moving a Flow file to
457
+ another directory is fine: plan and apply carry its lock entry over.
458
+
459
+ ```yaml
460
+ flows/Lounge Motion Light.advancedflow.yml:
461
+ id: 4e2b9c71-58d3-4f0a-b6e1-93a7c2d0f845
462
+ next: 4
463
+ cards:
464
+ c1: c73e2a5f-4b8d-4eab-9f36-9a5dab2c8e43
465
+ c2: b62d1f4e-3a7c-4d9a-8e25-8f4c9a1b7d32
466
+ c3: a51c0e3d-2f6b-4c89-9d14-7e3b8f0a6c21
467
+ flows/Wind Down.flow.yml:
468
+ id: 6f1d3b84-2c5a-4e97-a0b3-58e2d7c9f164
469
+ ```
470
+
471
+ ### HomeyScript scripts
472
+
473
+ `scripts/<name>.js` is a script's code, verbatim; its file name is its name. apply creates a
474
+ script from a new file and updates a changed one's code. Athom's `example-*` scripts are neither
475
+ exported nor deleted. Scripts are managed only when the scripts directory exists.
476
+
477
+ ## homey.yml
478
+
479
+ Every key is optional.
480
+
481
+ ```yaml
482
+ secrets:
483
+ - name: HUB_BASIC # written as `!secret HUB_BASIC`
484
+ pattern: 'Authorization:' # a JavaScript regular expression, tested against every string value
485
+ - name: HUB_TOKEN
486
+ variable: hubToken # or: this Logic variable's value, whatever it is
487
+ standardFlowFolder: Voice # warn about standard Flows outside this flow folder
488
+ scriptsDir: homeyscripts # where HomeyScript's scripts are (default: scripts)
489
+ ```
490
+
491
+ - **`secrets`:** values that must never be written to a file (see "Secrets").
492
+ - **`standardFlowFolder`:** if you keep standard Flows for a reason (for example, voice assistants
493
+ show starred standard Flows as scenes, but not Advanced Flows), name their folder here; export
494
+ and plan then warn about a standard Flow anywhere else. Unset: no warning.
495
+ - **`scriptsDir`:** the directory, relative to the repository's root, that holds HomeyScript's
496
+ scripts; default `scripts`. Set it when `scripts/` holds something else: export then writes and
497
+ deletes `*.js` only there. It must lie inside the repository and not under `flows/`.
498
+
499
+ ## Secrets
500
+
501
+ A value that must not be in a file is written as `!secret NAME`, by export following the
502
+ `secrets:` rules of `homey.yml`, or by hand. **apply reads it from the environment variable
503
+ `NAME`**, the same way it reads `HOMEY_TOKEN`. export and plan never need the values.
504
+
505
+ ```sh
506
+ # values in a SOPS-encrypted dotenv file:
507
+ sops exec-env secrets/homey.sops.env 'homeyform apply'
508
+
509
+ # values in 1Password: a .env file of references
510
+ op run --env-file=homey.env -- homeyform apply
511
+
512
+ # or by hand, for one run:
513
+ HUB_BASIC="$(cat ~/.hub-header)" homeyform apply
514
+ ```
515
+
516
+ - **Export refuses a literal secret.** If a file would hold a match of a rule's pattern, or a
517
+ credential the built-in rules recognise (an `Authorization: Basic|Bearer …` header value, a long
518
+ `Bearer …` token), export writes nothing and lists each match by location, never the value, with
519
+ the `homey.yml` rule that fixes it.
520
+ - Every `!secret` name the planned changes need must be set, to a value that is not empty, or
521
+ apply lists the names that are not and stops before the question and before any write.
522
+ - Only the names the files use are read from the environment. `HOMEY_*` names are homeyform's own
523
+ and are refused as `!secret` names, so the Homey's key can never end up in a Flow.
524
+ - A value goes into the request body and nowhere else: not into an argument, a file, a URL or the
525
+ output, from which the values are also redacted.
526
+
527
+ ## Safety
528
+
529
+ - **Paired hardware is never written.** apply never renames, moves, deletes or controls a paired
530
+ device. Only virtual devices and device groups listed in `devices.yml` are created, changed or
531
+ deleted.
532
+ - **export and plan cannot write:** their client refuses anything but a GET before it reaches the
533
+ network. apply writes through a separate client that refuses anything not on its list (there is
534
+ no route that runs a Flow or a script, or changes a variable's value).
535
+ - **No write before you confirm,** and none at all if a reference in the selected files does not
536
+ resolve.
537
+ - **Deletes need `--prune`.** A zone that still holds a device or a subzone, the root zone, a
538
+ folder that is not empty, and a variable, alarm, virtual device, group or script a Flow still
539
+ uses are never deleted.
540
+
541
+ ## Using an AI assistant to edit Flows
542
+
543
+ [`templates/AI_GUIDE.md`](templates/AI_GUIDE.md) is a set of instructions to copy into your own
544
+ repository (as `CLAUDE.md`, `AGENTS.md` or whatever your assistant reads), so that an AI edits the
545
+ files safely: no applies, no invented names or cards, a `plan` you read before anything reaches the
546
+ Homey.
547
+ `homeyform init --ai …` writes it for you. If the assistant's file is already there, a short
548
+ section pointing to `AI_GUIDE.md` is appended and the guide is written as `AI_GUIDE.md`; no file
549
+ is ever overwritten.
550
+
551
+ **Give the assistant a read-only API key** and run `apply` yourself with a separate key that may
552
+ write: that, not the guide's rules, is the real protection. The caveat: HomeyScript needs the full
553
+ _Apps_ scope (`homey.app`) even to read, so an assistant that must `plan` a repository with scripts
554
+ gets a key that could in principle write through the app's API (run or change scripts, manage
555
+ apps), although homeyform itself only reads with it. Without scripts, the read-only scopes suffice.
556
+
557
+ ## Contributing
558
+
559
+ Bug reports and pull requests are welcome; see [CONTRIBUTING.md](CONTRIBUTING.md). Never paste an
560
+ API key, your IP address or an unredacted export into an issue. Security issues: see
561
+ [SECURITY.md](SECURITY.md). The changes in each version are in [CHANGELOG.md](CHANGELOG.md).
562
+
563
+ ## Licence
564
+
565
+ MIT, see [LICENSE](LICENSE).
566
+
567
+ Homey is a trademark of Athom B.V. This project is not affiliated with or endorsed by Athom.
package/SECURITY.md ADDED
@@ -0,0 +1,25 @@
1
+ # Security
2
+
3
+ ## Reporting a vulnerability
4
+
5
+ Please report a vulnerability privately, through GitHub's private vulnerability reporting: the
6
+ **Security** tab of this repository, then **Report a vulnerability**. Do not open a public issue
7
+ for it.
8
+
9
+ Say what you found, how to reproduce it, and which version of homeyform you used. You will get an
10
+ answer as soon as possible; this is a hobby project, so allow a few days.
11
+
12
+ ## Keep your Homey's data private
13
+
14
+ Whether in a private report, an issue or a pull request:
15
+
16
+ - **Never paste an API key** (`HOMEY_TOKEN`) or a `!secret` value. If one has been posted, delete
17
+ the key in the Homey app (_Settings → API keys_) and create a new one.
18
+ - **Never paste an unredacted export** or plan output. They hold your device, zone and Flow names,
19
+ and can hold addresses, URLs and credentials typed into Flows. Reduce the problem to a small,
20
+ invented example instead.
21
+ - Leave out IP addresses and host names.
22
+
23
+ ## Supported versions
24
+
25
+ Only the latest minor version (`0.x`, the newest) receives fixes. Upgrade before you report.
@@ -0,0 +1,42 @@
1
+ // The Homey's address as `init` asks for it and `check` reads it: `http://<IP address>[:port]`,
2
+ // nothing else. The Homey's local Web API answers on http; on https it presents a certificate
3
+ // that is not valid for its IP address, and homeyform never turns certificate checks off.
4
+ import { err, ok } from './result.js';
5
+ /** Where a user finds the address, for a prompt and an error. */
6
+ export const ADDRESS_HINT = "The Homey's IP address is shown in the Homey app's settings, or in your router's list of connected devices.";
7
+ const SCHEME = /^[a-z][a-z0-9+.-]*:\/\//i;
8
+ /** Does the address name its scheme? Without one, fetch cannot use it. */
9
+ export const hasScheme = (address) => SCHEME.test(address.trim());
10
+ /** The http address of the same host (the port dropped: the Homey's http is on 80). */
11
+ export const httpAddressOf = (address) => {
12
+ const url = URL.canParse(address) ? new URL(address) : undefined;
13
+ return url ? `http://${url.hostname}` : address.replace(/^https:/i, 'http:');
14
+ };
15
+ /**
16
+ * An address as typed -- `192.0.2.10`, `192.0.2.10:8080`, `http://192.0.2.10/` -- as the one form
17
+ * homeyform uses. http is the default scheme; https is accepted with a warning; a path, a query,
18
+ * or a user name or password in it is an error.
19
+ */
20
+ export const normaliseAddress = (input) => {
21
+ const typed = input.trim();
22
+ if (typed === '')
23
+ return err(`no address given. ${ADDRESS_HINT}`);
24
+ const withScheme = SCHEME.test(typed) ? typed : `http://${typed}`;
25
+ if (!URL.canParse(withScheme))
26
+ return err(`${JSON.stringify(typed)} is not an address`);
27
+ const url = new URL(withScheme);
28
+ if (url.protocol !== 'http:' && url.protocol !== 'https:')
29
+ return err(`${JSON.stringify(typed)}: the address must be http:// (or https://)`);
30
+ if (url.username !== '' || url.password !== '')
31
+ return err('the address must not hold a user name or password');
32
+ if (url.pathname !== '/' || url.search !== '' || url.hash !== '')
33
+ return err(`${JSON.stringify(typed)}: give the address alone (http://<IP address>[:port]), no path: homeyform adds /api/ itself`);
34
+ const address = `${url.protocol}//${url.host}`;
35
+ const warnings = url.protocol === 'https:'
36
+ ? [
37
+ `https usually fails: the Homey's certificate is not valid for its IP address. Use ${httpAddressOf(address)} unless you know your https works.`,
38
+ ]
39
+ : [];
40
+ return ok({ address, warnings });
41
+ };
42
+ //# sourceMappingURL=address.js.map