Skip to content

Commit

Permalink
Handle unloaded config entry in system_health_info (#3605)
Browse files Browse the repository at this point in the history
* Handle unloaded config entry in system_health_info

* Update test

* Use snapshot

* Add the snapshot
  • Loading branch information
emontnemery authored Apr 11, 2024
1 parent 5de7986 commit bd02037
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion custom_components/hacs/system_health.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Provide info to system health."""
from typing import Any

from aiogithubapi.common.const import BASE_API_URL
from homeassistant.components import system_health
from homeassistant.core import HomeAssistant, callback
Expand All @@ -17,8 +19,11 @@ def async_register(hass: HomeAssistant, register: system_health.SystemHealthRegi
register.async_register_info(system_health_info, "/hacs")


async def system_health_info(hass):
async def system_health_info(hass: HomeAssistant) -> dict[str, Any]:
"""Get info for the info page."""
if DOMAIN not in hass.data:
return {"Disabled": "HACS is not loaded, but HA still requests this information..."}

hacs: HacsBase = hass.data[DOMAIN]
response = await hacs.githubapi.rate_limit()

Expand Down
3 changes: 3 additions & 0 deletions tests/snapshots/system_health/system_health_after_unload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Disabled": "HACS is not loaded, but HA still requests this information..."
}
6 changes: 4 additions & 2 deletions tests/test_system_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ async def test_system_health(
async def test_system_health_after_unload(
hacs: HacsBase,
hass: HomeAssistant,
snapshots: SnapshotFixture,
) -> None:
"""Test HACS system health."""
await hass.config_entries.async_unload(hacs.configuration.config_entry.entry_id)

assert await async_setup_component(hass, "system_health", {})
await hass.async_block_till_done()

with pytest.raises(KeyError, match="hacs"):
await get_system_health_info(hass, HACS_SYSTEM_HEALTH_DOMAIN)
info = await get_system_health_info(hass, HACS_SYSTEM_HEALTH_DOMAIN)

snapshots.assert_match(safe_json_dumps(info), "system_health/system_health_after_unload.json")


async def test_system_health_no_hacs(
Expand Down

0 comments on commit bd02037

Please sign in to comment.