chore: simplify

This commit is contained in:
andig 2026-06-04 12:21:11 +02:00
parent ffbdb4eac3
commit 82482f01cf
6 changed files with 11 additions and 10 deletions

View file

@ -17,7 +17,7 @@ var _ api.Capable = (*charger)(nil)
func (c *charger) Capability(typ reflect.Type) (any, bool) { func (c *charger) Capability(typ reflect.Type) (any, bool) {
cap, ok := c.caps[typ] cap, ok := c.caps[typ]
if !ok && reflect.TypeOf(c).Implements(typ) { if !ok && reflect.TypeFor[*charger]().Implements(typ) {
return c, true return c, true
} }
return cap, ok return cap, ok

View file

@ -108,7 +108,7 @@ func (m *hoymilesDTUModbusTCP) panelPower(panelIndex int) (float64, float64, boo
func (m *hoymilesDTUModbusTCP) readCurrentValues() (hoymilesDTUValues, error) { func (m *hoymilesDTUModbusTCP) readCurrentValues() (hoymilesDTUValues, error) {
var values hoymilesDTUValues var values hoymilesDTUValues
for i := 0; i < hoymilesMaxPanels; i++ { for i := range hoymilesMaxPanels {
power, cumulative, found, err := m.panelPower(i) power, cumulative, found, err := m.panelPower(i)
if err != nil { if err != nil {
return hoymilesDTUValues{}, err return hoymilesDTUValues{}, err

View file

@ -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) 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()) client.Transport = request.NewTripper(log, transport.Insecure())

View file

@ -60,7 +60,7 @@ type Discoverer struct {
conn *sunny.Connection conn *sunny.Connection
devices map[uint32]*Device devices map[uint32]*Device
mux sync.RWMutex mux sync.RWMutex
done uint32 done atomic.Uint32
} }
func (d *Discoverer) createDevice(device *sunny.Device) *Device { func (d *Discoverer) createDevice(device *sunny.Device) *Device {
@ -100,7 +100,7 @@ func (d *Discoverer) run() {
close(devices) close(devices)
// mark discover as done // mark discover as done
atomic.AddUint32(&d.done, 1) d.done.Add(1)
} }
func (d *Discoverer) get(serial uint32, password string) *Device { 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() start := time.Now()
for time.Since(start) < time.Second*3 { for time.Since(start) < time.Second*3 {
// discover done -> return immediately regardless of result // discover done -> return immediately regardless of result
if atomic.LoadUint32(&d.done) != 0 { if d.done.Load() != 0 {
return d.get(serial, password) return d.get(serial, password)
} }

View file

@ -3,6 +3,7 @@ package tariff
import ( import (
"errors" "errors"
"fmt" "fmt"
"slices"
"time" "time"
"github.com/evcc-io/evcc/api" "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()} hm := fixed.HourMin{Hour: ts.Hour(), Min: ts.Minute()}
zones := t.chargesZones.ForDayAndMonth(day, month) zones := t.chargesZones.ForDayAndMonth(day, month)
for j := len(zones) - 1; j >= 0; j-- { for _, z := range slices.Backward(zones) {
if zones[j].Hours.Contains(hm) { if z.Hours.Contains(hm) {
return zones[j].Price return z.Price
} }
} }
return t.Charges return t.Charges

View file

@ -68,7 +68,7 @@ func (s *sharderImpl) shards(useCache bool) iter.Seq2[string, any] {
func jsonKey(f *structs.Field) string { func jsonKey(f *structs.Field) string {
key := f.Name() key := f.Name()
if t := f.Tag("json"); t != "" { if t := f.Tag("json"); t != "" {
if n := strings.Split(t, ",")[0]; n != "" { if n, _, _ := strings.Cut(t, ","); n != "" {
key = n key = n
} }
} }