Skip to content

Commit

Permalink
refactor: ♻️ use a map[string]any for sensor attributes rather than r…
Browse files Browse the repository at this point in the history
…equiring custom structs
  • Loading branch information
joshuar committed Jun 18, 2024
1 parent 3cf55fb commit 2d03c84
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 152 deletions.
11 changes: 4 additions & 7 deletions internal/device/external_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,11 @@ func (a *address) Units() string { return "" }

func (a *address) Category() string { return "diagnostic" }

func (a *address) Attributes() any {
now := time.Now()
func (a *address) Attributes() map[string]any {
attributes := make(map[string]any)
attributes["last_updated"] = time.Now().Format(time.RFC3339)

return &struct {
LastUpdated string `json:"last_updated"`
}{
LastUpdated: now.Format(time.RFC3339),
}
return attributes
}

func lookupExternalIPs(client *resty.Client, ver int) (*address, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/device/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (v *version) Units() string { return "" }

func (v *version) Category() string { return "diagnostic" }

func (v *version) Attributes() any { return nil }
func (v *version) Attributes() map[string]any { return nil }

type versionWorker struct{}

Expand Down
6 changes: 3 additions & 3 deletions internal/hass/sensor/mock_Registration_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/hass/sensor/mock_State_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/hass/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type State interface {
State() any
SensorType() types.SensorClass
Units() string
Attributes() any
Attributes() map[string]any
}

//go:generate moq -out mock_Registration_test.go . Registration
Expand Down
2 changes: 1 addition & 1 deletion internal/hass/sensor/sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
var mockSensor = RegistrationMock{
IDFunc: func() string { return "mock_sensor" },
StateFunc: func() any { return "mockState" },
AttributesFunc: func() any { return nil },
AttributesFunc: func() map[string]any { return nil },
IconFunc: func() string { return "mdi:mock-icon" },
SensorTypeFunc: func() types.SensorClass { return types.Sensor },
NameFunc: func() string { return "Mock Sensor" },
Expand Down
17 changes: 7 additions & 10 deletions internal/linux/apps/runningApps.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,23 @@ type runningAppsSensor struct {
mu sync.Mutex
}

type runningAppsSensorAttributes struct {
DataSource string `json:"data_source"`
RunningApps []string `json:"running_apps"`
}
func (r *runningAppsSensor) Attributes() map[string]any {
attributes := make(map[string]any)

//nolint:exhaustruct
func (r *runningAppsSensor) Attributes() any {
attrs := &runningAppsSensorAttributes{}
var apps []string

r.mu.Lock()
for appName, state := range r.appList {
if dbusx.VariantToValue[uint32](state) > 0 {
attrs.RunningApps = append(attrs.RunningApps, appName)
apps = append(apps, appName)
}
}
r.mu.Unlock()

attrs.DataSource = linux.DataSrcDbus
attributes["running_apps"] = apps
attributes["data_source"] = linux.DataSrcDbus

return attrs
return attributes
}

func (r *runningAppsSensor) count() int {
Expand Down
8 changes: 6 additions & 2 deletions internal/linux/battery/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ func (s *upowerBatterySensor) Units() string {
}
}

func (s *upowerBatterySensor) Attributes() any {
return s.attributes
func (s *upowerBatterySensor) Attributes() map[string]any {
attributes := make(map[string]any)

attributes["extra_attributes"] = s.attributes

return attributes
}

//nolint:exhaustive
Expand Down
49 changes: 20 additions & 29 deletions internal/linux/disk/diskRates.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ type diskIOSensor struct {
prev uint64
}

type diskIOSensorAttributes struct {
DataSource string `json:"data_source"`
NativeUnit string `json:"native_unit_of_measurement,omitempty"`
Model string `json:"device_model,omitempty"`
SysFSPath string `json:"sysfs_path,omitempty"`
Sectors uint64 `json:"total_sectors,omitempty"`
Time uint64 `json:"total_milliseconds,omitempty"`
}

type sensors struct {
totalReads *diskIOSensor
totalWrites *diskIOSensor
Expand All @@ -63,35 +54,35 @@ func (s *diskIOSensor) ID() string {
return s.device.ID + "_" + strcase.ToSnake(s.SensorTypeValue.String())
}

//nolint:exhaustive,exhaustruct
func (s *diskIOSensor) Attributes() any {
//nolint:exhaustive
func (s *diskIOSensor) Attributes() map[string]any {
attributes := make(map[string]any)

// Common attributes for all disk IO sensors
attrs := &diskIOSensorAttributes{
DataSource: linux.DataSrcSysfs,
Model: s.device.Model,
SysFSPath: s.device.SysFSPath,
attributes["data_source"] = linux.DataSrcSysfs

if s.device.Model != "" {
attributes["device_model"] = s.device.Model
}

if s.device.SysFSPath != "" {
attributes["sysfs_path"] = s.device.SysFSPath
}

switch s.SensorTypeValue {
case linux.SensorDiskReads:
attrs.Sectors = s.stats[diskstats.TotalSectorsRead]
attrs.Time = s.stats[diskstats.TotalTimeReading]
attrs.NativeUnit = diskCountUnits

return attrs
attributes["total_sectors"] = s.stats[diskstats.TotalSectorsRead]
attributes["total_milliseconds"] = s.stats[diskstats.TotalTimeReading]
attributes["native_unit_of_measurement"] = diskCountUnits
case linux.SensorDiskWrites:
attrs.Sectors = s.stats[diskstats.TotalSectorsWritten]
attrs.Time = s.stats[diskstats.TotalTimeWriting]
attrs.NativeUnit = diskCountUnits

return attrs
attributes["total_sectors"] = s.stats[diskstats.TotalSectorsWritten]
attributes["total_milliseconds"] = s.stats[diskstats.TotalTimeWriting]
attributes["native_unit_of_measurement"] = diskCountUnits
case linux.SensorDiskReadRate, linux.SensorDiskWriteRate:
attrs.NativeUnit = diskRateUnits

return attrs
attributes["native_unit_of_measurement"] = diskRateUnits
}

return nil
return attributes
}

//nolint:exhaustive
Expand Down
15 changes: 7 additions & 8 deletions internal/linux/disk/diskUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ func (d *diskUsageSensor) ID() string {
return "mountpoint" + strings.ReplaceAll(d.stats.Path, "/", "_")
}

func (d *diskUsageSensor) Attributes() any {
return struct {
DataSource string `json:"data_source"`
Stats disk.UsageStat
}{
DataSource: linux.DataSrcProcfs,
Stats: *d.stats,
}
func (d *diskUsageSensor) Attributes() map[string]any {
attributes := make(map[string]any)

attributes["data_source"] = linux.DataSrcProcfs
attributes["stats"] = d.stats

return attributes
}

type usageWorker struct{}
Expand Down
10 changes: 0 additions & 10 deletions internal/linux/mem/memUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ type memorySensor struct {
linux.Sensor
}

func (s *memorySensor) Attributes() any {
return struct {
NativeUnit string `json:"native_unit_of_measurement"`
DataSource string `json:"data_source"`
}{
NativeUnit: s.UnitsString,
DataSource: s.SensorSrc,
}
}

//nolint:exhaustive,exhaustruct
func newMemoryUsageSensor(sensorType linux.SensorTypeValue, stats *mem.VirtualMemoryStat) (*memorySensor, error) {
newSensor := &memorySensor{}
Expand Down
19 changes: 17 additions & 2 deletions internal/linux/net/networkConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,23 @@ func (c *connection) Icon() string {
return i.String()
}

func (c *connection) Attributes() any {
return c.attrs
func (c *connection) Attributes() map[string]any {
attributes := make(map[string]any)

attributes["connection_type"] = c.attrs.ConnectionType
attributes["data_source"] = linux.DataSrcDbus

if c.attrs.Ipv4 != "" {
attributes["ipv4_address"] = c.attrs.Ipv4
attributes["ipv4_mask"] = c.attrs.IPv4Mask
}

if c.attrs.Ipv6 != "" {
attributes["ipv6_address"] = c.attrs.Ipv6
attributes["ipv6_mask"] = c.attrs.IPv6Mask
}

return attributes
}

func (c *connection) State() any {
Expand Down
18 changes: 8 additions & 10 deletions internal/linux/net/networkRates.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ type netIOSensor struct {
netIOSensorAttributes
}

func (s *netIOSensor) Attributes() any {
return struct {
NativeUnit string `json:"native_unit_of_measurement"`
DataSource string `json:"data_source"`
netIOSensorAttributes
}{
NativeUnit: s.UnitsString,
DataSource: linux.DataSrcProcfs,
netIOSensorAttributes: s.netIOSensorAttributes,
}
func (s *netIOSensor) Attributes() map[string]any {
attributes := make(map[string]any)

attributes["native_unit_of_measurement"] = s.UnitsString
attributes["data_source"] = linux.DataSrcProcfs
attributes["stats"] = s.netIOSensorAttributes

return attributes
}

//nolint:exhaustive
Expand Down
15 changes: 7 additions & 8 deletions internal/linux/power/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ func (s *idleSensor) Icon() string {
}
}

func (s *idleSensor) Attributes() any {
return struct {
DataSource string `json:"data_source"`
Seconds float64 `json:"duration"`
}{
DataSource: linux.DataSrcDbus,
Seconds: idleTime(s.idleTime),
}
func (s *idleSensor) Attributes() map[string]any {
attributes := make(map[string]any)

attributes["data_source"] = linux.DataSrcDbus
attributes["duration"] = idleTime(s.idleTime)

return attributes
}

//nolint:exhaustruct
Expand Down
16 changes: 9 additions & 7 deletions internal/linux/problems/problems.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ type problemsSensor struct {
linux.Sensor
}

func (s *problemsSensor) Attributes() any {
return struct {
ProblemList map[string]map[string]any `json:"problem_list"`
DataSource string `json:"data_source"`
}{
ProblemList: s.list,
DataSource: linux.DataSrcDbus,
func (s *problemsSensor) Attributes() map[string]any {
attributes := make(map[string]any)

if s.list != nil {
attributes["problem_list"] = s.list
}

attributes["data_source"] = linux.DataSrcDbus

return attributes
}

func parseProblem(details map[string]string) map[string]any {
Expand Down
16 changes: 9 additions & 7 deletions internal/linux/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,18 @@ func (l *Sensor) Units() string {
return l.UnitsString
}

func (l *Sensor) Attributes() any {
func (l *Sensor) Attributes() map[string]any {
attributes := make(map[string]any)

if l.SensorSrc != "" {
return struct {
DataSource string `json:"data_source"`
}{
DataSource: l.SensorSrc,
}
attributes["data_source"] = l.SensorSrc
}

if l.UnitsString != "" {
attributes["native_unit_of_measurement"] = l.UnitsString
}

return nil
return attributes
}

func (l *Sensor) String() string {
Expand Down
Loading

0 comments on commit 2d03c84

Please sign in to comment.