File: test_renault_vehicle.py

package info (click to toggle)
python-renault-api 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,680 kB
  • sloc: python: 7,679; makefile: 2
file content (486 lines) | stat: -rw-r--r-- 16,285 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
"""Test cases for the Renault client API keys."""

import os
from datetime import datetime
from datetime import timezone

import aiohttp
import pytest
from aioresponses import aioresponses
from aioresponses.core import RequestCall
from syrupy.assertion import SnapshotAssertion
from yarl import URL

from tests import fixtures
from tests.const import TEST_ACCOUNT_ID
from tests.const import TEST_COUNTRY
from tests.const import TEST_LOCALE_DETAILS
from tests.const import TEST_VIN
from tests.test_credential_store import get_logged_in_credential_store
from tests.test_renault_session import get_logged_in_session

from renault_api.exceptions import EndpointNotAvailableError
from renault_api.kamereon.helpers import DAYS_OF_WEEK
from renault_api.kamereon.models import ChargeSchedule
from renault_api.kamereon.models import HvacSchedule
from renault_api.renault_vehicle import RenaultVehicle


@pytest.fixture
def vehicle(websession: aiohttp.ClientSession) -> RenaultVehicle:
    """Fixture for testing RenaultVehicle."""
    return RenaultVehicle(
        account_id=TEST_ACCOUNT_ID,
        vin=TEST_VIN,
        session=get_logged_in_session(websession),
    )


def test_init(websession: aiohttp.ClientSession) -> None:
    """Test RenaultVehicle initialisation."""
    assert RenaultVehicle(
        account_id=TEST_ACCOUNT_ID,
        vin=TEST_VIN,
        session=get_logged_in_session(websession),
    )

    assert RenaultVehicle(
        account_id=TEST_ACCOUNT_ID,
        vin=TEST_VIN,
        websession=websession,
        country=TEST_COUNTRY,
        locale_details=TEST_LOCALE_DETAILS,
        credential_store=get_logged_in_credential_store(),
    )


@pytest.mark.asyncio
async def test_get_details(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_details."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    assert await vehicle.get_details()

    # Ensure second call still works (ie. use cached value)
    assert await vehicle.get_details()


@pytest.mark.asyncio
async def test_get_car_adapter(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_details."""
    fixtures.inject_get_car_adapter(mocked_responses, "zoe_40.1.json")
    assert await vehicle.get_car_adapter()

    # Ensure second call still works (ie. use cached value)
    assert await vehicle.get_car_adapter()


@pytest.mark.asyncio
async def test_get_contracts(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_contracts."""
    fixtures.inject_get_vehicle_contracts(mocked_responses, "fr_FR.1.json")

    assert await vehicle.get_contracts()

    # Ensure second call still works (ie. use cached value)
    assert await vehicle.get_contracts()


@pytest.mark.asyncio
async def test_get_battery_status(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_battery_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    fixtures.inject_get_battery_status(mocked_responses)
    assert await vehicle.get_battery_status()


@pytest.mark.asyncio
async def test_get_battery_soc(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test get_battery_soc."""
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    fixtures.inject_get_battery_soc(mocked_responses)
    assert await vehicle.get_battery_soc() == snapshot


@pytest.mark.asyncio
async def test_get_tyre_pressure(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_tyre_pressure."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_tyre_pressure(mocked_responses)
    assert await vehicle.get_tyre_pressure()


@pytest.mark.asyncio
async def test_get_location(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_location."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_location(mocked_responses)
    assert await vehicle.get_location()


@pytest.mark.asyncio
async def test_get_hvac_status(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_hvac_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_hvac_status(mocked_responses, "zoe")
    assert await vehicle.get_hvac_status()


@pytest.mark.asyncio
async def test_get_hvac_settings(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_hvac_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_hvac_settings(mocked_responses)
    data = await vehicle.get_hvac_settings()

    assert data.mode == "scheduled"
    assert data.schedules
    schedules: list[HvacSchedule] = data.schedules
    assert schedules
    assert schedules[1].id == 2
    assert schedules[1].activated is True
    assert schedules[1].wednesday
    assert schedules[1].wednesday.readyAtTime == "T15:15Z"
    assert schedules[1].friday
    assert schedules[1].friday.readyAtTime == "T15:15Z"

    for i in (0, 2, 3, 4):
        assert schedules[i].id == i + 1
        assert schedules[i].activated is False
        for day in DAYS_OF_WEEK:
            assert schedules[i].__dict__[day] is None


@pytest.mark.asyncio
async def test_get_charge_mode(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_charge_mode."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_charge_mode(mocked_responses)
    assert await vehicle.get_charge_mode()


@pytest.mark.asyncio
async def test_get_cockpit(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_cockpit."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    fixtures.inject_get_cockpit(mocked_responses, "zoe")
    assert await vehicle.get_cockpit()


@pytest.mark.asyncio
async def test_get_lock_status(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_lock_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "duster.1.json")
    fixtures.inject_get_lock_status(mocked_responses)
    assert await vehicle.get_lock_status()


@pytest.mark.asyncio
async def test_get_lock_status_no_endpoint(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_lock_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_lock_status(mocked_responses)

    with pytest.raises(
        EndpointNotAvailableError,
        match=f"{EndpointNotAvailableError('lock-status', 'X102VE')}",
    ):
        await vehicle.get_lock_status()


@pytest.mark.asyncio
async def test_get_charge_schedule(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test get_charging_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_charge_schedule(mocked_responses, "single")
    assert await vehicle.get_charge_schedule() == snapshot


@pytest.mark.asyncio
async def test_get_charge_schedule_kcm(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test get_charging_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    fixtures.inject_get_ev_settings(mocked_responses, "single")
    assert await vehicle.get_charge_schedule() == snapshot


@pytest.mark.asyncio
async def test_get_notification_settings(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_notification_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_notification_settings(mocked_responses)
    assert await vehicle.get_notification_settings()


@pytest.mark.asyncio
async def test_get_charge_history_month(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_charge_history."""
    fixtures.inject_get_charge_history(mocked_responses, "202010", "202011", "month")
    assert await vehicle.get_charge_history(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
        period="month",
    )


@pytest.mark.asyncio
async def test_get_charge_history_day(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_charge_history."""
    fixtures.inject_get_charge_history(mocked_responses, "20201001", "20201115", "day")
    assert await vehicle.get_charge_history(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
        period="day",
    )


@pytest.mark.asyncio
async def test_get_charges(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_charges."""
    fixtures.inject_get_charges(mocked_responses, "20201001", "20201115")
    assert await vehicle.get_charges(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
    )


@pytest.mark.asyncio
async def test_get_hvac_history(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_hvac_history."""
    fixtures.inject_get_hvac_history(mocked_responses, "202010", "202011", "month")
    assert await vehicle.get_hvac_history(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
        period="month",
    )


@pytest.mark.asyncio
async def test_get_hvac_sessions(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test get_hvac_sessions."""
    fixtures.inject_get_hvac_sessions(mocked_responses, "20201001", "20201115")
    assert await vehicle.get_hvac_sessions(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
    )


@pytest.mark.asyncio
async def test_set_ac_start(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test set_ac_start."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_hvac_start(mocked_responses, "start")
    assert await vehicle.set_ac_start(
        21, datetime(2020, 11, 24, 6, 30, tzinfo=timezone.utc)
    )

    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_ac_stop(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test set_ac_stop."""
    url = fixtures.inject_set_hvac_start(mocked_responses, "cancel")
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    assert await vehicle.set_ac_stop()

    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_mode(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_mode."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_charge_mode(mocked_responses, "schedule_mode")
    assert await vehicle.set_charge_mode("schedule_mode")

    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_schedules(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_schedules."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_charge_schedule(mocked_responses, "schedules")

    schedules: list[ChargeSchedule] = []
    assert await vehicle.set_charge_schedules(schedules)

    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_start(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_start."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_charging_start(mocked_responses, "start")

    assert await vehicle.set_charge_start()
    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_hvac_schedules(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test set_hvac_schedules."""
    schedules: list[HvacSchedule] = []
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_hvac_schedules(mocked_responses)

    assert await vehicle.set_hvac_schedules(schedules)
    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]

    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_start_horn(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test start_horn."""
    url = fixtures.inject_start_horn(mocked_responses)
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    assert await vehicle.start_horn()

    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_start_lights(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test start_lights."""
    url = fixtures.inject_start_lights(mocked_responses)
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    assert await vehicle.start_lights()

    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_http_get(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test http_get."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    endpoint = await vehicle.get_full_endpoint("charge-schedule")
    url = fixtures.inject_get_charge_schedule(mocked_responses, "single")

    assert await vehicle.http_get(endpoint) == snapshot
    request: RequestCall = mocked_responses.requests[("GET", URL(url))][0]

    assert request.kwargs["json"] is None


@pytest.mark.asyncio
async def test_http_post(
    vehicle: RenaultVehicle, mocked_responses: aioresponses, snapshot: SnapshotAssertion
) -> None:
    """Test http_post."""
    endpoint = (
        "/commerce/v1/accounts/{account_id}"
        "/kamereon/kca/car-adapter/v1/cars/{vin}/actions/charging-start"
    )
    json = {
        "data": {
            "attributes": {
                "action": "start",
            },
            "type": "ChargingStart",
        },
    }
    url = fixtures.inject_set_charging_start(mocked_responses, "start")

    assert await vehicle.http_post(endpoint, json) == snapshot
    request: RequestCall = mocked_responses.requests[("POST", URL(url))][0]

    assert request.kwargs["json"] == snapshot


@pytest.mark.parametrize(
    "filename", fixtures.get_json_files(f"{fixtures.KAMEREON_FIXTURE_PATH}/vehicles")
)
@pytest.mark.asyncio
async def test_get_endpoints(
    vehicle: RenaultVehicle,
    mocked_responses: aioresponses,
    filename: str,
    snapshot: SnapshotAssertion,
) -> None:
    """Test get_endpoints."""
    filename = os.path.basename(filename)
    fixtures.inject_get_vehicle_details(mocked_responses, filename)
    details = await vehicle.get_details()
    endpoints = details.get_endpoints()

    assert endpoints == snapshot


@pytest.mark.asyncio
async def test_get_full_endpoint_unknown(
    vehicle: RenaultVehicle, mocked_responses: aioresponses
) -> None:
    """Test http_get."""
    # Unkown endpoint
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    with pytest.raises(EndpointNotAvailableError):
        await vehicle.get_full_endpoint("random")