Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use TinyGo MQTT instead of Paho, currently broken. #53

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions chore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

case "${1}" in
"go")
go get -u
go mod tidy
git add go.mod go.sum
git commit -m "chore: Go update."
;;
"gen")
go run ./helpers --pull
git add devices/ config/
git commit -m "chore: Regenerate from upstream documentation."
;;
*)
echo "Unknown command: ${1}"
;;
esac


exit
19 changes: 7 additions & 12 deletions config/convert.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config

import (
"log"
// "log"
"time"

ExternalDevice "github.com/W-Floyd/ha-mqtt-iot/devices/externaldevice"
mqtt "github.com/eclipse/paho.mqtt.golang"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

func (c Config) Convert() ([]ExternalDevice.Device, *mqtt.ClientOptions) {
Expand All @@ -15,17 +15,12 @@ func (c Config) Convert() ([]ExternalDevice.Device, *mqtt.ClientOptions) {
opts.SetUsername(c.MQTT.Username)
opts.SetPassword(c.MQTT.Password)
opts.SetKeepAlive(30 * time.Second)
opts.SetDefaultPublishHandler(func(client mqtt.Client, msg mqtt.Message) {
log.Printf("TOPIC: %s\n", msg.Topic())
log.Printf("MSG: %s\n", msg.Payload())
})
// opts.SetDefaultPublishHandler(func(client mqtt.Client, msg mqtt.Message) {
// log.Printf("TOPIC: %s\n", msg.Topic())
// log.Printf("MSG: %s\n", msg.Payload())
// })
opts.SetPingTimeout(1 * time.Second)
opts.SetAutoReconnect(true)
opts.SetMaxReconnectInterval(10 * time.Second)
opts.SetConnectRetry(true)
opts.SetConnectRetryInterval(10 * time.Second)
opts.SetWriteTimeout(60 * time.Second)

// opts.SetAutoReconnect(true)
if c.MQTT.NodeId != "" {
ExternalDevice.NodeID = c.MQTT.NodeId
}
Expand Down
2 changes: 1 addition & 1 deletion devices/common/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/W-Floyd/ha-mqtt-iot/common"
mqtt "github.com/eclipse/paho.mqtt.golang"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

func ConstructCommandFunc(command []string) (f func(message mqtt.Message, connection mqtt.Client)) {
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/alarm_control_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/binary_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/climate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/device_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/fan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

func GetTopicPrefix(d Device) string {
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/humidifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/siren.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions devices/externaldevice/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -43,7 +43,7 @@ type Update struct {
LatestVersionTemplate *string `json:"latest_version_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) to extract the latest version value."
LatestVersionTopic *string `json:"latest_version_topic,omitempty"` // "The MQTT topic subscribed to receive an update of the latest version."
LatestVersionFunc func(mqtt.Message, mqtt.Client) `json:"-"`
Name *string `json:"name,omitempty"` // "The name of the Select."
Name *string `json:"name,omitempty"` // "The name of the Update."
ObjectId *string `json:"object_id,omitempty"` // "Used instead of `name` for automatic generation of `entity_id`"
PayloadInstall *string `json:"payload_install,omitempty"` // "The MQTT payload to start installing process."
Qos *int `json:"qos,omitempty"` // "The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages."
Expand All @@ -53,7 +53,7 @@ type Update struct {
StateTopic *string `json:"state_topic,omitempty"` // "The MQTT topic subscribed to receive state updates. The state update may be either JSON or a simple string with `installed_version` value. When a JSON payload is detected, the state value of the JSON payload should supply the `installed_version` and can optional supply: `latest_version`, `title`, `release_summary`, `release_url` or an `entity_picture` URL."
StateFunc func() string `json:"-"`
Title *string `json:"title,omitempty"` // "Title of the software, or firmware update. This helps to differentiate between the device or entity name versus the title of the software installed."
UniqueId *string `json:"unique_id,omitempty"` // "An ID that uniquely identifies this Select. If two Selects have the same unique ID Home Assistant will raise an exception."
UniqueId *string `json:"unique_id,omitempty"` // "An ID that uniquely identifies this Update. If two Updates have the same unique ID Home Assistant will raise an exception."
ValueTemplate *string `json:"value_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) to extract the `installed_version` state value or to render to a valid JSON payload on from the payload received on `state_topic`."
MQTT *MQTTFields `json:"-"` // MQTT configuration parameters
}
Expand Down
2 changes: 1 addition & 1 deletion devices/externaldevice/vacuum.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
common "github.com/W-Floyd/ha-mqtt-iot/common"
store "github.com/W-Floyd/ha-mqtt-iot/store"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
"log"
"time"
mqtt "tinygo.org/x/drivers/net/mqtt"
)

// //////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions devices/internaldevice/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Update struct {
JsonAttributes *([]string) `json:"json_attributes,omitempty"` // JsonAttributes for the Update
LatestVersionTemplate *string `json:"latest_version_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) to extract the latest version value."
LatestVersion *([]string) `json:"latest_version,omitempty"` // LatestVersion for the Update
Name *string `json:"name,omitempty"` // "The name of the Select."
Name *string `json:"name,omitempty"` // "The name of the Update."
ObjectId *string `json:"object_id,omitempty"` // "Used instead of `name` for automatic generation of `entity_id`"
PayloadInstall *string `json:"payload_install,omitempty"` // "The MQTT payload to start installing process."
Qos *int `json:"qos,omitempty"` // "The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages."
Expand All @@ -32,7 +32,7 @@ type Update struct {
Retain *bool `json:"retain,omitempty"` // "If the published message should have the retain flag on or not."
State *([]string) `json:"state,omitempty"` // State for the Update
Title *string `json:"title,omitempty"` // "Title of the software, or firmware update. This helps to differentiate between the device or entity name versus the title of the software installed."
UniqueId *string `json:"unique_id,omitempty"` // "An ID that uniquely identifies this Select. If two Selects have the same unique ID Home Assistant will raise an exception."
UniqueId *string `json:"unique_id,omitempty"` // "An ID that uniquely identifies this Update. If two Updates have the same unique ID Home Assistant will raise an exception."
ValueTemplate *string `json:"value_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) to extract the `installed_version` state value or to render to a valid JSON payload on from the payload received on `state_topic`."
MQTT struct {
UpdateInterval *float64 `json:"update_interval,omitempty"`
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ require (
github.com/Jeffail/gabs/v2 v2.6.1
github.com/dave/jennifer v1.5.0
github.com/denisbrodbeck/machineid v1.0.1
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/ghodss/yaml v1.0.0
github.com/iancoleman/strcase v0.2.0
github.com/imdario/mergo v0.3.13
github.com/imdario/mergo v0.3.15
tinygo.org/x/drivers v0.24.0
)

require (
github.com/gorilla/websocket v1.5.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
github.com/eclipse/paho.mqtt.golang v1.4.2 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading