2021-02-18 12:26:59 +13:00
|
|
|
from esphome.cpp_generator import RawExpression
|
2019-04-17 12:06:00 +02:00
|
|
|
import esphome.codegen as cg
|
2019-04-22 21:56:30 +02:00
|
|
|
import esphome.config_validation as cv
|
2020-12-01 17:41:39 -05:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_ID, CONF_NUM_ATTEMPTS, CONF_PASSWORD,
|
|
|
|
CONF_PORT, CONF_REBOOT_TIMEOUT, CONF_SAFE_MODE
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome.core import CORE, coroutine_with_priority
|
|
|
|
|
2020-07-25 15:57:18 +02:00
|
|
|
CODEOWNERS = ['@esphome/core']
|
2019-04-22 21:56:30 +02:00
|
|
|
DEPENDENCIES = ['network']
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
ota_ns = cg.esphome_ns.namespace('ota')
|
|
|
|
OTAComponent = ota_ns.class_('OTAComponent', cg.Component)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = cv.Schema({
|
2019-04-22 21:56:30 +02:00
|
|
|
cv.GenerateID(): cv.declare_id(OTAComponent),
|
2019-04-17 12:06:00 +02:00
|
|
|
cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean,
|
|
|
|
cv.SplitDefault(CONF_PORT, esp8266=8266, esp32=3232): cv.port,
|
|
|
|
cv.Optional(CONF_PASSWORD, default=''): cv.string,
|
2020-12-01 17:41:39 -05:00
|
|
|
cv.Optional(CONF_REBOOT_TIMEOUT, default='5min'): cv.positive_time_period_milliseconds,
|
|
|
|
cv.Optional(CONF_NUM_ATTEMPTS, default='10'): cv.positive_not_null_int
|
2019-04-17 12:06:00 +02:00
|
|
|
}).extend(cv.COMPONENT_SCHEMA)
|
|
|
|
|
|
|
|
|
|
|
|
@coroutine_with_priority(50.0)
|
|
|
|
def to_code(config):
|
2019-04-22 21:56:30 +02:00
|
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
|
|
|
cg.add(var.set_port(config[CONF_PORT]))
|
|
|
|
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
|
|
|
|
|
2019-06-09 17:03:51 +02:00
|
|
|
yield cg.register_component(var, config)
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
if config[CONF_SAFE_MODE]:
|
2021-02-18 12:26:59 +13:00
|
|
|
condition = var.should_enter_safe_mode(config[CONF_NUM_ATTEMPTS],
|
|
|
|
config[CONF_REBOOT_TIMEOUT])
|
|
|
|
cg.add(RawExpression(f"if ({condition}) return"))
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
if CORE.is_esp8266:
|
|
|
|
cg.add_library('Update', None)
|
|
|
|
elif CORE.is_esp32:
|
|
|
|
cg.add_library('Hash', None)
|