mirror of
https://github.com/randybb/esphome-configs.git
synced 2026-12-06 01:06:52 +01:00
nspanel-dev: LVGL UI on nspanel-terminal instead of nspanel_lovelace
The display runs the ptaru/nspanel-terminal TFT and ESPHome draws the UI with LVGL (component from a local checkout for now, see PR ptaru/nspanel-terminal#1). Pages: home (clock, weather, forecast, temperatures), lights, outside, irrigation, thermostat, gate, status/boot screen; light detail with brightness, colour temperature or hue on long press. Grid tiles come from packages in common/nspanel_terminal/ that add themselves to a page. Unavailable entities are greyed out, OTA progress shows on the status page, sleep dim and the notify/show_info actions match the other panels.
This commit is contained in:
1 parent
0e7dc6591f
commit
53c427e99c
17 files changed
+1789
-426
No files matched your search
@@ -0,0 +1,14 @@
|
||||
# Package: tile on a grid page that calls a Home Assistant action on an entity (e.g. button.press).
|
||||
# Its checked/disabled states are up to the owner (e.g. gate.yaml). Tiles fill the page in package order.
|
||||
# vars: page, id, name, icon (MDI glyph, must be in icon_s), action, entity
|
||||
lvgl:
|
||||
pages:
|
||||
- id: !extend ${page}
|
||||
widgets:
|
||||
- button:
|
||||
<<: !include {file: tile.yaml, vars: {id: "${id}", name: "${name}", icon: "${icon}"}}
|
||||
on_click:
|
||||
- homeassistant.action:
|
||||
action: ${action}
|
||||
data:
|
||||
entity_id: ${entity}
|
||||
@@ -0,0 +1,49 @@
|
||||
# One forecast row: day, icon, day/night temperature on two lines, all centred vertically.
|
||||
# Not clickable, so a tap falls through to the home page.
|
||||
obj:
|
||||
clickable: false
|
||||
width: SIZE_CONTENT
|
||||
height: SIZE_CONTENT
|
||||
bg_opa: TRANSP
|
||||
border_width: 0
|
||||
pad_all: 0
|
||||
layout:
|
||||
type: FLEX
|
||||
flex_flow: ROW
|
||||
flex_align_cross: CENTER
|
||||
pad_column: 10
|
||||
widgets:
|
||||
- label:
|
||||
id: forecast_day_${i}
|
||||
text: ""
|
||||
text_font: font_s
|
||||
text_color: 0xB4B4BA
|
||||
width: 26
|
||||
text_align: RIGHT
|
||||
- label:
|
||||
id: forecast_icon_${i}
|
||||
text: ""
|
||||
text_font: icon_s
|
||||
text_color: 0xFFFFFF
|
||||
- obj:
|
||||
clickable: false
|
||||
width: SIZE_CONTENT
|
||||
height: SIZE_CONTENT
|
||||
bg_opa: TRANSP
|
||||
border_width: 0
|
||||
pad_all: 0
|
||||
layout:
|
||||
type: FLEX
|
||||
flex_flow: COLUMN
|
||||
pad_row: 0
|
||||
widgets:
|
||||
- label:
|
||||
id: forecast_high_${i}
|
||||
text: ""
|
||||
text_font: font_s
|
||||
text_color: 0xFFFFFF
|
||||
- label:
|
||||
id: forecast_low_${i}
|
||||
text: ""
|
||||
text_font: font_s
|
||||
text_color: 0x8E8E93
|
||||
@@ -0,0 +1,55 @@
|
||||
# Package: gate state tile (two slots) on a grid page, and the state of the gate_open/gate_close action tiles
|
||||
# vars: page, state (select with Open/Closed/Opening/Closing)
|
||||
lvgl:
|
||||
pages:
|
||||
- id: !extend ${page}
|
||||
widgets:
|
||||
# highlighted while not closed
|
||||
- obj:
|
||||
id: gate_state_tile
|
||||
clickable: false
|
||||
width: 272
|
||||
height: 112
|
||||
styles: tile_style
|
||||
checked:
|
||||
bg_color: 0xF5B342
|
||||
text_color: 0x000000
|
||||
disabled:
|
||||
bg_color: 0x161618
|
||||
text_color: 0x48484A
|
||||
widgets:
|
||||
- label:
|
||||
id: gate_state_icon
|
||||
text: ""
|
||||
text_font: icon_l
|
||||
align: LEFT_MID
|
||||
- label:
|
||||
id: gate_state_label
|
||||
text: ""
|
||||
align: LEFT_MID
|
||||
x: 64
|
||||
|
||||
text_sensor:
|
||||
- platform: homeassistant
|
||||
id: gate_state
|
||||
entity_id: ${state}
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
static const char *const STATES[][3] = {{"Open", "Otvorená", "\U000F116A"},
|
||||
{"Closed", "Zatvorená", "\U000F0299"},
|
||||
{"Opening", "Otvára sa", "\U000F1169"},
|
||||
{"Closing", "Zatvára sa", "\U000F17F7"}};
|
||||
const char *text = x.c_str(), *icon = "\U000F17F8";
|
||||
for (auto &s : STATES)
|
||||
if (x == s[0]) text = s[1], icon = s[2];
|
||||
lv_label_set_text(id(gate_state_label), text);
|
||||
lv_label_set_text(id(gate_state_icon), icon);
|
||||
// highlighted unless closed
|
||||
lv_obj_set_state(id(gate_state_tile), LV_STATE_CHECKED, x != "Closed");
|
||||
// the button of the running movement
|
||||
lv_obj_set_state(id(gate_open), LV_STATE_CHECKED, x == "Opening");
|
||||
lv_obj_set_state(id(gate_close), LV_STATE_CHECKED, x == "Closing");
|
||||
// gate controller offline: grey out the whole gate page
|
||||
for (lv_obj_t *o : {(lv_obj_t *) id(gate_open), (lv_obj_t *) id(gate_close), id(gate_state_tile)})
|
||||
lv_obj_set_state(o, LV_STATE_DISABLED, ha_unavailable(x));
|
||||
@@ -0,0 +1,17 @@
|
||||
# Grid page (list item of lvgl pages): title and arrows; the 2x3 tiles come from the *_tile.yaml packages
|
||||
# vars: id, title, prev, next (page ids for the arrows), skip (hidden page: out of the page cycle, no next arrow)
|
||||
id: ${id}
|
||||
skip: ${skip}
|
||||
styles: page_style
|
||||
layout:
|
||||
type: FLEX
|
||||
flex_flow: ROW_WRAP
|
||||
widgets:
|
||||
- label:
|
||||
text: "${title}"
|
||||
text_font: font_s
|
||||
text_color: 0xB4B4BA
|
||||
width: 100%
|
||||
text_align: CENTER
|
||||
- <<: !include {file: page_arrow.yaml, vars: {icon: "\U000F0141", align: TOP_LEFT, x: -16, to: "${prev}", hidden: false}}
|
||||
- <<: !include {file: page_arrow.yaml, vars: {icon: "\U000F0142", align: TOP_RIGHT, x: 16, to: "${next}", hidden: "${skip}"}}
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
// Home Assistant entity state that means "no usable value": the tile is shown greyed out and disabled
|
||||
inline bool ha_unavailable(const std::string &s) { return s.empty() || s == "unavailable" || s == "unknown"; }
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
// HA brightness 0..255 -> slider percent; NaN (light off) -> 0
|
||||
inline int brightness_pct(float b) { return std::isnan(b) ? 0 : int(std::lround(b * 100 / 255)); }
|
||||
|
||||
// Hue from the hs_color attribute, "(30.0, 80.0)" or "[30.0, 80.0]"; -1 if missing
|
||||
inline int parse_hue(const std::string &s) {
|
||||
size_t p = s.find_first_of("([");
|
||||
if (p == std::string::npos) return -1;
|
||||
char *end;
|
||||
float h = std::strtof(s.c_str() + p + 1, &end);
|
||||
return end == s.c_str() + p + 1 ? -1 : int(std::lround(h));
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
# Package: Home Assistant light as a tile on a grid page, with its state. Tap toggles, long press opens page_light.
|
||||
# Tiles fill the page in package order.
|
||||
# vars: page, id, name, entity, icon (MDI glyph, must be in icon_s),
|
||||
# dim (brightness), kmin (lowest colour temperature in K, 0 = none), hs (hue)
|
||||
lvgl:
|
||||
pages:
|
||||
- id: !extend ${page}
|
||||
widgets:
|
||||
- button:
|
||||
<<: !include {file: tile.yaml, vars: {id: "${id}", name: "${name}", icon: "${icon}"}}
|
||||
# not checkable: the checked state comes only from Home Assistant
|
||||
on_short_click:
|
||||
- homeassistant.action:
|
||||
action: light.toggle
|
||||
data:
|
||||
entity_id: ${entity}
|
||||
on_long_press:
|
||||
- script.execute:
|
||||
id: open_light
|
||||
entity: ${entity}
|
||||
name: "${name}"
|
||||
dim: ${dim}
|
||||
kmin: ${kmin}
|
||||
hs: ${hs}
|
||||
is_on: !lambda return id(${id}_state).state == "on";
|
||||
bri: !lambda return id(${id}_bri).state;
|
||||
ct: !lambda return id(${id}_ct).state;
|
||||
hue: !lambda return id(${id}_hs).state;
|
||||
|
||||
sensor:
|
||||
- platform: homeassistant
|
||||
id: ${id}_bri
|
||||
entity_id: ${entity}
|
||||
attribute: brightness
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: 'if (id(cur_light) == "${entity}") lv_slider_set_value(id(detail_bri), brightness_pct(x), LV_ANIM_OFF);'
|
||||
- platform: homeassistant
|
||||
id: ${id}_ct
|
||||
entity_id: ${entity}
|
||||
attribute: color_temp_kelvin
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: 'if (id(cur_light) == "${entity}") lv_slider_set_value(id(detail_ct), x, LV_ANIM_OFF);'
|
||||
|
||||
text_sensor:
|
||||
# text, not binary: a binary sensor drops "unavailable"
|
||||
- platform: homeassistant
|
||||
id: ${id}_state
|
||||
entity_id: ${entity}
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
lv_obj_set_state(id(${id}), LV_STATE_CHECKED, x == "on");
|
||||
lv_obj_set_state(id(${id}), LV_STATE_DISABLED, ha_unavailable(x));
|
||||
if (id(cur_light) == "${entity}") lv_obj_set_state(id(detail_power), LV_STATE_CHECKED, x == "on");
|
||||
- platform: homeassistant
|
||||
id: ${id}_hs
|
||||
entity_id: ${entity}
|
||||
attribute: hs_color
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: 'if (id(cur_light) == "${entity}") lv_slider_set_value(id(detail_hue), parse_hue(x), LV_ANIM_OFF);'
|
||||
@@ -0,0 +1,19 @@
|
||||
# Package: tile on a grid page that opens another page, a big icon only (e.g. the robot for automations)
|
||||
# vars: page, to (page id), icon (MDI glyph, must be in icon_l)
|
||||
lvgl:
|
||||
pages:
|
||||
- id: !extend ${page}
|
||||
widgets:
|
||||
- button:
|
||||
width: 130
|
||||
height: 112
|
||||
styles: tile_style
|
||||
pressed:
|
||||
bg_color: 0x48484A
|
||||
on_click:
|
||||
- lvgl.page.show: ${to}
|
||||
widgets:
|
||||
- label:
|
||||
text: "${icon}"
|
||||
text_font: icon_l
|
||||
align: CENTER
|
||||
@@ -0,0 +1,21 @@
|
||||
# Page switch arrow in a top corner; floats over the page's flex layout
|
||||
# vars: icon, align, x, to (page id), hidden
|
||||
obj:
|
||||
floating: true
|
||||
hidden: ${hidden}
|
||||
align: ${align}
|
||||
x: ${x}
|
||||
y: -16
|
||||
width: 72
|
||||
height: 52
|
||||
bg_opa: TRANSP
|
||||
border_width: 0
|
||||
pad_all: 0
|
||||
on_click:
|
||||
- lvgl.page.show: ${to}
|
||||
widgets:
|
||||
- label:
|
||||
text: "${icon}"
|
||||
text_font: icon_s
|
||||
text_color: 0xB4B4BA
|
||||
align: CENTER
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
# nspanel-terminal version of common/nspanel_sleep_dim.yaml: the idle (dimmed) brightness drops to 0 at night
|
||||
# (sun below -6°), unless the room light is on. Uses the dimmed global, the panel display and ${dim_brightness}.
|
||||
# packages:
|
||||
# sleep_dim: !include
|
||||
# file: common/nspanel_terminal/sleep_dim.yaml
|
||||
# vars:
|
||||
# sleep_override_entity: light.bedroom_lights
|
||||
globals:
|
||||
# brightness the on_idle dim goes to
|
||||
- id: idle_brightness
|
||||
type: int
|
||||
initial_value: ${dim_brightness}
|
||||
|
||||
script:
|
||||
- id: apply_sleep_dim
|
||||
then:
|
||||
- lambda: |-
|
||||
bool day = id(sun_id).is_above_horizon(-6);
|
||||
id(idle_brightness) = id(sleep_override).state == "on" || day ? ${dim_brightness} : 0;
|
||||
if (id(dimmed))
|
||||
id(panel).request_brightness(id(idle_brightness));
|
||||
|
||||
time:
|
||||
- id: !extend ha_time
|
||||
on_time_sync:
|
||||
- script.execute: apply_sleep_dim
|
||||
|
||||
text_sensor:
|
||||
# sleepOverride; text, not binary: a binary sensor drops "unavailable"
|
||||
- platform: homeassistant
|
||||
id: sleep_override
|
||||
entity_id: ${sleep_override_entity}
|
||||
internal: true
|
||||
on_value:
|
||||
- script.execute: apply_sleep_dim
|
||||
|
||||
sun:
|
||||
id: sun_id
|
||||
time_id: ha_time
|
||||
latitude: 48.15°
|
||||
longitude: 17.11°
|
||||
on_sunrise:
|
||||
- elevation: -6°
|
||||
then:
|
||||
- script.execute: apply_sleep_dim
|
||||
on_sunset:
|
||||
- elevation: -6°
|
||||
then:
|
||||
- script.execute: apply_sleep_dim
|
||||
@@ -0,0 +1,12 @@
|
||||
# Package: empty slot on a grid page (e.g. to start the next row)
|
||||
# vars: page
|
||||
lvgl:
|
||||
pages:
|
||||
- id: !extend ${page}
|
||||
widgets:
|
||||
- obj:
|
||||
clickable: false
|
||||
width: 130
|
||||
height: 112
|
||||
bg_opa: TRANSP
|
||||
border_width: 0
|
||||
@@ -0,0 +1,25 @@
|
||||
# Package: on/off Home Assistant entity (switch, automation, ...) as a tile on a grid page, with its state.
|
||||
# Tiles fill the page in package order.
|
||||
# vars: page, id, name, entity, icon (MDI glyph, must be in icon_s), domain (tap calls <domain>.toggle)
|
||||
lvgl:
|
||||
pages:
|
||||
- id: !extend ${page}
|
||||
widgets:
|
||||
- button:
|
||||
<<: !include {file: tile.yaml, vars: {id: "${id}", name: "${name}", icon: "${icon}"}}
|
||||
on_click:
|
||||
- homeassistant.action:
|
||||
action: ${domain}.toggle
|
||||
data:
|
||||
entity_id: ${entity}
|
||||
|
||||
text_sensor:
|
||||
# text, not binary: a binary sensor drops "unavailable"
|
||||
- platform: homeassistant
|
||||
id: ${id}_state
|
||||
entity_id: ${entity}
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
lv_obj_set_state(id(${id}), LV_STATE_CHECKED, x == "on");
|
||||
lv_obj_set_state(id(${id}), LV_STATE_DISABLED, ha_unavailable(x));
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// HA list attribute as sent to ESPHome, "['none', 'away']" or '["none", "away"]' -> items
|
||||
inline std::vector<std::string> parse_list(const std::string &s) {
|
||||
std::vector<std::string> out;
|
||||
for (size_t p = 0; (p = s.find_first_of("'\"", p)) != std::string::npos;) {
|
||||
size_t end = s.find(s[p], p + 1);
|
||||
if (end == std::string::npos) break;
|
||||
out.push_back(s.substr(p + 1, end - p - 1));
|
||||
p = end + 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Display name for a climate preset; unknown presets are shown as they come
|
||||
inline std::string preset_name(const std::string &p) {
|
||||
static const char *const NAMES[][2] = {{"none", "Žiadny"}, {"home", "Doma"}, {"away", "Preč"},
|
||||
{"eco", "Eco"}, {"comfort", "Komfort"}, {"sleep", "Spánok"},
|
||||
{"activity", "Aktivita"}, {"boost", "Boost"}};
|
||||
for (auto &n : NAMES)
|
||||
if (p == n[0]) return n[1];
|
||||
return p;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
# Home Assistant state and target sending for the thermostat page (page_5).
|
||||
# Package vars: entity (climate entity with heat/off and the Better Thermostat presets), boiler (switch enabling heating)
|
||||
sensor:
|
||||
- platform: homeassistant
|
||||
id: thermo_target
|
||||
entity_id: ${entity}
|
||||
attribute: temperature
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
if (std::isnan(x)) return;
|
||||
lv_arc_set_value(id(thermo_arc), std::lround(x * 2));
|
||||
lv_label_set_text_fmt(id(thermo_target_label), "%.1f", x);
|
||||
- platform: homeassistant
|
||||
id: thermo_current
|
||||
entity_id: ${entity}
|
||||
attribute: current_temperature
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: 'lv_label_set_text_fmt(id(thermo_current_label), "teraz %.1f°C", x);'
|
||||
|
||||
text_sensor:
|
||||
# text, not binary: a binary sensor drops "unavailable"
|
||||
- platform: homeassistant
|
||||
id: heating_enabled
|
||||
entity_id: ${boiler}
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
lv_obj_set_state(id(heating_enabled_button), LV_STATE_CHECKED, x == "on");
|
||||
lv_obj_set_state(id(heating_enabled_button), LV_STATE_DISABLED, ha_unavailable(x));
|
||||
- platform: homeassistant
|
||||
id: thermo_mode
|
||||
entity_id: ${entity}
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
lv_obj_set_state(id(thermo_power), LV_STATE_CHECKED, x == "heat");
|
||||
// thermostat unavailable: grey out its controls
|
||||
for (lv_obj_t *o : {(lv_obj_t *) id(thermo_power), (lv_obj_t *) id(thermo_arc), id(thermo_minus), id(thermo_plus),
|
||||
id(thermo_preset_dropdown)->obj})
|
||||
lv_obj_set_state(o, LV_STATE_DISABLED, ha_unavailable(x));
|
||||
- script.execute: thermo_update_action
|
||||
- platform: homeassistant
|
||||
id: thermo_action
|
||||
entity_id: ${entity}
|
||||
attribute: hvac_action
|
||||
internal: true
|
||||
on_value:
|
||||
- script.execute: thermo_update_action
|
||||
- platform: homeassistant
|
||||
id: thermo_presets
|
||||
entity_id: ${entity}
|
||||
attribute: preset_modes
|
||||
internal: true
|
||||
on_value:
|
||||
- lambda: |-
|
||||
id(preset_list) = parse_list(x);
|
||||
std::string options;
|
||||
for (auto &p : id(preset_list)) options += (options.empty() ? "" : "\n") + preset_name(p);
|
||||
lv_dropdown_set_options(id(thermo_preset_dropdown)->obj, options.c_str());
|
||||
- script.execute: thermo_select_preset
|
||||
- platform: homeassistant
|
||||
id: thermo_preset
|
||||
entity_id: ${entity}
|
||||
attribute: preset_mode
|
||||
internal: true
|
||||
on_value:
|
||||
- script.execute: thermo_select_preset
|
||||
|
||||
script:
|
||||
# Arc and -/+ change the target locally; send once the user stops for a moment
|
||||
- id: thermo_send_target
|
||||
mode: restart
|
||||
then:
|
||||
- delay: 800ms
|
||||
- homeassistant.action:
|
||||
action: climate.set_temperature
|
||||
data:
|
||||
entity_id: ${entity}
|
||||
temperature: !lambda return lv_arc_get_value(id(thermo_arc)) / 2.0f;
|
||||
- id: thermo_update_action
|
||||
then:
|
||||
- lambda: |-
|
||||
bool off = id(thermo_mode).state == "off";
|
||||
bool heating = id(thermo_action).state == "heating";
|
||||
lv_label_set_text(id(thermo_action_label), off ? "vypnuté" : heating ? "kúri" : "nekúri");
|
||||
lv_obj_set_style_arc_color(id(thermo_arc), lv_color_hex(heating ? 0xF5B342 : 0x8E8E93), LV_PART_INDICATOR);
|
||||
- id: thermo_select_preset
|
||||
then:
|
||||
- lambda: |-
|
||||
auto &list = id(preset_list);
|
||||
auto it = std::find(list.begin(), list.end(), id(thermo_preset).state);
|
||||
if (it != list.end()) lv_dropdown_set_selected(id(thermo_preset_dropdown)->obj, it - list.begin());
|
||||
@@ -0,0 +1,25 @@
|
||||
# Look of a 2x3 grid tile (button body): icon top left, name bottom left; merge it into a button with <<:
|
||||
# checked = on (orange), disabled = entity unavailable (grey), pressed = touch feedback
|
||||
# vars: id, name, icon (MDI glyph, must be in icon_s)
|
||||
id: ${id}
|
||||
width: 130
|
||||
height: 112
|
||||
styles: tile_style
|
||||
pressed:
|
||||
bg_color: 0x48484A
|
||||
checked:
|
||||
bg_color: 0xF5B342
|
||||
text_color: 0x000000
|
||||
disabled:
|
||||
bg_color: 0x161618
|
||||
text_color: 0x48484A
|
||||
widgets:
|
||||
- label:
|
||||
text: "${icon}"
|
||||
text_font: icon_s
|
||||
align: TOP_LEFT
|
||||
- label:
|
||||
text: "${name}"
|
||||
align: BOTTOM_LEFT
|
||||
long_mode: WRAP
|
||||
width: 100%
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
// Home Assistant weather condition -> MDI icon (glyphs listed in the font: config)
|
||||
inline const char *weather_icon(const std::string &c) {
|
||||
static const char *const MAP[][2] = {
|
||||
{"clear-night", "\U000F0594"}, {"sunny", "\U000F0599"}, {"partlycloudy", "\U000F0595"},
|
||||
{"cloudy", "\U000F0590"}, {"fog", "\U000F0591"}, {"rainy", "\U000F0597"},
|
||||
{"pouring", "\U000F0596"}, {"snowy", "\U000F0598"}, {"snowy-rainy", "\U000F067F"},
|
||||
{"hail", "\U000F0592"}, {"lightning", "\U000F0593"}, {"lightning-rainy", "\U000F067E"},
|
||||
{"windy", "\U000F059D"}, {"windy-variant", "\U000F059E"}, {"exceptional", "\U000F05D6"}};
|
||||
for (auto &m : MAP)
|
||||
if (c == m[0]) return m[1];
|
||||
return "";
|
||||
}
|
||||
|
||||
// One day of the HA `forecast` attribute as sent to ESPHome (Python repr or JSON of a list of dicts).
|
||||
struct ForecastDay {
|
||||
std::string condition;
|
||||
float high, low;
|
||||
};
|
||||
|
||||
// Value after `key` in s, starting at pos; works for 'key': and "key":
|
||||
static inline const char *forecast_value_(const std::string &s, size_t pos, const char *key, size_t end) {
|
||||
size_t k = s.find(key, pos);
|
||||
if (k == std::string::npos || k >= end) return nullptr;
|
||||
k = s.find(':', k + std::strlen(key));
|
||||
if (k == std::string::npos || k >= end) return nullptr;
|
||||
while (++k < end && (s[k] == ' ' || s[k] == '\'' || s[k] == '"')) {
|
||||
}
|
||||
return s.c_str() + k;
|
||||
}
|
||||
|
||||
// Fills up to n days starting at index `first`; returns how many were parsed.
|
||||
inline int parse_forecast(const std::string &s, int first, ForecastDay *out, int n) {
|
||||
int index = 0, got = 0;
|
||||
size_t pos = 0;
|
||||
while (got < n && (pos = s.find('{', pos)) != std::string::npos) {
|
||||
size_t end = s.find('}', pos);
|
||||
if (end == std::string::npos) break;
|
||||
if (index++ >= first) {
|
||||
const char *cond = forecast_value_(s, pos, "condition", end);
|
||||
const char *high = forecast_value_(s, pos, "temperature", end);
|
||||
const char *low = forecast_value_(s, pos, "templow", end);
|
||||
if (cond && high && low) {
|
||||
out[got].condition.assign(cond, std::strcspn(cond, "'\","));
|
||||
out[got].high = std::strtof(high, nullptr);
|
||||
out[got].low = std::strtof(low, nullptr);
|
||||
++got;
|
||||
}
|
||||
}
|
||||
pos = end;
|
||||
}
|
||||
return got;
|
||||
}
|
||||
+1241
-426
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user