2018-10-16 23:16:06 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-01-06 19:38:23 +01:00
|
|
|
from esphomeyaml.automation import ACTION_REGISTRY, maybe_simple_id
|
2018-11-03 14:08:31 +01:00
|
|
|
from esphomeyaml.components import mqtt
|
2019-01-06 19:38:23 +01:00
|
|
|
from esphomeyaml.components.mqtt import setup_mqtt_component
|
2018-05-20 12:41:52 +02:00
|
|
|
import esphomeyaml.config_validation as cv
|
2019-01-06 19:38:23 +01:00
|
|
|
from esphomeyaml.const import CONF_ID, CONF_INTERNAL, CONF_MQTT_ID
|
|
|
|
from esphomeyaml.cpp_generator import Pvariable, add, get_variable
|
|
|
|
from esphomeyaml.cpp_types import Action, Nameable, esphomelib_ns
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
cover_ns = esphomelib_ns.namespace('cover')
|
2018-11-12 23:30:31 +01:00
|
|
|
|
|
|
|
Cover = cover_ns.class_('Cover', Nameable)
|
|
|
|
MQTTCoverComponent = cover_ns.class_('MQTTCoverComponent', mqtt.MQTTComponent)
|
|
|
|
|
|
|
|
CoverState = cover_ns.class_('CoverState')
|
2018-05-20 12:41:52 +02:00
|
|
|
COVER_OPEN = cover_ns.COVER_OPEN
|
|
|
|
COVER_CLOSED = cover_ns.COVER_CLOSED
|
2018-11-12 23:30:31 +01:00
|
|
|
|
|
|
|
# Actions
|
|
|
|
OpenAction = cover_ns.class_('OpenAction', Action)
|
|
|
|
CloseAction = cover_ns.class_('CloseAction', Action)
|
|
|
|
StopAction = cover_ns.class_('StopAction', Action)
|
2018-05-20 12:41:52 +02:00
|
|
|
|
2018-06-02 22:22:20 +02:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
|
2018-06-11 10:01:54 +02:00
|
|
|
COVER_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(COVER_SCHEMA.schema)
|
|
|
|
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
def setup_cover_core_(cover_var, mqtt_var, config):
|
2018-06-11 10:01:54 +02:00
|
|
|
if CONF_INTERNAL in config:
|
|
|
|
add(cover_var.set_internal(config[CONF_INTERNAL]))
|
2018-05-20 12:41:52 +02:00
|
|
|
setup_mqtt_component(mqtt_var, config)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_cover(cover_obj, mqtt_obj, config):
|
2018-06-02 22:22:20 +02:00
|
|
|
cover_var = Pvariable(config[CONF_ID], cover_obj, has_side_effects=False)
|
|
|
|
mqtt_var = Pvariable(config[CONF_MQTT_ID], mqtt_obj, has_side_effects=False)
|
2018-05-20 12:41:52 +02:00
|
|
|
setup_cover_core_(cover_var, mqtt_var, config)
|
|
|
|
|
|
|
|
|
|
|
|
BUILD_FLAGS = '-DUSE_COVER'
|
2018-10-16 23:16:06 +02:00
|
|
|
|
|
|
|
CONF_COVER_OPEN = 'cover.open'
|
|
|
|
COVER_OPEN_ACTION_SCHEMA = maybe_simple_id({
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
2018-10-16 23:16:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_COVER_OPEN, COVER_OPEN_ACTION_SCHEMA)
|
2019-01-06 19:38:23 +01:00
|
|
|
def cover_open_to_code(config, action_id, arg_type, template_arg):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_open_action(template_arg)
|
|
|
|
type = OpenAction.template(arg_type)
|
|
|
|
yield Pvariable(action_id, rhs, type=type)
|
|
|
|
|
|
|
|
|
|
|
|
CONF_COVER_CLOSE = 'cover.close'
|
|
|
|
COVER_CLOSE_ACTION_SCHEMA = maybe_simple_id({
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
2018-10-16 23:16:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_COVER_CLOSE, COVER_CLOSE_ACTION_SCHEMA)
|
2019-01-06 19:38:23 +01:00
|
|
|
def cover_close_to_code(config, action_id, arg_type, template_arg):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_close_action(template_arg)
|
|
|
|
type = CloseAction.template(arg_type)
|
|
|
|
yield Pvariable(action_id, rhs, type=type)
|
|
|
|
|
|
|
|
|
|
|
|
CONF_COVER_STOP = 'cover.stop'
|
|
|
|
COVER_STOP_ACTION_SCHEMA = maybe_simple_id({
|
2018-11-12 23:30:31 +01:00
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
2018-10-16 23:16:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_COVER_STOP, COVER_STOP_ACTION_SCHEMA)
|
2019-01-06 19:38:23 +01:00
|
|
|
def cover_stop_to_code(config, action_id, arg_type, template_arg):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_stop_action(template_arg)
|
|
|
|
type = StopAction.template(arg_type)
|
|
|
|
yield Pvariable(action_id, rhs, type=type)
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def core_to_hass_config(data, config):
|
|
|
|
ret = mqtt.build_hass_config(data, 'cover', config, include_state=True, include_command=True)
|
|
|
|
if ret is None:
|
|
|
|
return None
|
|
|
|
return ret
|