From b2725918b1e95624d12e9d7ea860c3a8fd23df87 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Mon, 2 Aug 2021 18:47:54 +0200 Subject: [PATCH] Fix TP-Link smart strip devices (#53799) --- homeassistant/components/tplink/__init__.py | 4 +- homeassistant/components/tplink/sensor.py | 4 +- homeassistant/components/tplink/switch.py | 2 +- tests/components/tplink/consts.py | 55 ++++++++++++++------- tests/components/tplink/test_init.py | 23 +++------ 5 files changed, 48 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/tplink/__init__.py b/homeassistant/components/tplink/__init__.py index 88160722669..552e5666db8 100644 --- a/homeassistant/components/tplink/__init__.py +++ b/homeassistant/components/tplink/__init__.py @@ -180,9 +180,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: continue hass_data[COORDINATORS][ - switch.mac + switch.context or switch.mac ] = coordinator = SmartPlugDataUpdateCoordinator(hass, switch) - await coordinator.async_config_entry_first_refresh() if unavailable_devices: @@ -275,4 +274,5 @@ class SmartPlugDataUpdateCoordinator(DataUpdateCoordinator): except SmartDeviceException as ex: raise UpdateFailed(ex) from ex + self.name = data[CONF_ALIAS] return data diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index bb6596b82d1..697641915f7 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -101,7 +101,9 @@ async def async_setup_entry( ] switches: list[SmartPlug] = hass.data[TPLINK_DOMAIN][CONF_SWITCH] for switch in switches: - coordinator: SmartPlugDataUpdateCoordinator = coordinators[switch.mac] + coordinator: SmartPlugDataUpdateCoordinator = coordinators[ + switch.context or switch.mac + ] if not switch.has_emeter and coordinator.data.get(CONF_EMETER_PARAMS) is None: continue for description in ENERGY_SENSORS: diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index f5319de999a..10cf5c64d75 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -39,7 +39,7 @@ async def async_setup_entry( ] switches: list[SmartPlug] = hass.data[TPLINK_DOMAIN][CONF_SWITCH] for switch in switches: - coordinator = coordinators[switch.mac] + coordinator = coordinators[switch.context or switch.mac] entities.append(SmartPlugSwitch(switch, coordinator)) async_add_entities(entities) diff --git a/tests/components/tplink/consts.py b/tests/components/tplink/consts.py index 95177a12a9c..e579be61df2 100644 --- a/tests/components/tplink/consts.py +++ b/tests/components/tplink/consts.py @@ -61,31 +61,48 @@ SMARTPLUG_HS100_DATA = { "err_code": 0, } } -SMARTSTRIPWITCH_DATA = { +SMARTSTRIP_KP303_DATA = { "sysinfo": { - "sw_ver": "1.0.4 Build 191111 Rel.143500", - "hw_ver": "4.0", - "model": "HS110(EU)", - "deviceId": "4C56447B395BB7A2FAC68C9DFEE2E84163222581", - "oemId": "40F54B43071E9436B6395611E9D91CEA", - "hwId": "A6C77E4FDD238B53D824AC8DA361F043", - "rssi": -24, - "longitude_i": 130793, - "latitude_i": 480582, - "alias": "SmartPlug", + "sw_ver": "1.0.4 Build 210428 Rel.135415", + "hw_ver": "1.0", + "model": "KP303(AU)", + "deviceId": "03102547AB1A57A4E4AA5B4EFE34C3005726B97D", + "oemId": "1F950FC9BFF278D9D35E046C129D9411", + "hwId": "9E86D4F840D2787D3D7A6523A731BA2C", + "rssi": -74, + "longitude_i": 1158985, + "latitude_i": -319172, + "alias": "TP-LINK_Power Strip_00B1", "status": "new", "mic_type": "IOT.SMARTPLUGSWITCH", "feature": "TIM", - "mac": "69:F2:3C:8E:E3:47", + "mac": "D4:DD:D6:95:B0:F9", "updating": 0, "led_off": 0, - "relay_state": 0, - "on_time": 0, - "active_mode": "none", - "icon_hash": "", - "dev_name": "Smart Wi-Fi Plug With Energy Monitoring", - "next_action": {"type": -1}, - "children": [{"id": "1", "state": 1, "alias": "SmartPlug#1"}], + "children": [ + { + "id": "8006B399B7FE68D4E6991CCCEA239C081DFA913000", + "state": 0, + "alias": "R-Plug 1", + "on_time": 0, + "next_action": {"type": -1}, + }, + { + "id": "8006B399B7FE68D4E6991CCCEA239C081DFA913001", + "state": 1, + "alias": "R-Plug 2", + "on_time": 93835, + "next_action": {"type": -1}, + }, + { + "id": "8006B399B7FE68D4E6991CCCEA239C081DFA913002", + "state": 1, + "alias": "R-Plug 3", + "on_time": 93834, + "next_action": {"type": -1}, + }, + ], + "child_num": 3, "err_code": 0, }, "realtime": { diff --git a/tests/components/tplink/test_init.py b/tests/components/tplink/test_init.py index a201788f35b..d96d6846939 100644 --- a/tests/components/tplink/test_init.py +++ b/tests/components/tplink/test_init.py @@ -20,11 +20,10 @@ from homeassistant.components.tplink.const import ( CONF_LIGHT, CONF_SW_VERSION, CONF_SWITCH, - COORDINATORS, UNAVAILABLE_RETRY_DELAY, ) from homeassistant.components.tplink.sensor import ENERGY_SENSORS -from homeassistant.const import CONF_ALIAS, CONF_DEVICE_ID, CONF_HOST +from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component @@ -34,7 +33,7 @@ from tests.common import MockConfigEntry, async_fire_time_changed, mock_coro from tests.components.tplink.consts import ( SMARTPLUG_HS100_DATA, SMARTPLUG_HS110_DATA, - SMARTSTRIPWITCH_DATA, + SMARTSTRIP_KP303_DATA, ) @@ -307,7 +306,7 @@ async def test_smartstrip_device(hass: HomeAssistant): """Moked SmartStrip class.""" def get_sysinfo(self): - return SMARTSTRIPWITCH_DATA["sysinfo"] + return SMARTSTRIP_KP303_DATA["sysinfo"] with patch( "homeassistant.components.tplink.common.Discover.discover" @@ -315,9 +314,7 @@ async def test_smartstrip_device(hass: HomeAssistant): "homeassistant.components.tplink.common.SmartDevice._query_helper" ), patch( "homeassistant.components.tplink.common.SmartPlug.get_sysinfo", - return_value=SMARTSTRIPWITCH_DATA["sysinfo"], - ), patch( - "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup" + return_value=SMARTSTRIP_KP303_DATA["sysinfo"], ): strip = SmartStrip("123.123.123.123") @@ -326,16 +323,8 @@ async def test_smartstrip_device(hass: HomeAssistant): assert await async_setup_component(hass, tplink.DOMAIN, config) await hass.async_block_till_done() - assert hass.data.get(tplink.DOMAIN) - assert hass.data[tplink.DOMAIN].get(COORDINATORS) - assert hass.data[tplink.DOMAIN][COORDINATORS].get(strip.mac) - assert isinstance( - hass.data[tplink.DOMAIN][COORDINATORS][strip.mac], - tplink.SmartPlugDataUpdateCoordinator, - ) - data = hass.data[tplink.DOMAIN][COORDINATORS][strip.mac].data - assert data[CONF_ALIAS] == strip.sys_info["children"][0]["alias"] - assert data[CONF_DEVICE_ID] == "1" + entities = hass.states.async_entity_ids(SWITCH_DOMAIN) + assert len(entities) == 3 async def test_no_config_creates_no_entry(hass):