@nebulahome/integration-sdk 1.5.0 → 1.6.1

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.
@@ -1,4 +1,4 @@
1
- import { n as __toESM, t as __commonJSMin } from "./node-rolldown-runtime.js";
1
+ import { r as __toESM, t as __commonJSMin } from "./node-rolldown-runtime.js";
2
2
  import { z } from "zod";
3
3
  import { createHash } from "node:crypto";
4
4
  import { readFile, readdir, stat } from "node:fs/promises";
@@ -1711,6 +1711,36 @@ z.strictObject({ vendorId: z.string().min(1).max(64) });
1711
1711
  z.object({ items: z.array(integrationAccountViewSchema) });
1712
1712
  z.object({ wasSignedIn: z.boolean() });
1713
1713
  z.object({ vendorId: z.string().min(1).max(64) });
1714
+ /**
1715
+ * How a port is spoken: the baud rate, and the framing - `9600 8N1` unless said. A driver of kind
1716
+ * `serial` gives its defaults in this shape; the owner may change them when adding a device.
1717
+ */
1718
+ const serialLineSchema = z.strictObject({
1719
+ baudRate: z.int().min(50).max(4e6),
1720
+ dataBits: z.union([
1721
+ z.literal(5),
1722
+ z.literal(6),
1723
+ z.literal(7),
1724
+ z.literal(8)
1725
+ ]).default(8),
1726
+ parity: z.enum([
1727
+ "none",
1728
+ "even",
1729
+ "odd",
1730
+ "mark",
1731
+ "space"
1732
+ ]).default("none"),
1733
+ stopBits: z.union([
1734
+ z.literal(1),
1735
+ z.literal(1.5),
1736
+ z.literal(2)
1737
+ ]).default(1),
1738
+ flowControl: z.enum([
1739
+ "none",
1740
+ "rtscts",
1741
+ "xonxoff"
1742
+ ]).default("none")
1743
+ });
1714
1744
  //#endregion
1715
1745
  //#region ../contracts/dist/integrations.js
1716
1746
  /**
@@ -1760,7 +1790,8 @@ const transportIdSchema = z.string().regex(/^(?:[a-z][a-z0-9-]{1,63}\/)?[a-z][a-
1760
1790
  * The wires the interface names on a vendor's card: what a person recognises on the box, whatever
1761
1791
  * transport of the platform's or of the vendor's own carries it. `udp` is a device's own protocol
1762
1792
  * over plain datagrams on the network (Govee's LAN API), `modbus` Modbus TCP, `matter` the
1763
- * house's Matter fabric.
1793
+ * house's Matter fabric, `thread` its Thread network, `serial` a serial port (USB, RS-232/485, or
1794
+ * one on the network), `ir` infrared and 433 MHz codes sent through a blaster.
1764
1795
  */
1765
1796
  const TRANSPORT_WIRES = [
1766
1797
  "http",
@@ -1769,24 +1800,37 @@ const TRANSPORT_WIRES = [
1769
1800
  "zigbee",
1770
1801
  "udp",
1771
1802
  "modbus",
1772
- "matter"
1803
+ "matter",
1804
+ "thread",
1805
+ "serial",
1806
+ "ir"
1773
1807
  ];
1774
1808
  /**
1775
1809
  * How a device can be heard announcing itself: the platform's mDNS browser, a vendor's own
1776
1810
  * listener, the house's Matter fabric - a node commissioned and not yet added - or the house's
1777
1811
  * MQTT broker - a device that announced itself on it, or that the owner mapped - or the house's
1778
- * Modbus client - a device the owner added at an address. Declared here
1779
- * rather than beside the discovered devices, because a vendor lists the ways it can be found and
1780
- * the discovery module already reads this file.
1812
+ * Modbus client - a device the owner added at an address - or the house's serial ports - a port
1813
+ * an integration's rule recognises - or its Bluetooth - a device a scanner hears - or its Infrared
1814
+ * broker - a remote the owner made. Declared here rather than beside the discovered devices,
1815
+ * because a vendor lists the ways it can be found and the discovery module already reads this file.
1781
1816
  */
1782
1817
  const DISCOVERY_SOURCES = [
1783
1818
  "mdns",
1784
1819
  "module",
1785
1820
  "matter",
1786
1821
  "mqtt",
1787
- "modbus"
1822
+ "modbus",
1823
+ "serial",
1824
+ "bluetooth",
1825
+ "infrared"
1788
1826
  ];
1789
1827
  /**
1828
+ * What a vendor's devices can be for the platform's brokers, besides devices of their own: an IR
1829
+ * blaster the Infrared broker sends remotes' codes through, a Bluetooth proxy that listens for the
1830
+ * house. Worked out from the profiles that declare them, and shown as chips on the vendor's page.
1831
+ */
1832
+ const VENDOR_ROLES = ["ir-blaster", "bluetooth-proxy"];
1833
+ /**
1790
1834
  * Who adds a device discovery found: the owner, from its card - the ordinary way - or the
1791
1835
  * platform, as soon as it is heard. Automatic for a vendor whose devices were already chosen
1792
1836
  * somewhere the owner set up: a Zigbee device paired with Zigbee2MQTT is one the owner meant to
@@ -1918,6 +1962,12 @@ const integrationKindSchema$1 = z.object({
1918
1962
  /** What setting up an instance asks. */
1919
1963
  setup: integrationSetupSchema,
1920
1964
  /**
1965
+ * A `serial` driver's line, which the add dialog preselects: such a device is added on a port
1966
+ * of the Serial broker, `transportConfig.serial` holding the port and line. Absent for every
1967
+ * other driver.
1968
+ */
1969
+ serial: serialLineSchema.optional(),
1970
+ /**
1921
1971
  * The port the device serves its own web page on, when it has one.
1922
1972
  *
1923
1973
  * Only a driver that says so gets a link: a device that answers on an API port with no page
@@ -2016,6 +2066,8 @@ const integrationVendorSchema = z.object({
2016
2066
  discoverySources: z.array(z.enum(DISCOVERY_SOURCES)).default([]),
2017
2067
  /** Who adds what discovery finds: the owner, or the platform on its own. */
2018
2068
  adoption: z.enum(DEVICE_ADOPTIONS).default("owner"),
2069
+ /** What its devices can be for the brokers, each once; absent or empty for most vendors. */
2070
+ roles: z.array(z.enum(VENDOR_ROLES)).max(VENDOR_ROLES.length).optional(),
2019
2071
  /**
2020
2072
  * What makes two of its devices one device reached two ways - the same MAC, the same serial -
2021
2073
  * as its manifest's `twins` says. Empty for a vendor that offers no links.
@@ -4546,26 +4598,36 @@ z.object({ isDismissed: z.boolean() });
4546
4598
  //#endregion
4547
4599
  //#region ../contracts/dist/brokers.js
4548
4600
  /**
4549
- * The platform's brokers, as the Brokers page lists them: the house's Matter fabric, its MQTT
4550
- * broker and its Modbus client, all run by the hub.
4601
+ * The platform's brokers, as the Brokers page lists them: the house's Matter fabric, its Thread
4602
+ * network, its Bluetooth, its MQTT broker, its Modbus client, its serial ports and its infrared
4603
+ * remotes, all run by the hub.
4551
4604
  *
4552
4605
  * A broker reaches every device of one protocol, whoever makes it. Each has a page of its own -
4553
- * `matter.ts`, `mqtt.ts` and `modbus.ts` say what it holds - and a card here, which says how it stands and how
4554
- * many devices the house has through it.
4606
+ * `matter.ts`, `thread.ts`, `bluetooth.ts`, `mqtt.ts`, `modbus.ts`, `serial.ts` and `infrared.ts`
4607
+ * say what it holds - and a card here, which says how it stands and how many devices the house
4608
+ * has through it.
4609
+ */
4610
+ /**
4611
+ * How a broker stands: coming up, running, stopped by a failure, turned off, or without the
4612
+ * hardware it needs - no adapter, no radio, no port, no blaster - which is not a failure.
4555
4613
  */
4556
- /** How a broker stands: coming up, running, stopped by a failure, or turned off. */
4557
4614
  const BROKER_STATES = [
4558
4615
  "starting",
4559
4616
  "running",
4560
4617
  "failed",
4561
- "disabled"
4618
+ "disabled",
4619
+ "unavailable"
4562
4620
  ];
4563
4621
  /** One broker, for its card. */
4564
4622
  const brokerSummarySchema = z.object({
4565
4623
  id: z.enum([
4566
4624
  "matter",
4625
+ "thread",
4626
+ "bluetooth",
4567
4627
  "mqtt",
4568
- "modbus"
4628
+ "modbus",
4629
+ "serial",
4630
+ "infrared"
4569
4631
  ]),
4570
4632
  state: z.enum(BROKER_STATES),
4571
4633
  /**
@@ -4579,6 +4641,45 @@ const brokerSummarySchema = z.object({
4579
4641
  })
4580
4642
  });
4581
4643
  z.object({ items: z.array(brokerSummarySchema) });
4644
+ /**
4645
+ * How a broker's job goes: one that changes hardware - a proxy installed, Thread set up - and
4646
+ * takes longer than one request may wait, so the page reads it until it ends.
4647
+ */
4648
+ const BROKER_JOB_STATES = [
4649
+ "running",
4650
+ "succeeded",
4651
+ "failed",
4652
+ "cancelled"
4653
+ ];
4654
+ /** How one step of a job stands. */
4655
+ const BROKER_STEP_STATES = [
4656
+ "pending",
4657
+ "running",
4658
+ "done",
4659
+ "failed"
4660
+ ];
4661
+ /**
4662
+ * A job's shape, with its own steps: each in order with how it stands, and the line it ended on.
4663
+ *
4664
+ * @param steps - The job's step ids, in order.
4665
+ * @returns The job's schema.
4666
+ */
4667
+ function brokerJobSchemaOf(steps) {
4668
+ return z.object({
4669
+ id: z.string().min(1).max(64),
4670
+ state: z.enum(BROKER_JOB_STATES),
4671
+ startedAt: timestampSchema,
4672
+ finishedAt: timestampSchema.nullable(),
4673
+ /** Why it failed, in a sentence; null unless it did. */
4674
+ error: z.string().max(2048).nullable(),
4675
+ steps: z.array(z.object({
4676
+ id: z.enum(steps),
4677
+ state: z.enum(BROKER_STEP_STATES),
4678
+ /** What the step found, or the line it failed on - the integration's, the log's. */
4679
+ message: z.string().max(2048).nullable()
4680
+ })).max(steps.length)
4681
+ });
4682
+ }
4582
4683
  //#endregion
4583
4684
  //#region ../contracts/dist/matter.js
4584
4685
  /**
@@ -4586,8 +4687,9 @@ z.object({ items: z.array(brokerSummarySchema) });
4586
4687
  *
4587
4688
  * Matter is one of the platform's brokers: the hub holds the controller - one fabric for the
4588
4689
  * house - and every Matter device is reached through it. A node is commissioned once, with the
4589
- * code another app (the maker's, Google Home, Apple Home, Alexa) shares, and is then a device of
4590
- * the house: of the integration whose `discovery.json` recognises it, or of the platform's own
4690
+ * code another app (the maker's, Google Home, Apple Home, Alexa) shares - or, a new device, over
4691
+ * the server's Bluetooth onto Wi-Fi or the house's Thread network - and is then a device of the
4692
+ * house: of the integration whose `discovery.json` recognises it, or of the platform's own
4591
4693
  * `matter` vendor when none does. A bridge is one node with a device per bridged endpoint.
4592
4694
  *
4593
4695
  * The identity a node is recognised by is {@link matterIdentitySchema}: what its Basic Information
@@ -4642,9 +4744,27 @@ const matterIdentitySchema = z.object({
4642
4744
  isBridged: z.boolean()
4643
4745
  });
4644
4746
  /**
4747
+ * How a device is commissioned: `shared` by the code another app shares, over the network it is
4748
+ * already on; `new`, one never set up or reset, over the server's Bluetooth, then given Wi-Fi or
4749
+ * the house's Thread network.
4750
+ */
4751
+ const MATTER_COMMISSION_MODES = ["shared", "new"];
4752
+ /** The network a new device joins, as the Found step's chip says it. */
4753
+ const MATTER_DEVICE_NETWORKS = ["wifi", "thread"];
4754
+ /**
4755
+ * What a job that needs an answer asks: whether to add a device with a test certificate; the
4756
+ * Wi-Fi a new device should join, when none is saved; or for Thread to be set up, which is
4757
+ * answered by cancelling.
4758
+ */
4759
+ const MATTER_QUESTIONS = [
4760
+ "test-certificate",
4761
+ "needs-wifi",
4762
+ "needs-thread"
4763
+ ];
4764
+ /**
4645
4765
  * How a commissioning goes. `needs-answer` is a job that stopped before the node joined and waits
4646
- * for the owner: a device whose certificate is a test one, added only when they say so.
4647
- * `cancelled` is a job the owner stopped before the device joined.
4766
+ * for the owner: a device whose certificate is a test one, added only when they say so, or a new
4767
+ * device that needs a network. `cancelled` is a job the owner stopped before the device joined.
4648
4768
  */
4649
4769
  const MATTER_COMMISSIONING_STATES = [
4650
4770
  "running",
@@ -4666,16 +4786,23 @@ const MATTER_COMMISSIONING_STEPS = [
4666
4786
  "reading",
4667
4787
  "in-house"
4668
4788
  ];
4789
+ /** How one step stands. `warning` is the certificate step of a device with a test certificate. */
4790
+ const MATTER_STEP_STATES = [
4791
+ "pending",
4792
+ "running",
4793
+ "done",
4794
+ "warning",
4795
+ "failed"
4796
+ ];
4669
4797
  /** One step of a commissioning, and what it found. */
4670
4798
  const matterCommissioningStepSchema = z.object({
4671
- id: z.enum(MATTER_COMMISSIONING_STEPS),
4672
- state: z.enum([
4673
- "pending",
4674
- "running",
4675
- "done",
4676
- "warning",
4677
- "failed"
4799
+ id: z.enum([
4800
+ ...MATTER_COMMISSIONING_STEPS,
4801
+ "looking",
4802
+ "network",
4803
+ "added"
4678
4804
  ]),
4805
+ state: z.enum(MATTER_STEP_STATES),
4679
4806
  /**
4680
4807
  * What the step found, as facts the page words itself: the discriminator a code carries, the
4681
4808
  * address and product a device answered with, the vendor and product a test certificate names.
@@ -4686,7 +4813,9 @@ const matterCommissioningStepSchema = z.object({
4686
4813
  address: z.string().max(256).nullable().default(null),
4687
4814
  product: z.string().max(256).nullable().default(null),
4688
4815
  vendorId: z.int().min(0).max(65535).nullable().default(null),
4689
- productId: z.int().min(0).max(65535).nullable().default(null)
4816
+ productId: z.int().min(0).max(65535).nullable().default(null),
4817
+ /** The network a new device found over Bluetooth joins; absent until it is found. */
4818
+ network: z.enum(MATTER_DEVICE_NETWORKS).optional()
4690
4819
  }).default({
4691
4820
  discriminator: null,
4692
4821
  isQrCode: false,
@@ -4704,6 +4833,10 @@ const matterCommissioningStepSchema = z.object({
4704
4833
  const matterCommissioningSchema = z.object({
4705
4834
  id: z.string().min(1).max(64),
4706
4835
  state: z.enum(MATTER_COMMISSIONING_STATES),
4836
+ /** By a shared code, or a new device over Bluetooth; shared for a hub that does not say. */
4837
+ mode: z.enum(MATTER_COMMISSION_MODES).optional(),
4838
+ /** What it asks while it needs an answer; absent otherwise, and from a hub that does not say. */
4839
+ question: z.enum(MATTER_QUESTIONS).optional(),
4707
4840
  startedAt: timestampSchema,
4708
4841
  finishedAt: timestampSchema.nullable(),
4709
4842
  /** The node it made; null until it is on the fabric. */
@@ -4739,7 +4872,21 @@ z.object({
4739
4872
  /** The UDP port the controller listens on. */
4740
4873
  port: z.int().min(1).max(65535).default(MATTER_PORT),
4741
4874
  /** When this hub made the fabric; null for one made before the hub kept the date. */
4742
- fabricCreatedAt: timestampSchema.nullable().default(null)
4875
+ fabricCreatedAt: timestampSchema.nullable().default(null),
4876
+ /**
4877
+ * Whether a new device can be paired over the server's Bluetooth, and why not: no adapter, the
4878
+ * adapter turned off, only proxies (which cannot connect), or Docker Desktop. Unavailable for a
4879
+ * hub that does not say.
4880
+ */
4881
+ ble: z.object({
4882
+ isAvailable: z.boolean(),
4883
+ reason: z.enum([
4884
+ "no-adapter",
4885
+ "adapter-off",
4886
+ "proxies-only",
4887
+ "desktop"
4888
+ ]).nullable()
4889
+ }).optional()
4743
4890
  });
4744
4891
  /** One device a node is in the house: the node itself, or one endpoint behind a bridge. */
4745
4892
  const matterNodeDeviceSchema = z.object({
@@ -4841,7 +4988,10 @@ z.object({
4841
4988
  nodes: z.array(matterNetworkNodeSchema).max(2048),
4842
4989
  links: z.array(matterNetworkLinkSchema).max(8192)
4843
4990
  });
4844
- z.object({ code: matterPairingCodeSchema });
4991
+ z.object({
4992
+ code: matterPairingCodeSchema,
4993
+ mode: z.enum(MATTER_COMMISSION_MODES).default("shared")
4994
+ });
4845
4995
  z.object({ nodeId: matterNodeIdSchema });
4846
4996
  z.object({ isRemoved: z.boolean() });
4847
4997
  z.object({
@@ -4849,12 +4999,670 @@ z.object({
4849
4999
  endpoint: z.int().min(0).max(65535).nullable()
4850
5000
  });
4851
5001
  z.object({ isRestored: z.boolean() });
5002
+ /** A Wi-Fi network's name, 1 to 32 bytes. */
5003
+ const wifiSsidSchema = z.string().min(1).max(32);
5004
+ /** A Wi-Fi password: WPA's 8 to 63 characters, or 64 hex digits. */
5005
+ const wifiPasswordSchema = z.string().min(8).max(64);
4852
5006
  z.object({
4853
5007
  jobId: z.string().min(1).max(64),
4854
- isAccepted: z.boolean()
5008
+ isAccepted: z.boolean(),
5009
+ wifi: z.object({
5010
+ ssid: wifiSsidSchema,
5011
+ password: wifiPasswordSchema,
5012
+ isSaved: z.boolean().default(false)
5013
+ }).optional()
4855
5014
  });
4856
5015
  z.object({ jobId: z.string().min(1).max(64) });
4857
5016
  z.object({});
5017
+ z.object({
5018
+ wifi: z.object({
5019
+ ssid: z.string().max(32).nullable(),
5020
+ hasPassword: z.boolean()
5021
+ }),
5022
+ /** The house's Thread network, read-only here; null while Thread is not set up for Matter. */
5023
+ thread: z.object({
5024
+ networkName: z.string().max(16),
5025
+ channel: z.int().min(0).max(255)
5026
+ }).nullable()
5027
+ });
5028
+ z.object({ wifi: z.object({
5029
+ ssid: wifiSsidSchema,
5030
+ password: wifiPasswordSchema.optional()
5031
+ }).nullable() });
5032
+ //#endregion
5033
+ //#region ../contracts/dist/bluetooth.js
5034
+ /**
5035
+ * The house's Bluetooth broker, as the hub runs it and the Brokers page shows it.
5036
+ *
5037
+ * Bluetooth Low Energy is one of the platform's brokers: the server's own adapter, through the
5038
+ * host's BlueZ over D-Bus, and **proxies** - devices of an integration that listen for the house
5039
+ * and forward what they hear (in v1, Shelly Gen2+ devices running a small script). Proxies only
5040
+ * listen; only the local adapter connects to a device.
5041
+ *
5042
+ * Every advertisement heard is kept per address and per scanner for a while: who hears it best,
5043
+ * how loud, how often. A device of the house is reached over the platform transport `bluetooth`,
5044
+ * its device key its address. An integration recognises its own devices in what is heard through
5045
+ * its `discovery.json` `bluetooth` rules; the platform recognises BTHome sensors itself, through
5046
+ * its own `bluetooth` vendor.
5047
+ *
5048
+ * A device plays a role for the broker through the connection its instance already has: the hub
5049
+ * sends the reserved {@link BLUETOOTH_PROXY_OPS} to a device whose profile declares
5050
+ * {@link bluetoothProxyProfileSchema}, and the proxy reports {@link bluetoothAdvertSchema}s back
5051
+ * through the SDK's scanner.
5052
+ */
5053
+ /** The platform transport every Bluetooth device is reached through; its device key the address. */
5054
+ const BLUETOOTH_TRANSPORT = "bluetooth";
5055
+ /** How a scanner listens: passive only hears, active also asks devices for more. */
5056
+ const BLUETOOTH_SCAN_MODES = ["passive", "active"];
5057
+ /** How an address is: the maker's (`public`), or one the device made up (`random`). */
5058
+ const BLUETOOTH_ADDRESS_TYPES = ["public", "random"];
5059
+ /** What a scanner is: the server's own adapter, or a device of an integration that listens. */
5060
+ const BLUETOOTH_SCANNER_KINDS = ["adapter", "proxy"];
5061
+ /** How a device of the house is reached: heard, or connected to (the local adapter only). */
5062
+ const BLUETOOTH_LINKS = ["listening", "connected"];
5063
+ /**
5064
+ * Why the local adapter cannot be used: Docker Desktop, no `/run/dbus` in the container, BlueZ
5065
+ * not running on the host, no adapter, the adapter powered off, or the owner turned it off.
5066
+ */
5067
+ const BLUETOOTH_ADAPTER_PROBLEMS = [
5068
+ "desktop",
5069
+ "no-dbus",
5070
+ "no-bluez",
5071
+ "no-adapter",
5072
+ "powered-off",
5073
+ "disabled"
5074
+ ];
5075
+ /** Why a device cannot be a proxy: Gen1, battery powered, firmware too old, no Bluetooth. */
5076
+ const BLUETOOTH_PROXY_INELIGIBLE_REASONS = [
5077
+ "gen1",
5078
+ "battery",
5079
+ "firmware",
5080
+ "no-bluetooth"
5081
+ ];
5082
+ /** What a proxy forwards: the house's devices and what a rule recognises, or everything. */
5083
+ const BLUETOOTH_FORWARDING = ["house", "everything"];
5084
+ /** A Bluetooth address, `AA:BB:CC:DD:EE:FF`: read in either case, kept upper. */
5085
+ const bluetoothAddressSchema = z.string().regex(/^[0-9A-Fa-f]{2}(?::[0-9A-Fa-f]{2}){5}$/u, "A Bluetooth address, AA:BB:CC:DD:EE:FF").transform((address) => address.toUpperCase());
5086
+ /** A service UUID: 16-bit (`fcd2`), 32-bit, or 128-bit; read in either case, kept lower. */
5087
+ const bluetoothUuidSchema = z.string().regex(/^(?:[0-9A-Fa-f]{4}|[0-9A-Fa-f]{8}|[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})$/u, "A Bluetooth UUID").transform((uuid) => uuid.toLowerCase());
5088
+ /** Bytes an advertisement carries: hex, two digits a byte, kept lower; 255 bytes at most. */
5089
+ const bluetoothHexSchema = z.string().max(510).regex(/^(?:[0-9A-Fa-f]{2})*$/u, "Hex, two digits a byte").transform((hex) => hex.toLowerCase());
5090
+ /** A signal strength, in dBm. */
5091
+ const rssiSchema = z.int().min(-127).max(20);
5092
+ /**
5093
+ * One advertisement a scanner heard: what a proxy reports through `sdk.bluetooth.scanner`, and
5094
+ * what the local adapter's D-Bus signals are turned into.
5095
+ */
5096
+ const bluetoothAdvertSchema = z.object({
5097
+ address: bluetoothAddressSchema,
5098
+ addressType: z.enum(BLUETOOTH_ADDRESS_TYPES),
5099
+ rssi: rssiSchema,
5100
+ name: z.string().max(248).optional(),
5101
+ /** By company id, in decimal (`76` is Apple): the bytes after it. */
5102
+ manufacturerData: z.record(z.string().regex(/^\d{1,5}$/u, "A company id"), bluetoothHexSchema).optional(),
5103
+ /** By service UUID: the bytes. */
5104
+ serviceData: z.record(bluetoothUuidSchema, bluetoothHexSchema).optional(),
5105
+ serviceUuids: z.array(bluetoothUuidSchema).max(64).optional(),
5106
+ at: timestampSchema
5107
+ });
5108
+ z.array(bluetoothAdvertSchema).max(1024);
5109
+ /**
5110
+ * What a device that can be a proxy declares in its profile: the scan modes it can run.
5111
+ */
5112
+ const bluetoothProxyProfileSchema = z.strictObject({ modes: z.array(z.enum(BLUETOOTH_SCAN_MODES)).min(1).max(BLUETOOTH_SCAN_MODES.length).refine((modes) => new Set(modes).size === modes.length, "Each mode once") });
5113
+ z.object({});
5114
+ z.object({ mode: z.enum(BLUETOOTH_SCAN_MODES) });
5115
+ z.object({ addresses: z.union([z.literal("all"), z.array(bluetoothAddressSchema).max(4096)]) });
5116
+ /** What `bluetooth-proxy.state` answers: a device that can be a proxy, and whether it is one. */
5117
+ const bluetoothProxyStateSchema = z.discriminatedUnion("eligible", [z.object({
5118
+ eligible: z.literal(true),
5119
+ installed: z.boolean(),
5120
+ /** The script's version on the device; absent when none is installed. */
5121
+ version: z.string().max(64).optional(),
5122
+ /** The script's version the integration ships now. */
5123
+ shipped: z.string().max(64),
5124
+ running: z.boolean()
5125
+ }), z.object({
5126
+ eligible: z.literal(false),
5127
+ reason: z.enum(BLUETOOTH_PROXY_INELIGIBLE_REASONS),
5128
+ /** For `firmware`: whether an update the device offers would make it eligible. */
5129
+ update: z.boolean().optional()
5130
+ })]);
5131
+ /** One characteristic of a device ({@link BLUETOOTH_GATT_OPS}, in `reserved-ops.ts`). */
5132
+ const gattTargetShape = {
5133
+ service: bluetoothUuidSchema,
5134
+ characteristic: bluetoothUuidSchema
5135
+ };
5136
+ z.object(gattTargetShape);
5137
+ z.object({
5138
+ ...gattTargetShape,
5139
+ data: bluetoothHexSchema,
5140
+ withResponse: z.boolean().default(true)
5141
+ });
5142
+ z.object({
5143
+ ...gattTargetShape,
5144
+ isOn: z.boolean()
5145
+ });
5146
+ z.object({ data: bluetoothHexSchema });
5147
+ /** The owner's settings. */
5148
+ const bluetoothSettingsSchema = z.object({
5149
+ /** Whether the server's own adapter is used; off, Matter's New device is off too. */
5150
+ localEnabled: z.boolean().default(true),
5151
+ /** Which adapter, `hci0`; null for the first BlueZ has. */
5152
+ adapterId: z.string().regex(/^hci\d{1,3}$/u).nullable().default(null),
5153
+ localScanMode: z.enum(BLUETOOTH_SCAN_MODES).default("active"),
5154
+ /** How a new proxy scans. */
5155
+ proxyScanMode: z.enum(BLUETOOTH_SCAN_MODES).default("passive"),
5156
+ /** Addresses not heard for this long are dropped. */
5157
+ forgetAfterDays: z.int().min(1).max(365).default(7),
5158
+ /** Whether Nearby lists private addresses: phones and watches that change theirs. */
5159
+ showPrivate: z.boolean().default(false)
5160
+ });
5161
+ /** An adapter BlueZ has. */
5162
+ const bluetoothAdapterSchema = z.object({
5163
+ /** `hci0`. */
5164
+ id: z.string().max(16),
5165
+ /** What BlueZ calls it, or the chip: `Intel AX211`; null when it says nothing. */
5166
+ name: z.string().max(128).nullable(),
5167
+ address: z.string().max(17).nullable(),
5168
+ isPowered: z.boolean()
5169
+ });
5170
+ /** How many connections an adapter holds, of how many it can. */
5171
+ const connectionsSchema = z.object({
5172
+ used: z.int().min(0),
5173
+ max: z.int().min(0)
5174
+ });
5175
+ z.object({
5176
+ state: z.enum(BROKER_STATES),
5177
+ error: z.string().max(2048).nullable(),
5178
+ /** The adapter in use; null when there is none to use, and `adapterProblem` says why. */
5179
+ adapter: bluetoothAdapterSchema.extend({
5180
+ scanMode: z.enum(BLUETOOTH_SCAN_MODES),
5181
+ /** Whether the host's BlueZ can scan passively; otherwise Passive is offered disabled. */
5182
+ canPassive: z.boolean(),
5183
+ connections: connectionsSchema
5184
+ }).nullable(),
5185
+ /** Every adapter BlueZ has, for the Settings choice. */
5186
+ adapters: z.array(bluetoothAdapterSchema).max(16),
5187
+ adapterProblem: z.enum(BLUETOOTH_ADAPTER_PROBLEMS).nullable(),
5188
+ proxies: z.object({
5189
+ total: z.int().min(0),
5190
+ reporting: z.int().min(0)
5191
+ }),
5192
+ /** What was heard in the last five minutes, and how much. */
5193
+ heard: z.object({
5194
+ devices: z.int().min(0),
5195
+ advertsPerMinute: z.int().min(0)
5196
+ }),
5197
+ /** Whether Matter is pairing a device over the local adapter now. */
5198
+ isMatterPairing: z.boolean(),
5199
+ /** Whether a Nearby lease is held, and proxies forward everything they hear. */
5200
+ isForwardingAll: z.boolean(),
5201
+ settings: bluetoothSettingsSchema,
5202
+ /** The house's Bluetooth devices, and how many were heard lately: the Brokers card's count. */
5203
+ devices: z.object({
5204
+ total: z.int().min(0),
5205
+ online: z.int().min(0)
5206
+ })
5207
+ });
5208
+ /** One scanner hearing an address. */
5209
+ const bluetoothHearingSchema = z.object({
5210
+ /** The scanner: `adapter:hci0`, or the proxy's device id. */
5211
+ scannerId: z.string().min(1).max(64),
5212
+ scannerName: z.string().max(256),
5213
+ rssi: rssiSchema,
5214
+ at: timestampSchema
5215
+ });
5216
+ /** An integration that recognises what an address advertises, and what it would add it as. */
5217
+ const bluetoothClaimSchema = z.object({
5218
+ vendorId: z.string().regex(VENDOR_ID_PATTERN$1),
5219
+ vendorName: z.string().max(128),
5220
+ kind: integrationKindIdSchema.nullable(),
5221
+ model: z.string().max(128).nullable()
5222
+ });
5223
+ /** A Bluetooth device of the house. */
5224
+ const bluetoothDeviceSchema = z.object({
5225
+ deviceId: integrationIdSchema,
5226
+ name: z.string().max(256),
5227
+ vendorId: z.string().regex(VENDOR_ID_PATTERN$1),
5228
+ areaId: z.uuid().nullable(),
5229
+ address: bluetoothAddressSchema,
5230
+ /** The best signal now; null when nobody heard it lately. */
5231
+ rssi: rssiSchema.nullable(),
5232
+ /** Every scanner hearing it, the best first. */
5233
+ heardBy: z.array(bluetoothHearingSchema).max(64),
5234
+ lastHeardAt: timestampSchema.nullable(),
5235
+ link: z.enum(BLUETOOTH_LINKS),
5236
+ addedAt: timestampSchema.nullable()
5237
+ });
5238
+ z.object({ items: z.array(bluetoothDeviceSchema) });
5239
+ /** One point of the map. */
5240
+ const bluetoothMapNodeSchema = z.object({
5241
+ /** `server`, `adapter:hci0`, the proxy's device id, or the device's address. */
5242
+ id: z.string().min(1).max(64),
5243
+ kind: z.enum([
5244
+ "server",
5245
+ "adapter",
5246
+ "proxy",
5247
+ "device"
5248
+ ]),
5249
+ name: z.string().max(256),
5250
+ deviceId: integrationIdSchema.nullable(),
5251
+ address: z.string().max(17).nullable(),
5252
+ isOnline: z.boolean().nullable()
5253
+ });
5254
+ /** One link of the map: a proxy to the server, or a device to the scanner that hears it best. */
5255
+ const bluetoothMapLinkSchema = z.object({
5256
+ source: z.string().min(1).max(64),
5257
+ target: z.string().min(1).max(64),
5258
+ network: z.enum(["wifi", "ble"]),
5259
+ strength: z.enum(MATTER_LINK_STRENGTHS),
5260
+ rssi: z.int().nullable()
5261
+ });
5262
+ z.object({
5263
+ collectedAt: timestampSchema.nullable(),
5264
+ nodes: z.array(bluetoothMapNodeSchema).max(2048),
5265
+ links: z.array(bluetoothMapLinkSchema).max(8192)
5266
+ });
5267
+ /** One address heard, the house's or not. */
5268
+ const bluetoothNearbySchema = z.object({
5269
+ address: bluetoothAddressSchema,
5270
+ addressType: z.enum(BLUETOOTH_ADDRESS_TYPES),
5271
+ /** A random resolvable or non-resolvable address: one a phone or a watch changes. */
5272
+ isPrivate: z.boolean(),
5273
+ name: z.string().max(248).nullable(),
5274
+ /** The maker, from the company id of its manufacturer data: `Xiaomi`, `Apple`. */
5275
+ maker: z.string().max(128).nullable(),
5276
+ companyId: z.int().min(0).max(65535).nullable(),
5277
+ serviceUuids: z.array(bluetoothUuidSchema).max(64),
5278
+ /** The best signal, and every scanner hearing it, the best first. */
5279
+ rssi: rssiSchema,
5280
+ heardBy: z.array(bluetoothHearingSchema).max(64),
5281
+ lastHeardAt: timestampSchema,
5282
+ claims: z.array(bluetoothClaimSchema).max(16),
5283
+ /** The device of the house it is; null for one the house does not have. */
5284
+ deviceId: integrationIdSchema.nullable(),
5285
+ isIgnored: z.boolean(),
5286
+ /** The raw advertisement, for integration authors. */
5287
+ manufacturerData: z.record(z.string(), bluetoothHexSchema),
5288
+ serviceData: z.record(z.string(), bluetoothHexSchema)
5289
+ });
5290
+ z.object({ items: z.array(bluetoothNearbySchema).max(4096) });
5291
+ z.object({
5292
+ leaseId: z.uuid(),
5293
+ isHeld: z.boolean()
5294
+ });
5295
+ z.object({
5296
+ isForwardingAll: z.boolean(),
5297
+ expiresAt: timestampSchema.nullable()
5298
+ });
5299
+ /** A device an integration recognises that the house does not have. */
5300
+ const bluetoothFoundSchema = z.object({
5301
+ address: bluetoothAddressSchema,
5302
+ name: z.string().max(248).nullable(),
5303
+ rssi: rssiSchema,
5304
+ /** The scanner that hears it best. */
5305
+ scannerName: z.string().max(256),
5306
+ claims: z.array(bluetoothClaimSchema).min(1).max(16),
5307
+ foundAt: timestampSchema
5308
+ });
5309
+ /** An address the owner ignores: never offered again. */
5310
+ const bluetoothIgnoredSchema = z.object({
5311
+ address: bluetoothAddressSchema,
5312
+ name: z.string().max(248).nullable(),
5313
+ at: timestampSchema
5314
+ });
5315
+ z.object({
5316
+ items: z.array(bluetoothFoundSchema),
5317
+ ignored: z.array(bluetoothIgnoredSchema)
5318
+ });
5319
+ z.object({
5320
+ address: bluetoothAddressSchema,
5321
+ isIgnored: z.boolean()
5322
+ });
5323
+ z.object({ count: z.int().min(0) });
5324
+ z.object({
5325
+ address: bluetoothAddressSchema,
5326
+ kind: integrationKindIdSchema.optional()
5327
+ });
5328
+ z.object({ deviceId: integrationIdSchema });
5329
+ /** A scanner: the local adapter, or a proxy. */
5330
+ const bluetoothScannerSchema = z.object({
5331
+ /** `adapter:hci0`, or the proxy's device id. */
5332
+ id: z.string().min(1).max(64),
5333
+ kind: z.enum(BLUETOOTH_SCANNER_KINDS),
5334
+ name: z.string().max(256),
5335
+ /** The proxy's device and integration; null for the adapter. */
5336
+ deviceId: integrationIdSchema.nullable(),
5337
+ vendorId: z.string().regex(VENDOR_ID_PATTERN$1).nullable(),
5338
+ areaId: z.uuid().nullable(),
5339
+ /** The adapter's MAC address and chip; null for a proxy. */
5340
+ address: z.string().max(17).nullable(),
5341
+ chip: z.string().max(128).nullable(),
5342
+ isOnline: z.boolean(),
5343
+ isEnabled: z.boolean(),
5344
+ scanMode: z.enum(BLUETOOTH_SCAN_MODES),
5345
+ canPassive: z.boolean(),
5346
+ advertsPerMinute: z.int().min(0),
5347
+ /** The adapter's connections; null for a proxy, which cannot connect. */
5348
+ connections: connectionsSchema.nullable(),
5349
+ /** What a proxy forwards; null for the adapter. */
5350
+ forwarding: z.enum(BLUETOOTH_FORWARDING).nullable(),
5351
+ lastReportAt: timestampSchema.nullable(),
5352
+ /** A proxy's script: the version on it, the one shipped, and whether it is behind. */
5353
+ script: z.object({
5354
+ version: z.string().max(64).nullable(),
5355
+ shipped: z.string().max(64).nullable(),
5356
+ isOutdated: z.boolean()
5357
+ }).nullable(),
5358
+ /** The addresses it hears best of every scanner: where a new proxy would help. */
5359
+ hearsBest: z.array(z.object({
5360
+ address: bluetoothAddressSchema,
5361
+ name: z.string().max(256).nullable(),
5362
+ deviceId: integrationIdSchema.nullable(),
5363
+ rssi: rssiSchema
5364
+ })).max(4096)
5365
+ });
5366
+ z.object({ items: z.array(bluetoothScannerSchema) });
5367
+ z.object({ deviceId: integrationIdSchema.optional() });
5368
+ /** A device whose integration can make it a proxy, and whether it can be one. */
5369
+ const bluetoothProxyCandidateSchema = z.object({
5370
+ deviceId: integrationIdSchema,
5371
+ name: z.string().max(256),
5372
+ vendorId: z.string().regex(VENDOR_ID_PATTERN$1),
5373
+ areaId: z.uuid().nullable(),
5374
+ isOnline: z.boolean(),
5375
+ /** Its Wi-Fi signal, in dBm; null when it does not say. */
5376
+ wifiRssi: z.int().min(-127).max(20).nullable(),
5377
+ /** Whether it can be one, and is; null while it does not answer. */
5378
+ proxy: bluetoothProxyStateSchema.nullable(),
5379
+ /** The scan modes its profile declares. */
5380
+ modes: z.array(z.enum(BLUETOOTH_SCAN_MODES)).max(BLUETOOTH_SCAN_MODES.length)
5381
+ });
5382
+ z.object({ items: z.array(bluetoothProxyCandidateSchema) });
5383
+ z.object({
5384
+ deviceId: integrationIdSchema,
5385
+ mode: z.enum(BLUETOOTH_SCAN_MODES)
5386
+ });
5387
+ z.object({ deviceId: integrationIdSchema });
5388
+ brokerJobSchemaOf([
5389
+ "installing",
5390
+ "started",
5391
+ "first-report"
5392
+ ]).extend({
5393
+ deviceId: integrationIdSchema,
5394
+ mode: z.enum(BLUETOOTH_SCAN_MODES)
5395
+ });
5396
+ z.object({ jobId: z.string().min(1).max(64) });
5397
+ z.object({ isRemoved: z.boolean() });
5398
+ //#endregion
5399
+ //#region ../contracts/dist/infrared.js
5400
+ /**
5401
+ * The house's Infrared broker, as the hub runs it and the Brokers page shows it.
5402
+ *
5403
+ * Infrared is one of the platform's brokers: the remotes the house has learned. An IR blaster - a
5404
+ * device of an integration whose profile declares {@link infraredProfileSchema} - only sends and
5405
+ * listens; what it sends belongs to a **remote**, a device of the platform's own `infrared` vendor
5406
+ * (driver `infrared/remote`, transport `infrared`, device key `infrared-<remoteId>`). Each learned
5407
+ * button is a `button` entity of that remote, and pressing it sends the button's code through the
5408
+ * remote's blaster.
5409
+ *
5410
+ * A code is kept in {@link infraredCodeSchema}'s shape - timings, plus the protocol when one is
5411
+ * recognised - and never in a blaster's own format, so a remote moves to another blaster, of
5412
+ * another brand even, without learning again. A blaster's integration converts to and from its
5413
+ * own format with the SDK's codec; the hub sends it the reserved {@link INFRARED_OPS} over its
5414
+ * instance's connection.
5415
+ */
5416
+ /** The platform transport every remote is reached through. */
5417
+ const INFRARED_TRANSPORT = "infrared";
5418
+ /** What a code is sent as: infrared, or radio on 433 MHz. */
5419
+ const INFRARED_SIGNALS = ["ir", "rf"];
5420
+ /** The protocols the codec recognises a code as; one it does not is raw timings. */
5421
+ const INFRARED_PROTOCOLS = [
5422
+ "nec",
5423
+ "nec-ext",
5424
+ "samsung32",
5425
+ "sirc",
5426
+ "sirc15",
5427
+ "sirc20",
5428
+ "rc5"
5429
+ ];
5430
+ /** What a remote is, for its icon: a TV, an air conditioner, a fan, audio, a light, or other. */
5431
+ const INFRARED_REMOTE_KINDS = [
5432
+ "tv",
5433
+ "air-conditioner",
5434
+ "fan",
5435
+ "audio",
5436
+ "light",
5437
+ "other"
5438
+ ];
5439
+ /** The formats Import reads: those, and the Flipper Zero `.ir` file. */
5440
+ const INFRARED_IMPORT_FORMATS = [...[
5441
+ "broadlink",
5442
+ "pronto",
5443
+ "raw"
5444
+ ], "flipper"];
5445
+ /** How a learn job goes: sweeping is the RF frequency search; captured and after are its end. */
5446
+ const INFRARED_LEARN_PHASES = [
5447
+ "ready",
5448
+ "sweeping",
5449
+ "listening",
5450
+ "captured",
5451
+ "failed",
5452
+ "cancelled"
5453
+ ];
5454
+ /** The most pulses one code carries. */
5455
+ const INFRARED_MAXIMUM_PULSES = 2048;
5456
+ /** A code recognised as a protocol: its address and command. */
5457
+ const infraredProtocolSchema = z.object({
5458
+ name: z.enum(INFRARED_PROTOCOLS),
5459
+ address: z.int().min(0).max(4294967295),
5460
+ command: z.int().min(0).max(4294967295)
5461
+ });
5462
+ /**
5463
+ * A code, whichever blaster learned it: the pulses in microseconds, a mark then a space in turn,
5464
+ * starting with a mark; the carrier an IR code is modulated on (0 for RF, which is on-off keyed);
5465
+ * an RF code's frequency; and the protocol, when the codec recognises one.
5466
+ */
5467
+ const infraredCodeSchema = z.object({
5468
+ signal: z.enum(INFRARED_SIGNALS),
5469
+ carrierHz: z.int().min(0).max(1e6),
5470
+ frequencyHz: z.int().min(1e6).max(1e10).optional(),
5471
+ pulses: z.array(z.int().min(1).max(1e7)).min(1).max(INFRARED_MAXIMUM_PULSES),
5472
+ protocol: infraredProtocolSchema.optional()
5473
+ }).refine((code) => code.signal === "rf" || code.frequencyHz === void 0, {
5474
+ message: "Only a radio code has a frequency",
5475
+ path: ["frequencyHz"]
5476
+ }).refine((code) => code.signal === "rf" || code.carrierHz > 0, {
5477
+ message: "An infrared code has a carrier",
5478
+ path: ["carrierHz"]
5479
+ });
5480
+ /** What a blaster declares in its profile: the signals it sends, and whether it can learn. */
5481
+ const infraredProfileSchema = z.strictObject({
5482
+ signals: z.union([z.tuple([z.literal("ir")]), z.tuple([z.literal("ir"), z.literal("rf")])]),
5483
+ receives: z.boolean()
5484
+ });
5485
+ z.object({ code: infraredCodeSchema });
5486
+ /** How long a blaster listens or sweeps at most. */
5487
+ const timeoutSchema = z.int().min(1e3).max(12e4);
5488
+ z.object({
5489
+ signal: z.enum(INFRARED_SIGNALS),
5490
+ timeoutMs: timeoutSchema
5491
+ });
5492
+ z.object({ timeoutMs: timeoutSchema });
5493
+ z.object({ frequencyHz: z.int().min(1e6).max(1e10) });
5494
+ z.object({});
5495
+ /** A blaster: an IR-capable device an integration provides. */
5496
+ const infraredBlasterSchema = z.object({
5497
+ deviceId: integrationIdSchema,
5498
+ name: z.string().max(256),
5499
+ vendorId: z.string().regex(VENDOR_ID_PATTERN$1),
5500
+ vendorName: z.string().max(128),
5501
+ areaId: z.uuid().nullable(),
5502
+ isOnline: z.boolean(),
5503
+ signals: z.array(z.enum(INFRARED_SIGNALS)).min(1).max(INFRARED_SIGNALS.length),
5504
+ receives: z.boolean(),
5505
+ lastSentAt: timestampSchema.nullable(),
5506
+ /** How many remotes send through it. */
5507
+ remotes: z.int().min(0)
5508
+ });
5509
+ z.object({ items: z.array(infraredBlasterSchema) });
5510
+ /** A learn job under way on a blaster, as the Learning card says it. */
5511
+ const infraredListeningSchema = z.object({
5512
+ jobId: z.string().min(1).max(64),
5513
+ blasterId: integrationIdSchema,
5514
+ blasterName: z.string().max(256)
5515
+ });
5516
+ z.object({
5517
+ state: z.enum(BROKER_STATES),
5518
+ error: z.string().max(2048).nullable(),
5519
+ blasters: z.object({
5520
+ total: z.int().min(0),
5521
+ online: z.int().min(0)
5522
+ }),
5523
+ remotes: z.int().min(0),
5524
+ /** The last button sent; null before any. */
5525
+ lastSent: z.object({
5526
+ remoteId: z.uuid(),
5527
+ remoteName: z.string().max(128),
5528
+ buttonId: z.uuid(),
5529
+ buttonName: z.string().max(64),
5530
+ blasterName: z.string().max(256).nullable(),
5531
+ at: timestampSchema
5532
+ }).nullable(),
5533
+ /** The learn jobs listening or sweeping now. */
5534
+ learning: z.array(infraredListeningSchema).max(64),
5535
+ /** The remotes, and how many have a blaster online: the Brokers card's count. */
5536
+ devices: z.object({
5537
+ total: z.int().min(0),
5538
+ online: z.int().min(0)
5539
+ })
5540
+ });
5541
+ /** A remote's name, as the owner writes it. */
5542
+ const remoteNameSchema = z.string().trim().min(1, "Needed: a name").max(128);
5543
+ /** A button's name, as the owner writes it. */
5544
+ const buttonNameSchema = z.string().trim().min(1, "Needed: a name").max(64);
5545
+ /** A cell of a keypad, counted along the rows from 0. */
5546
+ const cellSchema = z.int().min(0).max(1023);
5547
+ /** A remote, without its buttons. */
5548
+ const infraredRemoteSchema = z.object({
5549
+ id: z.uuid(),
5550
+ name: z.string().max(128),
5551
+ kind: z.enum(INFRARED_REMOTE_KINDS),
5552
+ /** The blaster its codes go through; null after it was removed, until the owner picks one. */
5553
+ blasterId: integrationIdSchema.nullable(),
5554
+ blasterName: z.string().max(256).nullable(),
5555
+ isBlasterOnline: z.boolean(),
5556
+ /** Whether the blaster sends RF, so the RF screens show. */
5557
+ canRf: z.boolean(),
5558
+ columns: z.int().min(1).max(6),
5559
+ /** The device it is in the house; null until it was added. */
5560
+ deviceId: integrationIdSchema.nullable(),
5561
+ areaId: z.uuid().nullable(),
5562
+ buttonCount: z.int().min(0),
5563
+ lastPressedAt: timestampSchema.nullable()
5564
+ });
5565
+ z.object({ items: z.array(infraredRemoteSchema) });
5566
+ /** A button of a remote. */
5567
+ const infraredButtonSchema = z.object({
5568
+ id: z.uuid(),
5569
+ remoteId: z.uuid(),
5570
+ name: z.string().max(64),
5571
+ icon: iconIdSchema.nullable(),
5572
+ cell: cellSchema,
5573
+ code: infraredCodeSchema,
5574
+ lastPressedAt: timestampSchema.nullable()
5575
+ });
5576
+ infraredRemoteSchema.extend({ buttons: z.array(infraredButtonSchema).max(256) });
5577
+ z.object({ id: z.uuid() });
5578
+ z.object({
5579
+ id: z.uuid().optional(),
5580
+ name: remoteNameSchema,
5581
+ kind: z.enum(INFRARED_REMOTE_KINDS),
5582
+ blasterId: integrationIdSchema.nullable(),
5583
+ columns: z.int().min(1).max(6).default(3),
5584
+ /** The room its device is put in when it is added; ignored afterwards, the device's own. */
5585
+ areaId: z.uuid().nullable().optional()
5586
+ });
5587
+ z.object({ isRemoved: z.boolean() });
5588
+ z.object({
5589
+ remoteId: z.uuid(),
5590
+ id: z.uuid().optional(),
5591
+ name: buttonNameSchema,
5592
+ icon: iconIdSchema.nullable().default(null),
5593
+ cell: cellSchema.optional(),
5594
+ code: infraredCodeSchema
5595
+ });
5596
+ z.object({
5597
+ id: z.uuid(),
5598
+ cell: cellSchema
5599
+ });
5600
+ z.object({ pressedAt: timestampSchema });
5601
+ z.object({
5602
+ blasterId: integrationIdSchema,
5603
+ code: infraredCodeSchema
5604
+ });
5605
+ z.object({
5606
+ blasterId: integrationIdSchema,
5607
+ signal: z.enum(INFRARED_SIGNALS).default("ir"),
5608
+ /** How long it listens: about 30 s, as a Broadlink does. */
5609
+ timeoutMs: timeoutSchema.default(3e4)
5610
+ });
5611
+ z.object({
5612
+ id: z.string().min(1).max(64),
5613
+ blasterId: integrationIdSchema,
5614
+ blasterName: z.string().max(256),
5615
+ signal: z.enum(INFRARED_SIGNALS),
5616
+ phase: z.enum(INFRARED_LEARN_PHASES),
5617
+ startedAt: timestampSchema,
5618
+ finishedAt: timestampSchema.nullable(),
5619
+ /** When listening or sweeping stops on its own; for the countdown ring. */
5620
+ endsAt: timestampSchema.nullable(),
5621
+ /** An RF code's frequency, once the sweep found it. */
5622
+ frequencyHz: z.int().min(1e6).max(1e10).nullable(),
5623
+ /** The code, once captured. */
5624
+ code: infraredCodeSchema.nullable(),
5625
+ /** Why it failed - nothing heard, or the integration's error - in a sentence. */
5626
+ error: z.string().max(2048).nullable()
5627
+ });
5628
+ z.object({ jobId: z.string().min(1).max(64) });
5629
+ z.object({
5630
+ text: z.string().max(1e6),
5631
+ fileName: z.string().max(256).optional()
5632
+ });
5633
+ /** One code an import found, named as its file named it or as the line it came from. */
5634
+ const infraredImportItemSchema = z.object({
5635
+ name: z.string().max(64),
5636
+ format: z.enum(INFRARED_IMPORT_FORMATS),
5637
+ code: infraredCodeSchema
5638
+ });
5639
+ z.object({
5640
+ items: z.array(infraredImportItemSchema).max(256),
5641
+ problems: z.array(z.object({
5642
+ /** The line, counted from 1; null for a problem of the whole text. */
5643
+ line: z.int().min(1).nullable(),
5644
+ text: z.string().max(256),
5645
+ reason: z.string().max(512)
5646
+ })).max(256)
5647
+ });
5648
+ z.object({
5649
+ remoteId: z.uuid(),
5650
+ items: z.array(z.object({
5651
+ name: buttonNameSchema,
5652
+ icon: iconIdSchema.nullable().default(null),
5653
+ code: infraredCodeSchema
5654
+ })).min(1).max(256)
5655
+ });
5656
+ z.object({ code: infraredCodeSchema });
5657
+ z.object({
5658
+ broadlink: z.string().max(16384).nullable(),
5659
+ pronto: z.string().max(65536).nullable(),
5660
+ raw: z.string().max(65536),
5661
+ /** The protocol the pulses are read as now; null for raw timings and for RF. */
5662
+ protocol: infraredProtocolSchema.nullable(),
5663
+ pulseCount: z.int().min(1).max(INFRARED_MAXIMUM_PULSES),
5664
+ carrierHz: z.int().min(0).max(1e6)
5665
+ });
4858
5666
  //#endregion
4859
5667
  //#region ../contracts/dist/modbus.js
4860
5668
  /**
@@ -5691,23 +6499,34 @@ function containerDependencyProblems(deps, setup) {
5691
6499
  * requires of executable content.
5692
6500
  */
5693
6501
  /**
5694
- * Bundle kind, which is both how it is written and how far it is trusted. `matter`, `mqtt` and
5695
- * `modbus` are drivers one of the platform's broker mappings runs: no profile and no code, so as
5696
- * far trusted as a profile. A `modbus` driver names its template, which the mapping reads.
6502
+ * Bundle kind, which is both how it is written and how far it is trusted. `matter`, `mqtt`,
6503
+ * `modbus` and `infrared` are drivers one of the platform's broker mappings runs: no profile and
6504
+ * no code, so as far trusted as a profile. A `modbus` driver names its template, which the
6505
+ * mapping reads; `infrared` is the platform's own remote, whose buttons come with the device.
6506
+ *
6507
+ * `serial` and `bluetooth` are code drivers, trusted as code is: a `serial` one says how its port
6508
+ * is spoken unless the owner says otherwise, and a `bluetooth` one runs on the platform's
6509
+ * `bluetooth` transport, its devices the addresses the house hears.
5697
6510
  */
5698
6511
  const BUNDLE_KINDS = [
5699
6512
  "declarative",
5700
6513
  "code",
5701
6514
  "matter",
5702
6515
  "mqtt",
5703
- "modbus"
6516
+ "modbus",
6517
+ "infrared",
6518
+ "serial",
6519
+ "bluetooth"
5704
6520
  ];
5705
6521
  /** The transport each kind the platform's mappings run is reached on. */
5706
6522
  const MAPPED_KINDS = {
5707
6523
  matter: MATTER_TRANSPORT,
5708
6524
  mqtt: MQTT_HOUSE_TRANSPORT,
5709
- modbus: MODBUS_TRANSPORT
6525
+ modbus: MODBUS_TRANSPORT,
6526
+ infrared: INFRARED_TRANSPORT
5710
6527
  };
6528
+ /** The transport a code kind is bound to, for the kinds that are. */
6529
+ const BOUND_CODE_KINDS = { bluetooth: BLUETOOTH_TRANSPORT };
5711
6530
  /** Validates a vendor id. */
5712
6531
  const vendorIdSchema = z.string().regex(/^[a-z][a-z0-9-]{1,63}$/, "Must be a vendor id");
5713
6532
  /** Validates a driver id. */
@@ -5902,6 +6721,12 @@ const driverShape = {
5902
6721
  /** Path to the Modbus template, relative to the driver. Modbus drivers only. */
5903
6722
  template: bundlePathSchema.optional(),
5904
6723
  /**
6724
+ * How the port is spoken unless the owner says otherwise: `{ "baudRate": 2400 }`, 8N1 and no
6725
+ * flow control unless said. Serial drivers only, and required of them: the device dialog
6726
+ * preselects it beside the port.
6727
+ */
6728
+ serial: serialLineSchema.optional(),
6729
+ /**
5905
6730
  * Path to `setup.json`, relative to the driver: what setting up an instance asks.
5906
6731
  *
5907
6732
  * Required of every driver, declarative or code. Without it the interface has no form to
@@ -5927,6 +6752,22 @@ function checkKindFiles(manifest, ctx) {
5927
6752
  path: ["template"],
5928
6753
  message: `A ${manifest.kind} driver must not name a template`
5929
6754
  });
6755
+ if (manifest.kind === "serial" && manifest.serial === void 0) ctx.addIssue({
6756
+ code: "custom",
6757
+ path: ["serial"],
6758
+ message: "A serial driver must say how its port is spoken: { \"baudRate\": … }"
6759
+ });
6760
+ if (manifest.kind !== "serial" && manifest.serial !== void 0) ctx.addIssue({
6761
+ code: "custom",
6762
+ path: ["serial"],
6763
+ message: `A ${manifest.kind} driver has no port to speak`
6764
+ });
6765
+ const bound = BOUND_CODE_KINDS[manifest.kind];
6766
+ if (bound !== void 0 && manifest.transport !== bound) ctx.addIssue({
6767
+ code: "custom",
6768
+ path: ["transport"],
6769
+ message: `A ${manifest.kind} driver runs on the ${bound} transport`
6770
+ });
5930
6771
  if (mapped !== void 0) {
5931
6772
  for (const named of ["profile", "entry"]) if (manifest[named] !== void 0) ctx.addIssue({
5932
6773
  code: "custom",
@@ -6052,6 +6893,7 @@ function resolveManifest(vendor, driver) {
6052
6893
  ...driver.profile === void 0 ? {} : { profile: driver.profile },
6053
6894
  ...driver.entry === void 0 ? {} : { entry: driver.entry },
6054
6895
  ...driver.template === void 0 ? {} : { template: driver.template },
6896
+ ...driver.serial === void 0 ? {} : { serial: driver.serial },
6055
6897
  setup: driver.setup
6056
6898
  };
6057
6899
  }
@@ -6116,6 +6958,59 @@ function defineAccount(module) {
6116
6958
  }
6117
6959
  z.array(setupFieldSchema).min(1).max(8);
6118
6960
  //#endregion
6961
+ //#region ../package-runtime/dist/bthome.js
6962
+ /**
6963
+ * A number of `bytes` bytes, scaled.
6964
+ *
6965
+ * @param key - What it measures.
6966
+ * @param name - What it is called.
6967
+ * @param bytes - How long it is.
6968
+ * @param factor - What one step of it is worth.
6969
+ * @param unit - Its unit, if any.
6970
+ * @param deviceClass - Its sensor class, if any.
6971
+ * @param isSigned - Whether it is signed.
6972
+ * @returns The object.
6973
+ */
6974
+ function numberObject(key, name, bytes, factor, unit, deviceClass, isSigned = false) {
6975
+ return {
6976
+ key,
6977
+ name,
6978
+ kind: "number",
6979
+ bytes,
6980
+ factor,
6981
+ isSigned,
6982
+ ...unit === void 0 ? {} : { unit },
6983
+ ...deviceClass === void 0 ? {} : { deviceClass }
6984
+ };
6985
+ }
6986
+ /**
6987
+ * A one-byte on/off.
6988
+ *
6989
+ * @param key - What it senses.
6990
+ * @param name - What it is called.
6991
+ * @param deviceClass - Its binary sensor class, when the platform has one.
6992
+ * @returns The object.
6993
+ */
6994
+ function binaryObject(key, name, deviceClass) {
6995
+ return {
6996
+ key,
6997
+ name,
6998
+ kind: "binary",
6999
+ bytes: 1,
7000
+ ...deviceClass === void 0 ? {} : { deviceClass }
7001
+ };
7002
+ }
7003
+ numberObject("battery", "Battery", 1, 1, "%", "battery"), numberObject("temperature", "Temperature", 2, .01, "°C", "temperature", true), numberObject("humidity", "Humidity", 2, .01, "%", "humidity"), numberObject("pressure", "Pressure", 3, .01, "hPa", "atmospheric_pressure"), numberObject("illuminance", "Illuminance", 3, .01, "lx", "illuminance"), numberObject("mass", "Mass", 2, .01, "kg", "weight"), numberObject("mass", "Mass", 2, .01, "lb", "weight"), numberObject("dew_point", "Dew point", 2, .01, "°C", "temperature", true), numberObject("count", "Count", 1, 1), numberObject("energy", "Energy", 3, .001, "kWh", "energy"), numberObject("power", "Power", 3, .01, "W", "power"), numberObject("voltage", "Voltage", 2, .001, "V", "voltage"), numberObject("pm25", "PM2.5", 2, 1, "µg/m³", "pm25"), numberObject("pm10", "PM10", 2, 1, "µg/m³", "pm10"), binaryObject("binary", "Binary"), binaryObject("power_state", "Power", "power"), binaryObject("opening", "Opening", "opening"), numberObject("carbon_dioxide", "Carbon dioxide", 2, 1, "ppm", "carbon_dioxide"), numberObject("tvoc", "VOC", 2, 1, "µg/m³", "volatile_organic_compounds"), numberObject("moisture", "Moisture", 2, .01, "%"), binaryObject("battery_low", "Battery low", "battery"), binaryObject("battery_charging", "Battery charging", "battery_charging"), binaryObject("carbon_monoxide", "Carbon monoxide"), binaryObject("cold", "Cold"), binaryObject("connectivity", "Connectivity", "connectivity"), binaryObject("door", "Door", "door"), binaryObject("garage_door", "Garage door", "garage_door"), binaryObject("gas_detected", "Gas", "gas"), binaryObject("heat", "Heat"), binaryObject("light", "Light"), binaryObject("lock", "Lock"), binaryObject("leak", "Moisture", "moisture"), binaryObject("motion", "Motion", "motion"), binaryObject("moving", "Moving"), binaryObject("occupancy", "Occupancy", "occupancy"), binaryObject("plug", "Plug", "plug"), binaryObject("presence", "Presence", "presence"), binaryObject("problem", "Problem", "problem"), binaryObject("running", "Running", "running"), binaryObject("safety", "Safety"), binaryObject("smoke", "Smoke", "smoke"), binaryObject("sound", "Sound"), binaryObject("tamper", "Tamper"), binaryObject("vibration", "Vibration"), binaryObject("window", "Window", "window"), numberObject("humidity", "Humidity", 1, 1, "%", "humidity"), numberObject("moisture", "Moisture", 1, 1, "%"), numberObject("count", "Count", 2, 1), numberObject("count", "Count", 4, 1), numberObject("rotation", "Rotation", 2, .1, "°", void 0, true), numberObject("distance", "Distance", 2, 1, "mm", "distance"), numberObject("distance", "Distance", 2, .1, "m", "distance"), numberObject("duration", "Duration", 3, .001, "s", "duration"), numberObject("current", "Current", 2, .001, "A", "current"), numberObject("speed", "Speed", 2, .01, "m/s", "speed"), numberObject("temperature", "Temperature", 2, .1, "°C", "temperature", true), numberObject("uv_index", "UV index", 1, .1), numberObject("volume", "Volume", 2, .1, "L", "volume"), numberObject("volume", "Volume", 2, 1, "mL", "volume"), numberObject("volume_flow_rate", "Flow rate", 2, .001, "m³/h", "volume_flow_rate"), numberObject("voltage", "Voltage", 2, .1, "V", "voltage"), numberObject("gas", "Gas", 3, .001, "m³", "gas"), numberObject("gas", "Gas", 4, .001, "m³", "gas"), numberObject("energy", "Energy", 4, .001, "kWh", "energy"), numberObject("volume", "Volume", 4, .001, "L", "volume"), numberObject("water", "Water", 4, .001, "L", "water");
7004
+ z.object({
7005
+ /** Its address, upper case: its device key on the `bluetooth` transport. */
7006
+ deviceKey: bluetoothAddressSchema,
7007
+ /** The driver it is read as: `bluetooth/bthome`, or an integration's of kind `bluetooth`. */
7008
+ kind: z.string().min(1).max(160),
7009
+ name: z.string().min(1).max(256),
7010
+ /** The advertisement it was recognised from. */
7011
+ advert: bluetoothAdvertSchema
7012
+ });
7013
+ //#endregion
6119
7014
  //#region ../package-runtime/dist/expression.js
6120
7015
  /**
6121
7016
  * The only expression a declarative bundle has: a path into a payload.
@@ -7076,7 +7971,19 @@ const integrationProfileSchema = z.strictObject({
7076
7971
  /** Where the device's own reports say which firmware it runs now. */
7077
7972
  device: deviceInfoMappingSchema.optional(),
7078
7973
  /** At least one: a profile that declares no entity describes nothing. */
7079
- entities: z.array(entityProfileSchema).min(1)
7974
+ entities: z.array(entityProfileSchema).min(1),
7975
+ /**
7976
+ * That the device is an IR blaster the Infrared broker sends remotes' codes through: the
7977
+ * signals it sends, and whether it can learn. Its integration answers the reserved
7978
+ * `infrared.*` ops over the device's connection.
7979
+ */
7980
+ infrared: infraredProfileSchema.optional(),
7981
+ /**
7982
+ * That the device can be a Bluetooth proxy, listening for the house: the scan modes it runs.
7983
+ * Its integration answers the reserved `bluetooth-proxy.*` ops and reports what it hears through
7984
+ * the SDK's scanner.
7985
+ */
7986
+ bluetoothProxy: bluetoothProxyProfileSchema.optional()
7080
7987
  });
7081
7988
  //#endregion
7082
7989
  //#region ../package-runtime/dist/discovery.js
@@ -7232,17 +8139,45 @@ const modbusDiscoverySchema = brokerDiscoverySchema("values.model").extend({
7232
8139
  /** What to read, by the name the rules use. */
7233
8140
  read: z.record(z.string().regex(MODBUS_VALUE_NAME, "A name of letters and digits"), modbusReadSchema).refine((read) => Object.keys(read).length <= MAXIMUM_MODBUS_READS, { message: `At most ${String(MAXIMUM_MODBUS_READS)} values` }).default({})
7234
8141
  });
7235
- /** The platform's brokers a vendor may take devices of, and the transport each reaches them on. */
8142
+ /**
8143
+ * Which of the serial ports the server sees are the vendor's: rules on the USB strings its adapter
8144
+ * gives (a {@link SerialSighting}) - `vendorId` and `productId`, four lower-case hex digits
8145
+ * (`10c4`, `ea60`), `manufacturer`, `product` and `serialNumber`; the model is `product` unless
8146
+ * said, in the drivers that say `"kind": "serial"`.
8147
+ *
8148
+ * A port the rules take is suggested for the vendor - "Use with …?" - and never taken on its own:
8149
+ * the owner says which device it is.
8150
+ */
8151
+ const serialDiscoverySchema = brokerDiscoverySchema("product");
8152
+ /**
8153
+ * Which of the devices the house's Bluetooth hears are the vendor's: rules on one advertisement (a
8154
+ * {@link BluetoothSighting}) - its `name`; `manufacturerData.'<company id>'`, the id in decimal,
8155
+ * that `exists` or `startsWith` the data's first bytes in hex; `serviceUuids`, which `contains` a
8156
+ * UUID in lower case; `serviceData.'<uuid>'`, that `exists`. The model is `name` unless said, in
8157
+ * the drivers on the `bluetooth` transport.
8158
+ *
8159
+ * A device the rules take is listed as found for the vendor, and added only by the owner: what a
8160
+ * neighbour's sensor advertises is heard as well as the house's own.
8161
+ */
8162
+ const bluetoothDiscoverySchema = brokerDiscoverySchema("name");
8163
+ /**
8164
+ * The platform's brokers a vendor may take devices of, and the transport each reaches them on.
8165
+ * Serial has none of its own: its devices are reached over their vendor's transport, through the
8166
+ * port the broker hands it, so its drivers are the ones that say `"kind": "serial"`.
8167
+ */
7236
8168
  const BROKER_TRANSPORTS = {
7237
8169
  matter: MATTER_TRANSPORT,
7238
8170
  mqtt: MQTT_HOUSE_TRANSPORT,
7239
- modbus: MODBUS_TRANSPORT
8171
+ modbus: MODBUS_TRANSPORT,
8172
+ bluetooth: BLUETOOTH_TRANSPORT
7240
8173
  };
7241
8174
  /** Where each broker's device says its model, when the vendor's block does not say. */
7242
8175
  const BROKER_MODELS = {
7243
8176
  matter: "productId",
7244
8177
  mqtt: "model",
7245
- modbus: "values.model"
8178
+ modbus: "values.model",
8179
+ serial: "product",
8180
+ bluetooth: "name"
7246
8181
  };
7247
8182
  /** Validates a whole `discovery.json`. */
7248
8183
  const vendorDiscoverySchema = z.strictObject({
@@ -7255,6 +8190,8 @@ const vendorDiscoverySchema = z.strictObject({
7255
8190
  matter: matterDiscoverySchema.optional(),
7256
8191
  mqtt: mqttDiscoverySchema.optional(),
7257
8192
  modbus: modbusDiscoverySchema.optional(),
8193
+ serial: serialDiscoverySchema.optional(),
8194
+ bluetooth: bluetoothDiscoverySchema.optional(),
7258
8195
  /**
7259
8196
  * Who adds a device found. `owner` - the default - proposes it on the vendor's
7260
8197
  * page; `automatic` adds it as soon as it is heard, for devices the owner already chose
@@ -7275,10 +8212,10 @@ const vendorDiscoverySchema = z.strictObject({
7275
8212
  state: z.string().min(1).max(64)
7276
8213
  }).optional()
7277
8214
  }).superRefine((discovery, ctx) => {
7278
- if (discovery.listen.length === 0 && discovery.matter === void 0 && discovery.mqtt === void 0 && discovery.modbus === void 0) ctx.addIssue({
8215
+ if (discovery.listen.length === 0 && discovery.matter === void 0 && discovery.mqtt === void 0 && discovery.modbus === void 0 && discovery.serial === void 0 && discovery.bluetooth === void 0) ctx.addIssue({
7279
8216
  code: "custom",
7280
8217
  path: ["listen"],
7281
- message: "A vendor listens somewhere, or says which Matter, MQTT or Modbus devices are its own, or both"
8218
+ message: "A vendor listens somewhere, or says which Matter, MQTT, Modbus, serial or Bluetooth devices are its own, or both"
7282
8219
  });
7283
8220
  if (discovery.listen.length > 0 && discovery.probe === void 0 && discovery.identity.model === void 0) ctx.addIssue({
7284
8221
  code: "custom",
@@ -7394,7 +8331,7 @@ function choicesOf(driver) {
7394
8331
  */
7395
8332
  function associateModel(plan, modelCode, claimedBy) {
7396
8333
  const brokers = Object.values(BROKER_TRANSPORTS);
7397
- const reached = new Set(plan.drivers.filter((driver) => !brokers.includes(driver.transport)).map(({ id }) => id));
8334
+ const reached = new Set(plan.drivers.filter((driver) => !brokers.includes(driver.transport) && driver.kind !== "serial").map(({ id }) => id));
7398
8335
  const coded = modelCode === null ? [] : (plan.codes.get(modelCode) ?? []).filter((entry) => reached.has(entry.driverId));
7399
8336
  if (coded.length > 0) {
7400
8337
  const found = claimedBy === void 0 ? coded.length === 1 ? coded[0] : void 0 : coded.find((entry) => claimedBy(entry.driverId));
@@ -7419,7 +8356,7 @@ function associateModel(plan, modelCode, claimedBy) {
7419
8356
  * Whether a device of one of the platform's brokers is the vendor's, by the rules of its block.
7420
8357
  *
7421
8358
  * @param plan - The vendor's discovery.
7422
- * @param broker - Which broker holds the device: `matter`, `mqtt`.
8359
+ * @param broker - Which broker holds the device: `matter`, `mqtt`, `serial`.
7423
8360
  * @param identity - Who the device says it is.
7424
8361
  * @returns True when the vendor has the broker's block and every one of its conditions holds.
7425
8362
  */
@@ -7431,15 +8368,16 @@ function matchesBroker(plan, broker, identity) {
7431
8368
  * model.
7432
8369
  *
7433
8370
  * The model the device says is looked up in the `modelCodes` of the vendor's drivers on that
7434
- * broker's transport; one it does not map goes to the vendor's driver there when it has only one.
8371
+ * broker's transport - for serial, its drivers of kind `serial` - and one it does not map goes to
8372
+ * the vendor's driver there when it has only one.
7435
8373
  *
7436
8374
  * @param plan - The vendor's discovery.
7437
- * @param broker - Which broker holds the device: `matter`, `mqtt`.
8375
+ * @param broker - Which broker holds the device: `matter`, `mqtt`, `serial`.
7438
8376
  * @param identity - Who the device says it is.
7439
8377
  * @returns The association, or null when no driver of the vendor on the broker takes it.
7440
8378
  */
7441
8379
  function associateBroker(plan, broker, identity) {
7442
- const drivers = plan.drivers.filter((driver) => driver.transport === BROKER_TRANSPORTS[broker]);
8380
+ const drivers = plan.drivers.filter((driver) => broker === "serial" ? driver.kind === "serial" : driver.transport === BROKER_TRANSPORTS[broker]);
7443
8381
  const modelCode = textAt(identity, plan.discovery[broker]?.model ?? BROKER_MODELS[broker]);
7444
8382
  const [first] = modelCode === null ? [] : (plan.codes.get(modelCode) ?? []).filter((entry) => drivers.some((driver) => driver.id === entry.driverId));
7445
8383
  if (first !== void 0) return {
@@ -7457,6 +8395,166 @@ function associateBroker(plan, broker, identity) {
7457
8395
  };
7458
8396
  }
7459
8397
  //#endregion
8398
+ //#region ../package-runtime/dist/infrared-mapping.js
8399
+ /**
8400
+ * How a remote the house learned is a device: pure.
8401
+ *
8402
+ * A remote is a device of the platform's own `infrared` vendor (driver `infrared/remote`), reached
8403
+ * over the platform transport `infrared` with the device key `infrared-<remoteId>`. Each learned
8404
+ * button is a `button` entity, by the button's id so a rename keeps it, with the button's name and
8405
+ * icon; pressing it asks the transport to `press` that button, and the Infrared broker sends the
8406
+ * button's code through the remote's blaster. A remote whose blaster is gone or offline keeps its
8407
+ * buttons, unavailable.
8408
+ *
8409
+ * The remote travels in the claim, as a Modbus device's template does: the broker hands each one
8410
+ * over with its buttons, so what a device describes and what a press sends are the same remote's.
8411
+ */
8412
+ /** A remote as the Infrared broker hands it over: its device key, name and buttons. */
8413
+ const infraredRemoteSnapshotSchema = z.object({
8414
+ /** `infrared-<remoteId>`. */
8415
+ deviceKey: z.string().min(1).max(160),
8416
+ /** The driver it is read as: `infrared/remote`. */
8417
+ kind: z.string().min(1).max(160),
8418
+ name: z.string().min(1).max(128),
8419
+ buttons: z.array(z.object({
8420
+ id: z.uuid(),
8421
+ name: z.string().min(1).max(64),
8422
+ icon: iconIdSchema.nullable()
8423
+ })).max(256)
8424
+ });
8425
+ /** What the transport delivers for a remote: whether its blaster can send now. */
8426
+ const inboundSchema$3 = z.object({ isAvailable: z.boolean() });
8427
+ /**
8428
+ * The remote a claim was made for.
8429
+ *
8430
+ * @param descriptor - The claim's descriptor.
8431
+ * @returns The remote, or undefined for anything that is not one.
8432
+ */
8433
+ function snapshotOf$1(descriptor) {
8434
+ const parsed = infraredRemoteSnapshotSchema.safeParse(descriptor);
8435
+ return parsed.success ? parsed.data : void 0;
8436
+ }
8437
+ /**
8438
+ * A button of a remote as the house registers it.
8439
+ *
8440
+ * @param button - The button.
8441
+ * @returns Its descriptor.
8442
+ */
8443
+ function buttonDescriptorOf(button) {
8444
+ return {
8445
+ domain: "button",
8446
+ objectId: button.id,
8447
+ name: button.name,
8448
+ capabilities: { button: {} },
8449
+ ...button.icon === null ? {} : { icon: button.icon }
8450
+ };
8451
+ }
8452
+ /**
8453
+ * Builds the driver of the platform's remotes: `infrared/remote`.
8454
+ *
8455
+ * @param manifest - The driver's.
8456
+ * @param manifest.id - Its kind, `vendor/driver`.
8457
+ * @returns The integration.
8458
+ */
8459
+ function createInfraredIntegration(manifest) {
8460
+ /**
8461
+ * The remote a claim was made for.
8462
+ *
8463
+ * @param context - The instance's mapping context.
8464
+ * @returns The remote.
8465
+ * @throws {ValidationError} When the claim holds no remote - which identification never makes.
8466
+ */
8467
+ const claimed = (context) => {
8468
+ const remote = snapshotOf$1(context.claim.descriptor);
8469
+ if (remote === void 0) throw new ValidationError(`${manifest.id} holds a claim that is not a remote`, { details: { integration: manifest.id } });
8470
+ return remote;
8471
+ };
8472
+ return {
8473
+ id: manifest.id,
8474
+ transport: INFRARED_TRANSPORT,
8475
+ optionsSchema: jsonObjectSchema,
8476
+ discovery: {},
8477
+ /**
8478
+ * Claims a remote the broker hands over as this driver's.
8479
+ *
8480
+ * @param discovered - What the transport handed over.
8481
+ * @returns The claim, or null for anything that is not one of this driver's remotes.
8482
+ */
8483
+ claim: (discovered) => {
8484
+ const remote = snapshotOf$1(discovered.payload);
8485
+ return remote?.kind !== manifest.id ? null : {
8486
+ deviceKey: remote.deviceKey,
8487
+ externalId: remote.deviceKey,
8488
+ descriptor: remote
8489
+ };
8490
+ },
8491
+ /**
8492
+ * Asks the transport for nothing: it says each remote's availability on its own.
8493
+ *
8494
+ * @returns No subscription.
8495
+ */
8496
+ subscriptions: () => [],
8497
+ /**
8498
+ * Describes the remote: a button entity per learned button.
8499
+ *
8500
+ * @param claim - The claim.
8501
+ * @returns The device.
8502
+ * @throws {ValidationError} When the claim holds no remote.
8503
+ */
8504
+ describe: (claim) => {
8505
+ const remote = snapshotOf$1(claim.descriptor);
8506
+ if (remote === void 0) throw new ValidationError(`${manifest.id} cannot describe a claim with no remote`, { details: { integration: manifest.id } });
8507
+ return {
8508
+ externalId: claim.externalId,
8509
+ name: remote.name,
8510
+ entities: remote.buttons.map(buttonDescriptorOf)
8511
+ };
8512
+ },
8513
+ /**
8514
+ * Turns whether the remote's blaster can send into a report per button.
8515
+ *
8516
+ * @param message - What the transport delivered.
8517
+ * @param context - The identifiers the hub assigned.
8518
+ * @returns The reports.
8519
+ */
8520
+ toState: (message, context) => {
8521
+ const parsed = inboundSchema$3.safeParse(message.payload);
8522
+ const remote = snapshotOf$1(context.claim.descriptor);
8523
+ if (!parsed.success || remote === void 0) return [];
8524
+ return remote.buttons.flatMap((button) => {
8525
+ const entityId = context.entities.get(button.id);
8526
+ return entityId === void 0 ? [] : [{
8527
+ entityId,
8528
+ state: { button: {} },
8529
+ attributes: {},
8530
+ isAvailable: parsed.data.isAvailable
8531
+ }];
8532
+ });
8533
+ },
8534
+ /**
8535
+ * Turns a press into the button the broker sends.
8536
+ *
8537
+ * @param target - The entity, the capability and the command.
8538
+ * @param context - The claim.
8539
+ * @returns The request.
8540
+ * @throws {ValidationError} When the entity is no button of the remote.
8541
+ */
8542
+ toCommand: (target, context) => {
8543
+ const remote = claimed(context);
8544
+ const button = remote.buttons.find((candidate) => context.entities.get(candidate.id) === target.entityId);
8545
+ if (button === void 0 || target.capability !== "button") throw new ValidationError(`${manifest.id} has no button ${target.entityId}`, { details: {
8546
+ integration: manifest.id,
8547
+ entityId: target.entityId
8548
+ } });
8549
+ return {
8550
+ deviceKey: remote.deviceKey,
8551
+ op: "press",
8552
+ args: { buttonId: button.id }
8553
+ };
8554
+ }
8555
+ };
8556
+ }
8557
+ //#endregion
7460
8558
  //#region ../../node_modules/.pnpm/semver@7.8.5/node_modules/semver/internal/constants.js
7461
8559
  var require_constants = /* @__PURE__ */ __commonJSMin(((exports, module) => {
7462
8560
  module.exports = {
@@ -11723,6 +12821,16 @@ function wiresOf(vendor, transports) {
11723
12821
  return TRANSPORT_WIRES.filter((wire) => found.has(wire));
11724
12822
  }
11725
12823
  /**
12824
+ * What a vendor's devices can be for the platform's brokers: an IR blaster when one of its
12825
+ * drivers' profiles declares `infrared`, a Bluetooth proxy when one declares `bluetoothProxy`.
12826
+ *
12827
+ * @param drivers - Its drivers that loaded.
12828
+ * @returns The roles, each once, in the order the contracts list them.
12829
+ */
12830
+ function vendorRolesOf(drivers) {
12831
+ return [...drivers.some((driver) => driver.infrared !== void 0) ? ["ir-blaster"] : [], ...drivers.some((driver) => driver.bluetoothProxy !== void 0) ? ["bluetooth-proxy"] : []];
12832
+ }
12833
+ /**
11726
12834
  * Reads one driver directory.
11727
12835
  *
11728
12836
  * @param vendor - The vendor it belongs to, already checked.
@@ -11753,7 +12861,7 @@ async function readDriver(vendor, directory, resolve) {
11753
12861
  profile
11754
12862
  };
11755
12863
  }
11756
- if (manifest.kind === "matter" || manifest.kind === "mqtt") return identity;
12864
+ if (manifest.kind === "matter" || manifest.kind === "mqtt" || manifest.kind === "infrared") return identity;
11757
12865
  if (manifest.kind === "modbus") {
11758
12866
  const template = await readBundleFile(modbusTemplateSchema, directory, manifest.template ?? "");
11759
12867
  return {
@@ -11800,6 +12908,7 @@ function planDiscovery(vendor, discovery, drivers) {
11800
12908
  const plan = resolveDiscovery(vendor.id, discovery, drivers.map((driver) => ({
11801
12909
  id: driver.driverId,
11802
12910
  name: driver.driverName,
12911
+ kind: driver.manifest.kind,
11803
12912
  transport: driver.manifest.transport,
11804
12913
  models: driver.manifest.models,
11805
12914
  modelCodes: driver.manifest.modelCodes
@@ -11942,6 +13051,8 @@ async function instantiateDriver(driver, sdk, options) {
11942
13051
  await options.verify?.(driver.manifest, driver.directory);
11943
13052
  if (profile !== void 0) return {
11944
13053
  ...identity,
13054
+ ...profile.infrared === void 0 ? {} : { infrared: profile.infrared },
13055
+ ...profile.bluetoothProxy === void 0 ? {} : { bluetoothProxy: profile.bluetoothProxy },
11945
13056
  integration: createDeclarativeIntegration(driver.manifest, profile)
11946
13057
  };
11947
13058
  if (driver.manifest.kind === "matter") return {
@@ -11956,6 +13067,10 @@ async function instantiateDriver(driver, sdk, options) {
11956
13067
  ...identity,
11957
13068
  integration: createModbusIntegration(driver.manifest)
11958
13069
  };
13070
+ if (driver.manifest.kind === "infrared") return {
13071
+ ...identity,
13072
+ integration: createInfraredIntegration(driver.manifest)
13073
+ };
11959
13074
  const exported = await defaultOf(options.importModule ?? importFile, entry ?? "");
11960
13075
  const integration = typeof exported === "function" ? exported(sdk) : exported;
11961
13076
  if (integration === null || typeof integration !== "object") throw new Error(`${driver.manifest.id} does not default-export an integration`);
@@ -12088,6 +13203,7 @@ async function instantiateVendor(declaration, options) {
12088
13203
  ...declaration.logo === void 0 ? {} : { logo: declaration.logo },
12089
13204
  transports: [...new Set(drivers.map((driver) => driver.manifest.transport))],
12090
13205
  wires: wiresOf(manifest, drivers.map((driver) => driver.manifest.transport)),
13206
+ roles: vendorRolesOf(drivers),
12091
13207
  ownTransports,
12092
13208
  streams,
12093
13209
  drivers,
@@ -12100,4 +13216,4 @@ async function instantiateVendor(declaration, options) {
12100
13216
  };
12101
13217
  }
12102
13218
  //#endregion
12103
- export { composeEntityId as _, associateModel as a, matchesBroker as c, accountDeclarationSchema as d, defineAccount as f, integrationSetupSchema as g, modbusTemplateSchema as h, associateBroker as i, vendorDiscoverySchema as l, vendorManifestSchema as m, readVendorDirectory as n, identifySighting as o, driverManifestSchema as p, require_semver as r, matchesAnnouncement as s, instantiateVendor as t, integrationProfileSchema as u };
13219
+ export { ValidationError as S, infraredCodeSchema as _, associateModel as a, integrationSetupSchema as b, matchesBroker as c, accountDeclarationSchema as d, defineAccount as f, INFRARED_MAXIMUM_PULSES as g, modbusTemplateSchema as h, associateBroker as i, vendorDiscoverySchema as l, vendorManifestSchema as m, readVendorDirectory as n, identifySighting as o, driverManifestSchema as p, require_semver as r, matchesAnnouncement as s, instantiateVendor as t, integrationProfileSchema as u, bluetoothAdvertSchema as v, composeEntityId as x, serialLineSchema as y };