Compare commits

..

7 Commits

Author SHA1 Message Date
83b2d14160 Update go.mod dependencies and improve vehicle retrieval error handling
Some checks failed
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Successful in 39s
Build and Push Docker Image / build-and-push (push) Failing after 10m50s
2025-07-22 16:58:09 -04:00
5db9042125 More debuging
All checks were successful
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Successful in 30s
Build and Push Docker Image / build-and-push (push) Successful in 9m54s
2025-06-09 12:48:01 -04:00
217846c17c Updated go.mod
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Has been cancelled
2025-06-06 17:42:43 -04:00
c4e9007d87 Fixed some issues
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Has been cancelled
2025-06-06 17:42:26 -04:00
118ee684ee Fixed vehicle health items reporting
All checks were successful
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Successful in 30s
Build and Push Docker Image / build-and-push (push) Successful in 9m59s
2025-06-06 12:48:35 -04:00
c7b62cc25f Bumped version to v1.1.1
All checks were successful
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Successful in 37s
Build and Push Docker Image / build-and-push (push) Successful in 9m59s
2025-06-06 12:27:31 -04:00
1011c09b29 Some fixes to the locks status reporting; Added troubles
Some checks failed
Build and Push Docker Image / testing (1.24.x, ubuntu-latest) (push) Successful in 36s
Build and Push Docker Image / build-and-push (push) Has been cancelled
2025-06-06 12:26:39 -04:00
3 changed files with 48 additions and 27 deletions

View File

@ -20,7 +20,7 @@ import (
)
const (
AppVersion = "1.1.0" // the current application version.
AppVersion = "1.1.1" // the current application version.
AppName = "mysubarumq" // the short app name
)

14
go.mod
View File

@ -5,7 +5,7 @@ go 1.24
replace github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.5.3
require (
git.savin.nyc/alex/mysubaru v0.0.0-20250605210309-91ab2ddf0018
git.savin.nyc/alex/mysubaru v0.0.0-20250722205404-92d4266f8b5a
github.com/eclipse/paho.mqtt.golang v1.5.0
github.com/hashicorp/consul/api v1.32.1
github.com/spf13/viper v1.20.1
@ -17,7 +17,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
@ -38,14 +38,14 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.14.0 // indirect
github.com/spf13/cast v1.9.2 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/spf13/pflag v1.0.7 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
golang.org/x/net v0.41.0 // indirect
golang.org/x/sync v0.15.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.26.0 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/text v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
resty.dev/v3 v3.0.0-beta.3 // indirect
)

View File

@ -71,7 +71,10 @@ func (s *MySubaruClient) Init(log *slog.Logger) error {
func (s *MySubaruClient) OneTime() error {
if s.config.Hassio.AutoDiscovery {
time.Sleep(3 * time.Second)
vehicles := s.mysubaru.GetVehicles()
vehicles, err := s.mysubaru.GetVehicles()
if err != nil {
s.log.Error("couldn't get vehicles from MySubaru", "error", err.Error())
}
for _, vehicle := range vehicles {
err := s.bus.Publish("mqtt:publish", s.mySubaruConfigToMQTTHassioConfig(vehicle))
@ -148,7 +151,7 @@ func (s *MySubaruClient) Serve() {
for {
select {
case <-tickerS.C:
vehicles := s.mysubaru.GetVehicles()
vehicles, _ := s.mysubaru.GetVehicles()
for _, vehicle := range vehicles {
err := s.bus.Publish("mqtt:publish", s.mySubaruStatusToMQTTMessage(vehicle))
if err != nil {
@ -156,7 +159,7 @@ func (s *MySubaruClient) Serve() {
}
}
case <-tickerM.C:
vehicles := s.mysubaru.GetVehicles()
vehicles, _ := s.mysubaru.GetVehicles()
for _, vehicle := range vehicles {
vehicle.GetLocation(true)
err := s.bus.Publish("mqtt:publish", s.mySubaruStatusToMQTTMessage(vehicle))
@ -195,14 +198,14 @@ func (s *MySubaruClient) eventLoop(ctx context.Context, chMQTTLockStatus, chMQTT
for _, message := range event.Data.([]*bus.Message) {
s.log.Debug("received a message with mysubary lock status", "topic", message.Topic, "payload", message.Payload)
if message.Payload == "LOCK" {
v := s.mysubaru.GetVehicleByVIN("4S4BTGPD0P3199198")
v, _ := s.mysubaru.GetVehicleByVin("4S4BTGPD0P3199198")
v.Lock()
var msgs []*bus.Message
msgs = s.messages("mysubarumq/4S4BTGPD0P3199198/lock", 0, true, "LOCK", msgs)
s.bus.Publish("mqtt:publish", msgs)
}
if message.Payload == "UNLOCK" {
v := s.mysubaru.GetVehicleByVIN("4S4BTGPD0P3199198")
v, _ := s.mysubaru.GetVehicleByVin("4S4BTGPD0P3199198")
v.Unlock()
var msgs []*bus.Message
msgs = s.messages("mysubarumq/4S4BTGPD0P3199198/lock", 0, true, "UNLOCK", msgs)
@ -213,14 +216,14 @@ func (s *MySubaruClient) eventLoop(ctx context.Context, chMQTTLockStatus, chMQTT
for _, message := range event.Data.([]*bus.Message) {
s.log.Debug("received a message with mysubary ignition status", "topic", message.Topic, "payload", message.Payload)
if message.Payload == "ON" {
v := s.mysubaru.GetVehicleByVIN("4S4BTGPD0P3199198")
v.EngineStart()
v, _ := s.mysubaru.GetVehicleByVin("4S4BTGPD0P3199198")
v.EngineStart(10, 0, false)
var msgs []*bus.Message
msgs = s.messages("mysubarumq/4S4BTGPD0P3199198/ignition", 0, true, "ON", msgs)
s.bus.Publish("mqtt:publish", msgs)
}
if message.Payload == "OFF" {
v := s.mysubaru.GetVehicleByVIN("4S4BTGPD0P3199198")
v, _ := s.mysubaru.GetVehicleByVin("4S4BTGPD0P3199198")
v.EngineStop()
var msgs []*bus.Message
msgs = s.messages("mysubarumq/4S4BTGPD0P3199198/ignition", 0, true, "OFF", msgs)
@ -306,21 +309,22 @@ func (s *MySubaruClient) mySubaruConfigToMQTTHassioConfig(v *mysubaru.Vehicle) [
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/engine_state/config`] = `{` + device + origin + topic + `"name":"Engine State","uniq_id":"` + v.Vin + `_engine_state","obj_id":"` + obj_id_prefix + `_engine_state","ic":"mdi:engine","stat_t":"mysubarumq/` + v.Vin + `/state","val_tpl":"{{value_json.engine_state}}"}`
hassioConfig[s.config.Hassio.Topics.Discovery+`/device_tracker/`+v.Vin+`/config`] = `{` + device + origin + `"name":"` + v.CarNickname + `","uniq_id":"` + v.Vin + `_device_tracker","obj_id":"` + obj_id_prefix + `","ic":"mdi:car-connected","json_attr_t":"mysubarumq/` + v.Vin + `/attr"}`
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/troubles/config`] = `{` + device + origin + topic + `"name":"Troubles","uniq_id":"` + v.Vin + `_troubles","obj_id":"` + obj_id_prefix + `_troubles","ic":"mdi:car-wrench","stat_t":"mysubarumq/` + v.Vin + `/state","val_tpl":"{{value_json.troubles}}","unit_of_meas":""}`
// homeassistant/sensor/mysubaru/VIN-NUMBER-HERE/troubles/config
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/troubles/config`] = `{` + device + origin + topic + `"name":"Troubles","uniq_id":"` + v.Vin + `_troubles","obj_id":"` + obj_id_prefix + `_troubles","ic":"mdi:car-wrench","stat_t":"mysubarumq/` + v.Vin + `/state","val_tpl":"{{value_json.troubles}}","json_attr_t":"mysubarumq/` + v.Vin + `/troubles/attr","unit_of_meas":""}`
for n, d := range v.Doors {
// homeassistant/sensor/mysubaru/VIN-NUMBER-HERE/door_frontleft_status/config
position := d.Position + ` ` + d.SubPosition
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/`+n+`_status/config`] = `{` + device + origin + topic + `"name":"Door ` + position + ` Status","uniq_id":"` + v.Vin + n + `_status","obj_id":"` + obj_id_prefix + n + `_status","ic":"mdi:car-door","json_attr_t":"mysubarumq/` + v.Vin + `/doors/attr","json_attr_tpl":"{{value_json.` + n + `}}","stat_t":"mysubarumq/` + v.Vin + `/doors/state","val_tpl":"{{value_json.` + n + `}}"}`
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/`+n+`_status/config`] = `{` + device + origin + topic + `"name":"Door ` + position + ` Status","uniq_id":"` + v.Vin + `_` + n + `_status","obj_id":"` + obj_id_prefix + n + `_status","ic":"mdi:car-door","stat_t":"mysubarumq/` + v.Vin + `/doors/state","val_tpl":"{{value_json.` + n + `}}","json_attr_t":"mysubarumq/` + v.Vin + `/doors/` + n + `/attr"}`
}
for n, w := range v.Windows {
// homeassistant/sensor/mysubaru/VIN-NUMBER-HERE/window_frontleft/config
position := w.Position + ` ` + w.SubPosition
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/`+n+`/config`] = `{` + device + origin + topic + `"name":"Window ` + position + `","uniq_id":"` + v.Vin + n + `_status","obj_id":"` + obj_id_prefix + n + `_status","ic":"mdi:car-door","stat_t":"mysubarumq/` + v.Vin + `/windows/state","val_tpl":"{{value_json.` + n + `}}"}`
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/`+n+`/config`] = `{` + device + origin + topic + `"name":"Window ` + position + `","uniq_id":"` + v.Vin + `_` + n + `_status","obj_id":"` + obj_id_prefix + n + `_status","ic":"mdi:car-door","stat_t":"mysubarumq/` + v.Vin + `/windows/state","val_tpl":"{{value_json.` + n + `}}"}`
}
for n, t := range v.Tires {
// homeassistant/sensor/mysubaru/VIN-NUMBER-HERE/tire_frontleft_psi/config
position := t.Position + ` ` + t.SubPosition
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/`+n+`_psi/config`] = `{` + device + origin + topic + `"name":"Tire ` + position + ` (Psi)","uniq_id":"` + v.Vin + n + `_psi","obj_id":"` + obj_id_prefix + n + `_psi","ic":"mdi:tire","stat_t":"mysubarumq/` + v.Vin + `/tires/state","val_tpl":"{{value_json.` + n + `}}","unit_of_meas":"psi"}`
hassioConfig[s.config.Hassio.Topics.Discovery+`/sensor/`+v.Vin+`/`+n+`_psi/config`] = `{` + device + origin + topic + `"name":"Tire ` + position + ` (Psi)","uniq_id":"` + v.Vin + `_` + n + `_psi","obj_id":"` + obj_id_prefix + n + `_psi","ic":"mdi:tire","stat_t":"mysubarumq/` + v.Vin + `/tires/state","val_tpl":"{{value_json.` + n + `}}","unit_of_meas":"psi"}`
}
topicState := `mysubarumq/` + v.Vin + `/ignition`
@ -370,30 +374,32 @@ func (s *MySubaruClient) mySubaruStatusToMQTTMessage(v *mysubaru.Vehicle) []*bus
var state = map[string]string{}
tank := ""
if v.DistanceToEmpty.Percentage > 0 && 101 >= v.DistanceToEmpty.Percentage {
if v.DistanceToEmpty.Percentage > 0 && 101 > v.DistanceToEmpty.Percentage {
tank = `"dist_to_empty_pc":` + strconv.Itoa(v.DistanceToEmpty.Percentage) + `,`
}
state[`mysubarumq/`+v.Vin+`/state`] = `{` + tank + `"odometer_km":` + strconv.Itoa(v.Odometer.Kilometers) + `,"odometer_mi":` + strconv.Itoa(v.Odometer.Miles) + `,"dist_to_empty_km":` + strconv.Itoa(v.DistanceToEmpty.Kilometers) + `,"dist_to_empty_mi":` + strconv.Itoa(v.DistanceToEmpty.Miles) + `,"consumption_us":` + fmt.Sprintf("%.2f", v.FuelConsumptionAvg.MPG) + `,"consumption_eu":` + fmt.Sprintf("%.2f", v.FuelConsumptionAvg.LP100Km) + `,"engine_state":"` + v.EngineState + `","troubles":"` + strconv.Itoa(len(v.Troubles)) + `"}`
state[`mysubarumq/`+v.Vin+`/attr`] = `{"source_type":"gps","latitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Latitude) + `,"longitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Longitude) + `,"course":` + strconv.Itoa(v.GeoLocation.Heading) + `,"speed":` + fmt.Sprintf("%.2f", v.GeoLocation.Speed) + `,"friendly_name":"` + v.CarNickname + `"}`
state[`mysubarumq/`+v.Vin+`/attr`] = `{"source_type":"gps","latitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Latitude) + `,"longitude":` + fmt.Sprintf("%.6f", v.GeoLocation.Longitude) + `,"course":` + strconv.Itoa(v.GeoLocation.Heading) + `,"speed":` + fmt.Sprintf("%d", v.GeoLocation.Speed) + `,"friendly_name":"` + v.CarNickname + `"}`
// Doors
doors := `{`
locks := `{}`
// locks := `{`
dq := len(v.Doors)
dc := 1
for n, d := range v.Doors {
doors = doors + `"` + n + `":"` + d.Status + `"`
locks = locks + `"` + n + `":"` + d.Lock + `"`
// locks = locks + `"` + n + `":"` + d.Lock + `"`
if dc != dq {
doors = doors + `,`
locks = locks + `,`
// locks = locks + `,`
}
dc++
}
doors = doors + `}`
locks = locks + `}`
// locks = locks + `}`
state[`mysubarumq/`+v.Vin+`/doors/state`] = doors
state[`mysubarumq/`+v.Vin+`/doors/attr`] = locks
// state[`mysubarumq/`+v.Vin+`/doors/attr`] = locks
// Windows
windows := `{`
wq := len(v.Windows)
wc := 1
@ -407,6 +413,7 @@ func (s *MySubaruClient) mySubaruStatusToMQTTMessage(v *mysubaru.Vehicle) []*bus
windows = windows + `}`
state[`mysubarumq/`+v.Vin+`/windows/state`] = windows
// Tires
tires := `{`
tq := len(v.Tires)
tc := 1
@ -420,6 +427,20 @@ func (s *MySubaruClient) mySubaruStatusToMQTTMessage(v *mysubaru.Vehicle) []*bus
tires = tires + `}`
state[`mysubarumq/`+v.Vin+`/tires/state`] = tires
// Vehicle Health Items
troubles := `{`
trq := len(v.Troubles)
trc := 1
for n, t := range v.Troubles {
troubles = troubles + `"` + n + `":"` + t.Description + `"`
if trc != trq {
troubles = troubles + `,`
}
trc++
}
troubles = troubles + `}`
state[`mysubarumq/`+v.Vin+`/troubles/attr`] = troubles
var msgs []*bus.Message
for topic, payload := range state {
msgs = s.messages(topic, 0, false, payload, msgs)