mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Fix DLink async I/O (#4301)
This commit is contained in:
parent
b0e3d5a576
commit
a862bc4edc
@ -48,20 +48,21 @@ def setup_platform(hass, config, add_devices, 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)
|
||||||
|
|
||||||
add_devices([SmartPlugSwitch(hass, SmartPlug(host,
|
data = SmartPlugData(SmartPlug(host,
|
||||||
password,
|
password,
|
||||||
username,
|
username,
|
||||||
use_legacy_protocol),
|
use_legacy_protocol))
|
||||||
name)])
|
|
||||||
|
add_devices([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, smartplug, name):
|
def __init__(self, hass, data, name):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
self.units = hass.config.units
|
self.units = hass.config.units
|
||||||
self.smartplug = smartplug
|
self.data = data
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -73,7 +74,7 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
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.smartplug.temperature),
|
ui_temp = self.units.temperature(int(self.data.temperature),
|
||||||
TEMP_CELSIUS)
|
TEMP_CELSIUS)
|
||||||
temperature = "%i %s" % \
|
temperature = "%i %s" % \
|
||||||
(ui_temp, self.units.temperature_unit)
|
(ui_temp, self.units.temperature_unit)
|
||||||
@ -82,13 +83,13 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
current_consumption = "%.2f W" % \
|
current_consumption = "%.2f W" % \
|
||||||
float(self.smartplug.current_consumption)
|
float(self.data.current_consumption)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
current_consumption = STATE_UNKNOWN
|
current_consumption = STATE_UNKNOWN
|
||||||
|
|
||||||
try:
|
try:
|
||||||
total_consumption = "%.1f kWh" % \
|
total_consumption = "%.1f kWh" % \
|
||||||
float(self.smartplug.total_consumption)
|
float(self.data.total_consumption)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
total_consumption = STATE_UNKNOWN
|
total_consumption = STATE_UNKNOWN
|
||||||
|
|
||||||
@ -104,19 +105,42 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
def current_power_watt(self):
|
def current_power_watt(self):
|
||||||
"""Return the current power usage in Watt."""
|
"""Return the current power usage in Watt."""
|
||||||
try:
|
try:
|
||||||
return float(self.smartplug.current_consumption)
|
return float(self.data.current_consumption)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if switch is on."""
|
"""Return true if switch is on."""
|
||||||
return self.smartplug.state == 'ON'
|
return self.data.state == 'ON'
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
self.smartplug.state = 'ON'
|
self.data.smartplug.state = 'ON'
|
||||||
|
|
||||||
def turn_off(self):
|
def turn_off(self):
|
||||||
"""Turn the switch off."""
|
"""Turn the switch off."""
|
||||||
self.smartplug.state = 'OFF'
|
self.data.smartplug.state = 'OFF'
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Get the latest data from the smart plug and updates the states."""
|
||||||
|
self.data.update()
|
||||||
|
|
||||||
|
|
||||||
|
class SmartPlugData(object):
|
||||||
|
"""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
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Get the latest data from the smart plug."""
|
||||||
|
self.state = self.smartplug.state
|
||||||
|
self.temperature = self.smartplug.temperature
|
||||||
|
self.current_consumption = self.smartplug.current_consumption
|
||||||
|
self.total_consumption = self.smartplug.total_consumption
|
||||||
|
Loading…
x
Reference in New Issue
Block a user