From d65e12ab6eadd8a9d2e5b842a020d741a4eec0e0 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 19 Apr 2022 16:11:16 +0200 Subject: [PATCH] Add clear_skipped service to update entity (#70116) --- homeassistant/components/update/__init__.py | 21 ++++++++++++++ homeassistant/components/update/services.yaml | 7 +++++ tests/components/update/test_init.py | 29 ++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/update/__init__.py b/homeassistant/components/update/__init__.py index 100b5dcc35e..f3c6df2072f 100644 --- a/homeassistant/components/update/__init__.py +++ b/homeassistant/components/update/__init__.py @@ -96,6 +96,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: {}, async_skip, ) + component.async_register_entity_service( + "clear_skipped", + {}, + async_clear_skipped, + ) + websocket_api.async_register_command(hass, websocket_release_notes) return True @@ -153,6 +159,15 @@ async def async_skip(entity: UpdateEntity, service_call: ServiceCall) -> None: await entity.async_skip() +async def async_clear_skipped(entity: UpdateEntity, service_call: ServiceCall) -> None: + """Service call wrapper to validate the call.""" + if entity.auto_update: + raise HomeAssistantError( + f"Clearing skipped update is not supported for {entity.name}" + ) + await entity.async_clear_skipped() + + @dataclass class UpdateEntityDescription(EntityDescription): """A class that describes update entities.""" @@ -276,6 +291,12 @@ class UpdateEntity(RestoreEntity): self.__skipped_version = latest_version self.async_write_ha_state() + @final + async def async_clear_skipped(self) -> None: + """Clear the skipped version.""" + self.__skipped_version = None + self.async_write_ha_state() + async def async_install( self, version: str | None, backup: bool, **kwargs: Any ) -> None: diff --git a/homeassistant/components/update/services.yaml b/homeassistant/components/update/services.yaml index 2a3370493cc..9b16dbd2713 100644 --- a/homeassistant/components/update/services.yaml +++ b/homeassistant/components/update/services.yaml @@ -25,3 +25,10 @@ skip: target: entity: domain: update + +clear_skipped: + name: Clear skipped update + description: Removes the skipped version marker from an update. + target: + entity: + domain: update diff --git a/tests/components/update/test_init.py b/tests/components/update/test_init.py index c4078460f57..bed69d85732 100644 --- a/tests/components/update/test_init.py +++ b/tests/components/update/test_init.py @@ -202,6 +202,21 @@ async def test_entity_with_no_install( assert state.attributes[ATTR_LATEST_VERSION] == "1.0.1" assert state.attributes[ATTR_SKIPPED_VERSION] == "1.0.1" + # We can clear the skipped marker again + await hass.services.async_call( + DOMAIN, + "clear_skipped", + {ATTR_ENTITY_ID: "update.update_no_install"}, + blocking=True, + ) + + state = hass.states.get("update.update_no_install") + assert state + assert state.state == STATE_ON + assert state.attributes[ATTR_INSTALLED_VERSION] == "1.0.0" + assert state.attributes[ATTR_LATEST_VERSION] == "1.0.1" + assert state.attributes[ATTR_SKIPPED_VERSION] is None + async def test_entity_with_no_updates( hass: HomeAssistant, @@ -279,7 +294,7 @@ async def test_entity_with_auto_update( blocking=True, ) - # Should not be to skip the update + # Should not be able to skip the update with pytest.raises( HomeAssistantError, match="Skipping update is not supported for Update with auto update", @@ -291,6 +306,18 @@ async def test_entity_with_auto_update( blocking=True, ) + # Should not be able to clear a skipped the update + with pytest.raises( + HomeAssistantError, + match="Clearing skipped update is not supported for Update with auto update", + ): + await hass.services.async_call( + DOMAIN, + "clear_skipped", + {ATTR_ENTITY_ID: "update.update_with_auto_update"}, + blocking=True, + ) + async def test_entity_with_updates_available( hass: HomeAssistant,