1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-15 23:30:28 +01:00

Add climate support

This commit is contained in:
Otto Winter 2019-03-27 22:10:23 +01:00
parent 1f7e1daa73
commit c6e2e1dfa0
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E
4 changed files with 243 additions and 8 deletions

View File

@ -0,0 +1,119 @@
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)
yield Pvariable(action_id, rhs, type=type)
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,65 @@
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_SENSOR, \
CONF_NAME
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

@ -16,7 +16,7 @@ from esphome.const import CONF_AVAILABILITY, CONF_COMMAND_TOPIC, CONF_DISCOVERY,
ESP_PLATFORM_ESP8266
from esphome.core import CORE, HexInt, IPAddress, Lambda, TimePeriod, TimePeriodMicroseconds, \
TimePeriodMilliseconds, TimePeriodSeconds, TimePeriodMinutes
from esphome.py_compat import integer_types, string_types, text_type
from esphome.py_compat import integer_types, string_types, text_type, IS_PY2
from esphome.voluptuous_schema import _Schema
_LOGGER = logging.getLogger(__name__)
@ -420,7 +420,7 @@ METRIC_SUFFIXES = {
def float_with_unit(quantity, regex_suffix):
pattern = re.compile(r"^([-+]?[0-9]*\.?[0-9]*)\s*(\w*?)" + regex_suffix + "$")
pattern = re.compile(ur"^([-+]?[0-9]*\.?[0-9]*)\s*(\w*?)" + regex_suffix + ur"$", re.UNICODE)
def validator(value):
match = pattern.match(string(value))
@ -438,12 +438,49 @@ def float_with_unit(quantity, regex_suffix):
return validator
frequency = float_with_unit("frequency", r"(Hz|HZ|hz)?")
resistance = float_with_unit("resistance", r"(Ω|Ω|ohm|Ohm|OHM)?")
current = float_with_unit("current", r"(a|A|amp|Amp|amps|Amps|ampere|Ampere)?")
voltage = float_with_unit("voltage", r"(v|V|volt|Volts)?")
distance = float_with_unit("distance", r"(m)")
framerate = float_with_unit("framerate", r"(FPS|fps|Fps|FpS|Hz)")
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)?")
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 ''
output = Exception.__unicode__(self)
if self.error_type:
output += u' for ' + self.error_type
return output + path
vol.Invalid.__unicode__ = _vol_invalid_unicode
def temperature(value):
try:
return _temperature_c(value)
except vol.Invalid as orig_err:
pass
try:
kelvin = _temperature_k(value)
return kelvin - 273.15
except vol.Invalid:
pass
try:
fahrenheit = _temperature_f(value)
return (fahrenheit - 32) * (5/9)
except vol.Invalid:
pass
raise orig_err
def validate_bytes(value):

View File

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