From c1139a9fdacc463b7f5ddacc5f27d0ddaf7dfc44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Br=C3=A6dstrup?= Date: Tue, 6 Sep 2016 19:52:22 +0200 Subject: [PATCH] dlink switch added device state attributes and support for legacy firmware (#3211) --- homeassistant/components/switch/dlink.py | 36 ++++++++++++++++++++++-- requirements_all.txt | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/switch/dlink.py b/homeassistant/components/switch/dlink.py index b65c521bad5..377826695a3 100644 --- a/homeassistant/components/switch/dlink.py +++ b/homeassistant/components/switch/dlink.py @@ -12,20 +12,27 @@ from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) from homeassistant.const import ( CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME) import homeassistant.helpers.config_validation as cv +from homeassistant.const import TEMP_CELSIUS REQUIREMENTS = ['https://github.com/LinuxChristian/pyW215/archive/' - 'v0.1.1.zip#pyW215==0.1.1'] + 'v0.3.4.zip#pyW215==0.3.4'] _LOGGER = logging.getLogger(__name__) DEFAULT_NAME = 'D-link Smart Plug W215' DEFAULT_PASSWORD = '' DEFAULT_USERNAME = 'admin' +CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol' + +ATTR_CURRENT_CONSUMPTION = 'Current Consumption' +ATTR_TOTAL_CONSUMPTION = 'Total Consumption' +ATTR_TEMPERATURE = 'Temperature' 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_USE_LEGACY_PROTOCOL, default=False): cv.boolean, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, }) @@ -38,16 +45,22 @@ def setup_platform(hass, config, add_devices, discovery_info=None): host = config.get(CONF_HOST) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) + use_legacy_protocol = config.get(CONF_USE_LEGACY_PROTOCOL) name = config.get(CONF_NAME) - add_devices([SmartPlugSwitch(SmartPlug(host, password, username), name)]) + add_devices([SmartPlugSwitch(hass, SmartPlug(host, + password, + username, + use_legacy_protocol), + name)]) class SmartPlugSwitch(SwitchDevice): """Representation of a D-link Smart Plug switch.""" - def __init__(self, smartplug, name): + def __init__(self, hass, smartplug, name): """Initialize the switch.""" + self.units = hass.config.units self.smartplug = smartplug self._name = name @@ -56,6 +69,23 @@ class SmartPlugSwitch(SwitchDevice): """Return the name of the Smart Plug, if any.""" return self._name + @property + def device_state_attributes(self): + """Return the state attributes of the device.""" + ui_temp = self.units.temperature(int(self.smartplug.temperature), + TEMP_CELSIUS) + temperature = "{} {}".format(ui_temp, self.units.temperature_unit) + current_consumption = "{} W".format(self.smartplug.current_consumption) + total_consumption = "{} W".format(self.smartplug.total_consumption) + + attrs = { + ATTR_CURRENT_CONSUMPTION: current_consumption, + ATTR_TOTAL_CONSUMPTION: total_consumption, + ATTR_TEMPERATURE: temperature + } + + return attrs + @property def current_power_watt(self): """Return the current power usage in Watt.""" diff --git a/requirements_all.txt b/requirements_all.txt index bd0f7f942ed..65a66692a3b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -126,7 +126,7 @@ hikvision==0.4 https://github.com/Danielhiversen/flux_led/archive/0.6.zip#flux_led==0.6 # homeassistant.components.switch.dlink -https://github.com/LinuxChristian/pyW215/archive/v0.1.1.zip#pyW215==0.1.1 +https://github.com/LinuxChristian/pyW215/archive/v0.3.4.zip#pyW215==0.3.4 # homeassistant.components.media_player.webostv # homeassistant.components.notify.webostv