iobroker.assistant 1.4.8 → 1.4.10

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/build/main.js CHANGED
@@ -38,6 +38,7 @@ const fs = __importStar(require("node:fs"));
38
38
  const node_child_process_1 = require("node:child_process");
39
39
  const node_async_hooks_1 = require("node:async_hooks");
40
40
  const node_util_1 = require("node:util");
41
+ const node_crypto_1 = require("node:crypto");
41
42
  const adapter_core_1 = require("@iobroker/adapter-core");
42
43
  const path = __importStar(require("node:path"));
43
44
  const mcp_server_1 = require("@iobroker/mcp-server");
@@ -64,6 +65,7 @@ const wyoming_1 = require("./lib/voice/wyoming");
64
65
  const esphome_1 = require("./lib/voice/esphome");
65
66
  const esphomeEntities_1 = require("./lib/voice/esphomeEntities");
66
67
  const mediaServer_1 = require("./lib/voice/mediaServer");
68
+ const mediaStream_1 = require("./lib/voice/mediaStream");
67
69
  const wakeWords_1 = require("./lib/voice/wakeWords");
68
70
  const tone_1 = require("./lib/voice/tone");
69
71
  const engines_1 = require("./lib/voice/engines");
@@ -352,6 +354,12 @@ class Assistant extends adapter_core_1.Adapter {
352
354
  mirroredTimers = new Map();
353
355
  /** Per satellite: resolves once its control objects exist, so state writes can wait for them. */
354
356
  satControlsReady = new Map();
357
+ /** Per media player (`<satId>.<objectId>`): the newest URL write, so a slow look at an older one cannot win. */
358
+ mediaPlays = new Map();
359
+ /** Per media player: the path of its live stream through ffmpeg on the media server, while there is one. */
360
+ mediaStreamPaths = new Map();
361
+ /** Whether ffmpeg starts, per binary, looked up at most once a minute (playing a URL asks every time). */
362
+ ffmpegCheck = null;
355
363
  /** Satellites whose `volume` state exists and is subscribed (see updateSatelliteVolume). */
356
364
  satVolumeEnsured = new Set();
357
365
  /** ESPHome satellite id → the table address already written to its object (see markEsphomeSatellite). */
@@ -996,6 +1004,11 @@ class Assistant extends adapter_core_1.Adapter {
996
1004
  const roomName = (room || '').replace(/^(enum\.rooms\.|system\.rooms\.)/, '');
997
1005
  return (roomName || device).replace(/[^\w-]/g, '_') || 'unknown';
998
1006
  }
1007
+ /** The id under `satellites.*` of the satellite a request came from, or '' when it has no states there (Wyoming). */
1008
+ knownSatelliteId(device, room) {
1009
+ const id = this.satelliteStateId(device, room);
1010
+ return this.satStatesEnsured.has(id) ? id : '';
1011
+ }
999
1012
  /** Reflect a satellite's status into `satellites.<room>.*` states (created on first sight). */
1000
1013
  async updateSatelliteState(device, room, state) {
1001
1014
  const id = await this.ensureSatelliteObjects(device, room);
@@ -1431,8 +1444,24 @@ class Assistant extends adapter_core_1.Adapter {
1431
1444
  await this.ensureState(`${id}.volume`, 'Volume (0…1)', 'number', 'level.volume', true);
1432
1445
  await this.ensureState(`${id}.command`, `Command (${Object.keys(esphomeEntities_1.MEDIA_COMMANDS).join(', ')})`, 'string', 'text', true);
1433
1446
  await this.ensureState(`${id}.muted`, 'Muted', 'boolean', 'media.mute', false);
1447
+ // Music from a URL (internet radio): the device plays it as media, not as an announcement
1448
+ await this.ensureState(`${id}.url`, 'Play a URL (radio, MP3/FLAC/WAV; others through ffmpeg); empty or "stop" = stop', 'string', 'media.url', true);
1449
+ await this.setObjectNotExistsAsync(`${id}.transcode`, {
1450
+ type: 'state',
1451
+ common: {
1452
+ name: 'Always play URLs through ffmpeg (else only when the device cannot decode them)',
1453
+ type: 'boolean',
1454
+ role: 'switch',
1455
+ read: true,
1456
+ write: true,
1457
+ def: false,
1458
+ },
1459
+ native: {},
1460
+ });
1434
1461
  this.subscribeStates(`${id}.volume`);
1435
1462
  this.subscribeStates(`${id}.command`);
1463
+ this.subscribeStates(`${id}.url`);
1464
+ this.subscribeStates(`${id}.transcode`);
1436
1465
  }
1437
1466
  else if (entity.kind === 'update') {
1438
1467
  await this.setObjectNotExistsAsync(id, { type: 'channel', common: { name }, native: {} });
@@ -1629,6 +1658,15 @@ class Assistant extends adapter_core_1.Adapter {
1629
1658
  else if (leaf === 'volume' || leaf === 'command') {
1630
1659
  payload = value;
1631
1660
  }
1661
+ else if (leaf === 'url') {
1662
+ this.playMediaUrl(satId, device, objectId, value).catch(e => this.log.warn(`Playing ${String(value)} on ${device} failed: ${e.message}`));
1663
+ return;
1664
+ }
1665
+ else if (leaf === 'transcode') {
1666
+ // A setting of ours, nothing for the device: taken as written
1667
+ this.setStateAsync(`satellites.${satId}.controls.${path}`, { val: !!value, ack: true }).catch(() => { });
1668
+ return;
1669
+ }
1632
1670
  else if (leaf) {
1633
1671
  return; // read-only leaf of a compound entity
1634
1672
  }
@@ -1636,6 +1674,144 @@ class Assistant extends adapter_core_1.Adapter {
1636
1674
  this.log.warn(`Writing ${objectId} on ${device} was rejected — see the warning above.`);
1637
1675
  }
1638
1676
  }
1677
+ /**
1678
+ * A URL written to `controls.<player>.url`: play it on the satellite as music (internet radio, a file on a web
1679
+ * server). Playlists are resolved to their first stream; a codec the device decodes goes there directly, anything
1680
+ * else (AAC, HLS …) through ffmpeg, served by our media server — see `mediaStream.ts`. Empty or "stop" stops.
1681
+ *
1682
+ * Acked with the URL once the device was told: it reports no URL back, only the player state (`.state`).
1683
+ */
1684
+ async playMediaUrl(satId, device, objectId, value) {
1685
+ const esphome = this.esphome;
1686
+ const media = this.media;
1687
+ if (!esphome || !media) {
1688
+ return;
1689
+ }
1690
+ const key = `${satId}.${objectId}`;
1691
+ const stateId = `satellites.${satId}.controls.${objectId}.url`;
1692
+ const seq = (this.mediaPlays.get(key) || 0) + 1;
1693
+ this.mediaPlays.set(key, seq);
1694
+ const raw = typeof value === 'string' ? value.trim() : '';
1695
+ const endStream = () => {
1696
+ const old = this.mediaStreamPaths.get(key);
1697
+ if (old) {
1698
+ this.mediaStreamPaths.delete(key);
1699
+ media.unserve(old);
1700
+ }
1701
+ };
1702
+ if (!raw || raw.toLowerCase() === 'stop') {
1703
+ const sent = esphome.setEntity(device, objectId, 'stop');
1704
+ endStream();
1705
+ if (sent) {
1706
+ await this.setStateAsync(stateId, { val: '', ack: true });
1707
+ }
1708
+ return;
1709
+ }
1710
+ let url;
1711
+ try {
1712
+ url = new URL(raw);
1713
+ }
1714
+ catch {
1715
+ this.log.warn(`Satellite ${device}: "${raw}" is not a URL — nothing played.`);
1716
+ return;
1717
+ }
1718
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
1719
+ this.log.warn(`Satellite ${device}: only http(s) URLs can be played, not ${url.protocol}`);
1720
+ return;
1721
+ }
1722
+ const entities = esphome.entities(device);
1723
+ if (!entities.some(e => e.kind === 'mediaPlayer' && e.objectId === objectId)) {
1724
+ this.log.warn(`Satellite ${device} is not connected or has no media player "${objectId}" — nothing played.`);
1725
+ return;
1726
+ }
1727
+ // The decoders are built into the firmware for all its players together
1728
+ const formats = entities.flatMap(e => e.formats ?? []);
1729
+ const always = (await this.getStateAsync(`satellites.${satId}.controls.${objectId}.transcode`))?.val === true;
1730
+ const [probe, ffmpeg] = await Promise.all([(0, mediaStream_1.probeStream)(url.href), this.ffmpegAvailable()]);
1731
+ if (this.mediaPlays.get(key) !== seq) {
1732
+ return; // a newer URL (or a stop) was written meanwhile
1733
+ }
1734
+ const plan = (0, mediaStream_1.planStream)(probe, (0, mediaStream_1.deviceCodecs)(formats), always, ffmpeg);
1735
+ endStream();
1736
+ let target = plan.url;
1737
+ let how = 'directly';
1738
+ if (plan.mode === 'transcode') {
1739
+ const t = (0, mediaStream_1.transcodeTarget)(formats);
1740
+ const streamPath = `/stream/${(0, node_crypto_1.randomBytes)(12).toString('hex')}.${mediaStream_1.STREAM_EXTENSIONS[t.codec]}`;
1741
+ const absolute = esphome.mediaUrl(device, streamPath);
1742
+ if (!absolute) {
1743
+ this.log.warn(`Satellite ${device} went offline — nothing played.`);
1744
+ return;
1745
+ }
1746
+ media.serveStream(streamPath, mediaStream_1.STREAM_CONTENT_TYPES[t.codec], this.transcodeSource(plan.url, t, plan.kind === 'hls', device));
1747
+ this.mediaStreamPaths.set(key, streamPath);
1748
+ target = absolute;
1749
+ how = `through ffmpeg as ${t.codec.toUpperCase()} ${t.sampleRate} Hz${t.channels === 2 ? ' stereo' : ''}`;
1750
+ }
1751
+ const resolved = plan.url !== url.href ? ` (playlist entry ${plan.url})` : '';
1752
+ this.log.info(`Satellite ${device}: playing ${raw}${resolved} ${how} — ${plan.reason}`);
1753
+ if (!esphome.setEntity(device, objectId, { url: target })) {
1754
+ endStream();
1755
+ return;
1756
+ }
1757
+ await this.setStateAsync(stateId, { val: raw, ack: true });
1758
+ }
1759
+ /**
1760
+ * The source of a transcoded stream: one ffmpeg per listener, reading `url` and writing `target` into the
1761
+ * response for as long as the device listens.
1762
+ */
1763
+ transcodeSource(url, target, hls, device) {
1764
+ return res => {
1765
+ const proc = (0, node_child_process_1.spawn)(this.ffmpegBin(), (0, mediaStream_1.transcodeArgs)(url, target, hls), {
1766
+ stdio: ['ignore', 'pipe', 'pipe'],
1767
+ });
1768
+ let stopped = false;
1769
+ let lastError = '';
1770
+ proc.stdout.pipe(res);
1771
+ proc.stderr.on('data', (d) => {
1772
+ lastError = String(d).trim().split(/\r?\n/).pop() || lastError;
1773
+ this.log.debug(`ffmpeg (${device}): ${String(d).trim()}`);
1774
+ });
1775
+ proc.on('error', e => {
1776
+ this.log.warn(`Stream for ${device}: ffmpeg failed: ${e.message}${FFMPEG_HINT}`);
1777
+ res.destroy();
1778
+ });
1779
+ proc.on('close', code => {
1780
+ if (stopped) {
1781
+ return;
1782
+ }
1783
+ // The source ended (a file played to its end) or failed (station offline, unknown format)
1784
+ if (code) {
1785
+ this.log.warn(`Stream for ${device} ended — ffmpeg exited with code ${code}${lastError ? `: ${lastError}` : ''}`);
1786
+ }
1787
+ else {
1788
+ this.log.debug(`Stream for ${device} ended — the source ended`);
1789
+ }
1790
+ res.end();
1791
+ });
1792
+ return () => {
1793
+ stopped = true;
1794
+ proc.kill();
1795
+ };
1796
+ };
1797
+ }
1798
+ /** Whether ffmpeg starts at all; remembered for a minute per binary. */
1799
+ async ffmpegAvailable() {
1800
+ const bin = this.ffmpegBin();
1801
+ const now = Date.now();
1802
+ if (this.ffmpegCheck?.bin === bin && now - this.ffmpegCheck.at < 60_000) {
1803
+ return this.ffmpegCheck.ok;
1804
+ }
1805
+ let ok = true;
1806
+ try {
1807
+ await (0, node_util_1.promisify)(node_child_process_1.execFile)(bin, ['-version'], { timeout: 5_000 });
1808
+ }
1809
+ catch {
1810
+ ok = false;
1811
+ }
1812
+ this.ffmpegCheck = { bin, ok, at: now };
1813
+ return ok;
1814
+ }
1639
1815
  /**
1640
1816
  * Apply a write to `satellites.<id>.wakeWords`. The state is *not* acked here: the device echoes its
1641
1817
  * new configuration back and {@link updateSatelliteWakeWords} writes the acked value, so what ioBroker
@@ -2480,18 +2656,38 @@ class Assistant extends adapter_core_1.Adapter {
2480
2656
  }
2481
2657
  return;
2482
2658
  }
2483
- // Scripts: start a countdown timer. message = { duration: seconds | "5 min", label?, room? }.
2659
+ // Scripts: start a countdown timer. message = { duration | seconds: seconds | "5 min", label | name?, room?,
2660
+ // satellite?, announce? }. With `satellite` (its id under satellites.*, or the device name) the timer is that
2661
+ // satellite's like one set there by voice: an ESPHome satellite shows and rings it, and the announcement plays
2662
+ // there. `announce` = what to say when it fires, word for word (default: "Timer finished: <name>.").
2484
2663
  if (obj.command === 'setTimer') {
2485
2664
  const m = (obj.message || {});
2486
- const duration = typeof m.duration === 'string'
2487
- ? (0, nlu_1.parseDurationSeconds)(m.duration, true) || 0
2488
- : Math.round(Number(m.duration) || 0);
2665
+ // `seconds` are seconds also as a string; a bare number in a `duration` string stays minutes as before
2666
+ const duration = m.seconds !== undefined
2667
+ ? Math.round(Number(m.seconds) || 0) ||
2668
+ (typeof m.seconds === 'string' ? (0, nlu_1.parseDurationSeconds)(m.seconds, false) || 0 : 0)
2669
+ : typeof m.duration === 'string'
2670
+ ? (0, nlu_1.parseDurationSeconds)(m.duration, true) || 0
2671
+ : Math.round(Number(m.duration) || 0);
2672
+ const satellite = this.timerSatellite(m.satellite);
2489
2673
  let result;
2490
- if (!this.timers || duration <= 0) {
2491
- result = { ok: false, error: this.timers ? 'duration must be > 0 seconds' : 'timers not ready' };
2674
+ if (!this.timers) {
2675
+ result = { ok: false, error: 'timers not ready' };
2676
+ }
2677
+ else if (duration <= 0) {
2678
+ result = { ok: false, error: 'duration must be > 0 seconds' };
2679
+ }
2680
+ else if (satellite === null) {
2681
+ result = { ok: false, error: `unknown satellite "${String(m.satellite)}"` };
2492
2682
  }
2493
2683
  else {
2494
- const info = this.timers.add({ label: m.label || '', room: m.room || '', source: '', duration });
2684
+ const info = this.timers.add({
2685
+ label: String(m.name ?? m.label ?? ''),
2686
+ room: m.room || '',
2687
+ source: satellite,
2688
+ duration,
2689
+ announce: typeof m.announce === 'string' ? m.announce : '',
2690
+ });
2495
2691
  result = { ok: true, id: info.id, fireAt: info.fireAt };
2496
2692
  }
2497
2693
  if (obj.callback) {
@@ -2499,19 +2695,37 @@ class Assistant extends adapter_core_1.Adapter {
2499
2695
  }
2500
2696
  return;
2501
2697
  }
2502
- // Scripts: cancel a timer by id, or all when no id is given. message = { id? }.
2698
+ // Scripts: cancel a timer by id, or all when no id is given. message = { id?, satellite? } — with `satellite`
2699
+ // only that satellite's timers.
2503
2700
  if (obj.command === 'cancelTimer') {
2504
2701
  const m = (obj.message || {});
2505
- const cancelled = m.id ? (this.timers?.cancel(m.id) ? 1 : 0) : (this.timers?.cancelAll() ?? 0);
2702
+ const satellite = this.timerSatellite(m.satellite);
2703
+ let result;
2704
+ if (satellite === null) {
2705
+ result = { ok: false, cancelled: 0, error: `unknown satellite "${String(m.satellite)}"` };
2706
+ }
2707
+ else {
2708
+ const mine = (this.timers?.list() || []).filter(t => !satellite || t.source === satellite);
2709
+ const ids = m.id ? mine.filter(t => t.id === m.id).map(t => t.id) : mine.map(t => t.id);
2710
+ const cancelled = satellite || m.id
2711
+ ? ids.filter(id => this.timers?.cancel(id)).length
2712
+ : (this.timers?.cancelAll() ?? 0);
2713
+ result = { ok: true, cancelled };
2714
+ }
2506
2715
  if (obj.callback) {
2507
- this.sendTo(obj.from, obj.command, { ok: true, cancelled }, obj.callback);
2716
+ this.sendTo(obj.from, obj.command, result, obj.callback);
2508
2717
  }
2509
2718
  return;
2510
2719
  }
2511
- // Scripts: list the running timers.
2720
+ // Scripts: list the running timers. message = { satellite? } — with it only that satellite's.
2512
2721
  if (obj.command === 'listTimers') {
2722
+ const m = (obj.message || {});
2723
+ const satellite = this.timerSatellite(m.satellite);
2724
+ const result = satellite === null
2725
+ ? { timers: [], error: `unknown satellite "${String(m.satellite)}"` }
2726
+ : { timers: (this.timers?.list() || []).filter(t => !satellite || t.source === satellite) };
2513
2727
  if (obj.callback) {
2514
- this.sendTo(obj.from, obj.command, { timers: this.timers?.list() || [] }, obj.callback);
2728
+ this.sendTo(obj.from, obj.command, result, obj.callback);
2515
2729
  }
2516
2730
  return;
2517
2731
  }
@@ -3150,9 +3364,10 @@ class Assistant extends adapter_core_1.Adapter {
3150
3364
  return Promise.resolve('');
3151
3365
  }
3152
3366
  const voice = VOICE_TRANSPORTS.has(origin.transport || '');
3367
+ const satellite = voice ? this.knownSatelliteId(origin.satellite || source, origin.room || '') : '';
3153
3368
  // The journal shows "[picture] What is in this picture?", like the conversation history
3154
3369
  const shown = (0, media_1.historyText)(question, attachments.images.length, !!attachments.video);
3155
- return this.requestOrigin.run({ source, room: origin.room || '', voice, attachments }, () => this.journal
3370
+ return this.requestOrigin.run({ source, room: origin.room || '', voice, satellite, attachments }, () => this.journal
3156
3371
  ? this.journal.run(shown, source, origin, () => {
3157
3372
  this.traceSpeechInput(question, origin);
3158
3373
  this.traceAttachments(attachments);
@@ -3677,7 +3892,11 @@ class Assistant extends adapter_core_1.Adapter {
3677
3892
  // pi-agent asks its questions to confirm a write ("… Say yes or no.") in this language, not in the system
3678
3893
  // language: a satellite listening in Russian does not hear a German "Ja"
3679
3894
  const language = String(this.config.voiceLanguage || this.language || '');
3680
- return await harness.ask({ text, conversation: source, instructions, language, images });
3895
+ // From a satellite: where pi-agent can speak later on its own (a reminder, the end of a background
3896
+ // task) — its `tts` state says it there.
3897
+ const satellite = this.requestOrigin.getStore()?.satellite || '';
3898
+ const tts = satellite ? `${this.namespace}.satellites.${satellite}.tts` : '';
3899
+ return await harness.ask({ text, conversation: source, instructions, language, images, satellite, tts });
3681
3900
  }
3682
3901
  catch (e) {
3683
3902
  if (localFallback) {
@@ -5033,13 +5252,25 @@ class Assistant extends adapter_core_1.Adapter {
5033
5252
  await this.setStateAsync(`${base}.duration`, { val: t.duration, ack: true });
5034
5253
  await this.setStateAsync(`${base}.fireAt`, { val: t.fireAt, ack: true });
5035
5254
  }
5255
+ /**
5256
+ * The `source` a timer of a satellite gets (the device name, like a timer set there by voice), from the satellite
5257
+ * a script named: '' when it named none, null when the name is unknown.
5258
+ */
5259
+ timerSatellite(name) {
5260
+ if (typeof name !== 'string' || !name.trim()) {
5261
+ return '';
5262
+ }
5263
+ const id = this.resolveSatelliteId(name);
5264
+ return id ? this.satDeviceById.get(id) || id : null;
5265
+ }
5036
5266
  /** A timer expired: record it, play the jingle (if any) and speak the announcement on the origin satellite. */
5037
5267
  onTimerFired(t) {
5038
5268
  this.log.info(`Timer fired: "${t.label || '(no label)'}"${t.room ? ` (${t.room})` : ''}`);
5039
5269
  this.setStateAsync('timers.lastFired', { val: t.label || t.id, ack: true }).catch(() => { });
5040
- const announce = this.config.timerAnnounce !== false;
5270
+ // A text a script gave for this timer (a reminder) is said even when timer announcements are off
5271
+ const announce = !!t.announce || this.config.timerAnnounce !== false;
5041
5272
  const target = this.announceTargetForSource(t.source);
5042
- this.fireEffects(this.config.timerSound || '', announce, this.timerAnnounceMessage(t), target);
5273
+ this.fireEffects(this.config.timerSound || '', announce, t.announce || this.timerAnnounceMessage(t), target);
5043
5274
  }
5044
5275
  /** Build the spoken expiry message ("Timer abgelaufen: die Nudeln.") in the interaction language. */
5045
5276
  timerAnnounceMessage(t) {