Merge pull request #719 from philipbl/decimal_places

Decimal places
This commit is contained in:
Paulus Schoutsen 2015-12-09 15:23:17 -08:00
commit 5d3d2d4110
3 changed files with 24 additions and 28 deletions

View File

@ -127,15 +127,14 @@ class ArestSensor(Entity):
if 'error' in values: if 'error' in values:
return values['error'] return values['error']
elif 'value' in values: elif 'value' in values:
if self._corr_factor is not None \ value = values['value']
and self._decimal_places is not None: if self._corr_factor is not None:
return round((float(values['value']) * value = float(value) * float(self._corr_factor)
float(self._corr_factor)), self._decimal_places) if self._decimal_places is not None:
elif self._corr_factor is not None \ value = round(value, self._decimal_places)
and self._decimal_places is None: if self._decimal_places == 0:
return round(float(values['value']) * float(self._corr_factor)) value = int(value)
else: return value
return values['value']
else: else:
return values.get(self._variable, 'n/a') return values.get(self._variable, 'n/a')

View File

@ -35,8 +35,8 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
data, data,
config.get('name', DEFAULT_NAME), config.get('name', DEFAULT_NAME),
config.get('unit_of_measurement'), config.get('unit_of_measurement'),
config.get('correction_factor', 1.0), config.get('correction_factor', None),
config.get('decimal_places', 0) config.get('decimal_places', None)
)]) )])
@ -49,7 +49,7 @@ class CommandSensor(Entity):
self._name = name self._name = name
self._state = False self._state = False
self._unit_of_measurement = unit_of_measurement self._unit_of_measurement = unit_of_measurement
self._corr_factor = float(corr_factor) self._corr_factor = corr_factor
self._decimal_places = decimal_places self._decimal_places = decimal_places
self.update() self.update()
@ -76,10 +76,12 @@ class CommandSensor(Entity):
try: try:
if value is not None: if value is not None:
if self._corr_factor is not None: if self._corr_factor is not None:
self._state = round((float(value) * self._corr_factor), value = float(value) * float(self._corr_factor)
self._decimal_places) if self._decimal_places is not None:
else: value = round(value, self._decimal_places)
self._state = value if self._decimal_places == 0:
value = int(value)
self._state = value
except ValueError: except ValueError:
self._state = value self._state = value

View File

@ -136,18 +136,13 @@ class RestSensor(Entity):
try: try:
if value is not None: if value is not None:
value = RestSensor.extract_value(value, self._variable) value = RestSensor.extract_value(value, self._variable)
if self._corr_factor is not None \ if self._corr_factor is not None:
and self._decimal_places is not None: value = float(value) * float(self._corr_factor)
self._state = round( if self._decimal_places is not None:
(float(value) * value = round(value, self._decimal_places)
float(self._corr_factor)), if self._decimal_places == 0:
self._decimal_places) value = int(value)
elif self._corr_factor is not None \ self._state = value
and self._decimal_places is None:
self._state = round(float(value) *
float(self._corr_factor))
else:
self._state = value
except ValueError: except ValueError:
self._state = RestSensor.extract_value(value, self._variable) self._state = RestSensor.extract_value(value, self._variable)