From 82482f01cf667d6210f2469118ca06c6f17bd7e5 Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 4 Jun 2026 12:21:11 +0200 Subject: [PATCH] chore: simplify --- core/capable_test.go | 2 +- meter/hoymiles-dtu-mbtcp.go | 2 +- meter/shelly/connection.go | 2 +- plugin/sma/discover.go | 6 +++--- tariff/embed.go | 7 ++++--- util/param_shard.go | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/core/capable_test.go b/core/capable_test.go index 40a925c34..00d34bfb4 100644 --- a/core/capable_test.go +++ b/core/capable_test.go @@ -17,7 +17,7 @@ var _ api.Capable = (*charger)(nil) func (c *charger) Capability(typ reflect.Type) (any, bool) { cap, ok := c.caps[typ] - if !ok && reflect.TypeOf(c).Implements(typ) { + if !ok && reflect.TypeFor[*charger]().Implements(typ) { return c, true } return cap, ok diff --git a/meter/hoymiles-dtu-mbtcp.go b/meter/hoymiles-dtu-mbtcp.go index 9f355d76a..9f4e202bb 100644 --- a/meter/hoymiles-dtu-mbtcp.go +++ b/meter/hoymiles-dtu-mbtcp.go @@ -108,7 +108,7 @@ func (m *hoymilesDTUModbusTCP) panelPower(panelIndex int) (float64, float64, boo func (m *hoymilesDTUModbusTCP) readCurrentValues() (hoymilesDTUValues, error) { var values hoymilesDTUValues - for i := 0; i < hoymilesMaxPanels; i++ { + for i := range hoymilesMaxPanels { power, cumulative, found, err := m.panelPower(i) if err != nil { return hoymilesDTUValues{}, err diff --git a/meter/shelly/connection.go b/meter/shelly/connection.go index 0bc5d402d..abee49f3c 100644 --- a/meter/shelly/connection.go +++ b/meter/shelly/connection.go @@ -55,7 +55,7 @@ func NewConnection(uri, user, password string, channel int, cache time.Duration) return nil, fmt.Errorf("%s (%s) missing user/password", resp.Model, resp.Mac) } - model := strings.Split(resp.Type+resp.Model, "-")[0] + model, _, _ := strings.Cut(resp.Type+resp.Model, "-") client.Transport = request.NewTripper(log, transport.Insecure()) diff --git a/plugin/sma/discover.go b/plugin/sma/discover.go index a772fe0be..78a0a31ca 100644 --- a/plugin/sma/discover.go +++ b/plugin/sma/discover.go @@ -60,7 +60,7 @@ type Discoverer struct { conn *sunny.Connection devices map[uint32]*Device mux sync.RWMutex - done uint32 + done atomic.Uint32 } func (d *Discoverer) createDevice(device *sunny.Device) *Device { @@ -100,7 +100,7 @@ func (d *Discoverer) run() { close(devices) // mark discover as done - atomic.AddUint32(&d.done, 1) + d.done.Add(1) } func (d *Discoverer) get(serial uint32, password string) *Device { @@ -119,7 +119,7 @@ func (d *Discoverer) DeviceBySerial(serial uint32, password string) *Device { start := time.Now() for time.Since(start) < time.Second*3 { // discover done -> return immediately regardless of result - if atomic.LoadUint32(&d.done) != 0 { + if d.done.Load() != 0 { return d.get(serial, password) } diff --git a/tariff/embed.go b/tariff/embed.go index 4ff62ce3a..0a0ea9e10 100644 --- a/tariff/embed.go +++ b/tariff/embed.go @@ -3,6 +3,7 @@ package tariff import ( "errors" "fmt" + "slices" "time" "github.com/evcc-io/evcc/api" @@ -96,9 +97,9 @@ func (t *embed) effectiveCharges(ts time.Time) float64 { hm := fixed.HourMin{Hour: ts.Hour(), Min: ts.Minute()} zones := t.chargesZones.ForDayAndMonth(day, month) - for j := len(zones) - 1; j >= 0; j-- { - if zones[j].Hours.Contains(hm) { - return zones[j].Price + for _, z := range slices.Backward(zones) { + if z.Hours.Contains(hm) { + return z.Price } } return t.Charges diff --git a/util/param_shard.go b/util/param_shard.go index 4ce0fe9d7..a789f1878 100644 --- a/util/param_shard.go +++ b/util/param_shard.go @@ -68,7 +68,7 @@ func (s *sharderImpl) shards(useCache bool) iter.Seq2[string, any] { func jsonKey(f *structs.Field) string { key := f.Name() if t := f.Tag("json"); t != "" { - if n := strings.Split(t, ",")[0]; n != "" { + if n, _, _ := strings.Cut(t, ","); n != "" { key = n } }