Use open-meteo for weather data outside the US

Fixes #50
This commit is contained in:
James Babcock committed 2026-10-23 14:11:40 -07:00
1 parent cb165d90d2
commit 2c819c8d0e
13 files changed
+537 -25

No files matched your search

+1
View File
@@ -97,6 +97,7 @@ test('Weather uses the portable location provider and preserves NWS parsing', as
'./location-permissions': { hasLocationPermission: () => permission },
'./settings-store': { getStringSetting: (_key, fallback) => fallback, setStringSetting() {} },
'./location': { getCurrentLocation: async () => fix },
'./reverse-geocode': { reverseGeocode: async () => null },
'../version': { USER_AGENT: 'test' },
'../util/http': { fetchWithUserAgent: async url => { requests.push(url); return { ok: true, json: async () =>
url.includes('/points/') ? { properties: { forecast: 'https://fixture/forecast', relativeLocation: { properties: { city: 'San Francisco', state: 'CA' } } } }
+120 -3
View File
@@ -5,22 +5,33 @@ const { loader } = require('./helpers/load-typescript.cjs');
const HOUR_MS = 60 * 60 * 1000;
const fix = { latitude: 37.7, longitude: -122.4, accuracyMeters: 8, timestampMs: 1234 };
/** weather.ts with fake timers, settings store and NWS responses. */
function weatherHarness({ periods, observation = null, store = new Map(), permission = () => true }) {
/**
* weather.ts with fake timers, settings store and NWS responses. Without
* NWS periods, the NWS point lookup 404s and openMeteo answers instead.
* nowMs, if given, fixes the module's clock.
*/
function weatherHarness({
periods, observation = null, openMeteo = null, place = null, store = new Map(), permission = () => true, nowMs,
}) {
const timers = new Map(), requests = [];
let nextTimer = 1;
const api = loader({
setTimeout: (fn, ms) => { const id = nextTimer++; timers.set(id, { fn: () => { timers.delete(id); fn(); }, ms }); return id; },
clearTimeout: id => timers.delete(id),
...(nowMs === undefined ? {} : { Date: class extends Date { static now() { return nowMs; } } }),
}, {
'./location-permissions': { hasLocationPermission: permission },
'./location': { getCurrentLocation: async () => fix },
'./reverse-geocode': { reverseGeocode: async () => place },
'./settings-store': { getStringSetting: (key, fallback) => store.get(key) ?? fallback, setStringSetting: (key, value) => store.set(key, value) },
'../version': { USER_AGENT: 'test' },
'../util/http': { fetchWithUserAgent: async url => {
requests.push(url);
let body;
if (url.includes('/points/')) {
if (url.startsWith('https://api.open-meteo.com/')) {
body = openMeteo;
} else if (url.includes('/points/')) {
if (!periods) return { ok: false, status: 404 };
body = { properties: {
forecast: 'https://fixture/forecast',
observationStations: observation ? 'https://fixture/stations' : undefined,
@@ -46,6 +57,27 @@ function weatherHarness({ periods, observation = null, store = new Map(), permis
const sunny = { name: 'Today', temperature: 70, temperatureUnit: 'F', shortForecast: 'Sunny', isDaytime: true };
/**
* An Open-Meteo answer (Fahrenheit, mph, unix seconds) whose hourly
* forecast starts at local midnight today, with hourly values from
* hour(index) (all fields optional).
*/
function openMeteoResponse({ offsetHours = 0, nowMs = Date.now(), hours = 192, hour = () => ({}), current = {} } = {}) {
const offsetMs = offsetHours * HOUR_MS;
const midnightMs = Math.floor((nowMs + offsetMs) / (24 * HOUR_MS)) * 24 * HOUR_MS - offsetMs;
const hourly = { time: [], temperature_2m: [], precipitation_probability: [], weather_code: [], wind_speed_10m: [], wind_direction_10m: [] };
for (let index = 0; index < hours; index++) {
const values = { temperature_2m: 60, precipitation_probability: 0, weather_code: 0, wind_speed_10m: 5, wind_direction_10m: 180, ...hour(index) };
hourly.time.push((midnightMs + index * HOUR_MS) / 1000);
for (const key of Object.keys(values)) hourly[key].push(values[key]);
}
return {
utc_offset_seconds: offsetHours * 3600,
current: { time: Math.floor(nowMs / 1000), temperature_2m: 64.2, relative_humidity_2m: 94, is_day: 1, weather_code: 2, wind_speed_10m: 2.7, wind_direction_10m: 292, ...current },
hourly,
};
}
test('Weather takes the condition from the NWS icon, falling back to the text', async () => {
const h = weatherHarness({
periods: [
@@ -128,6 +160,91 @@ test('Weather refreshes a stale cache at once and backs off after a failure', as
assert.ok(retry > 9 * 60_000 && retry <= 10 * 60_000, `retry delay ${retry}`);
});
test('Weather outside NWS coverage falls back to Open-Meteo and names the place with the geocoder', async () => {
const h = weatherHarness({
openMeteo: openMeteoResponse({ offsetHours: 10 }),
place: { locality: 'Sydney', region: 'New South Wales', country: 'Australia' },
});
const bridge = new h.api.WeatherBridge();
await bridge.refreshNow();
const state = bridge.snapshot();
assert.equal(state.phase, 'ready', state.status);
assert.equal(state.source, 'open-meteo');
assert.equal(state.locationName, 'Sydney, Australia');
assert.equal(h.requests.length, 2, 'one NWS point lookup, then one Open-Meteo request');
assert.match(h.requests[1], /latitude=37\.7000&longitude=-122\.4000/);
assert.match(h.requests[1], /temperature_unit=fahrenheit/);
assert.deepEqual({ ...state.current, timestampMs: null }, {
temperatureF: 64.2, description: 'Partly Cloudy', humidityPercent: 94, windSpeedMph: 2.7, windDirection: 'W',
timestampMs: null, observed: false, condition: 'partly-cloudy', isDaytime: true,
});
assert.equal(state.forecast.length, 14);
// The source survives a restart; a geocoder with nothing to say leaves the coordinates.
const restarted = new (weatherHarness({ openMeteo: openMeteoResponse(), store: h.store }).api.WeatherBridge)();
assert.equal(restarted.snapshot().source, 'open-meteo');
const unnamed = weatherHarness({ openMeteo: openMeteoResponse() });
const unnamedBridge = new unnamed.api.WeatherBridge();
await unnamedBridge.refreshNow();
assert.equal(unnamedBridge.snapshot().locationName, '37.7000, -122.4000');
});
test('Open-Meteo hours group into NWS-style day and night periods in local time', async () => {
// 3pm local, so the first period is the rest of today.
const offsetHours = -5;
const dayMs = 24 * HOUR_MS;
const nowMs = Math.floor(Date.now() / dayMs) * dayMs + (15 - offsetHours) * HOUR_MS;
const h = weatherHarness({
nowMs,
openMeteo: openMeteoResponse({
offsetHours, nowMs,
hour: index => ({
// Peaks at 80 at 1pm and bottoms out at 20 at 1am; rain tomorrow
// 3pm-5pm, then one stray hour of thunder; a windy 3am tonight.
temperature_2m: 50 + 30 * Math.sin(((index % 24) - 7) / 24 * 2 * Math.PI),
precipitation_probability: index === 24 + 15 ? 80 : 10,
weather_code: { [24 + 15]: 63, [24 + 16]: 63, [24 + 20]: 95 }[index] ?? 1,
wind_speed_10m: index === 24 + 3 ? 30 : 5,
wind_direction_10m: index === 24 + 3 ? 90 : 180,
}),
}),
place: { locality: '', region: 'Ontario', country: 'Canada' },
});
const bridge = new h.api.WeatherBridge();
await bridge.refreshNow();
const { forecast, locationName } = bridge.snapshot();
assert.equal(locationName, 'Ontario, Canada');
const weekday = days => ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][
new Date(nowMs + offsetHours * HOUR_MS + days * dayMs).getUTCDay()];
assert.deepEqual(Array.from(forecast.slice(0, 4), period => period.name), ['Today', 'Tonight', weekday(1), `${weekday(1)} Night`]);
assert.deepEqual(Array.from(forecast, period => period.isDaytime), Array.from({ length: 14 }, (_, index) => index % 2 === 0));
// Today is only 3pm-6pm, past its 1pm peak.
assert.equal(forecast[0].startTimeMs, nowMs);
assert.equal(Math.round(forecast[0].temperatureF), 76);
// Tonight runs to 6am and takes its low and its strongest wind.
assert.equal(Math.round(forecast[1].temperatureF), 20);
assert.equal(forecast[1].windSpeed, '30 mph');
assert.equal(forecast[1].windDirection, 'E');
// Tomorrow takes its most severe lasting code and its highest chance of rain.
assert.equal(Math.round(forecast[2].temperatureF), 80);
assert.equal(forecast[2].shortForecast, 'Rain');
assert.equal(forecast[2].condition, 'rain');
assert.equal(forecast[2].precipitationPercent, 80);
// One hour of thunder doesn't name tomorrow night.
assert.equal(forecast[3].shortForecast, 'Mostly Clear');
assert.equal(forecast[3].condition, 'partly-cloudy');
});
test('Open-Meteo after midnight starts with the rest of last night', async () => {
const dayMs = 24 * HOUR_MS;
const nowMs = Math.floor(Date.now() / dayMs) * dayMs + 2 * HOUR_MS;
const h = weatherHarness({ nowMs, openMeteo: openMeteoResponse({ nowMs }) });
const bridge = new h.api.WeatherBridge();
await bridge.refreshNow();
assert.deepEqual(Array.from(bridge.snapshot().forecast.slice(0, 3), period => period.name), ['Overnight', 'Today', 'Tonight']);
});
/** weather-indicators.ts with a fake shell, settings and bridge. */
function indicatorHarness() {
const settings = new Map(), settingListeners = new Set(), stateListeners = new Set(), trays = [];