From ab22c617646387af18f088aaf26aba348d606bce Mon Sep 17 00:00:00 2001 From: Matt Kasa Date: Tue, 22 Oct 2019 22:46:18 -0700 Subject: [PATCH] Support SmartStrip type devices (HS300, HS107) in tplink component (#26220) * Add support for SmartStrip type devices (HS300, HS107) to tplink component * Incorporate feedback from @MartinHjelmare using changes suggested by @shbatm - Setting `_state` now uses a list comprehension - `_alias` will use aliases from the Kasa app - `_device_id` will be set to `_mac` for single plugs to retain backwards compatibility --- homeassistant/components/tplink/__init__.py | 4 ++++ homeassistant/components/tplink/common.py | 20 +++++++++++++++--- homeassistant/components/tplink/switch.py | 23 ++++++++++++++++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/tplink/__init__.py b/homeassistant/components/tplink/__init__.py index 85258b5e94e..7aa261564f3 100644 --- a/homeassistant/components/tplink/__init__.py +++ b/homeassistant/components/tplink/__init__.py @@ -14,6 +14,7 @@ from .common import ( CONF_DISCOVERY, CONF_LIGHT, CONF_SWITCH, + CONF_STRIP, SmartDevices, async_discover_devices, get_static_devices, @@ -36,6 +37,9 @@ CONFIG_SCHEMA = vol.Schema( vol.Optional(CONF_SWITCH, default=[]): vol.All( cv.ensure_list, [TPLINK_HOST_SCHEMA] ), + vol.Optional(CONF_STRIP, default=[]): vol.All( + cv.ensure_list, [TPLINK_HOST_SCHEMA] + ), vol.Optional(CONF_DIMMER, default=[]): vol.All( cv.ensure_list, [TPLINK_HOST_SCHEMA] ), diff --git a/homeassistant/components/tplink/common.py b/homeassistant/components/tplink/common.py index 75636c8dc28..548edc6822c 100644 --- a/homeassistant/components/tplink/common.py +++ b/homeassistant/components/tplink/common.py @@ -4,7 +4,14 @@ from datetime import timedelta import logging from typing import Any, Callable, List -from pyHS100 import Discover, SmartBulb, SmartDevice, SmartDeviceException, SmartPlug +from pyHS100 import ( + Discover, + SmartBulb, + SmartDevice, + SmartDeviceException, + SmartPlug, + SmartStrip, +) from homeassistant.helpers.typing import HomeAssistantType @@ -15,6 +22,7 @@ ATTR_CONFIG = "config" CONF_DIMMER = "dimmer" CONF_DISCOVERY = "discovery" CONF_LIGHT = "light" +CONF_STRIP = "strip" CONF_SWITCH = "switch" @@ -74,7 +82,10 @@ async def async_discover_devices( if existing_devices.has_device_with_host(dev.host): continue - if isinstance(dev, SmartPlug): + if isinstance(dev, SmartStrip): + for plug in dev.plugs.values(): + switches.append(plug) + elif isinstance(dev, SmartPlug): try: if dev.is_dimmable: # Dimmers act as lights lights.append(dev) @@ -99,7 +110,7 @@ def get_static_devices(config_data) -> SmartDevices: lights = [] switches = [] - for type_ in [CONF_LIGHT, CONF_SWITCH, CONF_DIMMER]: + for type_ in [CONF_LIGHT, CONF_SWITCH, CONF_STRIP, CONF_DIMMER]: for entry in config_data[type_]: host = entry["host"] @@ -107,6 +118,9 @@ def get_static_devices(config_data) -> SmartDevices: lights.append(SmartBulb(host)) elif type_ == CONF_SWITCH: switches.append(SmartPlug(host)) + elif type_ == CONF_STRIP: + for plug in SmartStrip(host).plugs.values(): + switches.append(plug) # Dimmers need to be defined as smart plugs to work correctly. elif type_ == CONF_DIMMER: lights.append(SmartPlug(host)) diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index ebeac984515..791d358c509 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -69,11 +69,12 @@ class SmartPlugSwitch(SwitchDevice): self._mac = None self._alias = None self._model = None + self._device_id = None @property def unique_id(self): """Return a unique ID.""" - return self._mac + return self._device_id @property def name(self): @@ -120,10 +121,26 @@ class SmartPlugSwitch(SwitchDevice): if not self._sysinfo: self._sysinfo = self.smartplug.sys_info self._mac = self.smartplug.mac - self._alias = self.smartplug.alias self._model = self.smartplug.model + if self.smartplug.context is None: + self._alias = self.smartplug.alias + self._device_id = self._mac + else: + self._alias = [ + child + for child in self.smartplug.sys_info["children"] + if child["id"] == self.smartplug.context + ][0]["alias"] + self._device_id = self.smartplug.context - self._state = self.smartplug.state == self.smartplug.SWITCH_STATE_ON + if self.smartplug.context is None: + self._state = self.smartplug.state == self.smartplug.SWITCH_STATE_ON + else: + self._state = [ + child + for child in self.smartplug.sys_info["children"] + if child["id"] == self.smartplug.context + ][0]["state"] == 1 if self.smartplug.has_emeter: emeter_readings = self.smartplug.get_emeter_realtime()