core/homeassistant/components/thermostat/heat_control.py

216 lines
7.4 KiB
Python
Raw Normal View History

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
import voluptuous as vol
2015-02-17 18:12:27 +00:00
import homeassistant.helpers.config_validation as cv
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_COOL, STATE_IDLE, ThermostatDevice)
2016-08-09 00:42:34 +00:00
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_ON, STATE_OFF
from homeassistant.helpers import condition
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'
CONF_AC_MODE = 'ac_mode'
CONF_MIN_DUR = 'min_cycle_duration'
2015-10-23 05:04:37 +00:00
_LOGGER = logging.getLogger(__name__)
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),
vol.Optional(CONF_AC_MODE): vol.Coerce(bool),
vol.Optional(CONF_MIN_DUR): vol.All(cv.time_period, cv.positive_timedelta),
})
2015-02-17 18:12:27 +00:00
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."""
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)
min_temp = config.get(CONF_MIN_TEMP)
max_temp = config.get(CONF_MAX_TEMP)
target_temp = config.get(CONF_TARGET_TEMP)
ac_mode = config.get(CONF_AC_MODE)
min_cycle_duration = config.get(CONF_MIN_DUR)
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, ac_mode,
min_cycle_duration)])
2015-02-17 18:12:27 +00:00
# pylint: disable=too-many-instance-attributes, abstract-method
2015-02-17 18:12:27 +00:00
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,
min_temp, max_temp, target_temp, ac_mode, min_cycle_duration):
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.ac_mode = ac_mode
self.min_cycle_duration = min_cycle_duration
2015-10-23 05:04:37 +00:00
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
Add unit system support Add unit symbol constants Initial unit system object Import more constants Pydoc for unit system file Import constants for configuration validation Unit system validation method Typing for constants Inches are valid lengths too Typings Change base class to dict - needed for remote api call serialization Validation Use dictionary keys Defined unit systems Update location util to use metric instead of us fahrenheit Update constant imports Import defined unit systems Update configuration to use unit system Update schema to use unit system Update constants Add imports to core for unit system and distance Type for config Default unit system Convert distance from HASS instance Update temperature conversion to use unit system Update temperature conversion Set unit system based on configuration Set info unit system Return unit system dictionary with config dictionary Auto discover unit system Update location test for use metric Update forecast unit system Update mold indicator unit system Update thermostat unit system Update thermostat demo test Unit tests around unit system Update test common hass configuration Update configuration unit tests There should always be a unit system! Update core unit tests Constants typing Linting issues Remove unused import Update fitbit sensor to use application unit system Update google travel time to use application unit system Update configuration example Update dht sensor Update DHT temperature conversion to use the utility function Update swagger config Update my sensors metric flag Update hvac component temperature conversion HVAC conversion for temperature Pull unit from sensor type map Pull unit from sensor type map Update the temper sensor unit Update yWeather sensor unit Update hvac demo unit test Set unit test config unit system to metric Use hass unit system length for default in proximity Use the name of the system instead of temperature Use constants from const Unused import Forecasted temperature Fix calculation in case furthest distance is greater than 1000000 units Remove unneeded constants Set default length to km or miles Use constants Linting doesn't like importing just for typing Fix reference Test is expecting meters - set config to meters Use constant Use constant PyDoc for unit test Should be not in Rename to units Change unit system to be an object - not a dictionary Return tuple in conversion Move convert to temperature util Temperature conversion is now in unit system Update imports Rename to units Units is now an object Use temperature util conversion Unit system is now an object Validate and convert unit system config Return the scalar value in template distance Test is expecting meters Update unit tests around unit system Distance util returns tuple Fix location info test Set units Update unit tests Convert distance DOH Pull out the scalar from the vector Linting I really hate python linting Linting again BLARG Unit test documentation Unit test around is metric flag Break ternary statement into if/else blocks Don't use dictionary - use members is metric flag Rename constants Use is metric flag Move constants to CONST file Move to const file Raise error if unit is not expected Typing No need to return unit since only performing conversion if it can work Use constants Line wrapping Raise error if invalid value Remove subscripts from conversion as they are no longer returned as tuples No longer tuples No longer tuples Check for numeric type Fix string format to use correct variable Typing Assert errors raised Remove subscript Only convert temperature if we know the unit If no unit of measurement set - default to HASS config Convert only if we know the unit Remove subscription Fix not in clause Linting fixes Wants a boolean Clearer if-block Check if the key is in the config first Missed a couple expecting tuples Backwards compatibility No like-y ternary! Error handling around state setting Pretty unit system configuration validation More tuple crap Use is metric flag Error handling around min/max temp Explode if no unit Pull unit from config Celsius has a decimal Unused import Check if it's a temperature before we try to convert it to a temperature Linting says too many statements - combine lat/long in a fairly reasonable manner Backwards compatibility unit test Better doc
2016-07-31 20:24:49 +00:00
self._unit = hass.config.units.temperature_unit
2015-10-23 05:04:37 +00:00
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."""
if self.ac_mode:
cooling = self._active and self._is_device_active
return STATE_COOL if cooling else STATE_IDLE
else:
heating = self._active and self._is_device_active
return STATE_HEAT if 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-08-05 05:37:30 +00:00
try:
self._cur_temp = self.hass.config.units.temperature(
float(state.state), unit)
except ValueError as ex:
_LOGGER.error('Unable to update from sensor: %s', ex)
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
if self.min_cycle_duration:
if self._is_device_active:
current_state = STATE_ON
else:
current_state = STATE_OFF
long_enough = condition.state(self.hass, self.heater_entity_id,
current_state,
self.min_cycle_duration)
if not long_enough:
return
if self.ac_mode:
too_hot = self._cur_temp - self._target_temp > TOL_TEMP
is_cooling = self._is_device_active
if too_hot and not is_cooling:
_LOGGER.info('Turning on AC %s', self.heater_entity_id)
switch.turn_on(self.hass, self.heater_entity_id)
elif not too_hot and is_cooling:
_LOGGER.info('Turning off AC %s', self.heater_entity_id)
switch.turn_off(self.hass, self.heater_entity_id)
else:
too_cold = self._target_temp - self._cur_temp > TOL_TEMP
is_heating = self._is_device_active
2015-10-23 05:04:37 +00:00
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
def _is_device_active(self):
"""If the toggleable device is currently active."""
2015-10-23 05:04:37 +00:00
return switch.is_on(self.hass, self.heater_entity_id)