Skip to content

Commit

Permalink
style(hass,linux): change name of unknown state to match Go conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Sep 26, 2023
1 parent c4722fc commit c6fd675
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/hass/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
STATE_UNKNOWN = "unknown"
StateUnknown = "unknown"
)

// SensorRegistrationInfo is the JSON structure required to register a sensor
Expand Down
10 changes: 5 additions & 5 deletions internal/linux/batterySensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ func stringState(state uint32) string {
case 6:
return "Pending Discharge"
default:
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
}

func stringType(t uint32) string {
switch t {
case 0:
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
case 1:
return "Line Power"
case 2:
Expand All @@ -259,14 +259,14 @@ func stringType(t uint32) string {
case 8:
return "Phone"
default:
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
}

func stringLevel(l uint32) string {
switch l {
case 0:
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
case 1:
return "None"
case 3:
Expand All @@ -280,7 +280,7 @@ func stringLevel(l uint32) string {
case 8:
return "Full"
default:
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
}

Expand Down
40 changes: 20 additions & 20 deletions internal/linux/networkConnectionSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func getActiveConns() []dbus.ObjectPath {
func getConnType(p dbus.ObjectPath) string {
if !p.IsValid() {
log.Debug().Msgf("Invalid D-Bus object path %s.", p)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
v, err := NewBusRequest(SystemBus).
Path(p).
Destination(dBusNMObj).
GetProp(dBusPropConnType)
if err != nil {
log.Debug().Err(err).Msg("Could not fetch type of connection.")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
} else {
return string(variantToValue[[]uint8](v))
}
Expand All @@ -86,7 +86,7 @@ func getConnType(p dbus.ObjectPath) string {
func getConnName(p dbus.ObjectPath) string {
if !p.IsValid() {
log.Debug().Msgf("Invalid D-Bus object path %s.", p)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
variant, err := NewBusRequest(SystemBus).
Path(p).
Expand All @@ -95,7 +95,7 @@ func getConnName(p dbus.ObjectPath) string {
if err != nil {
log.Error().Err(err).
Msg("Could not fetch connection ID")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
return string(variantToValue[[]uint8](variant))
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func getConnState(p dbus.ObjectPath) string {
return "Activating"
}
}
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}

// getConnDevice returns the D-Bus object representing the device for the given connection object.
Expand All @@ -135,13 +135,13 @@ func getConnDevice(p dbus.ObjectPath) dbus.ObjectPath {
if err != nil {
log.Error().Err(err).
Msgf("Unable to fetch device for connection %s.", p)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
} else {
// ! this conversion might yield unexpected results
devicePath := variantToValue[[]dbus.ObjectPath](variant)[0]
if !devicePath.IsValid() {
log.Debug().Msgf("Invalid device path for connection %s.", p)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
return devicePath
}
Expand All @@ -153,7 +153,7 @@ func getConnDevice(p dbus.ObjectPath) dbus.ObjectPath {
func getIPAddr(p dbus.ObjectPath, ver int) string {
if !p.IsValid() {
log.Debug().Msgf("Invalid D-Bus object path %s.", p)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
var connProp, addrProp string
switch ver {
Expand All @@ -169,24 +169,24 @@ func getIPAddr(p dbus.ObjectPath, ver int) string {
Destination(dBusNMObj).
GetProp(connProp)
if err != nil {
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
addrPath, ok := v.Value().(dbus.ObjectPath)
if !ok {
log.Debug().Msgf("Cannot process value recieved from D-Bus. Got %T.", addrPath)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
v, err = NewBusRequest(SystemBus).
Path(addrPath).
Destination(dBusNMObj).
GetProp(addrProp)
if err != nil {
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
addrs, ok := v.Value().([]map[string]dbus.Variant)
if !ok {
log.Debug().Msgf("Cannot process value recieved from D-Bus. Got %T.", addrPath)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
for _, a := range addrs {
ip := net.ParseIP(a["address"].Value().(string))
Expand All @@ -195,15 +195,15 @@ func getIPAddr(p dbus.ObjectPath, ver int) string {
}
}
log.Debug().Msg("Could not ascertain IP address.")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}

// getWifiProp will fetch the appropriate value for the given wifi sensor type
// from D-Bus. If it cannot find the type, it returns sensor.STATE_UNKNOWN.
func getWifiProp(p dbus.ObjectPath, t sensorType) interface{} {
if !p.IsValid() {
log.Debug().Msgf("Invalid D-Bus object path %s.", p)
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
var prop string
switch t {
Expand All @@ -225,12 +225,12 @@ func getWifiProp(p dbus.ObjectPath, t sensorType) interface{} {
GetProp(dBusPropConnAP)
if err != nil {
log.Debug().Err(err).Msg("Unable to retrieve wireless device details from D-Bus.")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
} else {
path = dbus.ObjectPath((variantToValue[[]uint8](ap)))
if !path.IsValid() {
log.Debug().Msg("AP D-Bus Path is invalid")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
}
v, err := NewBusRequest(SystemBus).
Expand All @@ -239,7 +239,7 @@ func getWifiProp(p dbus.ObjectPath, t sensorType) interface{} {
GetProp(prop)
if err != nil {
log.Debug().Err(err).Msg("Could not fetch wifi property from D-Bus.")
return dbus.MakeVariant(sensor.STATE_UNKNOWN)
return dbus.MakeVariant(sensor.StateUnknown)
}
switch t {
case wifiSSID, wifiHWAddress:
Expand All @@ -248,7 +248,7 @@ func getWifiProp(p dbus.ObjectPath, t sensorType) interface{} {
return variantToValue[uint32](v)
}

return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}

// handleConn will treat the given path as a connection object and create a
Expand Down Expand Up @@ -279,15 +279,15 @@ func handleConnType(p dbus.ObjectPath, extraSensors chan *networkSensor) {
switch connType {
case "802-11-wireless":
devicePath := getConnDevice(p)
if devicePath == sensor.STATE_UNKNOWN {
if devicePath == sensor.StateUnknown {
return
}
wifiProps := []sensorType{wifiSSID, wifiHWAddress, wifiFrequency, wifiSpeed, wifiStrength}
for _, prop := range wifiProps {
s := newNetworkSensor(devicePath, prop, nil)
extraSensors <- s
}
case sensor.STATE_UNKNOWN:
case sensor.StateUnknown:
fallthrough
default:
log.Trace().Caller().Msgf("Unhandled connection type %s (%s).", connType, p)
Expand Down
4 changes: 2 additions & 2 deletions internal/linux/timeSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func getUptime(ctx context.Context) interface{} {
if err != nil {
log.Debug().Caller().Err(err).
Msg("Failed to retrieve uptime.")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
epoch := time.Unix(0, 0)
uptime := time.Unix(int64(u), 0)
Expand All @@ -85,7 +85,7 @@ func getBoottime(ctx context.Context) string {
if err != nil {
log.Debug().Caller().Err(err).
Msg("Failed to retrieve boottime.")
return sensor.STATE_UNKNOWN
return sensor.StateUnknown
}
return time.Unix(int64(u), 0).Format(time.RFC3339)
}

0 comments on commit c6fd675

Please sign in to comment.