2016-07-10 16:48:02 +00:00
|
|
|
"""
|
2016-12-17 21:49:43 +00:00
|
|
|
Support for TPLink HS100/HS110/HS200 smart switch.
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2016-07-13 09:10:31 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.tplink/
|
|
|
|
"""
|
2016-09-04 02:09:02 +00:00
|
|
|
import logging
|
|
|
|
|
2016-11-11 06:43:16 +00:00
|
|
|
import time
|
|
|
|
|
2016-09-04 02:09:02 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
|
|
|
from homeassistant.const import (CONF_HOST, CONF_NAME)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2017-04-08 13:33:25 +00:00
|
|
|
REQUIREMENTS = ['pyHS100==0.2.4.2']
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2016-09-04 02:09:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-09-28 19:12:02 +00:00
|
|
|
ATTR_CURRENT_CONSUMPTION = 'current_consumption'
|
|
|
|
ATTR_TOTAL_CONSUMPTION = 'total_consumption'
|
|
|
|
ATTR_DAILY_CONSUMPTION = 'daily_consumption'
|
|
|
|
ATTR_VOLTAGE = 'voltage'
|
|
|
|
ATTR_CURRENT = 'current'
|
2016-10-22 04:45:36 +00:00
|
|
|
|
2016-09-04 02:09:02 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
2016-12-17 21:49:43 +00:00
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
2016-09-04 02:09:02 +00:00
|
|
|
})
|
|
|
|
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2016-07-13 09:10:31 +00:00
|
|
|
# pylint: disable=unused-argument
|
2016-09-04 02:09:02 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the TPLink switch platform."""
|
2016-12-17 21:49:43 +00:00
|
|
|
from pyHS100 import SmartPlug
|
2016-07-10 16:48:02 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2016-09-04 02:09:02 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2016-11-02 19:23:43 +00:00
|
|
|
add_devices([SmartPlugSwitch(SmartPlug(host), name)], True)
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SmartPlugSwitch(SwitchDevice):
|
|
|
|
"""Representation of a TPLink Smart Plug switch."""
|
|
|
|
|
|
|
|
def __init__(self, smartplug, name):
|
|
|
|
"""Initialize the switch."""
|
|
|
|
self.smartplug = smartplug
|
2016-12-17 21:49:43 +00:00
|
|
|
|
|
|
|
# Use the name set on the device if not set
|
|
|
|
if name is None:
|
|
|
|
self._name = self.smartplug.alias
|
|
|
|
else:
|
|
|
|
self._name = name
|
|
|
|
|
2016-10-22 04:45:36 +00:00
|
|
|
self._state = None
|
2016-12-17 21:49:43 +00:00
|
|
|
_LOGGER.debug("Setting up TP-Link Smart Plug")
|
2016-10-22 04:45:36 +00:00
|
|
|
# Set up emeter cache
|
|
|
|
self._emeter_params = {}
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the Smart Plug, if any."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2017-01-28 06:45:57 +00:00
|
|
|
return self._state
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
2016-12-17 21:49:43 +00:00
|
|
|
self.smartplug.turn_on()
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn the switch off."""
|
2016-12-17 21:49:43 +00:00
|
|
|
self.smartplug.turn_off()
|
2016-10-22 04:45:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
return self._emeter_params
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the TP-Link switch's state."""
|
2017-03-05 07:44:34 +00:00
|
|
|
from pyHS100 import SmartPlugException
|
2016-11-11 06:43:16 +00:00
|
|
|
try:
|
2017-01-28 06:45:57 +00:00
|
|
|
self._state = self.smartplug.state == \
|
|
|
|
self.smartplug.SWITCH_STATE_ON
|
2016-11-11 06:43:16 +00:00
|
|
|
|
2016-12-17 21:49:43 +00:00
|
|
|
if self.smartplug.has_emeter:
|
2016-11-11 06:43:16 +00:00
|
|
|
emeter_readings = self.smartplug.get_emeter_realtime()
|
|
|
|
|
|
|
|
self._emeter_params[ATTR_CURRENT_CONSUMPTION] \
|
|
|
|
= "%.1f W" % emeter_readings["power"]
|
|
|
|
self._emeter_params[ATTR_TOTAL_CONSUMPTION] \
|
|
|
|
= "%.2f kW" % emeter_readings["total"]
|
|
|
|
self._emeter_params[ATTR_VOLTAGE] \
|
|
|
|
= "%.2f V" % emeter_readings["voltage"]
|
|
|
|
self._emeter_params[ATTR_CURRENT] \
|
|
|
|
= "%.1f A" % emeter_readings["current"]
|
|
|
|
|
|
|
|
emeter_statics = self.smartplug.get_emeter_daily()
|
2016-11-22 03:34:48 +00:00
|
|
|
try:
|
|
|
|
self._emeter_params[ATTR_DAILY_CONSUMPTION] \
|
|
|
|
= "%.2f kW" % emeter_statics[int(time.strftime("%e"))]
|
|
|
|
except KeyError:
|
|
|
|
# device returned no daily history
|
|
|
|
pass
|
2016-11-11 06:43:16 +00:00
|
|
|
|
2017-03-05 07:44:34 +00:00
|
|
|
except (SmartPlugException, OSError) as ex:
|
|
|
|
_LOGGER.warning('Could not read state for %s: %s', self.name, ex)
|