diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 306c9d43d72..a2a9d8ff028 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -119,7 +119,6 @@ from .handler import ( # noqa: F401 async_create_backup, async_get_green_settings, async_get_yellow_settings, - async_reboot_host, async_set_green_settings, async_set_yellow_settings, async_update_diagnostics, diff --git a/homeassistant/components/hassio/handler.py b/homeassistant/components/hassio/handler.py index 58f2aa8c144..254c392462c 100644 --- a/homeassistant/components/hassio/handler.py +++ b/homeassistant/components/hassio/handler.py @@ -133,16 +133,6 @@ async def async_set_yellow_settings( ) -@api_data -async def async_reboot_host(hass: HomeAssistant) -> dict: - """Reboot the host. - - Returns an empty dict. - """ - hassio: HassIO = hass.data[DOMAIN] - return await hassio.send_command("/host/reboot", method="post", timeout=60) - - class HassIO: """Small API wrapper for Hass.io.""" diff --git a/homeassistant/components/homeassistant_yellow/config_flow.py b/homeassistant/components/homeassistant_yellow/config_flow.py index 9edc5009171..2c58ecdfc1c 100644 --- a/homeassistant/components/homeassistant_yellow/config_flow.py +++ b/homeassistant/components/homeassistant_yellow/config_flow.py @@ -14,8 +14,8 @@ import voluptuous as vol from homeassistant.components.hassio import ( HassioAPIError, async_get_yellow_settings, - async_reboot_host, async_set_yellow_settings, + get_supervisor_client, ) from homeassistant.components.homeassistant_hardware.firmware_config_flow import ( BaseFirmwareConfigFlow, @@ -31,7 +31,7 @@ from homeassistant.config_entries import ( ConfigFlowResult, OptionsFlow, ) -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, async_get_hass, callback from homeassistant.helpers import discovery_flow, selector from .const import DOMAIN, FIRMWARE, RADIO_DEVICE, ZHA_DOMAIN, ZHA_HW_DISCOVERY_DATA @@ -67,11 +67,12 @@ class HomeAssistantYellowConfigFlow(BaseFirmwareConfigFlow, domain=DOMAIN): ) -> OptionsFlow: """Return the options flow.""" firmware_type = ApplicationType(config_entry.data[FIRMWARE]) + hass = async_get_hass() if firmware_type is ApplicationType.CPC: - return HomeAssistantYellowMultiPanOptionsFlowHandler(config_entry) + return HomeAssistantYellowMultiPanOptionsFlowHandler(hass, config_entry) - return HomeAssistantYellowOptionsFlowHandler(config_entry) + return HomeAssistantYellowOptionsFlowHandler(hass, config_entry) async def async_step_system( self, data: dict[str, Any] | None = None @@ -107,6 +108,11 @@ class BaseHomeAssistantYellowOptionsFlow(OptionsFlow, ABC): _hw_settings: dict[str, bool] | None = None + def __init__(self, hass: HomeAssistant, *args: Any, **kwargs: Any) -> None: + """Instantiate options flow.""" + super().__init__(*args, **kwargs) + self._supervisor_client = get_supervisor_client(hass) + @abstractmethod async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult: """Show the main menu.""" @@ -172,7 +178,7 @@ class BaseHomeAssistantYellowOptionsFlow(OptionsFlow, ABC): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Reboot now.""" - await async_reboot_host(self.hass) + await self._supervisor_client.host.reboot() return self.async_create_entry(data={}) async def async_step_reboot_later( @@ -251,9 +257,9 @@ class HomeAssistantYellowOptionsFlowHandler( ): """Handle a firmware options flow for Home Assistant Yellow.""" - def __init__(self, *args: Any, **kwargs: Any) -> None: + def __init__(self, hass: HomeAssistant, *args: Any, **kwargs: Any) -> None: """Instantiate options flow.""" - super().__init__(*args, **kwargs) + super().__init__(hass, *args, **kwargs) self._hardware_name = BOARD_NAME self._device = RADIO_DEVICE diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 5535ec3b976..8e49072e496 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -507,6 +507,7 @@ def supervisor_client() -> Generator[AsyncMock]: supervisor_client.addons = AsyncMock() supervisor_client.discovery = AsyncMock() supervisor_client.homeassistant = AsyncMock() + supervisor_client.host = AsyncMock() supervisor_client.os = AsyncMock() supervisor_client.resolution = AsyncMock() supervisor_client.supervisor = AsyncMock() diff --git a/tests/components/hassio/test_handler.py b/tests/components/hassio/test_handler.py index 56f0dcb706c..e6375171dab 100644 --- a/tests/components/hassio/test_handler.py +++ b/tests/components/hassio/test_handler.py @@ -341,20 +341,6 @@ async def test_api_set_yellow_settings( assert aioclient_mock.call_count == 1 -@pytest.mark.usefixtures("hassio_stubs") -async def test_api_reboot_host( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker -) -> None: - """Test setup with API ping.""" - aioclient_mock.post( - "http://127.0.0.1/host/reboot", - json={"result": "ok", "data": {}}, - ) - - assert await handler.async_reboot_host(hass) == {} - assert aioclient_mock.call_count == 1 - - @pytest.mark.usefixtures("hassio_stubs") async def test_send_command_invalid_command(hass: HomeAssistant) -> None: """Test send command fails when command is invalid.""" diff --git a/tests/components/homeassistant_yellow/test_config_flow.py b/tests/components/homeassistant_yellow/test_config_flow.py index ab6f158b211..1067be7b56e 100644 --- a/tests/components/homeassistant_yellow/test_config_flow.py +++ b/tests/components/homeassistant_yellow/test_config_flow.py @@ -1,7 +1,7 @@ """Test the Home Assistant Yellow config flow.""" from collections.abc import Generator -from unittest.mock import Mock, patch +from unittest.mock import AsyncMock, Mock, patch import pytest @@ -36,6 +36,16 @@ def config_flow_handler(hass: HomeAssistant) -> Generator[None]: yield +@pytest.fixture(autouse=True) +def mock_get_supervisor_client(supervisor_client: AsyncMock) -> Generator[None]: + """Mock get_supervisor_client method.""" + with patch( + "homeassistant.components.homeassistant_yellow.config_flow.get_supervisor_client", + return_value=supervisor_client, + ): + yield + + @pytest.fixture(name="get_yellow_settings") def mock_get_yellow_settings(): """Mock getting yellow settings.""" @@ -56,12 +66,9 @@ def mock_set_yellow_settings(): @pytest.fixture(name="reboot_host") -def mock_reboot_host(): +def mock_reboot_host(supervisor_client: AsyncMock) -> AsyncMock: """Mock rebooting host.""" - with patch( - "homeassistant.components.homeassistant_yellow.config_flow.async_reboot_host", - ) as reboot_host: - yield reboot_host + return supervisor_client.host.reboot async def test_config_flow(hass: HomeAssistant) -> None: @@ -130,11 +137,11 @@ async def test_config_flow_single_entry(hass: HomeAssistant) -> None: ) async def test_option_flow_led_settings( hass: HomeAssistant, - get_yellow_settings, - set_yellow_settings, - reboot_host, - reboot_menu_choice, - reboot_calls, + get_yellow_settings: AsyncMock, + set_yellow_settings: AsyncMock, + reboot_host: AsyncMock, + reboot_menu_choice: str, + reboot_calls: int, ) -> None: """Test updating LED settings.""" mock_integration(hass, MockModule("hassio")) @@ -176,7 +183,7 @@ async def test_option_flow_led_settings( {"next_step_id": reboot_menu_choice}, ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert len(reboot_host.mock_calls) == reboot_calls + assert reboot_host.call_count == reboot_calls async def test_option_flow_led_settings_unchanged(