@khalido/home 0.0.0-stage → 0.1.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 (217) hide show
  1. package/.env.example +25 -0
  2. package/CHANGELOG.md +25 -0
  3. package/LICENSE +21 -0
  4. package/README.md +77 -2
  5. package/dist/clock.d.ts +17 -0
  6. package/dist/clock.js +43 -0
  7. package/dist/config.d.ts +273 -0
  8. package/dist/config.js +392 -0
  9. package/dist/connector.d.ts +34 -0
  10. package/dist/connector.js +47 -0
  11. package/dist/connectors/amber/client.d.ts +61 -0
  12. package/dist/connectors/amber/client.js +106 -0
  13. package/dist/connectors/amber/index.d.ts +30 -0
  14. package/dist/connectors/amber/index.js +94 -0
  15. package/dist/connectors/cast/codec.d.ts +38 -0
  16. package/dist/connectors/cast/codec.js +102 -0
  17. package/dist/connectors/cast/index.d.ts +60 -0
  18. package/dist/connectors/cast/index.js +426 -0
  19. package/dist/connectors/cast/schemas.d.ts +167 -0
  20. package/dist/connectors/cast/schemas.js +87 -0
  21. package/dist/connectors/cast/serve.d.ts +11 -0
  22. package/dist/connectors/cast/serve.js +57 -0
  23. package/dist/connectors/cast/state.d.ts +62 -0
  24. package/dist/connectors/cast/state.js +133 -0
  25. package/dist/connectors/cast/tls.d.ts +3 -0
  26. package/dist/connectors/cast/tls.js +67 -0
  27. package/dist/connectors/cast/writes.d.ts +80 -0
  28. package/dist/connectors/cast/writes.js +390 -0
  29. package/dist/connectors/daikin/client.d.ts +65 -0
  30. package/dist/connectors/daikin/client.js +221 -0
  31. package/dist/connectors/daikin/index.d.ts +41 -0
  32. package/dist/connectors/daikin/index.js +108 -0
  33. package/dist/connectors/enphase/client.d.ts +65 -0
  34. package/dist/connectors/enphase/client.js +173 -0
  35. package/dist/connectors/enphase/index.d.ts +43 -0
  36. package/dist/connectors/enphase/index.js +174 -0
  37. package/dist/connectors/enphase/schemas.d.ts +51 -0
  38. package/dist/connectors/enphase/schemas.js +34 -0
  39. package/dist/connectors/hue/client.d.ts +48 -0
  40. package/dist/connectors/hue/client.js +95 -0
  41. package/dist/connectors/hue/events.d.ts +41 -0
  42. package/dist/connectors/hue/events.js +156 -0
  43. package/dist/connectors/hue/index.d.ts +42 -0
  44. package/dist/connectors/hue/index.js +205 -0
  45. package/dist/connectors/hue/schemas.d.ts +65 -0
  46. package/dist/connectors/hue/schemas.js +48 -0
  47. package/dist/connectors/hue/state.d.ts +43 -0
  48. package/dist/connectors/hue/state.js +84 -0
  49. package/dist/connectors/printers/client.d.ts +28 -0
  50. package/dist/connectors/printers/client.js +74 -0
  51. package/dist/connectors/printers/codec.d.ts +76 -0
  52. package/dist/connectors/printers/codec.js +246 -0
  53. package/dist/connectors/printers/found.d.ts +15 -0
  54. package/dist/connectors/printers/found.js +35 -0
  55. package/dist/connectors/printers/index.d.ts +52 -0
  56. package/dist/connectors/printers/index.js +282 -0
  57. package/dist/connectors/printers/state.d.ts +60 -0
  58. package/dist/connectors/printers/state.js +99 -0
  59. package/dist/connectors/spotify/changes.d.ts +32 -0
  60. package/dist/connectors/spotify/changes.js +84 -0
  61. package/dist/connectors/spotify/client.d.ts +199 -0
  62. package/dist/connectors/spotify/client.js +270 -0
  63. package/dist/connectors/spotify/index.d.ts +92 -0
  64. package/dist/connectors/spotify/index.js +745 -0
  65. package/dist/connectors/up/client.d.ts +183 -0
  66. package/dist/connectors/up/client.js +308 -0
  67. package/dist/connectors/up/index.d.ts +91 -0
  68. package/dist/connectors/up/index.js +213 -0
  69. package/dist/connectors/up/spend.d.ts +78 -0
  70. package/dist/connectors/up/spend.js +213 -0
  71. package/dist/connectors/up/webhook.d.ts +13 -0
  72. package/dist/connectors/up/webhook.js +32 -0
  73. package/dist/connectors/weather/blocks.d.ts +51 -0
  74. package/dist/connectors/weather/blocks.js +106 -0
  75. package/dist/connectors/weather/bom.d.ts +37 -0
  76. package/dist/connectors/weather/bom.js +237 -0
  77. package/dist/connectors/weather/ftp.d.ts +5 -0
  78. package/dist/connectors/weather/ftp.js +46 -0
  79. package/dist/connectors/weather/google.d.ts +31 -0
  80. package/dist/connectors/weather/google.js +364 -0
  81. package/dist/connectors/weather/index.d.ts +53 -0
  82. package/dist/connectors/weather/index.js +267 -0
  83. package/dist/connectors/weather/open-meteo.d.ts +15 -0
  84. package/dist/connectors/weather/open-meteo.js +285 -0
  85. package/dist/connectors/weather/rain.d.ts +19 -0
  86. package/dist/connectors/weather/rain.js +51 -0
  87. package/dist/connectors/weather/source.d.ts +149 -0
  88. package/dist/connectors/weather/source.js +77 -0
  89. package/dist/connectors/weather/sun.d.ts +48 -0
  90. package/dist/connectors/weather/sun.js +65 -0
  91. package/dist/discover.d.ts +195 -0
  92. package/dist/discover.js +446 -0
  93. package/dist/energy.d.ts +61 -0
  94. package/dist/energy.js +162 -0
  95. package/dist/finders-entry.d.ts +1 -0
  96. package/dist/finders-entry.js +2 -0
  97. package/dist/finders.d.ts +45 -0
  98. package/dist/finders.js +173 -0
  99. package/dist/health.d.ts +28 -0
  100. package/dist/health.js +35 -0
  101. package/dist/home.d.ts +81 -0
  102. package/dist/home.js +114 -0
  103. package/dist/index.d.ts +11 -0
  104. package/dist/index.js +11 -0
  105. package/dist/lane.d.ts +17 -0
  106. package/dist/lane.js +74 -0
  107. package/dist/locate.d.ts +27 -0
  108. package/dist/locate.js +59 -0
  109. package/dist/manifest.d.ts +31 -0
  110. package/dist/manifest.js +4 -0
  111. package/dist/manifests.d.ts +5 -0
  112. package/dist/manifests.js +23 -0
  113. package/dist/node-fetch.d.ts +9 -0
  114. package/dist/node-fetch.js +25 -0
  115. package/dist/poll.d.ts +10 -0
  116. package/dist/poll.js +33 -0
  117. package/docs/cast.md +133 -0
  118. package/docs/cli.md +39 -0
  119. package/docs/config.md +36 -0
  120. package/docs/decisions.md +150 -0
  121. package/docs/devices.md +128 -0
  122. package/docs/enphase-notes.md +24 -0
  123. package/docs/google.md +92 -0
  124. package/docs/ideas.md +69 -0
  125. package/docs/matter.md +55 -0
  126. package/docs/performance.md +12 -0
  127. package/docs/philosophy.md +23 -0
  128. package/docs/up.md +84 -0
  129. package/docs/using.md +65 -0
  130. package/docs/weather-probe.md +63 -0
  131. package/examples/home.jsonc +74 -0
  132. package/examples/home.svg +76 -0
  133. package/home.schema.json +474 -0
  134. package/llms.txt +32 -0
  135. package/package.json +143 -4
  136. package/skills/draw-home/README.md +26 -0
  137. package/skills/draw-home/SKILL.md +82 -0
  138. package/skills/use-home/SKILL.md +86 -0
  139. package/src/cli/cache.ts +29 -0
  140. package/src/cli/cast.ts +172 -0
  141. package/src/cli/config.ts +52 -0
  142. package/src/cli/context.ts +118 -0
  143. package/src/cli/discover.ts +175 -0
  144. package/src/cli/enphase.ts +62 -0
  145. package/src/cli/hue.ts +124 -0
  146. package/src/cli/locate.ts +47 -0
  147. package/src/cli/main.ts +182 -0
  148. package/src/cli/printers.ts +21 -0
  149. package/src/cli/probe.ts +274 -0
  150. package/src/cli/spotify.ts +76 -0
  151. package/src/cli/status.ts +228 -0
  152. package/src/cli/up.ts +350 -0
  153. package/src/clock.ts +62 -0
  154. package/src/config.ts +578 -0
  155. package/src/connector.ts +77 -0
  156. package/src/connectors/amber/README.md +40 -0
  157. package/src/connectors/amber/client.ts +157 -0
  158. package/src/connectors/amber/index.ts +118 -0
  159. package/src/connectors/cast/README.md +43 -0
  160. package/src/connectors/cast/codec.ts +126 -0
  161. package/src/connectors/cast/index.ts +551 -0
  162. package/src/connectors/cast/schemas.ts +101 -0
  163. package/src/connectors/cast/serve.ts +72 -0
  164. package/src/connectors/cast/state.ts +203 -0
  165. package/src/connectors/cast/tls.ts +100 -0
  166. package/src/connectors/cast/writes.ts +572 -0
  167. package/src/connectors/daikin/README.md +78 -0
  168. package/src/connectors/daikin/client.ts +295 -0
  169. package/src/connectors/daikin/index.ts +145 -0
  170. package/src/connectors/enphase/README.md +64 -0
  171. package/src/connectors/enphase/client.ts +244 -0
  172. package/src/connectors/enphase/index.ts +241 -0
  173. package/src/connectors/enphase/schemas.ts +45 -0
  174. package/src/connectors/hue/README.md +50 -0
  175. package/src/connectors/hue/client.ts +157 -0
  176. package/src/connectors/hue/events.ts +182 -0
  177. package/src/connectors/hue/index.ts +261 -0
  178. package/src/connectors/hue/schemas.ts +60 -0
  179. package/src/connectors/hue/state.ts +124 -0
  180. package/src/connectors/printers/README.md +46 -0
  181. package/src/connectors/printers/client.ts +114 -0
  182. package/src/connectors/printers/codec.ts +292 -0
  183. package/src/connectors/printers/found.ts +46 -0
  184. package/src/connectors/printers/index.ts +385 -0
  185. package/src/connectors/printers/state.ts +182 -0
  186. package/src/connectors/spotify/README.md +80 -0
  187. package/src/connectors/spotify/changes.ts +127 -0
  188. package/src/connectors/spotify/client.ts +387 -0
  189. package/src/connectors/spotify/index.ts +929 -0
  190. package/src/connectors/up/README.md +35 -0
  191. package/src/connectors/up/client.ts +518 -0
  192. package/src/connectors/up/index.ts +370 -0
  193. package/src/connectors/up/spend.ts +280 -0
  194. package/src/connectors/up/webhook.ts +62 -0
  195. package/src/connectors/weather/README.md +84 -0
  196. package/src/connectors/weather/blocks.ts +173 -0
  197. package/src/connectors/weather/bom.ts +344 -0
  198. package/src/connectors/weather/ftp.ts +48 -0
  199. package/src/connectors/weather/google.ts +425 -0
  200. package/src/connectors/weather/index.ts +380 -0
  201. package/src/connectors/weather/open-meteo.ts +309 -0
  202. package/src/connectors/weather/rain.ts +73 -0
  203. package/src/connectors/weather/source.ts +236 -0
  204. package/src/connectors/weather/sun.ts +114 -0
  205. package/src/discover.ts +634 -0
  206. package/src/energy.ts +221 -0
  207. package/src/finders-entry.ts +2 -0
  208. package/src/finders.ts +247 -0
  209. package/src/health.ts +74 -0
  210. package/src/home.ts +205 -0
  211. package/src/index.ts +62 -0
  212. package/src/lane.ts +110 -0
  213. package/src/locate.ts +85 -0
  214. package/src/manifest.ts +43 -0
  215. package/src/manifests.ts +27 -0
  216. package/src/node-fetch.ts +41 -0
  217. package/src/poll.ts +33 -0
@@ -0,0 +1,380 @@
1
+ import { type Clock, localTime, realClock } from "../../clock.ts";
2
+ import type { Integration, Status } from "../../home.ts";
3
+ import { field, type Manifest } from "../../manifest.ts";
4
+ import { poller } from "../../poll.ts";
5
+ import {
6
+ type BlockDef,
7
+ blockText,
8
+ checkBlocks,
9
+ type DayBlocks,
10
+ dayBlocks,
11
+ } from "./blocks.ts";
12
+ import { openMeteo } from "./open-meteo.ts";
13
+ import { type RainSoon, rainSoon } from "./rain.ts";
14
+ import {
15
+ DEFAULT_POLL_MINUTES,
16
+ type ForecastDay,
17
+ RAIN_HORIZON_MINUTES,
18
+ type SliceName,
19
+ type Slices,
20
+ sourcePollMinutes,
21
+ type SourceReading,
22
+ mergeWeather,
23
+ type WeatherSource,
24
+ } from "./source.ts";
25
+ import { type SunNow, sunNow } from "./sun.ts";
26
+
27
+ export {
28
+ type Condition,
29
+ type ForecastDay,
30
+ type ForecastHour,
31
+ mergeWeather,
32
+ nextPoll,
33
+ type Place,
34
+ type RainSegment,
35
+ type ReadRequest,
36
+ type SkyHour,
37
+ type SliceMeta,
38
+ type Slices,
39
+ type SourceReading,
40
+ type WeatherForecast,
41
+ type WeatherHourly,
42
+ type WeatherNow,
43
+ type WeatherRain,
44
+ type WeatherSky,
45
+ type WeatherSource,
46
+ } from "./source.ts";
47
+ export { type SunNext, type SunNow, sunNow } from "./sun.ts";
48
+ export { type RainSoon, rainSoon } from "./rain.ts";
49
+ export {
50
+ type BlockDef,
51
+ BLOCKS,
52
+ blockText,
53
+ type DayBlock,
54
+ type DayBlocks,
55
+ dayBlocks,
56
+ } from "./blocks.ts";
57
+ export { bom, type BomOptions } from "./bom.ts";
58
+ export { openMeteo, type OpenMeteoOptions } from "./open-meteo.ts";
59
+ export { google, type GoogleOptions } from "./google.ts";
60
+
61
+ /** The sun moves about a degree every four minutes; recompute it this often between source reads. */
62
+ const SUN_MS = 5 * 60_000;
63
+ /** However soon a source asks to be called again, never sooner than this. */
64
+ const MIN_POLL_MS = 60_000;
65
+
66
+ /**
67
+ * Where the home is, and the sources in preference order: each slice comes from the first source with a fresh one.
68
+ * The default is `[openMeteo()]`, which works anywhere with no key. In Australia put `bom({ fetch: ftpFetch(), forecastLocation })`
69
+ * first: it needs the FTP transport, so it is opt-in. `pollMinutes` (default 15) is how often observations and sky are read;
70
+ * forecasts follow their issue times. `timeZone` (IANA, the runtime's when left out) is where the home's day and its times are.
71
+ * `blocks` are the parts of the day `state.blocks` is cut into (`BLOCKS` by default).
72
+ */
73
+ export type WeatherConfig = {
74
+ latitude: number;
75
+ longitude: number;
76
+ sources?: WeatherSource[];
77
+ pollMinutes?: number;
78
+ timeZone?: string;
79
+ blocks?: readonly BlockDef[];
80
+ clock?: Clock;
81
+ };
82
+
83
+ /** The weather in the snapshot: one slice each from the preferred fresh source, the sun, and how each source is doing. */
84
+ export type WeatherState = Slices & {
85
+ sun: SunNow;
86
+ rainSoon?: RainSoon; // recomputed at every report and every 5 minutes; a consumer with its own clock calls `rainSoon(state.rain, now)`
87
+ blocks?: DayBlocks; // the same: `dayBlocks(state.hourly, now, state.timeZone, blocks)`
88
+ timeZone?: string; // the config's, which the blocks and the text use
89
+ sources: Record<
90
+ string,
91
+ { read: boolean; problem?: string; stopped?: string; anomaly?: string }
92
+ >;
93
+ };
94
+
95
+ const SLICE_NAMES = [
96
+ "now",
97
+ "forecast",
98
+ "hourly",
99
+ "sky",
100
+ "rain",
101
+ ] as const satisfies readonly SliceName[];
102
+
103
+ /** Outside weather as a home integration. Each source polls on its own cadence; the sun is computed, never fetched. */
104
+ export function weather(
105
+ config: WeatherConfig,
106
+ ): Integration<WeatherState, "weather"> {
107
+ const sources = config.sources ?? [openMeteo()];
108
+ const clock = config.clock ?? realClock;
109
+ const pollMinutes = config.pollMinutes ?? DEFAULT_POLL_MINUTES;
110
+ if (!(pollMinutes >= 1))
111
+ throw new Error(
112
+ `weather: pollMinutes must be at least 1, got ${config.pollMinutes}`,
113
+ );
114
+ if (config.blocks) checkBlocks(config.blocks); // a bad block fails here, not at every report
115
+ const place = { latitude: config.latitude, longitude: config.longitude };
116
+ const readings = new Map<string, SourceReading>();
117
+ const fresh = new Map<string, boolean>(); // whether a source's latest read brought anything new, rather than its last good slices again
118
+ let readAt: number | undefined; // the last read that brought something new: the part's `lastGoodAt`, so a re-report is not a read
119
+ const polls = new Map(sources.map((s) => [s.name, poller(clock)]));
120
+ let answer: (() => void) | undefined; // reports now: for a refresh no source will answer
121
+
122
+ return {
123
+ name: "weather",
124
+ /**
125
+ * Read every source now, whatever its schedule, forecasts included (`force`). A source with a floor (a billed one,
126
+ * Google) is not forced, and is skipped when its last read is younger than its floor.
127
+ */
128
+ refresh() {
129
+ for (const poll of polls.values()) poll.nudge();
130
+ if (sources.every((s) => readings.get(s.name)?.stop)) answer?.(); // every source has stopped: none will read, so say so now
131
+ },
132
+ async run(signal, update) {
133
+ const emit = () => {
134
+ const at = clock.now();
135
+ const slices = mergeWeather(
136
+ sources.map((source) => ({
137
+ source,
138
+ reading: readings.get(source.name),
139
+ })),
140
+ at,
141
+ pollMinutes,
142
+ );
143
+ const report = Object.fromEntries(
144
+ sources.map((s) => {
145
+ const r = readings.get(s.name);
146
+ return [
147
+ s.name,
148
+ {
149
+ read: r !== undefined,
150
+ problem: r?.problem,
151
+ stopped: r?.stop,
152
+ anomaly: r?.anomaly,
153
+ },
154
+ ];
155
+ }),
156
+ );
157
+ // Connected while some source's latest read brought something new; its older slices alone are a failed read, kept as state.
158
+ const status: Status = sources.some((s) => fresh.get(s.name))
159
+ ? "connected"
160
+ : sources.every((s) => readings.get(s.name)?.stop)
161
+ ? "unavailable"
162
+ : "disconnected";
163
+ const notes = (k: "problem" | "stopped" | "anomaly") =>
164
+ Object.values(report)
165
+ .map((r) => r[k])
166
+ .filter(Boolean)
167
+ .join("; ") || undefined;
168
+ const detail =
169
+ [notes("stopped"), notes("problem")].filter(Boolean).join("; ") ||
170
+ undefined;
171
+ const lastGoodAt =
172
+ status === "connected" && readAt !== undefined
173
+ ? new Date(readAt).toISOString()
174
+ : undefined;
175
+ update({
176
+ status,
177
+ detail,
178
+ anomaly: notes("anomaly"),
179
+ lastGoodAt,
180
+ state: {
181
+ ...slices,
182
+ sun: sunNow(place.latitude, place.longitude, at),
183
+ rainSoon: rainSoon(slices.rain, at),
184
+ blocks: dayBlocks(
185
+ slices.hourly,
186
+ at,
187
+ config.timeZone,
188
+ config.blocks,
189
+ ),
190
+ timeZone: config.timeZone,
191
+ sources: report,
192
+ },
193
+ });
194
+ };
195
+ const poll = async (source: WeatherSource) => {
196
+ const floorMs = (source.minPollMinutes ?? 0) * 60_000;
197
+ let force = false;
198
+ while (!signal.aborted) {
199
+ const at = clock.now();
200
+ const reading = await source.read({
201
+ place,
202
+ at,
203
+ pollMinutes: sourcePollMinutes(source, pollMinutes),
204
+ force,
205
+ signal,
206
+ });
207
+ if (signal.aborted) return; // an aborted read is not a problem to report
208
+ readings.set(source.name, reading);
209
+ const isFresh = SLICE_NAMES.some((k) =>
210
+ reading[k]?.fetchedAt
211
+ ? Date.parse(reading[k]!.fetchedAt) >= at
212
+ : false,
213
+ );
214
+ fresh.set(source.name, isFresh);
215
+ if (isFresh) readAt = Math.max(readAt ?? at, at);
216
+ emit();
217
+ if (reading.stop) return;
218
+ for (;;) {
219
+ const woken = await polls
220
+ .get(source.name)!
221
+ .wait(
222
+ Math.max(reading.nextAt - clock.now(), MIN_POLL_MS),
223
+ signal,
224
+ );
225
+ if (woken && clock.now() - at < floorMs) {
226
+ emit(); // a refresh never beats a billed source's floor, but it gets an answer: what there is now
227
+ continue;
228
+ }
229
+ force = woken && !floorMs;
230
+ break;
231
+ }
232
+ }
233
+ };
234
+ answer = emit;
235
+ const sun = async () => {
236
+ while (!signal.aborted) {
237
+ await clock.sleep(SUN_MS, signal);
238
+ if (!signal.aborted) emit();
239
+ }
240
+ };
241
+ await Promise.all([...sources.map(poll), sun()]);
242
+ },
243
+ };
244
+ }
245
+
246
+ /** "20:20" in the home's time zone (the runtime's when unset), or "?". */
247
+ const hhmmIn = (iso: string | undefined, timeZone?: string) =>
248
+ iso ? localTime(Date.parse(iso), timeZone) : "?";
249
+ const day = (d?: ForecastDay) =>
250
+ d
251
+ ? [
252
+ d.minC === undefined && d.maxC === undefined
253
+ ? ""
254
+ : `${d.minC ?? "?"}–${d.maxC ?? "?"} °C`,
255
+ d.text?.replace(/\.$/, "").toLowerCase() ?? "",
256
+ d.rainChance === undefined ? "" : `${d.rainChance}% chance of rain`,
257
+ ]
258
+ .filter(Boolean)
259
+ .join(" ")
260
+ : "?";
261
+
262
+ /** "rain in 30 min, ends in 55 min, 70%, 0.8 mm, moderate (google)", "raining, ends in 25 min", or "no rain for 2 h"; nothing when the forecast covers none of the window. */
263
+ export function rainText(
264
+ rain: Slices["rain"],
265
+ r: RainSoon | undefined,
266
+ ): string {
267
+ if (!r || !rain) return "";
268
+ const covered =
269
+ r.coveredMin >= RAIN_HORIZON_MINUTES
270
+ ? `${RAIN_HORIZON_MINUTES / 60} h`
271
+ : `${r.coveredMin} min`;
272
+ if (!r.wet) return `no rain for ${covered} (${rain.source})`;
273
+ const end =
274
+ r.endsInMin !== undefined
275
+ ? `ends in ${r.endsInMin} min`
276
+ : r.startsInMin === 0
277
+ ? `still at ${covered}`
278
+ : "";
279
+ const parts = [
280
+ r.startsInMin === 0 ? "raining" : `rain in ${r.startsInMin} min`,
281
+ end,
282
+ field(r.chance, (v) => `${v}%`),
283
+ field(r.mm, (v) => `${v} mm`),
284
+ r.intensity && r.intensity !== "NO_INTENSITY"
285
+ ? r.intensity.toLowerCase()
286
+ : "",
287
+ ];
288
+ return `${parts.filter(Boolean).join(", ")} (${rain.source})`;
289
+ }
290
+
291
+ /** The weather in one line, every part named by its source and time, never mixed. */
292
+ export function weatherText(
293
+ w: Slices & { rainSoon?: RainSoon; timeZone?: string },
294
+ sun: SunNow,
295
+ ): string {
296
+ const n = w.now;
297
+ const hhmm = (iso?: string) => hhmmIn(iso, w.timeZone);
298
+ const readings = n
299
+ ? [
300
+ field(n.tempC, (v) => `${v} °C`),
301
+ field(n.apparentC, (v) => `feels ${v}`),
302
+ field(n.humidity, (v) => `${v}%`),
303
+ field(n.rainMm, (v) => `${v} mm since 9am to ${hhmm(n.rainTo)}`),
304
+ ]
305
+ .filter(Boolean)
306
+ .join(", ")
307
+ : "";
308
+ const now = n
309
+ ? `${n.station ? `${n.station.name} (${n.station.distanceKm.toFixed(1)} km)` : n.source} ${hhmm(n.at)}${n.modelled ? " modelled" : ""}: ${readings}`
310
+ : "";
311
+ const f = w.forecast;
312
+ const forecast = f
313
+ ? `today ${day(f.today)}, tomorrow ${day(f.tomorrow)} (${f.source}${f.modelled ? " modelled" : ""})`
314
+ : "";
315
+ const k = w.sky;
316
+ const skyParts = k
317
+ ? [
318
+ field(k.cloudPct, (v) => `cloud ${v}%`),
319
+ field(k.uvIndex, (v) => `UV ${v}`),
320
+ field(k.radiationWm2, (v) => `${v} W/m²`),
321
+ ]
322
+ .filter(Boolean)
323
+ .join(", ")
324
+ : "";
325
+ const sky = k ? `${skyParts} (${k.source})` : "";
326
+ const sunText = `sun ${Math.round(sun.altitudeDeg)}°, ${sun.rising ? "rising" : "setting"}, ${sun.daylight ? `sets ${hhmm(sun.next.sunset)}` : `rises ${hhmm(sun.next.sunrise)}`}`;
327
+ return [now, forecast, sky, rainText(w.rain, w.rainSoon), sunText]
328
+ .filter(Boolean)
329
+ .join(" · ");
330
+ }
331
+
332
+ /** "today: afternoon …; evening …" and the same for tomorrow, when there is an hourly forecast. */
333
+ const blockLines = (state: Pick<WeatherState, "blocks" | "timeZone">) =>
334
+ state.blocks
335
+ ? (["today", "tomorrow"] as const)
336
+ .filter((d) => state.blocks![d].length)
337
+ .map(
338
+ (d) =>
339
+ `${d}: ${state.blocks![d].map((b) => blockText(b, state.timeZone)).join("; ")} (${state.blocks!.source})`,
340
+ )
341
+ : [];
342
+
343
+ export const manifest: Manifest<WeatherState> = {
344
+ name: "weather",
345
+ title: "Weather",
346
+ iotClass: "cloud_polling",
347
+ pollSeconds: DEFAULT_POLL_MINUTES * 60,
348
+ access:
349
+ "BOM anonymous FTP, Open-Meteo, Google with GOOGLE_WEATHER_KEY; sun computed",
350
+ env: [
351
+ {
352
+ name: "HOME_LATITUDE",
353
+ example: "-33.8688",
354
+ what: "the home's position for weather and the sun (this one is Sydney CBD)",
355
+ },
356
+ { name: "HOME_LONGITUDE", example: "151.2093", what: "" },
357
+ {
358
+ name: "HOME_TIMEZONE",
359
+ example: "Australia/Sydney",
360
+ optional: true,
361
+ what: "the home's IANA time zone for weather times and day blocks; this machine's when unset",
362
+ },
363
+ {
364
+ name: "HOME_BOM_FORECAST",
365
+ example: "Sydney",
366
+ optional: true,
367
+ what: "BOM précis location by name or aac; unset, the forecast comes from Open-Meteo",
368
+ },
369
+ {
370
+ name: "GOOGLE_WEATHER_KEY",
371
+ example: "",
372
+ optional: true,
373
+ what: "Maps Platform key with the Weather API enabled",
374
+ },
375
+ ],
376
+ lines: ({ state }) =>
377
+ state ? [weatherText(state, state.sun), ...blockLines(state)] : [],
378
+ record: ({ state }) =>
379
+ state ? { ...state, line: weatherText(state, state.sun) } : {},
380
+ };
@@ -0,0 +1,309 @@
1
+ import { z } from "zod";
2
+ import { getJson } from "../../connector.ts";
3
+ import {
4
+ type Condition,
5
+ type ForecastDay,
6
+ type ForecastHour,
7
+ HOURLY_MINUTES,
8
+ iso,
9
+ localDate,
10
+ nextPoll,
11
+ num,
12
+ type SourceReading,
13
+ USER_AGENT,
14
+ type WeatherSource,
15
+ } from "./source.ts";
16
+
17
+ // Open-Meteo's forecast API (https://open-meteo.com/en/docs): keyless, JSON, data CC BY 4.0. No `models`, so the default
18
+ // best_match answers; its BOM model (bom_access_global) has returned only nulls since BOM paused open data in mid 2025.
19
+
20
+ const CURRENT =
21
+ "temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_direction_10m,wind_gusts_10m,cloud_cover,uv_index,shortwave_radiation,weather_code";
22
+ const HOURLY =
23
+ "temperature_2m,apparent_temperature,precipitation_probability,precipitation,weather_code,wind_speed_10m,wind_gusts_10m,cloud_cover,uv_index,shortwave_radiation";
24
+ /** How many hours ahead `sky.hours` holds. */
25
+ const SKY_HOURS = 6;
26
+ const DAILY =
27
+ "temperature_2m_max,temperature_2m_min,precipitation_probability_max,precipitation_sum,weather_code";
28
+
29
+ const series = z.array(z.number().nullable()).optional();
30
+ const OpenMeteoResponse = z.looseObject({
31
+ utc_offset_seconds: z.number().optional(),
32
+ current: z.record(z.string(), z.unknown()).optional(),
33
+ hourly: z
34
+ .looseObject({
35
+ time: z.array(z.string()),
36
+ temperature_2m: series,
37
+ apparent_temperature: series,
38
+ precipitation_probability: series,
39
+ precipitation: series,
40
+ weather_code: series,
41
+ wind_speed_10m: series,
42
+ wind_gusts_10m: series,
43
+ cloud_cover: series,
44
+ uv_index: series,
45
+ shortwave_radiation: series,
46
+ })
47
+ .optional(),
48
+ daily: z
49
+ .looseObject({
50
+ time: z.array(z.string()),
51
+ temperature_2m_max: series,
52
+ temperature_2m_min: series,
53
+ precipitation_probability_max: series,
54
+ precipitation_sum: series,
55
+ weather_code: series,
56
+ })
57
+ .optional(),
58
+ });
59
+
60
+ /** WMO weather codes as Open-Meteo documents them (the table at the end of its docs page). */
61
+ const WMO: Record<number, string> = {
62
+ 0: "Clear sky",
63
+ 1: "Mainly clear",
64
+ 2: "Partly cloudy",
65
+ 3: "Overcast",
66
+ 45: "Fog",
67
+ 48: "Depositing rime fog",
68
+ 51: "Light drizzle",
69
+ 53: "Moderate drizzle",
70
+ 55: "Dense drizzle",
71
+ 56: "Light freezing drizzle",
72
+ 57: "Dense freezing drizzle",
73
+ 61: "Slight rain",
74
+ 63: "Moderate rain",
75
+ 65: "Heavy rain",
76
+ 66: "Light freezing rain",
77
+ 67: "Heavy freezing rain",
78
+ 71: "Slight snow",
79
+ 73: "Moderate snow",
80
+ 75: "Heavy snow",
81
+ 77: "Snow grains",
82
+ 80: "Slight rain showers",
83
+ 81: "Moderate rain showers",
84
+ 82: "Violent rain showers",
85
+ 85: "Slight snow showers",
86
+ 86: "Heavy snow showers",
87
+ 95: "Thunderstorm",
88
+ 96: "Thunderstorm with slight hail",
89
+ 99: "Thunderstorm with heavy hail",
90
+ };
91
+
92
+ /** WMO codes in a word: 0–1 clear, 2 partly cloudy, 3 cloudy, then by family. */
93
+ export function wmoCondition(code: number): Condition | undefined {
94
+ if (!(code in WMO)) return undefined;
95
+ if (code <= 1) return "clear";
96
+ if (code === 2) return "partly cloudy";
97
+ if (code === 3) return "cloudy";
98
+ if (code < 50) return "fog";
99
+ if (code < 60) return "drizzle";
100
+ if (code < 70) return "rain";
101
+ if (code < 80) return "snow";
102
+ if (code < 85) return "showers";
103
+ if (code < 95) return "snow"; // snow showers
104
+ return "storm";
105
+ }
106
+
107
+ /** Open-Meteo's local times ("2026-10-13T20:15") to UTC ISO, using the response's offset; undefined when it does not parse. Open-Meteo applies that one offset to every time it returns, across a daylight-saving change too (checked live, 2026-10-16), so it is right for all of them. */
108
+ const utc = (local: string, offset: number) => {
109
+ const t = Date.parse(`${local}Z`);
110
+ return Number.isNaN(t)
111
+ ? undefined
112
+ : iso(new Date(t - offset * 1000).toISOString());
113
+ };
114
+ const value = (s: (number | null)[] | undefined, i: number) =>
115
+ s?.[i] ?? undefined;
116
+
117
+ /** Map one forecast response to slices. Everything here is a model, so every slice says `modelled: true`. `forecastAt` is when the daily part was read. */
118
+ export function toSlices(
119
+ body: unknown,
120
+ fetchedAt: number,
121
+ forecastAt = fetchedAt,
122
+ ): Omit<SourceReading, "nextAt"> {
123
+ const r = OpenMeteoResponse.parse(body);
124
+ const offset = r.utc_offset_seconds ?? 0;
125
+ const odd: string[] = [];
126
+ const meta = (at: string) => ({
127
+ source: "open-meteo",
128
+ modelled: true,
129
+ at,
130
+ fetchedAt: new Date(fetchedAt).toISOString(),
131
+ });
132
+ const c = r.current;
133
+ const cur = (k: string) => num(c?.[k], `current.${k}`, odd);
134
+ const code = cur("weather_code");
135
+ const nowAt = typeof c?.time === "string" ? utc(c.time, offset) : undefined;
136
+ if (c && !nowAt) odd.push(`current.time not understood: ${String(c.time)}`);
137
+ const now = nowAt
138
+ ? {
139
+ ...meta(nowAt),
140
+ tempC: cur("temperature_2m"),
141
+ apparentC: cur("apparent_temperature"),
142
+ humidity: cur("relative_humidity_2m"),
143
+ windKmh: cur("wind_speed_10m"),
144
+ windDeg: cur("wind_direction_10m"),
145
+ gustKmh: cur("wind_gusts_10m"),
146
+ }
147
+ : undefined;
148
+ const h = r.hourly;
149
+ const unknownCodes = new Set<number>();
150
+ const hours = (h?.time ?? []).flatMap((t, i) => {
151
+ const at = utc(t, offset);
152
+ if (!at) odd.push(`hourly time not understood: ${t}`);
153
+ if (!at) return [];
154
+ const wmo = value(h?.weather_code, i);
155
+ if (wmo !== undefined && !(wmo in WMO)) unknownCodes.add(wmo);
156
+ const hour: ForecastHour & { radiationWm2?: number } = {
157
+ at,
158
+ tempC: value(h?.temperature_2m, i),
159
+ apparentC: value(h?.apparent_temperature, i),
160
+ rainChance: value(h?.precipitation_probability, i),
161
+ rainMm: value(h?.precipitation, i),
162
+ windKmh: value(h?.wind_speed_10m, i),
163
+ gustKmh: value(h?.wind_gusts_10m, i),
164
+ cloudPct: value(h?.cloud_cover, i),
165
+ uvIndex: value(h?.uv_index, i),
166
+ radiationWm2: value(h?.shortwave_radiation, i),
167
+ code: wmo === undefined ? undefined : String(wmo),
168
+ condition: wmo === undefined ? undefined : wmoCondition(wmo),
169
+ };
170
+ return [hour];
171
+ });
172
+ for (const code of unknownCodes)
173
+ odd.push(`unknown WMO code ${code} in the hourly forecast`);
174
+ // The hourly part is read once an hour; the sky's hours run from the hour it was read in.
175
+ const skyHours = hours
176
+ .filter((x) => Date.parse(x.at) + 3_600_000 > forecastAt)
177
+ .slice(0, SKY_HOURS)
178
+ .map((x) => ({
179
+ at: x.at,
180
+ cloudPct: x.cloudPct,
181
+ uvIndex: x.uvIndex,
182
+ radiationWm2: x.radiationWm2,
183
+ }));
184
+ const sky = now
185
+ ? {
186
+ ...meta(now.at),
187
+ cloudPct: cur("cloud_cover"),
188
+ uvIndex: cur("uv_index"),
189
+ radiationWm2: cur("shortwave_radiation"),
190
+ hours: skyHours,
191
+ }
192
+ : undefined;
193
+ const d = r.daily;
194
+ const day = (date: string): ForecastDay | undefined => {
195
+ const i = d?.time.indexOf(date) ?? -1;
196
+ if (!d || i < 0) return undefined;
197
+ const wmo = value(d.weather_code, i);
198
+ return {
199
+ date,
200
+ minC: value(d.temperature_2m_min, i),
201
+ maxC: value(d.temperature_2m_max, i),
202
+ rainChance: value(d.precipitation_probability_max, i),
203
+ rainMm: value(d.precipitation_sum, i),
204
+ text: wmo === undefined ? undefined : WMO[wmo],
205
+ code: wmo === undefined ? undefined : String(wmo),
206
+ };
207
+ };
208
+ const today = localDate(fetchedAt, offset);
209
+ const tomorrow = localDate(fetchedAt + 86_400_000, offset);
210
+ const forecastIso = new Date(forecastAt).toISOString();
211
+ const forecast = d
212
+ ? {
213
+ ...meta(forecastIso),
214
+ fetchedAt: forecastIso,
215
+ today: day(today),
216
+ tomorrow: day(tomorrow),
217
+ }
218
+ : undefined;
219
+ const hourly = hours.length
220
+ ? {
221
+ ...meta(forecastIso),
222
+ fetchedAt: forecastIso,
223
+ hours: hours.map(({ radiationWm2: _, ...x }) => x),
224
+ }
225
+ : undefined;
226
+ for (const [i, wmo] of (d?.weather_code ?? []).entries())
227
+ if (wmo !== null && !(wmo in WMO))
228
+ odd.push(`unknown WMO code ${wmo} on ${d?.time[i]}`);
229
+ if (code !== undefined && !(code in WMO))
230
+ odd.push(`unknown WMO code ${code} now`);
231
+ return {
232
+ now,
233
+ forecast,
234
+ hourly,
235
+ sky,
236
+ anomaly: odd.length ? odd.join("; ") : undefined,
237
+ };
238
+ }
239
+
240
+ export type OpenMeteoOptions = { fetch?: typeof fetch; base?: string };
241
+
242
+ /**
243
+ * Open-Meteo as a weather source: modelled `now`, `forecast`, `hourly` (today and tomorrow) and `sky` (the only source with radiation). `current` is
244
+ * 15-minutely, so it is read every poll; the daily and hourly parts come from models that update hourly at best, so
245
+ * they are read hourly (one call with everything), and the polls between ask for `current` alone.
246
+ */
247
+ export function openMeteo(options: OpenMeteoOptions = {}): WeatherSource {
248
+ const fetcher = options.fetch ?? fetch;
249
+ const base = options.base ?? "https://api.open-meteo.com/v1/forecast";
250
+ let last: Omit<SourceReading, "nextAt"> = {};
251
+ let full: { body: Record<string, unknown>; at: number } | undefined;
252
+ let fullDue = 0;
253
+ return {
254
+ name: "open-meteo",
255
+ owns: {
256
+ now: 30 * 60_000,
257
+ forecast: 6 * 3_600_000,
258
+ hourly: 6 * 3_600_000,
259
+ sky: 30 * 60_000,
260
+ }, // `current.time` starts its 15-minute interval
261
+ async read({
262
+ place,
263
+ at,
264
+ pollMinutes,
265
+ force,
266
+ signal,
267
+ }): Promise<SourceReading> {
268
+ const everything = force || !full || at >= fullDue;
269
+ const q = new URLSearchParams({
270
+ latitude: String(place.latitude),
271
+ longitude: String(place.longitude),
272
+ timezone: "auto",
273
+ current: CURRENT,
274
+ });
275
+ if (everything)
276
+ for (const [k, v] of Object.entries({
277
+ hourly: HOURLY,
278
+ daily: DAILY,
279
+ forecast_days: "2",
280
+ }))
281
+ q.set(k, v);
282
+ try {
283
+ const body = (await getJson(fetcher, `${base}?${q}`, {
284
+ init: { headers: { "user-agent": USER_AGENT } },
285
+ signal,
286
+ })) as Record<string, unknown>;
287
+ if (everything)
288
+ ((full = { body, at }), (fullDue = nextPoll(at, HOURLY_MINUTES)));
289
+ last = toSlices(
290
+ {
291
+ ...full!.body,
292
+ current: body.current,
293
+ utc_offset_seconds: body.utc_offset_seconds,
294
+ },
295
+ at,
296
+ full!.at,
297
+ );
298
+ return { ...last, nextAt: nextPoll(at, pollMinutes) };
299
+ } catch (error) {
300
+ return {
301
+ ...last,
302
+ anomaly: undefined,
303
+ problem: `open-meteo: ${(error as Error).message}`,
304
+ nextAt: nextPoll(at, pollMinutes),
305
+ };
306
+ }
307
+ },
308
+ };
309
+ }