# danieyal/ha-mytnb architecture

> Architecture diagram of the GitHub repository danieyal/ha-mytnb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/danieyal/ha-mytnb
- Repository: https://github.com/danieyal/ha-mytnb

## Overview

This Home Assistant integration discovers linked TNB accounts during setup, then periodically fetches usage, billing, payment, and balance data. The coordinator retries transient failures and supplies per-account sensors, while daily readings are imported as long-term recorder statistics. The README also documents rich sensor attributes and account reconfiguration. Arrows reflect the shown calls and data reads; the TNB service is external, and its API internals are not part of this repository.

Main entry points: [`custom_components/mytnb/coordinator.py`](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/coordinator.py) (Account data polling), [`custom_components/mytnb/__init__.py`](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/__init__.py) (Integration lifecycle), [`custom_components/mytnb/config_flow.py`](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/config_flow.py) (Login and account selection), [`custom_components/mytnb/statistics.py`](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/statistics.py) (Daily statistics import)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_onboarding["Account setup"]
  node_config_flow["Login and account selection<br/>[config_flow.py]"]
  node_setup["Integration lifecycle<br/>[__init__.py]"]
end

subgraph group_runtime["Polling runtime"]
  node_constants["Integration settings<br/>[const.py]"]
  node_coordinator["Account data polling<br/>[coordinator.py]"]
  node_retry["Transient retry policy<br/>[retry.py]"]
end

subgraph group_entities["Account sensors"]
  node_sensors["Usage and billing sensors<br/>[sensor.py]"]
end

subgraph group_history["Usage history"]
  node_statistics["Daily statistics import<br/>[statistics.py]"]
end

node_user(("Home Assistant user"))
node_tnb_api["myTNB service"]
node_recorder[("Home Assistant recorder")]

node_user -->|"configures"| node_config_flow
node_config_flow -->|"validates and discovers"| node_tnb_api
node_setup -->|"reuses validation"| node_config_flow
node_setup -->|"creates and refreshes"| node_coordinator
node_setup -->|"forwards platform setup"| node_sensors
node_coordinator -->|"logs in and fetches account data"| node_tnb_api
node_coordinator -->|"applies retry policy"| node_retry
node_coordinator -->|"backfills daily readings"| node_statistics
node_coordinator -->|"provides account data"| node_sensors
node_statistics -->|"reads and imports statistics"| node_recorder

click node_config_flow "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/config_flow.py"
click node_setup "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/__init__.py"
click node_constants "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/const.py"
click node_coordinator "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/coordinator.py"
click node_retry "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/retry.py"
click node_sensors "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/sensor.py"
click node_statistics "https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/statistics.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_config_flow,node_setup,node_user toneBlue
class node_constants,node_coordinator,node_retry,node_recorder toneAmber
class node_sensors toneMint
class node_statistics toneRose
class node_tnb_api toneIndigo
```

## Components

### Account setup

- [Login and account selection](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/config_flow.py): `custom_components/mytnb/config_flow.py`
- [Integration lifecycle](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/__init__.py): `custom_components/mytnb/__init__.py`

### Polling runtime

- [Integration settings](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/const.py): `custom_components/mytnb/const.py`
- [Account data polling](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/coordinator.py): `custom_components/mytnb/coordinator.py`
- [Transient retry policy](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/retry.py): `custom_components/mytnb/retry.py`

### Account sensors

- [Usage and billing sensors](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/sensor.py): `custom_components/mytnb/sensor.py`

### Usage history

- [Daily statistics import](https://github.com/danieyal/ha-mytnb/blob/master/custom_components/mytnb/statistics.py): `custom_components/mytnb/statistics.py`

### Other components

- **Home Assistant user**
- **myTNB service**
- **Home Assistant recorder**

## Connections

- Home Assistant user → Login and account selection: configures (evidence: `README.md`)
- Login and account selection → myTNB service: validates and discovers (evidence: `custom_components/mytnb/config_flow.py`)
- Integration lifecycle → Login and account selection: reuses validation (evidence: `custom_components/mytnb/__init__.py`)
- Integration lifecycle → Account data polling: creates and refreshes (evidence: `custom_components/mytnb/__init__.py`)
- Integration lifecycle → Usage and billing sensors: forwards platform setup (evidence: `custom_components/mytnb/__init__.py`)
- Account data polling → myTNB service: logs in and fetches account data (evidence: `custom_components/mytnb/coordinator.py`)
- Account data polling → Transient retry policy: applies retry policy (evidence: `custom_components/mytnb/coordinator.py`)
- Account data polling → Daily statistics import: backfills daily readings (evidence: `custom_components/mytnb/coordinator.py`)
- Account data polling → Usage and billing sensors: provides account data (evidence: `custom_components/mytnb/sensor.py`)
- Daily statistics import → Home Assistant recorder: reads and imports statistics (evidence: `custom_components/mytnb/statistics.py`)
