From bf1970b78cdfb5d0e467bbacfd10e6b265704657 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 11 Oct 2015 18:16:55 -0700 Subject: [PATCH] Make thermostat more robust --- .../components/thermostat/__init__.py | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/thermostat/__init__.py b/homeassistant/components/thermostat/__init__.py index 7d26c78f0f8..b021ec86c35 100644 --- a/homeassistant/components/thermostat/__init__.py +++ b/homeassistant/components/thermostat/__init__.py @@ -136,22 +136,16 @@ class ThermostatDevice(Entity): def state_attributes(self): """ Returns optional state attributes. """ - thermostat_unit = self.unit_of_measurement - user_unit = self.hass.config.temperature_unit - data = { - ATTR_CURRENT_TEMPERATURE: round(convert( - self.current_temperature, thermostat_unit, user_unit), 1), - ATTR_MIN_TEMP: round(convert( - self.min_temp, thermostat_unit, user_unit), 0), - ATTR_MAX_TEMP: round(convert( - self.max_temp, thermostat_unit, user_unit), 0), - ATTR_TEMPERATURE: round(convert( - self.target_temperature, thermostat_unit, user_unit), 0), - ATTR_TEMPERATURE_LOW: round(convert( - self.target_temperature_low, thermostat_unit, user_unit), 0), - ATTR_TEMPERATURE_HIGH: round(convert( - self.target_temperature_high, thermostat_unit, user_unit), 0), + ATTR_CURRENT_TEMPERATURE: + self._convert(self.current_temperature, 1), + ATTR_MIN_TEMP: self._convert(self.min_temp, 0), + ATTR_MAX_TEMP: self._convert(self.max_temp, 0), + ATTR_TEMPERATURE: self._convert(self.target_temperature, 0), + ATTR_TEMPERATURE_LOW: + self._convert(self.target_temperature_low, 0), + ATTR_TEMPERATURE_HIGH: + self._convert(self.target_temperature_high, 0), } operation = self.operation @@ -228,3 +222,14 @@ class ThermostatDevice(Entity): def max_temp(self): """ Return maxmum temperature. """ return convert(35, TEMP_CELCIUS, self.unit_of_measurement) + + def _convert(self, temp, round_dec=None): + """ Convert temperature from this thermost into user preferred + temperature. """ + if temp is None: + return None + + value = convert(temp, self.unit_of_measurement, + self.hass.config.temperature_unit) + + return value if round_dec is None else round(value, round_dec)