Add temperature util, helpers

This commit is contained in:
Paulus Schoutsen 2015-08-16 22:06:01 -07:00
parent b61b3c611d
commit 086961d109
3 changed files with 28 additions and 13 deletions

View file

@ -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

View file

@ -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)

View file

@ -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