@@ -7,25 +7,44 @@ Assuming that you've followed the {ref}`installations steps <installation>`, you
7
7
Example usage with ` bleak ` :
8
8
9
9
``` python
10
+ from __future__ import annotations
11
+
10
12
import asyncio
11
13
import logging
12
14
from collections.abc import Callable
15
+ from typing import TypedDict
13
16
14
17
import habluetooth
15
18
from aioesphomeapi import APIClient, ReconnectLogic
16
19
17
20
import bleak_esphome
18
21
19
- ESPHOME_DEVICE = " XXXX.local."
20
- NOISE_PSK = " "
22
+
23
+ class ESPHomeDeviceConfig (TypedDict ):
24
+ """ Configuration for an ESPHome device."""
25
+
26
+ address: str
27
+ noise_psk: str | None
28
+
29
+
30
+ # An unlimited number of devices can be added here
31
+ ESPHOME_DEVICES : list[ESPHomeDeviceConfig] = [
32
+ {
33
+ " address" : " XXXX.local." ,
34
+ " noise_psk" : None ,
35
+ },
36
+ {
37
+ " address" : " YYYY.local." ,
38
+ " noise_psk" : None ,
39
+ },
40
+ ]
21
41
22
42
23
- async def setup_api_connection () -> tuple[ReconnectLogic, APIClient]:
43
+ async def setup_api_connection (
44
+ address : str , noise_psk : str | None = None
45
+ ) -> tuple[ReconnectLogic, APIClient]:
24
46
""" Setup the API connection."""
25
- args = {" address" : ESPHOME_DEVICE , " port" : 6053 , " password" : None }
26
- if NOISE_PSK :
27
- args[" noise_psk" ] = NOISE_PSK
28
- cli = APIClient(** args)
47
+ cli = APIClient(address = address, port = 6053 , password = None , noise_psk = noise_psk)
29
48
unregister_scanner: Callable[[], None ] | None = None
30
49
31
50
async def on_disconnect (expected_disconnect : bool ) -> None :
@@ -66,16 +85,19 @@ async def run_application(cli: APIClient) -> None:
66
85
67
86
async def run () -> None :
68
87
""" Run the main application."""
88
+ esphome_connections: list[tuple[ReconnectLogic, APIClient]] = []
69
89
reconnect_logic: ReconnectLogic | None = None
70
90
cli: APIClient | None = None
71
91
try :
72
92
await habluetooth.BluetoothManager().async_setup()
73
- reconnect_logic, cli = await setup_api_connection()
93
+ for device in ESPHOME_DEVICES :
94
+ esphome_connections.append(
95
+ await setup_api_connection(device[" address" ], device[" noise_psk" ])
96
+ )
74
97
await run_application(cli)
75
98
finally :
76
- if reconnect_logic is not None :
99
+ for reconnect_logic, cli in esphome_connections :
77
100
await reconnect_logic.stop()
78
- if cli is not None :
79
101
await cli.disconnect()
80
102
81
103
0 commit comments