2019-04-03 15:40:03 +00:00
|
|
|
"""Support for D-Link W215 smart switch."""
|
2018-10-26 13:45:57 +00:00
|
|
|
from datetime import timedelta
|
2016-02-20 20:17:48 +00:00
|
|
|
import logging
|
2018-09-04 08:50:12 +00:00
|
|
|
import urllib
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2019-12-05 05:22:37 +00:00
|
|
|
from pyW215.pyW215 import SmartPlug
|
2016-08-20 22:25:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2018-10-26 13:45:57 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TEMPERATURE,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
2020-09-11 20:05:07 +00:00
|
|
|
STATE_UNKNOWN,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2016-08-20 22:25:11 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-09-04 08:50:12 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2016-02-20 20:17:48 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TOTAL_CONSUMPTION = "total_consumption"
|
2018-10-26 13:45:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_USE_LEGACY_PROTOCOL = "use_legacy_protocol"
|
2018-10-26 13:45:57 +00:00
|
|
|
|
|
|
|
DEFAULT_NAME = "D-Link Smart Plug W215"
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_PASSWORD = ""
|
|
|
|
DEFAULT_USERNAME = "admin"
|
2016-09-06 17:52:22 +00:00
|
|
|
|
2018-10-26 13:45:57 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=2)
|
2016-08-20 22:25:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
2016-08-20 22:25:11 +00:00
|
|
|
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up a D-Link Smart Plug."""
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2020-04-15 12:10:07 +00:00
|
|
|
host = config[CONF_HOST]
|
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
|
|
|
use_legacy_protocol = config[CONF_USE_LEGACY_PROTOCOL]
|
|
|
|
name = config[CONF_NAME]
|
2016-08-20 22:25:11 +00:00
|
|
|
|
2018-10-26 13:45:57 +00:00
|
|
|
smartplug = SmartPlug(host, password, username, use_legacy_protocol)
|
2018-09-04 08:50:12 +00:00
|
|
|
data = SmartPlugData(smartplug)
|
2016-11-17 02:55:58 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([SmartPlugSwitch(hass, data, name)], True)
|
2016-02-20 20:17:48 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class SmartPlugSwitch(SwitchEntity):
|
2018-10-26 13:45:57 +00:00
|
|
|
"""Representation of a D-Link Smart Plug switch."""
|
2016-03-08 12:35:39 +00:00
|
|
|
|
2016-11-17 02:55:58 +00:00
|
|
|
def __init__(self, hass, data, name):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the switch."""
|
2016-09-06 17:52:22 +00:00
|
|
|
self.units = hass.config.units
|
2016-11-17 02:55:58 +00:00
|
|
|
self.data = data
|
2016-02-20 20:17:48 +00:00
|
|
|
self._name = name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2018-10-26 13:45:57 +00:00
|
|
|
"""Return the name of the Smart Plug."""
|
2016-02-20 20:17:48 +00:00
|
|
|
return self._name
|
|
|
|
|
2016-09-06 17:52:22 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the device."""
|
2016-09-20 07:10:15 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
ui_temp = self.units.temperature(int(self.data.temperature), TEMP_CELSIUS)
|
2018-09-04 08:50:12 +00:00
|
|
|
temperature = ui_temp
|
2016-11-29 16:40:51 +00:00
|
|
|
except (ValueError, TypeError):
|
2018-09-04 08:50:12 +00:00
|
|
|
temperature = None
|
2016-09-20 07:10:15 +00:00
|
|
|
|
|
|
|
try:
|
2018-09-04 08:50:12 +00:00
|
|
|
total_consumption = float(self.data.total_consumption)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
total_consumption = None
|
2016-09-06 17:52:22 +00:00
|
|
|
|
|
|
|
attrs = {
|
|
|
|
ATTR_TOTAL_CONSUMPTION: total_consumption,
|
2018-09-04 08:50:12 +00:00
|
|
|
ATTR_TEMPERATURE: temperature,
|
2016-09-06 17:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
2016-02-20 20:17:48 +00:00
|
|
|
@property
|
2018-09-04 08:50:12 +00:00
|
|
|
def current_power_w(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the current power usage in Watt."""
|
2016-02-20 20:17:48 +00:00
|
|
|
try:
|
2016-11-17 02:55:58 +00:00
|
|
|
return float(self.data.current_consumption)
|
2018-09-04 08:50:12 +00:00
|
|
|
except (ValueError, TypeError):
|
2016-02-20 20:17:48 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if switch is on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.data.state == "ON"
|
2016-02-20 20:17:48 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.data.smartplug.state = "ON"
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.data.smartplug.state = "OFF"
|
2016-11-17 02:55:58 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from the smart plug and updates the states."""
|
|
|
|
self.data.update()
|
|
|
|
|
2018-09-04 08:50:12 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.data.available
|
|
|
|
|
2016-11-17 02:55:58 +00:00
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class SmartPlugData:
|
2016-11-17 02:55:58 +00:00
|
|
|
"""Get the latest data from smart plug."""
|
|
|
|
|
|
|
|
def __init__(self, smartplug):
|
|
|
|
"""Initialize the data object."""
|
|
|
|
self.smartplug = smartplug
|
|
|
|
self.state = None
|
|
|
|
self.temperature = None
|
|
|
|
self.current_consumption = None
|
|
|
|
self.total_consumption = None
|
2018-09-04 08:50:12 +00:00
|
|
|
self.available = False
|
|
|
|
self._n_tried = 0
|
|
|
|
self._last_tried = None
|
2016-11-17 02:55:58 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from the smart plug."""
|
2018-09-04 08:50:12 +00:00
|
|
|
if self._last_tried is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
last_try_s = (dt_util.now() - self._last_tried).total_seconds() / 60
|
|
|
|
retry_seconds = min(self._n_tried * 2, 10) - last_try_s
|
2018-09-04 08:50:12 +00:00
|
|
|
if self._n_tried > 0 and retry_seconds > 0:
|
|
|
|
_LOGGER.warning("Waiting %s s to retry", retry_seconds)
|
|
|
|
return
|
|
|
|
|
2020-09-11 20:05:07 +00:00
|
|
|
_state = STATE_UNKNOWN
|
2018-10-26 13:45:57 +00:00
|
|
|
|
2018-09-04 08:50:12 +00:00
|
|
|
try:
|
|
|
|
self._last_tried = dt_util.now()
|
|
|
|
_state = self.smartplug.state
|
|
|
|
except urllib.error.HTTPError:
|
2018-10-26 13:45:57 +00:00
|
|
|
_LOGGER.error("D-Link connection problem")
|
2020-09-11 20:05:07 +00:00
|
|
|
if _state == STATE_UNKNOWN:
|
2018-09-04 08:50:12 +00:00
|
|
|
self._n_tried += 1
|
|
|
|
self.available = False
|
2018-10-26 13:45:57 +00:00
|
|
|
_LOGGER.warning("Failed to connect to D-Link switch")
|
2018-09-04 08:50:12 +00:00
|
|
|
return
|
2018-10-26 13:45:57 +00:00
|
|
|
|
2018-09-04 08:50:12 +00:00
|
|
|
self.state = _state
|
|
|
|
self.available = True
|
|
|
|
|
2016-11-17 02:55:58 +00:00
|
|
|
self.temperature = self.smartplug.temperature
|
|
|
|
self.current_consumption = self.smartplug.current_consumption
|
|
|
|
self.total_consumption = self.smartplug.total_consumption
|
2018-09-04 08:50:12 +00:00
|
|
|
self._n_tried = 0
|