Skip to content

Commit 8fbc1c8

Browse files
committed
Support for ESPHome 2026.1.x (#59)
Fixes #59
1 parent 0b5c0dc commit 8fbc1c8

File tree

4 files changed

+128
-57
lines changed

4 files changed

+128
-57
lines changed

components/nspanel_lovelace/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ async def to_code(config):
630630
esp32.add_idf_sdkconfig_option("CONFIG_D0WD_PSRAM_CLK_IO", 5)
631631
esp32.add_idf_sdkconfig_option("CONFIG_D0WD_PSRAM_CS_IO", 18)
632632
# Also increase flash & CPU speed as NSPanel hardware supports it
633-
esp32.add_idf_sdkconfig_option("CONFIG_ESP32_DEFAULT_CPU_FREQ_240", True)
633+
esp32.add_idf_sdkconfig_option("CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240", True)
634634
esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_SPEED_80M", True)
635635
esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_MODE_QUAD", True)
636636
esp32.add_idf_sdkconfig_option("CONFIG_ESPTOOLPY_FLASHMODE_QIO", True)

components/nspanel_lovelace/entity.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool Entity::set_type(const char *type) {
7777
this->type_ = type;
7878

7979
if (this->enable_notifications_) {
80-
this->notify_type_change(type);
80+
this->notify_type_change(this->type_);
8181
}
8282
return true;
8383
}
@@ -91,7 +91,7 @@ void Entity::set_state(const std::string &state) {
9191
this->state_ = state;
9292

9393
if (this->enable_notifications_) {
94-
this->notify_state_change(state);
94+
this->notify_state_change(this->state_);
9595
}
9696
}
9797

components/nspanel_lovelace/nspanel_lovelace.cpp

Lines changed: 103 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,67 +2272,96 @@ void NSPanelLovelace::call_ha_service_(
22722272
const std::string &service,
22732273
const std::map<std::string, std::string> &data,
22742274
const std::map<std::string, std::string> &data_template) {
2275-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,10,0)
2276-
api::HomeassistantActionRequest resp;
2277-
#else
2278-
api::HomeassistantServiceResponse resp;
2279-
#endif
2280-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,8,0)
2281-
resp.set_service(esphome::StringRef(service));
2282-
#else
2283-
resp.service = service;
2284-
#endif
2275+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,10,0)
2276+
api::HomeassistantActionRequest resp;
2277+
#else
2278+
api::HomeassistantServiceResponse resp;
2279+
#endif
2280+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2281+
resp.service = esphome::StringRef(service);
2282+
#elif ESPHOME_VERSION_CODE >= VERSION_CODE(2025,8,0)
2283+
resp.set_service(esphome::StringRef(service));
2284+
#else
2285+
resp.service = service;
2286+
#endif
22852287

2286-
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
2287-
auto it = data.find(to_string(ha_attr_type::entity_id));
2288-
if (it == data.end())
2289-
ESP_LOGD(TAG, "Call HA: %s -> %s", service.c_str(), it->second.c_str());
2290-
else
2291-
ESP_LOGD(TAG, "Call HA: %s", service.c_str());
2292-
#endif
2288+
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
2289+
auto it = data.find(to_string(ha_attr_type::entity_id));
2290+
if (it == data.end())
2291+
ESP_LOGD(TAG, "Call HA: %s -> %s", service.c_str(), it->second.c_str());
2292+
else
2293+
ESP_LOGD(TAG, "Call HA: %s", service.c_str());
2294+
#endif
22932295

2294-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,11,0)
2295-
resp.data.init(data.size());
2296-
#endif
2296+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,11,0)
2297+
resp.data.init(data.size());
2298+
#endif
22972299
for (auto &it : data) {
22982300
api::HomeassistantServiceMap kv;
2299-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,8,0)
2300-
kv.set_key(esphome::StringRef(it.first));
2301-
#else
2302-
kv.key = it.first;
2303-
#endif
2301+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2302+
kv.key = esphome::StringRef(it.first);
2303+
#elif ESPHOME_VERSION_CODE >= VERSION_CODE(2025,8,0)
2304+
kv.set_key(esphome::StringRef(it.first));
2305+
#else
2306+
kv.key = it.first;
2307+
#endif
2308+
2309+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2310+
kv.value = esphome::StringRef(it.second);
2311+
#else
23042312
kv.value = it.second;
2313+
#endif
23052314
resp.data.push_back(kv);
23062315
}
23072316

2308-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,11,0)
2309-
resp.data_template.init(data_template.size());
2310-
#endif
2317+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,11,0)
2318+
resp.data_template.init(data_template.size());
2319+
#endif
23112320
for (auto &it : data_template) {
23122321
api::HomeassistantServiceMap kv;
2313-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,8,0)
2314-
kv.set_key(esphome::StringRef(it.first));
2315-
#else
2316-
kv.key = it.first;
2317-
#endif
2322+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2323+
kv.key = esphome::StringRef(it.first);
2324+
#elif ESPHOME_VERSION_CODE >= VERSION_CODE(2025,8,0)
2325+
kv.set_key(esphome::StringRef(it.first));
2326+
#else
2327+
kv.key = it.first;
2328+
#endif
2329+
2330+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2331+
kv.value = esphome::StringRef(it.second);
2332+
#else
23182333
kv.value = it.second;
2334+
#endif
23192335
resp.data_template.push_back(kv);
23202336
}
23212337

2322-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,10,0)
2338+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025,10,0)
23232339
api::global_api_server->send_homeassistant_action(resp);
2324-
#else
2340+
#else
23252341
api::global_api_server->send_homeassistant_service_call(resp);
2326-
#endif
2342+
#endif
23272343
}
23282344

2329-
void NSPanelLovelace::on_entity_state_update_(std::string entity_id, std::string state) {
2345+
void NSPanelLovelace::on_entity_state_update_(
2346+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2347+
const std::string &entity_id, esphome::StringRef state
2348+
#else
2349+
std::string entity_id, std::string state
2350+
#endif
2351+
) {
23302352
this->on_entity_attribute_update_(entity_id, to_string(ha_attr_type::state), state);
23312353
}
2332-
void NSPanelLovelace::on_entity_attribute_update_(std::string entity_id, std::string attr, std::string attr_value) {
2354+
2355+
void NSPanelLovelace::on_entity_attribute_update_(
2356+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2357+
const std::string &entity_id, const std::string &attr_name, esphome::StringRef attr_value
2358+
#else
2359+
std::string entity_id, std::string attr_name, std::string attr_value
2360+
#endif
2361+
) {
23332362
auto entity = this->get_entity_(entity_id);
23342363
if (entity == nullptr) return;
2335-
auto ha_attr = to_ha_attr(attr);
2364+
auto ha_attr = to_ha_attr(attr_name);
23362365
if (ha_attr == ha_attr_type::unknown) return;
23372366

23382367
if (ha_attr == ha_attr_type::state) {
@@ -2342,7 +2371,7 @@ void NSPanelLovelace::on_entity_attribute_update_(std::string entity_id, std::st
23422371
}
23432372

23442373
ESP_LOGD(TAG, "HA update: %s %s='%s'",
2345-
entity_id.c_str(), attr.c_str(),
2374+
entity_id.c_str(), attr_name.c_str(),
23462375
ha_attr == ha_attr_type::state
23472376
? entity->get_state().c_str()
23482377
: entity->get_attribute(ha_attr).c_str());
@@ -2352,7 +2381,7 @@ void NSPanelLovelace::on_entity_attribute_update_(std::string entity_id, std::st
23522381
// If there are lots of entity attributes that update within a short time
23532382
// then this will queue lots of commands unnecessarily.
23542383
// This re-schedules updates every time one happens within a 200ms period.
2355-
this->set_timeout(entity_id, 200, [this, entity_id] () {
2384+
this->set_timeout(entity->get_entity_id().c_str(), 200, [this, entity_id] () {
23562385
if (this->force_current_page_update_) return;
23572386
auto page = this->page_mgr_.current_page();
23582387
if (!page) return;
@@ -2412,32 +2441,56 @@ void NSPanelLovelace::send_weather_update_command_() {
24122441
this->send_buffered_command_();
24132442
}
24142443

2415-
void NSPanelLovelace::on_weather_state_update_(std::string entity_id, std::string state) {
2444+
void NSPanelLovelace::on_weather_state_update_(
2445+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2446+
const std::string &entity_id, esphome::StringRef state
2447+
#else
2448+
std::string entity_id, std::string state
2449+
#endif
2450+
) {
24162451
if (this->screensaver_ == nullptr) return;
24172452
auto item = this->screensaver_->get_item<WeatherItem>(0);
24182453
if (item == nullptr) return;
24192454
item->set_icon_by_weather_condition(state);
24202455
this->send_weather_update_command_();
24212456
}
24222457

2423-
void NSPanelLovelace::on_weather_temperature_update_(std::string entity_id, std::string temperature) {
2458+
void NSPanelLovelace::on_weather_temperature_update_(
2459+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2460+
const std::string &entity_id, esphome::StringRef temperature
2461+
#else
2462+
std::string entity_id, std::string temperature
2463+
#endif
2464+
) {
24242465
if (this->screensaver_ == nullptr) return;
24252466
auto item = this->screensaver_->get_item<WeatherItem>(0);
24262467
if (item == nullptr) return;
2427-
item->set_value(std::move(temperature));
2468+
item->set_value(temperature);
24282469
this->send_weather_update_command_();
24292470
}
24302471

2431-
void NSPanelLovelace::on_weather_temperature_unit_update_(std::string entity_id, std::string temperature_unit) {
2472+
void NSPanelLovelace::on_weather_temperature_unit_update_(
2473+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2474+
const std::string &entity_id, esphome::StringRef temperature_unit
2475+
#else
2476+
std::string entity_id, std::string temperature_unit
2477+
#endif
2478+
) {
24322479
if (this->screensaver_ == nullptr) return;
2433-
WeatherItem::temperature_unit = std::move(temperature_unit);
2480+
WeatherItem::temperature_unit = temperature_unit;
24342481
this->screensaver_->set_items_render_invalid();
24352482
this->send_weather_update_command_();
24362483
}
24372484

2438-
void NSPanelLovelace::on_weather_forecast_update_(std::string entity_id, std::string forecast_json) {
2485+
void NSPanelLovelace::on_weather_forecast_update_(
2486+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
2487+
const std::string &entity_id, esphome::StringRef forecast_json
2488+
#else
2489+
std::string entity_id, std::string forecast_json
2490+
#endif
2491+
) {
24392492
ESP_LOGV(TAG, "Weather forecast update (%u): %zu %s",
2440-
this->screensaver_ == nullptr, forecast_json.length(), forecast_json.c_str());
2493+
this->screensaver_ == nullptr, forecast_json.size(), forecast_json.c_str());
24412494
if (this->screensaver_ == nullptr) return;
24422495
// todo: check if we are on the screensaver otherwise don't update
24432496
// todo: implement color updates: "color~background~tTime~timeAMPM~tDate~tMainText~tForecast1~tForecast2~tForecast3~tForecast4~tForecast1Val~tForecast2Val~tForecast3Val~tForecast4Val~bar~tMainTextAlt2~tTimeAdd"
@@ -2465,12 +2518,12 @@ void NSPanelLovelace::on_weather_forecast_update_(std::string entity_id, std::st
24652518
BasicJsonDocument<SpiRamAllocator> doc(psram_available() ? 7680 : 6144);
24662519
#endif
24672520
ArduinoJson::DeserializationError error = ArduinoJson::deserializeJson(
2468-
doc, forecast_json, DeserializationOption::Filter(filter));
2521+
doc, forecast_json.c_str(), DeserializationOption::Filter(filter));
24692522
App.feed_wdt();
24702523

24712524
if (error || doc.overflowed()) {
24722525
ESP_LOGW(TAG, "Weather unparsable: %s %zu '%s'", error ? error.c_str() : "doc overflow",
2473-
forecast_json.length(), forecast_json.c_str());
2526+
forecast_json.size(), forecast_json.c_str());
24742527
return;
24752528
}
24762529

components/nspanel_lovelace/nspanel_lovelace.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "esphome/core/automation.h"
1515
#include "esphome/core/component.h"
1616
#include "esphome/core/preferences.h"
17+
#include "esphome/core/string_ref.h"
18+
#include "esphome/core/version.h"
1719
#include "esphome/components/uart/uart.h"
1820
#include "esphome/components/api/custom_api_device.h"
1921

@@ -186,11 +188,16 @@ class NSPanelLovelace : public Component, public uart::UARTDevice, protected api
186188
void send_nextion_command_(const std::string &command);
187189

188190
void subscribe_homeassistant_state_attr(
189-
void (NSPanelLovelace::*callback)(std::string, std::string, std::string),
190-
std::string entity_id, std::string attribute) {
191-
auto f = std::bind(callback, this, entity_id, attribute, std::placeholders::_1);
191+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
192+
void (NSPanelLovelace::*callback)(const std::string &, const std::string &, esphome::StringRef),
193+
#else
194+
void (NSPanelLovelace::*callback)(std::string, std::string, std::string),
195+
#endif
196+
const std::string &entity_id, const std::string &attr_name
197+
) {
198+
auto f = std::bind(callback, this, entity_id, attr_name, std::placeholders::_1);
192199
api::global_api_server->
193-
subscribe_home_assistant_state(entity_id, optional<std::string>(attribute), f);
200+
subscribe_home_assistant_state(entity_id, optional<std::string>(attr_name), std::move(f));
194201
}
195202

196203
bool process_data_();
@@ -251,6 +258,16 @@ class NSPanelLovelace : public Component, public uart::UARTDevice, protected api
251258
const std::string& service,
252259
const std::map<std::string, std::string> &data,
253260
const std::map<std::string, std::string> &data_template = {});
261+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026,1,0)
262+
void on_entity_state_update_(const std::string &entity_id, esphome::StringRef state);
263+
void on_entity_attribute_update_(
264+
const std::string &entity_id, const std::string &attr_name, esphome::StringRef attr_value);
265+
266+
void on_weather_state_update_(const std::string &entity_id, esphome::StringRef state);
267+
void on_weather_temperature_update_(const std::string &entity_id, esphome::StringRef temperature);
268+
void on_weather_temperature_unit_update_(const std::string &entity_id, esphome::StringRef temperature_unit);
269+
void on_weather_forecast_update_(const std::string &entity_id, esphome::StringRef forecast_json);
270+
#else
254271
void on_entity_state_update_(std::string entity_id, std::string state);
255272
void on_entity_attribute_update_(
256273
std::string entity_id, std::string attr_name, std::string attr_value);
@@ -259,6 +276,7 @@ class NSPanelLovelace : public Component, public uart::UARTDevice, protected api
259276
void on_weather_temperature_update_(std::string entity_id, std::string temperature);
260277
void on_weather_temperature_unit_update_(std::string entity_id, std::string temperature_unit);
261278
void on_weather_forecast_update_(std::string entity_id, std::string forecast_json);
279+
#endif
262280
void send_weather_update_command_();
263281
std::string weather_entity_id_;
264282
std::string language_;

0 commit comments

Comments
 (0)