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

Add SWBuild read before starting update (Was:OTA events not firing at the correct time) #155

Open
mathiasolofsson87 opened this issue Mar 21, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@mathiasolofsson87
Copy link

I've setup the event_success, event_fail and event_done to use "ota_success" "ota_fail" and "ota_done" as event names and an automation that listens for the ota_done event to fire and send a notification to my phone when an update is done.

However all those three events seem to fire when initiating an update, so I get a notification as soon as I call the ota.notify service. but no event is fired when the update is actually finished.

All three events seem to fire when starting the update, not after its done.

@mdeweerd
Copy link
Owner

These events relate to the result of the ota_notify request itself, not the result of the OTA Update.

ZHA-Toolkit sends an "invitation" to the device to indicate that it should try to get an update. The device can respond with "SUCCESS" which means that it received the request and that it can handle the request (it has an OTA cluster), or with an error (it does not support the OTA update for instance), or it does not reply.

In case of success the device will start the OTA update process which starts by checking with the coordinator if there is actually an update available (if there is no newer version, there will not be a real update).
If there is, there will be a long succession of requests to get the new firmware and when received completely, and validated (checksum, signature when required), it will actually update.

As far as I remember there is no update confirmation, but ZHA/Zigbee sets report_configuration of the FW version on the device so that when it changes the new version is notified to the coordinator.

So:

  • The "event_done" event (as defined) is always sent after the ota_notify to the device.
  • The "event_success" event should only be sent when the device replies success to ota_notify.
  • The "event_fail" event should occur only when the "ota_notify" request did not receive a "SUCCESS" response from the device.

If you receive all 3 events for each ota_notify request, then this is a ZHA Toolkit bug, if you only receive 2 then this is normal.

In order to know if the device was updated, the version before and after the update must be compared.

IMHO it is possible to extend the ZHA Toolkit code to record the FW Version before sending the OTA Notify, and then check on a regular basis if that FW version changed. I am not sure that the device notifies that an update failed, if it doesn't (and for more robustness), a timeout could be added as an option at which time a fail event would be sent if the version did not change.

I am open to a PR that implements monitoring the OTA Update until the long term timeout occurs.

In case all three events regarding the ota_notify are all sent at the same time, I will look into that bug myself.

@mathiasolofsson87
Copy link
Author

Thank you so much for clarification. I have misunderstood how the events were supposed to work. I was under the impression that events would fire when the update was complete.

I tested it now, and it only fires two events when initiating the update. "success" and "done" events. fail did not fire.

To explain what I was trying to accomplish in the long run:
Get a notification from HA when an update was complete. And get the result, if it was failed or a success, along with the entity ID in the notification. to easier keep track of what devices were getting updated.

@mdeweerd
Copy link
Owner

@mathiasolofsson87 I understood your goal can be usefull - I had to update all of my Thermostatic Valves at some point. However the effort of coding a monitoring process, tracking the events and showing that in the HA UI outweighs simply starting the OTA update for all the relevant devices and then check after a few hours if the version changed to the one I expected.

The SW build id can be viewed in a table using my fork of zha-network-card but that is a bit slowisch as reading the sw build id results in a zigbee request for each device (see my comment on HA's forum ).

@mathiasolofsson87
Copy link
Author

Forgive my ignorance, HA is very new to me and I am by no means a developer. But I keep learning and the community is great! How can I check what firmware version I should expect to see after the update is complete?

For my case it is only my Hue Lights that I need to manually trigger OTA Firmware updates for currently.

Is there a way to get a list of what Hue devices that have newer firmware available, so I know which ones need an update? Currently I've just been guessing by checking the zigbee device information in the HA GUI.

@mdeweerd
Copy link
Owner

Personnally I have the following methods:

FYI, the manufacturer code is 4107.

  1. Look in the https://github.com/Koenkk/zigbee-OTA/blob/master/index.json file - the manufacturer code lets you find some devices. Often the filename indicates the version you can expect somehow.
  2. Simpler : let ota_notify download the firmware, and then look in the "otau_directory" folder (as defined in your configuration).. It will download all the files for the manufacturer from the list above, but then you just have to look at the filenames.
  3. I guess that you could enable debug messages so that zigpy reports what it is doing.

As I was looking at the zha-toolkit code to see if it reports the file it uses, I notice that it enables reportint the "sw_build_id" on the device. every 30 minutes.

Maybe I should add a read request of the sw_build_id and add it to the event data so that you know what version you started with.

@mathiasolofsson87
Copy link
Author

That would be an awesome addition!

Thank you so much for your input and your work on ZHA Toolkit!

@mdeweerd mdeweerd added the enhancement New feature or request label Apr 17, 2023
@mdeweerd mdeweerd changed the title OTA events not firing at the correct time Add SWBuild read before starting update (Was:OTA events not firing at the correct time) Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants