2016-07-10 16:48:02 +00:00
|
|
|
"""
|
2016-07-13 09:10:31 +00:00
|
|
|
Support for TPLink HS100/HS110 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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
REQUIREMENTS = ['https://github.com/gadgetreactor/pyHS100/archive/'
|
2016-10-07 18:36:43 +00:00
|
|
|
'ef85f939fd5b07064a0f34dfa673fa7d6140bd95.zip#pyHS100==0.1.2']
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2016-09-04 02:09:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'TPLink Switch HS100'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
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):
|
2016-07-13 09:10:31 +00:00
|
|
|
"""Setup the TPLink switch platform."""
|
2016-07-10 16:48:02 +00:00
|
|
|
from pyHS100.pyHS100 import SmartPlug
|
|
|
|
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-09-04 02:09:02 +00:00
|
|
|
add_devices([SmartPlugSwitch(SmartPlug(host), name)])
|
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
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
@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."""
|
|
|
|
return self.smartplug.state == 'ON'
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
|
|
|
self.smartplug.state = 'ON'
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn the switch off."""
|
|
|
|
self.smartplug.state = 'OFF'
|