From 34409fcd052b0196419eed26669446b877d356a6 Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sat, 9 Dec 2023 20:38:02 -0500 Subject: [PATCH] add check for empty updates (#247) * add check for empty updates * add thumbnail debugs --- custom_components/moonraker/camera.py | 13 +++++++++++++ custom_components/moonraker/sensor.py | 6 ++++-- tests/test_sensor.py | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/custom_components/moonraker/camera.py b/custom_components/moonraker/camera.py index 0e26bb8..e2a7b26 100755 --- a/custom_components/moonraker/camera.py +++ b/custom_components/moonraker/camera.py @@ -115,22 +115,31 @@ async def async_camera_image( self, width: int | None = None, height: int | None = None ) -> bytes | None: """Return current camera image.""" + _LOGGER.debug("Trying to get thumbnail ") if ( self.coordinator.data["status"]["print_stats"]["state"] != PRINTSTATES.PRINTING.value ): + _LOGGER.debug("Not printing, no thumbnail") return None del width, height new_path = self.coordinator.data["thumbnails_path"] + _LOGGER.debug(f"Thumbnail new_path: {new_path}") if self._current_path == new_path and self._current_pic is not None: + _LOGGER.debug("no change in thumbnail, returning cached") return self._current_pic if new_path == "" or new_path is None: self._current_pic = None self._current_path = "" + _LOGGER.debug("Empty path, no thumbnail") return None + + _LOGGER.debug( + f"Fetching new thumbnail: http://{self.url}/server/files/gcodes/{new_path}" + ) response = await self._session.get( f"http://{self.url}/server/files/gcodes/{new_path}" ) @@ -138,4 +147,8 @@ async def async_camera_image( self._current_path = new_path self._current_pic = await response.read() + _LOGGER.debug( + f"Size of thumbnail: {self._current_pic.width} x {self._current_pic.height}" + ) + return self._current_pic diff --git a/custom_components/moonraker/sensor.py b/custom_components/moonraker/sensor.py index 727e785..e6204ec 100755 --- a/custom_components/moonraker/sensor.py +++ b/custom_components/moonraker/sensor.py @@ -493,7 +493,6 @@ async def async_setup_machine_update_sensors(coordinator, entry, async_add_entit machine_status = await coordinator.async_fetch_data(METHODS.MACHINE_UPDATE_STATUS) if machine_status.get("error"): return - coordinator.add_data_updater(_machine_update_updater) sensors = [] @@ -509,7 +508,10 @@ async def async_setup_machine_update_sensors(coordinator, entry, async_add_entit entity_registry_enabled_default=False, ) ) - else: + elif ( + "version" in machine_status["version_info"][version_info] + and "remote_version" in machine_status["version_info"][version_info] + ): sensors.append( MoonrakerSensorDescription( key=f"machine_update_{version_info}", diff --git a/tests/test_sensor.py b/tests/test_sensor.py index ddb4068..33e1221 100755 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -458,3 +458,20 @@ async def test_update_no_system_update(hass, get_machine_update_status): entity_registry = er.async_get(hass) entity = entity_registry.async_get("sensor.mainsail_machine_update_system") assert entity is None + + +async def test_update_no_info_item(hass, get_machine_update_status): + """Test update available.""" + get_machine_update_status["version_info"]["mainsail"] = {} + + config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + config_entry.add_to_hass(hass) + assert await async_setup_entry(hass, config_entry) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + entity = entity_registry.async_get("sensor.mainsail_version_mainsail") + assert entity is None + + entity = entity_registry.async_get("sensor.mainsail_machine_update_system") + assert entity is not None