Report availability of arest (#3614)

This commit is contained in:
William Scanlon authored and Paulus Schoutsen committed 2016-10-04 00:51:45 -07:00
1 parent c93b63963b
commit 8592ba3cb9
2 files changed
+35 -8

No files matched your search

+26 -6
View File
@@ -75,6 +75,7 @@ class ArestSwitchBase(SwitchDevice):
self._resource = resource
self._name = '{} {}'.format(location.title(), name.title())
self._state = None
self._available = True
@property
def name(self):
@@ -86,6 +87,11 @@ class ArestSwitchBase(SwitchDevice):
"""Return true if device is on."""
return self._state
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self._available
class ArestSwitchFunction(ArestSwitchBase):
"""Representation of an aREST switch."""
@@ -136,9 +142,15 @@ class ArestSwitchFunction(ArestSwitchBase):
def update(self):
"""Get the latest data from aREST API and update the state."""
request = requests.get(
'{}/{}'.format(self._resource, self._func), timeout=10)
self._state = request.json()['return_value'] != 0
try:
request = requests.get('{}/{}'.format(self._resource,
self._func), timeout=10)
self._state = request.json()['return_value'] != 0
self._available = True
except requests.exceptions.ConnectionError:
_LOGGER.warning("No route to device %s. Is device offline?",
self._resource)
self._available = False
class ArestSwitchPin(ArestSwitchBase):
@@ -153,6 +165,7 @@ class ArestSwitchPin(ArestSwitchBase):
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?")
self._available = False
def turn_on(self, **kwargs):
"""Turn the device on."""
@@ -176,6 +189,13 @@ class ArestSwitchPin(ArestSwitchBase):
def update(self):
"""Get the latest data from aREST API and update the state."""
request = requests.get(
'{}/digital/{}'.format(self._resource, self._pin), timeout=10)
self._state = request.json()['return_value'] != 0
try:
request = requests.get('{}/digital/{}'.format(self._resource,
self._pin),
timeout=10)
self._state = request.json()['return_value'] != 0
self._available = True
except requests.exceptions.ConnectionError:
_LOGGER.warning("No route to device %s. Is device offline?",
self._resource)
self._available = False