From b646accf87fb4f9e3a71a1f337157a9d2d27512c Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 18 Jun 2016 18:48:32 +0200 Subject: [PATCH] Catch ValueError (#2296) * Catch ValueError * Less options and don't use state --- homeassistant/components/binary_sensor/rest.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/binary_sensor/rest.py b/homeassistant/components/binary_sensor/rest.py index d952d04d32b..d9a6f1d8947 100644 --- a/homeassistant/components/binary_sensor/rest.py +++ b/homeassistant/components/binary_sensor/rest.py @@ -35,7 +35,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): rest.update() if rest.data is None: - _LOGGER.error('Unable to fetch Rest data') + _LOGGER.error('Unable to fetch REST data') return False add_devices([RestBinarySensor( @@ -57,6 +57,7 @@ class RestBinarySensor(BinarySensorDevice): self._name = name self._sensor_class = sensor_class self._state = False + self._previous_data = None self._value_template = value_template self.update() @@ -77,9 +78,14 @@ class RestBinarySensor(BinarySensorDevice): return False if self._value_template is not None: - self.rest.data = template.render_with_possible_json_value( + response = template.render_with_possible_json_value( self._hass, self._value_template, self.rest.data, False) - return bool(int(self.rest.data)) + + try: + return bool(int(response)) + except ValueError: + return {"true": True, "on": True, "open": True, + "yes": True}.get(response.lower(), False) def update(self): """Get the latest data from REST API and updates the state."""