From 8592ba3cb9d21feeb18d5f4c4ad078f2cab25372 Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Tue, 4 Oct 2016 03:51:45 -0400 Subject: [PATCH] Report availability of arest (#3614) --- homeassistant/components/sensor/arest.py | 11 ++++++-- homeassistant/components/switch/arest.py | 32 +++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/sensor/arest.py b/homeassistant/components/sensor/arest.py index c13ea15b222..cad9a5fc416 100644 --- a/homeassistant/components/sensor/arest.py +++ b/homeassistant/components/sensor/arest.py @@ -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 diff --git a/homeassistant/components/switch/arest.py b/homeassistant/components/switch/arest.py index ce5c946b436..76f5bc7b580 100644 --- a/homeassistant/components/switch/arest.py +++ b/homeassistant/components/switch/arest.py @@ -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