2015-01-06 08:10:33 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.thermostat
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to interact with thermostats.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from homeassistant.helpers import (
|
|
|
|
extract_entity_ids, platform_devices_from_config)
|
|
|
|
import homeassistant.util as util
|
|
|
|
from homeassistant.helpers import Device
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID, ATTR_TEMPERATURE, ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
STATE_ON, STATE_OFF)
|
|
|
|
|
|
|
|
DOMAIN = "thermostat"
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
2015-02-28 16:36:37 +00:00
|
|
|
SERVICE_SET_AWAY_MODE = "set_away_mode"
|
2015-01-06 08:10:33 +00:00
|
|
|
SERVICE_SET_TEMPERATURE = "set_temperature"
|
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
ATTR_CURRENT_TEMPERATURE = "current_temperature"
|
2015-01-06 08:10:33 +00:00
|
|
|
ATTR_AWAY_MODE = "away_mode"
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def turn_away_mode_on(hass, entity_id=None):
|
|
|
|
""" Turn all or specified thermostat away mode on. """
|
2015-02-28 16:36:37 +00:00
|
|
|
data = {
|
|
|
|
ATTR_AWAY_MODE: True
|
|
|
|
}
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-02-28 16:36:37 +00:00
|
|
|
if entity_id:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_SET_AWAY_MODE, data)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def turn_away_mode_off(hass, entity_id=None):
|
|
|
|
""" Turn all or specified thermostat away mode off. """
|
2015-02-28 16:36:37 +00:00
|
|
|
data = {
|
|
|
|
ATTR_AWAY_MODE: False
|
|
|
|
}
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-02-28 16:36:37 +00:00
|
|
|
if entity_id:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_SET_AWAY_MODE, data)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_temperature(hass, temperature, entity_id=None):
|
|
|
|
""" Set new target temperature. """
|
|
|
|
data = {ATTR_TEMPERATURE: temperature}
|
|
|
|
|
|
|
|
if entity_id is not None:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_SET_TEMPERATURE, data)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Setup thermostats. """
|
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-01-06 08:10:33 +00:00
|
|
|
thermostats = platform_devices_from_config(
|
|
|
|
config, DOMAIN, hass, ENTITY_ID_FORMAT, _LOGGER)
|
|
|
|
|
|
|
|
if not thermostats:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
|
|
|
|
def update_state(now):
|
|
|
|
""" Update thermostat state. """
|
2015-02-17 18:12:27 +00:00
|
|
|
logger.info("Updating thermostat state")
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
for thermostat in thermostats.values():
|
|
|
|
thermostat.update_ha_state(hass, True)
|
|
|
|
|
|
|
|
# Update state every minute
|
|
|
|
hass.track_time_change(update_state, second=[0])
|
|
|
|
update_state(None)
|
|
|
|
|
|
|
|
def thermostat_service(service):
|
|
|
|
""" Handles calls to the services. """
|
2015-02-17 18:12:27 +00:00
|
|
|
|
2015-01-06 08:10:33 +00:00
|
|
|
# Convert the entity ids to valid light ids
|
|
|
|
target_thermostats = [thermostats[entity_id] for entity_id
|
|
|
|
in extract_entity_ids(hass, service)
|
|
|
|
if entity_id in thermostats]
|
|
|
|
|
|
|
|
if not target_thermostats:
|
|
|
|
target_thermostats = thermostats.values()
|
|
|
|
|
2015-02-28 16:36:37 +00:00
|
|
|
if service.service == SERVICE_SET_AWAY_MODE:
|
|
|
|
away_mode = service.data.get(ATTR_AWAY_MODE)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-02-28 16:36:37 +00:00
|
|
|
if away_mode is None:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Received call to %s without attribute %s",
|
|
|
|
SERVICE_SET_AWAY_MODE, ATTR_AWAY_MODE)
|
|
|
|
|
|
|
|
elif away_mode:
|
|
|
|
for thermostat in target_thermostats:
|
|
|
|
thermostat.turn_away_mode_on()
|
|
|
|
else:
|
|
|
|
for thermostat in target_thermostats:
|
|
|
|
thermostat.turn_away_mode_off()
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
elif service.service == SERVICE_SET_TEMPERATURE:
|
|
|
|
temperature = util.convert(
|
|
|
|
service.data.get(ATTR_TEMPERATURE), float)
|
|
|
|
|
|
|
|
if temperature is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
for thermostat in target_thermostats:
|
2015-02-17 18:12:27 +00:00
|
|
|
thermostat.set_temperature(temperature)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
for thermostat in target_thermostats:
|
|
|
|
thermostat.update_ha_state(hass, True)
|
|
|
|
|
|
|
|
hass.services.register(
|
2015-02-28 16:36:37 +00:00
|
|
|
DOMAIN, SERVICE_SET_AWAY_MODE, thermostat_service)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
hass.services.register(
|
|
|
|
DOMAIN, SERVICE_SET_TEMPERATURE, thermostat_service)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class ThermostatDevice(Device):
|
|
|
|
""" Represents a thermostat within Home Assistant. """
|
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the current state. """
|
|
|
|
return self.target_temperature
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2015-01-06 08:10:33 +00:00
|
|
|
""" Returns the unit of measurement. """
|
|
|
|
return ""
|
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
2015-01-06 08:10:33 +00:00
|
|
|
""" Returns device specific state attributes. """
|
2015-01-12 05:21:18 +00:00
|
|
|
return None
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2015-01-06 08:10:33 +00:00
|
|
|
""" Returns optional state attributes. """
|
|
|
|
data = {
|
2015-01-12 05:21:18 +00:00
|
|
|
ATTR_UNIT_OF_MEASUREMENT: self.unit_of_measurement,
|
|
|
|
ATTR_CURRENT_TEMPERATURE: self.current_temperature
|
2015-01-06 08:10:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
is_away = self.is_away_mode_on
|
|
|
|
|
|
|
|
if is_away is not None:
|
|
|
|
data[ATTR_AWAY_MODE] = STATE_ON if is_away else STATE_OFF
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
device_attr = self.device_state_attributes
|
|
|
|
|
|
|
|
if device_attr is not None:
|
|
|
|
data.update(device_attr)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
return data
|
2015-01-12 05:21:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_temperature(self):
|
|
|
|
""" Returns the current temperature. """
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
|
|
|
def target_temperature(self):
|
|
|
|
""" Returns the temperature we try to reach. """
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_away_mode_on(self):
|
|
|
|
"""
|
|
|
|
Returns if away mode is on.
|
|
|
|
Return None if no away mode available.
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_temperate(self, temperature):
|
|
|
|
""" Set new target temperature. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def turn_away_mode_on(self):
|
|
|
|
""" Turns away mode on. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def turn_away_mode_off(self):
|
|
|
|
""" Turns away mode off. """
|
|
|
|
pass
|