# jtechczech/mydlink-ha-integration architecture

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

- Interactive diagram: https://gitdiagram.com/jtechczech/mydlink-ha-integration
- Repository: https://github.com/jtechczech/mydlink-ha-integration

## Overview

This Home Assistant integration connects a Mydlink account and exposes supported DSP-series smart plugs as switch entities. The main flow is credential validation, account/device initialization, entity creation, then polling and WebSocket-backed state updates and control. The API excerpt supports token persistence/refresh, cloud device discovery, and WebSocket messaging; other capabilities are limited to those documented or visible in the sampled files.

Main entry points: [`plug/api.py`](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.py) (Mydlink API client), [`plug/switch.py`](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/switch.py) (Plug switch entity), [`plug/config_flow.py`](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/config_flow.py) (Credential flow), [`plug/__init__.py`](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/__init__.py) (Integration lifecycle)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Account setup"]
  node_config_flow["Credential flow<br/>[config_flow.py]"]
end

subgraph group_integration["HA integration"]
  node_entry["Integration lifecycle<br/>[__init__.py]"]
end

subgraph group_devices["Plug entities"]
  node_switch_platform["Switch platform<br/>[switch.py]"]
  node_switch_entity["Plug switch entity<br/>[switch.py]"]
end

subgraph group_cloud["Mydlink access"]
  node_api["Mydlink API client<br/>[api.py]"]
  node_ws["WebSocket client<br/>[api.py]"]
  node_token_store[("Credential and device state<br/>[api.py]")]
end

node_user(("Account user"))
node_ha(("Home Assistant"))
node_mydlink{{"Mydlink cloud API"}}

node_user -->|"enters credentials"| node_config_flow
node_config_flow -->|"validates login"| node_api
node_api -->|"authenticates and discovers"| node_mydlink
node_api -->|"loads and saves tokens"| node_token_store
node_config_flow -->|"shows setup result"| node_user
node_ha -->|"starts entry"| node_entry
node_entry -->|"initializes session"| node_api
node_entry -->|"forwards platform setup"| node_switch_platform
node_switch_platform -->|"creates plug entities"| node_switch_entity
node_switch_platform -->|"requests periodic state"| node_api
node_switch_entity -->|"requests initial state"| node_api
node_switch_entity -->|"sets plug state"| node_api
node_api -->|"sends device messages"| node_ws
node_ws -->|"maintains device connection"| node_mydlink
node_mydlink -->|"delivers device updates"| node_ws
node_ws -->|"dispatches messages"| node_api
node_api -->|"calls state callback"| node_switch_entity

click node_config_flow "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/config_flow.py"
click node_entry "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/__init__.py"
click node_switch_platform "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/switch.py"
click node_switch_entity "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/switch.py"
click node_api "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.py"
click node_ws "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.py"
click node_token_store "https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.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_user toneBlue
class node_entry toneAmber
class node_switch_platform,node_switch_entity,node_mydlink toneMint
class node_api,node_ws,node_token_store toneRose
class node_ha toneIndigo
```

## Components

### Account setup

- [Credential flow](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/config_flow.py): `plug/config_flow.py`

### HA integration

- [Integration lifecycle](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/__init__.py): `plug/__init__.py`

### Plug entities

- [Switch platform](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/switch.py): `plug/switch.py`
- [Plug switch entity](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/switch.py): `plug/switch.py`

### Mydlink access

- [Mydlink API client](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.py): `plug/api.py`
- [WebSocket client](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.py): `plug/api.py`
- [Credential and device state](https://github.com/jtechczech/mydlink-ha-integration/blob/main/plug/api.py): `plug/api.py`

### Other components

- **Account user**
- **Home Assistant**
- **Mydlink cloud API**

## Connections

- Account user → Credential flow: enters credentials
- Credential flow → Mydlink API client: validates login
- Mydlink API client → Mydlink cloud API: authenticates and discovers
- Mydlink API client → Credential and device state: loads and saves tokens
- Credential flow → Account user: shows setup result
- Home Assistant → Integration lifecycle: starts entry
- Integration lifecycle → Mydlink API client: initializes session
- Integration lifecycle → Switch platform: forwards platform setup
- Switch platform → Plug switch entity: creates plug entities
- Switch platform → Mydlink API client: requests periodic state
- Plug switch entity → Mydlink API client: requests initial state
- Plug switch entity → Mydlink API client: sets plug state
- Mydlink API client → WebSocket client: sends device messages
- WebSocket client → Mydlink cloud API: maintains device connection
- Mydlink cloud API → WebSocket client: delivers device updates
- WebSocket client → Mydlink API client: dispatches messages
- Mydlink API client → Plug switch entity: calls state callback
