mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
add throttle and other minor improvements
This commit is contained in:
parent
72426e08b8
commit
1a88e48986
@ -33,7 +33,7 @@ unit
|
|||||||
*Optional
|
*Optional
|
||||||
Defines the units of measurement of the sensor, if any.
|
Defines the units of measurement of the sensor, if any.
|
||||||
|
|
||||||
Details for the API : http://arest.io
|
Details for the API: http://arest.io
|
||||||
|
|
||||||
Format of a default JSON response by aREST:
|
Format of a default JSON response by aREST:
|
||||||
{
|
{
|
||||||
@ -48,11 +48,16 @@ Format of a default JSON response by aREST:
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from requests import get, exceptions
|
from requests import get, exceptions
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from homeassistant.util import Throttle
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Return cached results if last scan was less then this time ago
|
||||||
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
""" Get the aREST sensor. """
|
""" Get the aREST sensor. """
|
||||||
@ -70,7 +75,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
"Please check the IP address in the configuration file.")
|
"Please check the IP address in the configuration file.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
data = ArestData(resource)
|
rest = ArestData(resource)
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for variable in config['monitored_variables']:
|
for variable in config['monitored_variables']:
|
||||||
@ -79,7 +84,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
if variable['name'] not in response.json()['variables']:
|
if variable['name'] not in response.json()['variables']:
|
||||||
_LOGGER.error('Variable: "%s" does not exist', variable['name'])
|
_LOGGER.error('Variable: "%s" does not exist', variable['name'])
|
||||||
else:
|
else:
|
||||||
dev.append(ArestSensor(data,
|
dev.append(ArestSensor(rest,
|
||||||
response.json()['name'],
|
response.json()['name'],
|
||||||
variable['name'],
|
variable['name'],
|
||||||
variable['unit']))
|
variable['unit']))
|
||||||
@ -90,8 +95,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
class ArestSensor(Entity):
|
class ArestSensor(Entity):
|
||||||
""" Implements an aREST sensor. """
|
""" Implements an aREST sensor. """
|
||||||
|
|
||||||
def __init__(self, data, location, variable, unit_of_measurement):
|
def __init__(self, rest, location, variable, unit_of_measurement):
|
||||||
self._data = data
|
self.rest = rest
|
||||||
self._name = '{} {}'.format(location.title(), variable.title())
|
self._name = '{} {}'.format(location.title(), variable.title())
|
||||||
self._variable = variable
|
self._variable = variable
|
||||||
self._state = 'n/a'
|
self._state = 'n/a'
|
||||||
@ -114,12 +119,14 @@ class ArestSensor(Entity):
|
|||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Gets the latest data from aREST API and updates the states. """
|
""" Gets the latest data from aREST API and updates the state. """
|
||||||
values = self._data.update()
|
self.rest.update()
|
||||||
if values is not None:
|
values = self.rest.data
|
||||||
self._state = values[self._variable]
|
|
||||||
|
if 'error' in values:
|
||||||
|
self._state = values['error']
|
||||||
else:
|
else:
|
||||||
self._state = 'n/a'
|
self._state = values[self._variable]
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
@ -128,12 +135,16 @@ class ArestData(object):
|
|||||||
|
|
||||||
def __init__(self, resource):
|
def __init__(self, resource):
|
||||||
self.resource = resource
|
self.resource = resource
|
||||||
|
self.data = dict()
|
||||||
|
|
||||||
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Gets the latest data from aREST API. """
|
""" Gets the latest data from aREST device. """
|
||||||
try:
|
try:
|
||||||
response = get(self.resource)
|
response = get(self.resource)
|
||||||
return response.json()['variables']
|
if 'error' in self.data:
|
||||||
|
del self.data['error']
|
||||||
|
self.data = response.json()['variables']
|
||||||
except exceptions.ConnectionError:
|
except exceptions.ConnectionError:
|
||||||
_LOGGER.error("No route to device. Is device offline?")
|
_LOGGER.error("No route to device. Is device offline?")
|
||||||
return None
|
self.data['error'] = 'n/a'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user