# google-home/smarthome-washer architecture

> Architecture diagram of the GitHub repository google-home/smarthome-washer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/google-home/smarthome-washer
- Repository: https://github.com/google-home/smarthome-washer

## Overview

##### Project Type and Purpose
- This is a “codelab” full‑stack serverless web application that demonstrates integrating a smart washer device with Google Assistant using Firebase as the backend.
- It comes in two variants (starter and completed), each containing:
- A Firebase Functions directory for smart‑home intent fulfillment
- A public directory serving the web UI via Firebase Hosting
- Firebase configuration and security rules for a Realtime Database

##### High‑Level Architecture Components
a. Google Assistant / Actions on Google
b. Firebase Cloud Functions (fulfillment webhook)
c. Firebase Realtime Database (device state store, security rules)
d. Firebase Hosting (static web UI)
e. Web Client (public/main.js + index.html)
f. (Conceptual) Smart Washer Device

##### Component Responsibilities
- Google Assistant: receives user voice commands (“Hey Google, start the washer”) and routes Smart Home intents (SYNC, QUERY, EXECUTE) to your Cloud Function endpoint.
- Cloud Functions (washer‑done/functions/index.js):
- Implements the Google Smart Home fulfillment API
- Handles SYNC (device discovery), QUERY (status), EXECUTE (start/stop washer)
- Reads/writes device state in the Realtime Database
- Realtime Database (washer‑done/database.rules.json):
- Persists device attributes (on/off, progress, diagnostics)
- Enforces read/write security via Firebase rules
- Firebase Hosting (washer‑done/public):
- Serves static assets (index.html, main.js, main.css)
- Provides a web dashboard to manually view or send commands to the washer by writing to the database or calling Functions
- Web Client:
- Listens for DB state changes (via Firebase SDK)
- Pushes user‑initiated commands (start/stop) back into the database or invokes Cloud Functions directly

##### Interactions and Data Flow
1. User issues voice command → Google Assistant → Actions on Google cloud
2. Actions cloud invokes your Cloud Function webhook
3. Cloud Function reads/writes to Realtime Database
4. Database change triggers:
– Function returns status back to Assistant → user hears confirmation
– If web client is open, it receives real‑time updates via Firebase SDK

Separately:
5. User opens the web UI (hosted on Firebase Hosting)
6. Web UI loads static assets + Firebase SDK, subscribes to Realtime DB
7. Web UI shows device status and can send commands to DB or call Functions

##### Architectural Patterns and Principles
- Serverless backend: no self‑managed servers, uses Firebase Functions + Realtime DB
- Event‑driven: DB triggers UI updates and may trigger backend logic
- Separation of concerns:
- Hosting layer (static UI)
- Business logic layer (Functions)
- Data persistence layer (Realtime DB)
- Declarative security: all data access controlled via database.rules.json

##### Diagramming Guidance
– Google Assistant (cloud bubble)

- Control plane (Assistant & Functions): e.g., blue
- Data plane (Database & UI updates): green
- Presentation layer (Hosting & Web Client): orange

[ ] Every component is labeled (e.g., “Firebase Realtime Database”).
[ ] Important technologies (Firebase Functions, Hosting, Realtime DB, Actions on Google) are noted.
[ ] Interactions (SYNC, QUERY, EXECUTE) are annotated on the Assistant → Function path.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Voice Interface
    subgraph "External Voice Interface"
        GA["Google Assistant / Actions on Google"]:::control
    end

    %% Serverless Backend
    subgraph "Serverless Backend"
        subgraph "Cloud Functions"
            FunctionsDir["Functions Directory"]:::control
            PackageJSON["package.json"]:::control
            ESLint[".eslintrc.json"]:::control
            IndexJS["index.js"]:::control
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        DB["Firebase Realtime Database"]:::data
        SecRules["database.rules.json"]:::data
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        Hosting["Firebase Hosting"]:::presentation
        HostingConfig["firebase.json"]:::presentation
        subgraph "Web Client (Browser)"
            IndexHTML["index.html"]:::presentation
            MainJS["main.js"]:::presentation
            MainCSS["main.css"]:::presentation
        end
    end

    %% IoT Device
    subgraph "IoT Device"
        Device["Smart Washer Device"]:::device
    end

    %% Connections
    GA -->|"SYNC, QUERY, EXECUTE"| IndexJS
    GA -->|"SYNC, QUERY, EXECUTE"| FunctionsDir

    IndexJS -->|"read/write"| DB
    FunctionsDir -->|"read/write"| DB

    DB -->|"subscribe/updates"| MainJS
    MainJS -->|"write to DB"| DB
    MainJS -->|"invoke Functions"| IndexJS

    Hosting -->|"serves assets"| IndexHTML
    IndexHTML -->|"loads CSS/JS via HTTP(s)"| MainJS

    Device -->|"reports status"| DB
    Device -->|"receives commands"| DB

    %% Security rules link
    SecRules --- DB

    %% Click Events
    click IndexJS "https://github.com/google-home/smarthome-washer/blob/master/washer-done/functions/index.js"
    click FunctionsDir "https://github.com/google-home/smarthome-washer/tree/master/washer-done/functions/"
    click PackageJSON "https://github.com/google-home/smarthome-washer/blob/master/washer-done/functions/package.json"
    click ESLint "https://github.com/google-home/smarthome-washer/blob/master/washer-done/functions/.eslintrc.json"
    click SecRules "https://github.com/google-home/smarthome-washer/blob/master/washer-done/database.rules.json"
    click HostingConfig "https://github.com/google-home/smarthome-washer/blob/master/washer-done/firebase.json"
    click IndexHTML "https://github.com/google-home/smarthome-washer/blob/master/washer-done/public/index.html"
    click MainCSS "https://github.com/google-home/smarthome-washer/blob/master/washer-done/public/main.css"
    click MainJS "https://github.com/google-home/smarthome-washer/blob/master/washer-done/public/main.js"

    %% Styles
    classDef control fill:#D0E6FF,stroke:#4192D9,stroke-width:2px
    classDef data fill:#E2F7D4,stroke:#5CB85C,stroke-width:2px
    classDef presentation fill:#FFEDD9,stroke:#F0AD4E,stroke-width:2px
    classDef device fill:#F5F5F5,stroke:#AAAAAA,stroke-dasharray: 5 5,stroke-width:1px
```
