Handle sensors that don't have a name, just a location

This commit is contained in:
Bendodroid 2024-01-14 21:54:01 +01:00
parent 04b7efd74a
commit 0241a506d4
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
3 changed files with 20 additions and 9 deletions

View file

@ -22,6 +22,16 @@
"allowed_credentials": [ "allowed_credentials": [
"home-assistant" "home-assistant"
] ]
},
{
"sensor_data": {
"unit": "C",
"location": "Hauptraum",
"description": "Sensor im Hauptraum"
},
"allowed_credentials": [
"home-assistant"
]
} }
], ],
"humidity": [ "humidity": [

View file

@ -42,10 +42,11 @@ func main() {
// Register handlers for Environmental Sensors // Register handlers for Environmental Sensors
for key, envSensorConfigs := range conf.Dynamic.Sensors { for key, envSensorConfigs := range conf.Dynamic.Sensors {
for i, envSensorConfig := range envSensorConfigs { for i, envSensorConfig := range envSensorConfigs {
http.HandleFunc( pattern := fmt.Sprintf("/sensors/%s/%s", key, envSensorConfig.SensorData.Location)
strings.ToLower(fmt.Sprintf( if envSensorConfig.SensorData.Name != "" {
"/sensors/%s/%s/%s", key, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name, pattern += "/" + envSensorConfig.SensorData.Name
)), }
http.HandleFunc(strings.ToLower(pattern),
handlers.EnvironmentSensor( handlers.EnvironmentSensor(
conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[key][i], conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[key][i],
), ),

View file

@ -46,9 +46,9 @@ type EnvironmentSensor struct {
Value float64 `json:"value"` Value float64 `json:"value"`
Unit string `json:"unit"` Unit string `json:"unit"`
Location string `json:"location"` Location string `json:"location"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Description string `json:"description"` Description string `json:"description,omitempty"`
LastChange int64 `json:"lastchange"` LastChange int64 `json:"lastchange,omitempty"`
} }
type PersistentStateV14 struct { type PersistentStateV14 struct {
@ -56,12 +56,12 @@ type PersistentStateV14 struct {
Open bool `json:"open"` Open bool `json:"open"`
LastChange int64 `json:"lastchange"` LastChange int64 `json:"lastchange"`
} `json:"state"` } `json:"state"`
Sensors map[string][]PersistentEnvironmentSensor `json:"sensors"` Sensors map[string][]PersistentEnvironmentSensor `json:"sensors,omitempty"`
} }
type PersistentEnvironmentSensor struct { type PersistentEnvironmentSensor struct {
Value float64 `json:"value"` Value float64 `json:"value"`
Location string `json:"location"` Location string `json:"location"`
Name string `json:"name"` Name string `json:"name,omitempty"`
LastChange int64 `json:"lastchange"` LastChange int64 `json:"lastchange"`
} }