Have thermostat class make all necessary unit conversions

This commit is contained in:
zyell 2015-08-24 16:50:22 -07:00
parent 6647894c36
commit e15eb90b33
2 changed files with 28 additions and 15 deletions

View File

@ -10,6 +10,7 @@ from homeassistant.helpers.entity_component import EntityComponent
import homeassistant.util as util import homeassistant.util as util
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.temperature import convert
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_TEMPERATURE, STATE_ON, STATE_OFF, TEMP_CELCIUS) ATTR_ENTITY_ID, ATTR_TEMPERATURE, STATE_ON, STATE_OFF, TEMP_CELCIUS)
@ -86,7 +87,9 @@ def setup(hass, config):
return return
for thermostat in target_thermostats: for thermostat in target_thermostats:
thermostat.set_temperature(temperature) thermostat.set_temperature(convert(
temperature, hass.config.temperature_unit,
thermostat.unit_of_measurement))
for thermostat in target_thermostats: for thermostat in target_thermostats:
thermostat.update_ha_state(True) thermostat.update_ha_state(True)
@ -118,9 +121,20 @@ class ThermostatDevice(Entity):
@property @property
def state_attributes(self): def state_attributes(self):
""" Returns optional state attributes. """ """ Returns optional state attributes. """
thermostat_unit = self.unit_of_measurement
user_unit = self.hass.config.temperature_unit
data = { data = {
ATTR_CURRENT_TEMPERATURE: self.hass.config.temperature( ATTR_CURRENT_TEMPERATURE: round(convert(self.current_temperature,
self.current_temperature, self.unit_of_measurement)[0] 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)
} }
is_away = self.is_away_mode_on is_away = self.is_away_mode_on
@ -133,11 +147,13 @@ class ThermostatDevice(Entity):
if device_attr is not None: if device_attr is not None:
data.update(device_attr) data.update(device_attr)
data[ATTR_MIN_TEMP] = self.min_temp
data[ATTR_MAX_TEMP] = self.max_temp
return data return data
@property
def unit_of_measurement(self):
""" Unit of measurement this thermostat expresses itself in. """
return NotImplementedError
@property @property
def current_temperature(self): def current_temperature(self):
""" Returns the current temperature. """ """ Returns the current temperature. """
@ -171,9 +187,9 @@ class ThermostatDevice(Entity):
@property @property
def min_temp(self): def min_temp(self):
""" Return minimum temperature. """ """ Return minimum temperature. """
return self.hass.config.temperature(7, TEMP_CELCIUS)[0] return convert(7, TEMP_CELCIUS, self.unit_of_measurement)
@property @property
def max_temp(self): def max_temp(self):
""" Return maxmum temperature. """ """ Return maxmum temperature. """
return self.hass.config.temperature(35, TEMP_CELCIUS)[0] return convert(35, TEMP_CELCIUS, self.unit_of_measurement)

View File

@ -4,7 +4,6 @@ Adds support for Nest thermostats.
import logging import logging
from homeassistant.components.thermostat import ThermostatDevice from homeassistant.components.thermostat import ThermostatDevice
from homeassistant.helpers.temperature import convert
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, TEMP_CELCIUS) from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, TEMP_CELCIUS)
REQUIREMENTS = ['python-nest>=2.4.0'] REQUIREMENTS = ['python-nest>=2.4.0']
@ -63,7 +62,7 @@ class NestThermostat(ThermostatDevice):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
""" Returns the unit of measurement. """ """ Unit of measurement this thermostat expresses itself in. """
return TEMP_CELCIUS return TEMP_CELCIUS
@property @property
@ -107,9 +106,7 @@ class NestThermostat(ThermostatDevice):
return self.structure.away return self.structure.away
def set_temperature(self, temperature): def set_temperature(self, temperature):
""" Set new target temperature ensuring it is in proper units """ """ Set new target temperature """
temperature = convert(temperature, self.hass.config.temperature_unit,
self.unit_of_measurement)
self.device.target = temperature self.device.target = temperature
def turn_away_mode_on(self): def turn_away_mode_on(self):
@ -127,7 +124,7 @@ class NestThermostat(ThermostatDevice):
if temp is None: if temp is None:
return super().min_temp return super().min_temp
else: else:
return round(self.hass.config.temperature(temp, TEMP_CELCIUS)[0]) return temp
@property @property
def max_temp(self): def max_temp(self):
@ -136,7 +133,7 @@ class NestThermostat(ThermostatDevice):
if temp is None: if temp is None:
return super().max_temp return super().max_temp
else: else:
return round(self.hass.config.temperature(temp, TEMP_CELCIUS)[0]) return temp
def update(self): def update(self):
""" Python-nest has its own mechanism for staying up to date. """ """ Python-nest has its own mechanism for staying up to date. """