From 086961d1093b466583c9c8125515981b242c0a2a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 16 Aug 2015 22:06:01 -0700 Subject: [PATCH] Add temperature util, helpers --- homeassistant/core.py | 18 +++++++----------- homeassistant/helpers/temperature.py | 19 +++++++++++++++++++ homeassistant/util/temperature.py | 4 ++-- 3 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 homeassistant/helpers/temperature.py diff --git a/homeassistant/core.py b/homeassistant/core.py index 76ff46cfe8b..76b4b38f3fc 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -23,7 +23,7 @@ from homeassistant.const import ( TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME) import homeassistant.util as util import homeassistant.util.dt as date_util -import homeassistant.util.temperature as temp_util +import homeassistant.helpers.temperature as temp_helper DOMAIN = "homeassistant" @@ -673,18 +673,14 @@ class Config(object): return value, unit try: - if unit == TEMP_CELCIUS: - # Convert C to F - return (round(temp_util.c_to_f(float(value)), 1), - TEMP_FAHRENHEIT) - - # Convert F to C - return round(temp_util.f_to_c(float(value)), 1), TEMP_CELCIUS - - except ValueError: - # Could not convert value to float + temp = float(value) + except ValueError: # Could not convert value to float return value, unit + return ( + round(temp_helper.convert(temp, unit, self.temperature_unit), 1), + self.temperature_unit) + def as_dict(self): """ Converts config to a dictionary. """ time_zone = self.time_zone or date_util.UTC diff --git a/homeassistant/helpers/temperature.py b/homeassistant/helpers/temperature.py new file mode 100644 index 00000000000..eaf1f78d927 --- /dev/null +++ b/homeassistant/helpers/temperature.py @@ -0,0 +1,19 @@ +""" +homeassistant.helpers.temperature +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Methods to help handle temperature in Home Assistant. +""" + +from homeassistant.const import TEMP_CELCIUS +import homeassistant.util.temperature as temp_util + + +def convert(temperature, unit, to_unit): + """ Converts temperature to correct unit. """ + if unit == to_unit: + return temperature + elif unit == TEMP_CELCIUS: + return temp_util.celcius_to_fahrenheit(temperature) + + return temp_util.fahrenheit_to_celcius(temperature) diff --git a/homeassistant/util/temperature.py b/homeassistant/util/temperature.py index 4cd0e1ed1d0..658639aae55 100644 --- a/homeassistant/util/temperature.py +++ b/homeassistant/util/temperature.py @@ -6,11 +6,11 @@ Temperature util functions. """ -def f_to_c(fahrenheit): +def fahrenheit_to_celcius(fahrenheit): """ Convert a Fahrenheit temperature to Celcius. """ return (fahrenheit - 32.0) / 1.8 -def c_to_f(celcius): +def celcius_to_fahrenheit(celcius): """ Convert a Celcius temperature to Fahrenheit. """ return celcius * 1.8 + 32.0