Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions homeassistant/components/knx/knx_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,5 @@ async def after_update_callback(self, device: XknxDevice) -> None:
async def async_added_to_hass(self) -> None:
"""Store register state change callback."""
self._device.register_device_updated_cb(self.after_update_callback)

async def async_will_remove_from_hass(self) -> None:
"""Disconnect device object when removed."""
# will also remove all callbacks
self._device.shutdown()
# will remove all callbacks and xknx tasks
self.async_on_remove(self._device.shutdown)
11 changes: 6 additions & 5 deletions homeassistant/components/knx/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
from functools import partial
from typing import Any

from xknx import XKNX
Expand Down Expand Up @@ -221,9 +222,9 @@ async def async_added_to_hass(self) -> None:
self.knx.xknx.connection_manager.register_connection_state_changed_cb(
self.after_update_callback
)

async def async_will_remove_from_hass(self) -> None:
"""Disconnect device object when removed."""
self.knx.xknx.connection_manager.unregister_connection_state_changed_cb(
self.after_update_callback
self.async_on_remove(
partial(
self.knx.xknx.connection_manager.unregister_connection_state_changed_cb,
self.after_update_callback,
)
)
8 changes: 4 additions & 4 deletions tests/components/knx/test_interface_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ async def test_removed_entity(
hass: HomeAssistant, knx: KNXTestKit, entity_registry: er.EntityRegistry
) -> None:
"""Test unregister callback when entity is removed."""
await knx.setup_integration({})

with patch.object(
knx.xknx.connection_manager, "unregister_connection_state_changed_cb"
with patch(
"xknx.core.connection_manager.ConnectionManager.unregister_connection_state_changed_cb"
) as unregister_mock:
await knx.setup_integration({})

entity_registry.async_update_entity(
"sensor.knx_interface_connection_established",
disabled_by=er.RegistryEntryDisabler.USER,
Expand Down