Skip to content

Commit

Permalink
Update platform back-compat for custom components without UpdateEntit…
Browse files Browse the repository at this point in the history
…yFeature (#106528)
  • Loading branch information
bdraco committed Dec 29, 2023
1 parent 8172680 commit 4b6aaf6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
21 changes: 17 additions & 4 deletions homeassistant/components/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def entity_category(self) -> EntityCategory | None:
return self._attr_entity_category
if hasattr(self, "entity_description"):
return self.entity_description.entity_category
if UpdateEntityFeature.INSTALL in self.supported_features:
if UpdateEntityFeature.INSTALL in self.supported_features_compat:
return EntityCategory.CONFIG
return EntityCategory.DIAGNOSTIC

Expand Down Expand Up @@ -322,6 +322,19 @@ def title(self) -> str | None:
"""
return self._attr_title

@property
def supported_features_compat(self) -> UpdateEntityFeature:
"""Return the supported features as UpdateEntityFeature.
Remove this compatibility shim in 2025.1 or later.
"""
features = self.supported_features
if type(features) is int: # noqa: E721
new_features = UpdateEntityFeature(features)
self._report_deprecated_supported_features_values(new_features)
return new_features
return features

@final
async def async_skip(self) -> None:
"""Skip the current offered version to update."""
Expand Down Expand Up @@ -408,7 +421,7 @@ def state_attributes(self) -> dict[str, Any] | None:

# If entity supports progress, return the in_progress value.
# Otherwise, we use the internal progress value.
if UpdateEntityFeature.PROGRESS in self.supported_features:
if UpdateEntityFeature.PROGRESS in self.supported_features_compat:
in_progress = self.in_progress
else:
in_progress = self.__in_progress
Expand Down Expand Up @@ -444,7 +457,7 @@ async def async_install_with_progress(
Handles setting the in_progress state in case the entity doesn't
support it natively.
"""
if UpdateEntityFeature.PROGRESS not in self.supported_features:
if UpdateEntityFeature.PROGRESS not in self.supported_features_compat:
self.__in_progress = True
self.async_write_ha_state()

Expand Down Expand Up @@ -490,7 +503,7 @@ async def websocket_release_notes(
)
return

if UpdateEntityFeature.RELEASE_NOTES not in entity.supported_features:
if UpdateEntityFeature.RELEASE_NOTES not in entity.supported_features_compat:
connection.send_error(
msg["id"],
websocket_api.const.ERR_NOT_SUPPORTED,
Expand Down
20 changes: 20 additions & 0 deletions tests/components/update/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,23 @@ async def async_setup_entry_platform(
state = hass.states.get(entity4.entity_id)
assert state
assert expected.items() <= state.attributes.items()


def test_deprecated_supported_features_ints(caplog: pytest.LogCaptureFixture) -> None:
"""Test deprecated supported features ints."""

class MockUpdateEntity(UpdateEntity):
@property
def supported_features(self) -> int:
"""Return supported features."""
return 1

entity = MockUpdateEntity()
assert entity.supported_features_compat is UpdateEntityFeature(1)
assert "MockUpdateEntity" in caplog.text
assert "is using deprecated supported features values" in caplog.text
assert "Instead it should use" in caplog.text
assert "UpdateEntityFeature.INSTALL" in caplog.text
caplog.clear()
assert entity.supported_features_compat is UpdateEntityFeature(1)
assert "is using deprecated supported features values" not in caplog.text

0 comments on commit 4b6aaf6

Please sign in to comment.