From d84bea3621e55fbb76b5b89ffa0c22f527ed2434 Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Tue, 8 Dec 2015 16:15:19 -0700 Subject: [PATCH 1/2] Allow decimal place to be used without corr factor --- homeassistant/components/sensor/arest.py | 15 ++++++--------- .../components/sensor/command_sensor.py | 14 +++++++------- homeassistant/components/sensor/rest.py | 17 +++++------------ 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/sensor/arest.py b/homeassistant/components/sensor/arest.py index f1faa7cc932..0af804a4580 100644 --- a/homeassistant/components/sensor/arest.py +++ b/homeassistant/components/sensor/arest.py @@ -127,15 +127,12 @@ class ArestSensor(Entity): if 'error' in values: return values['error'] elif 'value' in values: - if self._corr_factor is not None \ - and self._decimal_places is not None: - return round((float(values['value']) * - float(self._corr_factor)), self._decimal_places) - elif self._corr_factor is not None \ - and self._decimal_places is None: - return round(float(values['value']) * float(self._corr_factor)) - else: - return values['value'] + value = values['value'] + if self._corr_factor is not None: + value = float(value) * float(self._corr_factor) + if self._decimal_places is not None: + value = round(value, self._decimal_places) + return value else: return values.get(self._variable, 'n/a') diff --git a/homeassistant/components/sensor/command_sensor.py b/homeassistant/components/sensor/command_sensor.py index b5beaab13d0..9dc6ad8c426 100644 --- a/homeassistant/components/sensor/command_sensor.py +++ b/homeassistant/components/sensor/command_sensor.py @@ -35,8 +35,8 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): data, config.get('name', DEFAULT_NAME), config.get('unit_of_measurement'), - config.get('correction_factor', 1.0), - config.get('decimal_places', 0) + config.get('correction_factor', None), + config.get('decimal_places', None) )]) @@ -49,7 +49,7 @@ class CommandSensor(Entity): self._name = name self._state = False self._unit_of_measurement = unit_of_measurement - self._corr_factor = float(corr_factor) + self._corr_factor = corr_factor self._decimal_places = decimal_places self.update() @@ -76,10 +76,10 @@ class CommandSensor(Entity): try: if value is not None: if self._corr_factor is not None: - self._state = round((float(value) * self._corr_factor), - self._decimal_places) - else: - self._state = value + value = float(value) * float(self._corr_factor) + if self._decimal_places is not None: + value = round(value, self._decimal_places) + self._state = value except ValueError: self._state = value diff --git a/homeassistant/components/sensor/rest.py b/homeassistant/components/sensor/rest.py index 53609dbb237..abe46d03b95 100644 --- a/homeassistant/components/sensor/rest.py +++ b/homeassistant/components/sensor/rest.py @@ -136,18 +136,11 @@ class RestSensor(Entity): try: if value is not None: value = RestSensor.extract_value(value, self._variable) - if self._corr_factor is not None \ - and self._decimal_places is not None: - self._state = round( - (float(value) * - float(self._corr_factor)), - self._decimal_places) - elif self._corr_factor is not None \ - and self._decimal_places is None: - self._state = round(float(value) * - float(self._corr_factor)) - else: - self._state = value + if self._corr_factor is not None: + value = float(value) * float(self._corr_factor) + if self._decimal_places is not None: + value = round(value, self._decimal_places) + self._state = value except ValueError: self._state = RestSensor.extract_value(value, self._variable) From 3938b9f3f6b967e07c8df0b6147cb0e29a1f5c2b Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Wed, 9 Dec 2015 15:16:42 -0700 Subject: [PATCH 2/2] If decimal_places is 0, cast to int after rounding --- homeassistant/components/sensor/arest.py | 2 ++ homeassistant/components/sensor/command_sensor.py | 2 ++ homeassistant/components/sensor/rest.py | 2 ++ 3 files changed, 6 insertions(+) diff --git a/homeassistant/components/sensor/arest.py b/homeassistant/components/sensor/arest.py index 0af804a4580..3a0ce01719b 100644 --- a/homeassistant/components/sensor/arest.py +++ b/homeassistant/components/sensor/arest.py @@ -132,6 +132,8 @@ class ArestSensor(Entity): value = float(value) * float(self._corr_factor) if self._decimal_places is not None: value = round(value, self._decimal_places) + if self._decimal_places == 0: + value = int(value) return value else: return values.get(self._variable, 'n/a') diff --git a/homeassistant/components/sensor/command_sensor.py b/homeassistant/components/sensor/command_sensor.py index 9dc6ad8c426..e60723f6bfa 100644 --- a/homeassistant/components/sensor/command_sensor.py +++ b/homeassistant/components/sensor/command_sensor.py @@ -79,6 +79,8 @@ class CommandSensor(Entity): value = float(value) * float(self._corr_factor) if self._decimal_places is not None: value = round(value, self._decimal_places) + if self._decimal_places == 0: + value = int(value) self._state = value except ValueError: self._state = value diff --git a/homeassistant/components/sensor/rest.py b/homeassistant/components/sensor/rest.py index abe46d03b95..f50113350da 100644 --- a/homeassistant/components/sensor/rest.py +++ b/homeassistant/components/sensor/rest.py @@ -140,6 +140,8 @@ class RestSensor(Entity): value = float(value) * float(self._corr_factor) if self._decimal_places is not None: value = round(value, self._decimal_places) + if self._decimal_places == 0: + value = int(value) self._state = value except ValueError: self._state = RestSensor.extract_value(value, self._variable)