home-assistant, influxdb: full nox modules (ADR 0044/0046)

home-assistant: states/call-service/config tools, emits state.changed bounded
to actuator/contact domains (not attribute ticks), overridable via a watch
allowlist. influxdb: health/buckets/flux-query tools — tools-only, since a
time-series DB has no lifecycle event to emit here. Typecheck; manifests parse.
This commit is contained in:
2026-09-04 02:44:17 +02:00
parent eccfc70991
commit aca237b2e0
10 changed files with 430 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
// The Home Assistant API client — home-assistant's own code, living in the module (novox/hq
// ADR 0044). Both this module's tools and its events entrypoint import it, and nothing outside
// home-assistant does. Talks to the HA REST API (/api) with a long-lived access token.
export interface HAEntityState {
entity_id: string;
state: string;
attributes: Record<string, unknown>;
last_changed?: string;
last_updated?: string;
}
export interface HAConfig {
location_name?: string;
version?: string;
components?: string[];
time_zone?: string;
state?: string;
}
export class HomeAssistantClient {
readonly baseUrl: string;
constructor(
url: string,
private readonly token: string,
) {
this.baseUrl = url.replace(/\/$/, "");
}
/**
* Build from the module's resolved environment. The URL defaults to the local server (HA runs on
* the node); the token is the long-lived access token minted in HA's profile — required, since
* every API call is Bearer-authenticated and there is nowhere to discover it from.
*/
static fromEnv(env: NodeJS.ProcessEnv = process.env): HomeAssistantClient {
const url = env.MESH_HOMEASSISTANT_URL ?? `http://127.0.0.1:${env.HOMEASSISTANT_PORT ?? "8123"}`;
const token = env.MESH_HOMEASSISTANT_TOKEN;
if (!token) throw new Error("no Home Assistant token — set MESH_HOMEASSISTANT_TOKEN");
return new HomeAssistantClient(url, token);
}
private async request(path: string, init?: RequestInit): Promise<unknown> {
const res = await fetch(`${this.baseUrl}${path}`, {
...init,
headers: {
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json",
Accept: "application/json",
...(init?.headers ?? {}),
},
});
if (!res.ok) throw new Error(`Home Assistant API ${path}: ${res.status} ${await res.text()}`);
return res.json();
}
async getConfig(): Promise<HAConfig> {
return (await this.request("/api/config")) as HAConfig;
}
/** All entity states, or one entity when an id is given. */
async getStates(): Promise<HAEntityState[]> {
return (await this.request("/api/states")) as HAEntityState[];
}
async getState(entityId: string): Promise<HAEntityState> {
return (await this.request(`/api/states/${encodeURIComponent(entityId)}`)) as HAEntityState;
}
/** Call a service (e.g. switch.turn_on) — how "turn the light on" reaches HA. Returns the states
* the call changed. */
async callService(domain: string, service: string, data: Record<string, unknown> = {}): Promise<HAEntityState[]> {
return (await this.request(`/api/services/${encodeURIComponent(domain)}/${encodeURIComponent(service)}`, {
method: "POST",
body: JSON.stringify(data),
})) as HAEntityState[];
}
}
+66
View File
@@ -0,0 +1,66 @@
// home-assistant's events. The tool runtime imports this once the broker is bound. It watches the
// entities whose state changing is a real signal — a door opening, a lock turning, a switch
// flipping — and emits when one does.
//
// Emits (novox/hq ADR 0046/0047):
// module.home-assistant.state.changed — a watched entity's state value changed
//
// Bounded on purpose. Home Assistant has hundreds of entities and many (temperature, humidity,
// power draw) tick constantly; emitting every tick would be noise, not signal. So the watch is
// limited to actuator/contact domains where a change is an event a human would care about, and
// only the discrete `state` value is diffed — not the attribute bag. The set is overridable with
// MESH_HOMEASSISTANT_WATCH (comma-separated entity ids) for a node that wants a specific few.
import { emit } from "@novox/mesh-sdk/events";
import { HomeAssistantClient, type HAEntityState } from "./client.js";
const ha = HomeAssistantClient.fromEnv();
// Domains whose state changing is meaningful rather than a continuous reading.
const WATCH_DOMAINS = new Set(["binary_sensor", "lock", "cover", "switch", "input_boolean", "light", "alarm_control_panel"]);
// An explicit allowlist of entity ids, if the node set one; otherwise fall back to the domain filter.
const watchList = (process.env.MESH_HOMEASSISTANT_WATCH ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const watchSet = watchList.length ? new Set(watchList) : null;
function isWatched(s: HAEntityState): boolean {
if (watchSet) return watchSet.has(s.entity_id);
return WATCH_DOMAINS.has(s.entity_id.split(".")[0] ?? "");
}
const nameOf = (s: HAEntityState): string | undefined =>
typeof s.attributes.friendly_name === "string" ? s.attributes.friendly_name : undefined;
// Last seen state per watched entity. Primed silently on the first poll so a restart does not
// re-announce the current state of everything as a fresh change.
const lastState = new Map<string, string>();
let primed = false;
async function pollStates(): Promise<void> {
const states = (await ha.getStates()).filter(isWatched);
for (const s of states) {
const prev = lastState.get(s.entity_id);
if (primed && prev !== undefined && prev !== s.state) {
await emit("module.home-assistant.state.changed", {
entity: s.entity_id,
name: nameOf(s),
from: prev,
to: s.state,
});
}
lastState.set(s.entity_id, s.state);
}
primed = true;
}
const tick = (fn: () => Promise<void>, everyMs: number): void => {
const run = (): void => void fn().catch((err) => console.error(`[home-assistant] ${err}`));
setInterval(run, everyMs);
run();
};
tick(pollStates, 15_000);
console.log("[home-assistant] watching entity states, emitting on change");
+6
View File
@@ -4,6 +4,12 @@
"capabilities": [
"container-runtime"
],
"emits": [
"module.home-assistant.state.changed"
],
"own-secrets": {
"broker": "/var/lib/home-assistant/broker"
},
"listens": [
{
"port": 8123,
+14
View File
@@ -0,0 +1,14 @@
{
"name": "@novox/module-home-assistant",
"version": "0.1.0",
"description": "home-assistant — home automation platform. Its API client, tools and events live here (novox/hq ADR 0044).",
"type": "module",
"private": true,
"dependencies": {
"@novox/mesh-sdk": "^0.1.0"
},
"devDependencies": {
"@types/node": "^22.0.0",
"typescript": "^5.6.0"
}
}
+76
View File
@@ -0,0 +1,76 @@
// home-assistant's tools — its own code (novox/hq ADR 0044), importing its own client. They return
// structured data; the mesh serves them through the sdk's tool harness.
import { registerModuleTools, type ToolDefinition } from "@novox/mesh-sdk/tools";
import { HomeAssistantClient, type HAEntityState } from "../client.js";
/** Trim an entity to the fields worth returning — the full attribute bag is large and mostly noise. */
function summarize(s: HAEntityState): { entity_id: string; state: string; name?: string; last_changed?: string } {
return {
entity_id: s.entity_id,
state: s.state,
name: typeof s.attributes.friendly_name === "string" ? s.attributes.friendly_name : undefined,
last_changed: s.last_changed,
};
}
export function getHomeAssistantTools(ha: HomeAssistantClient): ToolDefinition[] {
return [
{
name: "homeassistant_states",
description:
"Entity states from Home Assistant. With no argument, lists every entity; with `entity` (e.g. light.kitchen), returns just that one with its full attributes.",
input: { entity: { type: "string", description: "an entity id to fetch one entity; omitted lists all" } },
run: async (args) => {
if (args.entity) {
const s = await ha.getState(String(args.entity));
return { entity_id: s.entity_id, state: s.state, attributes: s.attributes, last_changed: s.last_changed };
}
const states = await ha.getStates();
return { count: states.length, entities: states.map(summarize) };
},
},
{
name: "homeassistant_call_service",
description:
"Call a Home Assistant service — turn a switch/light on or off, lock a door, etc. Give the domain (e.g. switch), the service (e.g. turn_on), and optionally a target entity and extra data.",
input: {
domain: { type: "string", description: "the service domain, e.g. light, switch, lock" },
service: { type: "string", description: "the service, e.g. turn_on, turn_off, toggle" },
entity: { type: "string", description: "the entity id to target (optional)" },
data: { type: "object", description: "extra service data merged into the call (optional)" },
},
run: async (args) => {
const data: Record<string, unknown> = { ...(args.data as Record<string, unknown> | undefined) };
if (args.entity) data.entity_id = String(args.entity);
const changed = await ha.callService(String(args.domain), String(args.service), data);
return { changed: changed.map(summarize) };
},
},
{
name: "homeassistant_config",
description: "Home Assistant instance config: version, location, timezone, loaded components.",
input: {},
run: async () => {
const c = await ha.getConfig();
return {
location: c.location_name,
version: c.version,
time_zone: c.time_zone,
state: c.state,
components: c.components?.length ?? 0,
};
},
},
];
}
// The tools exist only when a token is configured; without one, home-assistant contributes none
// rather than failing the whole runtime.
registerModuleTools("home-assistant", (env) => {
try {
return getHomeAssistantTools(HomeAssistantClient.fromEnv(env));
} catch {
return [];
}
});
+12
View File
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"noEmit": true
},
"include": ["client.ts", "index.ts", "tools/index.ts"]
}