# jbergler/hass-ttlock architecture

> Architecture diagram of the GitHub repository jbergler/hass-ttlock, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jbergler/hass-ttlock
- Repository: https://github.com/jbergler/hass-ttlock

## Overview

##### Project Type and Purpose
- This is a Home Assistant integration that acts as a bridge between Home Assistant and TTLock-based smart locks.
- Its purpose is to support locking/unlocking, sensor updates (battery levels, last operator, etc.), and management functions (pass codes, history tracking) through the TTLock Cloud API.
- The README confirms that it communicates with TTLock Cloud (via OAuth credentials and webhooks) to trigger commands and receive real-time updates.

##### Analyzing the File Structure
- The integration code is housed in the “custom_components/ttlock” directory. Key modules include:
- api.py – for handling communication with the TTLock Cloud API.
- config_flow.py – managing the integration configuration within Home Assistant.
- coordinator.py and models.py – to manage data synchronization and domain models.
- Entity modules (lock.py, sensor.py, binary_sensor.py, switch.py) – abstraction for Home Assistant entities.
- services.py and their YAML/alike definitions – to define extra capabilities like adding or deleting pass codes.
- The project also includes tests (in the “tests” directory) to validate various components.
- Additional files like pyproject.toml, requirements.txt, and GitHub actions/workflows indicate standard Python project setup along with continuous integration practices.
- Notice the presence of localization files (translations) and manifest.json which are essential in Home Assistant integrations for discovery and metadata.

##### System Components
a. Home Assistant Platform
- It as the central environment where the integration runs.
b. hass-ttlock Integration (Custom Component)
- API Module: Handles all HTTP/OAuth interactions with TTLock Cloud.
- Entities: Lock, sensor, binary_sensor, switch – each mapped to the smart lock’s functionalities.
- Coordinator & Models: Manage state updates and data transformation.
- Config Flow & Application Credentials: For initial setup and configuration.
c. TTLock Cloud
- It as an external service that processes API requests, sends back responses, and triggers webhooks.
d. Webhook Component
- Webhook interface (callback URL setup) which receives real-time updates from TTLock Cloud.
e. User Interaction (Configuration & Monitoring)
- Although not a runtime component, include a node for Home Assistant’s user interface where configuration is done (OAuth credentials input, lock settings, etc.).

##### Relationships and Data Flow
- Flow of configuration data and authentication tokens (via the API module) between Home Assistant and TTLock Cloud.
- Interactions between internal modules. For instance:
- The Config Flow collects OAuth credentials, which are used by the API module.
- The API module interacts with the Coordinator to update entity states, which then are reflected as UI components/sensors.

##### Architectural Patterns & Design Principles
- Modularity: The integration is built as a collection of well-defined modules (API, Entities, Config Flow, etc.), making it extendable and maintainable.
- Separation of Concerns: The API communication is separated from entity definitions, which in turn are abstracted away from the Home Assistant configuration UI.
- Event-driven Architecture: The integration uses webhooks to react to real-time events from TTLock Cloud, reducing battery waste by eliminating periodic polling.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% External Components
    HA["Home Assistant Environment"]:::external
    UI["User Interface (Config Flow)"]:::external
    TT["TTLock Cloud"]:::external

    %% Integration Subgraph
    subgraph "hass-ttlock Integration"
        IH["Integration (hass-ttlock)"]:::integration
        CF["Config Flow & Application Credentials"]:::integration
        API["API Module"]:::integration
        CO["Coordinator & Domain Models"]:::integration
        SR["Services (Passcode Management)"]:::integration
        LM["Localization & Metadata"]:::integration
        WH["Webhook Receiver"]:::integration
        
        subgraph "Entities"
            LK["Lock Entity"]:::entity
            SS["Sensor Entity"]:::entity
            BS["Binary Sensor Entity"]:::entity
            SW["Switch Entity"]:::entity
        end
    end

    %% Relationships and Data Flow
    HA -->|"runs"| IH
    UI -->|"configures"| CF
    CF -->|"provides_OAuth"| API
    API -->|"sends_API_calls"| TT
    TT -->|"webhook_callbacks"| WH
    API -->|"updates_state"| CO
    CO -->|"syncs_to"| LK
    CO -->|"syncs_to"| SS
    CO -->|"syncs_to"| BS
    CO -->|"syncs_to"| SW
    SR -->|"enhances"| LK
    SR -->|"enhances"| SS
    SR -->|"enhances"| BS
    SR -->|"enhances"| SW
    LM -->|"supplies_metadata"| CF
    LM -->|"supplies_metadata"| API

    %% Click Events Mapping
    click IH "https://github.com/jbergler/hass-ttlock/tree/develop/custom_components/ttlock"
    click API "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/api.py"
    click CF "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/config_flow.py"
    click CF "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/application_credentials.py"
    click LK "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/lock.py"
    click SS "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/sensor.py"
    click BS "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/binary_sensor.py"
    click SW "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/switch.py"
    click CO "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/coordinator.py"
    click CO "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/models.py"
    click SR "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/services.py"
    click SR "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/services.yaml"
    click LM "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/manifest.json"
    click LM "https://github.com/jbergler/hass-ttlock/tree/develop/custom_components/ttlock/translations"
    click WH "https://github.com/jbergler/hass-ttlock/blob/develop/custom_components/ttlock/diagnostics.py"

    %% Class Definitions for Styling
    classDef integration fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef external fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef entity fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
```
