Skip to content

Commit 171509f

Browse files
mj23000adamlogan73
authored andcommitted
Add testing
1 parent 723a9d9 commit 171509f

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

tests/test_endpoints.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from datetime import datetime
55

66
from homeassistant_api import Client
7+
from homeassistant_api.errors import RequestError
8+
from homeassistant_api.models import ConfigEntryDisabler
79
from homeassistant_api.models.events import Event
810
from homeassistant_api.models.states import State
911
from homeassistant_api.websocket import WebsocketClient
@@ -197,6 +199,98 @@ def test_websocket_get_domain(websocket_client: WebsocketClient) -> None:
197199
assert domain.services
198200

199201

202+
def test_get_nonuser_flows_in_progress(websocket_client: WebsocketClient) -> None:
203+
"""Tests the `"type": "config_entries/flow/progress"` websocket command."""
204+
# No flows in progress
205+
flows = websocket_client.get_nonuser_flows_in_progress()
206+
assert not flows
207+
208+
209+
def test_disable_enable_config_entry(websocket_client: WebsocketClient) -> None:
210+
"""Tests the `"type": "config_entries/disable"` websocket command."""
211+
# Get sun entry
212+
entry = websocket_client.get_config_entries()[0]
213+
assert entry.disabled_by is None
214+
215+
# Disable entry
216+
websocket_client.disable_config_entry(entry.entry_id)
217+
218+
# Check that it was disabled
219+
disabled_entry = websocket_client.get_config_entries()[0]
220+
assert disabled_entry.disabled_by is ConfigEntryDisabler.USER
221+
222+
# Re-enable
223+
websocket_client.enable_config_entry(entry.entry_id)
224+
225+
# Check that it was enable
226+
enabled_entry = websocket_client.get_config_entries()[0]
227+
assert enabled_entry.disabled_by is None
228+
229+
230+
def test_ignore_config_flow(websocket_client: WebsocketClient) -> None:
231+
"""Tests the `"type": "config_entries/ignore_flow"` websocket command."""
232+
# Currently not able to test as no flows are in progress. Send invalid parameters and handle that error
233+
try:
234+
websocket_client.ignore_config_flow("", "")
235+
except RequestError as error:
236+
assert (
237+
error.__str__()
238+
== "An error occurred while making the request to 'Config entry not found' with data: 'not_found'"
239+
)
240+
241+
242+
def test_get_config_entries(websocket_client: WebsocketClient) -> None:
243+
"""Tests the `"type": "config_entries/get"` websocket command."""
244+
# Check number of entries
245+
entries = websocket_client.get_config_entries()
246+
assert len(entries) == 4
247+
248+
# Check that entries have expected values
249+
sun = entries[0]
250+
# Usually some kind of float. Is 0.0 in testing environment
251+
assert sun.created_at == 0.0
252+
assert sun.entry_id == "5f8426fa502435857743f302651753c9"
253+
assert sun.domain == "sun"
254+
assert sun.modified_at == 0.0
255+
assert sun.title == "Sun"
256+
assert sun.source == "import"
257+
assert sun.supports_options is False
258+
assert sun.supports_remove_device is False
259+
assert sun.supports_unload is True
260+
assert sun.supports_reconfigure is False
261+
assert sun.supported_subentry_types == {}
262+
assert sun.pref_disable_new_entities is False
263+
assert sun.pref_disable_polling is False
264+
assert sun.disabled_by is None
265+
assert sun.reason is None
266+
assert sun.error_reason_translation_key is None
267+
assert sun.error_reason_translation_placeholders is None
268+
assert sun.num_subentries == 0
269+
270+
271+
def test_get_entry_subentries(websocket_client: WebsocketClient) -> None:
272+
"""Tests the `"type": "config_entries/subentries/list"` websocket command."""
273+
# Currently not able to test as no entries with subentries available
274+
# Get the sun entry
275+
sun = websocket_client.get_config_entries()[0]
276+
assert sun
277+
278+
# Get subentries for sun (empty tuple)
279+
assert not websocket_client.get_entry_subentries(sun.entry_id)
280+
281+
282+
def test_delete_entry_subentry(websocket_client: WebsocketClient) -> None:
283+
"""Tests the `"type": "config_entries/subentries/delete"` websocket command."""
284+
# Currently not able to test as no entries with subentries available. Send invalid parameters and handle that error
285+
try:
286+
websocket_client.delete_entry_subentry("", "")
287+
except RequestError as error:
288+
assert (
289+
error.__str__()
290+
== "An error occurred while making the request to 'Config entry not found' with data: 'not_found'"
291+
)
292+
293+
200294
def test_trigger_service(cached_client: Client) -> None:
201295
"""Tests the `POST /api/services/<domain>/<service>` endpoint."""
202296
notify = cached_client.get_domain("notify")

tests/test_events.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import pytest
44

5+
from homeassistant_api.models import (
6+
ConfigEntryChange,
7+
ConfigEntryDisabler,
8+
ConfigEntryState,
9+
)
510
from homeassistant_api.websocket import WebsocketClient
611

712

@@ -28,3 +33,40 @@ def test_listen_trigger(websocket_client: WebsocketClient) -> None:
2833
assert datetime.fromisoformat(
2934
trigger["trigger"]["now"]
3035
).timestamp() == pytest.approx(future.timestamp(), abs=1)
36+
37+
38+
def test_listen_config_entries(websocket_client: WebsocketClient) -> None:
39+
with websocket_client.listen_config_entries() as flows:
40+
for i, flow in zip(range(5), flows):
41+
# The first "events" are currently available entries
42+
if i == 0:
43+
# Assumes that the first entry (sun.sun?) is enabled
44+
assert flow[0].type is None
45+
assert flow[0].entry.disabled_by is None
46+
assert flow[0].entry.state == ConfigEntryState.LOADED
47+
48+
# Trigger an "updated" event
49+
websocket_client.disable_config_entry(flow[0].entry.entry_id)
50+
51+
if i == 1:
52+
assert flow[0].type == ConfigEntryChange.UPDATED
53+
assert flow[0].entry.disabled_by == ConfigEntryDisabler.USER
54+
assert flow[0].entry.state == ConfigEntryState.UNLOAD_IN_PROGRESS
55+
56+
if i == 2:
57+
assert flow[0].type == ConfigEntryChange.UPDATED
58+
assert flow[0].entry.disabled_by == ConfigEntryDisabler.USER
59+
assert flow[0].entry.state == ConfigEntryState.NOT_LOADED
60+
61+
# Restore original state
62+
websocket_client.enable_config_entry(flow[0].entry.entry_id)
63+
64+
if i == 3:
65+
assert flow[0].type == ConfigEntryChange.UPDATED
66+
assert flow[0].entry.disabled_by is None
67+
assert flow[0].entry.state == ConfigEntryState.SETUP_IN_PROGRESS
68+
69+
if i == 4:
70+
assert flow[0].type == ConfigEntryChange.UPDATED
71+
assert flow[0].entry.disabled_by is None
72+
assert flow[0].entry.state == ConfigEntryState.LOADED

0 commit comments

Comments
 (0)