Skip to content

Commit

Permalink
Report availability of TP-Link smart sockets (#10933)
Browse files Browse the repository at this point in the history
* Report availability of TP-Link smart sockets

* Changes according to our style guide
  • Loading branch information
DanNixon authored and fabaff committed Dec 4, 2017
1 parent 4e02300 commit 17f3cf0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions homeassistant/components/switch/tplink.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
https://home-assistant.io/components/switch.tplink/
"""
import logging

import time

import voluptuous as vol
Expand Down Expand Up @@ -47,6 +46,7 @@ def __init__(self, smartplug, name):
self.smartplug = smartplug
self._name = name
self._state = None
self._available = True
# Set up emeter cache
self._emeter_params = {}

Expand All @@ -55,6 +55,11 @@ def name(self):
"""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."""
Expand All @@ -77,6 +82,7 @@ def update(self):
"""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

Expand All @@ -100,8 +106,9 @@ def update(self):
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

0 comments on commit 17f3cf0

Please sign in to comment.