Skip to content

Commit 5d3f531

Browse files
-quiet flag
1 parent 0c1a7ef commit 5d3f531

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ systemctl --user enable --now hacompanion
9898
# journalctl --user -xlf -u hacompanion
9999
```
100100

101+
## Controlling the output
102+
103+
By default, the companion will log all sent and received messages to the console.
104+
105+
You can reduce the output by using the `-quiet` flag:
106+
107+
```bash
108+
hacompanion -quiet
109+
```
110+
101111
## Custom scripts
102112

103113
You can add any number of custom scripts in your configuration file.

api/api.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,23 @@ type API struct {
127127
DeviceName string
128128
client http.Client
129129
Registration Registration
130+
quiet bool
130131
}
131132

132-
func NewAPI(host, token string, deviceName string) *API {
133+
func NewAPI(host, token, deviceName string, quiet bool) *API {
133134
return &API{
134135
Host: host,
135136
Token: token,
136137
DeviceName: deviceName,
137138
client: http.Client{Timeout: 5 * time.Second},
139+
quiet: quiet,
138140
}
139141
}
140142

141143
func (api *API) sendRequest(ctx context.Context, url string, payload []byte) ([]byte, error) {
142-
log.Printf("sending to %s: %+v", url, string(payload))
144+
if !api.quiet {
145+
log.Printf("sending to %s: %+v", url, string(payload))
146+
}
143147
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(payload))
144148
if err != nil {
145149
return nil, err
@@ -157,7 +161,9 @@ func (api *API) sendRequest(ctx context.Context, url string, payload []byte) ([]
157161
if resp.StatusCode >= 400 {
158162
return nil, fmt.Errorf("received invalid status code %d (%s)", resp.StatusCode, body)
159163
}
160-
log.Printf("received %s", string(body))
164+
if !api.quiet {
165+
log.Printf("received %s", string(body))
166+
}
161167
return body, nil
162168
}
163169

entity/sensor.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Sensor struct {
4848
Name string
4949
UniqueID string
5050
Unit string
51+
QuietOutput bool
5152
}
5253

5354
func (s Sensor) String() string {
@@ -62,14 +63,18 @@ func (s Sensor) Update(ctx context.Context, wg *sync.WaitGroup, outputs *Outputs
6263
log.Printf("failed to run sensor %s: %s", s, err)
6364
return
6465
}
65-
log.Printf("received Payload for %s: %+v", s.UniqueID, value)
66+
if !s.QuietOutput {
67+
log.Printf("received Payload for %s: %+v", s.UniqueID, value)
68+
}
6669
outputs.Add(Output{Sensor: s, Payload: value})
6770
}
6871

6972
// Invalidate sets the state of the given sensor(s) to unavailable.
7073
func (s Sensor) Invalidate(outputs *Outputs) {
7174
p := NewPayload()
7275
p.State = "unavailable"
73-
log.Printf("received p for %s: %+v", s.UniqueID, p)
76+
if !s.QuietOutput {
77+
log.Printf("received p for %s: %+v", s.UniqueID, p)
78+
}
7479
outputs.Add(Output{Sensor: s, Payload: p})
7580
}

main.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ func main() {
5151
var hassHost string
5252
var deviceName string
5353
var configFlag string
54+
var quiet bool
5455
flag.StringVar(&configFlag, "config", "~/.config/hacompanion.toml", "Path to the config file")
5556
flag.StringVar(&hassHost, "host", "", "Home Assistant host")
5657
flag.StringVar(&hassToken, "token", "", "Long-lived access token")
5758
flag.StringVar(&deviceName, "device-name", "", "Device name")
59+
flag.BoolVar(&quiet, "quiet", false, "Disable verbose logging")
5860
flag.Parse()
5961

6062
configFile, err := util.NewHomePath(configFlag)
@@ -136,12 +138,12 @@ func main() {
136138
// Build the application kernel.
137139
k := Kernel{
138140
config: &config,
139-
api: api.NewAPI(hassHost, hassToken, deviceName),
141+
api: api.NewAPI(hassHost, hassToken, deviceName, quiet),
140142
}
141143

142144
// Start the main process.
143145
go func() {
144-
err = k.Run(context.Background())
146+
err = k.Run(context.Background(), quiet)
145147
if err != nil {
146148
log.Fatalf("failed to start application: %s", err)
147149
}
@@ -166,7 +168,7 @@ func main() {
166168
}
167169

168170
// Run runs the application.
169-
func (k *Kernel) Run(appCtx context.Context) error {
171+
func (k *Kernel) Run(appCtx context.Context, quiet bool) error {
170172
log.Printf("Starting Desktop Companion version %s", Version)
171173
// Create a global application context that is later used for proper shutdowns.
172174
ctx, cancel := context.WithCancel(appCtx)
@@ -190,7 +192,7 @@ func (k *Kernel) Run(appCtx context.Context) error {
190192
}
191193

192194
// Parse out all sensors from the config file and register them in Home Assistant.
193-
sensors, err := k.buildSensors(k.config)
195+
sensors, err := k.buildSensors(k.config, quiet)
194196
if err != nil {
195197
return fmt.Errorf("failed to build sensors from config: %w", err)
196198
}
@@ -259,7 +261,7 @@ func (k *Kernel) Shutdown(ctx context.Context) error {
259261
}
260262

261263
// buildSensors returns a slice of concrete Sensor types based on the configuration.
262-
func (k *Kernel) buildSensors(config *Config) ([]entity.Sensor, error) {
264+
func (k *Kernel) buildSensors(config *Config, quiet bool) ([]entity.Sensor, error) {
263265
var sensors []entity.Sensor
264266
// Parse default sensor configuration.
265267
for key, sensorConfig := range config.Sensors {
@@ -279,6 +281,7 @@ func (k *Kernel) buildSensors(config *Config) ([]entity.Sensor, error) {
279281
DeviceClass: data.DeviceClass,
280282
Icon: data.Icon,
281283
Unit: data.Unit,
284+
QuietOutput: quiet,
282285
})
283286
}
284287
// Parse custom scripts.

0 commit comments

Comments
 (0)