feat: add home assistant capability with pi-relay

This commit is contained in:
Jet 2026-03-23 22:45:49 -07:00
parent 2374e3cd60
commit 4f7ac0e7d7
No known key found for this signature in database
13 changed files with 415 additions and 35 deletions

16
pi/pi-relay/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "noisebell-relay"
version = "0.1.0"
edition = "2021"
[lints]
workspace = true
[dependencies]
anyhow = "1.0"
axum = "0.8"
noisebell-common = { path = "../../remote/noisebell-common" }
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "signal", "time"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

148
pi/pi-relay/src/main.rs Normal file
View file

@ -0,0 +1,148 @@
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Context, Result};
use axum::extract::State;
use axum::http::{HeaderMap, StatusCode};
use axum::routing::{get, post};
use axum::{Json, Router};
use noisebell_common::{validate_bearer, WebhookPayload};
use tracing::{error, info, warn};
#[derive(Clone)]
struct AppState {
client: reqwest::Client,
inbound_api_key: String,
target_url: String,
target_secret: Option<String>,
retry_attempts: u32,
retry_base_delay_secs: u64,
}
async fn post_webhook(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
Json(payload): Json<WebhookPayload>,
) -> StatusCode {
if !validate_bearer(&headers, &state.inbound_api_key) {
return StatusCode::UNAUTHORIZED;
}
info!(status = %payload.status, timestamp = payload.timestamp, "relay received webhook");
for attempt in 0..=state.retry_attempts {
let mut req = state.client.post(&state.target_url).json(&payload);
if let Some(secret) = &state.target_secret {
req = req.bearer_auth(secret);
}
match req.send().await {
Ok(resp) if resp.status().is_success() => {
info!(status = %payload.status, "relay forwarded webhook");
return StatusCode::OK;
}
result => {
let err_msg = match &result {
Ok(resp) => format!("HTTP {}", resp.status()),
Err(err) => err.to_string(),
};
if attempt == state.retry_attempts {
error!(error = %err_msg, "relay failed to forward webhook after {} attempts", state.retry_attempts + 1);
return StatusCode::BAD_GATEWAY;
}
let delay = Duration::from_secs(state.retry_base_delay_secs * 2u64.pow(attempt));
warn!(error = %err_msg, attempt = attempt + 1, "relay forward failed, retrying in {:?}", delay);
tokio::time::sleep(delay).await;
}
}
}
StatusCode::BAD_GATEWAY
}
async fn health() -> StatusCode {
StatusCode::OK
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let port: u16 = std::env::var("NOISEBELL_RELAY_PORT")
.unwrap_or_else(|_| "8090".into())
.parse()
.context("NOISEBELL_RELAY_PORT must be a valid u16")?;
let bind_address =
std::env::var("NOISEBELL_RELAY_BIND_ADDRESS").unwrap_or_else(|_| "0.0.0.0".into());
let inbound_api_key = std::env::var("NOISEBELL_RELAY_INBOUND_API_KEY")
.context("NOISEBELL_RELAY_INBOUND_API_KEY is required")?;
let target_base_url = std::env::var("NOISEBELL_RELAY_TARGET_BASE_URL")
.unwrap_or_else(|_| "http://homeassistant.local:8123".into())
.trim_end_matches('/')
.to_string();
let target_webhook_id = std::env::var("NOISEBELL_RELAY_TARGET_WEBHOOK_ID")
.context("NOISEBELL_RELAY_TARGET_WEBHOOK_ID is required")?;
let target_secret =
std::env::var("NOISEBELL_RELAY_TARGET_SECRET").ok().filter(|value| !value.is_empty());
let retry_attempts: u32 = std::env::var("NOISEBELL_RELAY_RETRY_ATTEMPTS")
.unwrap_or_else(|_| "3".into())
.parse()
.context("NOISEBELL_RELAY_RETRY_ATTEMPTS must be a valid u32")?;
let retry_base_delay_secs: u64 = std::env::var("NOISEBELL_RELAY_RETRY_BASE_DELAY_SECS")
.unwrap_or_else(|_| "1".into())
.parse()
.context("NOISEBELL_RELAY_RETRY_BASE_DELAY_SECS must be a valid u64")?;
let http_timeout_secs: u64 = std::env::var("NOISEBELL_RELAY_HTTP_TIMEOUT_SECS")
.unwrap_or_else(|_| "10".into())
.parse()
.context("NOISEBELL_RELAY_HTTP_TIMEOUT_SECS must be a valid u64")?;
let target_url = format!("{target_base_url}/api/webhook/{target_webhook_id}");
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(http_timeout_secs))
.build()
.context("failed to build HTTP client")?;
let state = Arc::new(AppState {
client,
inbound_api_key,
target_url,
target_secret,
retry_attempts,
retry_base_delay_secs,
});
let app = Router::new()
.route("/health", get(health))
.route("/webhook", post(post_webhook))
.with_state(state);
let listener = tokio::net::TcpListener::bind((&*bind_address, port))
.await
.context(format!("failed to bind to {bind_address}:{port}"))?;
info!(port, "relay listening");
let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.context("failed to register SIGTERM handler")?;
axum::serve(listener, app)
.with_graceful_shutdown(async move {
sigterm.recv().await;
})
.await
.context("relay server error")?;
Ok(())
}