forked from hacf-fr/renault-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
1058 lines (892 loc) · 37.5 KB
/
Copy pathmodels.py
File metadata and controls
1058 lines (892 loc) · 37.5 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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Kamereon models."""
import json
import logging
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any
from typing import cast
from warnings import warn
from marshmallow.schema import Schema
from . import enums
from . import exceptions
from . import helpers
from .enums import AssetPictureSize
from renault_api.models import BaseModel
_LOGGER = logging.getLogger(__name__)
COMMON_ERRRORS: list[dict[str, Any]] = [
{
"errorCode": "err.func.400",
"error_type": exceptions.InvalidInputException,
},
{
"errorCode": "err.func.403",
"error_type": exceptions.AccessDeniedException,
},
{
"errorCode": "err.tech.500",
"error_type": exceptions.InvalidUpstreamException,
},
{
"errorCode": "err.tech.501",
"error_type": exceptions.NotSupportedException,
},
{
"errorCode": "err.func.wired.notFound",
"error_type": exceptions.ResourceNotFoundException,
},
{
"errorCode": "err.tech.wired.kamereon-proxy",
"error_type": exceptions.FailedForwardException,
},
{
"errorCode": "err.func.wired.overloaded",
"error_type": exceptions.QuotaLimitException,
},
{
"errorCode": "err.func.wired.forbidden",
"error_type": exceptions.ForbiddenException,
},
]
VEHICLE_SPECIFICATIONS: dict[str, dict[str, Any]] = {
"X101VE": { # ZOE phase 1
"reports-charge-session-durations-in-minutes": True,
"reports-in-watts": True,
},
"XBG1VE": { # DACIA SPRING
"control-charge-via-kcm": True,
},
}
GATEWAY_SPECIFICATIONS: dict[str, dict[str, Any]] = {
"GDC": { # ZOE phase 1
"reports-charge-session-durations-in-minutes": True,
"reports-in-watts": True,
},
}
@dataclass
class EndpointDefinition:
endpoint: str
mode: str = "default"
_DEFAULT_ENDPOINTS: dict[str, EndpointDefinition] = {
"actions/charge-set-mode": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/charge-mode"
),
"actions/charge-set-schedule": EndpointDefinition(
"/kca/car-adapter/v2/cars/{vin}/actions/charge-schedule"
),
"actions/charge-start": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/charging-start"
),
"actions/charge-stop": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/charging-start"
),
"actions/horn-start": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/horn-lights"
),
"actions/hvac-set-schedule": EndpointDefinition(
"/kca/car-adapter/v2/cars/{vin}/actions/hvac-schedule"
),
"actions/hvac-start": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/hvac-start"
),
"actions/hvac-stop": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/hvac-start"
),
"actions/lights-start": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/actions/horn-lights"
),
"alerts": EndpointDefinition("/vehicles/{vin}/alerts"),
"battery-status": EndpointDefinition(
"/kca/car-adapter/v2/cars/{vin}/battery-status"
),
"charge-history": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/charge-history"
),
"charge-mode": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/charge-mode"),
"charge-schedule": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/charge-schedule"
),
"charges": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/charges"),
"charging-settings": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/charging-settings"
),
"cockpit": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/cockpit"),
"hvac-history": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/hvac-history"),
"hvac-sessions": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/hvac-sessions"),
"hvac-settings": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/hvac-settings"),
"hvac-status": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/hvac-status"),
"location": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/location"),
"lock-status": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/lock-status"),
"notification-settings": EndpointDefinition(
"/kca/car-adapter/v1/cars/{vin}/notification-settings"
),
"pressure": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/pressure"),
"res-state": EndpointDefinition("/kca/car-adapter/v1/cars/{vin}/res-state"),
"soc-levels": EndpointDefinition("/kcm/v1/vehicles/{vin}/ev/soc-levels"),
}
_KCM_ENDPOINTS: dict[str, EndpointDefinition] = {
"actions/charge-start": EndpointDefinition(
"/kcm/v1/vehicles/{vin}/charge/pause-resume", mode="kcm"
),
"actions/charge-stop": EndpointDefinition(
"/kcm/v1/vehicles/{vin}/charge/pause-resume", mode="kcm"
),
"charge-schedule": EndpointDefinition(
"/kcm/v1/vehicles/{vin}/ev/settings", mode="kcm"
),
}
_VEHICLE_ENDPOINTS: dict[str, dict[str, EndpointDefinition | None]] = {
"R5E1VE": { # Renault 5 E-TECH
"actions/horn-start": _DEFAULT_ENDPOINTS["actions/horn-start"],
"actions/hvac-start": _DEFAULT_ENDPOINTS["actions/hvac-start"],
"actions/lights-start": _DEFAULT_ENDPOINTS["actions/lights-start"],
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-history": None, # Reason: "you should not be there..."
"charge-mode": None, # Reason: The access is forbidden
"charge-schedule": _KCM_ENDPOINTS["charge-schedule"],
"charges": _DEFAULT_ENDPOINTS["charges"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # Reason: 404
"pressure": None, # Reason: 404
"res-state": None, # Reason: The access is forbidden
"soc-levels": _DEFAULT_ENDPOINTS["soc-levels"],
},
"X071VE": { # TWINGO III
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.vcps.ev.charge-mode.error"
"charge-schedule": None, # Reason: "err.func.vcps.ev.charge-schedule.error"
"charging-settings": _DEFAULT_ENDPOINTS["charging-settings"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-history": None, # Reason: "err.func.wired.not-found"
"hvac-sessions": None, # Reason: "err.func.wired.not-found"
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # Reason: "err.func.wired.notFound"
"notification-settings": None, # Reason: "err.func.vcps.users-helper.get-notification-settings.error" # noqa: E501
"pressure": None, # Reason: "err.func.wired.notFound"
"res-state": None, # Reason: "err.func.wired.notFound"
},
"X101VE": { # ZOE phase 1
"actions/hvac-start": _DEFAULT_ENDPOINTS["actions/hvac-start"],
"actions/hvac-stop": _DEFAULT_ENDPOINTS["actions/hvac-stop"],
"actions/charge-start": _DEFAULT_ENDPOINTS["actions/charge-start"],
"battery-status": _DEFAULT_ENDPOINTS["battery-status"], # confirmed
"charge-mode": _DEFAULT_ENDPOINTS["charge-mode"], # confirmed
"charge-schedule": _DEFAULT_ENDPOINTS["charge-schedule"], # confirmed
"cockpit": _DEFAULT_ENDPOINTS["cockpit"], # confirmed
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"], # confirmed
"location": None, # not supported
"lock-status": None, # not supported
"pressure": None, # not supported
"res-state": None, # not supported
"soc-levels": None, # not supported
},
"X102VE": { # ZOE phase 2
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-mode": None, # default => 400 Bad Request
"charge-schedule": None, # default and _KCM_ENDPOINTS["charge-schedule"] => 404
"charging-settings": _DEFAULT_ENDPOINTS["charging-settings"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # default => 404
# pressure not supported by all vehicles - but confirmed to be working on some
"pressure": _DEFAULT_ENDPOINTS["pressure"],
"res-state": None, # default => 404
},
"XBG1VE": { # DACIA SPRING
"actions/charge-start": _KCM_ENDPOINTS["actions/charge-start"],
"actions/charge-stop": _KCM_ENDPOINTS["actions/charge-stop"],
"alerts": None, # Reason: "err.func.wired.not-found"
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.wired.forbidden"
"charge-schedule": None, # Reason: "err.func.wired.forbidden"
"charging-settings": None, # Reason: "err.func.wired.forbidden"
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-history": None, # Reason: "err.func.wired.not-found"
"hvac-sessions": None, # Reason: "err.func.wired.not-found"
"hvac-settings": None, # Reason: "err.tech.vcps.ev.hvac-settings.error"
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # Reason: "err.func.wired.notFound"
"notification-settings": None, # Reason: "err.func.vcps.users-helper.get-notification-settings.error" # noqa: E501
"pressure": None, # Reason: "err.func.wired.notFound"
"res-state": None, # Reason: "err.func.wired.notFound"
"soc-levels": None, # Reason: "err.func.wired.forbidden"
},
"XCB1SE": { # SCENIC E-TECH
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-mode": None,
"charge-schedule": _KCM_ENDPOINTS["charge-schedule"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None,
"res-state": None,
},
"XCB1VE": { # MEGANE E-TECH
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.vcps.ev.charge-mode.error"
"charge-schedule": None, # Reason: "err.func.vcps.ev.charge-schedule.error"
"charging-settings": _DEFAULT_ENDPOINTS["charging-settings"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-history": None, # Reason: "err.func.wired.not-found"
"hvac-sessions": None, # Reason: "err.func.wired.not-found"
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # Reason: "err.func.wired.notFound"
"notification-settings": None, # Reason: "err.func.vcps.users-helper.get-notification-settings.error" # noqa: E501
"pressure": None, # Reason: "err.func.wired.notFound"
"res-state": None, # Reason: "err.func.wired.notFound"
},
"XFB2BI": { # Megane IV
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.vcps.ev.charge-mode.error"
"charge-schedule": None, # Reason: "err.func.vcps.ev.charge-schedule.error"
"charging-settings": _DEFAULT_ENDPOINTS["charging-settings"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-history": None, # Reason: "err.func.wired.not-found"
"hvac-sessions": None, # Reason: "err.func.wired.not-found"
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": None, # Reason: "err.func.wired.notFound"
"lock-status": None, # Reason: "err.func.wired.notFound"
"notification-settings": None, # Reason: "err.func.vcps.users-helper.get-notification-settings.error" # noqa: E501
"pressure": None, # Reason: "err.func.wired.notFound"
"res-state": None, # Reason: "err.func.wired.notFound"
},
"XHN1SU": { # AUSTRAL
"actions/horn-start": _DEFAULT_ENDPOINTS["actions/horn-start"],
"actions/lights-start": _DEFAULT_ENDPOINTS["actions/lights-start"],
"battery-status": None, # Reason: "err.func.wired.notFound"
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.wired.forbidden"
"cockpit": _DEFAULT_ENDPOINTS["cockpit"], # confirmed
"hvac-status": None,
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None,
"pressure": None, # Reason: 404
"res-state": None,
},
"XHN1ML": { # Renault Espace VI (OpenRLink)
"actions/hvac-start": None, # err.func.wired.forbidden
"actions/horn-start": _DEFAULT_ENDPOINTS["actions/horn-start"],
"actions/lights-start": _DEFAULT_ENDPOINTS["actions/lights-start"],
"battery-status": None, # err.func.wired.notFound
"charge-history": None, # err.func.wired.not-found
"charge-mode": None, # err.func.wired.forbidden
"charge-schedule": None, # err.func.wired.forbidden
"charges": None, # err.func.wired.forbidden
"charging-settings": None, # err.func.wired.forbidden
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-history": None, # err.func.wired.not-found
"hvac-sessions": None, # err.func.wired.not-found
"hvac-settings": None, # err.func.wired.forbidden
"hvac-status": None, # err.func.wired.notFound
"location": _DEFAULT_ENDPOINTS["location"],
"notification-settings": None, # err.func.vcps.users-helper.get-notification-settings.error # noqa: E501
"lock-status": None, # err.func.wired.notFound
"pressure": None, # err.func.wired.notFound
"res-state": None, # err.func.wired.notFound
"soc-levels": None, # err.func.wired.notFound
},
"XJA1VP": { # CLIO V
"hvac-status": None,
},
"XJB2CP": { # Renault Symbioz 2025
"cockpit": _DEFAULT_ENDPOINTS["cockpit"], # confirmed
"hvac-status": None,
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None,
"res-state": None,
},
"XJB1SU": { # CAPTUR II
"battery-status": _DEFAULT_ENDPOINTS["battery-status"],
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.vcps.ev.charge-mode.error"
"charge-schedule": None, # Reason: "err.func.vcps.ev.charge-schedule.error"
"charging-settings": _DEFAULT_ENDPOINTS["charging-settings"],
"cockpit": _DEFAULT_ENDPOINTS["cockpit"],
"hvac-history": None, # Reason: "err.func.wired.not-found"
"hvac-sessions": None, # Reason: "err.func.wired.not-found"
"hvac-settings": _DEFAULT_ENDPOINTS["hvac-settings"],
"hvac-status": _DEFAULT_ENDPOINTS["hvac-status"],
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # Reason: "err.func.wired.notFound"
"notification-settings": None, # Reason: "err.func.vcps.users-helper.get-notification-settings.error" # noqa: E501
"pressure": None, # Reason: "err.func.wired.notFound"
"res-state": None, # Reason: "err.func.wired.notFound"
},
"XJL2TR": { # Arkana E-tech full hybrid
"cockpit": _DEFAULT_ENDPOINTS["cockpit"], # confirmed
"charge-history": None, # Reason: "err.func.wired.not-found"
"charge-mode": None, # Reason: "err.func.wired.forbidden"
"charge-schedule": None, # Reason: "err.func.wired.forbidden"
"charging-settings": None, # Reason: "err.func.wired.forbidden"
"hvac-status": None, # Reason: "err.func.wired.notFound"
"hvac-history": None, # Reason: "err.func.wired.not-found"
"hvac-settings": None, # Reason: "err.func.wired.not-found"
"hvac-sessions": None, # Reason: "err.func.wired.not-found"
"hvac-start": None, # Reason: "Request method 'GET' is not supported"
"hvac-schedule": None, # Reason: "Request method 'GET' is not supported"
"location": _DEFAULT_ENDPOINTS["location"],
"lock-status": None, # Reason: "err.func.wired.notFound"
"res-state": None, # Reason: "err.func.wired.notFound"
"pressure": None, # Reason: "err.func.wired.notFound"
},
}
_ALREADY_WARNED_VEHICLE: set[str] = set()
_ALREADY_WARNED_VEHICLE_ENDPOINT: set[str] = set()
def get_model_endpoints(
model_code: str | None,
) -> Mapping[str, EndpointDefinition | None]:
"""Return model endpoints."""
if not model_code:
# Model code not available
return _DEFAULT_ENDPOINTS
if model_code not in _VEHICLE_ENDPOINTS:
# Model not documented
if model_code not in _ALREADY_WARNED_VEHICLE:
_ALREADY_WARNED_VEHICLE.add(model_code)
_LOGGER.warning(
"Model %s is not documented, using default endpoints."
" Please help to document it at https://github.com/hacf-fr/renault-api",
model_code,
)
return _DEFAULT_ENDPOINTS
return _VEHICLE_ENDPOINTS[model_code]
def get_model_endpoint(
model_code: str | None, endpoint: str
) -> EndpointDefinition | None:
"""Return model endpoint"""
endpoints = get_model_endpoints(model_code)
if endpoint not in endpoints:
# Endpoint not documented
key = f"{model_code}:{endpoint}"
if key not in _ALREADY_WARNED_VEHICLE_ENDPOINT:
_ALREADY_WARNED_VEHICLE_ENDPOINT.add(key)
_LOGGER.warning(
"Endpoint %s for model %s is not documented, using default endpoints."
" Please help to document it at https://github.com/hacf-fr/renault-api",
endpoint,
model_code,
)
return _DEFAULT_ENDPOINTS.get(endpoint)
return endpoints[endpoint]
@dataclass
class KamereonResponseError(BaseModel):
"""Kamereon response error."""
errorCode: str | None
errorMessage: str | None
def raise_for_error_code(self) -> None:
"""Raise exception from response error."""
error_details = self.get_error_details()
for common_error in COMMON_ERRRORS:
if self.errorCode == common_error["errorCode"]:
error_type = common_error["error_type"]
raise error_type(self.errorCode, error_details)
raise exceptions.KamereonResponseException(self.errorCode, error_details)
def get_error_details(self) -> str | None:
"""Extract the error details sometimes hidden inside nested JSON."""
try:
error_details = json.loads(self.errorMessage or "{}")
except json.JSONDecodeError:
return self.errorMessage
error_descriptions = []
for inner_error in error_details.get("errors", []):
error_description = " ".join(
filter(
None,
[
inner_error.get("title"),
inner_error.get("source", {}).get("pointer"),
inner_error.get("detail"),
],
)
)
error_descriptions.append(error_description)
return ", ".join(error_descriptions) or self.errorMessage
@dataclass
class KamereonResponse(BaseModel):
"""Kamereon response."""
errors: list[KamereonResponseError] | None
def raise_for_error_code(self) -> None:
"""Raise exception if errors found in the response."""
for error in self.errors or []:
# Until we have a sample for multiple errors, just raise on first one
error.raise_for_error_code()
@dataclass
class KamereonPersonAccount(BaseModel):
"""Kamereon person account data."""
accountId: str | None
accountType: str | None
accountStatus: str | None
@dataclass
class KamereonPersonResponse(KamereonResponse):
"""Kamereon response to GET on /persons/{gigya_person_id}."""
accounts: list[KamereonPersonAccount] | None
@dataclass
class KamereonVehicleDetailsGroup(BaseModel):
"""Kamereon vehicle details group data."""
code: str | None
label: str | None
group: str | None
@dataclass
class KamereonVehicleDetails(BaseModel):
"""Kamereon vehicle details."""
vin: str | None
registrationNumber: str | None
radioCode: str | None
brand: KamereonVehicleDetailsGroup | None
model: KamereonVehicleDetailsGroup | None
energy: KamereonVehicleDetailsGroup | None
engineEnergyType: str | None
assets: list[dict[str, Any]] | None
def get_energy_code(self) -> str | None:
"""Return vehicle energy code."""
return self.energy.code if self.energy else None
def get_brand_label(self) -> str | None:
"""Return vehicle model label."""
return self.brand.label if self.brand else None
def get_model_code(self) -> str | None:
"""Return vehicle model code."""
return self.model.code if self.model else None
def get_model_label(self) -> str | None:
"""Return vehicle model label."""
return self.model.label if self.model else None
def get_asset(self, asset_type: str) -> dict[str, Any] | None:
"""Return asset."""
return next(
filter(
lambda asset: asset.get("assetType") == asset_type, self.assets or []
)
)
def get_picture(
self, size: AssetPictureSize = AssetPictureSize.LARGE
) -> str | None:
"""Return vehicle picture."""
asset: dict[str, Any] = self.get_asset("PICTURE") or {}
rendition: dict[str, str] = next(
filter(
lambda rendition: rendition.get("resolutionType")
== f"ONE_MYRENAULT_{size.name}",
asset.get("renditions", [{}]),
)
)
return rendition.get("url") if rendition else None
def uses_electricity(self) -> bool:
"""Return True if model uses electricity."""
energy_type = self.engineEnergyType or self.get_energy_code()
if energy_type in [
"ELEC",
"ELECX",
"PHEV",
]:
return True
return False
def uses_fuel(self) -> bool:
"""Return True if model uses fuel."""
energy_type = self.engineEnergyType or self.get_energy_code()
if energy_type in [
"OTHER",
"PHEV",
"HEV",
]:
return True
return False
def reports_charge_session_durations_in_minutes(self) -> bool:
"""Return True if model reports history durations in minutes."""
# Default to False (=seconds) for unknown vehicles
if self.model and self.model.code:
return VEHICLE_SPECIFICATIONS.get( # type:ignore[no-any-return]
self.model.code, {}
).get("reports-charge-session-durations-in-minutes", False)
return False
def reports_charging_power_in_watts(self) -> bool:
"""Return True if model reports chargingInstantaneousPower in watts."""
# Default to False for unknown vehicles
if self.model and self.model.code:
return VEHICLE_SPECIFICATIONS.get( # type:ignore[no-any-return]
self.model.code, {}
).get("reports-in-watts", False)
return False
def supports_endpoint(self, endpoint: str) -> bool:
"""Return True if model supports specified endpoint."""
# Default to True for unknown vehicles
return self.get_endpoint(endpoint) is not None
def controls_action_via_kcm(self, action: str) -> bool:
"""Return True if model uses endpoint via kcm."""
warn( # Deprecated in v0.3.2
"This method is deprecated, please use get_endpoint.",
DeprecationWarning,
stacklevel=2,
)
# Default to False for unknown vehicles
if self.model and self.model.code:
return VEHICLE_SPECIFICATIONS.get( # type:ignore[no-any-return]
self.model.code, {}
).get(f"control-{action}-via-kcm", False)
return False
def get_endpoints(self) -> Mapping[str, EndpointDefinition | None]:
"""Return model endpoints."""
return get_model_endpoints(self.get_model_code())
def get_endpoint(self, endpoint: str) -> EndpointDefinition | None:
"""Return model endpoint"""
return get_model_endpoint(self.get_model_code(), endpoint)
@dataclass
class KamereonVehiclesLink(BaseModel):
"""Kamereon vehicles link data."""
vin: str | None
vehicleDetails: KamereonVehicleDetails | None
@dataclass
class KamereonVehiclesResponse(KamereonResponse):
"""Kamereon response to GET on /accounts/{account_id}/vehicles."""
accountId: str | None
country: str | None
vehicleLinks: list[KamereonVehiclesLink] | None
@dataclass
class KamereonVehicleDetailsResponse(KamereonResponse, KamereonVehicleDetails):
"""Kamereon response to GET on /accounts/{account_id}/vehicles/{vin}/details."""
@dataclass
class KamereonVehicleDataAttributes(BaseModel):
"""Kamereon vehicle data attributes."""
@dataclass
class KamereonVehicleContract(BaseModel):
"""Kamereon vehicle contract."""
type: str | None
contractId: str | None
code: str | None
group: str | None
durationMonths: int | None
startDate: str | None
endDate: str | None
status: str | None
statusLabel: str | None
description: str | None
@dataclass
class KamereonVehicleContractsResponse(KamereonResponse):
"""Kamereon response to GET on /accounts/{accountId}/vehicles/{vin}/contracts."""
contractList: list[KamereonVehicleContract] | None
@dataclass
class KamereonVehicleData(BaseModel):
"""Kamereon vehicle data."""
type: str | None
id: str | None
attributes: dict[str, Any] | None
@dataclass
class KamereonVehicleDataResponse(KamereonResponse):
"""Kamereon response to GET/POST on .../cars/{vin}/{type}."""
data: KamereonVehicleData | None
def get_attributes(self, schema: Schema) -> KamereonVehicleDataAttributes:
"""Return jwt token."""
attributes = {}
if self.data and self.data.attributes is not None:
attributes = self.data.attributes
return cast(KamereonVehicleDataAttributes, schema.load(attributes))
@dataclass
class KamereonVehicleBatteryStatusData(KamereonVehicleDataAttributes):
"""Kamereon vehicle battery-status data."""
timestamp: str | None
batteryLevel: int | None
batteryTemperature: int | None
batteryAutonomy: int | None
batteryCapacity: int | None
batteryAvailableEnergy: int | None
plugStatus: int | None
chargingStatus: float | None
chargingRemainingTime: int | None
chargingInstantaneousPower: float | None
def get_plug_status(self) -> enums.PlugState | None:
"""Return plug status."""
try:
return (
enums.PlugState(self.plugStatus)
if self.plugStatus is not None
else None
)
except ValueError:
_LOGGER.warning("Unable to convert `%s` to PlugState.", self.plugStatus)
return None
def get_charging_status(self) -> enums.ChargeState | None:
"""Return charging status."""
try:
return (
enums.ChargeState(self.chargingStatus)
if self.chargingStatus is not None
else None
)
except ValueError:
_LOGGER.warning(
"Unable to convert `%s` to ChargeState.", self.chargingStatus
)
return None
@dataclass
class KamereonVehicleBatterySocData(KamereonVehicleDataAttributes):
"""Kamereon vehicle battery state of charge limits data."""
lastEnergyUpdateTimestamp: str | None
socMin: int | None
socTarget: int | None
@dataclass
class KamereonVehicleTyrePressureData(KamereonVehicleDataAttributes):
"""Kamereon vehicle tyre-pressure data."""
flPressure: int | None
frPressure: int | None
rlPressure: int | None
rrPressure: int | None
flStatus: int | None
frStatus: int | None
rlStatus: int | None
rrStatus: int | None
@dataclass
class KamereonVehicleLocationData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data location attributes."""
lastUpdateTime: str | None
gpsLatitude: float | None
gpsLongitude: float | None
@dataclass
class KamereonVehicleHvacStatusData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data hvac-status attributes."""
lastUpdateTime: str | None
externalTemperature: float | None
hvacStatus: str | None
nextHvacStartDate: str | None
socThreshold: float | None
@dataclass
class KamereonVehicleChargeModeData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data charge-mode attributes."""
chargeMode: str | None
@dataclass
class KamereonVehicleCockpitData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data cockpit attributes."""
fuelAutonomy: float | None
fuelQuantity: float | None
totalMileage: float | None
@dataclass
class KamereonVehicleLockStatusData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data lock-status attributes."""
lockStatus: str | None
doorStatusRearLeft: str | None
doorStatusRearRight: str | None
doorStatusDriver: str | None
doorStatusPassenger: str | None
hatchStatus: str | None
lastUpdateTime: str | None
@dataclass
class KamereonVehicleResStateData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data res-set attributes."""
details: str | None
code: str | None
@dataclass
class KamereonVehicleCarAdapterData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data hvac-status attributes."""
vin: str | None
vehicleId: int | None
batteryCode: str | None
brand: str | None
canGeneration: str | None
carGateway: str | None
deliveryCountry: str | None
deliveryDate: str | None
energy: str | None
engineType: str | None
familyCode: str | None
firstRegistrationDate: str | None
gearbox: str | None
modelCode: str | None
modelCodeDetail: str | None
modelName: str | None
radioType: str | None
region: str | None
registrationCountry: str | None
registrationNumber: str | None
tcuCode: str | None
versionCode: str | None
privacyMode: str | None
privacyModeUpdateDate: str | None
svtFlag: bool | None
svtBlockFlag: bool | None
def uses_electricity(self) -> bool:
"""Return True if model uses electricity."""
if self.energy in [
"electric",
]:
return True
return False
def uses_fuel(self) -> bool:
"""Return True if model uses fuel."""
if self.energy in [
"gasoline",
]:
return True
return False
def reports_charging_power_in_watts(self) -> bool:
"""Return True if model reports chargingInstantaneousPower in watts."""
# Default to False for unknown vehicles
if self.carGateway:
return GATEWAY_SPECIFICATIONS.get( # type:ignore[no-any-return]
self.carGateway, {}
).get("reports-in-watts", False)
return False
def controls_action_via_kcm(self, action: str) -> bool:
"""Return True if model uses endpoint via kcm."""
warn( # Deprecated in v0.3.2
"This method is deprecated, please use get_endpoint.",
DeprecationWarning,
stacklevel=2,
)
# Default to False for unknown vehicles
if self.modelCodeDetail:
return VEHICLE_SPECIFICATIONS.get( # type:ignore[no-any-return]
self.modelCodeDetail, {}
).get(f"control-{action}-via-kcm", False)
return False
@dataclass
class ChargeDaySchedule(BaseModel):
"""Kamereon vehicle charge schedule for day."""
startTime: str | None
duration: int | None
def for_json(self) -> dict[str, Any]:
"""Create dict for json."""
return {
"startTime": self.startTime,
"duration": self.duration,
}
def get_end_time(self) -> str | None:
"""Get end time."""
if self.startTime is None:
return None
return helpers.get_end_time(self.startTime, self.duration)
@dataclass
class ChargeSchedule(BaseModel):
"""Kamereon vehicle charge schedule for week."""
id: int | None
activated: bool | None
monday: ChargeDaySchedule | None
tuesday: ChargeDaySchedule | None
wednesday: ChargeDaySchedule | None
thursday: ChargeDaySchedule | None
friday: ChargeDaySchedule | None
saturday: ChargeDaySchedule | None
sunday: ChargeDaySchedule | None
def for_json(self) -> dict[str, Any]:
"""Create dict for json."""
result: dict[str, Any] = {
"id": self.id,
"activated": self.activated,
}
for day in helpers.DAYS_OF_WEEK:
day_spec: ChargeDaySchedule | None = getattr(self, day, None)
if day_spec is None:
result[day] = day_spec
else:
result[day] = day_spec.for_json()
return result
@dataclass
class HvacDaySchedule(BaseModel):
"""Kamereon vehicle hvac schedule for day."""
readyAtTime: str | None
def for_json(self) -> dict[str, str | None]:
"""Create dict for json."""
return {
"readyAtTime": self.readyAtTime,
}
@dataclass
class HvacSchedule(BaseModel):
"""Kamereon vehicle hvac schedule for week."""
id: int | None
activated: bool | None
monday: HvacDaySchedule | None
tuesday: HvacDaySchedule | None
wednesday: HvacDaySchedule | None
thursday: HvacDaySchedule | None
friday: HvacDaySchedule | None
saturday: HvacDaySchedule | None
sunday: HvacDaySchedule | None
def for_json(self) -> dict[str, Any]:
"""Create dict for json."""
result: dict[str, Any] = {
"id": self.id,
"activated": self.activated,
}
for day in helpers.DAYS_OF_WEEK:
day_spec: HvacDaySchedule | None = getattr(self, day, None)
if day_spec is None:
result[day] = day_spec
else:
result[day] = day_spec.for_json()
return result
@dataclass
class KamereonVehicleChargingSettingsData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data charging-settings attributes."""
mode: str | None
schedules: list[ChargeSchedule] | None
def update(self, args: dict[str, Any]) -> None:
"""Update schedule."""
if "id" not in args:
raise ValueError("id not provided for update.")
if self.schedules is None:
self.schedules = []
for schedule in self.schedules:
if schedule.id == args["id"]:
helpers.update_charge_schedule(schedule, args)
return
self.schedules.append(helpers.create_charge_schedule(args))
@dataclass
class KamereonVehicleHvacSettingsData(KamereonVehicleDataAttributes):
"""Kamereon vehicle data hvac-settings (mode+schedules) attributes."""
mode: str | None
schedules: list[HvacSchedule] | None
def update(self, args: dict[str, Any]) -> None:
"""Update schedule."""
if "id" not in args:
raise ValueError("id not provided for update.")
if self.schedules is None:
self.schedules = []
for schedule in self.schedules:
if schedule.id == args["id"]: