commit
d6dad8c818
|
@ -11,9 +11,11 @@ import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from homeassistant.const import DEVICE_DEFAULT_NAME
|
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, \
|
||||||
|
DEVICE_DEFAULT_NAME
|
||||||
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import template, Throttle
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -50,36 +52,50 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
|
||||||
arest = ArestData(resource)
|
arest = ArestData(resource)
|
||||||
|
|
||||||
|
def make_renderer(value_template):
|
||||||
|
""" Creates renderer based on variable_template value """
|
||||||
|
if value_template is None:
|
||||||
|
return lambda value: value
|
||||||
|
|
||||||
|
def _render(value):
|
||||||
|
try:
|
||||||
|
return template.render(hass, value_template, {'value': value})
|
||||||
|
except TemplateError:
|
||||||
|
_LOGGER.exception('Error parsing value')
|
||||||
|
return value
|
||||||
|
|
||||||
|
return _render
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
|
|
||||||
if var_conf is not None:
|
if var_conf is not None:
|
||||||
for variable in config['monitored_variables']:
|
for variable in var_conf:
|
||||||
if variable['name'] not in response['variables']:
|
if variable['name'] not in response['variables']:
|
||||||
_LOGGER.error('Variable: "%s" does not exist',
|
_LOGGER.error('Variable: "%s" does not exist',
|
||||||
variable['name'])
|
variable['name'])
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
renderer = make_renderer(variable.get(CONF_VALUE_TEMPLATE))
|
||||||
dev.append(ArestSensor(arest,
|
dev.append(ArestSensor(arest,
|
||||||
resource,
|
resource,
|
||||||
config.get('name', response['name']),
|
config.get('name', response['name']),
|
||||||
variable['name'],
|
variable['name'],
|
||||||
variable=variable['name'],
|
variable=variable['name'],
|
||||||
unit_of_measurement=variable.get(
|
unit_of_measurement=variable.get(
|
||||||
'unit_of_measurement')))
|
ATTR_UNIT_OF_MEASUREMENT),
|
||||||
|
renderer=renderer))
|
||||||
|
|
||||||
if pins is not None:
|
if pins is not None:
|
||||||
for pinnum, pin in pins.items():
|
for pinnum, pin in pins.items():
|
||||||
|
renderer = make_renderer(pin.get(CONF_VALUE_TEMPLATE))
|
||||||
dev.append(ArestSensor(ArestData(resource, pinnum),
|
dev.append(ArestSensor(ArestData(resource, pinnum),
|
||||||
resource,
|
resource,
|
||||||
config.get('name', response['name']),
|
config.get('name', response['name']),
|
||||||
pin.get('name'),
|
pin.get('name'),
|
||||||
pin=pinnum,
|
pin=pinnum,
|
||||||
unit_of_measurement=pin.get(
|
unit_of_measurement=pin.get(
|
||||||
'unit_of_measurement'),
|
ATTR_UNIT_OF_MEASUREMENT),
|
||||||
corr_factor=pin.get('correction_factor',
|
renderer=renderer))
|
||||||
None),
|
|
||||||
decimal_places=pin.get('decimal_places',
|
|
||||||
None)))
|
|
||||||
|
|
||||||
add_devices(dev)
|
add_devices(dev)
|
||||||
|
|
||||||
|
@ -89,8 +105,7 @@ class ArestSensor(Entity):
|
||||||
""" Implements an aREST sensor for exposed variables. """
|
""" Implements an aREST sensor for exposed variables. """
|
||||||
|
|
||||||
def __init__(self, arest, resource, location, name, variable=None,
|
def __init__(self, arest, resource, location, name, variable=None,
|
||||||
pin=None, unit_of_measurement=None, corr_factor=None,
|
pin=None, unit_of_measurement=None, renderer=None):
|
||||||
decimal_places=None):
|
|
||||||
self.arest = arest
|
self.arest = arest
|
||||||
self._resource = resource
|
self._resource = resource
|
||||||
self._name = '{} {}'.format(location.title(), name.title()) \
|
self._name = '{} {}'.format(location.title(), name.title()) \
|
||||||
|
@ -99,8 +114,7 @@ class ArestSensor(Entity):
|
||||||
self._pin = pin
|
self._pin = pin
|
||||||
self._state = 'n/a'
|
self._state = 'n/a'
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._unit_of_measurement = unit_of_measurement
|
||||||
self._corr_factor = corr_factor
|
self._renderer = renderer
|
||||||
self._decimal_places = decimal_places
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
if self._pin is not None:
|
if self._pin is not None:
|
||||||
|
@ -126,17 +140,11 @@ class ArestSensor(Entity):
|
||||||
|
|
||||||
if 'error' in values:
|
if 'error' in values:
|
||||||
return values['error']
|
return values['error']
|
||||||
elif 'value' in values:
|
|
||||||
value = values['value']
|
value = self._renderer(values.get('value',
|
||||||
if self._corr_factor is not None:
|
values.get(self._variable,
|
||||||
value = float(value) * float(self._corr_factor)
|
'N/A')))
|
||||||
if self._decimal_places is not None:
|
return value
|
||||||
value = round(value, self._decimal_places)
|
|
||||||
if self._decimal_places == 0:
|
|
||||||
value = int(value)
|
|
||||||
return value
|
|
||||||
else:
|
|
||||||
return values.get(self._variable, 'n/a')
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Gets the latest data from aREST API. """
|
""" Gets the latest data from aREST API. """
|
||||||
|
|
Loading…
Reference in New Issue