host platform: improvements and bugfixes (#6137)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Clyde Stubbs 2024-02-07 12:24:06 +11:00 committed by GitHub
parent 0ede4a3095
commit f3ef05f5c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 7 deletions

View file

@ -4,6 +4,7 @@ from esphome.const import (
KEY_TARGET_FRAMEWORK, KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM, KEY_TARGET_PLATFORM,
PLATFORM_HOST, PLATFORM_HOST,
CONF_MAC_ADDRESS,
) )
from esphome.core import CORE from esphome.core import CORE
from esphome.helpers import IS_MACOS from esphome.helpers import IS_MACOS
@ -28,13 +29,18 @@ def set_core_data(config):
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema({}), cv.Schema(
{
cv.Optional(CONF_MAC_ADDRESS, default="98:35:69:ab:f6:79"): cv.mac_address,
}
),
set_core_data, set_core_data,
) )
async def to_code(config): async def to_code(config):
cg.add_build_flag("-DUSE_HOST") cg.add_build_flag("-DUSE_HOST")
cg.add_define("USE_ESPHOME_HOST_MAC_ADDRESS", config[CONF_MAC_ADDRESS].parts)
cg.add_build_flag("-std=c++17") cg.add_build_flag("-std=c++17")
cg.add_build_flag("-lsodium") cg.add_build_flag("-lsodium")
if IS_MACOS: if IS_MACOS:

View file

@ -212,6 +212,14 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) {
return; return;
#endif #endif
#ifdef USE_HOST #ifdef USE_HOST
time_t rawtime;
struct tm *timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof buffer, "[%H:%M:%S]", timeinfo);
fputs(buffer, stdout);
puts(msg); puts(msg);
#endif #endif

View file

@ -25,6 +25,7 @@ namespace sntp {
static const char *const TAG = "sntp"; static const char *const TAG = "sntp";
void SNTPComponent::setup() { void SNTPComponent::setup() {
#ifndef USE_HOST
ESP_LOGCONFIG(TAG, "Setting up SNTP..."); ESP_LOGCONFIG(TAG, "Setting up SNTP...");
#if defined(USE_ESP32) || defined(USE_LIBRETINY) #if defined(USE_ESP32) || defined(USE_LIBRETINY)
if (sntp_enabled()) { if (sntp_enabled()) {
@ -48,6 +49,7 @@ void SNTPComponent::setup() {
#endif #endif
sntp_init(); sntp_init();
#endif
} }
void SNTPComponent::dump_config() { void SNTPComponent::dump_config() {
ESP_LOGCONFIG(TAG, "SNTP Time:"); ESP_LOGCONFIG(TAG, "SNTP Time:");
@ -57,7 +59,7 @@ void SNTPComponent::dump_config() {
ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str()); ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str());
} }
void SNTPComponent::update() { void SNTPComponent::update() {
#ifndef USE_ESP_IDF #if !defined(USE_ESP_IDF) && !defined(USE_HOST)
// force resync // force resync
if (sntp_enabled()) { if (sntp_enabled()) {
sntp_stop(); sntp_stop();

View file

@ -28,7 +28,7 @@ class Filter {
* @param value The new value. * @param value The new value.
* @return An optional string, the new value that should be pushed out. * @return An optional string, the new value that should be pushed out.
*/ */
virtual optional<std::string> new_value(std::string value); virtual optional<std::string> new_value(std::string value) = 0;
/// Initialize this filter, please note this can be called more than once. /// Initialize this filter, please note this can be called more than once.
virtual void initialize(TextSensor *parent, Filter *next); virtual void initialize(TextSensor *parent, Filter *next);

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <string>
#include <functional>
#include <cmath> #include <cmath>
#include <cstdint>
#include <functional>
#include <string>
#include "esphome/core/optional.h" #include "esphome/core/optional.h"

View file

@ -11,6 +11,12 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#ifdef USE_HOST
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif
#if defined(USE_ESP8266) #if defined(USE_ESP8266)
#include <osapi.h> #include <osapi.h>
#include <user_interface.h> #include <user_interface.h>
@ -415,7 +421,7 @@ std::string value_accuracy_to_string(float value, int8_t accuracy_decimals) {
int8_t step_to_accuracy_decimals(float step) { int8_t step_to_accuracy_decimals(float step) {
// use printf %g to find number of digits based on temperature step // use printf %g to find number of digits based on temperature step
char buf[32]; char buf[32];
sprintf(buf, "%.5g", step); snprintf(buf, sizeof buf, "%.5g", step);
std::string str{buf}; std::string str{buf};
size_t dot_pos = str.find('.'); size_t dot_pos = str.find('.');
@ -551,7 +557,10 @@ void HighFrequencyLoopRequester::stop() {
bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; } bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; }
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter) void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
#if defined(USE_ESP32) #if defined(USE_HOST)
static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS;
memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address));
#elif defined(USE_ESP32)
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) || defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC) #if defined(CONFIG_SOC_IEEE802154_SUPPORTED) || defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC)
// When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default // When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default
// returns the 802.15.4 EUI-64 address. Read directly from eFuse instead. // returns the 802.15.4 EUI-64 address. Read directly from eFuse instead.
@ -569,6 +578,8 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
WiFi.macAddress(mac); WiFi.macAddress(mac);
#elif defined(USE_LIBRETINY) #elif defined(USE_LIBRETINY)
WiFi.macAddress(mac); WiFi.macAddress(mac);
#else
// this should be an error, but that messes with CI checks. #error No mac address method defined
#endif #endif
} }
std::string get_mac_address() { std::string get_mac_address() {

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <string> #include <string>