From eb5c6076af76cfe9b254ae2f8d052216d7af5994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 26 Jan 2022 16:31:21 +0100 Subject: [PATCH] Check for empty release array (#64973) --- .../components/github/coordinator.py | 2 +- tests/components/github/test_sensor.py | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/github/coordinator.py b/homeassistant/components/github/coordinator.py index 3c36bd21b30..faa48e44d0b 100644 --- a/homeassistant/components/github/coordinator.py +++ b/homeassistant/components/github/coordinator.py @@ -97,7 +97,7 @@ class RepositoryReleaseDataUpdateCoordinator( response: GitHubResponseModel[GitHubReleaseModel | None], ) -> GitHubReleaseModel | None: """Parse the response from GitHub API.""" - if response.data is None: + if not response.data: return None for release in response.data: diff --git a/tests/components/github/test_sensor.py b/tests/components/github/test_sensor.py index 24870adbfba..cea3edc6b47 100644 --- a/tests/components/github/test_sensor.py +++ b/tests/components/github/test_sensor.py @@ -1,5 +1,5 @@ """Test GitHub sensor.""" -from unittest.mock import patch +from unittest.mock import MagicMock, patch from aiogithubapi import GitHubNotModifiedException import pytest @@ -40,3 +40,23 @@ async def test_sensor_updates_with_not_modified_content( ) new_state = hass.states.get(TEST_SENSOR_ENTITY) assert state.state == new_state.state + + +async def test_sensor_updates_with_empty_release_array( + hass: HomeAssistant, + init_integration: MockConfigEntry, +) -> None: + """Test the sensor updates by default GitHub sensors.""" + state = hass.states.get(TEST_SENSOR_ENTITY) + assert state.state == "v1.0.0" + + with patch( + "aiogithubapi.namespaces.releases.GitHubReleasesNamespace.list", + return_value=MagicMock(data=[]), + ): + + async_fire_time_changed(hass, dt.utcnow() + DEFAULT_UPDATE_INTERVAL) + await hass.async_block_till_done() + + new_state = hass.states.get(TEST_SENSOR_ENTITY) + assert new_state.state == "unavailable"