mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Cleanup dashboard JS (#491)
* Cleanup dashboard JS * Add vscode * Save start_mark/end_mark * Updates * Updates * Remove need for cv.nameable It's a bit hacky but removes so much bloat from integrations * Add enum helper * Document APIs, and Improvements * Fixes * Fixes * Update PULL_REQUEST_TEMPLATE.md * Updates * Updates * Updates
This commit is contained in:
		| @@ -1,9 +1,10 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.automation import ACTION_REGISTRY, maybe_simple_id, Condition | ||||
| from esphome import automation | ||||
| from esphome.automation import maybe_simple_id, Condition | ||||
| from esphome.components import mqtt | ||||
| from esphome.const import CONF_ID, CONF_INTERNAL, CONF_DEVICE_CLASS, CONF_STATE, \ | ||||
|     CONF_POSITION, CONF_TILT, CONF_STOP, CONF_MQTT_ID | ||||
|     CONF_POSITION, CONF_TILT, CONF_STOP, CONF_MQTT_ID, CONF_NAME | ||||
| from esphome.core import CORE, coroutine | ||||
|  | ||||
| IS_PLATFORM_COMPONENT = True | ||||
| @@ -24,7 +25,7 @@ COVER_STATES = { | ||||
|     'OPEN': COVER_OPEN, | ||||
|     'CLOSED': COVER_CLOSED, | ||||
| } | ||||
| validate_cover_state = cv.one_of(*COVER_STATES, upper=True) | ||||
| validate_cover_state = cv.enum(COVER_STATES, upper=True) | ||||
|  | ||||
| CoverOperation = cover_ns.enum('CoverOperation') | ||||
| COVER_OPERATIONS = { | ||||
| @@ -32,20 +33,20 @@ COVER_OPERATIONS = { | ||||
|     'OPENING': CoverOperation.COVER_OPERATION_OPENING, | ||||
|     'CLOSING': CoverOperation.COVER_OPERATION_CLOSING, | ||||
| } | ||||
| validate_cover_operation = cv.one_of(*COVER_OPERATIONS, upper=True) | ||||
| validate_cover_operation = cv.enum(COVER_OPERATIONS, upper=True) | ||||
|  | ||||
| # Actions | ||||
| OpenAction = cover_ns.class_('OpenAction', cg.Action) | ||||
| CloseAction = cover_ns.class_('CloseAction', cg.Action) | ||||
| StopAction = cover_ns.class_('StopAction', cg.Action) | ||||
| ControlAction = cover_ns.class_('ControlAction', cg.Action) | ||||
| CoverPublishAction = cover_ns.class_('CoverPublishAction', cg.Action) | ||||
| OpenAction = cover_ns.class_('OpenAction', automation.Action) | ||||
| CloseAction = cover_ns.class_('CloseAction', automation.Action) | ||||
| StopAction = cover_ns.class_('StopAction', automation.Action) | ||||
| ControlAction = cover_ns.class_('ControlAction', automation.Action) | ||||
| CoverPublishAction = cover_ns.class_('CoverPublishAction', automation.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.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_variable_id(mqtt.MQTTCoverComponent), | ||||
|     cv.GenerateID(): cv.declare_id(Cover), | ||||
|     cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_id(mqtt.MQTTCoverComponent), | ||||
|     cv.Optional(CONF_DEVICE_CLASS): cv.one_of(*DEVICE_CLASSES, lower=True), | ||||
|     # TODO: MQTT topic options | ||||
| }) | ||||
| @@ -53,6 +54,7 @@ COVER_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ | ||||
|  | ||||
| @coroutine | ||||
| def setup_cover_core_(var, config): | ||||
|     cg.add(var.set_name(config[CONF_NAME])) | ||||
|     if CONF_INTERNAL in config: | ||||
|         cg.add(var.set_internal(config[CONF_INTERNAL])) | ||||
|     if CONF_DEVICE_CLASS in config: | ||||
| @@ -72,63 +74,54 @@ def register_cover(var, config): | ||||
|  | ||||
|  | ||||
| COVER_ACTION_SCHEMA = maybe_simple_id({ | ||||
|     cv.Required(CONF_ID): cv.use_variable_id(Cover), | ||||
|     cv.Required(CONF_ID): cv.use_id(Cover), | ||||
| }) | ||||
|  | ||||
|  | ||||
| @ACTION_REGISTRY.register('cover.open', COVER_ACTION_SCHEMA) | ||||
| @automation.register_action('cover.open', OpenAction, COVER_ACTION_SCHEMA) | ||||
| def cover_open_to_code(config, action_id, template_arg, args): | ||||
|     var = yield cg.get_variable(config[CONF_ID]) | ||||
|     type = OpenAction.template(template_arg) | ||||
|     rhs = type.new(var) | ||||
|     yield cg.Pvariable(action_id, rhs, type=type) | ||||
|     paren = yield cg.get_variable(config[CONF_ID]) | ||||
|     yield cg.new_Pvariable(action_id, template_arg, paren) | ||||
|  | ||||
|  | ||||
| @ACTION_REGISTRY.register('cover.close', COVER_ACTION_SCHEMA) | ||||
| @automation.register_action('cover.close', CloseAction, COVER_ACTION_SCHEMA) | ||||
| def cover_close_to_code(config, action_id, template_arg, args): | ||||
|     var = yield cg.get_variable(config[CONF_ID]) | ||||
|     type = CloseAction.template(template_arg) | ||||
|     rhs = type.new(var) | ||||
|     yield cg.Pvariable(action_id, rhs, type=type) | ||||
|     paren = yield cg.get_variable(config[CONF_ID]) | ||||
|     yield cg.new_Pvariable(action_id, template_arg, paren) | ||||
|  | ||||
|  | ||||
| @ACTION_REGISTRY.register('cover.stop', COVER_ACTION_SCHEMA) | ||||
| @automation.register_action('cover.stop', StopAction, COVER_ACTION_SCHEMA) | ||||
| def cover_stop_to_code(config, action_id, template_arg, args): | ||||
|     var = yield cg.get_variable(config[CONF_ID]) | ||||
|     type = StopAction.template(template_arg) | ||||
|     rhs = type.new(var) | ||||
|     yield cg.Pvariable(action_id, rhs, type=type) | ||||
|     paren = yield cg.get_variable(config[CONF_ID]) | ||||
|     yield cg.new_Pvariable(action_id, template_arg, paren) | ||||
|  | ||||
|  | ||||
| COVER_CONTROL_ACTION_SCHEMA = cv.Schema({ | ||||
|     cv.Required(CONF_ID): cv.use_variable_id(Cover), | ||||
|     cv.Required(CONF_ID): cv.use_id(Cover), | ||||
|     cv.Optional(CONF_STOP): cv.templatable(cv.boolean), | ||||
|     cv.Exclusive(CONF_STATE, 'pos'): cv.templatable(cv.one_of(*COVER_STATES)), | ||||
|     cv.Exclusive(CONF_STATE, 'pos'): cv.templatable(validate_cover_state), | ||||
|     cv.Exclusive(CONF_POSITION, 'pos'): cv.templatable(cv.percentage), | ||||
|     cv.Optional(CONF_TILT): cv.templatable(cv.percentage), | ||||
| }) | ||||
|  | ||||
|  | ||||
| @ACTION_REGISTRY.register('cover.control', COVER_CONTROL_ACTION_SCHEMA) | ||||
| @automation.register_action('cover.control', ControlAction, COVER_CONTROL_ACTION_SCHEMA) | ||||
| def cover_control_to_code(config, action_id, template_arg, args): | ||||
|     var = yield cg.get_variable(config[CONF_ID]) | ||||
|     type = StopAction.template(template_arg) | ||||
|     rhs = type.new(var) | ||||
|     action = cg.Pvariable(action_id, rhs, type=type) | ||||
|     paren = yield cg.get_variable(config[CONF_ID]) | ||||
|     var = cg.new_Pvariable(action_id, template_arg, paren) | ||||
|     if CONF_STOP in config: | ||||
|         template_ = yield cg.templatable(config[CONF_STOP], args, bool) | ||||
|         cg.add(action.set_stop(template_)) | ||||
|         cg.add(var.set_stop(template_)) | ||||
|     if CONF_STATE in config: | ||||
|         template_ = yield cg.templatable(config[CONF_STATE], args, float, | ||||
|                                          to_exp=COVER_STATES) | ||||
|         cg.add(action.set_position(template_)) | ||||
|         template_ = yield cg.templatable(config[CONF_STATE], args, float) | ||||
|         cg.add(var.set_position(template_)) | ||||
|     if CONF_POSITION in config: | ||||
|         template_ = yield cg.templatable(config[CONF_POSITION], args, float) | ||||
|         cg.add(action.set_position(template_)) | ||||
|         cg.add(var.set_position(template_)) | ||||
|     if CONF_TILT in config: | ||||
|         template_ = yield cg.templatable(config[CONF_TILT], args, float) | ||||
|         cg.add(action.set_tilt(template_)) | ||||
|     yield action | ||||
|         cg.add(var.set_tilt(template_)) | ||||
|     yield var | ||||
|  | ||||
|  | ||||
| def to_code(config): | ||||
|   | ||||
| @@ -11,10 +11,7 @@ template<typename... Ts> class OpenAction : public Action<Ts...> { | ||||
|  public: | ||||
|   explicit OpenAction(Cover *cover) : cover_(cover) {} | ||||
|  | ||||
|   void play(Ts... x) override { | ||||
|     this->cover_->open(); | ||||
|     this->play_next(x...); | ||||
|   } | ||||
|   void play(Ts... x) override { this->cover_->open(); } | ||||
|  | ||||
|  protected: | ||||
|   Cover *cover_; | ||||
| @@ -24,10 +21,7 @@ template<typename... Ts> class CloseAction : public Action<Ts...> { | ||||
|  public: | ||||
|   explicit CloseAction(Cover *cover) : cover_(cover) {} | ||||
|  | ||||
|   void play(Ts... x) override { | ||||
|     this->cover_->close(); | ||||
|     this->play_next(x...); | ||||
|   } | ||||
|   void play(Ts... x) override { this->cover_->close(); } | ||||
|  | ||||
|  protected: | ||||
|   Cover *cover_; | ||||
| @@ -37,10 +31,7 @@ template<typename... Ts> class StopAction : public Action<Ts...> { | ||||
|  public: | ||||
|   explicit StopAction(Cover *cover) : cover_(cover) {} | ||||
|  | ||||
|   void play(Ts... x) override { | ||||
|     this->cover_->stop(); | ||||
|     this->play_next(x...); | ||||
|   } | ||||
|   void play(Ts... x) override { this->cover_->stop(); } | ||||
|  | ||||
|  protected: | ||||
|   Cover *cover_; | ||||
| @@ -59,7 +50,6 @@ template<typename... Ts> class ControlAction : public Action<Ts...> { | ||||
|     if (this->tilt_.has_value()) | ||||
|       call.set_tilt(this->tilt_.value(x...)); | ||||
|     call.perform(); | ||||
|     this->play_next(x...); | ||||
|   } | ||||
|  | ||||
|   TEMPLATABLE_VALUE(bool, stop) | ||||
| @@ -81,7 +71,6 @@ template<typename... Ts> class CoverPublishAction : public Action<Ts...> { | ||||
|     if (this->current_operation_.has_value()) | ||||
|       this->cover_->current_operation = this->current_operation_.value(x...); | ||||
|     this->cover_->publish_state(); | ||||
|     this->play_next(x...); | ||||
|   } | ||||
|  | ||||
|   TEMPLATABLE_VALUE(float, position) | ||||
|   | ||||
| @@ -105,8 +105,8 @@ const char *cover_operation_to_str(CoverOperation op); | ||||
|  */ | ||||
| class Cover : public Nameable { | ||||
|  public: | ||||
|   explicit Cover(const std::string &name); | ||||
|   explicit Cover(); | ||||
|   explicit Cover(const std::string &name); | ||||
|  | ||||
|   /// The current operation of the cover (idle, opening, closing). | ||||
|   CoverOperation current_operation{COVER_OPERATION_IDLE}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user