Skip to content

Commit

Permalink
Fibaro input: for battery operated devices, add battery level scraping (
Browse files Browse the repository at this point in the history
  • Loading branch information
dynek authored and Vincent Orlowski committed May 4, 2020
1 parent 8adc004 commit af7d4c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions plugins/inputs/fibaro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Those values could be true (1) or false (0) for switches, percentage for dimmers
- name (device name)
- type (device type)
- fields:
- batteryLevel (float, when available from device)
- energy (float, when available from device)
- power (float, when available from device)
- value (float)
Expand All @@ -52,4 +53,5 @@ fibaro,deviceId=220,host=vm1,name=CO2\ (ppm),room=Salon,section=Pièces\ commune
fibaro,deviceId=221,host=vm1,name=Humidité\ (%),room=Salon,section=Pièces\ communes,type=com.fibaro.humiditySensor value=61 1529996807000000000
fibaro,deviceId=222,host=vm1,name=Pression\ (mb),room=Salon,section=Pièces\ communes,type=com.fibaro.multilevelSensor value=1013.7 1529996807000000000
fibaro,deviceId=223,host=vm1,name=Bruit\ (db),room=Salon,section=Pièces\ communes,type=com.fibaro.multilevelSensor value=44 1529996807000000000
fibaro,deviceId=248,host=vm1,name=Température,room=Garage,section=Extérieur,type=com.fibaro.temperatureSensor batteryLevel=85,value=10.8 1529996807000000000
```
17 changes: 12 additions & 5 deletions plugins/inputs/fibaro/fibaro.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ type Devices struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
Properties struct {
Dead interface{} `json:"dead"`
Energy interface{} `json:"energy"`
Power interface{} `json:"power"`
Value interface{} `json:"value"`
Value2 interface{} `json:"value2"`
BatteryLevel interface{} `json:"batteryLevel"`
Dead interface{} `json:"dead"`
Energy interface{} `json:"energy"`
Power interface{} `json:"power"`
Value interface{} `json:"value"`
Value2 interface{} `json:"value2"`
} `json:"properties"`
}

Expand Down Expand Up @@ -174,6 +175,12 @@ func (f *Fibaro) Gather(acc telegraf.Accumulator) error {
}
fields := make(map[string]interface{})

if device.Properties.BatteryLevel != nil {
if fValue, err := strconv.ParseFloat(device.Properties.BatteryLevel.(string), 64); err == nil {
fields["batteryLevel"] = fValue
}
}

if device.Properties.Energy != nil {
if fValue, err := strconv.ParseFloat(device.Properties.Energy.(string), 64); err == nil {
fields["energy"] = fValue
Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/fibaro/fibaro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const devicesJSON = `
"type": "com.fibaro.temperatureSensor",
"enabled": true,
"properties": {
"batteryLevel": "100",
"dead": "false",
"value": "22.80"
},
Expand Down Expand Up @@ -196,7 +197,7 @@ func TestJSONSuccess(t *testing.T) {

// Ensure fields / values are correct - Device 4
tags = map[string]string{"deviceId": "4", "section": "Section 3", "room": "Room 4", "name": "Device 4", "type": "com.fibaro.temperatureSensor"}
fields = map[string]interface{}{"value": float64(22.8)}
fields = map[string]interface{}{"batteryLevel": float64(100), "value": float64(22.8)}
acc.AssertContainsTaggedFields(t, "fibaro", fields, tags)

// Ensure fields / values are correct - Device 5
Expand Down

0 comments on commit af7d4c1

Please sign in to comment.