Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/ev-charge-now-slider-top-step.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftw": patch
---

The Charge now slider in the EV charger dialog now stops at the most FTW can order. It took its top from the charger's maximum, so a 32 A one-phase charger whose levels stop at 3.7 kW read 6–32 A, "32 A · 7.4 kW" and "FTW received 32 A" while FTW charged at 16 A. When a charger has a list of levels, the top is now the highest level at or below its maximum: 16 A · 3.7 kW for that charger.
30 changes: 25 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3556,7 +3556,7 @@
}

// buildManualChargeSection renders the Tesla-style manual override: an
// amp slider (range = the charger's min/max charge current) plus Start /
// amp slider (range = the charger's minimum up to evChargeNowTopW) plus Start /
// Stop. Start pins a persistent manual hold at the slider's amps,
// which overrides surplus_only and the plan (the fuse clamp still
// applies); Stop clears the hold and drops back to whatever mode the
Expand All @@ -3568,6 +3568,20 @@
lp.manual && (lp.manual.requested_w === 0 || lp.manual.state === "paused" || lp.manual.state === "pausing")));
}

// evChargeNowTopW is the most a hold can order. Core treats the hold as a
// ceiling and floors it to the step table within max_charge_w, so a
// one-phase charger rated 7,360 W with a table that stops at 3,680 W
// charges at 16 A, not 32.
function evChargeNowTopW(lp) {
var maxW = (lp && lp.max_charge_w) || 0;
var steps = lp && Array.isArray(lp.allowed_steps_w) ? lp.allowed_steps_w : [];
var topW = 0;
steps.forEach(function (w) {
if (w > topW && (maxW <= 0 || w <= maxW)) topW = w;
});
return topW || maxW;
}

function buildManualChargeSection(lp) {
var phases = (lp && lp.phases) || 3;
var voltage = (lp && lp.voltage_v) || 230;
Expand All @@ -3576,8 +3590,14 @@
function aToW(a) { return Math.round(a * perA); }

var minA = Math.max(1, Math.round(wToA((lp && lp.min_charge_w) || 0)) || 6);
var maxA = Math.round(wToA((lp && lp.max_charge_w) || 0)) || 16;
var topW = evChargeNowTopW(lp);
var maxA = Math.round(wToA(topW)) || 16;
if (maxA <= minA) { maxA = minA + 1; }
// orderW is the hold a slider position sends. The top sends the top step
// itself: a step off whole amps rounds down, and Core floors the rounded
// watts to the step below (10,400 W at 3 × 230 V reads 15 A, and
// 15 A = 10,350 W orders 9,700 W).
function orderW(a) { return a >= maxA && topW > 0 ? topW : aToW(a); }

var active = !!(lp && lp.manual_active);
var curA = active ? Math.round(wToA((lp && lp.manual_charge_w) || 0)) : maxA;
Expand Down Expand Up @@ -3631,7 +3651,7 @@
readout.style.color = "var(--fg)";
function renderReadout() {
var a = parseInt(slider.value, 10) || minA;
readout.textContent = a + " A · " + (aToW(a) / 1000).toFixed(1) + " kW";
readout.textContent = a + " A · " + (orderW(a) / 1000).toFixed(1) + " kW";
}
renderReadout();
var currentEditingUntil = 0;
Expand All @@ -3651,7 +3671,7 @@
status.style.minHeight = "1em";
status.setAttribute("role", "status");
status.setAttribute("aria-live", "polite");
var idleText = "Starts at up to " + maxA + " A · " + (aToW(maxA) / 1000).toFixed(1) + " kW. Ignores the goal and solar rule until you return to the plan or unplug.";
var idleText = "Starts at up to " + maxA + " A · " + (orderW(maxA) / 1000).toFixed(1) + " kW. Ignores the goal and solar rule until you return to the plan or unplug.";
status.textContent = active ? "Changes apply when you release the slider." : idleText;
box.appendChild(status);

Expand Down Expand Up @@ -3763,7 +3783,7 @@
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
power_w: pause ? 0 : aToW(a),
power_w: pause ? 0 : orderW(a),
hold_s: 0,
phase_mode: phases === 1 ? "1p" : "3p",
}),
Expand Down
123 changes: 123 additions & 0 deletions web/ev-charge-now-range.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import test from 'node:test';

const source = readFileSync(new URL('./app.js', import.meta.url), 'utf8');
const section = source.slice(
source.indexOf('function evIsPaused'),
source.indexOf('function sliderHeader'),
);

// Core treats a Charge now hold as a ceiling and floors it to the charger's
// step table within max_charge_w. The slider must stop where Core does: a
// one-phase charger rated 32 A with a 16 A table showed 6–32 A and
// "32 A · 7.4 kW" while Core ordered 3,680 W (22 Oct 2026 rig).

class FakeEl {
constructor(tag) {
this.tagName = tag.toUpperCase();
this.style = {};
this.children = [];
this.attributes = {};
this.listeners = {};
this.textContent = '';
}
setAttribute(k, v) { this.attributes[k] = String(v); }
appendChild(c) { this.children.push(c); return c; }
addEventListener(type, fn) { (this.listeners[type] ||= []).push(fn); }
cloneNode() { const c = new FakeEl(this.tagName); Object.assign(c.style, this.style); return c; }
fire(type) { for (const fn of this.listeners[type] || []) fn({ type }); }
}

function walk(el, out = []) {
out.push(el);
for (const c of el.children) walk(c, out);
return out;
}

function build(lp) {
const sent = [];
const evWrite = (path, opts) => { sent.push(JSON.parse(opts.body)); return new Promise(() => {}); };
const make = new Function('document', 'evWrite', 'refreshEvModalAfterWrite', section + '; return buildManualChargeSection;');
const ctl = make({ createElement: tag => new FakeEl(tag) }, evWrite, () => {})(lp);
const all = walk(ctl.el);
const slider = all.find(el => el.tagName === 'INPUT' && el.type === 'range');
const row = all.find(el => el.children.includes(slider));
return {
slider,
readout: row.children[1],
status: all.find(el => el.tagName === 'SMALL'),
start: all.find(el => el.tagName === 'BUTTON' && el.textContent === 'Charge now'),
sent,
};
}

const onePhase = {
id: 'garage', phases: 1, voltage_v: 230, plugged_in: true,
min_charge_w: 1380, max_charge_w: 7360, allowed_steps_w: [0, 1380, 2300, 3680],
};

test('a one-phase charger tops out at the top of its step table', () => {
const ui = build(onePhase);
assert.equal(ui.slider.min, '6');
assert.equal(ui.slider.max, '16');
assert.equal(ui.readout.textContent, '16 A · 3.7 kW');
assert.match(ui.status.textContent, /^Starts at up to 16 A · 3\.7 kW\./);
ui.start.fire('click');
assert.deepEqual(ui.sent, [{ power_w: 3680, hold_s: 0, phase_mode: '1p' }]);
});

test('a hold above the table reads as the top Core can order', () => {
const ui = build({ ...onePhase, manual_active: true, manual_charge_w: 7360 });
assert.equal(ui.slider.value, '16');
assert.equal(ui.readout.textContent, '16 A · 3.7 kW');
});

test('the top is the highest step within max_charge_w', () => {
const ui = build({
id: 'garage', phases: 3, voltage_v: 230, plugged_in: true,
min_charge_w: 4140, max_charge_w: 7360, allowed_steps_w: [0, 4140, 6900, 11040],
});
assert.equal(ui.slider.max, '10');
assert.equal(ui.readout.textContent, '10 A · 6.9 kW');
});

test('without a table the top is max_charge_w, as before', () => {
const ui = build({ id: 'garage', phases: 3, voltage_v: 230, min_charge_w: 4140, max_charge_w: 11040 });
assert.equal(ui.slider.min, '6');
assert.equal(ui.slider.max, '16');
assert.equal(ui.readout.textContent, '16 A · 11.0 kW');
});

// A top step off whole amps: 10,400 W at 3 × 230 V is 15.07 A. Sending
// 15 A = 10,350 W made Core floor the hold to 9,700 W, so the top step
// could not be ordered (Codex, #1808).
const offWholeAmps = {
id: 'garage', phases: 3, voltage_v: 230, plugged_in: true,
min_charge_w: 4140, max_charge_w: 11000, allowed_steps_w: [0, 4140, 6900, 9700, 10400],
};

test('the top orders a step that is not on whole amps', () => {
const ui = build(offWholeAmps);
assert.equal(ui.slider.max, '15');
assert.equal(ui.readout.textContent, '15 A · 10.4 kW');
assert.match(ui.status.textContent, /^Starts at up to 15 A · 10\.4 kW\./);
ui.start.fire('click');
assert.deepEqual(ui.sent, [{ power_w: 10400, hold_s: 0, phase_mode: '3p' }]);
});

test('below the top the slider still sends its amps', () => {
// 14 A = 9,660 W lies between steps; Core floors it to 6,900 W, as before.
const ui = build(offWholeAmps);
ui.slider.value = '14';
ui.slider.fire('input');
assert.equal(ui.readout.textContent, '14 A · 9.7 kW');
ui.start.fire('click');
assert.deepEqual(ui.sent, [{ power_w: 9660, hold_s: 0, phase_mode: '3p' }]);
});

test('a hold at the top step reads as the top', () => {
const ui = build({ ...offWholeAmps, manual_active: true, manual_charge_w: 10400 });
assert.equal(ui.slider.value, '15');
assert.equal(ui.readout.textContent, '15 A · 10.4 kW');
});
Loading