@nexgen-os/homeassistant 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nexgen OS contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,36 @@
1
- # Temporary Holding Version
1
+ # `@nexgen-os/homeassistant` — Home Assistant ⇄ plain shapes
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Reads what a Home Assistant instance exports — its floors, areas, devices,
4
+ entities and the states its recorder keeps — into plain shapes any tool can
5
+ use, and writes the shapes back, with no field lost. MIT; no dependency.
6
+
7
+ | Home Assistant | Shape | Notes |
8
+ |---|---|---|
9
+ | floor | `HomeSpace`, kind `floor` | `level` as the file says it, or null |
10
+ | area | `HomeSpace`, kind `room` | `parent` = its floor, or null |
11
+ | device | `HomeDevice` | `name` = the name a person gave it, else its own; manufacturer, model, serial number; `space` = its area |
12
+ | entity | `HomePoint` | `device`, `space`; `deviceClass` and `unit` as stated; `role` from the domain (`number`, `climate`, `water_heater` → `setpoint`; `switch`, `light`, `cover`, `button`… → `command`; the rest → `measured`); `valueKind` from the state class (`total`, `total_increasing` → `cumulative`, else `instant`); `binary` for on/off domains |
13
+ | state | `HomeReading` | `on`/`off` → 1/0; `unavailable`, `unknown` or not a number → `null` |
14
+
15
+ Every field a shape has no field for (icons, aliases, identifiers,
16
+ connections, versions, `via_device_id`, `unique_id`, `platform`,
17
+ `entity_category`, `disabled_by`, original names and classes) travels in
18
+ its `extra`, so that `writeHomeAssistant(readHomeAssistant(s))` deep-equals
19
+ `s`. The one thing the round trip flattens: `unknown` comes back as
20
+ `unavailable`.
21
+
22
+ ```ts
23
+ import { readHomeAssistant, writeHomeAssistant } from '@nexgen-os/homeassistant';
24
+
25
+ const home = readHomeAssistant(snapshot); // { spaces, devices, points }
26
+ const again = writeHomeAssistant(home); // deep-equals snapshot
27
+ ```
28
+
29
+ What it never does: convert a unit, guess a quantity, open a connection or
30
+ call Home Assistant. Into Nexgen OS, `importHomeAssistant()` of the `building`
31
+ module (`modules/building/src`) writes the shapes for a participant — spaces
32
+ of its building through `building.add_space()`, devices under the
33
+ participant through `install_device()`, their area kept in their metadata,
34
+ one series per point
35
+ through `series_ensure()`, the readings through `store()` converted by the
36
+ `units` vocabulary — and `exportHomeAssistant()` reads them back.
@@ -0,0 +1,119 @@
1
+ export interface HaFloor {
2
+ floor_id: string;
3
+ name: string;
4
+ level: number | null;
5
+ icon: string | null;
6
+ aliases: string[];
7
+ }
8
+ export interface HaArea {
9
+ area_id: string;
10
+ name: string;
11
+ floor_id: string | null;
12
+ icon: string | null;
13
+ aliases: string[];
14
+ }
15
+ export interface HaDevice {
16
+ id: string;
17
+ name: string | null;
18
+ name_by_user: string | null;
19
+ manufacturer: string | null;
20
+ model: string | null;
21
+ sw_version: string | null;
22
+ hw_version: string | null;
23
+ serial_number: string | null;
24
+ area_id: string | null;
25
+ via_device_id: string | null;
26
+ identifiers: [string, string][];
27
+ connections: [string, string][];
28
+ }
29
+ export interface HaEntity {
30
+ entity_id: string;
31
+ unique_id: string;
32
+ platform: string;
33
+ name: string | null;
34
+ original_name: string | null;
35
+ device_id: string | null;
36
+ area_id: string | null;
37
+ device_class: string | null;
38
+ original_device_class: string | null;
39
+ state_class: string | null;
40
+ unit_of_measurement: string | null;
41
+ icon: string | null;
42
+ entity_category: string | null;
43
+ disabled_by: string | null;
44
+ }
45
+ export interface HaState {
46
+ entity_id: string;
47
+ state: string;
48
+ last_updated: string;
49
+ }
50
+ /** What `hass` exports: its registries and the recorder's states. */
51
+ export interface HaSnapshot {
52
+ floors: HaFloor[];
53
+ areas: HaArea[];
54
+ devices: HaDevice[];
55
+ entities: HaEntity[];
56
+ states: HaState[];
57
+ }
58
+ /** A floor or a room. `key` is Home Assistant's id (`floor_id`, `area_id`). */
59
+ export interface HomeSpace {
60
+ key: string;
61
+ kind: 'floor' | 'room';
62
+ name: string;
63
+ /** The floor's level as the file says it; null for a room, or a floor without one. */
64
+ level: number | null;
65
+ /** The floor a room is on, by key; null for a floor, or a room on no floor. */
66
+ parent: string | null;
67
+ /** The fields of the floor or the area no other field carries (`icon`, `aliases`). */
68
+ extra: Record<string, unknown>;
69
+ }
70
+ /** A device. `key` is Home Assistant's device id. */
71
+ export interface HomeDevice {
72
+ key: string;
73
+ /** The name a person gave it, else its own, else its key. */
74
+ name: string;
75
+ manufacturer: string | null;
76
+ model: string | null;
77
+ serialNumber: string | null;
78
+ /** The room it is in, by key. */
79
+ space: string | null;
80
+ /** `name`, `name_by_user`, versions, `via_device_id`, identifiers and connections, as the registry holds them. */
81
+ extra: Record<string, unknown>;
82
+ }
83
+ /** One reading of a point: `value` null when the state was unavailable, unknown or not a number. */
84
+ export interface HomeReading {
85
+ at: string;
86
+ value: number | null;
87
+ }
88
+ /** An entity: a point read or commanded, with its readings. `key` is the entity id. */
89
+ export interface HomePoint {
90
+ key: string;
91
+ /** The device it belongs to, by key. */
92
+ device: string | null;
93
+ /** The room it was placed in, by key (its device's room otherwise). */
94
+ space: string | null;
95
+ /** The part of the entity id before the dot: `sensor`, `switch`, `climate`… */
96
+ domain: string;
97
+ /** `device_class`, else `original_device_class`, else null. */
98
+ deviceClass: string | null;
99
+ /** `unit_of_measurement`, as the entity states it. */
100
+ unit: string | null;
101
+ /** What the point is: read (`measured`), a value asked of the device (`setpoint`), an order (`command`). */
102
+ role: 'measured' | 'setpoint' | 'command';
103
+ /** `cumulative` for a total (`total`, `total_increasing`), else `instant`. */
104
+ valueKind: 'instant' | 'cumulative';
105
+ /** Whether its states are `on` and `off`. */
106
+ binary: boolean;
107
+ /** The fields of the entity no other field carries. */
108
+ extra: Record<string, unknown>;
109
+ readings: HomeReading[];
110
+ }
111
+ export interface Home {
112
+ spaces: HomeSpace[];
113
+ devices: HomeDevice[];
114
+ points: HomePoint[];
115
+ }
116
+ /** A snapshot as plain shapes. */
117
+ export declare function readHomeAssistant(snapshot: HaSnapshot): Home;
118
+ /** Plain shapes as a snapshot, in the order of the shapes. */
119
+ export declare function writeHomeAssistant(home: Home): HaSnapshot;
package/dist/index.js ADDED
@@ -0,0 +1,93 @@
1
+ // Home Assistant ⇄ plain shapes.
2
+ //
3
+ // Home Assistant describes a home as floors, areas, devices and entities,
4
+ // and keeps the states of its entities over time. This library reads such a
5
+ // snapshot into plain shapes any tool can use — spaces (floors and rooms,
6
+ // one tree), devices, and points with their readings — and writes the
7
+ // shapes back as a snapshot, without losing a field:
8
+ //
9
+ // floor → a space of kind floor (level as the file says it, or null)
10
+ // area → a space of kind room (under its floor, or at the top)
11
+ // device → a device (in its area)
12
+ // entity → a point (on its device and in its area; its
13
+ // class and unit as the file says them;
14
+ // its role from its domain, measured,
15
+ // setpoint or command; instant or
16
+ // cumulative from its state class)
17
+ // state → a reading (on/off as 1/0; unavailable, unknown
18
+ // or not a number as null)
19
+ //
20
+ // Every Home Assistant field a shape has no field for travels in its
21
+ // `extra`, so that `writeHomeAssistant(readHomeAssistant(s))` gives the
22
+ // snapshot back as it was. The one thing the round trip flattens: `unknown`
23
+ // comes back as `unavailable`. No unit is converted and no value invented:
24
+ // the library carries what the snapshot says. It imports nothing, opens no
25
+ // connection and calls no network.
26
+ //
27
+ // SPDX-License-Identifier: MIT
28
+ const SETPOINT_DOMAINS = new Set(['number', 'input_number', 'climate', 'water_heater']);
29
+ const COMMAND_DOMAINS = new Set(['switch', 'button', 'input_boolean', 'light', 'cover', 'fan', 'lock', 'script']);
30
+ const BINARY_DOMAINS = new Set(['binary_sensor', 'switch', 'input_boolean', 'light', 'lock']);
31
+ const domainOf = (entityId) => entityId.split('.')[0];
32
+ function roleOf(domain) {
33
+ if (SETPOINT_DOMAINS.has(domain))
34
+ return 'setpoint';
35
+ if (COMMAND_DOMAINS.has(domain))
36
+ return 'command';
37
+ return 'measured';
38
+ }
39
+ function reading(state) {
40
+ const n = state.state === 'on' ? 1 : state.state === 'off' ? 0 : Number(state.state);
41
+ const missing = state.state === 'unavailable' || state.state === 'unknown' || state.state.trim() === '' || Number.isNaN(n);
42
+ return { at: state.last_updated, value: missing ? null : n };
43
+ }
44
+ /** A snapshot as plain shapes. */
45
+ export function readHomeAssistant(snapshot) {
46
+ const spaces = [
47
+ ...snapshot.floors.map(({ floor_id, name, level, ...extra }) => ({ key: floor_id, kind: 'floor', name, level, parent: null, extra })),
48
+ ...snapshot.areas.map(({ area_id, name, floor_id, ...extra }) => ({ key: area_id, kind: 'room', name, level: null, parent: floor_id, extra })),
49
+ ];
50
+ const devices = snapshot.devices.map(({ id, manufacturer, model, serial_number, area_id, ...extra }) => ({
51
+ key: id, name: extra.name_by_user ?? extra.name ?? id, manufacturer, model, serialNumber: serial_number, space: area_id, extra,
52
+ }));
53
+ const readings = new Map();
54
+ for (const s of snapshot.states)
55
+ readings.set(s.entity_id, [...(readings.get(s.entity_id) ?? []), reading(s)]);
56
+ const points = snapshot.entities.map(({ entity_id, device_id, area_id, unit_of_measurement, ...extra }) => {
57
+ const domain = domainOf(entity_id);
58
+ return {
59
+ key: entity_id,
60
+ device: device_id,
61
+ space: area_id,
62
+ domain,
63
+ deviceClass: extra.device_class ?? extra.original_device_class ?? null,
64
+ unit: unit_of_measurement,
65
+ role: roleOf(domain),
66
+ valueKind: extra.state_class === 'total' || extra.state_class === 'total_increasing' ? 'cumulative' : 'instant',
67
+ binary: BINARY_DOMAINS.has(domain),
68
+ extra,
69
+ readings: readings.get(entity_id) ?? [],
70
+ };
71
+ });
72
+ return { spaces, devices, points };
73
+ }
74
+ /** Twelve significant digits: what survives a conversion there and back. */
75
+ const round = (n) => Number(n.toPrecision(12));
76
+ /** Plain shapes as a snapshot, in the order of the shapes. */
77
+ export function writeHomeAssistant(home) {
78
+ return {
79
+ floors: home.spaces.filter((s) => s.kind === 'floor').map((s) => ({ ...s.extra, floor_id: s.key, name: s.name, level: s.level })),
80
+ areas: home.spaces.filter((s) => s.kind === 'room').map((s) => ({ ...s.extra, area_id: s.key, name: s.name, floor_id: s.parent })),
81
+ devices: home.devices.map((d) => ({
82
+ ...d.extra, id: d.key, manufacturer: d.manufacturer, model: d.model, serial_number: d.serialNumber, area_id: d.space,
83
+ })),
84
+ entities: home.points.map((p) => ({
85
+ ...p.extra, entity_id: p.key, device_id: p.device, area_id: p.space, unit_of_measurement: p.unit,
86
+ })),
87
+ states: home.points.flatMap((p) => p.readings.map((r) => ({
88
+ entity_id: p.key,
89
+ state: r.value === null ? 'unavailable' : p.binary ? (r.value >= 1 ? 'on' : 'off') : String(round(r.value)),
90
+ last_updated: new Date(r.at).toISOString(),
91
+ }))),
92
+ };
93
+ }
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@nexgen-os/homeassistant",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0",
4
+ "description": "Home Assistant floors, areas, devices, entities and recorder states read into plain shapes and written back, with no field lost. No database, no dependency.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nexgen-os/nexgen-os.git",
9
+ "directory": "packages/formats/homeassistant"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "engines": {
16
+ "node": ">=20"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ }
30
+ },
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.build.json"
33
+ }
34
+ }