Skip to content

Commit c843ab6

Browse files
committed
docs: Fix example to clean up before cache flush
The example was missing device_manager.close() before cache.flush(), causing it to hang indefinitely waiting for background tasks (MQTT connections, local reconnection loops, health managers) to complete. Added try/finally block to ensure cleanup always happens.
1 parent f20ade9 commit c843ab6

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

examples/example.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,34 @@ async def main():
5656

5757
# Create a device manager that can discover devices.
5858
device_manager = await create_device_manager(user_params, cache=cache)
59-
devices = await device_manager.get_devices()
6059

61-
# Get all vacuum devices that support the v1 PropertiesApi
62-
device_results = []
63-
for device in devices:
64-
if not device.v1_properties:
65-
continue
66-
67-
# Refresh the current device status
68-
status_trait = device.v1_properties.status
69-
await status_trait.refresh()
70-
71-
# Print the device status as JSON
72-
device_results.append(
73-
{
74-
"device": device.name,
75-
"status": remove_none_values(dataclasses.asdict(status_trait)),
76-
}
77-
)
78-
79-
print(json.dumps(device_results, indent=2))
80-
81-
await cache.flush()
60+
try:
61+
devices = await device_manager.get_devices()
62+
63+
# Get all vacuum devices that support the v1 PropertiesApi
64+
device_results = []
65+
for device in devices:
66+
if not device.v1_properties:
67+
continue
68+
69+
# Refresh the current device status
70+
status_trait = device.v1_properties.status
71+
await status_trait.refresh()
72+
73+
# Print the device status as JSON
74+
device_results.append(
75+
{
76+
"device": device.name,
77+
"status": remove_none_values(dataclasses.asdict(status_trait)),
78+
}
79+
)
80+
81+
print(json.dumps(device_results, indent=2))
82+
83+
finally:
84+
# Close device manager to cancel background tasks before flushing cache
85+
await device_manager.close()
86+
await cache.flush()
8287

8388

8489
if __name__ == "__main__":

0 commit comments

Comments
 (0)