Update go.mod dependencies and improve vehicle retrieval error handling
This commit is contained in:
@ -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)
|
||||
@ -375,7 +378,7 @@ func (s *MySubaruClient) mySubaruStatusToMQTTMessage(v *mysubaru.Vehicle) []*bus
|
||||
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 := `{`
|
||||
|
Reference in New Issue
Block a user