mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Report availability of arest (#3614)
This commit is contained in:
parent
c93b63963b
commit
8592ba3cb9
@ -74,7 +74,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
|
|
||||||
def _render(value):
|
def _render(value):
|
||||||
try:
|
try:
|
||||||
return value_template.render({'value': value})
|
return value_template.async_render({'value': value})
|
||||||
except TemplateError:
|
except TemplateError:
|
||||||
_LOGGER.exception('Error parsing value')
|
_LOGGER.exception('Error parsing value')
|
||||||
return value
|
return value
|
||||||
@ -157,6 +157,11 @@ class ArestSensor(Entity):
|
|||||||
"""Get the latest data from aREST API."""
|
"""Get the latest data from aREST API."""
|
||||||
self.arest.update()
|
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
|
# pylint: disable=too-few-public-methods
|
||||||
class ArestData(object):
|
class ArestData(object):
|
||||||
@ -167,6 +172,7 @@ class ArestData(object):
|
|||||||
self._resource = resource
|
self._resource = resource
|
||||||
self._pin = pin
|
self._pin = pin
|
||||||
self.data = {}
|
self.data = {}
|
||||||
|
self.available = True
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
@ -188,7 +194,8 @@ class ArestData(object):
|
|||||||
response = requests.get('{}/digital/{}'.format(
|
response = requests.get('{}/digital/{}'.format(
|
||||||
self._resource, self._pin), timeout=10)
|
self._resource, self._pin), timeout=10)
|
||||||
self.data = {'value': response.json()['return_value']}
|
self.data = {'value': response.json()['return_value']}
|
||||||
|
self.available = True
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.error("No route to device %s. Is device offline?",
|
_LOGGER.error("No route to device %s. Is device offline?",
|
||||||
self._resource)
|
self._resource)
|
||||||
self.data = {'error': 'error fetching'}
|
self.available = False
|
||||||
|
@ -75,6 +75,7 @@ class ArestSwitchBase(SwitchDevice):
|
|||||||
self._resource = resource
|
self._resource = resource
|
||||||
self._name = '{} {}'.format(location.title(), name.title())
|
self._name = '{} {}'.format(location.title(), name.title())
|
||||||
self._state = None
|
self._state = None
|
||||||
|
self._available = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -86,6 +87,11 @@ class ArestSwitchBase(SwitchDevice):
|
|||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Could the device be accessed during the last update call."""
|
||||||
|
return self._available
|
||||||
|
|
||||||
|
|
||||||
class ArestSwitchFunction(ArestSwitchBase):
|
class ArestSwitchFunction(ArestSwitchBase):
|
||||||
"""Representation of an aREST switch."""
|
"""Representation of an aREST switch."""
|
||||||
@ -136,9 +142,15 @@ class ArestSwitchFunction(ArestSwitchBase):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from aREST API and update the state."""
|
"""Get the latest data from aREST API and update the state."""
|
||||||
request = requests.get(
|
try:
|
||||||
'{}/{}'.format(self._resource, self._func), timeout=10)
|
request = requests.get('{}/{}'.format(self._resource,
|
||||||
|
self._func), timeout=10)
|
||||||
self._state = request.json()['return_value'] != 0
|
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):
|
class ArestSwitchPin(ArestSwitchBase):
|
||||||
@ -153,6 +165,7 @@ class ArestSwitchPin(ArestSwitchBase):
|
|||||||
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
|
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
|
||||||
if request.status_code is not 200:
|
if request.status_code is not 200:
|
||||||
_LOGGER.error("Can't set mode. Is device offline?")
|
_LOGGER.error("Can't set mode. Is device offline?")
|
||||||
|
self._available = False
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
@ -176,6 +189,13 @@ class ArestSwitchPin(ArestSwitchBase):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from aREST API and update the state."""
|
"""Get the latest data from aREST API and update the state."""
|
||||||
request = requests.get(
|
try:
|
||||||
'{}/digital/{}'.format(self._resource, self._pin), timeout=10)
|
request = requests.get('{}/digital/{}'.format(self._resource,
|
||||||
|
self._pin),
|
||||||
|
timeout=10)
|
||||||
self._state = request.json()['return_value'] != 0
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user