1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 15:12:21 +01:00

Add cover position/tilt support (#496)

* Add climate support

* Fixes

* Updates

* Updates

* Add ota

* Remove climate changes

* Lint
This commit is contained in:
Otto Winter
2019-04-08 18:08:58 +02:00
committed by GitHub
parent cb9f36a153
commit 7fd5634a51
21 changed files with 425 additions and 192 deletions

View File

@@ -1,41 +1,58 @@
import voluptuous as vol
from esphome.automation import ACTION_REGISTRY, maybe_simple_id
from esphome.automation import ACTION_REGISTRY, maybe_simple_id, Condition
from esphome.components import mqtt
from esphome.components.mqtt import setup_mqtt_component
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERNAL, CONF_MQTT_ID
from esphome.const import CONF_ID, CONF_INTERNAL, CONF_MQTT_ID, CONF_DEVICE_CLASS, CONF_STATE, \
CONF_POSITION, CONF_TILT, CONF_STOP
from esphome.core import CORE
from esphome.cpp_generator import Pvariable, add, get_variable
from esphome.cpp_types import Action, Nameable, esphome_ns
from esphome.cpp_generator import Pvariable, add, get_variable, templatable
from esphome.cpp_types import Action, Nameable, esphome_ns, App
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
})
DEVICE_CLASSES = [
'', 'awning', 'blind', 'curtain', 'damper', 'door', 'garage',
'shade', 'shutter', 'window'
]
cover_ns = esphome_ns.namespace('cover')
Cover = cover_ns.class_('Cover', Nameable)
MQTTCoverComponent = cover_ns.class_('MQTTCoverComponent', mqtt.MQTTComponent)
CoverState = cover_ns.class_('CoverState')
COVER_OPEN = cover_ns.COVER_OPEN
COVER_CLOSED = cover_ns.COVER_CLOSED
validate_cover_state = cv.one_of('OPEN', 'CLOSED', upper=True)
COVER_STATES = {
'OPEN': COVER_OPEN,
'CLOSED': COVER_CLOSED,
}
validate_cover_state = cv.one_of(*COVER_STATES, upper=True)
CoverOperation = cover_ns.enum('CoverOperation')
COVER_OPERATIONS = {
'IDLE': CoverOperation.COVER_OPERATION_IDLE,
'OPENING': CoverOperation.COVER_OPERATION_OPENING,
'CLOSING': CoverOperation.COVER_OPERATION_CLOSING,
}
validate_cover_operation = cv.one_of(*COVER_OPERATIONS, upper=True)
# Actions
OpenAction = cover_ns.class_('OpenAction', Action)
CloseAction = cover_ns.class_('CloseAction', Action)
StopAction = cover_ns.class_('StopAction', Action)
ControlAction = cover_ns.class_('ControlAction', Action)
CoverIsOpenCondition = cover_ns.class_('CoverIsOpenCondition', Condition)
CoverIsClosedCondition = cover_ns.class_('CoverIsClosedCondition', Condition)
COVER_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(Cover),
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTCoverComponent),
vol.Optional(CONF_DEVICE_CLASS): cv.one_of(*DEVICE_CLASSES, lower=True),
})
COVER_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(COVER_SCHEMA.schema)
@@ -44,6 +61,8 @@ COVER_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(COVER_SCHEMA.schema)
def setup_cover_core_(cover_var, config):
if CONF_INTERNAL in config:
add(cover_var.set_internal(config[CONF_INTERNAL]))
if CONF_DEVICE_CLASS in config:
add(cover_var.set_device_class(config[CONF_DEVICE_CLASS]))
setup_mqtt_component(cover_var.Pget_mqtt(), config)
@@ -51,6 +70,13 @@ def setup_cover(cover_obj, config):
CORE.add_job(setup_cover_core_, cover_obj, config)
def register_cover(var, config):
if not CORE.has_id(config[CONF_ID]):
var = Pvariable(config[CONF_ID], var, has_side_effects=True)
add(App.register_cover(var))
CORE.add_job(setup_cover_core_, var, config)
BUILD_FLAGS = '-DUSE_COVER'
CONF_COVER_OPEN = 'cover.open'
@@ -63,8 +89,8 @@ COVER_OPEN_ACTION_SCHEMA = maybe_simple_id({
def cover_open_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
rhs = var.make_open_action(template_arg)
type = OpenAction.template(template_arg)
rhs = type.new(var)
yield Pvariable(action_id, rhs, type=type)
@@ -78,8 +104,8 @@ COVER_CLOSE_ACTION_SCHEMA = maybe_simple_id({
def cover_close_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
rhs = var.make_close_action(template_arg)
type = CloseAction.template(template_arg)
rhs = type.new(var)
yield Pvariable(action_id, rhs, type=type)
@@ -93,6 +119,43 @@ COVER_STOP_ACTION_SCHEMA = maybe_simple_id({
def cover_stop_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
rhs = var.make_stop_action(template_arg)
type = StopAction.template(template_arg)
rhs = type.new(var)
yield Pvariable(action_id, rhs, type=type)
CONF_COVER_CONTROL = 'cover.control'
COVER_CONTROL_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(Cover),
vol.Optional(CONF_STOP): cv.templatable(cv.boolean),
vol.Exclusive(CONF_STATE, 'pos'): cv.templatable(cv.one_of(*COVER_STATES)),
vol.Exclusive(CONF_POSITION, 'pos'): cv.templatable(cv.percentage),
vol.Optional(CONF_TILT): cv.templatable(cv.percentage),
})
@ACTION_REGISTRY.register(CONF_COVER_CONTROL, COVER_CONTROL_ACTION_SCHEMA)
def cover_control_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
type = StopAction.template(template_arg)
rhs = type.new(var)
action = Pvariable(action_id, rhs, type=type)
if CONF_STOP in config:
for template_ in templatable(config[CONF_STOP], args, bool):
yield None
add(action.set_stop(template_))
if CONF_STATE in config:
for template_ in templatable(config[CONF_STATE], args, float,
to_exp=COVER_STATES):
yield None
add(action.set_position(template_))
if CONF_POSITION in config:
for template_ in templatable(config[CONF_POSITION], args, float):
yield None
add(action.set_position(template_))
if CONF_TILT in config:
for template_ in templatable(config[CONF_TILT], args, float):
yield None
add(action.set_tilt(template_))
yield action

View File

@@ -0,0 +1,57 @@
import voluptuous as vol
from esphome import automation
from esphome.components import binary_sensor, cover
import esphome.config_validation as cv
from esphome.const import CONF_CLOSE_ACTION, CONF_CLOSE_DURATION, \
CONF_CLOSE_ENDSTOP, CONF_ID, CONF_NAME, CONF_OPEN_ACTION, CONF_OPEN_DURATION, \
CONF_OPEN_ENDSTOP, CONF_STOP_ACTION, CONF_MAX_DURATION
from esphome.cpp_generator import Pvariable, add, get_variable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, Component
EndstopCover = cover.cover_ns.class_('EndstopCover', cover.Cover, Component)
PLATFORM_SCHEMA = cv.nameable(cover.COVER_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(EndstopCover),
vol.Required(CONF_STOP_ACTION): automation.validate_automation(single=True),
vol.Required(CONF_OPEN_ENDSTOP): cv.use_variable_id(binary_sensor.BinarySensor),
vol.Required(CONF_OPEN_ACTION): automation.validate_automation(single=True),
vol.Required(CONF_OPEN_DURATION): cv.positive_time_period_milliseconds,
vol.Required(CONF_CLOSE_ACTION): automation.validate_automation(single=True),
vol.Required(CONF_CLOSE_ENDSTOP): cv.use_variable_id(binary_sensor.BinarySensor),
vol.Required(CONF_CLOSE_DURATION): cv.positive_time_period_milliseconds,
vol.Optional(CONF_MAX_DURATION): cv.positive_time_period_milliseconds,
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.register_component(EndstopCover.new(config[CONF_NAME]))
var = Pvariable(config[CONF_ID], rhs)
cover.register_cover(var, config)
setup_component(var, config)
automation.build_automations(var.get_stop_trigger(), [],
config[CONF_STOP_ACTION])
for bin in get_variable(config[CONF_OPEN_ENDSTOP]):
yield
add(var.set_open_endstop(bin))
add(var.set_open_duration(config[CONF_OPEN_DURATION]))
automation.build_automations(var.get_open_trigger(), [],
config[CONF_OPEN_ACTION])
for bin in get_variable(config[CONF_CLOSE_ENDSTOP]):
yield
add(var.set_close_endstop(bin))
add(var.set_close_duration(config[CONF_CLOSE_DURATION]))
automation.build_automations(var.get_close_trigger(), [],
config[CONF_CLOSE_ACTION])
if CONF_MAX_DURATION in config:
add(var.set_max_duration(config[CONF_MAX_DURATION]))
BUILD_FLAGS = '-DUSE_ENDSTOP_COVER'

View File

@@ -5,15 +5,22 @@ from esphome.automation import ACTION_REGISTRY
from esphome.components import cover
import esphome.config_validation as cv
from esphome.const import CONF_ASSUMED_STATE, CONF_CLOSE_ACTION, CONF_ID, CONF_LAMBDA, CONF_NAME, \
CONF_OPEN_ACTION, CONF_OPTIMISTIC, CONF_STATE, CONF_STOP_ACTION
CONF_OPEN_ACTION, CONF_OPTIMISTIC, CONF_STATE, CONF_STOP_ACTION, CONF_POSITION, \
CONF_CURRENT_OPERATION, CONF_RESTORE_MODE
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import Action, App, optional
from esphome.py_compat import string_types
TemplateCover = cover.cover_ns.class_('TemplateCover', cover.Cover)
CoverPublishAction = cover.cover_ns.class_('CoverPublishAction', Action)
TemplateCoverRestoreMode = cover.cover_ns.enum('TemplateCoverRestoreMode')
RESTORE_MODES = {
'NO_RESTORE': TemplateCoverRestoreMode.NO_RESTORE,
'RESTORE': TemplateCoverRestoreMode.RESTORE,
'RESTORE_AND_CALL': TemplateCoverRestoreMode.RESTORE_AND_CALL,
}
PLATFORM_SCHEMA = cv.nameable(cover.COVER_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(TemplateCover),
vol.Optional(CONF_LAMBDA): cv.lambda_,
@@ -22,19 +29,19 @@ PLATFORM_SCHEMA = cv.nameable(cover.COVER_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_OPEN_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_CLOSE_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_STOP_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_RESTORE_MODE): cv.one_of(*RESTORE_MODES, upper=True),
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.make_template_cover(config[CONF_NAME])
rhs = App.register_component(TemplateCover.new(config[CONF_NAME]))
var = Pvariable(config[CONF_ID], rhs)
cover.setup_cover(var, config)
cover.register_cover(var, config)
setup_component(var, config)
if CONF_LAMBDA in config:
for template_ in process_lambda(config[CONF_LAMBDA], [],
return_type=optional.template(cover.CoverState)):
return_type=optional.template(float)):
yield
add(var.set_state_lambda(template_))
if CONF_OPEN_ACTION in config:
@@ -50,6 +57,8 @@ def to_code(config):
add(var.set_optimistic(config[CONF_OPTIMISTIC]))
if CONF_ASSUMED_STATE in config:
add(var.set_assumed_state(config[CONF_ASSUMED_STATE]))
if CONF_RESTORE_MODE in config:
add(var.set_restore_mode(RESTORE_MODES[config[CONF_RESTORE_MODE]]))
BUILD_FLAGS = '-DUSE_TEMPLATE_COVER'
@@ -57,7 +66,9 @@ BUILD_FLAGS = '-DUSE_TEMPLATE_COVER'
CONF_COVER_TEMPLATE_PUBLISH = 'cover.template.publish'
COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(cover.Cover),
vol.Required(CONF_STATE): cv.templatable(cover.validate_cover_state),
vol.Exclusive(CONF_STATE, 'pos'): cv.templatable(cover.validate_cover_state),
vol.Exclusive(CONF_POSITION, 'pos'): cv.templatable(cv.zero_to_one_float),
vol.Optional(CONF_CURRENT_OPERATION): cv.templatable(cover.validate_cover_operation),
})
@@ -66,14 +77,22 @@ COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({
def cover_template_publish_to_code(config, action_id, template_arg, args):
for var in get_variable(config[CONF_ID]):
yield None
rhs = var.make_cover_publish_action(template_arg)
type = CoverPublishAction.template(template_arg)
rhs = type.new(var)
action = Pvariable(action_id, rhs, type=type)
state = config[CONF_STATE]
if isinstance(state, string_types):
template_ = cover.COVER_STATES[state]
else:
for template_ in templatable(state, args, cover.CoverState):
if CONF_STATE in config:
for template_ in templatable(config[CONF_STATE], args, float,
to_exp=cover.COVER_STATES):
yield None
add(action.set_state(template_))
add(action.set_position(template_))
if CONF_POSITION in config:
for template_ in templatable(config[CONF_POSITION], args, float,
to_exp=cover.COVER_STATES):
yield None
add(action.set_position(template_))
if CONF_CURRENT_OPERATION in config:
for template_ in templatable(config[CONF_CURRENT_OPERATION], args, cover.CoverOperation,
to_exp=cover.COVER_OPERATIONS):
yield None
add(action.set_current_operation(template_))
yield action

View File

@@ -0,0 +1,44 @@
import voluptuous as vol
from esphome import automation
from esphome.components import cover
import esphome.config_validation as cv
from esphome.const import CONF_CLOSE_ACTION, CONF_CLOSE_DURATION, CONF_ID, CONF_NAME, \
CONF_OPEN_ACTION, CONF_OPEN_DURATION, CONF_STOP_ACTION
from esphome.cpp_generator import Pvariable, add
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, Component
TimeBasedCover = cover.cover_ns.class_('TimeBasedCover', cover.Cover, Component)
PLATFORM_SCHEMA = cv.nameable(cover.COVER_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(TimeBasedCover),
vol.Required(CONF_STOP_ACTION): automation.validate_automation(single=True),
vol.Required(CONF_OPEN_ACTION): automation.validate_automation(single=True),
vol.Required(CONF_OPEN_DURATION): cv.positive_time_period_milliseconds,
vol.Required(CONF_CLOSE_ACTION): automation.validate_automation(single=True),
vol.Required(CONF_CLOSE_DURATION): cv.positive_time_period_milliseconds,
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.register_component(TimeBasedCover.new(config[CONF_NAME]))
var = Pvariable(config[CONF_ID], rhs)
cover.register_cover(var, config)
setup_component(var, config)
automation.build_automations(var.get_stop_trigger(), [],
config[CONF_STOP_ACTION])
add(var.set_open_duration(config[CONF_OPEN_DURATION]))
automation.build_automations(var.get_open_trigger(), [],
config[CONF_OPEN_ACTION])
add(var.set_close_duration(config[CONF_CLOSE_DURATION]))
automation.build_automations(var.get_close_trigger(), [],
config[CONF_CLOSE_ACTION])
BUILD_FLAGS = '-DUSE_TIME_BASED_COVER'