Skip to content

Commit

Permalink
Add update_percentage property to update entity (#128908)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Oct 21, 2024
1 parent e7a7a18 commit 23b4331
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
25 changes: 18 additions & 7 deletions homeassistant/components/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def _version_is_newer(latest_version: str, installed_version: str) -> bool:
"release_url",
"supported_features",
"title",
"update_percentage",
}


Expand Down Expand Up @@ -227,6 +228,7 @@ class UpdateEntity(
_attr_state: None = None
_attr_supported_features: UpdateEntityFeature = UpdateEntityFeature(0)
_attr_title: str | None = None
_attr_update_percentage: int | None = None
__skipped_version: str | None = None
__in_progress: bool = False

Expand Down Expand Up @@ -284,8 +286,7 @@ def in_progress(self) -> bool | int | None:
Needs UpdateEntityFeature.PROGRESS flag to be set for it to be used.
Can either return a boolean (True if in progress, False if not)
or an integer to indicate the progress in from 0 to 100%.
Should return a boolean (True if in progress, False if not).
"""
return self._attr_in_progress

Expand Down Expand Up @@ -335,6 +336,16 @@ def supported_features_compat(self) -> UpdateEntityFeature:
return new_features
return features

@cached_property
def update_percentage(self) -> int | None:
"""Update installation progress.
Needs UpdateEntityFeature.PROGRESS flag to be set for it to be used.
Can either return an integer to indicate the progress from 0 to 100% or None.
"""
return self._attr_update_percentage

@final
async def async_skip(self) -> None:
"""Skip the current offered version to update."""
Expand Down Expand Up @@ -424,17 +435,17 @@ def state_attributes(self) -> dict[str, Any] | None:
if (release_summary := self.release_summary) is not None:
release_summary = release_summary[:255]

update_percentage = None

# If entity supports progress, return the in_progress value.
# Otherwise, we use the internal progress value.
if UpdateEntityFeature.PROGRESS in self.supported_features_compat:
in_progress = self.in_progress
update_percentage = self.update_percentage
if type(in_progress) is not bool and isinstance(in_progress, int):
update_percentage = in_progress
in_progress = True
else:
in_progress = self.__in_progress
if type(in_progress) is not bool and isinstance(in_progress, int):
update_percentage = in_progress
in_progress = True
update_percentage = None

installed_version = self.installed_version
latest_version = self.latest_version
Expand Down
5 changes: 5 additions & 0 deletions tests/components/update/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def title(self) -> str | None:
"""Title of the software."""
return self._handle("title")

@property
def update_percentage(self) -> int | None:
"""Update installation progress."""
return self._handle("update_percentage")

def install(self, version: str | None, backup: bool, **kwargs: Any) -> None:
"""Install an update."""
if backup:
Expand Down
3 changes: 2 additions & 1 deletion tests/components/update/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ def mock_update_entities() -> list[MockUpdateEntity]:
unique_id="update_already_in_progres",
installed_version="1.0.0",
latest_version="1.0.1",
in_progress=50,
in_progress=True,
supported_features=UpdateEntityFeature.INSTALL
| UpdateEntityFeature.PROGRESS,
update_percentage=50,
),
MockUpdateEntity(
name="Update No Install",
Expand Down

0 comments on commit 23b4331

Please sign in to comment.