From e5b3ce827115b091f98bef332bb391f1f3698f20 Mon Sep 17 00:00:00 2001 From: Simon Schenk Date: Sat, 31 May 2025 10:21:36 +0200 Subject: [PATCH] Config UI: cleanup circuit references (#21525) --- core/keys/loadpoint.go | 5 +++-- core/loadpoint/api.go | 6 +++--- core/loadpoint/mock.go | 12 ++++++++++++ core/loadpoint_api.go | 15 +++++++++++++++ server/http_config_device_handler.go | 8 ++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/core/keys/loadpoint.go b/core/keys/loadpoint.go index 56d412286..82d90ca7f 100644 --- a/core/keys/loadpoint.go +++ b/core/keys/loadpoint.go @@ -5,8 +5,9 @@ const ( Title = "title" // loadpoint title Mode = "mode" // charge mode DefaultMode = "defaultMode" // default charge mode - Charger = "charger" // charger ref - Meter = "meter" // meter ref + Charger = "charger" // charger ref + Meter = "meter" // meter ref + Circuit = "circuit" // circuit ref DefaultVehicle = "vehicle" // default vehicle ref Priority = "priority" // priority MinCurrent = "minCurrent" // min current diff --git a/core/loadpoint/api.go b/core/loadpoint/api.go index e640523f6..6908ecbbe 100644 --- a/core/loadpoint/api.go +++ b/core/loadpoint/api.go @@ -26,8 +26,6 @@ type API interface { // references // - // TODO SetCircuitRef - // GetChargerRef returns the loadpoint charger GetChargerRef() string // SetChargerRef sets the loadpoint charger @@ -36,8 +34,10 @@ type API interface { GetMeterRef() string // SetMeterRef sets the loadpoint meter SetMeterRef(string) - // GetCircuitRef returns the loadpoint circuit name + // GetCircuitRef returns the loadpoint circuit GetCircuitRef() string + // SetCircuitRef sets the loadpoint circuit + SetCircuitRef(string) // GetCircuit returns the loadpoint circuit GetCircuit() api.Circuit // GetDefaultVehicleRef returns the loadpoint default vehicle diff --git a/core/loadpoint/mock.go b/core/loadpoint/mock.go index ca1e5d6ec..e098a02cd 100644 --- a/core/loadpoint/mock.go +++ b/core/loadpoint/mock.go @@ -696,6 +696,18 @@ func (mr *MockAPIMockRecorder) SetChargerRef(arg0 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetChargerRef", reflect.TypeOf((*MockAPI)(nil).SetChargerRef), arg0) } +// SetCircuitRef mocks base method. +func (m *MockAPI) SetCircuitRef(arg0 string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetCircuitRef", arg0) +} + +// SetCircuitRef indicates an expected call of SetCircuitRef. +func (mr *MockAPIMockRecorder) SetCircuitRef(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetCircuitRef", reflect.TypeOf((*MockAPI)(nil).SetCircuitRef), arg0) +} + // SetDefaultMode mocks base method. func (m *MockAPI) SetDefaultMode(arg0 api.ChargeMode) { m.ctrl.T.Helper() diff --git a/core/loadpoint_api.go b/core/loadpoint_api.go index a2117777e..049021ca5 100644 --- a/core/loadpoint_api.go +++ b/core/loadpoint_api.go @@ -66,6 +66,21 @@ func (lp *Loadpoint) GetCircuitRef() string { return lp.CircuitRef } +// SetCircuitRef sets the loadpoint circuit +func (lp *Loadpoint) SetCircuitRef(ref string) { + if !lp.isConfigurable() { + lp.log.ERROR.Println("cannot set circuit ref: not configurable") + return + } + + lp.log.DEBUG.Println("set circuit ref:", ref) + + lp.Lock() + defer lp.Unlock() + lp.CircuitRef = ref + lp.settings.SetString(keys.Circuit, ref) +} + // GetDefaultVehicleRef returns the loadpoint default vehicle func (lp *Loadpoint) GetDefaultVehicleRef() string { lp.RLock() diff --git a/server/http_config_device_handler.go b/server/http_config_device_handler.go index 36984e31d..578d2f226 100644 --- a/server/http_config_device_handler.go +++ b/server/http_config_device_handler.go @@ -487,6 +487,14 @@ func deleteDeviceHandler(site site.API) func(w http.ResponseWriter, r *http.Requ case templates.Circuit: err = deleteDevice(id, config.Circuits()) + + // cleanup references + for _, dev := range h.Devices() { + lp := dev.Instance() + if lp.GetCircuitRef() == config.NameForID(id) { + lp.SetCircuitRef("") + } + } } setConfigDirty()