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
This commit is contained in:
Matt Kasa 2019-10-22 22:46:18 -07:00 committed by Paulus Schoutsen
parent 09d8a4204a
commit ab22c61764
3 changed files with 41 additions and 6 deletions

View File

@ -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]
),

View File

@ -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))

View File

@ -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()