Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explictly close the TPLink SmartDevice protocol on unload #56743

Merged
merged 2 commits into from Sep 28, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/tplink/__init__.py
Expand Up @@ -141,8 +141,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass_data: dict[str, Any] = hass.data[DOMAIN]
if entry.entry_id not in hass_data:
return True
device: SmartDevice = hass.data[DOMAIN][entry.entry_id].device
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass_data.pop(entry.entry_id)
await device.protocol.close()
return unload_ok


Expand Down
12 changes: 12 additions & 0 deletions tests/components/tplink/__init__.py
Expand Up @@ -4,6 +4,7 @@

from kasa import SmartBulb, SmartPlug, SmartStrip
from kasa.exceptions import SmartDeviceException
from kasa.protocol import TPLinkSmartHomeProtocol

MODULE = "homeassistant.components.tplink"
MODULE_CONFIG_FLOW = "homeassistant.components.tplink.config_flow"
Expand All @@ -14,6 +15,12 @@
DEFAULT_ENTRY_TITLE = f"{ALIAS} {MODEL}"


def _mock_protocol() -> TPLinkSmartHomeProtocol:
protocol = MagicMock(auto_spec=TPLinkSmartHomeProtocol)
protocol.close = AsyncMock()
return protocol


def _mocked_bulb() -> SmartBulb:
bulb = MagicMock(auto_spec=SmartBulb)
bulb.update = AsyncMock()
Expand All @@ -36,6 +43,7 @@ def _mocked_bulb() -> SmartBulb:
bulb.set_brightness = AsyncMock()
bulb.set_hsv = AsyncMock()
bulb.set_color_temp = AsyncMock()
bulb.protocol = _mock_protocol()
return bulb


Expand All @@ -55,6 +63,7 @@ def _mocked_plug() -> SmartPlug:
plug.hw_info = {"sw_ver": "1.0.0"}
plug.turn_off = AsyncMock()
plug.turn_on = AsyncMock()
plug.protocol = _mock_protocol()
return plug


Expand All @@ -74,14 +83,17 @@ def _mocked_strip() -> SmartStrip:
strip.hw_info = {"sw_ver": "1.0.0"}
strip.turn_off = AsyncMock()
strip.turn_on = AsyncMock()
strip.protocol = _mock_protocol()
plug0 = _mocked_plug()
plug0.alias = "Plug0"
plug0.device_id = "bb:bb:cc:dd:ee:ff_PLUG0DEVICEID"
plug0.mac = "bb:bb:cc:dd:ee:ff"
plug0.protocol = _mock_protocol()
plug1 = _mocked_plug()
plug1.device_id = "cc:bb:cc:dd:ee:ff_PLUG1DEVICEID"
plug1.mac = "cc:bb:cc:dd:ee:ff"
plug1.alias = "Plug1"
plug1.protocol = _mock_protocol()
strip.children = [plug0, plug1]
return strip

Expand Down