Improve Shelly RPC device update progress (#114566)

Co-authored-by: Shay Levy <levyshay1@gmail.com>
Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
pull/114764/head
Maciej Bieniek 2024-04-02 10:15:58 +02:00 committed by Franck Nijhof
parent 18b6de567d
commit 7b84e86f89
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 21 additions and 14 deletions

View File

@ -222,7 +222,7 @@ class RpcUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity):
) -> None:
"""Initialize update entity."""
super().__init__(coordinator, key, attribute, description)
self._ota_in_progress: bool = False
self._ota_in_progress: bool | int = False
self._attr_release_url = get_release_url(
coordinator.device.gen, coordinator.model, description.beta
)
@ -237,14 +237,13 @@ class RpcUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity):
@callback
def _ota_progress_callback(self, event: dict[str, Any]) -> None:
"""Handle device OTA progress."""
if self._ota_in_progress:
if self.in_progress is not False:
event_type = event["event"]
if event_type == OTA_BEGIN:
self._attr_in_progress = 0
self._ota_in_progress = 0
elif event_type == OTA_PROGRESS:
self._attr_in_progress = event["progress_percent"]
self._ota_in_progress = event["progress_percent"]
elif event_type in (OTA_ERROR, OTA_SUCCESS):
self._attr_in_progress = False
self._ota_in_progress = False
self.async_write_ha_state()
@ -262,6 +261,11 @@ class RpcUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity):
return self.installed_version
@property
def in_progress(self) -> bool | int:
"""Update installation in progress."""
return self._ota_in_progress
async def async_install(
self, version: str | None, backup: bool, **kwargs: Any
) -> None:
@ -292,7 +296,7 @@ class RpcUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity):
await self.coordinator.async_shutdown_device_and_start_reauth()
else:
self._ota_in_progress = True
LOGGER.debug("OTA update call successful")
LOGGER.info("OTA update call for %s successful", self.coordinator.name)
class RpcSleepingUpdateEntity(

View File

@ -255,6 +255,16 @@ async def test_rpc_update(
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
assert mock_rpc_device.trigger_ota_update.call_count == 1
state = hass.states.get(entity_id)
assert state.state == STATE_ON
assert state.attributes[ATTR_INSTALLED_VERSION] == "1"
assert state.attributes[ATTR_LATEST_VERSION] == "2"
assert state.attributes[ATTR_IN_PROGRESS] is True
assert state.attributes[ATTR_RELEASE_URL] == GEN2_RELEASE_URL
inject_rpc_device_event(
monkeypatch,
mock_rpc_device,
@ -270,14 +280,7 @@ async def test_rpc_update(
},
)
assert mock_rpc_device.trigger_ota_update.call_count == 1
state = hass.states.get(entity_id)
assert state.state == STATE_ON
assert state.attributes[ATTR_INSTALLED_VERSION] == "1"
assert state.attributes[ATTR_LATEST_VERSION] == "2"
assert state.attributes[ATTR_IN_PROGRESS] == 0
assert state.attributes[ATTR_RELEASE_URL] == GEN2_RELEASE_URL
assert hass.states.get(entity_id).attributes[ATTR_IN_PROGRESS] == 0
inject_rpc_device_event(
monkeypatch,