forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_init.py
329 lines (302 loc) · 11.4 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""The tests for the Everything but the Kitchen Sink integration."""
import datetime
from http import HTTPStatus
from unittest.mock import ANY
import pytest
from homeassistant.components.kitchen_sink import DOMAIN
from homeassistant.components.recorder import Recorder, get_instance
from homeassistant.components.recorder.statistics import (
async_add_external_statistics,
get_last_statistics,
list_statistic_ids,
)
from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from tests.components.recorder.common import async_wait_recording_done
from tests.typing import ClientSessionGenerator, WebSocketGenerator
@pytest.fixture
def mock_history(hass):
"""Mock history component loaded."""
hass.config.components.add("history")
async def test_demo_statistics(
recorder_mock: Recorder, mock_history, hass: HomeAssistant
) -> None:
"""Test that the kitchen sink component makes some statistics available."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
await hass.async_start()
await async_wait_recording_done(hass)
statistic_ids = await get_instance(hass).async_add_executor_job(
list_statistic_ids, hass
)
assert {
"display_unit_of_measurement": "°C",
"has_mean": True,
"has_sum": False,
"name": "Outdoor temperature",
"source": DOMAIN,
"statistic_id": f"{DOMAIN}:temperature_outdoor",
"statistics_unit_of_measurement": "°C",
"unit_class": "temperature",
} in statistic_ids
assert {
"display_unit_of_measurement": "kWh",
"has_mean": False,
"has_sum": True,
"name": "Energy consumption 1",
"source": DOMAIN,
"statistic_id": f"{DOMAIN}:energy_consumption_kwh",
"statistics_unit_of_measurement": "kWh",
"unit_class": "energy",
} in statistic_ids
async def test_demo_statistics_growth(
recorder_mock: Recorder, mock_history, hass: HomeAssistant
) -> None:
"""Test that the kitchen sink sum statistics adds to the previous state."""
hass.config.units = US_CUSTOMARY_SYSTEM
now = dt_util.now()
last_week = now - datetime.timedelta(days=7)
last_week_midnight = last_week.replace(hour=0, minute=0, second=0, microsecond=0)
statistic_id = f"{DOMAIN}:energy_consumption_kwh"
metadata = {
"source": DOMAIN,
"name": "Energy consumption 1",
"statistic_id": statistic_id,
"unit_of_measurement": "m³",
"has_mean": False,
"has_sum": True,
}
statistics = [
{
"start": last_week_midnight,
"sum": 2**20,
}
]
async_add_external_statistics(hass, metadata, statistics)
await async_wait_recording_done(hass)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
await hass.async_start()
await async_wait_recording_done(hass)
statistics = await get_instance(hass).async_add_executor_job(
get_last_statistics, hass, 1, statistic_id, False, {"sum"}
)
assert statistics[statistic_id][0]["sum"] > 2**20
assert statistics[statistic_id][0]["sum"] <= (2**20 + 24)
@pytest.mark.freeze_time("2023-10-21")
async def test_issues_created(
mock_history,
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test issues are created and can be fixed."""
assert await async_setup_component(hass, REPAIRS_DOMAIN, {REPAIRS_DOMAIN: {}})
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
await hass.async_start()
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert msg["result"] == {
"issues": [
{
"breaks_in_ha_version": "2023.1.1",
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": False,
"issue_id": "transmogrifier_deprecated",
"issue_domain": None,
"learn_more_url": "https://en.wiktionary.org/wiki/transmogrifier",
"severity": "warning",
"translation_key": "transmogrifier_deprecated",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": "2023.1.1",
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": True,
"issue_id": "out_of_blinker_fluid",
"issue_domain": None,
"learn_more_url": "https://www.youtube.com/watch?v=b9rntRxLlbU",
"severity": "critical",
"translation_key": "out_of_blinker_fluid",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": False,
"issue_id": "unfixable_problem",
"issue_domain": None,
"learn_more_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"severity": "warning",
"translation_key": "unfixable_problem",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": True,
"issue_domain": None,
"issue_id": "bad_psu",
"learn_more_url": "https://www.youtube.com/watch?v=b9rntRxLlbU",
"severity": "critical",
"translation_key": "bad_psu",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"is_fixable": True,
"issue_domain": None,
"issue_id": "cold_tea",
"learn_more_url": None,
"severity": "warning",
"translation_key": "cold_tea",
"translation_placeholders": None,
"ignored": False,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": "homeassistant",
"is_fixable": False,
"issue_domain": DOMAIN,
"issue_id": ANY,
"learn_more_url": None,
"severity": "error",
"translation_key": "config_entry_reauth",
"translation_placeholders": {"name": "Kitchen Sink"},
"ignored": False,
},
]
}
url = "/api/repairs/issues/fix"
resp = await client.post(
url, json={"handler": DOMAIN, "issue_id": "out_of_blinker_fluid"}
)
assert resp.status == HTTPStatus.OK
data = await resp.json()
flow_id = data["flow_id"]
assert data == {
"data_schema": [],
"description_placeholders": None,
"errors": None,
"flow_id": ANY,
"handler": DOMAIN,
"last_step": None,
"preview": None,
"step_id": "confirm",
"type": "form",
}
url = f"/api/repairs/issues/fix/{flow_id}"
resp = await client.post(url)
assert resp.status == HTTPStatus.OK
data = await resp.json()
flow_id = data["flow_id"]
assert data == {
"description": None,
"description_placeholders": None,
"flow_id": flow_id,
"handler": DOMAIN,
"minor_version": 1,
"type": "create_entry",
"version": 1,
}
await ws_client.send_json({"id": 4, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert msg["result"] == {
"issues": [
{
"breaks_in_ha_version": "2023.1.1",
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": False,
"issue_id": "transmogrifier_deprecated",
"issue_domain": None,
"learn_more_url": "https://en.wiktionary.org/wiki/transmogrifier",
"severity": "warning",
"translation_key": "transmogrifier_deprecated",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": False,
"issue_id": "unfixable_problem",
"issue_domain": None,
"learn_more_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"severity": "warning",
"translation_key": "unfixable_problem",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"ignored": False,
"is_fixable": True,
"issue_domain": None,
"issue_id": "bad_psu",
"learn_more_url": "https://www.youtube.com/watch?v=b9rntRxLlbU",
"severity": "critical",
"translation_key": "bad_psu",
"translation_placeholders": None,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": DOMAIN,
"is_fixable": True,
"issue_domain": None,
"issue_id": "cold_tea",
"learn_more_url": None,
"severity": "warning",
"translation_key": "cold_tea",
"translation_placeholders": None,
"ignored": False,
},
{
"breaks_in_ha_version": None,
"created": "2023-10-21T00:00:00+00:00",
"dismissed_version": None,
"domain": "homeassistant",
"is_fixable": False,
"issue_domain": DOMAIN,
"issue_id": ANY,
"learn_more_url": None,
"severity": "error",
"translation_key": "config_entry_reauth",
"translation_placeholders": {"name": "Kitchen Sink"},
"ignored": False,
},
]
}