init home-assistant
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Tests for the AirVisual Pro integration."""
|
||||
96
home-assistant/tests/components/airvisual_pro/conftest.py
Normal file
96
home-assistant/tests/components/airvisual_pro/conftest.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""Define test fixtures for AirVisual Pro."""
|
||||
|
||||
from collections.abc import AsyncGenerator, Generator
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.airvisual_pro.const import DOMAIN
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util.json import JsonObjectType
|
||||
|
||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.airvisual_pro.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture(
|
||||
hass: HomeAssistant, config: dict[str, Any]
|
||||
) -> MockConfigEntry:
|
||||
"""Define a config entry fixture."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
entry_id="6a2b3770e53c28dc1eeb2515e906b0ce",
|
||||
unique_id="XXXXXXX",
|
||||
data=config,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
return entry
|
||||
|
||||
|
||||
@pytest.fixture(name="config")
|
||||
def config_fixture() -> dict[str, Any]:
|
||||
"""Define a config entry data fixture."""
|
||||
return {
|
||||
CONF_IP_ADDRESS: "192.168.1.101",
|
||||
CONF_PASSWORD: "password123",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(name="connect")
|
||||
def connect_fixture() -> AsyncMock:
|
||||
"""Define a mocked async_connect method."""
|
||||
return AsyncMock(return_value=True)
|
||||
|
||||
|
||||
@pytest.fixture(name="disconnect")
|
||||
def disconnect_fixture() -> AsyncMock:
|
||||
"""Define a mocked async_connect method."""
|
||||
return AsyncMock()
|
||||
|
||||
|
||||
@pytest.fixture(name="data", scope="package")
|
||||
def data_fixture() -> JsonObjectType:
|
||||
"""Define an update coordinator data example."""
|
||||
return load_json_object_fixture("data.json", "airvisual_pro")
|
||||
|
||||
|
||||
@pytest.fixture(name="pro")
|
||||
def pro_fixture(
|
||||
connect: AsyncMock, data: JsonObjectType, disconnect: AsyncMock
|
||||
) -> Mock:
|
||||
"""Define a mocked NodeSamba object."""
|
||||
return Mock(
|
||||
async_connect=connect,
|
||||
async_disconnect=disconnect,
|
||||
async_get_latest_measurements=AsyncMock(return_value=data),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_airvisual_pro")
|
||||
async def setup_airvisual_pro_fixture(
|
||||
hass: HomeAssistant, config, pro
|
||||
) -> AsyncGenerator[None]:
|
||||
"""Define a fixture to set up AirVisual Pro."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.airvisual_pro.config_flow.NodeSamba",
|
||||
return_value=pro,
|
||||
),
|
||||
patch("homeassistant.components.airvisual_pro.NodeSamba", return_value=pro),
|
||||
patch("homeassistant.components.airvisual_pro.PLATFORMS", []),
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
yield
|
||||
@@ -0,0 +1 @@
|
||||
"""Define data fixtures for AirVisual Pro."""
|
||||
@@ -0,0 +1,65 @@
|
||||
{
|
||||
"date_and_time": {
|
||||
"date": "2022/10/06",
|
||||
"time": "16:00:44",
|
||||
"timestamp": "1665072044"
|
||||
},
|
||||
"measurements": {
|
||||
"co2": "472",
|
||||
"humidity": "57",
|
||||
"pm0_1": "0",
|
||||
"pm1_0": "0",
|
||||
"aqi_cn": "0",
|
||||
"aqi_us": "0",
|
||||
"pm2_5": "0",
|
||||
"temperature_C": "23.0",
|
||||
"temperature_F": "73.4",
|
||||
"voc": "-1"
|
||||
},
|
||||
"serial_number": "XXXXXXX",
|
||||
"settings": {
|
||||
"follow_mode": "station",
|
||||
"followed_station": "0",
|
||||
"is_aqi_usa": true,
|
||||
"is_concentration_showed": true,
|
||||
"is_indoor": true,
|
||||
"is_lcd_on": true,
|
||||
"is_network_time": true,
|
||||
"is_temperature_celsius": false,
|
||||
"language": "en-US",
|
||||
"lcd_brightness": 80,
|
||||
"node_name": "Office",
|
||||
"power_saving": {
|
||||
"2slots": [
|
||||
{ "hour_off": 9, "hour_on": 7 },
|
||||
{ "hour_off": 22, "hour_on": 18 }
|
||||
],
|
||||
"mode": "yes",
|
||||
"running_time": 99,
|
||||
"yes": [
|
||||
{ "hour": 8, "minute": 0 },
|
||||
{ "hour": 21, "minute": 0 }
|
||||
]
|
||||
},
|
||||
"sensor_mode": { "custom_mode_interval": 3, "mode": 1 },
|
||||
"speed_unit": "mph",
|
||||
"timezone": "America/New York",
|
||||
"tvoc_unit": "ppb"
|
||||
},
|
||||
"status": {
|
||||
"app_version": "1.1826",
|
||||
"battery": 100,
|
||||
"datetime": 1665072044,
|
||||
"device_name": "AIRVISUAL-XXXXXXX",
|
||||
"ip_address": "192.168.1.101",
|
||||
"mac_address": "1234567890ab",
|
||||
"model": 20,
|
||||
"sensor_life": { "pm2_5": 1567924345130 },
|
||||
"sensor_pm25_serial": "00000005050224011145",
|
||||
"sync_time": 250000,
|
||||
"system_version": "KBG63F84",
|
||||
"used_memory": 3,
|
||||
"wifi_strength": 4
|
||||
},
|
||||
"last_measurement_timestamp": 1665072044
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
# serializer version: 1
|
||||
# name: test_entry_diagnostics
|
||||
dict({
|
||||
'data': dict({
|
||||
'date_and_time': dict({
|
||||
'date': '2022/10/06',
|
||||
'time': '16:00:44',
|
||||
'timestamp': '1665072044',
|
||||
}),
|
||||
'history': dict({
|
||||
}),
|
||||
'last_measurement_timestamp': 1665072044,
|
||||
'measurements': dict({
|
||||
'aqi_cn': '0',
|
||||
'aqi_us': '0',
|
||||
'co2': '472',
|
||||
'humidity': '57',
|
||||
'pm0_1': '0',
|
||||
'pm1_0': '0',
|
||||
'pm2_5': '0',
|
||||
'temperature_C': '23.0',
|
||||
'temperature_F': '73.4',
|
||||
'voc': '-1',
|
||||
}),
|
||||
'serial_number': '**REDACTED**',
|
||||
'settings': dict({
|
||||
'follow_mode': 'station',
|
||||
'followed_station': '0',
|
||||
'is_aqi_usa': True,
|
||||
'is_concentration_showed': True,
|
||||
'is_indoor': True,
|
||||
'is_lcd_on': True,
|
||||
'is_network_time': True,
|
||||
'is_temperature_celsius': False,
|
||||
'language': 'en-US',
|
||||
'lcd_brightness': 80,
|
||||
'node_name': 'Office',
|
||||
'power_saving': dict({
|
||||
'2slots': list([
|
||||
dict({
|
||||
'hour_off': 9,
|
||||
'hour_on': 7,
|
||||
}),
|
||||
dict({
|
||||
'hour_off': 22,
|
||||
'hour_on': 18,
|
||||
}),
|
||||
]),
|
||||
'mode': 'yes',
|
||||
'running_time': 99,
|
||||
'yes': list([
|
||||
dict({
|
||||
'hour': 8,
|
||||
'minute': 0,
|
||||
}),
|
||||
dict({
|
||||
'hour': 21,
|
||||
'minute': 0,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
'sensor_mode': dict({
|
||||
'custom_mode_interval': 3,
|
||||
'mode': 1,
|
||||
}),
|
||||
'speed_unit': 'mph',
|
||||
'timezone': 'America/New York',
|
||||
'tvoc_unit': 'ppb',
|
||||
}),
|
||||
'status': dict({
|
||||
'app_version': '1.1826',
|
||||
'battery': 100,
|
||||
'datetime': 1665072044,
|
||||
'device_name': 'AIRVISUAL-XXXXXXX',
|
||||
'ip_address': '192.168.1.101',
|
||||
'mac_address': '**REDACTED**',
|
||||
'model': 20,
|
||||
'sensor_life': dict({
|
||||
'pm2_5': 1567924345130,
|
||||
}),
|
||||
'sensor_pm25_serial': '00000005050224011145',
|
||||
'sync_time': 250000,
|
||||
'system_version': 'KBG63F84',
|
||||
'used_memory': 3,
|
||||
'wifi_strength': 4,
|
||||
}),
|
||||
}),
|
||||
'entry': dict({
|
||||
'data': dict({
|
||||
'ip_address': '192.168.1.101',
|
||||
'password': '**REDACTED**',
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'discovery_keys': dict({
|
||||
}),
|
||||
'domain': 'airvisual_pro',
|
||||
'entry_id': '6a2b3770e53c28dc1eeb2515e906b0ce',
|
||||
'minor_version': 1,
|
||||
'options': dict({
|
||||
}),
|
||||
'pref_disable_new_entities': False,
|
||||
'pref_disable_polling': False,
|
||||
'source': 'user',
|
||||
'title': 'Mock Title',
|
||||
'unique_id': 'XXXXXXX',
|
||||
'version': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,131 @@
|
||||
"""Test the AirVisual Pro config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from pyairvisual.node import (
|
||||
InvalidAuthenticationError,
|
||||
NodeConnectionError,
|
||||
NodeProError,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.airvisual_pro.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("connect_mock", "connect_errors"),
|
||||
[
|
||||
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
|
||||
(AsyncMock(side_effect=InvalidAuthenticationError), {"base": "invalid_auth"}),
|
||||
(AsyncMock(side_effect=NodeConnectionError), {"base": "cannot_connect"}),
|
||||
(AsyncMock(side_effect=NodeProError), {"base": "unknown"}),
|
||||
],
|
||||
)
|
||||
async def test_create_entry(
|
||||
hass: HomeAssistant, config, connect_errors, connect_mock, pro, setup_airvisual_pro
|
||||
) -> None:
|
||||
"""Test creating an entry."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# Test errors that can arise when connecting to a Pro:
|
||||
with patch.object(pro, "async_connect", connect_mock):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=config
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == connect_errors
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=config
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "192.168.1.101"
|
||||
assert result["data"] == {
|
||||
CONF_IP_ADDRESS: "192.168.1.101",
|
||||
CONF_PASSWORD: "password123",
|
||||
}
|
||||
|
||||
|
||||
async def test_duplicate_error(
|
||||
hass: HomeAssistant, config, config_entry, setup_airvisual_pro
|
||||
) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=config
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_step_import(hass: HomeAssistant, config, setup_airvisual_pro) -> None:
|
||||
"""Test that the user step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "192.168.1.101"
|
||||
assert result["data"] == {
|
||||
CONF_IP_ADDRESS: "192.168.1.101",
|
||||
CONF_PASSWORD: "password123",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("connect_mock", "connect_errors"),
|
||||
[
|
||||
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
|
||||
(AsyncMock(side_effect=InvalidAuthenticationError), {"base": "invalid_auth"}),
|
||||
(AsyncMock(side_effect=NodeConnectionError), {"base": "cannot_connect"}),
|
||||
(AsyncMock(side_effect=NodeProError), {"base": "unknown"}),
|
||||
],
|
||||
)
|
||||
async def test_reauth(
|
||||
hass: HomeAssistant,
|
||||
config,
|
||||
config_entry: MockConfigEntry,
|
||||
connect_errors,
|
||||
connect_mock,
|
||||
pro,
|
||||
setup_airvisual_pro,
|
||||
) -> None:
|
||||
"""Test re-auth (including errors)."""
|
||||
result = await config_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
# Test errors that can arise when connecting to a Pro:
|
||||
with patch.object(pro, "async_connect", connect_mock):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "new_password"}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == connect_errors
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "new_password"}
|
||||
)
|
||||
|
||||
# Allow reload to finish:
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
@@ -0,0 +1,22 @@
|
||||
"""Test AirVisual Pro diagnostics."""
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
from syrupy.filters import props
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
config_entry,
|
||||
hass_client: ClientSessionGenerator,
|
||||
setup_airvisual_pro,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
assert await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, config_entry
|
||||
) == snapshot(exclude=props("created_at", "modified_at"))
|
||||
Reference in New Issue
Block a user