Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report availability of TP-Link smart sockets #10933

Merged
merged 2 commits into from
Dec 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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