init home-assistant
This commit is contained in:
1
home-assistant/tests/components/tile/__init__.py
Normal file
1
home-assistant/tests/components/tile/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Define tests for the Tile component."""
|
||||
77
home-assistant/tests/components/tile/conftest.py
Normal file
77
home-assistant/tests/components/tile/conftest.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""Define test fixtures for Tile."""
|
||||
|
||||
from collections.abc import Generator
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
from pytile.tile import Tile
|
||||
|
||||
from homeassistant.components.tile.const import DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
TEST_PASSWORD = "123abc"
|
||||
TEST_USERNAME = "user@host.com"
|
||||
|
||||
|
||||
@pytest.fixture(name="api")
|
||||
def api_fixture(data_tile_details: dict[str, Any]) -> Mock:
|
||||
"""Define a pytile API object."""
|
||||
tile = Tile(None, data_tile_details)
|
||||
tile.async_update = AsyncMock()
|
||||
|
||||
return Mock(
|
||||
async_get_tiles=AsyncMock(
|
||||
return_value={data_tile_details["result"]["tile_uuid"]: tile}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@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, unique_id=config[CONF_USERNAME], 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_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(name="data_tile_details", scope="package")
|
||||
def data_tile_details_fixture():
|
||||
"""Define a Tile details data payload."""
|
||||
return json.loads(load_fixture("tile_details_data.json", "tile"))
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_pytile")
|
||||
def mock_pytile_fixture(api: Mock) -> Generator[None]:
|
||||
"""Define a fixture to patch pytile."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.tile.config_flow.async_login", return_value=api
|
||||
),
|
||||
patch("homeassistant.components.tile.async_login", return_value=api),
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_config_entry")
|
||||
async def setup_config_entry_fixture(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, mock_pytile: None
|
||||
) -> None:
|
||||
"""Define a fixture to set up tile."""
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
@@ -0,0 +1,94 @@
|
||||
{
|
||||
"version": 1,
|
||||
"revision": 1,
|
||||
"timestamp": "2020-08-12T21:46:52.962Z",
|
||||
"timestamp_ms": 1597268812962,
|
||||
"result_code": 0,
|
||||
"result": {
|
||||
"group": null,
|
||||
"parents": [],
|
||||
"user_node_relationships": null,
|
||||
"owner_user_uuid": "fd0c10a5-d0f7-4619-9bce-5b2cb7a6754b",
|
||||
"node_type": null,
|
||||
"name": "Wallet",
|
||||
"description": null,
|
||||
"image_url": "https://local-tile-pub.s3.amazonaws.com/images/wallet.jpeg",
|
||||
"product": "DUTCH1",
|
||||
"archetype": "WALLET",
|
||||
"visible": true,
|
||||
"user_node_data": {},
|
||||
"permissions_mask": null,
|
||||
"tile_uuid": "19264d2dffdbca32",
|
||||
"firmware_version": "01.12.14.0",
|
||||
"category": null,
|
||||
"superseded_tile_uuid": null,
|
||||
"is_dead": false,
|
||||
"hw_version": "02.09",
|
||||
"configuration": {
|
||||
"fw10_advertising_interval": null
|
||||
},
|
||||
"last_tile_state": {
|
||||
"uuid": "19264d2dffdbca32",
|
||||
"connectionStateCode": 0,
|
||||
"ringStateCode": 0,
|
||||
"tile_uuid": "19264d2dffdbca32",
|
||||
"client_uuid": "2cc56adc-b96a-4293-9b94-eda716e0aa17",
|
||||
"timestamp": 1597254926000,
|
||||
"advertised_rssi": -89,
|
||||
"client_rssi": 0,
|
||||
"battery_level": 0,
|
||||
"latitude": 51.528308,
|
||||
"longitude": -0.3817765,
|
||||
"altitude": 0.4076319168123,
|
||||
"raw_h_accuracy": 13.496111,
|
||||
"v_accuracy": 9.395408,
|
||||
"speed": 0.167378,
|
||||
"course": 147.42035,
|
||||
"authentication": null,
|
||||
"owned": false,
|
||||
"has_authentication": null,
|
||||
"lost_timestamp": -1,
|
||||
"connection_client_uuid": null,
|
||||
"connection_event_timestamp": 0,
|
||||
"last_owner_update": 0,
|
||||
"partner_id": null,
|
||||
"partner_client_id": null,
|
||||
"speed_accuracy": null,
|
||||
"course_accuracy": null,
|
||||
"discovery_timestamp": 1597254933661,
|
||||
"connection_state": "DISCONNECTED",
|
||||
"ring_state": "STOPPED",
|
||||
"is_lost": false,
|
||||
"h_accuracy": 13.496111,
|
||||
"voip_state": "OFFLINE"
|
||||
},
|
||||
"firmware": {
|
||||
"expected_firmware_version": "01.19.01.0",
|
||||
"expected_firmware_imagename": "Tile_FW_Image_01.19.01.0.bin",
|
||||
"expected_firmware_urlprefix": "https://s3.amazonaws.com/tile-tofu-fw/prod/",
|
||||
"expected_firmware_publish_date": 1574380800000,
|
||||
"expected_ppm": null,
|
||||
"expected_advertising_interval": null,
|
||||
"security_level": 1,
|
||||
"expiry_timestamp": 1597290412960,
|
||||
"expected_tdt_cmd_config": "xxxxxxxx"
|
||||
},
|
||||
"auth_key": "xxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
"renewal_status": "NONE",
|
||||
"metadata": {
|
||||
"battery_state": "10"
|
||||
},
|
||||
"battery_status": "NONE",
|
||||
"serial_number": null,
|
||||
"auto_retile": false,
|
||||
"all_user_node_relationships": null,
|
||||
"tile_type": "TILE",
|
||||
"registration_timestamp": 1569634958090,
|
||||
"is_lost": false,
|
||||
"auth_timestamp": 1569634958090,
|
||||
"status": "ACTIVATED",
|
||||
"activation_timestamp": 1569634959186,
|
||||
"thumbnail_image": "https://local-tile-pub.s3.amazonaws.com/images/thumb.jpeg",
|
||||
"last_modified_timestamp": 1597268811531
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
# serializer version: 1
|
||||
# name: test_entry_diagnostics
|
||||
dict({
|
||||
'tiles': list([
|
||||
dict({
|
||||
'accuracy': 13.496111,
|
||||
'altitude': '**REDACTED**',
|
||||
'archetype': 'WALLET',
|
||||
'dead': False,
|
||||
'firmware_version': '01.12.14.0',
|
||||
'hardware_version': '02.09',
|
||||
'kind': 'TILE',
|
||||
'last_timestamp': '2020-08-12T17:55:26',
|
||||
'latitude': '**REDACTED**',
|
||||
'longitude': '**REDACTED**',
|
||||
'lost': False,
|
||||
'lost_timestamp': '1969-12-31T23:59:59.999000',
|
||||
'name': 'Wallet',
|
||||
'ring_state': 'STOPPED',
|
||||
'uuid': '**REDACTED**',
|
||||
'visible': True,
|
||||
'voip_state': 'OFFLINE',
|
||||
}),
|
||||
]),
|
||||
})
|
||||
# ---
|
||||
97
home-assistant/tests/components/tile/test_config_flow.py
Normal file
97
home-assistant/tests/components/tile/test_config_flow.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""Define tests for the Tile config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from pytile.errors import InvalidAuthError, TileError
|
||||
|
||||
from homeassistant.components.tile import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .conftest import TEST_PASSWORD, TEST_USERNAME
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mock_login_response", "errors"),
|
||||
[
|
||||
(AsyncMock(side_effect=InvalidAuthError), {"base": "invalid_auth"}),
|
||||
(AsyncMock(side_effect=TileError), {"base": "unknown"}),
|
||||
],
|
||||
)
|
||||
async def test_create_entry(
|
||||
hass: HomeAssistant, api, config, errors, mock_login_response, mock_pytile
|
||||
) -> 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:
|
||||
with patch(
|
||||
"homeassistant.components.tile.config_flow.async_login", mock_login_response
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=config
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == errors
|
||||
|
||||
# Test that we can recover from login 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"] == TEST_USERNAME
|
||||
assert result["data"] == {
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
}
|
||||
|
||||
|
||||
async def test_duplicate_error(hass: HomeAssistant, config, setup_config_entry) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_import_entry(hass: HomeAssistant, config, mock_pytile) -> None:
|
||||
"""Test importing an entry."""
|
||||
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"] == TEST_USERNAME
|
||||
assert result["data"] == {
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
}
|
||||
|
||||
|
||||
async def test_step_reauth(
|
||||
hass: HomeAssistant, config, config_entry: MockConfigEntry, setup_config_entry
|
||||
) -> None:
|
||||
"""Test that the reauth step works."""
|
||||
result = await config_entry.start_reauth_flow(hass)
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
22
home-assistant/tests/components/tile/test_diagnostics.py
Normal file
22
home-assistant/tests/components/tile/test_diagnostics.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""Test Tile diagnostics."""
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
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_config_entry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||
== snapshot
|
||||
)
|
||||
Reference in New Issue
Block a user