From 0ae855d345f4865a0f79a3dc161d8c8d53266c3e Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Wed, 11 Jan 2023 19:53:06 -0700 Subject: [PATCH] Fix Litter-Robot 4 firmware versions reported while updating (#85710) --- .../components/litterrobot/manifest.json | 2 +- .../components/litterrobot/update.py | 15 +++++---- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/litterrobot/test_update.py | 33 ++++++++++++++++++- 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/litterrobot/manifest.json b/homeassistant/components/litterrobot/manifest.json index ea656a3488e..a6c392f4f62 100644 --- a/homeassistant/components/litterrobot/manifest.json +++ b/homeassistant/components/litterrobot/manifest.json @@ -3,7 +3,7 @@ "name": "Litter-Robot", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/litterrobot", - "requirements": ["pylitterbot==2023.1.0"], + "requirements": ["pylitterbot==2023.1.1"], "codeowners": ["@natekspencer", "@tkdrob"], "dhcp": [{ "hostname": "litter-robot4" }], "iot_class": "cloud_push", diff --git a/homeassistant/components/litterrobot/update.py b/homeassistant/components/litterrobot/update.py index 2ed46220a8c..845b42efaee 100644 --- a/homeassistant/components/litterrobot/update.py +++ b/homeassistant/components/litterrobot/update.py @@ -69,19 +69,20 @@ class RobotUpdateEntity(LitterRobotEntity[LitterRobot4], UpdateEntity): async def async_update(self) -> None: """Update the entity.""" - if await self.robot.has_firmware_update(): - latest_version = await self.robot.get_latest_firmware() - else: - latest_version = self.installed_version - - if self._attr_latest_version != self.installed_version: + # If the robot has a firmware update already in progress, checking for the + # latest firmware informs that an update has already been triggered, no + # firmware information is returned and we won't know the latest version. + if not self.robot.firmware_update_triggered: + latest_version = await self.robot.get_latest_firmware(True) + if not await self.robot.has_firmware_update(): + latest_version = self.robot.firmware self._attr_latest_version = latest_version async def async_install( self, version: str | None, backup: bool, **kwargs: Any ) -> None: """Install an update.""" - if await self.robot.has_firmware_update(): + if await self.robot.has_firmware_update(True): if not await self.robot.update_firmware(): message = f"Unable to start firmware update on {self.robot.name}" raise HomeAssistantError(message) diff --git a/requirements_all.txt b/requirements_all.txt index 078d7791234..f68ed0cf697 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1741,7 +1741,7 @@ pylibrespot-java==0.1.1 pylitejet==0.3.0 # homeassistant.components.litterrobot -pylitterbot==2023.1.0 +pylitterbot==2023.1.1 # homeassistant.components.lutron_caseta pylutron-caseta==0.17.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7226e928eb4..fce85207534 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1248,7 +1248,7 @@ pylibrespot-java==0.1.1 pylitejet==0.3.0 # homeassistant.components.litterrobot -pylitterbot==2023.1.0 +pylitterbot==2023.1.1 # homeassistant.components.lutron_caseta pylutron-caseta==0.17.1 diff --git a/tests/components/litterrobot/test_update.py b/tests/components/litterrobot/test_update.py index f4311992c8e..4940ec64824 100644 --- a/tests/components/litterrobot/test_update.py +++ b/tests/components/litterrobot/test_update.py @@ -11,7 +11,13 @@ from homeassistant.components.update import ( SERVICE_INSTALL, UpdateDeviceClass, ) -from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, STATE_OFF, STATE_ON +from homeassistant.const import ( + ATTR_DEVICE_CLASS, + ATTR_ENTITY_ID, + STATE_OFF, + STATE_ON, + STATE_UNKNOWN, +) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -28,6 +34,7 @@ async def test_robot_with_no_update( """Tests the update entity was set up.""" robot: LitterRobot4 = mock_account_with_litterrobot_4.robots[0] robot.has_firmware_update = AsyncMock(return_value=False) + robot.get_latest_firmware = AsyncMock(return_value=None) entry = await setup_integration( hass, mock_account_with_litterrobot_4, PLATFORM_DOMAIN @@ -79,3 +86,27 @@ async def test_robot_with_update( ) await hass.async_block_till_done() assert robot.update_firmware.call_count == 1 + + +async def test_robot_with_update_already_in_progress( + hass: HomeAssistant, mock_account_with_litterrobot_4: MagicMock +): + """Tests the update entity was set up.""" + robot: LitterRobot4 = mock_account_with_litterrobot_4.robots[0] + robot._update_data( # pylint:disable=protected-access + {"isFirmwareUpdateTriggered": True}, partial=True + ) + + entry = await setup_integration( + hass, mock_account_with_litterrobot_4, PLATFORM_DOMAIN + ) + + state = hass.states.get(ENTITY_ID) + assert state + assert state.state == STATE_UNKNOWN + assert state.attributes[ATTR_DEVICE_CLASS] == UpdateDeviceClass.FIRMWARE + assert state.attributes[ATTR_INSTALLED_VERSION] == OLD_FIRMWARE + assert state.attributes[ATTR_LATEST_VERSION] is None + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done()