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).
This commit is contained in:
Otto Winter 2019-04-08 21:30:21 +02:00 committed by GitHub
parent 7fd5634a51
commit 526a414743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 208 additions and 12 deletions

View File

@ -0,0 +1,115 @@
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

View File

@ -0,0 +1,66 @@
import voluptuous as vol
from esphome import automation
from esphome.components import climate, sensor
import esphome.config_validation as cv
from esphome.const import CONF_AWAY_CONFIG, CONF_COOL_ACTION, \
CONF_DEFAULT_TARGET_TEMPERATURE_HIGH, \
CONF_DEFAULT_TARGET_TEMPERATURE_LOW, CONF_HEAT_ACTION, CONF_ID, CONF_IDLE_ACTION, CONF_NAME, \
CONF_SENSOR
from esphome.cpp_generator import Pvariable, add, get_variable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App
BangBangClimate = climate.climate_ns.class_('BangBangClimate', climate.ClimateDevice)
BangBangClimateTargetTempConfig = climate.climate_ns.struct('BangBangClimateTargetTempConfig')
PLATFORM_SCHEMA = cv.nameable(climate.CLIMATE_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(BangBangClimate),
vol.Required(CONF_SENSOR): cv.use_variable_id(sensor.Sensor),
vol.Required(CONF_DEFAULT_TARGET_TEMPERATURE_LOW): cv.temperature,
vol.Required(CONF_DEFAULT_TARGET_TEMPERATURE_HIGH): cv.temperature,
vol.Required(CONF_IDLE_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_COOL_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_HEAT_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_AWAY_CONFIG): cv.Schema({
vol.Required(CONF_DEFAULT_TARGET_TEMPERATURE_LOW): cv.temperature,
vol.Required(CONF_DEFAULT_TARGET_TEMPERATURE_HIGH): cv.temperature,
}),
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.register_component(BangBangClimate.new(config[CONF_NAME]))
control = Pvariable(config[CONF_ID], rhs)
climate.register_climate(control, config)
setup_component(control, config)
for var in get_variable(config[CONF_SENSOR]):
yield
add(control.set_sensor(var))
normal_config = BangBangClimateTargetTempConfig(
config[CONF_DEFAULT_TARGET_TEMPERATURE_LOW],
config[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH]
)
add(control.set_normal_config(normal_config))
automation.build_automations(control.get_idle_trigger(), [], config[CONF_IDLE_ACTION])
if CONF_COOL_ACTION in config:
automation.build_automations(control.get_cool_trigger(), [], config[CONF_COOL_ACTION])
add(control.set_supports_cool(True))
if CONF_HEAT_ACTION in config:
automation.build_automations(control.get_heat_trigger(), [], config[CONF_HEAT_ACTION])
add(control.set_supports_heat(True))
if CONF_AWAY_CONFIG in config:
away = config[CONF_AWAY_CONFIG]
away_config = BangBangClimateTargetTempConfig(
away[CONF_DEFAULT_TARGET_TEMPERATURE_LOW],
away[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH]
)
add(control.set_away_config(away_config))
BUILD_FLAGS = '-DUSE_BANG_BANG_CLIMATE'

View File

@ -420,7 +420,7 @@ METRIC_SUFFIXES = {
def float_with_unit(quantity, regex_suffix):
pattern = re.compile(ur"^([-+]?[0-9]*\.?[0-9]*)\s*(\w*?)" + regex_suffix + ur"$", re.UNICODE)
pattern = re.compile(r"^([-+]?[0-9]*\.?[0-9]*)\s*(\w*?)" + regex_suffix + r"$", re.UNICODE)
def validator(value):
match = pattern.match(string(value))
@ -438,21 +438,22 @@ def float_with_unit(quantity, regex_suffix):
return validator
frequency = float_with_unit("frequency", ur"(Hz|HZ|hz)?")
resistance = float_with_unit("resistance", ur"(Ω|Ω|ohm|Ohm|OHM)?")
current = float_with_unit("current", ur"(a|A|amp|Amp|amps|Amps|ampere|Ampere)?")
voltage = float_with_unit("voltage", ur"(v|V|volt|Volts)?")
distance = float_with_unit("distance", ur"(m)")
framerate = float_with_unit("framerate", ur"(FPS|fps|Fps|FpS|Hz)")
_temperature_c = float_with_unit("temperature", ur"(°C|° C|°|C)?")
_temperature_k = float_with_unit("temperature", ur"(° K|° K|K)?")
_temperature_f = float_with_unit("temperature", ur"(°F|° F|F)?")
frequency = float_with_unit("frequency", u"(Hz|HZ|hz)?")
resistance = float_with_unit("resistance", u"(Ω|Ω|ohm|Ohm|OHM)?")
current = float_with_unit("current", u"(a|A|amp|Amp|amps|Amps|ampere|Ampere)?")
voltage = float_with_unit("voltage", u"(v|V|volt|Volts)?")
distance = float_with_unit("distance", u"(m)")
framerate = float_with_unit("framerate", u"(FPS|fps|Fps|FpS|Hz)")
_temperature_c = float_with_unit("temperature", u"(°C|° C|°|C)?")
_temperature_k = float_with_unit("temperature", u"(° K|° K|K)?")
_temperature_f = float_with_unit("temperature", u"(°F|° F|F)?")
if IS_PY2:
# Override voluptuous invalid to unicode for py2
def _vol_invalid_unicode(self):
path = u' @ data[%s]' % u']['.join(map(repr, self.path)) \
if self.path else ''
# pylint: disable=no-member
output = Exception.__unicode__(self)
if self.error_type:
output += u' for ' + self.error_type
@ -482,8 +483,8 @@ def temperature(value):
raise orig_err
_color_temperature_mireds = float_with_unit('Color Temperature', ur'(mireds|Mireds)')
_color_temperature_kelvin = float_with_unit('Color Temperature', ur'(K|Kelvin)')
_color_temperature_mireds = float_with_unit('Color Temperature', r'(mireds|Mireds)')
_color_temperature_kelvin = float_with_unit('Color Temperature', r'(K|Kelvin)')
def color_temperature(value):

View File

@ -400,6 +400,20 @@ CONF_RESTORE_VALUE = 'restore_value'
CONF_PINS = 'pins'
CONF_SENSORS = 'sensors'
CONF_BINARY_SENSORS = 'binary_sensors'
CONF_AWAY = 'away'
CONF_MIN_TEMPERATURE = 'min_temperature'
CONF_MAX_TEMPERATURE = 'max_temperature'
CONF_TARGET_TEMPERATURE = 'target_temperature'
CONF_TARGET_TEMPERATURE_LOW = 'target_temperature_low'
CONF_TARGET_TEMPERATURE_HIGH = 'target_temperature_high'
CONF_TEMPERATURE_STEP = 'temperature_step'
CONF_VISUAL = 'visual'
CONF_AWAY_CONFIG = 'away_config'
CONF_COOL_ACTION = 'cool_action'
CONF_HEAT_ACTION = 'heat_action'
CONF_IDLE_ACTION = 'idle_action'
CONF_DEFAULT_TARGET_TEMPERATURE_HIGH = 'default_target_temperature_high'
CONF_DEFAULT_TARGET_TEMPERATURE_LOW = 'default_target_temperature_low'
CONF_OUTPUTS = 'outputs'
CONF_SWITCHES = 'switches'
CONF_TEXT_SENSORS = 'text_sensors'