1
0
Fork 0

Get state float

main v0.15.0
Jordan Hotmann 2023-12-04 11:11:33 -07:00
parent 85de9f6d5c
commit 64f800fdd0
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
2 changed files with 16 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package client
import ( import (
"fmt" "fmt"
"strconv"
"code.jhot.me/jhot/hats/internal/api" "code.jhot.me/jhot/hats/internal/api"
ha "code.jhot.me/jhot/hats/pkg/homeassistant" ha "code.jhot.me/jhot/hats/pkg/homeassistant"
@ -68,6 +69,17 @@ func (c *HatsClient) GetStateBool(entityId string) (bool, error) {
return ha.StateToBool(stateString), nil return ha.StateToBool(stateString), nil
} }
func (c *HatsClient) GetStateFloat(entityId string) (float64, error) {
stateString, err := c.GetState(entityId)
if err != nil {
return 0.0, err
}
parsed, err := strconv.ParseFloat(stateString, 64)
return parsed, err
}
func (c *HatsClient) CallService(entityId string, service string, extras ...map[string]any) error { func (c *HatsClient) CallService(entityId string, service string, extras ...map[string]any) error {
req := c.client.R() req := c.client.R()
if len(extras) > 0 { if len(extras) > 0 {

View File

@ -29,6 +29,8 @@ type HatsConfig struct {
NtfyHost string NtfyHost string
NtfyToken string NtfyToken string
ConfigDir string
} }
func FromEnvironment() *HatsConfig { func FromEnvironment() *HatsConfig {
@ -50,6 +52,8 @@ func FromEnvironment() *HatsConfig {
NtfyHost: util.GetEnvWithDefault("NTFY_HOST", "https://ntfy.sh"), NtfyHost: util.GetEnvWithDefault("NTFY_HOST", "https://ntfy.sh"),
NtfyToken: util.GetEnvWithDefault("NTFY_TOKEN", ""), NtfyToken: util.GetEnvWithDefault("NTFY_TOKEN", ""),
ConfigDir: util.GetEnvWithDefault("CONFIG_DIR", "/config"),
} }
config.HomeAssistantSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HASS_SECURE", "false")) config.HomeAssistantSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HASS_SECURE", "false"))