Skip to content

Commit

Permalink
Merge e88cfb5 into b789649
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff committed Oct 26, 2018
2 parents b789649 + e88cfb5 commit 7f70d0f
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions homeassistant/components/switch/dlink.py
@@ -1,43 +1,44 @@
"""
Support for D-link W215 smart switch.
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/
"""
from datetime import timedelta
import logging
import urllib
from datetime import timedelta

import voluptuous as vol

from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import (
ATTR_TEMPERATURE, CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME,
TEMP_CELSIUS)
import homeassistant.helpers.config_validation as cv
from homeassistant.util import dt as dt_util
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (ATTR_TEMPERATURE,
CONF_HOST, CONF_NAME, CONF_PASSWORD,
CONF_USERNAME, TEMP_CELSIUS)

REQUIREMENTS = ['pyW215==0.6.0']

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'D-link Smart Plug W215'
ATTR_TOTAL_CONSUMPTION = 'total_consumption'

CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol'

DEFAULT_NAME = "D-Link Smart Plug W215"
DEFAULT_PASSWORD = ''
DEFAULT_USERNAME = 'admin'
CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol'

ATTR_TOTAL_CONSUMPTION = 'total_consumption'
SCAN_INTERVAL = timedelta(minutes=2)

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.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_USE_LEGACY_PROTOCOL, default=False): cv.boolean,
})

SCAN_INTERVAL = timedelta(minutes=2)


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up a D-Link Smart Plug."""
Expand All @@ -49,17 +50,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
use_legacy_protocol = config.get(CONF_USE_LEGACY_PROTOCOL)
name = config.get(CONF_NAME)

smartplug = SmartPlug(host,
password,
username,
use_legacy_protocol)
smartplug = SmartPlug(host, password, username, use_legacy_protocol)
data = SmartPlugData(smartplug)

add_entities([SmartPlugSwitch(hass, data, name)], True)


class SmartPlugSwitch(SwitchDevice):
"""Representation of a D-link Smart Plug switch."""
"""Representation of a D-Link Smart Plug switch."""

def __init__(self, hass, data, name):
"""Initialize the switch."""
Expand All @@ -69,15 +67,15 @@ def __init__(self, hass, data, name):

@property
def name(self):
"""Return the name of the Smart Plug, if any."""
"""Return the name of the Smart Plug."""
return self._name

@property
def device_state_attributes(self):
"""Return the state attributes of the device."""
try:
ui_temp = self.units.temperature(int(self.data.temperature),
TEMP_CELSIUS)
ui_temp = self.units.temperature(
int(self.data.temperature), TEMP_CELSIUS)
temperature = ui_temp
except (ValueError, TypeError):
temperature = None
Expand Down Expand Up @@ -149,16 +147,18 @@ def update(self):
return

_state = 'unknown'

try:
self._last_tried = dt_util.now()
_state = self.smartplug.state
except urllib.error.HTTPError:
_LOGGER.error("Dlink connection problem")
_LOGGER.error("D-Link connection problem")
if _state == 'unknown':
self._n_tried += 1
self.available = False
_LOGGER.warning("Failed to connect to dlink switch.")
_LOGGER.warning("Failed to connect to D-Link switch")
return

self.state = _state
self.available = True

Expand Down

0 comments on commit 7f70d0f

Please sign in to comment.