parent
9ffebaab02
commit
dbf76af86a
|
@ -29,7 +29,7 @@ const (
|
||||||
func Listen(parentLogger *slog.Logger) {
|
func Listen(parentLogger *slog.Logger) {
|
||||||
logger = parentLogger
|
logger = parentLogger
|
||||||
cfg = config.FromEnvironment()
|
cfg = config.FromEnvironment()
|
||||||
haClient = homeassistant.NewRestClient(cfg.HomeAssistantBaseUrl, cfg.HomeAssistantToken)
|
haClient = homeassistant.NewRestClient(cfg.GetHomeAssistantBaseUrl(), cfg.HomeAssistantToken)
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
|
|
||||||
router.Use(middleware.RequestID)
|
router.Use(middleware.RequestID)
|
||||||
|
|
|
@ -42,7 +42,7 @@ func Subscribe(parentLogger *slog.Logger) error {
|
||||||
cfg = config.FromEnvironment()
|
cfg = config.FromEnvironment()
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
url := fmt.Sprintf(cfg.HomeAssistantWebsocketUrl)
|
url := cfg.GetHomeAssistantWebsocketUrl()
|
||||||
|
|
||||||
logger.Debug("Dialing Home Assistant websocket API", "url", url)
|
logger.Debug("Dialing Home Assistant websocket API", "url", url)
|
||||||
|
|
||||||
|
|
5
main.go
5
main.go
|
@ -9,19 +9,22 @@ import (
|
||||||
"code.jhot.me/jhot/hats/internal/api"
|
"code.jhot.me/jhot/hats/internal/api"
|
||||||
"code.jhot.me/jhot/hats/internal/homeassistant"
|
"code.jhot.me/jhot/hats/internal/homeassistant"
|
||||||
"code.jhot.me/jhot/hats/internal/nats"
|
"code.jhot.me/jhot/hats/internal/nats"
|
||||||
|
"code.jhot.me/jhot/hats/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
interrupt chan os.Signal
|
interrupt chan os.Signal
|
||||||
|
cfg *config.HatsConfig
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
cfg = config.FromEnvironment()
|
||||||
ctx, cancel = context.WithCancel(context.Background())
|
ctx, cancel = context.WithCancel(context.Background())
|
||||||
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
Level: slog.LevelDebug,
|
Level: cfg.GetLogLevel(),
|
||||||
}))
|
}))
|
||||||
interrupt = make(chan os.Signal, 1)
|
interrupt = make(chan os.Signal, 1)
|
||||||
signal.Notify(interrupt, os.Interrupt)
|
signal.Notify(interrupt, os.Interrupt)
|
||||||
|
|
|
@ -64,7 +64,7 @@ func (c *HatsClient) GetStateBool(entityId string) (bool, error) {
|
||||||
return ha.StateToBool(stateString), nil
|
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()
|
req := c.client.R()
|
||||||
if len(extras) > 0 {
|
if len(extras) > 0 {
|
||||||
data := map[string]interface{}{}
|
data := map[string]interface{}{}
|
||||||
|
@ -101,6 +101,11 @@ func (c *HatsClient) GetTimer(name string) (string, error) {
|
||||||
return resp.String(), nil
|
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) {
|
func (c *HatsClient) SetTimer(name string, duration string, force bool) (string, error) {
|
||||||
data := api.CreateTimerData{
|
data := api.CreateTimerData{
|
||||||
Duration: duration,
|
Duration: duration,
|
||||||
|
|
|
@ -2,33 +2,34 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.jhot.me/jhot/hats/internal/util"
|
"code.jhot.me/jhot/hats/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HatsConfig struct {
|
type HatsConfig struct {
|
||||||
|
LogLevl string
|
||||||
|
|
||||||
HomeAssistantHost string
|
HomeAssistantHost string
|
||||||
HomeAssistantPort string
|
HomeAssistantPort string
|
||||||
HomeAssistantSecure bool
|
HomeAssistantSecure bool
|
||||||
HomeAssistantBaseUrl string
|
|
||||||
HomeAssistantWebsocketUrl string
|
|
||||||
HomeAssistantToken string
|
HomeAssistantToken string
|
||||||
|
|
||||||
NatsHost string
|
NatsHost string
|
||||||
NatsPort string
|
NatsPort string
|
||||||
NatsBaseUrl string
|
|
||||||
NatsToken string
|
NatsToken string
|
||||||
NatsClientName string
|
NatsClientName string
|
||||||
|
|
||||||
HatsHost string
|
HatsHost string
|
||||||
HatsPort string
|
HatsPort string
|
||||||
HatsSecure bool
|
HatsSecure bool
|
||||||
HatsBaseUrl string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromEnvironment() *HatsConfig {
|
func FromEnvironment() *HatsConfig {
|
||||||
config := &HatsConfig{
|
config := &HatsConfig{
|
||||||
|
LogLevl: util.GetEnvWithDefault("LOG_LEVEL", "INFO"),
|
||||||
HomeAssistantHost: util.GetEnvWithDefault("HASS_HOST", "127.0.0.1"),
|
HomeAssistantHost: util.GetEnvWithDefault("HASS_HOST", "127.0.0.1"),
|
||||||
HomeAssistantPort: util.GetEnvWithDefault("HASS_PORT", "8123"),
|
HomeAssistantPort: util.GetEnvWithDefault("HASS_PORT", "8123"),
|
||||||
HomeAssistantToken: util.GetEnvWithDefault("HASS_TOKEN", ""),
|
HomeAssistantToken: util.GetEnvWithDefault("HASS_TOKEN", ""),
|
||||||
|
@ -41,23 +42,48 @@ func FromEnvironment() *HatsConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
config.HomeAssistantSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HASS_SECURE", "false"))
|
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"))
|
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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue