mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Clean up dlink and some bug fix (#16346)
* Update dlink.py * style * style
This commit is contained in:
parent
e61ac1a4a1
commit
85658b6dd1
@ -5,14 +5,17 @@ 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/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
import urllib
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.const import TEMP_CELSIUS, STATE_UNKNOWN
|
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']
|
||||||
|
|
||||||
@ -23,9 +26,7 @@ DEFAULT_PASSWORD = ''
|
|||||||
DEFAULT_USERNAME = 'admin'
|
DEFAULT_USERNAME = 'admin'
|
||||||
CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol'
|
CONF_USE_LEGACY_PROTOCOL = 'use_legacy_protocol'
|
||||||
|
|
||||||
ATTR_CURRENT_CONSUMPTION = 'power_consumption'
|
|
||||||
ATTR_TOTAL_CONSUMPTION = 'total_consumption'
|
ATTR_TOTAL_CONSUMPTION = 'total_consumption'
|
||||||
ATTR_TEMPERATURE = 'temperature'
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
@ -35,6 +36,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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."""
|
||||||
@ -46,10 +49,11 @@ 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)
|
||||||
|
|
||||||
data = SmartPlugData(SmartPlug(host,
|
smartplug = SmartPlug(host,
|
||||||
password,
|
password,
|
||||||
username,
|
username,
|
||||||
use_legacy_protocol))
|
use_legacy_protocol)
|
||||||
|
data = SmartPlugData(smartplug)
|
||||||
|
|
||||||
add_entities([SmartPlugSwitch(hass, data, name)], True)
|
add_entities([SmartPlugSwitch(hass, data, name)], True)
|
||||||
|
|
||||||
@ -74,37 +78,28 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
try:
|
try:
|
||||||
ui_temp = self.units.temperature(int(self.data.temperature),
|
ui_temp = self.units.temperature(int(self.data.temperature),
|
||||||
TEMP_CELSIUS)
|
TEMP_CELSIUS)
|
||||||
temperature = "%i %s" % \
|
temperature = ui_temp
|
||||||
(ui_temp, self.units.temperature_unit)
|
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
temperature = STATE_UNKNOWN
|
temperature = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_consumption = "%.2f W" % \
|
total_consumption = float(self.data.total_consumption)
|
||||||
float(self.data.current_consumption)
|
except (ValueError, TypeError):
|
||||||
except ValueError:
|
total_consumption = None
|
||||||
current_consumption = STATE_UNKNOWN
|
|
||||||
|
|
||||||
try:
|
|
||||||
total_consumption = "%.1f kWh" % \
|
|
||||||
float(self.data.total_consumption)
|
|
||||||
except ValueError:
|
|
||||||
total_consumption = STATE_UNKNOWN
|
|
||||||
|
|
||||||
attrs = {
|
attrs = {
|
||||||
ATTR_CURRENT_CONSUMPTION: current_consumption,
|
|
||||||
ATTR_TOTAL_CONSUMPTION: total_consumption,
|
ATTR_TOTAL_CONSUMPTION: total_consumption,
|
||||||
ATTR_TEMPERATURE: temperature
|
ATTR_TEMPERATURE: temperature,
|
||||||
}
|
}
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_power_watt(self):
|
def current_power_w(self):
|
||||||
"""Return the current power usage in Watt."""
|
"""Return the current power usage in Watt."""
|
||||||
try:
|
try:
|
||||||
return float(self.data.current_consumption)
|
return float(self.data.current_consumption)
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -124,6 +119,11 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
"""Get the latest data from the smart plug and updates the states."""
|
"""Get the latest data from the smart plug and updates the states."""
|
||||||
self.data.update()
|
self.data.update()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return True if entity is available."""
|
||||||
|
return self.data.available
|
||||||
|
|
||||||
|
|
||||||
class SmartPlugData:
|
class SmartPlugData:
|
||||||
"""Get the latest data from smart plug."""
|
"""Get the latest data from smart plug."""
|
||||||
@ -135,10 +135,34 @@ class SmartPlugData:
|
|||||||
self.temperature = None
|
self.temperature = None
|
||||||
self.current_consumption = None
|
self.current_consumption = None
|
||||||
self.total_consumption = None
|
self.total_consumption = None
|
||||||
|
self.available = False
|
||||||
|
self._n_tried = 0
|
||||||
|
self._last_tried = None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from the smart plug."""
|
"""Get the latest data from the smart plug."""
|
||||||
self.state = self.smartplug.state
|
if self._last_tried is not None:
|
||||||
|
last_try_s = (dt_util.now() - self._last_tried).total_seconds()/60
|
||||||
|
retry_seconds = min(self._n_tried*2, 10) - last_try_s
|
||||||
|
if self._n_tried > 0 and retry_seconds > 0:
|
||||||
|
_LOGGER.warning("Waiting %s s to retry", retry_seconds)
|
||||||
|
return
|
||||||
|
|
||||||
|
_state = 'unknown'
|
||||||
|
try:
|
||||||
|
self._last_tried = dt_util.now()
|
||||||
|
_state = self.smartplug.state
|
||||||
|
except urllib.error.HTTPError:
|
||||||
|
_LOGGER.error("Dlink connection problem")
|
||||||
|
if _state == 'unknown':
|
||||||
|
self._n_tried += 1
|
||||||
|
self.available = False
|
||||||
|
_LOGGER.warning("Failed to connect to dlink switch.")
|
||||||
|
return
|
||||||
|
self.state = _state
|
||||||
|
self.available = True
|
||||||
|
|
||||||
self.temperature = self.smartplug.temperature
|
self.temperature = self.smartplug.temperature
|
||||||
self.current_consumption = self.smartplug.current_consumption
|
self.current_consumption = self.smartplug.current_consumption
|
||||||
self.total_consumption = self.smartplug.total_consumption
|
self.total_consumption = self.smartplug.total_consumption
|
||||||
|
self._n_tried = 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user