Skip to content

Commit d3bebd9

Browse files
authored
Fix SolarEdge unload failing when there are no sensors (home-assistant#155979)
1 parent 53c807b commit d3bebd9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

homeassistant/components/solaredge/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: SolarEdgeConfigEntry) ->
5757

5858
async def async_unload_entry(hass: HomeAssistant, entry: SolarEdgeConfigEntry) -> bool:
5959
"""Unload SolarEdge config entry."""
60+
if DATA_API_CLIENT not in entry.runtime_data:
61+
return True # Nothing to unload
62+
6063
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

tests/components/solaredge/test_init.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for the SolarEdge integration."""
22

3-
from unittest.mock import AsyncMock, Mock
3+
from unittest.mock import AsyncMock, Mock, patch
44

55
from aiohttp import ClientError
66

@@ -15,8 +15,15 @@
1515
from tests.common import MockConfigEntry
1616

1717

18+
@patch(
19+
"homeassistant.config_entries.ConfigEntries.async_unload_platforms",
20+
return_value=True,
21+
)
1822
async def test_setup_unload_api_key(
19-
recorder_mock: Recorder, hass: HomeAssistant, solaredge_api: Mock
23+
mock_unload_platforms: AsyncMock,
24+
recorder_mock: Recorder,
25+
hass: HomeAssistant,
26+
solaredge_api: Mock,
2027
) -> None:
2128
"""Test successful setup and unload of a config entry with API key."""
2229
entry = MockConfigEntry(
@@ -33,11 +40,21 @@ async def test_setup_unload_api_key(
3340

3441
assert await hass.config_entries.async_unload(entry.entry_id)
3542
await hass.async_block_till_done()
43+
44+
# Unloading should be attempted because sensors were set up.
45+
mock_unload_platforms.assert_awaited_once()
3646
assert entry.state is ConfigEntryState.NOT_LOADED
3747

3848

49+
@patch(
50+
"homeassistant.config_entries.ConfigEntries.async_unload_platforms",
51+
return_value=True,
52+
)
3953
async def test_setup_unload_web_login(
40-
recorder_mock: Recorder, hass: HomeAssistant, solaredge_web_api: AsyncMock
54+
mock_unload_platforms: AsyncMock,
55+
recorder_mock: Recorder,
56+
hass: HomeAssistant,
57+
solaredge_web_api: AsyncMock,
4158
) -> None:
4259
"""Test successful setup and unload of a config entry with web login."""
4360
entry = MockConfigEntry(
@@ -59,10 +76,18 @@ async def test_setup_unload_web_login(
5976

6077
assert await hass.config_entries.async_unload(entry.entry_id)
6178
await hass.async_block_till_done()
79+
80+
# Unloading should NOT be attempted because sensors were not set up.
81+
mock_unload_platforms.assert_not_called()
6282
assert entry.state is ConfigEntryState.NOT_LOADED
6383

6484

85+
@patch(
86+
"homeassistant.config_entries.ConfigEntries.async_unload_platforms",
87+
return_value=True,
88+
)
6589
async def test_setup_unload_both(
90+
mock_unload_platforms: AsyncMock,
6691
recorder_mock: Recorder,
6792
hass: HomeAssistant,
6893
solaredge_api: Mock,
@@ -90,6 +115,8 @@ async def test_setup_unload_both(
90115

91116
assert await hass.config_entries.async_unload(entry.entry_id)
92117
await hass.async_block_till_done()
118+
119+
mock_unload_platforms.assert_awaited_once()
93120
assert entry.state is ConfigEntryState.NOT_LOADED
94121

95122

0 commit comments

Comments
 (0)