Skip to content

Commit 4a97019

Browse files
authored
feat: update example to show how to use multiple devices (#85)
1 parent f860433 commit 4a97019

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

docs/usage.md

+32-10
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,44 @@ Assuming that you've followed the {ref}`installations steps <installation>`, you
77
Example usage with `bleak`:
88

99
```python
10+
from __future__ import annotations
11+
1012
import asyncio
1113
import logging
1214
from collections.abc import Callable
15+
from typing import TypedDict
1316

1417
import habluetooth
1518
from aioesphomeapi import APIClient, ReconnectLogic
1619

1720
import bleak_esphome
1821

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+
]
2141

2242

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]:
2446
"""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)
2948
unregister_scanner: Callable[[], None] | None = None
3049

3150
async def on_disconnect(expected_disconnect: bool) -> None:
@@ -66,16 +85,19 @@ async def run_application(cli: APIClient) -> None:
6685

6786
async def run() -> None:
6887
"""Run the main application."""
88+
esphome_connections: list[tuple[ReconnectLogic, APIClient]] = []
6989
reconnect_logic: ReconnectLogic | None = None
7090
cli: APIClient | None = None
7191
try:
7292
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+
)
7497
await run_application(cli)
7598
finally:
76-
if reconnect_logic is not None:
99+
for reconnect_logic, cli in esphome_connections:
77100
await reconnect_logic.stop()
78-
if cli is not None:
79101
await cli.disconnect()
80102

81103

examples/setup_scanner.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1+
from __future__ import annotations
2+
13
import asyncio
24
import logging
35
from collections.abc import Callable
6+
from typing import TypedDict
47

58
import habluetooth
69
from aioesphomeapi import APIClient, ReconnectLogic
710

811
import bleak_esphome
912

10-
ESPHOME_DEVICE = "XXXX.local."
11-
NOISE_PSK = ""
13+
14+
class ESPHomeDeviceConfig(TypedDict):
15+
"""Configuration for an ESPHome device."""
16+
17+
address: str
18+
noise_psk: str | None
19+
20+
21+
# An unlimited number of devices can be added here
22+
ESPHOME_DEVICES: list[ESPHomeDeviceConfig] = [
23+
{
24+
"address": "XXXX.local.",
25+
"noise_psk": None,
26+
},
27+
{
28+
"address": "YYYY.local.",
29+
"noise_psk": None,
30+
},
31+
]
1232

1333

14-
async def setup_api_connection() -> tuple[ReconnectLogic, APIClient]:
34+
async def setup_api_connection(
35+
address: str, noise_psk: str | None = None
36+
) -> tuple[ReconnectLogic, APIClient]:
1537
"""Setup the API connection."""
16-
args = {"address": ESPHOME_DEVICE, "port": 6053, "password": None}
17-
if NOISE_PSK:
18-
args["noise_psk"] = NOISE_PSK
19-
cli = APIClient(**args)
38+
cli = APIClient(address=address, port=6053, password=None, noise_psk=noise_psk)
2039
unregister_scanner: Callable[[], None] | None = None
2140

2241
async def on_disconnect(expected_disconnect: bool) -> None:
@@ -57,16 +76,19 @@ async def run_application(cli: APIClient) -> None:
5776

5877
async def run() -> None:
5978
"""Run the main application."""
79+
esphome_connections: list[tuple[ReconnectLogic, APIClient]] = []
6080
reconnect_logic: ReconnectLogic | None = None
6181
cli: APIClient | None = None
6282
try:
6383
await habluetooth.BluetoothManager().async_setup()
64-
reconnect_logic, cli = await setup_api_connection()
84+
for device in ESPHOME_DEVICES:
85+
esphome_connections.append(
86+
await setup_api_connection(device["address"], device["noise_psk"])
87+
)
6588
await run_application(cli)
6689
finally:
67-
if reconnect_logic is not None:
90+
for reconnect_logic, cli in esphome_connections:
6891
await reconnect_logic.stop()
69-
if cli is not None:
7092
await cli.disconnect()
7193

7294

0 commit comments

Comments
 (0)