Skip to content

Commit

Permalink
refactor(linux): rename diagnostic -> isDiagnostic and add isBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Dec 11, 2023
1 parent c9acd91 commit 89d0a51
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 63 deletions.
2 changes: 1 addition & 1 deletion internal/linux/networkConnectionSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func newConnection(ctx context.Context, p dbus.ObjectPath) *connection {
path: p,
}
c.sensorType = connectionState
c.diagnostic = true
c.isDiagnostic = true

r := dbushelpers.NewBusRequest(ctx, dbushelpers.SystemBus).
Path(p).
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/powerProfileSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newPowerSensor(t sensorType, v dbus.Variant) *powerSensor {
s.sensorType = t
s.icon = "mdi:flash"
s.source = srcDbus
s.diagnostic = true
s.isDiagnostic = true
return s
}

Expand Down
8 changes: 4 additions & 4 deletions internal/linux/powerStateSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func PowerStateUpdater(ctx context.Context) chan tracker.Sensor {
func newPowerState(state string) *powerStateSensor {
return &powerStateSensor{
linuxSensor: linuxSensor{
sensorType: powerState,
value: state,
source: srcDbus,
diagnostic: true,
sensorType: powerState,
value: state,
source: srcDbus,
isDiagnostic: true,
},
}
}
Expand Down
6 changes: 1 addition & 5 deletions internal/linux/screenlockSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"

"github.com/godbus/dbus/v5"
"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/tracker"
"github.com/joshuar/go-hass-agent/pkg/dbushelpers"
"github.com/rs/zerolog/log"
Expand All @@ -31,14 +30,11 @@ func (s *screenlockSensor) Icon() string {
}
}

func (s *screenlockSensor) SensorType() sensor.SensorType {
return sensor.TypeBinary
}

func newScreenlockEvent(v bool) *screenlockSensor {
return &screenlockSensor{
linuxSensor: linuxSensor{
sensorType: screenLock,
isBinary: true,
source: srcDbus,
value: v,
},
Expand Down
16 changes: 10 additions & 6 deletions internal/linux/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,37 @@ type linuxSensor struct {
units string
source string
sensorType
diagnostic bool
deviceClass sensor.SensorDeviceClass
stateClass sensor.SensorStateClass
isBinary bool
isDiagnostic bool
deviceClass sensor.SensorDeviceClass
stateClass sensor.SensorStateClass
}

// linuxSensor satisfies the tracker.Sensor interface, allowing it to be sent as
// a sensor update to Home Assistant. Any of the methods below can be overridden
// by embedding linuxSensor in another struct and defining the needed function.

func (l *linuxSensor) Name() string {
return l.String()
return l.sensorType.String()
}

func (l *linuxSensor) ID() string {
return strcase.ToSnake(l.String())
return strcase.ToSnake(l.sensorType.String())
}

func (l *linuxSensor) State() any {
return l.value
}

func (l *linuxSensor) SensorType() sensor.SensorType {
if l.isBinary {
return sensor.TypeBinary
}
return sensor.TypeSensor
}

func (l *linuxSensor) Category() string {
if l.diagnostic {
if l.isDiagnostic {
return "diagnostic"
}
return ""
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/tempSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *tempSensor) Attributes() any {

func newTempSensor(t host.TemperatureStat) *tempSensor {
s := &tempSensor{}
s.diagnostic = true
s.isDiagnostic = true
s.deviceClass = sensor.SensorTemperature
s.stateClass = sensor.StateMeasurement
s.units = "°C"
Expand Down
24 changes: 12 additions & 12 deletions internal/linux/timeSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ func TimeUpdater(ctx context.Context) chan tracker.Sensor {
updateTimes := func(_ time.Duration) {
sensorCh <- &timeSensor{
linuxSensor{
sensorType: uptime,
value: getUptime(ctx),
diagnostic: true,
units: "h",
icon: "mdi:restart",
deviceClass: sensor.Duration,
stateClass: sensor.StateMeasurement,
sensorType: uptime,
value: getUptime(ctx),
isDiagnostic: true,
units: "h",
icon: "mdi:restart",
deviceClass: sensor.Duration,
stateClass: sensor.StateMeasurement,
},
}
sensorCh <- &timeSensor{
linuxSensor{
sensorType: boottime,
value: getBoottime(ctx),
diagnostic: true,
icon: "mdi:restart",
deviceClass: sensor.Timestamp,
sensorType: boottime,
value: getBoottime(ctx),
isDiagnostic: true,
icon: "mdi:restart",
deviceClass: sensor.Timestamp,
},
}
}
Expand Down
30 changes: 15 additions & 15 deletions internal/linux/versionSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ func Versions(ctx context.Context) chan tracker.Sensor {
return sensorCh
}
sensorCh <- &linuxSensor{
sensorType: kernel,
value: info.KernelVersion,
diagnostic: true,
icon: "mdi:chip",
source: srcProcfs,
sensorType: kernel,
value: info.KernelVersion,
isDiagnostic: true,
icon: "mdi:chip",
source: srcProcfs,
}
sensorCh <- &linuxSensor{
sensorType: distribution,
value: cases.Title(language.English).String(info.Platform),
diagnostic: true,
icon: "mdi:linux",
source: srcProcfs,
sensorType: distribution,
value: cases.Title(language.English).String(info.Platform),
isDiagnostic: true,
icon: "mdi:linux",
source: srcProcfs,
}
sensorCh <- &linuxSensor{
sensorType: version,
value: info.PlatformVersion,
diagnostic: true,
icon: "mdi:numeric",
source: srcProcfs,
sensorType: version,
value: info.PlatformVersion,
isDiagnostic: true,
icon: "mdi:numeric",
source: srcProcfs,
}
return sensorCh
}
36 changes: 18 additions & 18 deletions internal/linux/wifiSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@ import (
var wifiProps = map[string]*wifiSensor{
"Ssid": {
linuxSensor: linuxSensor{
sensorType: wifiSSID,
diagnostic: true,
sensorType: wifiSSID,
isDiagnostic: true,
},
},
"HwAddress": {
linuxSensor: linuxSensor{
sensorType: wifiHWAddress,
diagnostic: true,
sensorType: wifiHWAddress,
isDiagnostic: true,
},
},
"MaxBitrate": {
linuxSensor: linuxSensor{
sensorType: wifiSpeed,
units: "kB/s",
deviceClass: sensor.Data_rate,
stateClass: sensor.StateMeasurement,
diagnostic: true,
sensorType: wifiSpeed,
units: "kB/s",
deviceClass: sensor.Data_rate,
stateClass: sensor.StateMeasurement,
isDiagnostic: true,
},
},
"Frequency": {
linuxSensor: linuxSensor{
sensorType: wifiFrequency,
units: "MHz",
deviceClass: sensor.Frequency,
stateClass: sensor.StateMeasurement,
diagnostic: true,
sensorType: wifiFrequency,
units: "MHz",
deviceClass: sensor.Frequency,
stateClass: sensor.StateMeasurement,
isDiagnostic: true,
},
},
"Strength": {
linuxSensor: linuxSensor{
sensorType: wifiStrength,
units: "%",
stateClass: sensor.StateMeasurement,
diagnostic: true,
sensorType: wifiStrength,
units: "%",
stateClass: sensor.StateMeasurement,
isDiagnostic: true,
},
},
}
Expand Down

0 comments on commit 89d0a51

Please sign in to comment.