mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
87 lines
3.0 KiB
Python
87 lines
3.0 KiB
Python
"""Tests for the openSenseMap integration setup."""
|
|
|
|
from datetime import timedelta
|
|
from unittest.mock import AsyncMock
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
|
from opensensemap_api.exceptions import OpenSenseMapError
|
|
import pytest
|
|
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_opensensemap_api")
|
|
async def test_setup_entry(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test successful setup of a config entry."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_config_entry.state is ConfigEntryState.LOADED
|
|
|
|
state = hass.states.get("sensor.test_station_pm2_5")
|
|
assert state is not None
|
|
assert state.state == "5.42"
|
|
|
|
|
|
async def test_async_update_failure_marks_unavailable(
|
|
hass: HomeAssistant,
|
|
mock_opensensemap_api: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
freezer: FrozenDateTimeFactory,
|
|
) -> None:
|
|
"""Test that an API error after setup marks the entity as unavailable."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
assert hass.states.get("sensor.test_station_pm2_5").state != "unavailable"
|
|
|
|
mock_opensensemap_api.get_data.side_effect = OpenSenseMapError
|
|
freezer.tick(timedelta(minutes=10))
|
|
async_fire_time_changed(hass)
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get("sensor.test_station_pm2_5").state == "unavailable"
|
|
|
|
mock_opensensemap_api.get_data.side_effect = None
|
|
freezer.tick(timedelta(minutes=10))
|
|
async_fire_time_changed(hass)
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get("sensor.test_station_pm2_5").state != "unavailable"
|
|
|
|
|
|
async def test_setup_entry_cannot_connect(
|
|
hass: HomeAssistant,
|
|
mock_opensensemap_api: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test a config entry stays in SETUP_RETRY if the API is unreachable."""
|
|
mock_opensensemap_api.get_data.side_effect = OpenSenseMapError
|
|
mock_config_entry.add_to_hass(hass)
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_opensensemap_api")
|
|
async def test_unload_entry(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test unloading a config entry."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
assert mock_config_entry.state is ConfigEntryState.LOADED
|
|
|
|
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|