2015-02-17 18:12:27 +00:00
|
|
|
"""
|
2015-11-09 12:12:18 +00:00
|
|
|
Adds support for heat control units.
|
2015-02-19 19:14:37 +00:00
|
|
|
|
2015-10-18 18:05:53 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/thermostat.heat_control/
|
2015-02-17 18:12:27 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-04-21 14:59:35 +00:00
|
|
|
import voluptuous as vol
|
2015-02-17 18:12:27 +00:00
|
|
|
|
2016-04-21 14:59:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-07-23 20:15:17 +00:00
|
|
|
import homeassistant.util as util
|
2015-10-23 05:04:37 +00:00
|
|
|
from homeassistant.components import switch
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.thermostat import (
|
|
|
|
STATE_HEAT, STATE_IDLE, ThermostatDevice)
|
2015-10-23 05:04:37 +00:00
|
|
|
from homeassistant.const import (
|
2016-04-20 03:30:44 +00:00
|
|
|
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.event import track_state_change
|
2015-10-23 05:04:37 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['switch', 'sensor']
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
TOL_TEMP = 0.3
|
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
CONF_NAME = 'name'
|
|
|
|
DEFAULT_NAME = 'Heat Control'
|
|
|
|
CONF_HEATER = 'heater'
|
|
|
|
CONF_SENSOR = 'target_sensor'
|
2015-11-30 08:14:32 +00:00
|
|
|
CONF_MIN_TEMP = 'min_temp'
|
|
|
|
CONF_MAX_TEMP = 'max_temp'
|
|
|
|
CONF_TARGET_TEMP = 'target_temp'
|
2015-10-23 05:04:37 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-21 14:59:35 +00:00
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Required("platform"): "heat_control",
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_HEATER): cv.entity_id,
|
|
|
|
vol.Required(CONF_SENSOR): cv.entity_id,
|
|
|
|
vol.Optional(CONF_MIN_TEMP): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_MAX_TEMP): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_TARGET_TEMP): vol.Coerce(float),
|
|
|
|
})
|
|
|
|
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
2015-03-01 09:35:58 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Setup the heat control thermostat."""
|
2016-04-21 14:59:35 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2015-10-23 05:04:37 +00:00
|
|
|
heater_entity_id = config.get(CONF_HEATER)
|
|
|
|
sensor_entity_id = config.get(CONF_SENSOR)
|
2016-04-21 14:59:35 +00:00
|
|
|
min_temp = config.get(CONF_MIN_TEMP)
|
|
|
|
max_temp = config.get(CONF_MAX_TEMP)
|
|
|
|
target_temp = config.get(CONF_TARGET_TEMP)
|
2015-10-23 05:04:37 +00:00
|
|
|
|
2015-11-30 09:05:05 +00:00
|
|
|
add_devices([HeatControl(hass, name, heater_entity_id, sensor_entity_id,
|
|
|
|
min_temp, max_temp, target_temp)])
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
class HeatControl(ThermostatDevice):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Representation of a HeatControl device."""
|
|
|
|
|
2015-11-30 09:32:32 +00:00
|
|
|
# pylint: disable=too-many-arguments
|
2015-11-30 08:22:04 +00:00
|
|
|
def __init__(self, hass, name, heater_entity_id, sensor_entity_id,
|
2015-11-30 09:05:05 +00:00
|
|
|
min_temp, max_temp, target_temp):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Initialize the thermostat."""
|
2015-02-17 18:12:27 +00:00
|
|
|
self.hass = hass
|
2015-10-23 05:04:37 +00:00
|
|
|
self._name = name
|
|
|
|
self.heater_entity_id = heater_entity_id
|
|
|
|
|
|
|
|
self._active = False
|
|
|
|
self._cur_temp = None
|
2015-11-30 08:14:32 +00:00
|
|
|
self._min_temp = min_temp
|
|
|
|
self._max_temp = max_temp
|
|
|
|
self._target_temp = target_temp
|
2015-10-23 05:04:37 +00:00
|
|
|
self._unit = None
|
|
|
|
|
|
|
|
track_state_change(hass, sensor_entity_id, self._sensor_changed)
|
|
|
|
|
|
|
|
sensor_state = hass.states.get(sensor_entity_id)
|
|
|
|
if sensor_state:
|
|
|
|
self._update_temp(sensor_state)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""No polling needed."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return False
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return the name of the thermostat."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return self._name
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return the unit of measurement."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return self._unit
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_temperature(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return the sensor temperature."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return self._cur_temp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def operation(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return current operation ie. heat, cool, idle."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return STATE_HEAT if self._active and self._is_heating else STATE_IDLE
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def target_temperature(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return the temperature we try to reach."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return self._target_temp
|
2015-02-17 18:12:27 +00:00
|
|
|
|
|
|
|
def set_temperature(self, temperature):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Set new target temperature."""
|
2015-10-23 05:04:37 +00:00
|
|
|
self._target_temp = temperature
|
|
|
|
self._control_heating()
|
|
|
|
self.update_ha_state()
|
|
|
|
|
2015-11-30 08:14:32 +00:00
|
|
|
@property
|
|
|
|
def min_temp(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return the minimum temperature."""
|
2015-11-30 10:45:09 +00:00
|
|
|
# pylint: disable=no-member
|
2015-11-30 08:14:32 +00:00
|
|
|
if self._min_temp:
|
|
|
|
return self._min_temp
|
|
|
|
else:
|
2015-11-30 10:45:09 +00:00
|
|
|
# get default temp from super class
|
2015-11-30 08:14:32 +00:00
|
|
|
return ThermostatDevice.min_temp.fget(self)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_temp(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Return the maximum temperature."""
|
2015-11-30 10:45:09 +00:00
|
|
|
# pylint: disable=no-member
|
2015-11-30 08:14:32 +00:00
|
|
|
if self._min_temp:
|
|
|
|
return self._max_temp
|
|
|
|
else:
|
2016-03-07 21:44:35 +00:00
|
|
|
# Get default temp from super class
|
2015-11-30 08:14:32 +00:00
|
|
|
return ThermostatDevice.max_temp.fget(self)
|
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
def _sensor_changed(self, entity_id, old_state, new_state):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Called when temperature changes."""
|
2015-10-23 05:04:37 +00:00
|
|
|
if new_state is None:
|
2015-02-17 18:12:27 +00:00
|
|
|
return
|
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
self._update_temp(new_state)
|
|
|
|
self._control_heating()
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def _update_temp(self, state):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Update thermostat with latest state from sensor."""
|
2015-10-23 05:04:37 +00:00
|
|
|
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
|
|
|
|
2016-04-20 03:30:44 +00:00
|
|
|
if unit not in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
|
2015-10-23 05:04:37 +00:00
|
|
|
self._cur_temp = None
|
|
|
|
self._unit = None
|
|
|
|
_LOGGER.error('Sensor has unsupported unit: %s (allowed: %s, %s)',
|
2016-04-20 03:30:44 +00:00
|
|
|
unit, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
2015-02-17 18:12:27 +00:00
|
|
|
return
|
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
temp = util.convert(state.state, float)
|
2015-02-17 18:12:27 +00:00
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
if temp is None:
|
|
|
|
self._cur_temp = None
|
|
|
|
self._unit = None
|
|
|
|
_LOGGER.error('Unable to parse sensor temperature: %s',
|
|
|
|
state.state)
|
|
|
|
return
|
2015-10-02 15:57:38 +00:00
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
self._cur_temp = temp
|
|
|
|
self._unit = unit
|
2015-02-17 18:12:27 +00:00
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
def _control_heating(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""Check if we need to turn heating on or off."""
|
2015-10-23 05:04:37 +00:00
|
|
|
if not self._active and None not in (self._cur_temp,
|
|
|
|
self._target_temp):
|
|
|
|
self._active = True
|
|
|
|
_LOGGER.info('Obtained current and target temperature. '
|
|
|
|
'Heat control active.')
|
2015-07-23 20:15:17 +00:00
|
|
|
|
2015-10-23 05:04:37 +00:00
|
|
|
if not self._active:
|
|
|
|
return
|
|
|
|
|
|
|
|
too_cold = self._target_temp - self._cur_temp > TOL_TEMP
|
|
|
|
is_heating = self._is_heating
|
|
|
|
|
|
|
|
if too_cold and not is_heating:
|
|
|
|
_LOGGER.info('Turning on heater %s', self.heater_entity_id)
|
|
|
|
switch.turn_on(self.hass, self.heater_entity_id)
|
|
|
|
elif not too_cold and is_heating:
|
|
|
|
_LOGGER.info('Turning off heater %s', self.heater_entity_id)
|
|
|
|
switch.turn_off(self.hass, self.heater_entity_id)
|
2015-07-23 20:15:17 +00:00
|
|
|
|
|
|
|
@property
|
2015-10-23 05:04:37 +00:00
|
|
|
def _is_heating(self):
|
2016-03-07 21:44:35 +00:00
|
|
|
"""If the heater is currently heating."""
|
2015-10-23 05:04:37 +00:00
|
|
|
return switch.is_on(self.hass, self.heater_entity_id)
|