4
4
5
5
import logging
6
6
import re
7
+ from typing import Any
7
8
8
- try :
9
- from pydantic .v1 import BaseModel , ConfigDict , ValidationError
10
- except ImportError :
11
- from pydantic import BaseModel , ConfigDict , ValidationError
9
+ from pydantic import BaseModel , ConfigDict , TypeAdapter , ValidationError
12
10
13
11
_LOGGER = logging .getLogger (__name__ )
14
12
15
13
16
- class TypeWrapper :
17
- """Workaround class for v1 pydantic."""
18
-
19
-
20
14
class Installation (BaseModel ):
15
+ """Pydantic model for a Zaptec installation."""
16
+
21
17
model_config = ConfigDict (extra = "allow" )
22
18
Id : str
19
+ Active : bool
20
+ AuthenticationType : int
21
+ CurrentUserRoles : int
22
+ InstallationType : int
23
+ NetworkType : int
23
24
24
25
25
26
class Installations (BaseModel ):
27
+ """Pydantic model for a list of Zaptec installations."""
28
+
26
29
model_config = ConfigDict (extra = "allow" )
27
30
Data : list [Installation ]
31
+ Pages : int
28
32
29
33
30
34
class Charger (BaseModel ):
35
+ """Pydantic model for a Zaptec charger."""
36
+
31
37
model_config = ConfigDict (extra = "allow" )
32
38
Id : str
33
39
Name : str
40
+ Active : bool
41
+ DeviceType : int
34
42
35
43
36
44
class ChargerState (BaseModel ):
45
+ """Pydantic model for a single state of a Zaptec charger."""
46
+
37
47
model_config = ConfigDict (extra = "allow" )
38
48
StateId : int
39
49
ValueAsString : str
40
50
41
51
42
- # pydantic v2
43
- # ChargerStates = TypeAdapter[list[ChargerState]]
44
- class ChargerStates (TypeWrapper , BaseModel ):
45
- _data : list [ChargerState ]
46
-
47
-
48
- class ChargerUpdate (TypeWrapper , BaseModel ):
49
- _data : dict [str , str ]
52
+ ChargerStates = TypeAdapter [list [ChargerState ]]
53
+ ChargerUpdate = TypeAdapter [dict [str , str ]]
50
54
51
55
52
56
class Chargers (BaseModel ):
57
+ """Pydantic model for a list of Zaptec chargers."""
58
+
53
59
model_config = ConfigDict (extra = "allow" )
54
60
Data : list [Charger ]
61
+ Pages : int
55
62
56
63
57
64
class Circuit (BaseModel ):
65
+ """Pydantic model for a Zaptec circuit."""
66
+
58
67
model_config = ConfigDict (extra = "allow" )
59
68
Id : str
60
69
Name : str
61
70
Chargers : list [Charger ]
62
71
63
72
64
73
class Hierarchy (BaseModel ):
74
+ """Pydantic model for the hierarchy of Zaptec objects in an installation."""
75
+
65
76
model_config = ConfigDict (extra = "allow" )
66
77
Id : str
67
78
Name : str
79
+ NetworkType : int
68
80
Circuits : list [Circuit ]
69
81
70
82
71
83
class ChargerFirmware (BaseModel ):
84
+ """Pydantic model for the firmware information of a Zaptec charger."""
85
+
72
86
model_config = ConfigDict (extra = "allow" )
73
87
ChargerId : str
88
+ DeviceType : int
89
+ IsOnline : bool
74
90
CurrentVersion : str
75
91
AvailableVersion : str
76
92
IsUpToDate : bool
77
93
78
94
79
95
class ChargerLocalSettings (BaseModel ):
96
+ """
97
+ Pydantic model for the local settings of a Zaptec charger.
98
+
99
+ Note: This model is used in an undocumented API-call, and should be removed as soon as we have
100
+ official API-calls that can cover the necessary functionality
101
+ """
102
+
80
103
model_config = ConfigDict (extra = "allow" )
81
104
Id : str
82
- # Name: str
83
- # DeviceId: str
105
+ Name : str | None = None
106
+ DeviceId : str | None = None
84
107
85
108
86
- # pydantic v2
87
- # ChargerFirmwares = TypeAdapter[list[ChargerFirmware]]
88
- class ChargerFirmwares (TypeWrapper , BaseModel ):
89
- _data : list [ChargerFirmware ]
109
+ ChargerFirmwares = TypeAdapter [list [ChargerFirmware ]]
90
110
91
111
92
112
class InstallationConnectionDetails (BaseModel ):
113
+ """Pydantic model for the servicebus connection details of a Zaptec installation."""
114
+
93
115
model_config = ConfigDict (extra = "allow" )
94
116
Host : str
95
117
Password : str
96
- # Port: int
118
+ Port : int
119
+ UseSSL : bool
97
120
Subscription : str
98
- # Type: int
121
+ Type : int
99
122
Username : str
100
123
Topic : str
101
124
@@ -121,29 +144,24 @@ class InstallationConnectionDetails(BaseModel):
121
144
_URLS = [(k , re .compile (k ), v ) for k , v in URLS .items ()]
122
145
123
146
124
- def validate (data , url ):
125
- """Validate the data."""
147
+ def validate (data : Any , url : str ) -> None :
148
+ """
149
+ Validate the data.
150
+
151
+ Raises:
152
+ ValidationError: If data doesn't match the pydantic model associated with the url.
153
+
154
+ """
126
155
127
156
for pat , re_pat , model in _URLS :
128
157
# Mathes either the exact string or its regexp
129
158
if url == pat or re_pat .fullmatch (url ):
130
159
try :
131
- d = data
132
-
133
- # pydantic v1
134
- if isinstance (model , TypeWrapper ):
135
- d = {"_data" : data }
136
-
137
160
if isinstance (model , BaseModel ):
138
- # pydantic v1
139
- model .parse_obj (d )
140
-
141
- # pydantic v2
142
- # model.model_validate(data, strict=True)
161
+ model .model_validate (data , strict = True )
143
162
144
- # pydantic v2
145
- # elif isinstance(model, TypeAdapter):
146
- # model.validate_python(data, strict=True)
163
+ elif isinstance (model , TypeAdapter ):
164
+ model .validate_python (data , strict = True )
147
165
148
166
except ValidationError as err :
149
167
_LOGGER .error ("Failed to validate %s (pattern %s): %s" , url , pat , err )
0 commit comments