2016-02-20 20:17:48 +00:00
|
|
|
"""
|
|
|
|
Support for D-link W215 smart switch.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.dlink/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-08-20 22:25:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
2016-02-20 20:17:48 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME)
|
2016-08-20 22:25:11 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-20 20:17:48 +00:00
|
|
|
|
|
|
|
REQUIREMENTS = ['https://github.com/LinuxChristian/pyW215/archive/'
|
|
|
|
'v0.1.1.zip#pyW215==0.1.1']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-20 22:25:11 +00:00
|
|
|
DEFAULT_NAME = 'D-link Smart Plug W215'
|
|
|
|
DEFAULT_PASSWORD = ''
|
|
|
|
DEFAULT_USERNAME = 'admin'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2016-02-20 20:17:48 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
2016-08-20 22:25:11 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
"""Setup a D-Link Smart Plug."""
|
2016-02-20 20:17:48 +00:00
|
|
|
from pyW215.pyW215 import SmartPlug
|
|
|
|
|
|
|
|
host = config.get(CONF_HOST)
|
2016-08-20 22:25:11 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
|
|
|
add_devices([SmartPlugSwitch(SmartPlug(host, password, username), name)])
|
2016-02-20 20:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SmartPlugSwitch(SwitchDevice):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a D-link Smart Plug switch."""
|
|
|
|
|
2016-02-20 20:17:48 +00:00
|
|
|
def __init__(self, smartplug, name):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the switch."""
|
2016-02-20 20:17:48 +00:00
|
|
|
self.smartplug = smartplug
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the Smart Plug, if any."""
|
2016-02-20 20:17:48 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_power_watt(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the current power usage in Watt."""
|
2016-02-20 20:17:48 +00:00
|
|
|
try:
|
|
|
|
return float(self.smartplug.current_consumption)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if switch is on."""
|
2016-02-20 20:17:48 +00:00
|
|
|
return self.smartplug.state == 'ON'
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2016-02-20 20:17:48 +00:00
|
|
|
self.smartplug.state = 'ON'
|
|
|
|
|
|
|
|
def turn_off(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2016-02-20 20:17:48 +00:00
|
|
|
self.smartplug.state = 'OFF'
|