# rairulyle/meralco-ph architecture

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

- Interactive diagram: https://gitdiagram.com/rairulyle/meralco-ph
- Repository: https://github.com/rairulyle/meralco-ph

## Overview

MERALCO PH fetches MERALCO’s monthly residential-bill PDFs, derives rates and month-over-month changes, then serves them through REST endpoints or Home Assistant MQTT sensors. The REST API caches fetched results; the add-on entry point selects REST or MQTT operation and, in MQTT mode, publishes rates and discovery data. Source excerpts show the core calls; the README documents the API and add-on capabilities. Some add-on loop details and parser internals were not sampled, so those relationships are not expanded.

Main entry points: [`src/addon_main.py`](https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py) (Mode dispatcher), [`src/parser.py`](https://github.com/rairulyle/meralco-ph/blob/main/src/parser.py) (PDF rate parser), [`src/api.py`](https://github.com/rairulyle/meralco-ph/blob/main/src/api.py) (Rates and health routes), [`src/mqtt_bridge.py`](https://github.com/rairulyle/meralco-ph/blob/main/src/mqtt_bridge.py) (MQTT sensor bridge)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_rates["Rate processing"]
  node_pdf_rates["PDF rate parser<br/>[parser.py]"]
  node_rate_changes["Monthly comparisons<br/>[parser.py]"]
end

subgraph group_rest["REST API"]
  node_api_routes["Rates and health routes<br/>[api.py]"]
  node_api_cache["Monthly result cache<br/>[api.py]"]
end

subgraph group_addon["Add-on runtime"]
  node_addon_config["Add-on configuration<br/>[addon_main.py]"]
  node_addon_dispatch["Mode dispatcher<br/>[addon_main.py]"]
  node_mqtt_cycle["Rate publish cycle<br/>[addon_main.py]"]
  node_supervisor["Supervisor MQTT lookup<br/>[addon_main.py]"]
end

subgraph group_mqtt["MQTT integration"]
  node_mqtt_bridge["MQTT sensor bridge<br/>[mqtt_bridge.py]"]
end

node_api_user(("API client"))
node_ha_user(("Home Assistant user"))
node_meralco["MERALCO PDF service"]
node_broker["MQTT broker"]
node_ha_mqtt["Home Assistant MQTT"]

node_api_user -->|"requests rates"| node_api_routes
node_api_routes -->|"gets cached data"| node_api_cache
node_api_cache -->|"fetches rates"| node_pdf_rates
node_pdf_rates -->|"downloads PDFs"| node_meralco
node_pdf_rates -->|"compares months"| node_rate_changes
node_ha_user -->|"configures add-on"| node_addon_config
node_addon_config -->|"selects mode"| node_addon_dispatch
node_addon_dispatch -->|"starts REST mode"| node_api_routes
node_addon_dispatch -->|"runs MQTT mode"| node_mqtt_cycle
node_mqtt_cycle -->|"fetches rates"| node_pdf_rates
node_mqtt_cycle -->|"publishes rates"| node_mqtt_bridge
node_addon_dispatch -->|"looks up broker"| node_supervisor
node_supervisor -->|"retrieves credentials"| node_broker
node_mqtt_bridge -->|"publishes MQTT"| node_broker
node_broker -->|"delivers discovery"| node_ha_mqtt

click node_pdf_rates "https://github.com/rairulyle/meralco-ph/blob/main/src/parser.py"
click node_rate_changes "https://github.com/rairulyle/meralco-ph/blob/main/src/parser.py"
click node_api_routes "https://github.com/rairulyle/meralco-ph/blob/main/src/api.py"
click node_api_cache "https://github.com/rairulyle/meralco-ph/blob/main/src/api.py"
click node_addon_config "https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py"
click node_addon_dispatch "https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py"
click node_mqtt_cycle "https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py"
click node_supervisor "https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py"
click node_mqtt_bridge "https://github.com/rairulyle/meralco-ph/blob/main/src/mqtt_bridge.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_pdf_rates,node_rate_changes,node_api_user,node_ha_user toneBlue
class node_api_routes,node_api_cache toneAmber
class node_addon_config,node_addon_dispatch,node_mqtt_cycle,node_supervisor toneMint
class node_mqtt_bridge toneRose
class node_meralco,node_broker,node_ha_mqtt toneIndigo
```

## Components

### Rate processing

- [PDF rate parser](https://github.com/rairulyle/meralco-ph/blob/main/src/parser.py): `src/parser.py`
- [Monthly comparisons](https://github.com/rairulyle/meralco-ph/blob/main/src/parser.py): `src/parser.py`

### REST API

- [Rates and health routes](https://github.com/rairulyle/meralco-ph/blob/main/src/api.py): `src/api.py`
- [Monthly result cache](https://github.com/rairulyle/meralco-ph/blob/main/src/api.py): `src/api.py`

### Add-on runtime

- [Add-on configuration](https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py): `src/addon_main.py`
- [Mode dispatcher](https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py): `src/addon_main.py`
- [Rate publish cycle](https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py): `src/addon_main.py`
- [Supervisor MQTT lookup](https://github.com/rairulyle/meralco-ph/blob/main/src/addon_main.py): `src/addon_main.py`

### MQTT integration

- [MQTT sensor bridge](https://github.com/rairulyle/meralco-ph/blob/main/src/mqtt_bridge.py): `src/mqtt_bridge.py`

### Other components

- **API client**
- **Home Assistant user**
- **MERALCO PDF service**
- **MQTT broker**
- **Home Assistant MQTT**

## Connections

- API client → Rates and health routes: requests rates (evidence: `README.md`)
- Rates and health routes → Monthly result cache: gets cached data (evidence: `src/api.py`)
- Monthly result cache → PDF rate parser: fetches rates (evidence: `src/api.py`)
- PDF rate parser → MERALCO PDF service: downloads PDFs (evidence: `src/parser.py`)
- PDF rate parser → Monthly comparisons: compares months (evidence: `src/parser.py`)
- Home Assistant user → Add-on configuration: configures add-on (evidence: `README.md`)
- Add-on configuration → Mode dispatcher: selects mode (evidence: `src/addon_main.py`)
- Mode dispatcher → Rates and health routes: starts REST mode (evidence: `src/addon_main.py`)
- Mode dispatcher → Rate publish cycle: runs MQTT mode (evidence: `src/addon_main.py`)
- Rate publish cycle → PDF rate parser: fetches rates (evidence: `src/addon_main.py`)
- Rate publish cycle → MQTT sensor bridge: publishes rates (evidence: `src/addon_main.py`)
- Mode dispatcher → Supervisor MQTT lookup: looks up broker (evidence: `src/addon_main.py`)
- Supervisor MQTT lookup → MQTT broker: retrieves credentials (evidence: `src/addon_main.py`)
- MQTT sensor bridge → MQTT broker: publishes MQTT (evidence: `src/mqtt_bridge.py`)
- MQTT broker → Home Assistant MQTT: delivers discovery (evidence: `README.md`)
