Add hardkernel hardware integration (#72489)

* Add hardkernel hardware integration

* Remove debug prints

* Improve tests

* Improve test coverage
This commit is contained in:
Erik Montnemery 2022-05-26 22:15:44 +02:00 committed by GitHub
parent ff3374b4e0
commit 0cca73fb23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 318 additions and 0 deletions

View file

@ -417,6 +417,8 @@ build.json @home-assistant/supervisor
/tests/components/guardian/ @bachya
/homeassistant/components/habitica/ @ASMfreaK @leikoilja
/tests/components/habitica/ @ASMfreaK @leikoilja
/homeassistant/components/hardkernel/ @home-assistant/core
/tests/components/hardkernel/ @home-assistant/core
/homeassistant/components/hardware/ @home-assistant/core
/tests/components/hardware/ @home-assistant/core
/homeassistant/components/harmony/ @ehendrix23 @bramkragten @bdraco @mkeesey @Aohzan

View file

@ -0,0 +1,22 @@
"""The Hardkernel integration."""
from __future__ import annotations
from homeassistant.components.hassio import get_os_info
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a Hardkernel config entry."""
if (os_info := get_os_info(hass)) is None:
# The hassio integration has not yet fetched data from the supervisor
raise ConfigEntryNotReady
board: str
if (board := os_info.get("board")) is None or not board.startswith("odroid"):
# Not running on a Hardkernel board, Home Assistant may have been migrated
hass.async_create_task(hass.config_entries.async_remove(entry.entry_id))
return False
return True

View file

@ -0,0 +1,22 @@
"""Config flow for the Hardkernel integration."""
from __future__ import annotations
from typing import Any
from homeassistant.config_entries import ConfigFlow
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN
class HardkernelConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Hardkernel."""
VERSION = 1
async def async_step_system(self, data: dict[str, Any] | None = None) -> FlowResult:
"""Handle the initial step."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="Hardkernel", data={})

View file

@ -0,0 +1,3 @@
"""Constants for the Hardkernel integration."""
DOMAIN = "hardkernel"

View file

@ -0,0 +1,39 @@
"""The Hardkernel hardware platform."""
from __future__ import annotations
from homeassistant.components.hardware.models import BoardInfo, HardwareInfo
from homeassistant.components.hassio import get_os_info
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from .const import DOMAIN
BOARD_NAMES = {
"odroid-c2": "Hardkernel Odroid-C2",
"odroid-c4": "Hardkernel Odroid-C4",
"odroid-n2": "Home Assistant Blue / Hardkernel Odroid-N2",
"odroid-xu4": "Hardkernel Odroid-XU4",
}
@callback
def async_info(hass: HomeAssistant) -> HardwareInfo:
"""Return board info."""
if (os_info := get_os_info(hass)) is None:
raise HomeAssistantError
board: str
if (board := os_info.get("board")) is None:
raise HomeAssistantError
if not board.startswith("odroid"):
raise HomeAssistantError
return HardwareInfo(
board=BoardInfo(
hassio_board_id=board,
manufacturer=DOMAIN,
model=board,
revision=None,
),
name=BOARD_NAMES.get(board, f"Unknown hardkernel Odroid model '{board}'"),
url=None,
)

View file

@ -0,0 +1,9 @@
{
"domain": "hardkernel",
"name": "Hardkernel",
"config_flow": false,
"documentation": "https://www.home-assistant.io/integrations/hardkernel",
"dependencies": ["hardware", "hassio"],
"codeowners": ["@home-assistant/core"],
"integration_type": "hardware"
}

View file

@ -52,6 +52,7 @@ NO_IOT_CLASS = [
"downloader",
"ffmpeg",
"frontend",
"hardkernel",
"hardware",
"history",
"homeassistant",

View file

@ -0,0 +1 @@
"""Tests for the Hardkernel integration."""

View file

@ -0,0 +1,58 @@
"""Test the Hardkernel config flow."""
from unittest.mock import patch
from homeassistant.components.hardkernel.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT, RESULT_TYPE_CREATE_ENTRY
from tests.common import MockConfigEntry, MockModule, mock_integration
async def test_config_flow(hass: HomeAssistant) -> None:
"""Test the config flow."""
mock_integration(hass, MockModule("hassio"))
with patch(
"homeassistant.components.hardkernel.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "system"}
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Hardkernel"
assert result["data"] == {}
assert result["options"] == {}
assert len(mock_setup_entry.mock_calls) == 1
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
assert config_entry.data == {}
assert config_entry.options == {}
assert config_entry.title == "Hardkernel"
async def test_config_flow_single_entry(hass: HomeAssistant) -> None:
"""Test only a single entry is allowed."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={},
title="Hardkernel",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hardkernel.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "system"}
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "single_instance_allowed"
mock_setup_entry.assert_not_called()

View file

@ -0,0 +1,89 @@
"""Test the Hardkernel hardware platform."""
from unittest.mock import patch
import pytest
from homeassistant.components.hardkernel.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, MockModule, mock_integration
async def test_hardware_info(hass: HomeAssistant, hass_ws_client) -> None:
"""Test we can get the board info."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={},
title="Hardkernel",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hardkernel.get_os_info",
return_value={"board": "odroid-n2"},
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
client = await hass_ws_client(hass)
with patch(
"homeassistant.components.hardkernel.hardware.get_os_info",
return_value={"board": "odroid-n2"},
):
await client.send_json({"id": 1, "type": "hardware/info"})
msg = await client.receive_json()
assert msg["id"] == 1
assert msg["success"]
assert msg["result"] == {
"hardware": [
{
"board": {
"hassio_board_id": "odroid-n2",
"manufacturer": "hardkernel",
"model": "odroid-n2",
"revision": None,
},
"name": "Home Assistant Blue / Hardkernel Odroid-N2",
"url": None,
}
]
}
@pytest.mark.parametrize("os_info", [None, {"board": None}, {"board": "other"}])
async def test_hardware_info_fail(hass: HomeAssistant, hass_ws_client, os_info) -> None:
"""Test async_info raises if os_info is not as expected."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={},
title="Hardkernel",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hardkernel.get_os_info",
return_value={"board": "odroid-n2"},
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
client = await hass_ws_client(hass)
with patch(
"homeassistant.components.hardkernel.hardware.get_os_info",
return_value=os_info,
):
await client.send_json({"id": 1, "type": "hardware/info"})
msg = await client.receive_json()
assert msg["id"] == 1
assert msg["success"]
assert msg["result"] == {"hardware": []}

View file

@ -0,0 +1,72 @@
"""Test the Hardkernel integration."""
from unittest.mock import patch
from homeassistant.components.hardkernel.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, MockModule, mock_integration
async def test_setup_entry(hass: HomeAssistant) -> None:
"""Test setup of a config entry."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={},
title="Hardkernel",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hardkernel.get_os_info",
return_value={"board": "odroid-n2"},
) as mock_get_os_info:
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(mock_get_os_info.mock_calls) == 1
async def test_setup_entry_wrong_board(hass: HomeAssistant) -> None:
"""Test setup of a config entry with wrong board type."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={},
title="Hardkernel",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hardkernel.get_os_info",
return_value={"board": "generic-x86-64"},
) as mock_get_os_info:
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(mock_get_os_info.mock_calls) == 1
async def test_setup_entry_wait_hassio(hass: HomeAssistant) -> None:
"""Test setup of a config entry when hassio has not fetched os_info."""
mock_integration(hass, MockModule("hassio"))
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={},
title="Hardkernel",
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.hardkernel.get_os_info",
return_value=None,
) as mock_get_os_info:
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(mock_get_os_info.mock_calls) == 1
assert config_entry.state == ConfigEntryState.SETUP_RETRY