init home-assistant
This commit is contained in:
10
home-assistant/tests/components/thethingsnetwork/__init__.py
Normal file
10
home-assistant/tests/components/thethingsnetwork/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
"""Define tests for the The Things Network."""
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
async def init_integration(hass: HomeAssistant, config_entry) -> None:
|
||||
"""Mock TTNClient."""
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
95
home-assistant/tests/components/thethingsnetwork/conftest.py
Normal file
95
home-assistant/tests/components/thethingsnetwork/conftest.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""Define fixtures for the The Things Network tests."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from ttn_client import TTNSensorValue
|
||||
|
||||
from homeassistant.components.thethingsnetwork.const import (
|
||||
CONF_APP_ID,
|
||||
DOMAIN,
|
||||
TTN_API_HOST,
|
||||
)
|
||||
from homeassistant.const import CONF_API_KEY, CONF_HOST
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
HOST = "example.com"
|
||||
APP_ID = "my_app"
|
||||
API_KEY = "my_api_key"
|
||||
|
||||
DEVICE_ID = "my_device"
|
||||
DEVICE_ID_2 = "my_device_2"
|
||||
DEVICE_FIELD = "a_field"
|
||||
DEVICE_FIELD_2 = "a_field_2"
|
||||
DEVICE_FIELD_VALUE = 42
|
||||
|
||||
DATA = {
|
||||
DEVICE_ID: {
|
||||
DEVICE_FIELD: TTNSensorValue(
|
||||
{
|
||||
"end_device_ids": {"device_id": DEVICE_ID},
|
||||
"received_at": "2024-03-11T08:49:11.153738893Z",
|
||||
},
|
||||
DEVICE_FIELD,
|
||||
DEVICE_FIELD_VALUE,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
DATA_UPDATE = {
|
||||
DEVICE_ID: {
|
||||
DEVICE_FIELD: TTNSensorValue(
|
||||
{
|
||||
"end_device_ids": {"device_id": DEVICE_ID},
|
||||
"received_at": "2024-03-12T08:49:11.153738893Z",
|
||||
},
|
||||
DEVICE_FIELD,
|
||||
DEVICE_FIELD_VALUE,
|
||||
)
|
||||
},
|
||||
DEVICE_ID_2: {
|
||||
DEVICE_FIELD_2: TTNSensorValue(
|
||||
{
|
||||
"end_device_ids": {"device_id": DEVICE_ID_2},
|
||||
"received_at": "2024-03-12T08:49:11.153738893Z",
|
||||
},
|
||||
DEVICE_FIELD_2,
|
||||
DEVICE_FIELD_VALUE,
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock a config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=APP_ID,
|
||||
title=APP_ID,
|
||||
data={
|
||||
CONF_APP_ID: APP_ID,
|
||||
CONF_HOST: TTN_API_HOST,
|
||||
CONF_API_KEY: API_KEY,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ttnclient():
|
||||
"""Mock TTNClient."""
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.thethingsnetwork.coordinator.TTNClient",
|
||||
autospec=True,
|
||||
) as ttn_client,
|
||||
patch(
|
||||
"homeassistant.components.thethingsnetwork.config_flow.TTNClient",
|
||||
new=ttn_client,
|
||||
),
|
||||
):
|
||||
instance = ttn_client.return_value
|
||||
instance.fetch_data = AsyncMock(return_value=DATA)
|
||||
yield ttn_client
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Define tests for the The Things Network onfig flows."""
|
||||
|
||||
import pytest
|
||||
from ttn_client import TTNAuthError
|
||||
|
||||
from homeassistant.components.thethingsnetwork.const import CONF_APP_ID, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY, CONF_HOST
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from . import init_integration
|
||||
from .conftest import API_KEY, APP_ID, HOST
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
USER_DATA = {CONF_HOST: HOST, CONF_APP_ID: APP_ID, CONF_API_KEY: API_KEY}
|
||||
|
||||
|
||||
async def test_user(hass: HomeAssistant, mock_ttnclient) -> None:
|
||||
"""Test user config."""
|
||||
|
||||
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_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=USER_DATA,
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == APP_ID
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
assert result["data"][CONF_APP_ID] == APP_ID
|
||||
assert result["data"][CONF_API_KEY] == API_KEY
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("fetch_data_exception", "base_error"),
|
||||
[(TTNAuthError, "invalid_auth"), (Exception, "unknown")],
|
||||
)
|
||||
async def test_user_errors(
|
||||
hass: HomeAssistant, fetch_data_exception, base_error, mock_ttnclient
|
||||
) -> None:
|
||||
"""Test user config errors."""
|
||||
|
||||
# Test error
|
||||
mock_ttnclient.return_value.fetch_data.side_effect = fetch_data_exception
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=USER_DATA,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert base_error in result["errors"]["base"]
|
||||
|
||||
# Recover
|
||||
mock_ttnclient.return_value.fetch_data.side_effect = None
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=USER_DATA,
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_duplicate_entry(
|
||||
hass: HomeAssistant, mock_ttnclient, mock_config_entry
|
||||
) -> None:
|
||||
"""Test that duplicate entries are caught."""
|
||||
|
||||
await init_integration(hass, mock_config_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"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=USER_DATA,
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_step_reauth(
|
||||
hass: HomeAssistant, mock_ttnclient, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that the reauth step works."""
|
||||
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert not result["errors"]
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert not result["errors"]
|
||||
|
||||
new_api_key = "1234"
|
||||
new_user_input = dict(USER_DATA)
|
||||
new_user_input[CONF_API_KEY] = new_api_key
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=new_user_input
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
assert hass.config_entries.async_entries()[0].data[CONF_API_KEY] == new_api_key
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Define tests for the The Things Network init."""
|
||||
|
||||
import pytest
|
||||
from ttn_client import TTNAuthError
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("exception_class"), [TTNAuthError, Exception])
|
||||
async def test_init_exceptions(
|
||||
hass: HomeAssistant, mock_ttnclient, exception_class, mock_config_entry
|
||||
) -> None:
|
||||
"""Test TTN Exceptions."""
|
||||
|
||||
mock_ttnclient.return_value.fetch_data.side_effect = exception_class
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
assert not await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Define tests for the The Things Network sensor."""
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from . import init_integration
|
||||
from .conftest import (
|
||||
APP_ID,
|
||||
DATA_UPDATE,
|
||||
DEVICE_FIELD,
|
||||
DEVICE_FIELD_2,
|
||||
DEVICE_ID,
|
||||
DEVICE_ID_2,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
|
||||
async def test_sensor(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_ttnclient,
|
||||
mock_config_entry,
|
||||
) -> None:
|
||||
"""Test a working configurations."""
|
||||
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
# Check devices
|
||||
assert (
|
||||
device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, f"{APP_ID}_{DEVICE_ID}")}
|
||||
).name
|
||||
== DEVICE_ID
|
||||
)
|
||||
|
||||
# Check entities
|
||||
assert entity_registry.async_get(f"sensor.{DEVICE_ID}_{DEVICE_FIELD}")
|
||||
|
||||
assert not entity_registry.async_get(f"sensor.{DEVICE_ID_2}_{DEVICE_FIELD}")
|
||||
push_callback = mock_ttnclient.call_args.kwargs["push_callback"]
|
||||
await push_callback(DATA_UPDATE)
|
||||
assert entity_registry.async_get(f"sensor.{DEVICE_ID_2}_{DEVICE_FIELD_2}")
|
||||
Reference in New Issue
Block a user