Files
2026-07-16 16:32:44 -04:00

80 lines
3.6 KiB
Go

// Package config contains ServiceConfig, which is intended
// to extend go-app/pkg/config.AppConfig. Add any custom fields
// here, and it will automatically be handled by go-app, including
// overrides by environment, and json schema generation
package config
import (
"time"
"github.com/caarlos0/env/v11"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
)
// DefaultPollInterval is used when PollInterval is unset. Each tick re-fetches
// device state and daily energy/water usage over REST.
const DefaultPollInterval = 1 * time.Minute
type ServiceConfig struct {
// Rheem EcoNet cloud credentials. Prefer supplying the password via
// the ECONET_PASSWORD environment variable rather than config.yaml.
EconetEmail string `yaml:"econetEmail" json:"econetEmail,omitempty" env:"ECONET_EMAIL"`
EconetPassword string `yaml:"econetPassword" json:"econetPassword,omitempty" env:"ECONET_PASSWORD"`
// Necessary due to ancient TLS certificate run by cloudblade that isn't trusted in modern ca-certificate bundles
EconetTLSInsecure bool `yaml:"econetTLSInsecure" json:"econetTLSInsecure,omitempty" env:"ECONET_TLS_INSECURE" default:"false"`
// CostPerKWH is the electricity price in US dollars per kWh (e.g. 0.18
// means 18 cents/kWh, NOT 18). It derives the econet_energy_cost_dollars
// metric from kWh energy usage.
CostPerKWH float64 `yaml:"costPerKWH" json:"costPerKWH,omitempty" env:"ECONET_COST_PER_KWH" default:"0.19"`
// PollInterval controls how often device state and daily energy/water
// usage are re-fetched over REST.
PollInterval time.Duration `yaml:"pollInterval" json:"pollInterval,omitempty" env:"ECONET_POLL_INTERVAL"`
// EnableWebUI toggles the admin UI served at /. It defaults to enabled:
// a pointer so an unset value (nil) is distinguishable from an explicit
// false, which is what makes the default actually true at runtime —
// LoadEnv's plain env.Parse does not apply the `default` tag (that tag
// only feeds the JSON schema). Use WebUIEnabled to read it.
EnableWebUI *bool `yaml:"enableWebUI" json:"enableWebUI,omitempty" env:"ECONET_ENABLE_WEB_UI" default:"true"`
// DisableWaterUsage skips the per-device water-usage REST call on each
// refresh. Handy for units that don't report water usage, where the call is
// wasted work and just produces debug-log noise. Zero value (false) keeps
// water usage enabled, so unlike EnableWebUI this needs no pointer.
DisableWaterUsage bool `yaml:"disableWaterUsage" json:"disableWaterUsage,omitempty" env:"ECONET_DISABLE_WATER_USAGE" default:"false"`
// Embeds go-app config, used by go-app to
// merge custom config into go-app config, and to produce
// a complete configuration json schema
*config.AppConfig
}
// LoadEnv applies environment-variable overrides to the custom ServiceConfig
// fields. go-app's MustLoadConfigInto only handles env for the embedded
// AppConfig, so the caller must apply env for custom fields. The embedded
// AppConfig is skipped here to preserve go-app's own env/yaml precedence.
func (c *ServiceConfig) LoadEnv() error {
appConfig := c.AppConfig
c.AppConfig = nil
defer func() { c.AppConfig = appConfig }()
return env.Parse(c)
}
// WebUIEnabled reports whether the admin UI should be served. Unset defaults to
// true; set enableWebUI: false (or ECONET_ENABLE_WEB_UI=false) to disable it.
func (c *ServiceConfig) WebUIEnabled() bool {
return c.EnableWebUI == nil || *c.EnableWebUI
}
// GetPollInterval returns the configured poll interval or the default.
func (c *ServiceConfig) GetPollInterval() time.Duration {
if c.PollInterval <= 0 {
return DefaultPollInterval
}
return c.PollInterval
}