Write an integration
An integration connects r3cui to one program or device. This page shows the smallest one that works.
The idea
Section titled “The idea”An integration says which jobs it can do. r3cui gives the user the choice of where to use it.
| Job | Field in Provides |
What your integration does |
|---|---|---|
| Global control | global |
Volume, the input list and power for a device |
| Power alone | power |
Turns a device on and off |
| Voice | voice |
Takes the microphone’s audio and gives back an answer |
| An input’s keys | remote |
Receives key presses |
| An input’s screen | ui |
Says what’s playing, or gives a library to show |
Global control goes to one place, whatever input you’re on:
Each input has its own keys and its own screen:
Volume, inputs, power and voice always go to the same place. Every other key, and the screen, follow the input you're on.
You never write screens or settings pages. r3cui draws its setup pages, and the web configurator’s forms, from your description.
1. Make a crate
Section titled “1. Make a crate”[package]name = "mybox"version.workspace = trueedition.workspace = truelicense.workspace = true
[dependencies]r3cui-sdk = { path = "../sdk" }tokio.workspace = trueAn integration depends on r3cui-sdk and on nothing else of r3cui’s.
2. Describe it
Section titled “2. Describe it”The Spec is the description: a name, an icon, the fields to ask for, and the jobs it can do.
use r3cui_sdk::api::*;use r3cui_sdk::{DriverEntry, Integration};use std::sync::Arc;use tokio::sync::{mpsc, watch};
pub mod field { pub const HOST: &str = "host";}
pub static SPEC: Spec = Spec { id: "mybox", name: "My Box", trademark: Trademark::None, // SVG path data on a 24×24 box, filled in one colour. icon: "M4 4h16v16H4z", fields: &[Field { key: field::HOST, label: "Address", kind: FieldKind::Text, required: true, default: None }], address: Some(field::HOST), provides: Provides { global: false, remote: true, ui: false, power: false, voice: false }, pairing: None,};A field’s kind is Text, Url, Email, Port, Secret, Toggle or Target. It picks the
keyboard the remote shows. A Secret is never shown back once it’s set.
3. Write the driver
Section titled “3. Write the driver”The driver is the running connection. This one takes an input’s keys.
pub struct MyBox { status: watch::Receiver<Status>, remote: mpsc::UnboundedSender<RemoteKey>,}
impl MyBox { pub fn start(entry: &DriverEntry) -> Self { let (remote, mut keys) = mpsc::unbounded_channel(); let Some(host) = entry.text(field::HOST) else { return Self { status: watch::channel(Status::with(Health::NotSetUp, "Address missing")).1, remote }; }; let (status_tx, status) = watch::channel(Status::new(Health::Connecting)); tokio::spawn(async move { // Connect to the device at `host` here. status_tx.send_replace(Status::new(Health::Up)); while let Some(key) = keys.recv().await { // Send the key to the device. } }); Self { status, remote } }}
impl Driver for MyBox { fn spec(&self) -> &'static Spec { &SPEC }
fn status(&self) -> watch::Receiver<Status> { self.status.clone() }
fn remote(&self) -> Option<mpsc::UnboundedSender<RemoteKey>> { Some(self.remote.clone()) }}statusis the dot the user sees beside your integration:Connecting,Up,Down,NotSetUporNotPaired. Add a few words when you can say what’s wrong.RemoteKeyis one ofUp,Down,Left,Right,Select,Back,Menu,ChannelUp,ChannelDown,Previous,PlayPause,Next,StopandRecord. Volume, mute, power, Home and voice never reach an integration.
4. Export it
Section titled “4. Export it”pub const INTEGRATION: Integration = Integration { spec: &SPEC, start: |entry| Arc::new(MyBox::start(entry)) };5. Add it to the app
Section titled “5. Add it to the app”Two lines. In crates/r3cui/Cargo.toml:
mybox = { path = "../mybox" }In crates/r3cui/src/app/registry.rs, add it to the list:
const CARRIED: &[Integration] = &[ homeassistant::driver::INTEGRATION, kodi::driver::INTEGRATION, androidtv::driver::INTEGRATION, mybox::INTEGRATION,];Run the app. My Box is now in Add integration, on the remote and in the web configurator, with an Address field. Give it to an input’s Remote slot and your keys arrive.
The other jobs
Section titled “The other jobs”Every job has the same shape, called a port: your integration publishes its state, and r3cui sends it what the user asked for.
Method of Driver |
You publish | r3cui sends |
|---|---|---|
volume |
The level, the maximum, muted or not | Up, down, set, mute |
source |
The input list and the current input | Select an input |
power |
On or off | Turn on, turn off |
ui |
A Stage: what’s playing, a library, or the app in front |
Play or pause, seek, pick audio or subtitles |
voice |
What was heard and the answer | Start, audio, end or cancel |
Return None for a job you don’t do. That’s the default.
Other things the contract has:
- Pairing by a code that the device shows. Set
pairingin theSpecand implementpair. r3cui shows the keyboard and stores the credentials you return. - Targets: a list for the user to pick from, such as Home Assistant’s media players. Implement
targets. - Key light: a colour for the keys, from
key_light. - Images are URLs. r3cui fetches, scales and caches them.
Good examples to read
Section titled “Good examples to read”| Crate | Shows |
|---|---|
crates/kodi |
Keys and a screen: now playing and the library wall |
crates/androidtv |
Pairing by code, power, the app in front |
crates/homeassistant |
Global control, targets, voice |
The full contract is crates/sdk/src/api.rs.
Test it
Section titled “Test it”The existing integrations each have a fake device: a small server that runs inside the tests. Your tests talk to
the fake, so they need no real device. r3cui_sdk::ws has the WebSocket parts most clients need (a connection
that reconnects, pending calls) and r3cui_sdk::ws::testing has the base for a fake server.
cargo test -p myboxscripts/test.sh