Files
esphome-dev/esphome/components/climate/__init__.py
T
Otto Winter 526a414743 Add climate support (#502)
## Description:


**Related issue (if applicable):** fixes <link to issue>

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>
**Pull request in [esphome-core](https://github.com/esphome/esphome-core) with C++ framework changes (if applicable):** esphome/esphome-core#<esphome-core PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-08 21:30:21 +02:00

116 lines
4.7 KiB
Python

import voluptuous as vol
from esphome import core
from esphome.automation import ACTION_REGISTRY
from esphome.components import mqtt
from esphome.components.mqtt import setup_mqtt_component
import esphome.config_validation as cv
from esphome.const import CONF_AWAY, CONF_ID, CONF_INTERNAL, CONF_MAX_TEMPERATURE, \
CONF_MIN_TEMPERATURE, CONF_MODE, CONF_MQTT_ID, CONF_TARGET_TEMPERATURE, \
CONF_TARGET_TEMPERATURE_HIGH, CONF_TARGET_TEMPERATURE_LOW, CONF_TEMPERATURE_STEP, CONF_VISUAL
from esphome.core import CORE
from esphome.cpp_generator import Pvariable, add, get_variable, templatable
from esphome.cpp_types import Action, App, Nameable, bool_, esphome_ns, float_
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
})
climate_ns = esphome_ns.namespace('climate')
ClimateDevice = climate_ns.class_('ClimateDevice', Nameable)
ClimateCall = climate_ns.class_('ClimateCall')
ClimateTraits = climate_ns.class_('ClimateTraits')
MQTTClimateComponent = climate_ns.class_('MQTTClimateComponent', mqtt.MQTTComponent)
ClimateMode = climate_ns.enum('ClimateMode')
CLIMATE_MODES = {
'OFF': ClimateMode.CLIMATE_MODE_OFF,
'AUTO': ClimateMode.CLIMATE_MODE_AUTO,
'COOL': ClimateMode.CLIMATE_MODE_COOL,
'HEAT': ClimateMode.CLIMATE_MODE_HEAT,
}
validate_climate_mode = cv.one_of(*CLIMATE_MODES, upper=True)
# Actions
ControlAction = climate_ns.class_('ControlAction', Action)
CLIMATE_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(ClimateDevice),
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTClimateComponent),
vol.Optional(CONF_VISUAL, default={}): cv.Schema({
vol.Optional(CONF_MIN_TEMPERATURE): cv.temperature,
vol.Optional(CONF_MAX_TEMPERATURE): cv.temperature,
vol.Optional(CONF_TEMPERATURE_STEP): cv.temperature,
})
})
CLIMATE_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(CLIMATE_SCHEMA.schema)
def setup_climate_core_(climate_var, config):
if CONF_INTERNAL in config:
add(climate_var.set_internal(config[CONF_INTERNAL]))
visual = config[CONF_VISUAL]
if CONF_MIN_TEMPERATURE in visual:
add(climate_var.set_visual_min_temperature_override(visual[CONF_MIN_TEMPERATURE]))
if CONF_MAX_TEMPERATURE in visual:
add(climate_var.set_visual_max_temperature_override(visual[CONF_MAX_TEMPERATURE]))
if CONF_TEMPERATURE_STEP in visual:
add(climate_var.set_visual_temperature_step_override(visual[CONF_TEMPERATURE_STEP]))
setup_mqtt_component(climate_var.Pget_mqtt(), config)
def register_climate(var, config):
if not CORE.has_id(config[CONF_ID]):
var = Pvariable(config[CONF_ID], var, has_side_effects=True)
add(App.register_climate(var))
CORE.add_job(setup_climate_core_, var, config)
BUILD_FLAGS = '-DUSE_CLIMATE'
CONF_CLIMATE_CONTROL = 'climate.control'
CLIMATE_CONTROL_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(ClimateDevice),
vol.Optional(CONF_MODE): cv.templatable(validate_climate_mode),
vol.Optional(CONF_TARGET_TEMPERATURE): cv.templatable(cv.temperature),
vol.Optional(CONF_TARGET_TEMPERATURE_LOW): cv.templatable(cv.temperature),
vol.Optional(CONF_TARGET_TEMPERATURE_HIGH): cv.templatable(cv.temperature),
vol.Optional(CONF_AWAY): cv.templatable(cv.boolean),
})
@ACTION_REGISTRY.register(CONF_CLIMATE_CONTROL, CLIMATE_CONTROL_ACTION_SCHEMA)
def climate_control_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
rhs = var.make_control_action(template_arg)
type = ControlAction.template(template_arg)
action = Pvariable(action_id, rhs, type=type)
if CONF_MODE in config:
if isinstance(config[CONF_MODE], core.Lambda):
for template_ in templatable(config[CONF_MODE], args, ClimateMode):
yield None
add(action.set_mode(template_))
else:
add(action.set_mode(CLIMATE_MODES[config[CONF_MODE]]))
if CONF_TARGET_TEMPERATURE in config:
for template_ in templatable(config[CONF_TARGET_TEMPERATURE], args, float_):
yield None
add(action.set_target_temperature(template_))
if CONF_TARGET_TEMPERATURE_LOW in config:
for template_ in templatable(config[CONF_TARGET_TEMPERATURE_LOW], args, float_):
yield None
add(action.set_target_temperature_low(template_))
if CONF_TARGET_TEMPERATURE_HIGH in config:
for template_ in templatable(config[CONF_TARGET_TEMPERATURE_HIGH], args, float_):
yield None
add(action.set_target_temperature_high(template_))
if CONF_AWAY in config:
for template_ in templatable(config[CONF_AWAY], args, bool_):
yield None
add(action.set_away(template_))
yield action