Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
Mike Degatano
1b528ba96f Remove unnecessary init 2024-11-12 14:32:17 +00:00
Mike Degatano
9db3ff9059 Remove invalid test 2024-11-12 14:32:17 +00:00
Mike Degatano
147c578592 Reboot host to aiohasupervisor 2024-11-12 14:32:17 +00:00
6 changed files with 33 additions and 44 deletions

View file

@ -119,7 +119,6 @@ from .handler import ( # noqa: F401
async_create_backup, async_create_backup,
async_get_green_settings, async_get_green_settings,
async_get_yellow_settings, async_get_yellow_settings,
async_reboot_host,
async_set_green_settings, async_set_green_settings,
async_set_yellow_settings, async_set_yellow_settings,
async_update_diagnostics, async_update_diagnostics,

View file

@ -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: class HassIO:
"""Small API wrapper for Hass.io.""" """Small API wrapper for Hass.io."""

View file

@ -14,8 +14,8 @@ import voluptuous as vol
from homeassistant.components.hassio import ( from homeassistant.components.hassio import (
HassioAPIError, HassioAPIError,
async_get_yellow_settings, async_get_yellow_settings,
async_reboot_host,
async_set_yellow_settings, async_set_yellow_settings,
get_supervisor_client,
) )
from homeassistant.components.homeassistant_hardware.firmware_config_flow import ( from homeassistant.components.homeassistant_hardware.firmware_config_flow import (
BaseFirmwareConfigFlow, BaseFirmwareConfigFlow,
@ -31,7 +31,7 @@ from homeassistant.config_entries import (
ConfigFlowResult, ConfigFlowResult,
OptionsFlow, OptionsFlow,
) )
from homeassistant.core import callback from homeassistant.core import HomeAssistant, async_get_hass, callback
from homeassistant.helpers import discovery_flow, selector from homeassistant.helpers import discovery_flow, selector
from .const import DOMAIN, FIRMWARE, RADIO_DEVICE, ZHA_DOMAIN, ZHA_HW_DISCOVERY_DATA from .const import DOMAIN, FIRMWARE, RADIO_DEVICE, ZHA_DOMAIN, ZHA_HW_DISCOVERY_DATA
@ -67,11 +67,12 @@ class HomeAssistantYellowConfigFlow(BaseFirmwareConfigFlow, domain=DOMAIN):
) -> OptionsFlow: ) -> OptionsFlow:
"""Return the options flow.""" """Return the options flow."""
firmware_type = ApplicationType(config_entry.data[FIRMWARE]) firmware_type = ApplicationType(config_entry.data[FIRMWARE])
hass = async_get_hass()
if firmware_type is ApplicationType.CPC: 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( async def async_step_system(
self, data: dict[str, Any] | None = None self, data: dict[str, Any] | None = None
@ -107,6 +108,11 @@ class BaseHomeAssistantYellowOptionsFlow(OptionsFlow, ABC):
_hw_settings: dict[str, bool] | None = None _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 @abstractmethod
async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult: async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult:
"""Show the main menu.""" """Show the main menu."""
@ -172,7 +178,7 @@ class BaseHomeAssistantYellowOptionsFlow(OptionsFlow, ABC):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Reboot now.""" """Reboot now."""
await async_reboot_host(self.hass) await self._supervisor_client.host.reboot()
return self.async_create_entry(data={}) return self.async_create_entry(data={})
async def async_step_reboot_later( async def async_step_reboot_later(
@ -251,9 +257,9 @@ class HomeAssistantYellowOptionsFlowHandler(
): ):
"""Handle a firmware options flow for Home Assistant Yellow.""" """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.""" """Instantiate options flow."""
super().__init__(*args, **kwargs) super().__init__(hass, *args, **kwargs)
self._hardware_name = BOARD_NAME self._hardware_name = BOARD_NAME
self._device = RADIO_DEVICE self._device = RADIO_DEVICE

View file

@ -507,6 +507,7 @@ def supervisor_client() -> Generator[AsyncMock]:
supervisor_client.addons = AsyncMock() supervisor_client.addons = AsyncMock()
supervisor_client.discovery = AsyncMock() supervisor_client.discovery = AsyncMock()
supervisor_client.homeassistant = AsyncMock() supervisor_client.homeassistant = AsyncMock()
supervisor_client.host = AsyncMock()
supervisor_client.os = AsyncMock() supervisor_client.os = AsyncMock()
supervisor_client.resolution = AsyncMock() supervisor_client.resolution = AsyncMock()
supervisor_client.supervisor = AsyncMock() supervisor_client.supervisor = AsyncMock()

View file

@ -341,20 +341,6 @@ async def test_api_set_yellow_settings(
assert aioclient_mock.call_count == 1 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") @pytest.mark.usefixtures("hassio_stubs")
async def test_send_command_invalid_command(hass: HomeAssistant) -> None: async def test_send_command_invalid_command(hass: HomeAssistant) -> None:
"""Test send command fails when command is invalid.""" """Test send command fails when command is invalid."""

View file

@ -1,7 +1,7 @@
"""Test the Home Assistant Yellow config flow.""" """Test the Home Assistant Yellow config flow."""
from collections.abc import Generator from collections.abc import Generator
from unittest.mock import Mock, patch from unittest.mock import AsyncMock, Mock, patch
import pytest import pytest
@ -36,6 +36,16 @@ def config_flow_handler(hass: HomeAssistant) -> Generator[None]:
yield 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") @pytest.fixture(name="get_yellow_settings")
def mock_get_yellow_settings(): def mock_get_yellow_settings():
"""Mock getting yellow settings.""" """Mock getting yellow settings."""
@ -56,12 +66,9 @@ def mock_set_yellow_settings():
@pytest.fixture(name="reboot_host") @pytest.fixture(name="reboot_host")
def mock_reboot_host(): def mock_reboot_host(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock rebooting host.""" """Mock rebooting host."""
with patch( return supervisor_client.host.reboot
"homeassistant.components.homeassistant_yellow.config_flow.async_reboot_host",
) as reboot_host:
yield reboot_host
async def test_config_flow(hass: HomeAssistant) -> None: 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( async def test_option_flow_led_settings(
hass: HomeAssistant, hass: HomeAssistant,
get_yellow_settings, get_yellow_settings: AsyncMock,
set_yellow_settings, set_yellow_settings: AsyncMock,
reboot_host, reboot_host: AsyncMock,
reboot_menu_choice, reboot_menu_choice: str,
reboot_calls, reboot_calls: int,
) -> None: ) -> None:
"""Test updating LED settings.""" """Test updating LED settings."""
mock_integration(hass, MockModule("hassio")) mock_integration(hass, MockModule("hassio"))
@ -176,7 +183,7 @@ async def test_option_flow_led_settings(
{"next_step_id": reboot_menu_choice}, {"next_step_id": reboot_menu_choice},
) )
assert result["type"] is FlowResultType.CREATE_ENTRY 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( async def test_option_flow_led_settings_unchanged(