File: test_v2.py

package info (click to toggle)
simplisafe-python 2024.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,268 kB
  • sloc: python: 5,252; sh: 50; makefile: 19
file content (289 lines) | stat: -rw-r--r-- 10,253 bytes parent folder | download
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
"""Define tests for v2 System objects."""
from typing import Any

import aiohttp
import pytest
from aresponses import ResponsesMockServer

from simplipy import API
from simplipy.system import SystemStates
from tests.common import (
    TEST_AUTHORIZATION_CODE,
    TEST_CODE_VERIFIER,
    TEST_SUBSCRIPTION_ID,
    TEST_SYSTEM_ID,
    TEST_SYSTEM_SERIAL_NO,
)


@pytest.mark.asyncio
async def test_clear_notifications(
    aresponses: ResponsesMockServer,
    authenticated_simplisafe_server_v2: ResponsesMockServer,
    v2_settings_response: dict[str, Any],
) -> None:
    """Test clearing all active notifications.

    Args:
        aresponses: An aresponses server.
        authenticated_simplisafe_server_v2: A authenticated API connection.
        v2_settings_response: An API response payload.
    """
    async with authenticated_simplisafe_server_v2:
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/messages",
            "delete",
            response=aiohttp.web_response.json_response(
                v2_settings_response, status=200
            ),
        )

        async with aiohttp.ClientSession() as session:
            simplisafe = await API.async_from_auth(
                TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
            )
            systems = await simplisafe.async_get_systems()
            system = systems[TEST_SYSTEM_ID]
            await system.async_clear_notifications()
            assert system.notifications == []

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_get_pins(
    aresponses: ResponsesMockServer,
    authenticated_simplisafe_server_v2: ResponsesMockServer,
    v2_pins_response: dict[str, Any],
) -> None:
    """Test getting PINs associated with a V2 system.

    Args:
        aresponses: An aresponses server.
        authenticated_simplisafe_server_v2: A authenticated API connection.
        v2_pins_response: An API response payload.
    """
    async with authenticated_simplisafe_server_v2:
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/pins",
            "get",
            response=aiohttp.web_response.json_response(v2_pins_response, status=200),
        )

        async with aiohttp.ClientSession() as session:
            simplisafe = await API.async_from_auth(
                TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
            )
            systems = await simplisafe.async_get_systems()
            system = systems[TEST_SYSTEM_ID]
            pins = await system.async_get_pins()

            assert len(pins) == 4
            assert pins["master"] == "1234"
            assert pins["duress"] == "9876"
            assert pins["Mother"] == "3456"
            assert pins["Father"] == "4567"

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_async_get_systems(
    aresponses: ResponsesMockServer,
    authenticated_simplisafe_server_v2: ResponsesMockServer,
) -> None:
    """Test the ability to get systems attached to a v2 account.

    Args:
        aresponses: An aresponses server.
        authenticated_simplisafe_server_v2: A authenticated API connection.
    """
    async with authenticated_simplisafe_server_v2, aiohttp.ClientSession() as session:
        simplisafe = await API.async_from_auth(
            TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
        )
        systems = await simplisafe.async_get_systems()
        assert len(systems) == 1

        system = systems[TEST_SYSTEM_ID]
        assert system.serial == TEST_SYSTEM_SERIAL_NO
        assert system.system_id == TEST_SYSTEM_ID
        assert len(system.sensors) == 35

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_set_pin(
    aresponses: ResponsesMockServer,
    authenticated_simplisafe_server_v2: ResponsesMockServer,
    v2_pins_response: dict[str, Any],
    v2_settings_response: dict[str, Any],
) -> None:
    """Test setting a PIN in a V2 system.

    Args:
        aresponses: An aresponses server.
        authenticated_simplisafe_server_v2: A authenticated API connection.
        v2_pins_response: An API response payload.
        v2_settings_response: An API response payload.
    """
    async with authenticated_simplisafe_server_v2:
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/pins",
            "get",
            response=aiohttp.web_response.json_response(v2_pins_response, status=200),
        )
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/pins",
            "get",
            response=aiohttp.web_response.json_response(v2_pins_response, status=200),
        )
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/pins",
            "post",
            response=aiohttp.web_response.json_response(
                v2_settings_response, status=200
            ),
        )

        v2_pins_response["pins"]["pin4"]["value"] = "1275"
        v2_pins_response["pins"]["pin4"]["name"] = "whatever"

        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/pins",
            "get",
            response=aiohttp.web_response.json_response(v2_pins_response, status=200),
        )

        async with aiohttp.ClientSession() as session:
            simplisafe = await API.async_from_auth(
                TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
            )
            systems = await simplisafe.async_get_systems()
            system = systems[TEST_SYSTEM_ID]

            latest_pins = await system.async_get_pins()
            assert len(latest_pins) == 4

            await system.async_set_pin("whatever", "1275")
            new_pins = await system.async_get_pins()
            assert len(new_pins) == 5

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_set_states(
    aresponses: ResponsesMockServer,
    authenticated_simplisafe_server_v2: ResponsesMockServer,
    v2_state_response: dict[str, Any],
) -> None:
    """Test the ability to set the state of a v2 system.

    Args:
        aresponses: An aresponses server.
        authenticated_simplisafe_server_v2: A authenticated API connection.
        v2_state_response: An API response payload.
    """
    async with authenticated_simplisafe_server_v2:
        v2_state_response["requestedState"] = "away"

        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/state",
            "post",
            response=aiohttp.web_response.json_response(v2_state_response, status=200),
        )

        v2_state_response["requestedState"] = "home"

        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/state",
            "post",
            response=aiohttp.web_response.json_response(v2_state_response, status=200),
        )

        v2_state_response["requestedState"] = "off"

        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/state",
            "post",
            response=aiohttp.web_response.json_response(v2_state_response, status=200),
        )

        async with aiohttp.ClientSession() as session:
            simplisafe = await API.async_from_auth(
                TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
            )
            systems = await simplisafe.async_get_systems()
            system = systems[TEST_SYSTEM_ID]

            await system.async_set_away()
            state = system.state
            assert state == SystemStates.AWAY
            await system.async_set_home()
            state = system.state
            assert state == SystemStates.HOME
            await system.async_set_off()
            state = system.state
            assert state == SystemStates.OFF

    aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_update_system_data(
    aresponses: ResponsesMockServer,
    authenticated_simplisafe_server_v2: ResponsesMockServer,
    v2_settings_response: dict[str, Any],
    v2_subscriptions_response: dict[str, Any],
) -> None:
    """Test getting updated data for a v2 system.

    Args:
        aresponses: An aresponses server.
        authenticated_simplisafe_server_v2: A authenticated API connection.
        v2_settings_response: An API response payload.
        v2_subscriptions_response: An API response payload.
    """
    async with authenticated_simplisafe_server_v2:
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/users/{TEST_SUBSCRIPTION_ID}/subscriptions",
            "get",
            response=aiohttp.web_response.json_response(
                v2_subscriptions_response, status=200
            ),
        )
        authenticated_simplisafe_server_v2.add(
            "api.simplisafe.com",
            f"/v1/subscriptions/{TEST_SUBSCRIPTION_ID}/settings",
            "get",
            response=aiohttp.web_response.json_response(
                v2_settings_response, status=200
            ),
        )

        async with aiohttp.ClientSession() as session:
            simplisafe = await API.async_from_auth(
                TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
            )
            systems = await simplisafe.async_get_systems()
            system = systems[TEST_SYSTEM_ID]
            assert system.serial == TEST_SYSTEM_SERIAL_NO
            assert system.system_id == TEST_SYSTEM_ID
            assert len(system.sensors) == 35

            # If this succeeds without throwing an exception, the update is successful:
            await system.async_update()

    aresponses.assert_plan_strictly_followed()