# tinohox/iona-ha architecture

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

- Interactive diagram: https://gitdiagram.com/tinohox/iona-ha
- Repository: https://github.com/tinohox/iona-ha

## Overview

#### Purpose and runtime
This is a HACS-distributed custom integration for Home Assistant 2024.11+. It exposes iONA smart-meter readings as Home Assistant entities, preferentially polling an iONA Box on the local network and falling back to enviaM web services. It optionally supports dynamic “mein Strom Vision” tariff pricing and scheduling optimization.

Core integration metadata and HA loading are rooted in `manifest.json`, `__init__.py`, and `const.py`.

#### Setup and lifecycle entry points
Home Assistant discovers the integration through its manifest and initializes it through the integration module. UI-driven onboarding and runtime option changes are handled by the config-flow layer; credentials are stored in Home Assistant’s ConfigEntry storage rather than ordinary integration configuration.

- `__init__.py`
- `config_flow.py`
- `const.py`

Configuration controls local-box address/credentials, LAN and web polling intervals, and optional Vision features.

#### Acquisition and source failover
Meter acquisition has two explicit adapters: LAN access to the physical iONA Box and a web/API path. The intended control flow is LAN-first at a short interval; connection failures cause web polling at a longer interval; recovery returns the active source to LAN. Token modules isolate authentication for the respective remote/local API interactions.

- `app/get_lan_data.py`
- `app/get_web_data.py`
- `app/get_lan_token.py`
- `app/get_web_token.py`

This is the primary reliability boundary: local-network availability affects live data, while web access is an external-service fallback.

#### Central data coordination and persistence
`data_manager.py` is the likely integration-level coordination boundary between acquisition, entity updates, source state, and optional tariff data. Runtime files appear to be kept under `app/data` and `app/env`; environment backup/restore utilities protect token/configuration files from HACS overwrites.

- `data_manager.py`
- `env_backup.py`
- `env_utils.py`
- `app/data/.gitkeep`
- `app/env/.gitkeep`

The Home Assistant ConfigEntry remains the authoritative credential/configuration boundary; filesystem state supports tokens and recoverable integration-local data.

#### Home Assistant entity surface
Platform modules map coordinated readings and optimization state into HA entities. Sensors provide meter totals, instantaneous power, data source, and tariff/optimization outputs; number, switch, and button entities provide the Vision Tools controls and recalculation trigger.

- `sensor.py`
- `number.py`
- `switch.py`
- `button.py`

This boundary makes energy data compatible with Home Assistant’s Energy Dashboard and gives automations timestamp-oriented optimization results rather than directly controlling appliances.

#### Vision tariff and optimization pipeline
The optional Vision path retrieves quarter-hour spot prices and contract tariff inputs, calculates end-user prices, then searches available future intervals for the lowest-average contiguous window. It is separate from core meter operation, so tariff-service failures should not prevent LAN/web meter updates.

- `app/get_spot_prices.py`
- `app/get_tariff_data.py`
- `app/calc_preise.py`
- `app/vision.py`

External dependencies are enviaM tariff/web APIs and the iONA Box; the optimization consumes derived price series and user-configured duration, horizon, delay, and night-only constraints.

#### Frontend delivery
The integration bundles Lovelace custom cards for power/meter visualization and Vision Tools control. These are browser-side Home Assistant dashboard resources, distinct from the Python entity and polling runtime.

- `www/iona-card.js`
- `www/iona-vision-card.js`

Localization for the HA configuration/UI is supplied through `strings.json` and `translations/`.

Main entry points: [`data_manager.py`](https://github.com/tinohox/iona-ha/blob/main/data_manager.py) (Data manager), [`app/get_lan_data.py`](https://github.com/tinohox/iona-ha/blob/main/app/get_lan_data.py) (iONA Box LAN adapter), [`app/get_web_data.py`](https://github.com/tinohox/iona-ha/blob/main/app/get_web_data.py) (enviaM web adapter), [`app/calc_preise.py`](https://github.com/tinohox/iona-ha/blob/main/app/calc_preise.py) (End-user price calculation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ha["Home Assistant integration"]
  node_ha_core{{"Integration lifecycle<br/>HA integration<br/>[__init__.py]"}}
  node_config_flow["Config flow<br/>HA onboarding<br/>[config_flow.py]"]
  node_constants["Integration constants<br/>shared definitions<br/>[const.py]"]
  node_config_entry[("ConfigEntry storage<br/>HA credential store")]
  node_data_manager{{"Data manager<br/>runtime coordinator<br/>[data_manager.py]"}}
  node_entity_platforms["HA entity platforms<br/>sensors and controls<br/>[sensor.py]"]
  node_vision_controls["Vision controls<br/>number, switch, button<br/>[number.py]"]
end

subgraph group_acquisition["Meter acquisition"]
  node_lan_adapter["iONA Box LAN adapter<br/>meter adapter<br/>[get_lan_data.py]"]
  node_lan_token["LAN token<br/>authentication<br/>[get_lan_token.py]"]
  node_iona_box["iONA Box<br/>local meter device"]
  node_web_adapter["enviaM web adapter<br/>fallback adapter<br/>[get_web_data.py]"]
  node_web_token["Web token<br/>authentication<br/>[get_web_token.py]"]
  node_envia_web["enviaM web APIs<br/>external service"]
end

subgraph group_vision["Vision pricing"]
  node_spot_prices["Spot price retrieval<br/>tariff adapter<br/>[get_spot_prices.py]"]
  node_tariff_data["Contract tariff retrieval<br/>tariff adapter<br/>[get_tariff_data.py]"]
  node_price_calculation["End-user price calculation<br/>price derivation<br/>[calc_preise.py]"]
  node_optimizer{{"Vision optimizer<br/>schedule optimizer<br/>[vision.py]"}}
end

subgraph group_storage["Runtime state"]
  node_runtime_state[("Runtime files<br/>[.gitkeep]")]
  node_env_backup["Environment backup<br/>state protection<br/>[env_backup.py]"]
end

subgraph group_frontend["Dashboard delivery"]
  node_lovelace_cards["Lovelace cards<br/>browser dashboard<br/>[iona-card.js]"]
end

node_ha_core -->|"starts setup"| node_config_flow
node_config_flow -->|"stores settings"| node_config_entry
node_ha_core -->|"initializes"| node_data_manager
node_constants -->|"defines shared settings"| node_data_manager
node_config_entry -->|"provides configuration"| node_data_manager
node_data_manager -->|"polls first"| node_lan_adapter
node_lan_token -->|"authenticates"| node_lan_adapter
node_lan_adapter -->|"reads meter"| node_iona_box
node_data_manager -.->|"falls back on LAN failure"| node_web_adapter
node_web_token -->|"authenticates"| node_web_adapter
node_web_adapter -->|"retrieves readings"| node_envia_web
node_data_manager -->|"updates entities"| node_entity_platforms
node_vision_controls -->|"supplies constraints and triggers"| node_data_manager
node_data_manager -.->|"requests optional prices"| node_spot_prices
node_data_manager -.->|"requests tariff inputs"| node_tariff_data
node_spot_prices -->|"price series"| node_price_calculation
node_tariff_data -->|"contract inputs"| node_price_calculation
node_price_calculation -->|"derived prices"| node_optimizer
node_optimizer -->|"optimization result"| node_data_manager
node_data_manager -->|"persists local state"| node_runtime_state
node_env_backup -->|"backs up and restores"| node_runtime_state
node_entity_platforms -->|"dashboard state and controls"| node_lovelace_cards

click node_ha_core "https://github.com/tinohox/iona-ha/blob/main/__init__.py"
click node_config_flow "https://github.com/tinohox/iona-ha/blob/main/config_flow.py"
click node_constants "https://github.com/tinohox/iona-ha/blob/main/const.py"
click node_data_manager "https://github.com/tinohox/iona-ha/blob/main/data_manager.py"
click node_lan_adapter "https://github.com/tinohox/iona-ha/blob/main/app/get_lan_data.py"
click node_lan_token "https://github.com/tinohox/iona-ha/blob/main/app/get_lan_token.py"
click node_web_adapter "https://github.com/tinohox/iona-ha/blob/main/app/get_web_data.py"
click node_web_token "https://github.com/tinohox/iona-ha/blob/main/app/get_web_token.py"
click node_entity_platforms "https://github.com/tinohox/iona-ha/blob/main/sensor.py"
click node_vision_controls "https://github.com/tinohox/iona-ha/blob/main/number.py"
click node_spot_prices "https://github.com/tinohox/iona-ha/blob/main/app/get_spot_prices.py"
click node_tariff_data "https://github.com/tinohox/iona-ha/blob/main/app/get_tariff_data.py"
click node_price_calculation "https://github.com/tinohox/iona-ha/blob/main/app/calc_preise.py"
click node_optimizer "https://github.com/tinohox/iona-ha/blob/main/app/vision.py"
click node_runtime_state "https://github.com/tinohox/iona-ha/blob/main/app/data/.gitkeep"
click node_env_backup "https://github.com/tinohox/iona-ha/blob/main/env_backup.py"
click node_lovelace_cards "https://github.com/tinohox/iona-ha/blob/main/www/iona-card.js"

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_ha_core,node_config_flow,node_constants,node_config_entry,node_data_manager,node_entity_platforms,node_vision_controls toneBlue
class node_lan_adapter,node_lan_token,node_iona_box,node_web_adapter,node_web_token,node_envia_web toneAmber
class node_spot_prices,node_tariff_data,node_price_calculation,node_optimizer toneMint
class node_runtime_state,node_env_backup toneRose
class node_lovelace_cards toneIndigo
```

## Components

### Home Assistant integration

- [Integration lifecycle](https://github.com/tinohox/iona-ha/blob/main/__init__.py): HA integration · Manifest-driven setup and runtime lifecycle. · `__init__.py`
- [Config flow](https://github.com/tinohox/iona-ha/blob/main/config_flow.py): HA onboarding · Collects connection settings and Vision options. · `config_flow.py`
- [Integration constants](https://github.com/tinohox/iona-ha/blob/main/const.py): shared definitions · `const.py`
- **ConfigEntry storage**: HA credential store · Authoritative credentials and user configuration.
- [Data manager](https://github.com/tinohox/iona-ha/blob/main/data_manager.py): runtime coordinator · Coordinates readings, source state, entity updates, and optional tariff data. · `data_manager.py`
- [HA entity platforms](https://github.com/tinohox/iona-ha/blob/main/sensor.py): sensors and controls · Publishes meter, source, tariff, and optimization entities. · `sensor.py`
- [Vision controls](https://github.com/tinohox/iona-ha/blob/main/number.py): number, switch, button · Accepts optimization constraints and recalculation requests. · `number.py`

### Meter acquisition

- [iONA Box LAN adapter](https://github.com/tinohox/iona-ha/blob/main/app/get_lan_data.py): meter adapter · Preferred short-interval local polling path. · `app/get_lan_data.py`
- [LAN token](https://github.com/tinohox/iona-ha/blob/main/app/get_lan_token.py): authentication · `app/get_lan_token.py`
- **iONA Box**: local meter device
- [enviaM web adapter](https://github.com/tinohox/iona-ha/blob/main/app/get_web_data.py): fallback adapter · Longer-interval fallback when LAN access fails. · `app/get_web_data.py`
- [Web token](https://github.com/tinohox/iona-ha/blob/main/app/get_web_token.py): authentication · `app/get_web_token.py`
- **enviaM web APIs**: external service

### Vision pricing

Optional tariff and scheduling pipeline.

- [Spot price retrieval](https://github.com/tinohox/iona-ha/blob/main/app/get_spot_prices.py): tariff adapter · `app/get_spot_prices.py`
- [Contract tariff retrieval](https://github.com/tinohox/iona-ha/blob/main/app/get_tariff_data.py): tariff adapter · `app/get_tariff_data.py`
- [End-user price calculation](https://github.com/tinohox/iona-ha/blob/main/app/calc_preise.py): price derivation · `app/calc_preise.py`
- [Vision optimizer](https://github.com/tinohox/iona-ha/blob/main/app/vision.py): schedule optimizer · Finds the lowest-average future contiguous interval. · `app/vision.py`

### Runtime state

- [Runtime files](https://github.com/tinohox/iona-ha/blob/main/app/data/.gitkeep): local token and data state · Recoverable integration-local tokens and data. · `app/data/.gitkeep`
- [Environment backup](https://github.com/tinohox/iona-ha/blob/main/env_backup.py): state protection · Backs up and restores runtime files across HACS updates. · `env_backup.py`

### Dashboard delivery

- [Lovelace cards](https://github.com/tinohox/iona-ha/blob/main/www/iona-card.js): browser dashboard · Custom power, meter, and Vision Tools interfaces. · `www/iona-card.js`

## Connections

- Integration lifecycle → Config flow: starts setup
- Config flow → ConfigEntry storage: stores settings
- Integration lifecycle → Data manager: initializes
- Integration constants → Data manager: defines shared settings
- ConfigEntry storage → Data manager: provides configuration
- Data manager → iONA Box LAN adapter: polls first
- LAN token → iONA Box LAN adapter: authenticates
- iONA Box LAN adapter → iONA Box: reads meter
- Data manager → enviaM web adapter: falls back on LAN failure
- Web token → enviaM web adapter: authenticates
- enviaM web adapter → enviaM web APIs: retrieves readings
- Data manager → HA entity platforms: updates entities
- Vision controls → Data manager: supplies constraints and triggers
- Data manager → Spot price retrieval: requests optional prices
- Data manager → Contract tariff retrieval: requests tariff inputs
- Spot price retrieval → End-user price calculation: price series
- Contract tariff retrieval → End-user price calculation: contract inputs
- End-user price calculation → Vision optimizer: derived prices
- Vision optimizer → Data manager: optimization result
- Data manager → Runtime files: persists local state
- Environment backup → Runtime files: backs up and restores
- HA entity platforms → Lovelace cards: dashboard state and controls
