# t0bst4r/home-assistant-matter-hub architecture

> Architecture diagram of the GitHub repository t0bst4r/home-assistant-matter-hub, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/t0bst4r/home-assistant-matter-hub
- Repository: https://github.com/t0bst4r/home-assistant-matter-hub

## Overview

##### Project type and purpose
- This is a full-stack, monorepo IoT integration platform (“Home-Assistant-Matter-Hub”) that simulates Matter bridges for Home Assistant entities.
- It exposes HA entities over the Matter protocol to local controllers (Apple Home, Google Home, Alexa) without port forwarding.

##### File structure
- Root
- pnpm/Nx monorepo config (`pnpm-workspace.yaml`, `nx.json`, `biome.json`)
- workspace-wide scripts and CI (`.github/workflows`, `utils/build`)
- apps/docker
- Two Dockerized deployment modes: “addon” (Home Assistant addon) and “standalone”
- Build scripts, Dockerfiles, entrypoint
- apps/home-assistant-matter-hub
- CLI package to scaffold or publish the HA addon
- packages/common
- Shared TypeScript definitions: clusters, schemas, device and HA entity types
- packages/backend
- Core Bridge Service (Node.js/TS)
- CLI entrypoint (`cli.ts`, `bootstrap.ts`, `start-command.ts`)
- API adapters: HTTP web-API (`web-api.ts`, `web-ui.ts`), Matter bridge service (`bridge-server-node.ts`), Home Assistant client (`home-assistant-client.ts`)
- Persistence: custom & legacy storage adapters, migrations
- Matter behaviors and device definitions (in `matter/behaviors` and `matter/devices`)
- packages/frontend
- React (Vite) single-page app to manage bridges and devices
- State management with Redux (slices for bridges/devices), API hooks, UI components
- packages/docs
- Static documentation site source

##### README
- Describes the core use case: publish HA entities as Matter devices on the local network.
- Points to online docs for installation and guides.

a. Main components
- External Systems: Home Assistant, Matter Controllers (e.g., Apple Home)
- Backend Service
- HTTP API layer
- Matter Bridge Service
- Home Assistant Client
- Storage & Migrations
- CLI/Bootstrap
- Frontend SPA
- Shared Library (common types & schemas)
- Deployment Artifacts: Docker images (addon & standalone)
b. Relationships & interactions
- Frontend ⇄ Backend HTTP API
- Backend ⇄ Home Assistant via REST/WebSocket client
- Backend ⇄ Matter Controllers via mDNS and Matter server node
- Backend ⇄ Storage (read/write configs & state, run migrations)
- CLI initiates Backend bootstrap (registers MDNS, loads behaviors/devices)
- Both frontend and backend import shared types from “common”
c. Architectural patterns
- Monorepo modularization (apps vs packages)
- Hexagonal/adaptor pattern in the backend (core logic decoupled from storage & network)
- Redux/Flux in the frontend for predictable state
- Docker for packaging two deployment modes
d. Key technologies
- Node.js + TypeScript, Vite, React
- Redux, React Query (via custom hooks)
- pnpm, Nx, Biome (lint/build)
- Docker, shell scripts, mDNS (zeroconf)

##### Detailed pointers
- Bootstrap sequence: CLI loads environment → runs start-handler → registers mDNS → instantiates bridge-service → syncs HA entities → exposes Matter endpoints.
- Matter Device Manager grouping multiple device behaviors and clusters.
- Storage migration pipeline under the storage adapter.
- Extension points: the `custom-behaviors` folder and the shared schemas for extensibility.
- Call out the Docker build steps (build.sh scripts) for the addon vs standalone modes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    subgraph "External Systems"
        HA["Home Assistant"]:::external
        Controllers["Matter Controllers"]:::external
    end

    %% Domain
    subgraph "Domain (Common Library)"
        Common["Shared Types & Schemas"]:::common
    end

    %% Backend Layer
    subgraph "Backend Layer"
        %% CLI / Bootstrap
        subgraph "CLI / Bootstrap" 
            CLI_CLI["cli.ts"]:::backend
            CLI_BS["bootstrap.ts"]:::backend
            CLI_SC["start-command.ts"]:::backend
            CLI_SH["start-handler.ts"]:::backend
        end

        %% API Layer
        subgraph "HTTP API Layer"
            API_Web["web-api.ts"]:::backend
            API_UI["web-ui.ts"]:::backend
            API_Matter["matter-api.ts"]:::backend
            API_Log["access-log.ts"]:::backend
            API_Proxy["proxy-support.ts"]:::backend
        end

        %% Business Logic
        subgraph "Matter Bridge Service"
            BServer["bridge-server-node.ts"]:::backend
            BService["bridge-service.ts"]:::backend
            BManager["bridge-device-manager.ts"]:::backend
        end

        subgraph "Home Assistant Client"
            HA_Client["home-assistant-client.ts"]:::backend
            HA_GetReg["get-registry.ts"]:::backend
            HA_Sub["subscribe-entities.ts"]:::backend
        end

        %% Persistence
        subgraph "Storage & Migrations"
            Storage_Bridge["bridge-storage.ts"]:::backend
            Storage_App["app-storage.ts"]:::backend
            Migration["v1-to-v2.ts, …"]:::backend
        end
    end

    %% Frontend Layer
    subgraph "Frontend SPA"
        Frontend["React SPA"]:::frontend
    end

    %% Deployment Artifacts
    subgraph "Docker Deployment"
        subgraph "Addon Image"
            Addon_Dockerfile["Dockerfile"]:::deployment
            Addon_build["build.sh"]:::deployment
            Addon_entry["docker-entrypoint.sh"]:::deployment
        end
        subgraph "Standalone Image"
            Standalone_Dockerfile["Dockerfile"]:::deployment
            Standalone_build["build.sh"]:::deployment
        end
    end

    %% Monorepo Config
    PNPM["pnpm-workspace.yaml"]:::deployment
    NX["nx.json"]:::deployment
    Biome["biome.json"]:::deployment

    %% Docs & CI
    Docs["packages/docs"]:::deployment
    CI[".github/workflows"]:::deployment
    UtilsBuild["utils/build/index.js"]:::deployment
    HA_Scaffold["apps/home-assistant-matter-hub"]:::deployment

    %% Connections
    Frontend -->|HTTP Requests| API_Web
    API_UI -->|Serves UI| Frontend
    API_Web -->|Invokes| BService
    BService -->|Uses| HA_Client
    BService -->|Announces mDNS| Controllers
    BService -->|Exposes Matter Endpoints| Controllers
    HA_Client -->|REST/WebSocket| HA
    BService -->|Reads/Writes| Storage_Bridge
    BService -->|Runs Migrations| Migration
    CLI_SC -->|Executes| BService
    CLI_SH -->|Initializes| BService
    CLI_BS -->|Bootstraps| CLI_SC
    CLI_CLI -->|Starts| CLI_BS
    Common --> API_Web
    Common --> BService
    Common --> HA_Client
    Common --> Storage_Bridge
    Common --> Frontend

    %% Click Events
    click PNPM "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/pnpm-workspace.yaml"
    click NX "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/nx.json"
    click Biome "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/biome.json"
    click CLI_CLI "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/cli.ts"
    click CLI_BS "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/bootstrap.ts"
    click CLI_SC "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/commands/start/start-command.ts"
    click CLI_SH "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/commands/start/start-handler.ts"
    click API_Web "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/api/web-api.ts"
    click API_UI "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/api/web-ui.ts"
    click API_Matter "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/api/matter-api.ts"
    click API_Log "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/api/access-log.ts"
    click API_Proxy "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/api/proxy-support.ts"
    click BServer "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/matter/bridge/bridge-server-node.ts"
    click BService "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/matter/bridge/bridge-service.ts"
    click BManager "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/matter/bridge/bridge-device-manager.ts"
    click HA_Client "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/home-assistant/home-assistant-client.ts"
    click HA_GetReg "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/home-assistant/api/get-registry.ts"
    click HA_Sub "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/home-assistant/api/subscribe-entities.ts"
    click Storage_Bridge "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/storage/bridge-storage.ts"
    click Storage_App "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/storage/app-storage.ts"
    click Migration "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/packages/backend/src/storage/migrations/bridge/v1-to-v2.ts"
    click Common "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/packages/common/src"
    click Frontend "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/packages/frontend/src"
    click Addon_Dockerfile "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/apps/docker/addon/Dockerfile"
    click Addon_build "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/apps/docker/addon/build.sh"
    click Addon_entry "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/apps/docker/addon/docker-entrypoint.sh"
    click Standalone_Dockerfile "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/apps/docker/standalone/Dockerfile"
    click Standalone_build "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/apps/docker/standalone/build.sh"
    click HA_Scaffold "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/apps/home-assistant-matter-hub"
    click Docs "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/packages/docs"
    click CI "https://github.com/t0bst4r/home-assistant-matter-hub/tree/main/.github/workflows"
    click UtilsBuild "https://github.com/t0bst4r/home-assistant-matter-hub/blob/main/utils/build/index.js"

    %% Styles
    classDef frontend fill:#blue,stroke:#000,color:#fff
    classDef backend fill:#2ecc71,stroke:#000,color:#fff
    classDef common fill:#bdc3c7,stroke:#000,color:#000
    classDef external fill:#e67e22,stroke:#000,color:#000
    classDef deployment fill:#f1c40f,stroke:#000,color:#000
```
