5
5
from collections import UserDict
6
6
import json
7
7
import logging
8
- from typing import ClassVar , Literal
8
+ from typing import Any , ClassVar , Literal
9
9
10
10
from .misc import to_under
11
11
26
26
# Helper wrapper for reading constants from the API
27
27
#
28
28
class ZConst (UserDict ):
29
- """Zaptec constants wrapper class"""
29
+ """Zaptec constants wrapper class. """
30
30
31
31
observations : dict [str , int ]
32
32
settings : dict [str , int ]
@@ -53,7 +53,9 @@ class ZConst(UserDict):
53
53
}
54
54
"""Mapping of charger models to device serial number prefixes."""
55
55
56
- def get_remap (self , wanted , device_types = None ) -> dict :
56
+ def get_remap (
57
+ self , wanted : list [str ], device_types : set [str ] | None = None
58
+ ) -> dict [str , int ]:
57
59
"""Parse the Zaptec constants and return a remap dict.
58
60
59
61
Parse the given zaptec constants record `CONST` and generate
@@ -83,7 +85,7 @@ def get_remap(self, wanted, device_types=None) -> dict:
83
85
ids .update ({v : k for k , v in ids .items ()})
84
86
return ids
85
87
86
- def update_ids_from_schema (self , device_types ) :
88
+ def update_ids_from_schema (self , device_types : set [ str ] | None ) -> None :
87
89
"""Update the id from a schema.
88
90
89
91
Read observations, settings and command ids from the
@@ -105,27 +107,27 @@ def update_ids_from_schema(self, device_types):
105
107
# DATA EXTRACTION
106
108
#
107
109
@property
108
- def charger_operation_modes_list (self ):
110
+ def charger_operation_modes_list (self ) -> list [ str ] :
109
111
"""Return a list of all charger operation modes."""
110
112
return list (self .get ("ChargerOperationModes" , {}))
111
113
112
114
@property
113
- def device_types_list (self ):
115
+ def device_types_list (self ) -> list [ str ] :
114
116
"""Return a list of all device types."""
115
117
return list (self .get ("DeviceTypes" , {}))
116
118
117
119
@property
118
- def installation_authentication_type_list (self ):
120
+ def installation_authentication_type_list (self ) -> list [ str ] :
119
121
"""Return a list of all installation authentication types."""
120
122
return list (self .get ("InstallationAuthenticationType" , {}))
121
123
122
124
@property
123
- def installation_types_list (self ):
125
+ def installation_types_list (self ) -> list [ str ] :
124
126
"""Return a list of all installation types."""
125
127
return list (self .get ("InstallationTypes" , {}))
126
128
127
129
@property
128
- def network_types_list (self ):
130
+ def network_types_list (self ) -> list [ str ] :
129
131
"""Return a list of all electrical network types."""
130
132
return list (self .get ("NetworkTypes" , {}))
131
133
@@ -143,53 +145,52 @@ def serial_to_model(self) -> dict[str, str]:
143
145
# Want these to be methods so they can be used for type convertions of
144
146
# attributes in the API responses.
145
147
#
146
- def type_authentication_type (self , v ) :
148
+ def type_authentication_type (self , val : int ) -> str :
147
149
"""Convert the authentication type to a string."""
148
150
modes = {str (v ): k for k , v in self .get ("InstallationAuthenticationType" , {}).items ()}
149
- return modes .get (str (v ), str (v ))
151
+ return modes .get (str (val ), str (val ))
150
152
151
- def type_completed_session (self , data ) :
153
+ def type_completed_session (self , val : str ) -> dict [ str , Any ] :
152
154
"""Convert the CompletedSession to a dict."""
153
- data = json .loads (data )
155
+ data = json .loads (val )
154
156
if "SignedSession" in data :
155
157
data ["SignedSession" ] = self .type_ocmf (data ["SignedSession" ])
156
158
return data
157
159
158
- def type_device_type (self , v ) :
160
+ def type_device_type (self , val : int ) -> str :
159
161
"""Convert the device type to a string."""
160
162
modes = {str (v ): k for k , v in self .get ("DeviceTypes" , {}).items ()}
161
- return modes .get (str (v ), str (v ))
163
+ return modes .get (str (val ), str (val ))
162
164
163
- def type_installation_type (self , v ) :
165
+ def type_installation_type (self , val : int ) -> str :
164
166
"""Convert the installation type to a string."""
165
167
modes = {
166
168
str (v .get ("Id" )): v .get ("Name" ) for v in self .get ("InstallationTypes" , {}).values ()
167
169
}
168
- return modes .get (str (v ), str (v ))
170
+ return modes .get (str (val ), str (val ))
169
171
170
- def type_network_type (self , v ) :
172
+ def type_network_type (self , val : int ) -> str :
171
173
"""Convert the network type to a string."""
172
174
modes = {str (v ): k for k , v in self .get ("NetworkTypes" , {}).items ()}
173
- return modes .get (str (v ), str (v ))
175
+ return modes .get (str (val ), str (val ))
174
176
175
- def type_ocmf (self , data ) :
177
+ def type_ocmf (self , data : str ) -> dict [ str , Any ] :
176
178
"""Open Charge Metering Format (OCMF) type."""
177
179
# https://github.com/SAFE-eV/OCMF-Open-Charge-Metering-Format/blob/master/OCMF-en.md
178
180
sects = data .split ("|" )
179
181
if len (sects ) not in (2 , 3 ) or sects [0 ] != "OCMF" :
180
182
raise ValueError (f"Invalid OCMF data: { data } " )
181
- data = json .loads (sects [1 ])
182
- return data
183
+ return json .loads (sects [1 ])
183
184
184
- def type_charger_operation_mode (self , v ) :
185
+ def type_charger_operation_mode (self , val : int ) -> str :
185
186
"""Convert the operation mode to a string."""
186
187
modes = {str (v ): k for k , v in self .get ("ChargerOperationModes" , {}).items ()}
187
- return modes .get (str (v ), str (v ))
188
+ return modes .get (str (val ), str (val ))
188
189
189
- def type_user_roles (self , v ) :
190
+ def type_user_roles (self , val : int ) -> str :
190
191
"""Convert the user roles to a string."""
191
- val = int (v )
192
- if not val :
192
+ if val == 0 :
193
193
return "None"
194
- roles = set (k for k , v in self .get ("UserRoles" , {}).items () if v & val == v )
194
+ # v != 0 is needed to avoid the 0 == 0 case leading to None always being included
195
+ roles = {k for k , v in self .get ("UserRoles" , {}).items () if v != 0 and (v & val ) == v }
195
196
return ", " .join (roles )
0 commit comments