Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libretiny: split ota, mdns, disable i2c, spi, mqtt #13

Merged
merged 5 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ fi
mkdir -p "${pio_cache_base}"

bashio::log.info "Starting ESPHome dashboard..."
exec esphome dashboard /config/libretuya-esphome --socket /var/run/esphome.sock --ha-addon
exec esphome dashboard /config/esphome --socket /var/run/esphome.sock --ha-addon
37 changes: 20 additions & 17 deletions esphome/components/i2c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,26 @@ def _bus_declare_type(value):
)


CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): _bus_declare_type,
cv.Optional(CONF_SDA, default="SDA"): pin_with_input_and_output_support,
cv.SplitDefault(CONF_SDA_PULLUP_ENABLED, esp32_idf=True): cv.All(
cv.only_with_esp_idf, cv.boolean
),
cv.Optional(CONF_SCL, default="SCL"): pin_with_input_and_output_support,
cv.SplitDefault(CONF_SCL_PULLUP_ENABLED, esp32_idf=True): cv.All(
cv.only_with_esp_idf, cv.boolean
),
cv.Optional(CONF_FREQUENCY, default="50kHz"): cv.All(
cv.frequency, cv.Range(min=0, min_included=False)
),
cv.Optional(CONF_SCAN, default=True): cv.boolean,
}
).extend(cv.COMPONENT_SCHEMA)
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): _bus_declare_type,
cv.Optional(CONF_SDA, default="SDA"): pin_with_input_and_output_support,
cv.SplitDefault(CONF_SDA_PULLUP_ENABLED, esp32_idf=True): cv.All(
cv.only_with_esp_idf, cv.boolean
),
cv.Optional(CONF_SCL, default="SCL"): pin_with_input_and_output_support,
cv.SplitDefault(CONF_SCL_PULLUP_ENABLED, esp32_idf=True): cv.All(
cv.only_with_esp_idf, cv.boolean
),
cv.Optional(CONF_FREQUENCY, default="50kHz"): cv.All(
cv.frequency, cv.Range(min=0, min_included=False)
),
cv.Optional(CONF_SCAN, default=True): cv.boolean,
}
).extend(cv.COMPONENT_SCHEMA),
cv.only_on(["esp32", "esp8266", "rp2040"]),
)


@coroutine_with_priority(1.0)
Expand Down
43 changes: 43 additions & 0 deletions esphome/components/mdns/mdns_libretiny.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifdef USE_LIBRETINY

#include "esphome/components/network/ip_address.h"
#include "esphome/components/network/util.h"
#include "esphome/core/log.h"
#include "mdns_component.h"

#include <mDNS.h>

namespace esphome {
namespace mdns {

void MDNSComponent::setup() {
this->compile_records_();

MDNS.begin(this->hostname_.c_str());

for (const auto &service : this->services_) {
// Strip the leading underscore from the proto and service_type. While it is
// part of the wire protocol to have an underscore, and for example ESP-IDF
// expects the underscore to be there, the ESP8266 implementation always adds
// the underscore itself.
auto *proto = service.proto.c_str();
while (*proto == '_') {
proto++;
}
auto *service_type = service.service_type.c_str();
while (*service_type == '_') {
service_type++;
}
MDNS.addService(service_type, proto, service.port);
for (const auto &record : service.txt_records) {
MDNS.addServiceTxt(service_type, proto, record.key.c_str(), record.value.c_str());
}
}
}

void MDNSComponent::on_shutdown() {}

} // namespace mdns
} // namespace esphome

#endif
14 changes: 1 addition & 13 deletions esphome/components/mdns/mdns_rp2040.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#if defined(USE_RP2040) || defined(USE_LIBRETINY)
#ifdef USE_RP2040

#include "esphome/components/network/ip_address.h"
#include "esphome/components/network/util.h"
#include "esphome/core/log.h"
#include "mdns_component.h"

#ifndef USE_LIBRETINY
#include <ESP8266mDNS.h>
#else
#include <mDNS.h>
#endif

namespace esphome {
namespace mdns {

void MDNSComponent::setup() {
this->compile_records_();

#ifndef USE_LIBRETINY
network::IPAddress addr = network::get_ip_address();
MDNS.begin(this->hostname_.c_str(), (uint32_t) addr);
#else
MDNS.begin(this->hostname_.c_str());
#endif

for (const auto &service : this->services_) {
// Strip the leading underscore from the proto and service_type. While it is
Expand All @@ -44,15 +36,11 @@ void MDNSComponent::setup() {
}
}

#ifndef USE_LIBRETINY
void MDNSComponent::loop() { MDNS.update(); }
#endif

void MDNSComponent::on_shutdown() {
#ifndef USE_LIBRETINY
MDNS.close();
delay(40);
#endif
}

} // namespace mdns
Expand Down
12 changes: 4 additions & 8 deletions esphome/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def validate_fingerprint(value):
}
),
validate_config,
cv.only_on(["esp32", "esp8266", "bk72xx", "rtl87xx"]),
cv.only_on(["esp32", "esp8266"]),
)


Expand All @@ -271,14 +271,10 @@ def exp_mqtt_message(config):
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
# Add required libraries for ESP8266 and LibreTiny
if CORE.is_esp8266 or CORE.is_libretiny:
# Add required libraries for ESP8266
if CORE.is_esp8266:
# https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json
cg.add_library(
"AsyncMqttClient-esphome",
None,
"https://github.com/libretiny-eu/async-mqtt-client",
)
cg.add_library("ottowinter/AsyncMqttClient-esphome", "0.8.6")

cg.add_define("USE_MQTT")
cg.add_global(mqtt_ns.using)
Expand Down
2 changes: 1 addition & 1 deletion esphome/components/mqtt/mqtt_backend_esp8266.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
#ifdef USE_ESP8266

#include "mqtt_backend.h"
#include <AsyncMqttClient.h>
Expand Down
20 changes: 15 additions & 5 deletions esphome/components/mqtt/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void MQTTClientComponent::start_dnslookup_() {
this->dns_resolve_error_ = false;
this->dns_resolved_ = false;
ip_addr_t addr;
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
#ifdef USE_ESP32
err_t err = dns_gethostbyname_addrtype(this->credentials_.address.c_str(), &addr,
MQTTClientComponent::dns_found_callback, this, LWIP_DNS_ADDRTYPE_IPV4);
#endif
Expand All @@ -165,10 +165,15 @@ void MQTTClientComponent::start_dnslookup_() {
case ERR_OK: {
// Got IP immediately
this->dns_resolved_ = true;
#if LWIP_IPV4 && LWIP_IPV6
#ifdef USE_ESP32
#if LWIP_IPV6
this->ip_ = addr.u_addr.ip4.addr;
#else
this->ip_ = addr.addr;
#endif
#endif
#ifdef USE_ESP8266
this->ip_ = addr.addr;
#endif
this->start_connect_();
return;
Expand Down Expand Up @@ -220,10 +225,15 @@ void MQTTClientComponent::dns_found_callback(const char *name, const ip_addr_t *
if (ipaddr == nullptr) {
a_this->dns_resolve_error_ = true;
} else {
#if LWIP_IPV4 && LWIP_IPV6
#ifdef USE_ESP32
#if LWIP_IPV6
a_this->ip_ = ipaddr->u_addr.ip4.addr;
#else
a_this->ip_ = ipaddr->addr;
#endif
#endif // USE_ESP32
#ifdef USE_ESP8266
a_this->ip_ = ipaddr->addr;
#endif
a_this->dns_resolved_ = true;
}
Expand Down Expand Up @@ -546,7 +556,7 @@ static bool topic_match(const char *message, const char *subscription) {
}

void MQTTClientComponent::on_message(const std::string &topic, const std::string &payload) {
#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
#ifdef USE_ESP8266
// on ESP8266, this is called in lwIP/AsyncTCP task; some components do not like running
// from a different task.
this->defer([this, topic, payload]() {
Expand All @@ -555,7 +565,7 @@ void MQTTClientComponent::on_message(const std::string &topic, const std::string
if (topic_match(topic.c_str(), subscription.topic.c_str()))
subscription.callback(topic, payload);
}
#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
#ifdef USE_ESP8266
});
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions esphome/components/mqtt/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "esphome/components/network/ip_address.h"
#if defined(USE_ESP32)
#include "mqtt_backend_esp32.h"
#elif defined(USE_ESP8266) || defined(USE_LIBRETINY)
#elif defined(USE_ESP8266)
#include "mqtt_backend_esp8266.h"
#endif
#include "lwip/ip_addr.h"
Expand Down Expand Up @@ -298,7 +298,7 @@ class MQTTClientComponent : public Component {
std::vector<MQTTSubscription> subscriptions_;
#if defined(USE_ESP32)
MQTTBackendESP32 mqtt_backend_;
#elif defined(USE_ESP8266) || defined(USE_LIBRETINY)
#elif defined(USE_ESP8266)
MQTTBackendESP8266 mqtt_backend_;
#endif

Expand Down
10 changes: 3 additions & 7 deletions esphome/components/ota/ota_backend_arduino_esp32.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "esphome/core/defines.h"
#if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_LIBRETINY)
#ifdef USE_ESP32_FRAMEWORK_ARDUINO

#include "ota_backend_arduino_esp32.h"
#include "ota_component.h"
Expand All @@ -22,11 +22,7 @@ OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) {
return OTA_RESPONSE_ERROR_UNKNOWN;
}

void ArduinoESP32OTABackend::set_update_md5(const char *md5) {
#ifndef USE_LIBRETINY // not yet implemented
Update.setMD5(md5);
#endif
}
void ArduinoESP32OTABackend::set_update_md5(const char *md5) { Update.setMD5(md5); }

OTAResponseTypes ArduinoESP32OTABackend::write(uint8_t *data, size_t len) {
size_t written = Update.write(data, len);
Expand All @@ -47,4 +43,4 @@ void ArduinoESP32OTABackend::abort() { Update.abort(); }
} // namespace ota
} // namespace esphome

#endif // USE_ESP32_FRAMEWORK_ARDUINO || USE_LIBRETINY
#endif // USE_ESP32_FRAMEWORK_ARDUINO
4 changes: 2 additions & 2 deletions esphome/components/ota/ota_backend_arduino_esp32.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "esphome/core/defines.h"
#if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_LIBRETINY)
#ifdef USE_ESP32_FRAMEWORK_ARDUINO

#include "ota_component.h"
#include "ota_backend.h"
Expand All @@ -21,4 +21,4 @@ class ArduinoESP32OTABackend : public OTABackend {
} // namespace ota
} // namespace esphome

#endif // USE_ESP32_FRAMEWORK_ARDUINO || USE_LIBRETINY
#endif // USE_ESP32_FRAMEWORK_ARDUINO
48 changes: 48 additions & 0 deletions esphome/components/ota/ota_backend_arduino_libretiny.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "esphome/core/defines.h"
#ifdef USE_LIBRETINY

#include "ota_backend_arduino_libretiny.h"
#include "ota_component.h"
#include "ota_backend.h"

#include <Update.h>

namespace esphome {
namespace ota {

OTAResponseTypes ArduinoLibretinyOTABackend::begin(size_t image_size) {
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved
bool ret = Update.begin(image_size, U_FLASH);
if (ret) {
return OTA_RESPONSE_OK;
}

uint8_t error = Update.getError();
if (error == UPDATE_ERROR_SIZE)
return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE;
return OTA_RESPONSE_ERROR_UNKNOWN;
}

void ArduinoLibretinyOTABackend::set_update_md5(const char *md5) {
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved
// not yet implemented
}

OTAResponseTypes ArduinoLibretinyOTABackend::write(uint8_t *data, size_t len) {
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved
size_t written = Update.write(data, len);
if (written != len) {
return OTA_RESPONSE_ERROR_WRITING_FLASH;
}
return OTA_RESPONSE_OK;
}

OTAResponseTypes ArduinoLibretinyOTABackend::end() {
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved
if (!Update.end())
return OTA_RESPONSE_ERROR_UPDATE_END;
return OTA_RESPONSE_OK;
}

void ArduinoLibretinyOTABackend::abort() { Update.abort(); }
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved

} // namespace ota
} // namespace esphome

#endif // USE_LIBRETINY
24 changes: 24 additions & 0 deletions esphome/components/ota/ota_backend_arduino_libretiny.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_LIBRETINY

#include "ota_component.h"
#include "ota_backend.h"

namespace esphome {
namespace ota {

class ArduinoLibretinyOTABackend : public OTABackend {
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved
public:
OTAResponseTypes begin(size_t image_size) override;
void set_update_md5(const char *md5) override;
OTAResponseTypes write(uint8_t *data, size_t len) override;
OTAResponseTypes end() override;
void abort() override;
bool supports_compression() override { return false; }
};

} // namespace ota
} // namespace esphome

#endif // USE_LIBRETINY
6 changes: 5 additions & 1 deletion esphome/components/ota/ota_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ota_backend_arduino_esp32.h"
#include "ota_backend_arduino_esp8266.h"
#include "ota_backend_arduino_rp2040.h"
#include "ota_backend_arduino_libretiny.h"
#include "ota_backend_esp_idf.h"

#include "esphome/core/log.h"
Expand All @@ -29,7 +30,7 @@ std::unique_ptr<OTABackend> make_ota_backend() {
#ifdef USE_ESP8266
return make_unique<ArduinoESP8266OTABackend>();
#endif // USE_ESP8266
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
#ifdef USE_ESP32
return make_unique<ArduinoESP32OTABackend>();
#endif // USE_ESP32
#endif // USE_ARDUINO
Expand All @@ -39,6 +40,9 @@ std::unique_ptr<OTABackend> make_ota_backend() {
#ifdef USE_RP2040
return make_unique<ArduinoRP2040OTABackend>();
#endif // USE_RP2040
#ifdef USE_LIBRETINY
return make_unique<ArduinoLibretinyOTABackend>();
kuba2k2 marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

OTAComponent::OTAComponent() { global_ota_component = this; }
Expand Down
Loading