Skip to content

Commit

Permalink
Raise correct error for library in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 committed Oct 15, 2024
1 parent efba7c8 commit 97bcc9b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
27 changes: 20 additions & 7 deletions homeassistant/components/hassio/addon_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ async def async_get_addon_discovery_info(self) -> dict:
discovery_info_config: dict = discovery_info["config"]
return discovery_info_config

@api_error("Failed to get the {addon_name} add-on info")
@api_error(
"Failed to get the {addon_name} add-on info",
expected_error_type=SupervisorError,
)
async def async_get_addon_info(self) -> AddonInfo:
"""Return and cache manager add-on info."""
addon_store_info = await self._supervisor_client.store.addon_info(
Expand Down Expand Up @@ -202,11 +205,12 @@ async def async_set_addon_options(self, config: dict) -> None:

def _check_addon_available(self, addon_info: AddonInfo) -> None:
"""Check if the managed add-on is available."""

if not addon_info.available:
raise AddonError(f"{self.addon_name} add-on is not available")

@api_error("Failed to install the {addon_name} add-on")
@api_error(
"Failed to install the {addon_name} add-on", expected_error_type=SupervisorError
)
async def async_install_addon(self) -> None:
"""Install the managed add-on."""
addon_info = await self.async_get_addon_info()
Expand All @@ -215,7 +219,10 @@ async def async_install_addon(self) -> None:

await self._supervisor_client.store.install_addon(self.addon_slug)

@api_error("Failed to uninstall the {addon_name} add-on")
@api_error(
"Failed to uninstall the {addon_name} add-on",
expected_error_type=SupervisorError,
)
async def async_uninstall_addon(self) -> None:
"""Uninstall the managed add-on."""
await self._supervisor_client.addons.uninstall_addon(self.addon_slug)
Expand All @@ -238,17 +245,23 @@ async def async_update_addon(self) -> None:
self.addon_slug, StoreAddonUpdate(backup=False)
)

@api_error("Failed to start the {addon_name} add-on")
@api_error(
"Failed to start the {addon_name} add-on", expected_error_type=SupervisorError
)
async def async_start_addon(self) -> None:
"""Start the managed add-on."""
await self._supervisor_client.addons.start_addon(self.addon_slug)

@api_error("Failed to restart the {addon_name} add-on")
@api_error(
"Failed to restart the {addon_name} add-on", expected_error_type=SupervisorError
)
async def async_restart_addon(self) -> None:
"""Restart the managed add-on."""
await self._supervisor_client.addons.restart_addon(self.addon_slug)

@api_error("Failed to stop the {addon_name} add-on")
@api_error(
"Failed to stop the {addon_name} add-on", expected_error_type=SupervisorError
)
async def async_stop_addon(self) -> None:
"""Stop the managed add-on."""
await self._supervisor_client.addons.stop_addon(self.addon_slug)
Expand Down
8 changes: 4 additions & 4 deletions tests/components/hassio/test_addon_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def test_get_addon_info(
"addon_store_info_error",
"addon_store_info_calls",
),
[(SupervisorError("Boom"), 1, None, 1), (None, 0, HassioAPIError("Boom"), 1)],
[(SupervisorError("Boom"), 1, None, 1), (None, 0, SupervisorError("Boom"), 1)],
)
async def test_get_addon_info_error(
addon_manager: AddonManager,
Expand Down Expand Up @@ -216,7 +216,7 @@ async def test_install_addon_error(
"""Test install addon raises error."""
addon_store_info.return_value.available = True
addon_info.return_value.available = True
install_addon.side_effect = HassioAPIError("Boom")
install_addon.side_effect = SupervisorError("Boom")

with pytest.raises(AddonError) as err:
await addon_manager.async_install_addon()
Expand Down Expand Up @@ -267,7 +267,7 @@ async def test_schedule_install_addon_error(
install_addon: AsyncMock,
) -> None:
"""Test schedule install addon raises error."""
install_addon.side_effect = HassioAPIError("Boom")
install_addon.side_effect = SupervisorError("Boom")

with pytest.raises(AddonError) as err:
await addon_manager.async_schedule_install_addon()
Expand All @@ -284,7 +284,7 @@ async def test_schedule_install_addon_logs_error(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test schedule install addon logs error."""
install_addon.side_effect = HassioAPIError("Boom")
install_addon.side_effect = SupervisorError("Boom")

await addon_manager.async_schedule_install_addon(catch_error=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
from aiohasupervisor.models import AddonsOptions
import pytest

from homeassistant.components.hassio import (
AddonError,
AddonInfo,
AddonState,
HassIO,
HassioAPIError,
)
from homeassistant.components.hassio import AddonError, AddonInfo, AddonState, HassIO
from homeassistant.components.homeassistant_hardware import silabs_multiprotocol_addon
from homeassistant.components.zha import DOMAIN as ZHA_DOMAIN
from homeassistant.config_entries import ConfigEntry, ConfigFlow
Expand Down Expand Up @@ -1174,7 +1168,7 @@ async def test_option_flow_install_multi_pan_addon_install_fails(
) -> None:
"""Test installing the multi pan addon."""

install_addon.side_effect = HassioAPIError("Boom")
install_addon.side_effect = SupervisorError("Boom")

# Setup the config entry
config_entry = MockConfigEntry(
Expand Down Expand Up @@ -1318,7 +1312,7 @@ async def test_option_flow_addon_info_fails(
) -> None:
"""Test installing the multi pan addon."""

addon_store_info.side_effect = HassioAPIError("Boom")
addon_store_info.side_effect = SupervisorError("Boom")

# Setup the config entry
config_entry = MockConfigEntry(
Expand Down Expand Up @@ -1616,7 +1610,7 @@ async def test_check_multi_pan_addon_info_error(
) -> None:
"""Test `check_multi_pan_addon` where the addon info cannot be read."""

addon_store_info.side_effect = HassioAPIError("Boom")
addon_store_info.side_effect = SupervisorError("Boom")

with pytest.raises(HomeAssistantError):
await silabs_multiprotocol_addon.check_multi_pan_addon(hass)
Expand Down
4 changes: 2 additions & 2 deletions tests/components/matter/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ async def test_addon_not_installed_failures(
install_addon: AsyncMock,
) -> None:
"""Test add-on install failure."""
install_addon.side_effect = HassioAPIError()
install_addon.side_effect = SupervisorError()

result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
Expand Down Expand Up @@ -1355,7 +1355,7 @@ async def test_addon_not_installed_failures_zeroconf(
zeroconf_info: ZeroconfServiceInfo,
) -> None:
"""Test add-on install failure."""
install_addon.side_effect = HassioAPIError()
install_addon.side_effect = SupervisorError()

result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, data=zeroconf_info
Expand Down
8 changes: 2 additions & 6 deletions tests/components/mqtt/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

from homeassistant import config_entries
from homeassistant.components import mqtt
from homeassistant.components.hassio import (
AddonError,
HassioAPIError,
HassioServiceInfo,
)
from homeassistant.components.hassio import AddonError, HassioServiceInfo
from homeassistant.components.mqtt.config_flow import PWD_NOT_CHANGED
from homeassistant.const import (
CONF_CLIENT_ID,
Expand Down Expand Up @@ -858,7 +854,7 @@ async def test_addon_not_installed_failures(
Case: The Mosquitto add-on install fails.
"""
install_addon.side_effect = HassioAPIError()
install_addon.side_effect = SupervisorError()

result = await hass.config_entries.flow.async_init(
"mqtt", context={"source": config_entries.SOURCE_USER}
Expand Down
6 changes: 3 additions & 3 deletions tests/components/zwave_js/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ async def test_addon_running(
{"config": ADDON_DISCOVERY_INFO},
None,
None,
HassioAPIError(),
SupervisorError(),
"addon_info_failed",
),
],
Expand Down Expand Up @@ -1363,7 +1363,7 @@ async def test_addon_installed(

@pytest.mark.parametrize(
("discovery_info", "start_addon_side_effect"),
[({"config": ADDON_DISCOVERY_INFO}, HassioAPIError())],
[({"config": ADDON_DISCOVERY_INFO}, SupervisorError())],
)
async def test_addon_installed_start_failure(
hass: HomeAssistant,
Expand Down Expand Up @@ -1766,7 +1766,7 @@ async def test_install_addon_failure(
hass: HomeAssistant, supervisor, addon_not_installed, install_addon
) -> None:
"""Test add-on install failure."""
install_addon.side_effect = HassioAPIError()
install_addon.side_effect = SupervisorError()

result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
Expand Down
2 changes: 1 addition & 1 deletion tests/components/zwave_js/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ async def test_install_addon(
assert start_addon.call_args == call("core_zwave_js")


@pytest.mark.parametrize("addon_info_side_effect", [HassioAPIError("Boom")])
@pytest.mark.parametrize("addon_info_side_effect", [SupervisorError("Boom")])
async def test_addon_info_failure(
hass: HomeAssistant,
addon_installed,
Expand Down

0 comments on commit 97bcc9b

Please sign in to comment.