From 92be2e1392354096b9e892827a5c8a36ed32fb99 Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Thu, 25 Apr 2024 09:17:42 +1000 Subject: [PATCH] feat(device): :sparkles: add an agent version sensor --- internal/agent/runners.go | 2 +- internal/device/version.go | 66 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 internal/device/version.go diff --git a/internal/agent/runners.go b/internal/agent/runners.go index bf8305725..05e20dda1 100644 --- a/internal/agent/runners.go +++ b/internal/agent/runners.go @@ -25,7 +25,7 @@ import ( // for this device. func runWorkers(ctx context.Context, trk SensorTracker, reg sensor.Registry) { workerFuncs := sensorWorkers() - workerFuncs = append(workerFuncs, device.ExternalIPUpdater) + workerFuncs = append(workerFuncs, device.ExternalIPUpdater, device.VersionUpdater) var wg sync.WaitGroup var outCh []<-chan sensor.Details diff --git a/internal/device/version.go b/internal/device/version.go new file mode 100644 index 000000000..a8973e106 --- /dev/null +++ b/internal/device/version.go @@ -0,0 +1,66 @@ +// Copyright (c) 2024 Joshua Rich +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +package device + +import ( + "context" + + "github.com/joshuar/go-hass-agent/internal/hass/sensor" + "github.com/joshuar/go-hass-agent/internal/hass/sensor/types" + "github.com/joshuar/go-hass-agent/internal/preferences" +) + +type version string + +func (v *version) Name() string { + return "Go Hass Agent Version" +} + +func (v *version) ID() string { + return "agent_version" +} + +func (v *version) Icon() string { + return "mdi:face-agent" +} + +func (v *version) SensorType() types.SensorClass { + return types.Sensor +} + +func (v *version) DeviceClass() types.DeviceClass { + return 0 +} + +func (v *version) StateClass() types.StateClass { + return 0 +} + +func (v *version) State() any { + return preferences.AppVersion +} + +func (v *version) Units() string { + return "" +} + +func (v *version) Category() string { + return "diagnostic" +} + +func (v *version) Attributes() any { + return nil +} + +func VersionUpdater(_ context.Context) chan sensor.Details { + sensorCh := make(chan sensor.Details) + v := new(version) + go func() { + defer close(sensorCh) + sensorCh <- v + }() + return sensorCh +}