Only process EndDevices during add and more verbose errors

If a Coordinator is present in the device list, AddDevicesFromJSON will
fail catastrophically, which shouldn't happen. Therefore, make sure only
EndDevices are considered during add. Also updated the tests to check
for this.

Added a device descriptor for failed adds. This will help with
identifying which device failed (and perhaps, why).
This commit is contained in:
Darell Tan
2024-11-22 03:40:27 +08:00
parent 5e24287d8c
commit e678f2a31d
3 changed files with 48 additions and 21 deletions

View File

@@ -442,6 +442,12 @@ func (br *Bridge) accessories() []*accessory.A {
return acc
}
func deviceJsonDescriptor(d Device) []byte {
d.Definition = nil
j, _ := json.Marshal(d)
return j
}
// Creates and calls AddDevice() based on the JSON definitions from zigbee2mqtt/bridge/devices.
func (br *Bridge) AddDevicesFromJSON(devJson []byte) error {
var devices []Device
@@ -458,12 +464,13 @@ func (br *Bridge) AddDevicesFromJSON(devJson []byte) error {
if err == ErrDeviceSkipped || err == ErrUnknownDeviceType {
continue
}
return err
return fmt.Errorf("createAccessory failed: %+v %s", err, deviceJsonDescriptor(dev))
}
err = br.AddDevice(&dev, acc, exp)
if err != nil {
return err
return fmt.Errorf("AddDevice failed: %+v %s", err, deviceJsonDescriptor(dev))
}
}