From 038b1c1fc67ca33e0e242a27d6dc80ad7305b675 Mon Sep 17 00:00:00 2001
From: Sean Dague <sean@dague.net>
Date: Sun, 27 Nov 2016 17:19:12 -0500
Subject: [PATCH] precision properties for climate components (#4562)

This lets components declare their precision for temperatures. If
nothing is declared, we assume 0.1 C and whole integer precision in
F. Currently this supports only WHOLE, HALVES, and TENTHS for
precision, but adding other precision levels is pretty straight
forward.

This also uses proliphix as an example of changing the precision for a
platform.

Closes bug #4350
---
 homeassistant/components/climate/__init__.py  | 20 +++++++++++++++++--
 homeassistant/components/climate/proliphix.py | 12 ++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/homeassistant/components/climate/__init__.py b/homeassistant/components/climate/__init__.py
index d35b142a8c4..80ef97622d5 100644
--- a/homeassistant/components/climate/__init__.py
+++ b/homeassistant/components/climate/__init__.py
@@ -58,6 +58,11 @@ ATTR_OPERATION_LIST = "operation_list"
 ATTR_SWING_MODE = "swing_mode"
 ATTR_SWING_LIST = "swing_list"
 
+# The degree of precision for each platform
+PRECISION_WHOLE = 1
+PRECISION_HALVES = 0.5
+PRECISION_TENTHS = 0.1
+
 CONVERTIBLE_ATTRIBUTE = [
     ATTR_TEMPERATURE,
     ATTR_TARGET_TEMP_LOW,
@@ -371,6 +376,14 @@ class ClimateDevice(Entity):
         else:
             return STATE_UNKNOWN
 
+    @property
+    def precision(self):
+        """Return the precision of the system."""
+        if self.unit_of_measurement == TEMP_CELSIUS:
+            return PRECISION_TENTHS
+        else:
+            return PRECISION_WHOLE
+
     @property
     def state_attributes(self):
         """Return the optional state attributes."""
@@ -569,8 +582,11 @@ class ClimateDevice(Entity):
         value = convert_temperature(temp, self.temperature_unit,
                                     self.unit_of_measurement)
 
-        if self.unit_of_measurement == TEMP_CELSIUS:
+        # Round in the units appropriate
+        if self.precision == PRECISION_HALVES:
+            return round(value * 2) / 2.0
+        elif self.precision == PRECISION_TENTHS:
             return round(value, 1)
         else:
-            # Users of fahrenheit generally expect integer units.
+            # PRECISION_WHOLE as a fall back
             return round(value)
diff --git a/homeassistant/components/climate/proliphix.py b/homeassistant/components/climate/proliphix.py
index 5b3708db72e..ef6553cc062 100644
--- a/homeassistant/components/climate/proliphix.py
+++ b/homeassistant/components/climate/proliphix.py
@@ -7,7 +7,8 @@ https://home-assistant.io/components/climate.proliphix/
 import voluptuous as vol
 
 from homeassistant.components.climate import (
-    STATE_COOL, STATE_HEAT, STATE_IDLE, ClimateDevice, PLATFORM_SCHEMA)
+    PRECISION_TENTHS, STATE_COOL, STATE_HEAT, STATE_IDLE,
+    ClimateDevice, PLATFORM_SCHEMA)
 from homeassistant.const import (
     CONF_HOST, CONF_PASSWORD, CONF_USERNAME, TEMP_FAHRENHEIT, ATTR_TEMPERATURE)
 import homeassistant.helpers.config_validation as cv
@@ -60,6 +61,15 @@ class ProliphixThermostat(ClimateDevice):
         """Return the name of the thermostat."""
         return self._name
 
+    @property
+    def precision(self):
+        """Return the precision of the system.
+
+        Proliphix temperature values are passed back and forth in the
+        API as tenths of degrees F (i.e. 690 for 69 degrees).
+        """
+        return PRECISION_TENTHS
+
     @property
     def device_state_attributes(self):
         """Return the device specific state attributes."""
-- 
GitLab