Fix tplink child plug state reporting (#97658)

regressed in https://github.com/home-assistant/core/pull/96246
This commit is contained in:
J. Nick Koston 2023-08-03 00:35:22 -10:00 committed by GitHub
parent 53703448ec
commit db5d1b10ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 18 deletions

View file

@ -116,7 +116,7 @@ class SmartPlugSwitchChild(SmartPlugSwitch):
coordinator: TPLinkDataUpdateCoordinator, coordinator: TPLinkDataUpdateCoordinator,
plug: SmartDevice, plug: SmartDevice,
) -> None: ) -> None:
"""Initialize the switch.""" """Initialize the child switch."""
super().__init__(device, coordinator) super().__init__(device, coordinator)
self._plug = plug self._plug = plug
self._attr_unique_id = legacy_device_id(plug) self._attr_unique_id = legacy_device_id(plug)
@ -124,10 +124,15 @@ class SmartPlugSwitchChild(SmartPlugSwitch):
@async_refresh_after @async_refresh_after
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on.""" """Turn the child switch on."""
await self._plug.turn_on() await self._plug.turn_on()
@async_refresh_after @async_refresh_after
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off.""" """Turn the child switch off."""
await self._plug.turn_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)

View file

@ -180,12 +180,14 @@ def _mocked_strip() -> SmartStrip:
plug0.alias = "Plug0" plug0.alias = "Plug0"
plug0.device_id = "bb:bb:cc:dd:ee:ff_PLUG0DEVICEID" plug0.device_id = "bb:bb:cc:dd:ee:ff_PLUG0DEVICEID"
plug0.mac = "bb:bb:cc:dd:ee:ff" plug0.mac = "bb:bb:cc:dd:ee:ff"
plug0.is_on = True
plug0.protocol = _mock_protocol() plug0.protocol = _mock_protocol()
plug1 = _mocked_plug() plug1 = _mocked_plug()
plug1.device_id = "cc:bb:cc:dd:ee:ff_PLUG1DEVICEID" plug1.device_id = "cc:bb:cc:dd:ee:ff_PLUG1DEVICEID"
plug1.mac = "cc:bb:cc:dd:ee:ff" plug1.mac = "cc:bb:cc:dd:ee:ff"
plug1.alias = "Plug1" plug1.alias = "Plug1"
plug1.protocol = _mock_protocol() plug1.protocol = _mock_protocol()
plug1.is_on = False
strip.children = [plug0, plug1] strip.children = [plug0, plug1]
return strip return strip

View file

@ -9,7 +9,7 @@ import pytest
from homeassistant.components import tplink from homeassistant.components import tplink
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.tplink.const import 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.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component 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 # since this is what the previous version did
assert hass.states.get("switch.my_strip") is None assert hass.states.get("switch.my_strip") is None
for plug_id in range(2): entity_id = "switch.my_strip_plug0"
entity_id = f"switch.my_strip_plug{plug_id}" state = hass.states.get(entity_id)
state = hass.states.get(entity_id) assert state.state == STATE_ON
assert state.state == STATE_ON
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
) )
strip.children[plug_id].turn_off.assert_called_once() strip.children[0].turn_off.assert_called_once()
strip.children[plug_id].turn_off.reset_mock() strip.children[0].turn_off.reset_mock()
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True
) )
strip.children[plug_id].turn_on.assert_called_once() strip.children[0].turn_on.assert_called_once()
strip.children[plug_id].turn_on.reset_mock() 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: async def test_strip_unique_ids(hass: HomeAssistant) -> None: