From a71943aa45132a8e9ae39e5bcf73c2bd6e67d524 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Jan 2026 08:49:46 +0000 Subject: [PATCH] Clean up stream resources in Teslemetry config entry unloading https://claude.ai/code/session_01PgBVSERiUypBC5WzxZRhcm --- homeassistant/components/teslemetry/__init__.py | 11 ++++++++++- .../components/teslemetry/quality_scale.yaml | 8 +------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/teslemetry/__init__.py b/homeassistant/components/teslemetry/__init__.py index 42d0ac25388b5..2fa22a03e1855 100644 --- a/homeassistant/components/teslemetry/__init__.py +++ b/homeassistant/components/teslemetry/__init__.py @@ -319,7 +319,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) - async def async_unload_entry(hass: HomeAssistant, entry: TeslemetryConfigEntry) -> bool: """Unload Teslemetry Config.""" - return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): + # Remove stream listeners for each vehicle + for vehicle in entry.runtime_data.vehicles: + vehicle.remove_listener() + + # Close the stream connection + if entry.runtime_data.stream: + entry.runtime_data.stream.close() + + return unload_ok async def async_migrate_entry( diff --git a/homeassistant/components/teslemetry/quality_scale.yaml b/homeassistant/components/teslemetry/quality_scale.yaml index 4592320997c7a..2865f05cdc070 100644 --- a/homeassistant/components/teslemetry/quality_scale.yaml +++ b/homeassistant/components/teslemetry/quality_scale.yaml @@ -26,13 +26,7 @@ rules: unique-config-entry: done # Silver action-exceptions: done - config-entry-unloading: - status: todo - comment: | - async_unload_entry must clean up: (1) close TeslemetryStream websocket via - stream.close(), (2) call remove_listener() for each vehicle to unsubscribe - from stream events, (3) consider using entry.async_on_unload() during setup - to register cleanup callbacks automatically. + config-entry-unloading: done docs-configuration-parameters: done docs-installation-parameters: done entity-unavailable: done