diff --git a/.coveragerc b/.coveragerc index 376648c36f0..aa010d67b2b 100644 --- a/.coveragerc +++ b/.coveragerc @@ -142,6 +142,7 @@ omit = homeassistant/components/sensor/worldclock.py homeassistant/components/switch/arest.py homeassistant/components/switch/edimax.py + homeassistant/components/switch/dlink.py homeassistant/components/switch/hikvisioncam.py homeassistant/components/switch/mystrom.py homeassistant/components/switch/orvibo.py diff --git a/homeassistant/components/switch/dlink.py b/homeassistant/components/switch/dlink.py new file mode 100644 index 00000000000..00b8ac0dab1 --- /dev/null +++ b/homeassistant/components/switch/dlink.py @@ -0,0 +1,79 @@ +""" +homeassistant.components.switch.dlink +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +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 + +from homeassistant.components.switch import DOMAIN, SwitchDevice +from homeassistant.const import ( + CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME) +from homeassistant.helpers import validate_config + +# constants +DEFAULT_USERNAME = 'admin' +DEFAULT_PASSWORD = '' +DEVICE_DEFAULT_NAME = 'D-link Smart Plug W215' +REQUIREMENTS = ['https://github.com/LinuxChristian/pyW215/archive/' + 'v0.1.1.zip#pyW215==0.1.1'] + +# setup logger +_LOGGER = logging.getLogger(__name__) + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices_callback, discovery_info=None): + """ Find and return D-Link Smart Plugs. """ + from pyW215.pyW215 import SmartPlug + + # check for required values in configuration file + if not validate_config({DOMAIN: config}, + {DOMAIN: [CONF_HOST]}, + _LOGGER): + return False + + host = config.get(CONF_HOST) + username = config.get(CONF_USERNAME, DEFAULT_USERNAME) + password = str(config.get(CONF_PASSWORD, DEFAULT_PASSWORD)) + name = config.get(CONF_NAME, DEVICE_DEFAULT_NAME) + + add_devices_callback([SmartPlugSwitch(SmartPlug(host, + password, + username), + name)]) + + +class SmartPlugSwitch(SwitchDevice): + """ Represents an d-link Smart Plug switch. """ + def __init__(self, smartplug, name): + self.smartplug = smartplug + self._name = name + + @property + def name(self): + """ Returns the name of the Smart Plug, if any. """ + return self._name + + @property + def current_power_watt(self): + """ Current power usage in watt. """ + try: + return float(self.smartplug.current_consumption) + except ValueError: + return None + + @property + def is_on(self): + """ True if switch is on. """ + return self.smartplug.state == 'ON' + + def turn_on(self, **kwargs): + """ Turns the switch on. """ + self.smartplug.state = 'ON' + + def turn_off(self): + """ Turns the switch off. """ + self.smartplug.state = 'OFF' diff --git a/requirements_all.txt b/requirements_all.txt index f251d900deb..c3e56d357f7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -69,6 +69,9 @@ https://github.com/Danielhiversen/pyRFXtrx/archive/0.4.zip#RFXtrx==0.4 # homeassistant.components.sensor.netatmo https://github.com/HydrelioxGitHub/netatmo-api-python/archive/43ff238a0122b0939a0dc4e8836b6782913fb6e2.zip#lnetatmo==0.4.0 +# homeassistant.components.switch.dlink +https://github.com/LinuxChristian/pyW215/archive/v0.1.1.zip#pyW215==0.1.1 + # homeassistant.components.alarm_control_panel.alarmdotcom https://github.com/Xorso/pyalarmdotcom/archive/0.1.1.zip#pyalarmdotcom==0.1.1