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:
Dan Nixon 2017-12-04 07:33:22 +00:00 committed by Fabian Affolter
parent 4e02300cbc
commit 17f3cf0389

View file

@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.tplink/
"""
import logging
import time
import voluptuous as vol
@ -47,6 +46,7 @@ class SmartPlugSwitch(SwitchDevice):
self.smartplug = smartplug
self._name = name
self._state = None
self._available = True
# Set up emeter cache
self._emeter_params = {}
@ -55,6 +55,11 @@ class SmartPlugSwitch(SwitchDevice):
"""Return the name of the Smart Plug, if any."""
return self._name
@property
def available(self) -> bool:
"""Return if switch is available."""
return self._available
@property
def is_on(self):
"""Return true if switch is on."""
@ -77,6 +82,7 @@ class SmartPlugSwitch(SwitchDevice):
"""Update the TP-Link switch's state."""
from pyHS100 import SmartDeviceException
try:
self._available = True
self._state = self.smartplug.state == \
self.smartplug.SWITCH_STATE_ON
@ -100,8 +106,9 @@ class SmartPlugSwitch(SwitchDevice):
self._emeter_params[ATTR_DAILY_CONSUMPTION] \
= "%.2f kW" % emeter_statics[int(time.strftime("%e"))]
except KeyError:
# device returned no daily history
# Device returned no daily history
pass
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