Minor changes (#17812)

This commit is contained in:
Fabian Affolter 2018-10-26 15:45:57 +02:00 committed by GitHub
parent 92bad453f2
commit 434c848104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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'
DEFAULT_PASSWORD = ''
DEFAULT_USERNAME = 'admin'
ATTR_TOTAL_CONSUMPTION = 'total_consumption'
CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol'
ATTR_TOTAL_CONSUMPTION = 'total_consumption'
DEFAULT_NAME = "D-Link Smart Plug W215"
DEFAULT_PASSWORD = ''
DEFAULT_USERNAME = 'admin'
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."""
@ -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."""
@ -69,15 +67,15 @@ class SmartPlugSwitch(SwitchDevice):
@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
@ -149,16 +147,18 @@ class SmartPlugData:
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