From 20949d39c45ea7add2fa6a2f0132f49f486f1d5c Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 3 Oct 2025 21:22:59 -0300 Subject: [PATCH] Address comments for the add-on switch entity (#153518) --- homeassistant/components/hassio/switch.py | 1 - tests/components/hassio/test_switch.py | 103 +++++++++------------- 2 files changed, 44 insertions(+), 60 deletions(-) diff --git a/homeassistant/components/hassio/switch.py b/homeassistant/components/hassio/switch.py index 43fde5190e7..4aa7813783a 100644 --- a/homeassistant/components/hassio/switch.py +++ b/homeassistant/components/hassio/switch.py @@ -73,7 +73,6 @@ class HassioAddonSwitch(HassioAddonEntity, SwitchEntity): try: await supervisor_client.addons.start_addon(self._addon_slug) except SupervisorError as err: - _LOGGER.error("Failed to start addon %s: %s", self._addon_slug, err) raise HomeAssistantError(err) from err await self.coordinator.force_addon_info_data_refresh(self._addon_slug) diff --git a/tests/components/hassio/test_switch.py b/tests/components/hassio/test_switch.py index 744a277412f..7963389e8ca 100644 --- a/tests/components/hassio/test_switch.py +++ b/tests/components/hassio/test_switch.py @@ -1,5 +1,6 @@ """The tests for the hassio switch.""" +from collections.abc import AsyncGenerator import os from unittest.mock import AsyncMock, patch @@ -18,6 +19,39 @@ from tests.test_util.aiohttp import AiohttpClientMocker MOCK_ENVIRON = {"SUPERVISOR": "127.0.0.1", "SUPERVISOR_TOKEN": "abcdefgh"} +@pytest.fixture +async def setup_integration( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> AsyncGenerator[MockConfigEntry]: + """Set up the hassio integration and enable entity.""" + config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN) + config_entry.add_to_hass(hass) + + with patch.dict(os.environ, MOCK_ENVIRON): + result = await async_setup_component( + hass, + "hassio", + {"http": {"server_port": 9999, "server_host": "127.0.0.1"}, "hassio": {}}, + ) + assert result + await hass.async_block_till_done() + + yield config_entry + + +async def enable_entity( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry: MockConfigEntry, + entity_id: str, +) -> None: + """Enable an entity and reload the config entry.""" + entity_registry.async_update_entity(entity_id, disabled_by=None) + await hass.config_entries.async_reload(config_entry.entry_id) + await hass.async_block_till_done() + + @pytest.fixture(autouse=True) def mock_all( aioclient_mock: AiohttpClientMocker, @@ -170,31 +204,18 @@ async def test_switch_state( entity_id: str, expected: str, addon_state: str, - aioclient_mock: AiohttpClientMocker, entity_registry: er.EntityRegistry, addon_installed: AsyncMock, + setup_integration: MockConfigEntry, ) -> None: """Test hassio addon switch state.""" addon_installed.return_value.state = addon_state - config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN) - config_entry.add_to_hass(hass) - - with patch.dict(os.environ, MOCK_ENVIRON): - result = await async_setup_component( - hass, - "hassio", - {"http": {"server_port": 9999, "server_host": "127.0.0.1"}, "hassio": {}}, - ) - assert result - await hass.async_block_till_done() # Verify that the entity is disabled by default. assert hass.states.get(entity_id) is None # Enable the entity. - entity_registry.async_update_entity(entity_id, disabled_by=None) - await hass.config_entries.async_reload(config_entry.entry_id) - await hass.async_block_till_done() + await enable_entity(hass, entity_registry, setup_integration, entity_id) # Verify that the entity have the expected state. state = hass.states.get(entity_id) @@ -210,6 +231,7 @@ async def test_switch_turn_on( aioclient_mock: AiohttpClientMocker, entity_registry: er.EntityRegistry, addon_installed: AsyncMock, + setup_integration: MockConfigEntry, ) -> None: """Test turning on addon switch.""" entity_id = "switch.test_two" @@ -218,25 +240,11 @@ async def test_switch_turn_on( # Mock the start addon API call aioclient_mock.post("http://127.0.0.1/addons/test-two/start", json={"result": "ok"}) - config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN) - config_entry.add_to_hass(hass) - - with patch.dict(os.environ, MOCK_ENVIRON): - result = await async_setup_component( - hass, - "hassio", - {"http": {"server_port": 9999, "server_host": "127.0.0.1"}, "hassio": {}}, - ) - assert result - await hass.async_block_till_done() - # Verify that the entity is disabled by default. assert hass.states.get(entity_id) is None # Enable the entity. - entity_registry.async_update_entity(entity_id, disabled_by=None) - await hass.config_entries.async_reload(config_entry.entry_id) - await hass.async_block_till_done() + await enable_entity(hass, entity_registry, setup_integration, entity_id) # Verify initial state is off state = hass.states.get(entity_id) @@ -252,13 +260,8 @@ async def test_switch_turn_on( ) # Verify the API was called - assert len(aioclient_mock.mock_calls) > 0 - start_call_found = False - for call in aioclient_mock.mock_calls: - if call[1].path == "/addons/test-two/start" and call[0] == "POST": - start_call_found = True - break - assert start_call_found + assert aioclient_mock.mock_calls[-1][1].path == "/addons/test-two/start" + assert aioclient_mock.mock_calls[-1][0] == "POST" @pytest.mark.parametrize( @@ -269,6 +272,7 @@ async def test_switch_turn_off( aioclient_mock: AiohttpClientMocker, entity_registry: er.EntityRegistry, addon_installed: AsyncMock, + setup_integration: MockConfigEntry, ) -> None: """Test turning off addon switch.""" entity_id = "switch.test" @@ -277,25 +281,11 @@ async def test_switch_turn_off( # Mock the stop addon API call aioclient_mock.post("http://127.0.0.1/addons/test/stop", json={"result": "ok"}) - config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN) - config_entry.add_to_hass(hass) - - with patch.dict(os.environ, MOCK_ENVIRON): - result = await async_setup_component( - hass, - "hassio", - {"http": {"server_port": 9999, "server_host": "127.0.0.1"}, "hassio": {}}, - ) - assert result - await hass.async_block_till_done() - # Verify that the entity is disabled by default. assert hass.states.get(entity_id) is None # Enable the entity. - entity_registry.async_update_entity(entity_id, disabled_by=None) - await hass.config_entries.async_reload(config_entry.entry_id) - await hass.async_block_till_done() + await enable_entity(hass, entity_registry, setup_integration, entity_id) # Verify initial state is on state = hass.states.get(entity_id) @@ -311,10 +301,5 @@ async def test_switch_turn_off( ) # Verify the API was called - assert len(aioclient_mock.mock_calls) > 0 - stop_call_found = False - for call in aioclient_mock.mock_calls: - if call[1].path == "/addons/test/stop" and call[0] == "POST": - stop_call_found = True - break - assert stop_call_found + assert aioclient_mock.mock_calls[-1][1].path == "/addons/test/stop" + assert aioclient_mock.mock_calls[-1][0] == "POST"