Skip to content

Commit

Permalink
Decimal OTA progress (#288)
Browse files Browse the repository at this point in the history
* Allow non-integer OTA progress percentage

* Update unit tests

* Revert debouncer, progress is naturally limited to roughly 1/200ms

* Rename `progress` to `update_percentage`
  • Loading branch information
puddly authored Nov 5, 2024
1 parent 701f575 commit 0a94d55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
6 changes: 4 additions & 2 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
ATTR_IN_PROGRESS,
ATTR_INSTALLED_VERSION,
ATTR_LATEST_VERSION,
ATTR_PROGRESS,
ATTR_UPDATE_PERCENTAGE,
)
from zha.exceptions import ZHAException

Expand Down Expand Up @@ -309,7 +309,9 @@ async def endpoint_reply(cluster, sequence, data, **kwargs):
== f"0x{installed_fw_version:08x}"
)
assert entity.state[ATTR_IN_PROGRESS] is True
assert entity.state[ATTR_PROGRESS] == 57
assert entity.state[ATTR_UPDATE_PERCENTAGE] == pytest.approx(
100 * (40 / 70)
)
assert (
entity.state[ATTR_LATEST_VERSION]
== f"0x{fw_image.firmware.header.file_version:08x}"
Expand Down
24 changes: 11 additions & 13 deletions zha/application/platforms/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ class UpdateEntityFeature(IntFlag):
RELEASE_NOTES = 16


SERVICE_INSTALL: Final = "install"

ATTR_BACKUP: Final = "backup"
ATTR_INSTALLED_VERSION: Final = "installed_version"
ATTR_IN_PROGRESS: Final = "in_progress"
ATTR_PROGRESS: Final = "progress"
ATTR_UPDATE_PERCENTAGE: Final = "update_percentage"
ATTR_LATEST_VERSION: Final = "latest_version"
ATTR_RELEASE_SUMMARY: Final = "release_summary"
ATTR_RELEASE_NOTES: Final = "release_notes"
Expand Down Expand Up @@ -89,7 +87,7 @@ class FirmwareUpdateEntity(PlatformEntity):
)
_attr_installed_version: str | None = None
_attr_in_progress: bool = False
_attr_progress: int = 0
_attr_update_percentage: float | None = None
_attr_latest_version: str | None = None
_attr_release_summary: str | None = None
_attr_release_notes: str | None = None
Expand Down Expand Up @@ -152,14 +150,13 @@ def in_progress(self) -> bool | None:
return self._attr_in_progress

@property
def progress(self) -> int | None:
def update_percentage(self) -> float | None:
"""Update installation progress.
Needs UpdateEntityFeature.PROGRESS flag to be set for it to be used.
Returns an integer indicating the progress from 0 to 100%.
Returns a number indicating the progress from 0 to 100%. If an update's progress
is indeterminate, this will return None.
"""
return self._attr_progress
return self._attr_update_percentage

@property
def latest_version(self) -> str | None:
Expand Down Expand Up @@ -200,7 +197,7 @@ def state_attributes(self) -> dict[str, Any] | None:
return {
ATTR_INSTALLED_VERSION: self.installed_version,
ATTR_IN_PROGRESS: self.in_progress,
ATTR_PROGRESS: self.progress,
ATTR_UPDATE_PERCENTAGE: self.update_percentage,
ATTR_LATEST_VERSION: self.latest_version,
ATTR_RELEASE_SUMMARY: release_summary,
ATTR_RELEASE_NOTES: self.release_notes,
Expand Down Expand Up @@ -260,7 +257,7 @@ def _update_progress(self, current: int, total: int, progress: float) -> None:
if not self._attr_in_progress:
return

self._attr_progress = int(progress)
self._attr_update_percentage = progress
self.maybe_emit_state_changed_event()

async def async_install(self, version: str | None) -> None:
Expand All @@ -284,7 +281,7 @@ async def async_install(self, version: str | None) -> None:
raise ZHAException(f"Version {version!r} is not available")

self._attr_in_progress = True
self._attr_progress = 0
self._attr_update_percentage = None
self.maybe_emit_state_changed_event()

try:
Expand All @@ -309,5 +306,6 @@ async def async_install(self, version: str | None) -> None:

async def on_remove(self) -> None:
"""Call when entity will be removed."""
await super().on_remove()
self._attr_in_progress = False
self.device.device.remove_listener(self)
await super().on_remove()

0 comments on commit 0a94d55

Please sign in to comment.