- Added detailed comments to clarify the purpose of various functions and types in the Discord bot and HA gateway. - Introduced new methods in the CommandApp for handling light and switch operations, including HandleLightOn, HandleLightOff, HandleLightToggle, and their respective autocomplete functions. - Updated the HAClient interface to include methods for fetching states and calling services, enhancing the interaction with Home Assistant. - Improved the structure of entity and light domain models to include additional attributes and clearer documentation. - Implemented logging enhancements in both the Discord bot and HA gateway to ensure better traceability and context in logs. - Refactored the configuration loading process to streamline environment variable handling and defaults. - Stubbed out switch control methods in the gRPC adapter, indicating future implementation plans. - Enhanced telemetry setup to ensure proper initialization and shutdown procedures for observability.
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
)
|
|
|
|
// Config holds runtime configuration for the Home Assistant gRPC gateway.
|
|
type Config struct {
|
|
GRPCPort string // GRPC_PORT, default "50051"
|
|
HABaseURL string // HA_BASE_URL, e.g. "http://ha.home.arpa:8123"
|
|
HAToken string // HA_TOKEN — long-lived access token (required)
|
|
OTELEndpoint string // OTEL_ENDPOINT, e.g. "otel-collector.monitoring.svc:4317"
|
|
LogLevel string // LOG_LEVEL, default "info"
|
|
LogFormat string // LOG_FORMAT, default "json"
|
|
// empty = telemetry disabled (local dev default)
|
|
}
|
|
|
|
// Load reads configuration from environment variables and applies defaults.
|
|
func Load() (*Config, error) {
|
|
token := os.Getenv("HA_TOKEN")
|
|
if token == "" {
|
|
return nil, errors.New("HA_TOKEN is required but not set")
|
|
}
|
|
|
|
port := os.Getenv("GRPC_PORT")
|
|
if port == "" {
|
|
port = "50051"
|
|
}
|
|
|
|
return &Config{
|
|
GRPCPort: port,
|
|
HABaseURL: os.Getenv("HA_BASE_URL"),
|
|
HAToken: token,
|
|
OTELEndpoint: os.Getenv("OTEL_ENDPOINT"),
|
|
LogLevel: getenvDefault("LOG_LEVEL", "info"),
|
|
LogFormat: getenvDefault("LOG_FORMAT", "json"),
|
|
}, nil
|
|
}
|
|
|
|
// getenvDefault keeps config loading concise for optional variables with defaults.
|
|
func getenvDefault(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|