From 0241a506d4e85a014760264ec86f78b232172917 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Sun, 14 Jan 2024 21:54:01 +0100 Subject: [PATCH] Handle sensors that don't have a name, just a location --- config-template.json | 10 ++++++++++ main.go | 9 +++++---- types/v14.go | 10 +++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/config-template.json b/config-template.json index bd1ecfb..6fbaf81 100644 --- a/config-template.json +++ b/config-template.json @@ -22,6 +22,16 @@ "allowed_credentials": [ "home-assistant" ] + }, + { + "sensor_data": { + "unit": "C", + "location": "Hauptraum", + "description": "Sensor im Hauptraum" + }, + "allowed_credentials": [ + "home-assistant" + ] } ], "humidity": [ diff --git a/main.go b/main.go index 044ceed..5f516ea 100644 --- a/main.go +++ b/main.go @@ -42,10 +42,11 @@ func main() { // Register handlers for Environmental Sensors for key, envSensorConfigs := range conf.Dynamic.Sensors { for i, envSensorConfig := range envSensorConfigs { - http.HandleFunc( - strings.ToLower(fmt.Sprintf( - "/sensors/%s/%s/%s", key, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name, - )), + pattern := fmt.Sprintf("/sensors/%s/%s", key, envSensorConfig.SensorData.Location) + if envSensorConfig.SensorData.Name != "" { + pattern += "/" + envSensorConfig.SensorData.Name + } + http.HandleFunc(strings.ToLower(pattern), handlers.EnvironmentSensor( conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[key][i], ), diff --git a/types/v14.go b/types/v14.go index 8540fe7..2bcdc06 100644 --- a/types/v14.go +++ b/types/v14.go @@ -46,9 +46,9 @@ type EnvironmentSensor struct { Value float64 `json:"value"` Unit string `json:"unit"` Location string `json:"location"` - Name string `json:"name"` - Description string `json:"description"` - LastChange int64 `json:"lastchange"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + LastChange int64 `json:"lastchange,omitempty"` } type PersistentStateV14 struct { @@ -56,12 +56,12 @@ type PersistentStateV14 struct { Open bool `json:"open"` LastChange int64 `json:"lastchange"` } `json:"state"` - Sensors map[string][]PersistentEnvironmentSensor `json:"sensors"` + Sensors map[string][]PersistentEnvironmentSensor `json:"sensors,omitempty"` } type PersistentEnvironmentSensor struct { Value float64 `json:"value"` Location string `json:"location"` - Name string `json:"name"` + Name string `json:"name,omitempty"` LastChange int64 `json:"lastchange"` }