Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/components/smhi/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
"""Tests for the SMHI component."""
ENTITY_ID = "weather.smhi_test"
TEST_CONFIG = {"name": "test", "longitude": "17.84197", "latitude": "59.32624"}
10 changes: 10 additions & 0 deletions tests/components/smhi/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Provide common smhi fixtures."""
import pytest

from tests.common import load_fixture


@pytest.fixture(scope="session")
def api_response():
"""Return an API response."""
return load_fixture("smhi.json")
56 changes: 35 additions & 21 deletions tests/components/smhi/test_init.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
"""Test SMHI component setup process."""
from unittest.mock import Mock
from smhi.smhi_lib import APIURL_TEMPLATE

from homeassistant.components import smhi
from homeassistant.components.smhi.const import DOMAIN
from homeassistant.core import HomeAssistant

from .common import AsyncMock
from . import ENTITY_ID, TEST_CONFIG

TEST_CONFIG = {
"config": {
"name": "0123456789ABCDEF",
"longitude": "62.0022",
"latitude": "17.0022",
}
}
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker


async def test_forward_async_setup_entry() -> None:
"""Test that it will forward setup entry."""
hass = Mock()
async def test_setup_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str
) -> None:
"""Test setup entry."""
uri = APIURL_TEMPLATE.format(TEST_CONFIG["longitude"], TEST_CONFIG["latitude"])
aioclient_mock.get(uri, text=api_response)
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
entry.add_to_hass(hass)

assert await smhi.async_setup_entry(hass, {}) is True
assert len(hass.config_entries.async_setup_platforms.mock_calls) == 1
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get(ENTITY_ID)
assert state

async def test_forward_async_unload_entry() -> None:
"""Test that it will forward unload entry."""
hass = AsyncMock()
hass.config_entries.async_unload_platforms = AsyncMock(return_value=True)
assert await smhi.async_unload_entry(hass, {}) is True
assert len(hass.config_entries.async_unload_platforms.mock_calls) == 1

async def test_remove_entry(hass: HomeAssistant) -> None:
"""Test remove entry."""
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
entry.add_to_hass(hass)

await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get(ENTITY_ID)
assert state

await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get(ENTITY_ID)
assert not state
Loading