forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_init.py
90 lines (66 loc) · 2.75 KB
/
test_init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Tests for Glances integration."""
from unittest.mock import AsyncMock, MagicMock
from glances_api.exceptions import (
GlancesApiAuthorizationError,
GlancesApiConnectionError,
GlancesApiNoDataAvailable,
)
import pytest
from homeassistant.components.glances.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from . import HA_SENSOR_DATA, MOCK_USER_INPUT
from tests.common import MockConfigEntry
async def test_successful_config_entry(hass: HomeAssistant) -> None:
"""Test that Glances is configured successfully."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.LOADED
async def test_entry_deprecated_version(
hass: HomeAssistant, issue_registry: ir.IssueRegistry, mock_api: AsyncMock
) -> None:
"""Test creating an issue if glances server is version 2."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
mock_api.return_value.get_ha_sensor_data.side_effect = [
GlancesApiNoDataAvailable("endpoint: 'all' is not valid"),
HA_SENSOR_DATA,
HA_SENSOR_DATA,
]
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.LOADED
issue = issue_registry.async_get_issue(DOMAIN, "deprecated_version")
assert issue is not None
assert issue.severity == ir.IssueSeverity.WARNING
@pytest.mark.parametrize(
("error", "entry_state"),
[
(GlancesApiAuthorizationError, ConfigEntryState.SETUP_ERROR),
(GlancesApiConnectionError, ConfigEntryState.SETUP_RETRY),
(GlancesApiNoDataAvailable, ConfigEntryState.SETUP_ERROR),
],
)
async def test_setup_error(
hass: HomeAssistant,
error: Exception,
entry_state: ConfigEntryState,
mock_api: MagicMock,
) -> None:
"""Test Glances failed due to api error."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
mock_api.return_value.get_ha_sensor_data.side_effect = error
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is entry_state
async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test removing Glances."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.NOT_LOADED
assert DOMAIN not in hass.data