mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Report availability of TP-Link smart sockets (#10933)
* Report availability of TP-Link smart sockets * Changes according to our style guide
This commit is contained in:
parent
4e02300cbc
commit
17f3cf0389
@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/switch.tplink/
|
https://home-assistant.io/components/switch.tplink/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -47,6 +46,7 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
self.smartplug = smartplug
|
self.smartplug = smartplug
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = None
|
self._state = None
|
||||||
|
self._available = True
|
||||||
# Set up emeter cache
|
# Set up emeter cache
|
||||||
self._emeter_params = {}
|
self._emeter_params = {}
|
||||||
|
|
||||||
@ -55,6 +55,11 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
"""Return the name of the Smart Plug, if any."""
|
"""Return the name of the Smart Plug, if any."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if switch is available."""
|
||||||
|
return self._available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if switch is on."""
|
"""Return true if switch is on."""
|
||||||
@ -77,6 +82,7 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
"""Update the TP-Link switch's state."""
|
"""Update the TP-Link switch's state."""
|
||||||
from pyHS100 import SmartDeviceException
|
from pyHS100 import SmartDeviceException
|
||||||
try:
|
try:
|
||||||
|
self._available = True
|
||||||
self._state = self.smartplug.state == \
|
self._state = self.smartplug.state == \
|
||||||
self.smartplug.SWITCH_STATE_ON
|
self.smartplug.SWITCH_STATE_ON
|
||||||
|
|
||||||
@ -100,8 +106,9 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
self._emeter_params[ATTR_DAILY_CONSUMPTION] \
|
self._emeter_params[ATTR_DAILY_CONSUMPTION] \
|
||||||
= "%.2f kW" % emeter_statics[int(time.strftime("%e"))]
|
= "%.2f kW" % emeter_statics[int(time.strftime("%e"))]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# device returned no daily history
|
# Device returned no daily history
|
||||||
pass
|
pass
|
||||||
|
|
||||||
except (SmartDeviceException, OSError) as ex:
|
except (SmartDeviceException, OSError) as ex:
|
||||||
_LOGGER.warning('Could not read state for %s: %s', self.name, ex)
|
_LOGGER.warning("Could not read state for %s: %s", self.name, ex)
|
||||||
|
self._available = False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user