Skip to content

Commit

Permalink
feat(hass,linux): ♻️ support flagging for retryable requests through …
Browse files Browse the repository at this point in the history
…sensor options
  • Loading branch information
joshuar committed Dec 22, 2024
1 parent 73f218b commit 5d55cc6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
13 changes: 11 additions & 2 deletions internal/hass/sensor/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

type Option[T any] func(T) T

type RequestMetadata struct {
type requestMetadata struct {
RetryRequest bool
}

Expand All @@ -28,7 +28,6 @@ type State struct {
Icon string `json:"icon,omitempty" validate:"omitempty,startswith=mdi:"`
ID string `json:"unique_id" validate:"required"`
EntityType types.SensorType `json:"type" validate:"omitempty"`
RequestMetadata
}

// WithValue assigns a value to the sensor.
Expand Down Expand Up @@ -143,6 +142,7 @@ func (s *State) Validate() error {

type Entity struct {
*State
requestMetadata
Name string `json:"name" validate:"required"`
Units string `json:"unit_of_measurement,omitempty" validate:"omitempty"`
DeviceClass types.DeviceClass `json:"device_class,omitempty" validate:"omitempty"`
Expand Down Expand Up @@ -212,6 +212,15 @@ func AsDiagnostic() Option[Entity] {
}
}

// WithRequestRetry flags that any API requests for this entity should be
// retried.
func WithRequestRetry(value bool) Option[Entity] {
return func(e Entity) Entity {
e.RetryRequest = value
return e
}
}

// NewSensor provides a way to build a sensor entity with the given options.
func NewSensor(options ...Option[Entity]) Entity {
sensor := Entity{}
Expand Down
2 changes: 2 additions & 0 deletions internal/linux/net/networkRates.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ func (s *netStatsSensor) update(link string, sensorType netStatsType, stats *rtn
switch sensorType {
case bytesRecv:
s.UpdateValue(stats.RXBytes)

if link != totalsName {
maps.Copy(s.Attributes, getRXAttributes(stats))
}
case bytesSent:
s.UpdateValue(stats.TXBytes)

if link != totalsName {
maps.Copy(s.Attributes, getTXAttributes(stats))
}
Expand Down
10 changes: 5 additions & 5 deletions internal/linux/net/wifiSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newWifiSensor(prop string, value any) sensor.Entity {
icon = generateStrIcon(value)
}

s := sensor.NewSensor(
wifiSensor := sensor.NewSensor(
sensor.WithName(name),
sensor.AsDiagnostic(),
sensor.WithState(
Expand All @@ -80,18 +80,18 @@ func newWifiSensor(prop string, value any) sensor.Entity {
)

if deviceClass != types.SensorDeviceClassNone {
s = sensor.WithDeviceClass(deviceClass)(s)
wifiSensor = sensor.WithDeviceClass(deviceClass)(wifiSensor)
}

if stateClass != types.StateClassNone {
s = sensor.WithStateClass(stateClass)(s)
wifiSensor = sensor.WithStateClass(stateClass)(wifiSensor)
}

if units != "" {
s = sensor.WithUnits(units)(s)
wifiSensor = sensor.WithUnits(units)(wifiSensor)
}

return s
return wifiSensor
}

func getWifiSensors(bus *dbusx.Bus, apPath string) []sensor.Entity {
Expand Down
9 changes: 2 additions & 7 deletions internal/linux/power/powerState.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
type powerSignal int

func newPowerState(name powerSignal, value any) sensor.Entity {
s := sensor.NewSensor(
return sensor.NewSensor(
sensor.WithName("Power State"),
sensor.AsDiagnostic(),
sensor.WithState(
Expand All @@ -40,13 +40,8 @@ func newPowerState(name powerSignal, value any) sensor.Entity {
sensor.WithValue(powerStateString(name, value)),
sensor.WithDataSourceAttribute(linux.DataSrcDbus),
),
sensor.WithRequestRetry(true),
)

s.State.RequestMetadata = sensor.RequestMetadata{
RetryRequest: true,
}

return s
}

func powerStateString(signal powerSignal, value any) string {
Expand Down
9 changes: 2 additions & 7 deletions internal/linux/power/screenLock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
)

func newScreenlockSensor(value bool) sensor.Entity {
s := sensor.NewSensor(
return sensor.NewSensor(
sensor.WithName("Screen Lock"),
sensor.WithDeviceClass(types.BinarySensorDeviceClassLock),
sensor.WithState(
Expand All @@ -36,13 +36,8 @@ func newScreenlockSensor(value bool) sensor.Entity {
sensor.WithValue(!value), // For device class BinarySensorDeviceClassLock: On means open (unlocked), Off means closed (locked).
sensor.WithDataSourceAttribute(linux.DataSrcDbus),
),
sensor.WithRequestRetry(true),
)

s.RequestMetadata = sensor.RequestMetadata{
RetryRequest: true,
}

return s
}

func screenLockIcon(value bool) string {
Expand Down
12 changes: 6 additions & 6 deletions internal/linux/system/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func newHWSensor(details *hwmon.Sensor) sensor.Entity {
case hwmon.Alarm, hwmon.Intrusion:
if v, ok := details.Value().(bool); ok && v {
icon = "mdi:alarm-light"
} else {
icon = "mdi:alarm-light-off"
}

icon = "mdi:alarm-light-off"

if details.MonitorType == hwmon.Alarm {
deviceClass = types.BinarySensorDeviceClassProblem
} else {
Expand All @@ -66,7 +66,7 @@ func newHWSensor(details *hwmon.Sensor) sensor.Entity {
stateClass = types.StateClassMeasurement
}

s := sensor.NewSensor(
hwMonSensor := sensor.NewSensor(
sensor.WithName(details.Name()),
sensor.WithDeviceClass(deviceClass),
sensor.AsDiagnostic(),
Expand All @@ -80,14 +80,14 @@ func newHWSensor(details *hwmon.Sensor) sensor.Entity {
)

if stateClass != types.StateClassNone {
s = sensor.WithStateClass(stateClass)(s)
hwMonSensor = sensor.WithStateClass(stateClass)(hwMonSensor)
}

if details.MonitorType == hwmon.Alarm || details.MonitorType == hwmon.Intrusion {
s.EntityType = types.BinarySensor
hwMonSensor.EntityType = types.BinarySensor
}

return s
return hwMonSensor
}

type hwMonWorker struct{}
Expand Down

0 comments on commit 5d55cc6

Please sign in to comment.