Report availability of arest (#3614)

This commit is contained in:
William Scanlon 2016-10-04 03:51:45 -04:00 committed by Paulus Schoutsen
parent c93b63963b
commit 8592ba3cb9
2 changed files with 35 additions and 8 deletions

View File

@ -74,7 +74,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
def _render(value):
try:
return value_template.render({'value': value})
return value_template.async_render({'value': value})
except TemplateError:
_LOGGER.exception('Error parsing value')
return value
@ -157,6 +157,11 @@ class ArestSensor(Entity):
"""Get the latest data from aREST API."""
self.arest.update()
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self.arest.available
# pylint: disable=too-few-public-methods
class ArestData(object):
@ -167,6 +172,7 @@ class ArestData(object):
self._resource = resource
self._pin = pin
self.data = {}
self.available = True
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
@ -188,7 +194,8 @@ class ArestData(object):
response = requests.get('{}/digital/{}'.format(
self._resource, self._pin), timeout=10)
self.data = {'value': response.json()['return_value']}
self.available = True
except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device %s. Is device offline?",
self._resource)
self.data = {'error': 'error fetching'}
self.available = False

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