mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Minor changes (#17812)
This commit is contained in:
parent
92bad453f2
commit
434c848104
@ -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
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/switch.dlink/
|
https://home-assistant.io/components/switch.dlink/
|
||||||
"""
|
"""
|
||||||
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import urllib
|
import urllib
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
import voluptuous as vol
|
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.util import dt as dt_util
|
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']
|
REQUIREMENTS = ['pyW215==0.6.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = 'D-link Smart Plug W215'
|
ATTR_TOTAL_CONSUMPTION = 'total_consumption'
|
||||||
DEFAULT_PASSWORD = ''
|
|
||||||
DEFAULT_USERNAME = 'admin'
|
|
||||||
CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol'
|
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({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
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.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_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):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up a D-Link Smart Plug."""
|
"""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)
|
use_legacy_protocol = config.get(CONF_USE_LEGACY_PROTOCOL)
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
|
|
||||||
smartplug = SmartPlug(host,
|
smartplug = SmartPlug(host, password, username, use_legacy_protocol)
|
||||||
password,
|
|
||||||
username,
|
|
||||||
use_legacy_protocol)
|
|
||||||
data = SmartPlugData(smartplug)
|
data = SmartPlugData(smartplug)
|
||||||
|
|
||||||
add_entities([SmartPlugSwitch(hass, data, name)], True)
|
add_entities([SmartPlugSwitch(hass, data, name)], True)
|
||||||
|
|
||||||
|
|
||||||
class SmartPlugSwitch(SwitchDevice):
|
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):
|
def __init__(self, hass, data, name):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
@ -69,15 +67,15 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the Smart Plug, if any."""
|
"""Return the name of the Smart Plug."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes of the device."""
|
"""Return the state attributes of the device."""
|
||||||
try:
|
try:
|
||||||
ui_temp = self.units.temperature(int(self.data.temperature),
|
ui_temp = self.units.temperature(
|
||||||
TEMP_CELSIUS)
|
int(self.data.temperature), TEMP_CELSIUS)
|
||||||
temperature = ui_temp
|
temperature = ui_temp
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
temperature = None
|
temperature = None
|
||||||
@ -149,16 +147,18 @@ class SmartPlugData:
|
|||||||
return
|
return
|
||||||
|
|
||||||
_state = 'unknown'
|
_state = 'unknown'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._last_tried = dt_util.now()
|
self._last_tried = dt_util.now()
|
||||||
_state = self.smartplug.state
|
_state = self.smartplug.state
|
||||||
except urllib.error.HTTPError:
|
except urllib.error.HTTPError:
|
||||||
_LOGGER.error("Dlink connection problem")
|
_LOGGER.error("D-Link connection problem")
|
||||||
if _state == 'unknown':
|
if _state == 'unknown':
|
||||||
self._n_tried += 1
|
self._n_tried += 1
|
||||||
self.available = False
|
self.available = False
|
||||||
_LOGGER.warning("Failed to connect to dlink switch.")
|
_LOGGER.warning("Failed to connect to D-Link switch")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.state = _state
|
self.state = _state
|
||||||
self.available = True
|
self.available = True
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user