Extract temperature util

This commit is contained in:
Paulus Schoutsen 2015-08-16 21:36:33 -07:00
parent dda399fc76
commit b61b3c611d
2 changed files with 20 additions and 2 deletions

View file

@ -23,6 +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
DOMAIN = "homeassistant"
@ -674,10 +675,11 @@ class Config(object):
try:
if unit == TEMP_CELCIUS:
# Convert C to F
return round(float(value) * 1.8 + 32.0, 1), TEMP_FAHRENHEIT
return (round(temp_util.c_to_f(float(value)), 1),
TEMP_FAHRENHEIT)
# Convert F to C
return round((float(value)-32.0)/1.8, 1), TEMP_CELCIUS
return round(temp_util.f_to_c(float(value)), 1), TEMP_CELCIUS
except ValueError:
# Could not convert value to float

View file

@ -0,0 +1,16 @@
"""
homeassistant.util.temperature
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Temperature util functions.
"""
def f_to_c(fahrenheit):
""" Convert a Fahrenheit temperature to Celcius. """
return (fahrenheit - 32.0) / 1.8
def c_to_f(celcius):
""" Convert a Celcius temperature to Fahrenheit. """
return celcius * 1.8 + 32.0