Merge branch 'heatcontroll_config' into dev
commit
dd0424435a
|
@ -24,6 +24,9 @@ CONF_NAME = 'name'
|
|||
DEFAULT_NAME = 'Heat Control'
|
||||
CONF_HEATER = 'heater'
|
||||
CONF_SENSOR = 'target_sensor'
|
||||
CONF_MIN_TEMP = 'min_temp'
|
||||
CONF_MAX_TEMP = 'max_temp'
|
||||
CONF_TARGET_TEMP = 'target_temp'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -34,27 +37,34 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
name = config.get(CONF_NAME, DEFAULT_NAME)
|
||||
heater_entity_id = config.get(CONF_HEATER)
|
||||
sensor_entity_id = config.get(CONF_SENSOR)
|
||||
min_temp = util.convert(config.get(CONF_MIN_TEMP), float, None)
|
||||
max_temp = util.convert(config.get(CONF_MAX_TEMP), float, None)
|
||||
target_temp = util.convert(config.get(CONF_TARGET_TEMP), float, None)
|
||||
|
||||
if None in (heater_entity_id, sensor_entity_id):
|
||||
_LOGGER.error('Missing required key %s or %s', CONF_HEATER,
|
||||
CONF_SENSOR)
|
||||
return False
|
||||
|
||||
add_devices([HeatControl(hass, name, heater_entity_id, sensor_entity_id)])
|
||||
add_devices([HeatControl(hass, name, heater_entity_id, sensor_entity_id,
|
||||
min_temp, max_temp, target_temp)])
|
||||
|
||||
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
class HeatControl(ThermostatDevice):
|
||||
""" Represents a HeatControl device. """
|
||||
|
||||
def __init__(self, hass, name, heater_entity_id, sensor_entity_id):
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self, hass, name, heater_entity_id, sensor_entity_id,
|
||||
min_temp, max_temp, target_temp):
|
||||
self.hass = hass
|
||||
self._name = name
|
||||
self.heater_entity_id = heater_entity_id
|
||||
|
||||
self._active = False
|
||||
self._cur_temp = None
|
||||
self._target_temp = None
|
||||
self._min_temp = min_temp
|
||||
self._max_temp = max_temp
|
||||
self._target_temp = target_temp
|
||||
self._unit = None
|
||||
|
||||
track_state_change(hass, sensor_entity_id, self._sensor_changed)
|
||||
|
@ -79,6 +89,7 @@ class HeatControl(ThermostatDevice):
|
|||
|
||||
@property
|
||||
def current_temperature(self):
|
||||
""" Returns the sensor temperature. """
|
||||
return self._cur_temp
|
||||
|
||||
@property
|
||||
|
@ -97,6 +108,26 @@ class HeatControl(ThermostatDevice):
|
|||
self._control_heating()
|
||||
self.update_ha_state()
|
||||
|
||||
@property
|
||||
def min_temp(self):
|
||||
""" Return minimum temperature. """
|
||||
# pylint: disable=no-member
|
||||
if self._min_temp:
|
||||
return self._min_temp
|
||||
else:
|
||||
# get default temp from super class
|
||||
return ThermostatDevice.min_temp.fget(self)
|
||||
|
||||
@property
|
||||
def max_temp(self):
|
||||
""" Return maximum temperature. """
|
||||
# pylint: disable=no-member
|
||||
if self._min_temp:
|
||||
return self._max_temp
|
||||
else:
|
||||
# get default temp from super class
|
||||
return ThermostatDevice.max_temp.fget(self)
|
||||
|
||||
def _sensor_changed(self, entity_id, old_state, new_state):
|
||||
""" Called when temperature changes. """
|
||||
if new_state is None:
|
||||
|
|
|
@ -21,6 +21,9 @@ from homeassistant.components import switch, thermostat
|
|||
entity = 'thermostat.test'
|
||||
ent_sensor = 'sensor.test'
|
||||
ent_switch = 'switch.test'
|
||||
min_temp = 3.0
|
||||
max_temp = 65.0
|
||||
target_temp = 42.0
|
||||
|
||||
|
||||
class TestThermostatHeatControl(unittest.TestCase):
|
||||
|
@ -43,6 +46,28 @@ class TestThermostatHeatControl(unittest.TestCase):
|
|||
def test_setup_defaults_to_unknown(self):
|
||||
self.assertEqual('unknown', self.hass.states.get(entity).state)
|
||||
|
||||
def test_default_setup_params(self):
|
||||
state = self.hass.states.get(entity)
|
||||
self.assertEqual(7, state.attributes.get('min_temp'))
|
||||
self.assertEqual(35, state.attributes.get('max_temp'))
|
||||
self.assertEqual(None, state.attributes.get('temperature'))
|
||||
|
||||
def test_custom_setup_params(self):
|
||||
thermostat.setup(self.hass, {'thermostat': {
|
||||
'platform': 'heat_control',
|
||||
'name': 'test',
|
||||
'heater': ent_switch,
|
||||
'target_sensor': ent_sensor,
|
||||
'min_temp': min_temp,
|
||||
'max_temp': max_temp,
|
||||
'target_temp': target_temp
|
||||
}})
|
||||
state = self.hass.states.get(entity)
|
||||
self.assertEqual(min_temp, state.attributes.get('min_temp'))
|
||||
self.assertEqual(max_temp, state.attributes.get('max_temp'))
|
||||
self.assertEqual(target_temp, state.attributes.get('temperature'))
|
||||
self.assertEqual(str(target_temp), self.hass.states.get(entity).state)
|
||||
|
||||
def test_set_target_temp(self):
|
||||
thermostat.set_temperature(self.hass, 30)
|
||||
self.hass.pool.block_till_done()
|
||||
|
@ -113,3 +138,4 @@ class TestThermostatHeatControl(unittest.TestCase):
|
|||
|
||||
self.hass.services.register('switch', SERVICE_TURN_ON, log_call)
|
||||
self.hass.services.register('switch', SERVICE_TURN_OFF, log_call)
|
||||
|
||||
|
|
Loading…
Reference in New Issue