mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Improve Airthings test coverage (#144750)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
parent
56d97f5545
commit
eeca5a8030
@ -45,6 +45,8 @@ class AirthingsConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
)
|
||||
|
||||
errors = {}
|
||||
await self.async_set_unique_id(user_input[CONF_ID])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
try:
|
||||
await airthings.get_token(
|
||||
@ -60,9 +62,6 @@ class AirthingsConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
await self.async_set_unique_id(user_input[CONF_ID])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return self.async_create_entry(title="Airthings", data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
|
@ -150,7 +150,7 @@ async def async_setup_entry(
|
||||
|
||||
coordinator = entry.runtime_data
|
||||
entities = [
|
||||
AirthingsHeaterEnergySensor(
|
||||
AirthingsDeviceSensor(
|
||||
coordinator,
|
||||
airthings_device,
|
||||
SENSORS[sensor_types],
|
||||
@ -162,7 +162,7 @@ async def async_setup_entry(
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class AirthingsHeaterEnergySensor(
|
||||
class AirthingsDeviceSensor(
|
||||
CoordinatorEntity[AirthingsDataUpdateCoordinator], SensorEntity
|
||||
):
|
||||
"""Representation of a Airthings Sensor device."""
|
||||
|
@ -1 +1,12 @@
|
||||
"""Tests for the Airthings integration."""
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
|
||||
"""Fixture for setting up the component."""
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
79
tests/components/airthings/conftest.py
Normal file
79
tests/components/airthings/conftest.py
Normal file
@ -0,0 +1,79 @@
|
||||
"""Airthings test configuration."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from airthings import Airthings, AirthingsDevice
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.airthings.const import CONF_SECRET, DOMAIN
|
||||
from homeassistant.const import CONF_ID
|
||||
|
||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock a config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_ID: "client_id",
|
||||
CONF_SECRET: "secret",
|
||||
},
|
||||
unique_id="client_id",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(params=["view_plus", "wave_plus", "wave_enhance"])
|
||||
def airthings_fixture(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> str:
|
||||
"""Return the fixture name for Airthings device types."""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_airthings_device(airthings_fixture: str) -> AirthingsDevice:
|
||||
"""Mock an Airthings device."""
|
||||
return AirthingsDevice(
|
||||
**load_json_object_fixture(f"device_{airthings_fixture}.json", DOMAIN)
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_airthings_client(
|
||||
mock_airthings_device: AirthingsDevice, mock_airthings_token: AsyncMock
|
||||
) -> Generator[Airthings]:
|
||||
"""Mock an Airthings client."""
|
||||
with patch(
|
||||
"homeassistant.components.airthings.Airthings",
|
||||
autospec=True,
|
||||
) as mock_airthings:
|
||||
client = mock_airthings.return_value
|
||||
client.update_devices.return_value = {
|
||||
mock_airthings_device.device_id: mock_airthings_device
|
||||
}
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_airthings_token() -> Generator[Airthings]:
|
||||
"""Mock an Airthings client."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.airthings.config_flow.airthings.get_token",
|
||||
return_value="test_token",
|
||||
) as mock_get_token,
|
||||
):
|
||||
yield mock_get_token
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.airthings.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
19
tests/components/airthings/fixtures/device_view_plus.json
Normal file
19
tests/components/airthings/fixtures/device_view_plus.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"device_id": "2960000001",
|
||||
"name": "Living Room",
|
||||
"is_active": true,
|
||||
"device_type": "VIEW_PLUS",
|
||||
"product_name": "View Plus",
|
||||
"location_name": "Home",
|
||||
"sensors": {
|
||||
"battery": 1.1,
|
||||
"co2": 2.2,
|
||||
"humidity": 3.3,
|
||||
"pm1": 4.4,
|
||||
"pm25": 5.5,
|
||||
"pressure": 6.6,
|
||||
"radonShortTermAvg": 7.7,
|
||||
"temp": 8.8,
|
||||
"voc": 9.9
|
||||
}
|
||||
}
|
18
tests/components/airthings/fixtures/device_wave_enhance.json
Normal file
18
tests/components/airthings/fixtures/device_wave_enhance.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"device_id": "3210000003",
|
||||
"name": "Bedroom",
|
||||
"is_active": true,
|
||||
"device_type": "WAVE_ENHANCE",
|
||||
"product_name": "Wave Enhance",
|
||||
"location_name": "Home",
|
||||
"sensors": {
|
||||
"battery": 1.1,
|
||||
"co2": 2.2,
|
||||
"humidity": 3.3,
|
||||
"lux": 4.4,
|
||||
"pressure": 5.5,
|
||||
"sla": 6.6,
|
||||
"temp": 7.7,
|
||||
"voc": 8.8
|
||||
}
|
||||
}
|
17
tests/components/airthings/fixtures/device_wave_plus.json
Normal file
17
tests/components/airthings/fixtures/device_wave_plus.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"device_id": "2930000002",
|
||||
"name": "Office",
|
||||
"is_active": true,
|
||||
"device_type": "WAVE_PLUS",
|
||||
"product_name": "Wave Plus",
|
||||
"location_name": "Home",
|
||||
"sensors": {
|
||||
"battery": 1.1,
|
||||
"co2": 2.2,
|
||||
"humidity": 3.3,
|
||||
"pressure": 4.4,
|
||||
"radonShortTermAvg": 5.5,
|
||||
"temp": 6.6,
|
||||
"voc": 7.7
|
||||
}
|
||||
}
|
1352
tests/components/airthings/snapshots/test_sensor.ambr
Normal file
1352
tests/components/airthings/snapshots/test_sensor.ambr
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,12 +1,12 @@
|
||||
"""Test the Airthings config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import airthings
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.airthings.const import CONF_SECRET, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER
|
||||
from homeassistant.const import CONF_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
@ -38,108 +38,87 @@ DHCP_SERVICE_INFO = [
|
||||
]
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
async def test_full_flow(
|
||||
hass: HomeAssistant, mock_airthings_token: AsyncMock, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test we get the full flow working."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
with (
|
||||
patch(
|
||||
"airthings.get_token",
|
||||
return_value="test_token",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.airthings.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Airthings"
|
||||
assert result["data"] == TEST_DATA
|
||||
assert result["result"].unique_id == "client_id"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
||||
"""Test we handle invalid auth."""
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
(airthings.AirthingsAuthError, "invalid_auth"),
|
||||
(airthings.AirthingsConnectionError, "cannot_connect"),
|
||||
(Exception, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_exceptions(
|
||||
hass: HomeAssistant,
|
||||
mock_airthings_token: AsyncMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
exception: Exception,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test we handle exceptions correctly."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"airthings.get_token",
|
||||
side_effect=airthings.AirthingsAuthError,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
mock_airthings_token.side_effect = exception
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"airthings.get_token",
|
||||
side_effect=airthings.AirthingsConnectionError,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_airthings_token.side_effect = None
|
||||
|
||||
async def test_form_unknown_error(hass: HomeAssistant) -> None:
|
||||
"""Test we handle unknown error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"airthings.get_token",
|
||||
side_effect=Exception,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
|
||||
async def test_flow_entry_already_exists(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test user input for config_entry that already exists."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
first_entry = MockConfigEntry(
|
||||
domain="airthings",
|
||||
data=TEST_DATA,
|
||||
unique_id=TEST_DATA[CONF_ID],
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
first_entry.add_to_hass(hass)
|
||||
|
||||
with patch("airthings.get_token", return_value="token"):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=TEST_DATA
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
@ -147,54 +126,45 @@ async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
|
||||
|
||||
@pytest.mark.parametrize("dhcp_service_info", DHCP_SERVICE_INFO)
|
||||
async def test_dhcp_flow(
|
||||
hass: HomeAssistant, dhcp_service_info: DhcpServiceInfo
|
||||
hass: HomeAssistant,
|
||||
dhcp_service_info: DhcpServiceInfo,
|
||||
mock_airthings_token: AsyncMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the DHCP discovery flow."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_DHCP},
|
||||
data=dhcp_service_info,
|
||||
context={"source": config_entries.SOURCE_DHCP},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.airthings.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
patch(
|
||||
"airthings.get_token",
|
||||
return_value="test_token",
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Airthings"
|
||||
assert result["data"] == TEST_DATA
|
||||
assert result["result"].unique_id == TEST_DATA[CONF_ID]
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_dhcp_flow_hub_already_configured(hass: HomeAssistant) -> None:
|
||||
async def test_dhcp_flow_hub_already_configured(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that DHCP discovery fails when already configured."""
|
||||
|
||||
first_entry = MockConfigEntry(
|
||||
domain="airthings",
|
||||
data=TEST_DATA,
|
||||
unique_id=TEST_DATA[CONF_ID],
|
||||
)
|
||||
first_entry.add_to_hass(hass)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_DHCP},
|
||||
data=DHCP_SERVICE_INFO[0],
|
||||
context={"source": config_entries.SOURCE_DHCP},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
|
23
tests/components/airthings/test_sensor.py
Normal file
23
tests/components/airthings/test_sensor.py
Normal file
@ -0,0 +1,23 @@
|
||||
"""Test the Airthings sensors."""
|
||||
|
||||
from airthings import Airthings
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
async def test_all_device_types(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_airthings_client: Airthings,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all device types."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
Loading…
x
Reference in New Issue
Block a user