Skip to content

Commit

Permalink
refactor(agent,linux,preferences,command): ⬆️ update github.com/joshu…
Browse files Browse the repository at this point in the history
…ar/go-hass-anything to v12 and implement required changes

- implement new builder methods for creating MQTT entities
- switch webcam controller from image to camera entity
  • Loading branch information
joshuar committed Oct 13, 2024
1 parent c24d8d4 commit f81430a
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 497 deletions.
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/goreleaser/nfpm/v2 v2.40.0
github.com/grandcat/zeroconf v1.0.0
github.com/iancoleman/strcase v0.3.0
github.com/joshuar/go-hass-anything/v11 v11.1.0
github.com/joshuar/go-hass-anything/v12 v12.0.0
github.com/lthibault/jitterbug/v2 v2.2.2
github.com/lxzan/gws v1.8.8
github.com/magefile/mage v1.15.0
Expand All @@ -21,7 +21,7 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.9.0
github.com/yassinebenaid/godump v0.10.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/text v0.19.0
golang.org/x/tools v0.26.0
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -95,7 +95,6 @@ require (
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
gitlab.com/digitalxero/go-conventional-commit v1.0.7 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand All @@ -106,6 +105,7 @@ require (
fyne.io/systray v1.11.0 // indirect
github.com/adrg/xdg v0.5.0
github.com/alecthomas/kong v1.2.1
github.com/blackjack/webcam v0.6.1
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/fredbi/uri v1.1.0 // indirect
Expand Down Expand Up @@ -142,7 +142,6 @@ require (
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect
github.com/tklauser/go-sysconf v0.3.14
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/vladimirvivien/go4vl v0.0.5
github.com/yuin/goldmark v1.7.1 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/image v0.18.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/blackjack/webcam v0.6.1 h1:K0T6Q0zto23U99gNAa5q/hFoye6uGcKr2aE6hFoxVoE=
github.com/blackjack/webcam v0.6.1/go.mod h1:zs+RkUZzqpFPHPiwBZ6U5B34ZXXe9i+SiHLKnnukJuI=
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb h1:m935MPodAbYS46DG4pJSv7WO+VECIWUQ7OJYSoTrMh4=
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
Expand Down Expand Up @@ -347,8 +349,8 @@ github.com/jfreymuth/pulse v0.1.1/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZ
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/joshuar/go-hass-anything/v11 v11.1.0 h1:UhQk52C0yLwzvm/k1I7bq9N38lUC/xuzfroTZfTh32E=
github.com/joshuar/go-hass-anything/v11 v11.1.0/go.mod h1:qxIJmkorMRUBy067+rQ8hnrWBpGgGL74Kc33mcsr0h0=
github.com/joshuar/go-hass-anything/v12 v12.0.0 h1:/627Elwhj5hH9j7U4WbveFdq8s8zm58jSEEGfeljrAI=
github.com/joshuar/go-hass-anything/v12 v12.0.0/go.mod h1:P/G9hYsLGS8eSXoI/T+MTiWqLkN7/LgKlAyB0OjTZug=
github.com/jsimonetti/rtnetlink v1.4.2 h1:Df9w9TZ3npHTyDn0Ev9e1uzmN2odmXd0QX+J5GTEn90=
github.com/jsimonetti/rtnetlink v1.4.2/go.mod h1:92s6LJdE+1iOrw+F2/RO7LYI2Qd8pPpFNNUYW06gcoM=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -525,8 +527,6 @@ github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYg
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/vladimirvivien/go4vl v0.0.5 h1:jHuo/CZOAzYGzrSMOc7anOMNDr03uWH5c1B5kQ+Chnc=
github.com/vladimirvivien/go4vl v0.0.5/go.mod h1:FP+/fG/X1DUdbZl9uN+l33vId1QneVn+W80JMc17OL8=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
Expand Down Expand Up @@ -590,8 +590,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/agent_mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/agent/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package agent
import (
"context"

mqtthass "github.com/joshuar/go-hass-anything/v11/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v11/pkg/mqtt"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"

"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/preferences"
Expand Down
4 changes: 2 additions & 2 deletions internal/agent/mqtt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/adrg/xdg"

mqtthass "github.com/joshuar/go-hass-anything/v11/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v11/pkg/mqtt"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"

"github.com/joshuar/go-hass-agent/internal/commands"
"github.com/joshuar/go-hass-agent/internal/logging"
Expand Down
24 changes: 15 additions & 9 deletions internal/agent/mqtt_controller_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"context"
"log/slog"

mqtthass "github.com/joshuar/go-hass-anything/v11/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v11/pkg/mqtt"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"

"github.com/joshuar/go-hass-agent/internal/linux"
"github.com/joshuar/go-hass-agent/internal/linux/media"
Expand All @@ -26,22 +26,28 @@ type mqttWorker struct {
numbers []*mqtthass.NumberEntity[int]
switches []*mqtthass.SwitchEntity
controls []*mqttapi.Subscription
binarySensors []*mqtthass.BinarySensorEntity
cameras []*mqtthass.ImageEntity
binarySensors []*mqtthass.SensorEntity
cameras []*mqtthass.CameraEntity
}

type linuxMQTTController struct {
*mqttWorker
logger *slog.Logger
}

// entity is a convienience interface to avoid duplicating a lot of loop content
// stateEntity is a convienience interface to avoid duplicating a lot of loop content
// when configuring the controller.
type entity interface {
MarshalSubscription() (*mqttapi.Subscription, error)
type stateEntity interface {
MarshalConfig() (*mqttapi.Msg, error)
}

// commandEntity is a convienience interface to avoid duplicating a lot of loop content
// when configuring the controller.
type commandEntity interface {
stateEntity
MarshalSubscription() (*mqttapi.Subscription, error)
}

func (c *linuxMQTTController) Subscriptions() []*mqttapi.Subscription {
totalLength := len(c.buttons) + len(c.numbers) + len(c.switches) + len(c.cameras)
subs := make([]*mqttapi.Subscription, 0, totalLength)
Expand Down Expand Up @@ -102,7 +108,7 @@ func (c *linuxMQTTController) Msgs() chan *mqttapi.Msg {

// generateConfig is a helper function to avoid duplicate code around generating
// an entity subscription.
func (c *linuxMQTTController) generateSubscription(e entity) *mqttapi.Subscription {
func (c *linuxMQTTController) generateSubscription(e commandEntity) *mqttapi.Subscription {
sub, err := e.MarshalSubscription()
if err != nil {
c.logger.Warn("Could not create subscription.", slog.Any("error", err))
Expand All @@ -115,7 +121,7 @@ func (c *linuxMQTTController) generateSubscription(e entity) *mqttapi.Subscripti

// generateConfig is a helper function to avoid duplicate code around generating
// an entity config.
func (c *linuxMQTTController) generateConfig(e entity) *mqttapi.Msg {
func (c *linuxMQTTController) generateConfig(e stateEntity) *mqttapi.Msg {
cfg, err := e.MarshalConfig()
if err != nil {
c.logger.Warn("Could not create config.", slog.Any("error", err.Error()))
Expand Down
106 changes: 72 additions & 34 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/pelletier/go-toml/v2"
"golang.org/x/exp/constraints"

mqtthass "github.com/joshuar/go-hass-anything/v11/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v11/pkg/mqtt"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"

"github.com/joshuar/go-hass-agent/internal/logging"
"github.com/joshuar/go-hass-agent/internal/preferences"
Expand Down Expand Up @@ -240,12 +240,17 @@ func (d *Controller) generateButtons(buttonCmds []Command) {
}

entities = append(entities,
mqtthass.AsButton(
mqtthass.NewEntity(preferences.AppName, name, id).
WithOriginInfo(preferences.MQTTOrigin()).
WithDeviceInfo(d.device).
WithIcon(icon).
WithCommandCallback(callback)))
mqtthass.NewButtonEntity().
WithDetails(
mqtthass.App(preferences.AppName),
mqtthass.Name(name),
mqtthass.ID(id),
mqtthass.OriginInfo(preferences.MQTTOrigin()),
mqtthass.DeviceInfo(d.device),
mqtthass.Icon(icon),
).WithCommand(
mqtthass.CommandCallback(callback),
))
}

d.buttons = entities
Expand Down Expand Up @@ -282,14 +287,22 @@ func (d *Controller) generateSwitches(switchCmds []Command) {
}

entities = append(entities,
mqtthass.AsSwitch(
mqtthass.NewEntity(preferences.AppName, name, id).
WithOriginInfo(preferences.MQTTOrigin()).
WithDeviceInfo(d.device).
WithIcon(icon).
WithStateCallback(stateCallBack).
WithCommandCallback(cmdCallBack),
true))
mqtthass.NewSwitchEntity().
WithDetails(
mqtthass.App(preferences.AppName),
mqtthass.Name(name),
mqtthass.ID(id),
mqtthass.OriginInfo(preferences.MQTTOrigin()),
mqtthass.DeviceInfo(d.device),
mqtthass.Icon(icon),
).
WithCommand(
mqtthass.CommandCallback(cmdCallBack),
).
WithState(
mqtthass.StateCallback(stateCallBack),
).
OptimisticMode())
}

d.switches = entities
Expand Down Expand Up @@ -367,15 +380,28 @@ func (d *Controller) generateNumbers(numberCommands []Command) {
}

floats = append(floats,
mqtthass.AsNumber(
mqtthass.NewEntity(preferences.AppName, name, id).
WithOriginInfo(preferences.MQTTOrigin()).
WithDeviceInfo(d.device).
WithIcon(icon).
WithStateCallback(stateCallBack).
WithCommandCallback(cmdCallBack).
WithValueTemplate(stateValueTemplate),
step, min, max, displayType))
mqtthass.NewNumberEntity[float64]().
WithDetails(
mqtthass.App(preferences.AppName),
mqtthass.Name(name),
mqtthass.ID(id),
mqtthass.OriginInfo(preferences.MQTTOrigin()),
mqtthass.DeviceInfo(d.device),
mqtthass.Icon(icon),
).
WithCommand(
mqtthass.CommandCallback(cmdCallBack),
).
WithState(
mqtthass.StateCallback(stateCallBack),
mqtthass.ValueTemplate(stateValueTemplate),
).
WithMode(displayType).
WithStep(step).
WithMin(min).
WithMax(max).
OptimisticMode())

default:
min := convValue[int](cmd.Min) //nolint:predeclared

Expand All @@ -390,15 +416,27 @@ func (d *Controller) generateNumbers(numberCommands []Command) {
}

ints = append(ints,
mqtthass.AsNumber(
mqtthass.NewEntity(preferences.AppName, name, id).
WithOriginInfo(preferences.MQTTOrigin()).
WithDeviceInfo(d.device).
WithIcon(icon).
WithStateCallback(stateCallBack).
WithCommandCallback(cmdCallBack).
WithValueTemplate(stateValueTemplate),
step, min, max, displayType))
mqtthass.NewNumberEntity[int]().
WithDetails(
mqtthass.App(preferences.AppName),
mqtthass.Name(name),
mqtthass.ID(id),
mqtthass.OriginInfo(preferences.MQTTOrigin()),
mqtthass.DeviceInfo(d.device),
mqtthass.Icon(icon),
).
WithCommand(
mqtthass.CommandCallback(cmdCallBack),
).
WithState(
mqtthass.StateCallback(stateCallBack),
mqtthass.ValueTemplate(stateValueTemplate),
).
WithMode(displayType).
WithStep(step).
WithMin(min).
WithMax(max).
OptimisticMode())
}
}

Expand Down
63 changes: 43 additions & 20 deletions internal/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,55 @@ import (
"github.com/go-test/deep"
"github.com/stretchr/testify/require"

mqtthass "github.com/joshuar/go-hass-anything/v11/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v11/pkg/mqtt"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"
)

var mockCommandCallback = func(_ *paho.Publish) {}

var mockButton = mqtthass.AsButton(
mqtthass.NewEntity("test", "test button", "test_button").
WithOriginInfo(&mqtthass.Origin{}).
WithDeviceInfo(&mqtthass.Device{}).
WithIcon("mdi:test").
WithCommandCallback(mockCommandCallback))
var mockButton = mqtthass.NewButtonEntity().
WithDetails(
mqtthass.App("test"),
mqtthass.Name("test button"),
mqtthass.ID("test_button"),
mqtthass.OriginInfo(&mqtthass.Origin{}),
mqtthass.DeviceInfo(&mqtthass.Device{}),
mqtthass.Icon("mdi:test"),
).
WithCommand(
mqtthass.CommandCallback(mockCommandCallback),
)

var mockSwitch = mqtthass.AsSwitch(
mqtthass.NewEntity("test", "test switch", "test_switch").
WithOriginInfo(&mqtthass.Origin{}).
WithDeviceInfo(&mqtthass.Device{}).
WithIcon("mdi:test").
WithCommandCallback(mockCommandCallback), true)
var mockSwitch = mqtthass.NewSwitchEntity().
WithDetails(
mqtthass.App("test"),
mqtthass.Name("test switch"),
mqtthass.ID("test_switch"),
mqtthass.OriginInfo(&mqtthass.Origin{}),
mqtthass.DeviceInfo(&mqtthass.Device{}),
mqtthass.Icon("mdi:test"),
).
WithCommand(
mqtthass.CommandCallback(mockCommandCallback),
).
OptimisticMode()

var mockNumber = mqtthass.AsNumber(
mqtthass.NewEntity("test", "test number", "test_number").
WithOriginInfo(&mqtthass.Origin{}).
WithDeviceInfo(&mqtthass.Device{}).
WithIcon("mdi:test").
WithCommandCallback(mockCommandCallback), 0, 100, 1, mqtthass.NumberAuto)
var mockNumber = mqtthass.NewNumberEntity[int]().
WithDetails(
mqtthass.App("test"),
mqtthass.Name("test number"),
mqtthass.ID("test_number"),
mqtthass.OriginInfo(&mqtthass.Origin{}),
mqtthass.DeviceInfo(&mqtthass.Device{}),
mqtthass.Icon("mdi:test"),
).
WithCommand(
mqtthass.CommandCallback(mockCommandCallback),
).
WithStep(1).
WithMin(0).
WithMax(100).
WithMode(mqtthass.NumberAuto)

func TestController_Subscriptions(t *testing.T) {
var mockButtonSubscription, mockSwitchSubscription, mockNumberSubscription *mqttapi.Subscription
Expand Down
Loading

0 comments on commit f81430a

Please sign in to comment.