mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add temperature util, helpers
This commit is contained in:
parent
b61b3c611d
commit
086961d109
@ -23,7 +23,7 @@ from homeassistant.const import (
|
|||||||
TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME)
|
TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME)
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
import homeassistant.util.dt as date_util
|
import homeassistant.util.dt as date_util
|
||||||
import homeassistant.util.temperature as temp_util
|
import homeassistant.helpers.temperature as temp_helper
|
||||||
|
|
||||||
DOMAIN = "homeassistant"
|
DOMAIN = "homeassistant"
|
||||||
|
|
||||||
@ -673,18 +673,14 @@ class Config(object):
|
|||||||
return value, unit
|
return value, unit
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if unit == TEMP_CELCIUS:
|
temp = float(value)
|
||||||
# Convert C to F
|
except ValueError: # Could not convert value to float
|
||||||
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
|
|
||||||
return value, unit
|
return value, unit
|
||||||
|
|
||||||
|
return (
|
||||||
|
round(temp_helper.convert(temp, unit, self.temperature_unit), 1),
|
||||||
|
self.temperature_unit)
|
||||||
|
|
||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
""" Converts config to a dictionary. """
|
""" Converts config to a dictionary. """
|
||||||
time_zone = self.time_zone or date_util.UTC
|
time_zone = self.time_zone or date_util.UTC
|
||||||
|
19
homeassistant/helpers/temperature.py
Normal file
19
homeassistant/helpers/temperature.py
Normal 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)
|
@ -6,11 +6,11 @@ Temperature util functions.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def f_to_c(fahrenheit):
|
def fahrenheit_to_celcius(fahrenheit):
|
||||||
""" Convert a Fahrenheit temperature to Celcius. """
|
""" Convert a Fahrenheit temperature to Celcius. """
|
||||||
return (fahrenheit - 32.0) / 1.8
|
return (fahrenheit - 32.0) / 1.8
|
||||||
|
|
||||||
|
|
||||||
def c_to_f(celcius):
|
def celcius_to_fahrenheit(celcius):
|
||||||
""" Convert a Celcius temperature to Fahrenheit. """
|
""" Convert a Celcius temperature to Fahrenheit. """
|
||||||
return celcius * 1.8 + 32.0
|
return celcius * 1.8 + 32.0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user