forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_switch.py
More file actions
220 lines (178 loc) · 7.03 KB
/
Copy pathtest_switch.py
File metadata and controls
220 lines (178 loc) · 7.03 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Test UptimeRobot switch."""
from unittest.mock import patch
import pytest
from pyuptimerobot import UptimeRobotAuthenticationException, UptimeRobotException
from pyuptimerobot.const import API_PATH_MONITOR_DETAIL
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.uptimerobot.const import COORDINATOR_UPDATE_INTERVAL
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util import dt as dt_util
from .common import (
MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA,
MOCK_UPTIMEROBOT_MONITOR,
MOCK_UPTIMEROBOT_MONITOR_2,
MOCK_UPTIMEROBOT_MONITOR_PAUSED,
UPTIMEROBOT_SWITCH_TEST_ENTITY,
mock_uptimerobot_api_response,
setup_uptimerobot_integration,
)
from tests.common import MockConfigEntry, async_fire_time_changed
async def test_presentation(hass: HomeAssistant) -> None:
"""Test the presentation of UptimeRobot switches."""
await setup_uptimerobot_integration(hass)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
assert entity.attributes["target"] == MOCK_UPTIMEROBOT_MONITOR["url"]
async def test_switch_off(hass: HomeAssistant) -> None:
"""Test entity unavailable on update failure."""
mock_entry = MockConfigEntry(**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA)
mock_entry.add_to_hass(hass)
with (
patch(
"pyuptimerobot.UptimeRobot.async_get_monitors",
return_value=mock_uptimerobot_api_response(
data=[MOCK_UPTIMEROBOT_MONITOR_PAUSED]
),
),
patch(
"pyuptimerobot.UptimeRobot.async_pause_monitor",
return_value=mock_uptimerobot_api_response(
api_path=API_PATH_MONITOR_DETAIL, data=MOCK_UPTIMEROBOT_MONITOR_PAUSED
),
),
):
assert await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: UPTIMEROBOT_SWITCH_TEST_ENTITY},
blocking=True,
)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_OFF
async def test_switch_on(hass: HomeAssistant) -> None:
"""Test entity unavailable on update failure."""
mock_entry = MockConfigEntry(**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA)
mock_entry.add_to_hass(hass)
with (
patch(
"pyuptimerobot.UptimeRobot.async_get_monitors",
return_value=mock_uptimerobot_api_response(data=[MOCK_UPTIMEROBOT_MONITOR]),
),
patch(
"pyuptimerobot.UptimeRobot.async_start_monitor",
return_value=mock_uptimerobot_api_response(
api_path=API_PATH_MONITOR_DETAIL,
data=MOCK_UPTIMEROBOT_MONITOR,
),
),
):
assert await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: UPTIMEROBOT_SWITCH_TEST_ENTITY},
blocking=True,
)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
async def test_authentication_error(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test authentication error turning switch on/off."""
await setup_uptimerobot_integration(hass)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
with (
patch(
"pyuptimerobot.UptimeRobot.async_start_monitor",
side_effect=UptimeRobotAuthenticationException,
),
patch(
"homeassistant.config_entries.ConfigEntry.async_start_reauth"
) as config_entry_reauth,
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: UPTIMEROBOT_SWITCH_TEST_ENTITY},
blocking=True,
)
assert config_entry_reauth.assert_called
async def test_action_execution_failure(hass: HomeAssistant) -> None:
"""Test turning switch on/off failure."""
await setup_uptimerobot_integration(hass)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
with (
patch(
"pyuptimerobot.UptimeRobot.async_start_monitor",
side_effect=UptimeRobotException,
),
pytest.raises(HomeAssistantError) as exc_info,
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: UPTIMEROBOT_SWITCH_TEST_ENTITY},
blocking=True,
)
assert exc_info.value.translation_domain == "uptimerobot"
assert exc_info.value.translation_key == "api_switch_exception"
assert exc_info.value.translation_placeholders == {
"error": "Generic UptimeRobot exception"
}
async def test_switch_api_failure(hass: HomeAssistant) -> None:
"""Test general exception turning switch on/off."""
await setup_uptimerobot_integration(hass)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY)) is not None
assert entity.state == STATE_ON
with (
patch(
"pyuptimerobot.UptimeRobot.async_pause_monitor",
side_effect=UptimeRobotException,
),
pytest.raises(HomeAssistantError) as exc_info,
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: UPTIMEROBOT_SWITCH_TEST_ENTITY},
blocking=True,
)
assert exc_info.value.translation_domain == "uptimerobot"
assert exc_info.value.translation_key == "api_switch_exception"
assert exc_info.value.translation_placeholders == {
"error": "Generic UptimeRobot exception"
}
async def test_switch_dynamic(hass: HomeAssistant) -> None:
"""Test switch dynamically added."""
await setup_uptimerobot_integration(hass)
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY))
assert entity.state == STATE_ON
entity_id_2 = "switch.test_monitor_2"
with patch(
"pyuptimerobot.UptimeRobot.async_get_monitors",
return_value=mock_uptimerobot_api_response(
data=[
MOCK_UPTIMEROBOT_MONITOR,
MOCK_UPTIMEROBOT_MONITOR_2,
]
),
):
async_fire_time_changed(hass, dt_util.utcnow() + COORDINATOR_UPDATE_INTERVAL)
await hass.async_block_till_done()
assert (entity := hass.states.get(UPTIMEROBOT_SWITCH_TEST_ENTITY))
assert entity.state == STATE_ON
assert (entity := hass.states.get(entity_id_2))
assert entity.state == STATE_ON