From db5d1b10eaa7749c08a6833f06b301a4f1d68c68 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Aug 2023 00:35:22 -1000 Subject: [PATCH] Fix tplink child plug state reporting (#97658) regressed in https://github.com/home-assistant/core/pull/96246 --- homeassistant/components/tplink/switch.py | 11 ++++-- tests/components/tplink/__init__.py | 2 + tests/components/tplink/test_switch.py | 45 +++++++++++++++-------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index 6c843246663..fb812abc293 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -116,7 +116,7 @@ class SmartPlugSwitchChild(SmartPlugSwitch): coordinator: TPLinkDataUpdateCoordinator, plug: SmartDevice, ) -> None: - """Initialize the switch.""" + """Initialize the child switch.""" super().__init__(device, coordinator) self._plug = plug self._attr_unique_id = legacy_device_id(plug) @@ -124,10 +124,15 @@ class SmartPlugSwitchChild(SmartPlugSwitch): @async_refresh_after async def async_turn_on(self, **kwargs: Any) -> None: - """Turn the switch on.""" + """Turn the child switch on.""" await self._plug.turn_on() @async_refresh_after async def async_turn_off(self, **kwargs: Any) -> None: - """Turn the switch off.""" + """Turn the child switch off.""" await self._plug.turn_off() + + @property + def is_on(self) -> bool: + """Return true if child switch is on.""" + return bool(self._plug.is_on) diff --git a/tests/components/tplink/__init__.py b/tests/components/tplink/__init__.py index 4232d3e6909..816251ae3bb 100644 --- a/tests/components/tplink/__init__.py +++ b/tests/components/tplink/__init__.py @@ -180,12 +180,14 @@ def _mocked_strip() -> SmartStrip: plug0.alias = "Plug0" plug0.device_id = "bb:bb:cc:dd:ee:ff_PLUG0DEVICEID" plug0.mac = "bb:bb:cc:dd:ee:ff" + plug0.is_on = True 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() + plug1.is_on = False strip.children = [plug0, plug1] return strip diff --git a/tests/components/tplink/test_switch.py b/tests/components/tplink/test_switch.py index 1e5e03c0f37..05286e5ff48 100644 --- a/tests/components/tplink/test_switch.py +++ b/tests/components/tplink/test_switch.py @@ -9,7 +9,7 @@ import pytest from homeassistant.components import tplink from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.tplink.const import DOMAIN -from homeassistant.const import ATTR_ENTITY_ID, STATE_ON, STATE_UNAVAILABLE +from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component @@ -146,22 +146,37 @@ async def test_strip(hass: HomeAssistant) -> None: # since this is what the previous version did assert hass.states.get("switch.my_strip") is None - for plug_id in range(2): - entity_id = f"switch.my_strip_plug{plug_id}" - state = hass.states.get(entity_id) - assert state.state == STATE_ON + entity_id = "switch.my_strip_plug0" + state = hass.states.get(entity_id) + assert state.state == STATE_ON - await hass.services.async_call( - SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True - ) - strip.children[plug_id].turn_off.assert_called_once() - strip.children[plug_id].turn_off.reset_mock() + await hass.services.async_call( + SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + strip.children[0].turn_off.assert_called_once() + strip.children[0].turn_off.reset_mock() - await hass.services.async_call( - SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True - ) - strip.children[plug_id].turn_on.assert_called_once() - strip.children[plug_id].turn_on.reset_mock() + await hass.services.async_call( + SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + strip.children[0].turn_on.assert_called_once() + strip.children[0].turn_on.reset_mock() + + entity_id = "switch.my_strip_plug1" + state = hass.states.get(entity_id) + assert state.state == STATE_OFF + + await hass.services.async_call( + SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + strip.children[1].turn_off.assert_called_once() + strip.children[1].turn_off.reset_mock() + + await hass.services.async_call( + SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + strip.children[1].turn_on.assert_called_once() + strip.children[1].turn_on.reset_mock() async def test_strip_unique_ids(hass: HomeAssistant) -> None: