1
0
Fork 0

Config updates, accept any type in extra data

main v0.5.0
Jordan Hotmann 2023-10-17 13:58:34 -06:00
parent 9ffebaab02
commit dbf76af86a
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
5 changed files with 65 additions and 31 deletions

View File

@ -29,7 +29,7 @@ const (
func Listen(parentLogger *slog.Logger) {
logger = parentLogger
cfg = config.FromEnvironment()
haClient = homeassistant.NewRestClient(cfg.HomeAssistantBaseUrl, cfg.HomeAssistantToken)
haClient = homeassistant.NewRestClient(cfg.GetHomeAssistantBaseUrl(), cfg.HomeAssistantToken)
router := chi.NewRouter()
router.Use(middleware.RequestID)

View File

@ -42,7 +42,7 @@ func Subscribe(parentLogger *slog.Logger) error {
cfg = config.FromEnvironment()
var err error
url := fmt.Sprintf(cfg.HomeAssistantWebsocketUrl)
url := cfg.GetHomeAssistantWebsocketUrl()
logger.Debug("Dialing Home Assistant websocket API", "url", url)

View File

@ -9,19 +9,22 @@ import (
"code.jhot.me/jhot/hats/internal/api"
"code.jhot.me/jhot/hats/internal/homeassistant"
"code.jhot.me/jhot/hats/internal/nats"
"code.jhot.me/jhot/hats/pkg/config"
)
var (
interrupt chan os.Signal
cfg *config.HatsConfig
logger *slog.Logger
ctx context.Context
cancel context.CancelFunc
)
func main() {
cfg = config.FromEnvironment()
ctx, cancel = context.WithCancel(context.Background())
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
Level: cfg.GetLogLevel(),
}))
interrupt = make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

View File

@ -64,7 +64,7 @@ func (c *HatsClient) GetStateBool(entityId string) (bool, error) {
return ha.StateToBool(stateString), nil
}
func (c *HatsClient) CallService(entityId string, service string, extras ...map[string]string) error {
func (c *HatsClient) CallService(entityId string, service string, extras ...map[string]any) error {
req := c.client.R()
if len(extras) > 0 {
data := map[string]interface{}{}
@ -101,6 +101,11 @@ func (c *HatsClient) GetTimer(name string) (string, error) {
return resp.String(), nil
}
// Set a timer
//
// name: the name of the timer (should be unique)
// duration: time.Duration string for how long the timer should last
// force: if true, will start the timer over even if it is already running
func (c *HatsClient) SetTimer(name string, duration string, force bool) (string, error) {
data := api.CreateTimerData{
Duration: duration,

View File

@ -2,33 +2,34 @@ package config
import (
"fmt"
"log/slog"
"strconv"
"strings"
"code.jhot.me/jhot/hats/internal/util"
)
type HatsConfig struct {
HomeAssistantHost string
HomeAssistantPort string
HomeAssistantSecure bool
HomeAssistantBaseUrl string
HomeAssistantWebsocketUrl string
HomeAssistantToken string
LogLevl string
HomeAssistantHost string
HomeAssistantPort string
HomeAssistantSecure bool
HomeAssistantToken string
NatsHost string
NatsPort string
NatsBaseUrl string
NatsToken string
NatsClientName string
HatsHost string
HatsPort string
HatsSecure bool
HatsBaseUrl string
HatsHost string
HatsPort string
HatsSecure bool
}
func FromEnvironment() *HatsConfig {
config := &HatsConfig{
LogLevl: util.GetEnvWithDefault("LOG_LEVEL", "INFO"),
HomeAssistantHost: util.GetEnvWithDefault("HASS_HOST", "127.0.0.1"),
HomeAssistantPort: util.GetEnvWithDefault("HASS_PORT", "8123"),
HomeAssistantToken: util.GetEnvWithDefault("HASS_TOKEN", ""),
@ -41,23 +42,48 @@ func FromEnvironment() *HatsConfig {
}
config.HomeAssistantSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HASS_SECURE", "false"))
hassProtocol := "http"
HassWsProtocol := "ws"
if config.HomeAssistantSecure {
hassProtocol += "s"
HassWsProtocol += "s"
}
config.HomeAssistantBaseUrl = fmt.Sprintf("%s://%s:%s", hassProtocol, config.HomeAssistantHost, config.HomeAssistantPort)
config.HomeAssistantWebsocketUrl = fmt.Sprintf("%s://%s:%s/api/websocket", HassWsProtocol, config.HomeAssistantHost, config.HomeAssistantPort)
config.NatsBaseUrl = fmt.Sprintf("nats://%s:%s", config.NatsHost, config.NatsPort)
config.HatsSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HATS_SECURE", "false"))
hatsProtocol := "http"
if config.HatsSecure {
hatsProtocol += "s"
}
config.HatsBaseUrl = fmt.Sprintf("%s://%s:%s", hatsProtocol, config.HatsHost, config.HatsPort)
return config
}
func (c *HatsConfig) GetHomeAssistantBaseUrl() string {
hassProtocol := "http"
if c.HomeAssistantSecure {
hassProtocol += "s"
}
return fmt.Sprintf("%s://%s:%s", hassProtocol, c.HomeAssistantHost, c.HomeAssistantPort)
}
func (c *HatsConfig) GetHomeAssistantWebsocketUrl() string {
protocol := "ws"
if c.HomeAssistantSecure {
protocol += "s"
}
return fmt.Sprintf("%s://%s:%s/api/websocket", protocol, c.HomeAssistantHost, c.HomeAssistantPort)
}
func (c *HatsConfig) GetNatsBaseUrl() string {
return fmt.Sprintf("nats://%s:%s", c.NatsHost, c.NatsPort)
}
func (c *HatsConfig) GetHatsBaseUrl() string {
protocol := "http"
if c.HomeAssistantSecure {
protocol += "s"
}
return fmt.Sprintf("%s://%s:%s", protocol, c.HatsHost, c.HatsPort)
}
func (c *HatsConfig) GetLogLevel() slog.Level {
switch strings.ToLower(c.LogLevl) {
case "error":
return slog.LevelError
case "warn":
return slog.LevelWarn
case "debug":
return slog.LevelDebug
default:
return slog.LevelInfo
}
}