init home-assistant
This commit is contained in:
1
home-assistant/tests/components/raspberry_pi/__init__.py
Normal file
1
home-assistant/tests/components/raspberry_pi/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the Raspberry Pi integration."""
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Test the Raspberry Pi config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.raspberry_pi.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
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.raspberry_pi.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"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Raspberry Pi"
|
||||
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 == "Raspberry Pi"
|
||||
|
||||
|
||||
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="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.raspberry_pi.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"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
mock_setup_entry.assert_not_called()
|
||||
103
home-assistant/tests/components/raspberry_pi/test_hardware.py
Normal file
103
home-assistant/tests/components/raspberry_pi/test_hardware.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""Test the Raspberry Pi hardware platform."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN
|
||||
from homeassistant.components.raspberry_pi.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, MockModule, mock_integration
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
|
||||
async def test_hardware_info(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test we can get the board info."""
|
||||
mock_integration(hass, MockModule("hassio"))
|
||||
await async_setup_component(hass, HASSIO_DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={},
|
||||
title="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.raspberry_pi.get_os_info",
|
||||
return_value={"board": "rpi"},
|
||||
):
|
||||
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.raspberry_pi.hardware.get_os_info",
|
||||
return_value={"board": "rpi"},
|
||||
):
|
||||
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": "rpi",
|
||||
"manufacturer": "raspberry_pi",
|
||||
"model": "1",
|
||||
"revision": None,
|
||||
},
|
||||
"config_entries": [config_entry.entry_id],
|
||||
"dongle": None,
|
||||
"name": "Raspberry Pi",
|
||||
"url": None,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("os_info", [None, {"board": None}, {"board": "other"}])
|
||||
async def test_hardware_info_fail(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, os_info
|
||||
) -> None:
|
||||
"""Test async_info raises if os_info is not as expected."""
|
||||
mock_integration(hass, MockModule("hassio"))
|
||||
await async_setup_component(hass, HASSIO_DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={},
|
||||
title="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.raspberry_pi.get_os_info",
|
||||
return_value={"board": "rpi"},
|
||||
):
|
||||
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.raspberry_pi.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": []}
|
||||
121
home-assistant/tests/components/raspberry_pi/test_init.py
Normal file
121
home-assistant/tests/components/raspberry_pi/test_init.py
Normal file
@@ -0,0 +1,121 @@
|
||||
"""Test the Raspberry Pi integration."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN
|
||||
from homeassistant.components.raspberry_pi.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, MockModule, mock_integration
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_rpi_power():
|
||||
"""Mock the rpi_power integration."""
|
||||
with patch(
|
||||
"homeassistant.components.rpi_power.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_setup_entry(hass: HomeAssistant) -> None:
|
||||
"""Test setup of a config entry."""
|
||||
mock_integration(hass, MockModule("hassio"))
|
||||
await async_setup_component(hass, HASSIO_DOMAIN, {})
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={},
|
||||
title="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert not hass.config_entries.async_entries("rpi_power")
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.raspberry_pi.get_os_info",
|
||||
return_value={"board": "rpi"},
|
||||
) as mock_get_os_info,
|
||||
patch("homeassistant.components.rpi_power.config_flow.new_under_voltage"),
|
||||
):
|
||||
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
|
||||
|
||||
assert len(hass.config_entries.async_entries("rpi_power")) == 1
|
||||
|
||||
|
||||
async def test_setup_entry_no_hassio(hass: HomeAssistant) -> None:
|
||||
"""Test setup of a config entry without hassio."""
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={},
|
||||
title="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
with patch("homeassistant.components.raspberry_pi.get_os_info") 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) == 0
|
||||
assert len(hass.config_entries.async_entries()) == 0
|
||||
|
||||
|
||||
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"))
|
||||
await async_setup_component(hass, HASSIO_DOMAIN, {})
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={},
|
||||
title="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.raspberry_pi.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
|
||||
assert len(hass.config_entries.async_entries()) == 0
|
||||
|
||||
|
||||
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"))
|
||||
await async_setup_component(hass, HASSIO_DOMAIN, {})
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={},
|
||||
title="Raspberry Pi",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.raspberry_pi.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 is ConfigEntryState.SETUP_RETRY
|
||||
Reference in New Issue
Block a user