def write_cpp(config): _LOGGER.info("Generating C++ source...") CORE.add_job(core_config.to_code, config[CONF_ESPHOME], domain='esphome') for domain in PRE_INITIALIZE: if domain == CONF_ESPHOME or domain not in config: continue CORE.add_job(get_component(domain).to_code, config[domain], domain=domain) for domain, component, conf in iter_components(config): if domain in PRE_INITIALIZE or not hasattr(component, 'to_code'): continue CORE.add_job(component.to_code, conf, domain=domain) CORE.flush_tasks() add(RawStatement('')) add(RawStatement('')) all_code = [] for exp in CORE.expressions: if not config[CONF_ESPHOME][CONF_USE_CUSTOM_CODE]: if isinstance(exp, Expression) and not exp.required: continue all_code.append(text_type(statement(exp))) writer.write_platformio_project() code_s = indent('\n'.join(line.rstrip() for line in all_code)) writer.write_cpp(code_s) return 0
def cpp_global_section(self): from esphome.cpp_generator import statement global_code = [] for exp in self.global_statements: text = str(statement(exp)) text = text.rstrip() global_code.append(text) return "\n".join(global_code) + "\n"
def cpp_main_section(self): from esphome.cpp_generator import statement main_code = [] for exp in self.main_statements: text = str(statement(exp)) text = text.rstrip() main_code.append(text) return '\n'.join(main_code) + '\n\n'
def add_global(self, expression): from esphome.cpp_generator import Expression, Statement, statement if isinstance(expression, Expression): expression = statement(expression) if not isinstance(expression, Statement): raise ValueError("Add '{}' must be expression or statement, not {}" "".format(expression, type(expression))) self.global_statements.append(expression) _LOGGER.debug("Adding global: %s", expression) return expression
def logger_log_action_to_code(config, action_id, template_arg, args): esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]] args_ = [RawExpression(text_type(x)) for x in config[CONF_ARGS]] text = text_type( statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args_))) for lambda_ in process_lambda(Lambda(text), args, return_type=void): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(action_id, rhs, type=type)
def add(self, expression): from esphome.cpp_generator import Expression, Statement, statement if isinstance(expression, Expression): expression = statement(expression) if not isinstance(expression, Statement): raise ValueError( f"Add '{expression}' must be expression or statement, not {type(expression)}" ) self.main_statements.append(expression) _LOGGER.debug("Adding: %s", expression) return expression