Skip to content

Commit 5e856fd

Browse files
committed
Switch to python 3.9 collections typing hints
1 parent abdfeda commit 5e856fd

13 files changed

+101
-109
lines changed

goodwe/const.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from typing import Dict
2-
31
GOODWE_TCP_PORT = 502
42
GOODWE_UDP_PORT = 8899
53

6-
BATTERY_MODES: Dict[int, str] = {
4+
BATTERY_MODES: dict[int, str] = {
75
0: "No battery",
86
1: "Standby",
97
2: "Discharge",
@@ -12,7 +10,7 @@
1210
5: "To be discharged",
1311
}
1412

15-
ENERGY_MODES: Dict[int, str] = {
13+
ENERGY_MODES: dict[int, str] = {
1614
0: "Check Mode",
1715
1: "Wait Mode",
1816
2: "Normal (On-Grid)",
@@ -24,37 +22,37 @@
2422
128: "Battery Discharging",
2523
}
2624

27-
GRID_MODES: Dict[int, str] = {
25+
GRID_MODES: dict[int, str] = {
2826
0: "Not connected to grid",
2927
1: "Connected to grid",
3028
2: "Fault",
3129
}
3230

33-
GRID_IN_OUT_MODES: Dict[int, str] = {
31+
GRID_IN_OUT_MODES: dict[int, str] = {
3432
0: "Idle",
3533
1: "Exporting",
3634
2: "Importing",
3735
}
3836

39-
LOAD_MODES: Dict[int, str] = {
37+
LOAD_MODES: dict[int, str] = {
4038
0: "Inverter and the load is disconnected",
4139
1: "The inverter is connected to a load",
4240
}
4341

44-
PV_MODES: Dict[int, str] = {
42+
PV_MODES: dict[int, str] = {
4543
0: "PV panels not connected",
4644
1: "PV panels connected, no power",
4745
2: "PV panels connected, producing power",
4846
}
4947

50-
WORK_MODES: Dict[int, str] = {
48+
WORK_MODES: dict[int, str] = {
5149
0: "Wait Mode",
5250
1: "Normal",
5351
2: "Error",
5452
4: "Check Mode",
5553
}
5654

57-
WORK_MODES_ET: Dict[int, str] = {
55+
WORK_MODES_ET: dict[int, str] = {
5856
0: "Wait Mode",
5957
1: "Normal (On-Grid)",
6058
2: "Normal (Off-Grid)",
@@ -63,14 +61,14 @@
6361
5: "Check Mode",
6462
}
6563

66-
WORK_MODES_ES: Dict[int, str] = {
64+
WORK_MODES_ES: dict[int, str] = {
6765
0: "Inverter Off - Standby",
6866
1: "Inverter On",
6967
2: "Inverter Abnormal, stopping power",
7068
3: "Inverter Severly Abnormal, 20 seconds to restart",
7169
}
7270

73-
SAFETY_COUNTRIES: Dict[int, str] = {
71+
SAFETY_COUNTRIES: dict[int, str] = {
7472
0: "IT CEI 0-21",
7573
1: "CZ-A1",
7674
2: "DE LV with PV",
@@ -198,7 +196,7 @@
198196
149: "Brazil 254Vac",
199197
}
200198

201-
ERROR_CODES: Dict[int, str] = {
199+
ERROR_CODES: dict[int, str] = {
202200
31: 'Internal Communication Failure',
203201
30: 'EEPROM R/W Failure',
204202
29: 'Fac Failure',
@@ -233,7 +231,7 @@
233231
0: 'GFCI Device Check Failure',
234232
}
235233

236-
DIAG_STATUS_CODES: Dict[int, str] = {
234+
DIAG_STATUS_CODES: dict[int, str] = {
237235
0: "Battery voltage low",
238236
1: "Battery SOC low",
239237
2: "Battery SOC in back",
@@ -265,7 +263,7 @@
265263
28: "SOC protect off",
266264
}
267265

268-
BMS_ALARM_CODES: Dict[int, str] = {
266+
BMS_ALARM_CODES: dict[int, str] = {
269267
15: 'Charging over-voltage 3',
270268
14: 'Discharging under-voltage 3',
271269
13: 'Cell temperature high 3',
@@ -284,7 +282,7 @@
284282
0: 'Charging over-voltage 2',
285283
}
286284

287-
BMS_WARNING_CODES: Dict[int, str] = {
285+
BMS_WARNING_CODES: dict[int, str] = {
288286
11: 'System temperature high',
289287
10: 'System temperature low 2',
290288
9: 'System temperature low 1',
@@ -299,7 +297,7 @@
299297
0: 'Charging over-voltage 1',
300298
}
301299

302-
DERATING_MODE_CODES: Dict[int, str] = {
300+
DERATING_MODE_CODES: dict[int, str] = {
303301
31: '',
304302
30: '',
305303
29: '',

goodwe/dt.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
22

33
import logging
4-
from typing import Tuple
54

5+
from .const import *
66
from .exceptions import InverterError, RequestFailedException, RequestRejectedException
7-
from .inverter import Inverter
8-
from .inverter import OperationMode
9-
from .inverter import SensorKind as Kind
7+
from .inverter import Inverter, OperationMode, SensorKind as Kind
108
from .modbus import ILLEGAL_DATA_ADDRESS
119
from .model import is_3_mppt, is_single_phase
1210
from .protocol import ProtocolCommand
@@ -18,7 +16,7 @@
1816
class DT(Inverter):
1917
"""Class representing inverter of DT/MS/D-NS/XS or GE's GEP(PSB/PSC) families"""
2018

21-
__all_sensors: Tuple[Sensor, ...] = (
19+
__all_sensors: tuple[Sensor, ...] = (
2220
Timestamp("timestamp", 30100, "Timestamp"),
2321
Voltage("vpv1", 30103, "PV1 Voltage", Kind.PV),
2422
Current("ipv1", 30104, "PV1 Current", Kind.PV),
@@ -37,9 +35,9 @@ class DT(Inverter):
3735
"PV3 Power", "W", Kind.PV),
3836
# ppv1 + ppv2 + ppv3
3937
Calculated("ppv",
40-
lambda data: (round(read_voltage(data, 30103) * read_current(data, 30104))) + (round(
41-
read_voltage(data, 30105) * read_current(data, 30106))) + (round(
42-
read_voltage(data, 30107) * read_current(data, 30108))),
38+
lambda data: (round(read_voltage(data, 30103) * read_current(data, 30104))) + (
39+
round(read_voltage(data, 30105) * read_current(data, 30106))) + (
40+
round(read_voltage(data, 30107) * read_current(data, 30108))),
4341
"PV Power", "W", Kind.PV),
4442
# Voltage("vpv4", 14, "PV4 Voltage", Kind.PV),
4543
# Current("ipv4", 16, "PV4 Current", Kind.PV),
@@ -115,12 +113,12 @@ class DT(Inverter):
115113

116114
# Inverter's meter data
117115
# Modbus registers from offset 0x75f4 (30196)
118-
__all_sensors_meter: Tuple[Sensor, ...] = (
116+
__all_sensors_meter: tuple[Sensor, ...] = (
119117
PowerS("active_power", 30196, "Active Power", Kind.GRID),
120118
)
121119

122120
# Modbus registers of inverter settings, offsets are modbus register addresses
123-
__all_settings: Tuple[Sensor, ...] = (
121+
__all_settings: tuple[Sensor, ...] = (
124122
Timestamp("time", 40313, "Inverter time"),
125123

126124
Integer("shadow_scan", 40326, "Shadow Scan", "", Kind.PV),
@@ -133,12 +131,12 @@ class DT(Inverter):
133131
)
134132

135133
# Settings for single phase inverters
136-
__settings_single_phase: Tuple[Sensor, ...] = (
134+
__settings_single_phase: tuple[Sensor, ...] = (
137135
Long("grid_export_limit", 40328, "Grid Export Limit", "W", Kind.GRID),
138136
)
139137

140138
# Settings for three phase inverters
141-
__settings_three_phase: Tuple[Sensor, ...] = (
139+
__settings_three_phase: tuple[Sensor, ...] = (
142140
Integer("grid_export_limit", 40336, "Grid Export Limit", "%", Kind.GRID),
143141
)
144142

@@ -193,7 +191,7 @@ async def read_device_info(self):
193191
self._sensors = tuple(filter(self._pv1_pv2_only, self._sensors))
194192
pass
195193

196-
async def read_runtime_data(self) -> Dict[str, Any]:
194+
async def read_runtime_data(self) -> dict[str, Any]:
197195
response = await self._read_from_socket(self._READ_RUNNING_DATA)
198196
data = self._map_response(response, self._sensors)
199197

@@ -253,7 +251,7 @@ async def _write_setting(self, setting: Sensor, value: Any):
253251
else:
254252
await self._read_from_socket(self._write_multi_command(setting.offset, raw_value))
255253

256-
async def read_settings_data(self) -> Dict[str, Any]:
254+
async def read_settings_data(self) -> dict[str, Any]:
257255
data = {}
258256
for setting in self.settings():
259257
value = await self.read_setting(setting.id_)
@@ -267,7 +265,7 @@ async def set_grid_export_limit(self, export_limit: int) -> None:
267265
if export_limit >= 0:
268266
return await self.write_setting('grid_export_limit', export_limit)
269267

270-
async def get_operation_modes(self, include_emulated: bool) -> Tuple[OperationMode, ...]:
268+
async def get_operation_modes(self, include_emulated: bool) -> tuple[OperationMode, ...]:
271269
return ()
272270

273271
async def get_operation_mode(self) -> OperationMode:
@@ -283,11 +281,11 @@ async def get_ongrid_battery_dod(self) -> int:
283281
async def set_ongrid_battery_dod(self, dod: int) -> None:
284282
raise InverterError("Operation not supported, inverter has no batteries.")
285283

286-
def sensors(self) -> Tuple[Sensor, ...]:
284+
def sensors(self) -> tuple[Sensor, ...]:
287285
result = self._sensors
288286
if self._has_meter:
289287
result = result + self._sensors_meter
290288
return result
291289

292-
def settings(self) -> Tuple[Sensor, ...]:
290+
def settings(self) -> tuple[Sensor, ...]:
293291
return tuple(self._settings.values())

goodwe/es.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
22

33
import logging
4-
from typing import Tuple
54

5+
from .const import *
66
from .exceptions import InverterError
7-
from .inverter import Inverter
8-
from .inverter import OperationMode
9-
from .inverter import SensorKind as Kind
7+
from .inverter import Inverter, OperationMode, SensorKind as Kind
108
from .protocol import ProtocolCommand, Aa55ProtocolCommand, Aa55ReadCommand, Aa55WriteCommand, Aa55WriteMultiCommand
119
from .sensor import *
1210

@@ -20,7 +18,7 @@ class ES(Inverter):
2018
_READ_DEVICE_RUNNING_DATA: ProtocolCommand = Aa55ProtocolCommand("010600", "0186")
2119
_READ_DEVICE_SETTINGS_DATA: ProtocolCommand = Aa55ProtocolCommand("010900", "0189")
2220

23-
__sensors: Tuple[Sensor, ...] = (
21+
__sensors: tuple[Sensor, ...] = (
2422
Voltage("vpv1", 0, "PV1 Voltage", Kind.PV), # modbus 0x500
2523
Current("ipv1", 2, "PV1 Current", Kind.PV),
2624
Calculated("ppv1",
@@ -124,7 +122,7 @@ class ES(Inverter):
124122
"House Consumption", "W", Kind.AC),
125123
)
126124

127-
__all_settings: Tuple[Sensor, ...] = (
125+
__all_settings: tuple[Sensor, ...] = (
128126
Integer("backup_supply", 12, "Backup Supply"),
129127
Integer("off-grid_charge", 14, "Off-grid Charge"),
130128
Integer("shadow_scan", 16, "Shadow Scan", "", Kind.PV),
@@ -156,7 +154,7 @@ class ES(Inverter):
156154
)
157155

158156
# Settings added in ARM firmware 14
159-
__settings_arm_fw_14: Tuple[Sensor, ...] = (
157+
__settings_arm_fw_14: tuple[Sensor, ...] = (
160158
EcoModeV2("eco_mode_1", 47547, "Eco Mode Group 1"),
161159
ByteH("eco_mode_1_switch", 47549, "Eco Mode Group 1 Switch"),
162160
EcoModeV2("eco_mode_2", 47553, "Eco Mode Group 2"),
@@ -202,7 +200,7 @@ async def read_device_info(self):
202200
if self._supports_eco_mode_v2():
203201
self._settings.update({s.id_: s for s in self.__settings_arm_fw_14})
204202

205-
async def read_runtime_data(self) -> Dict[str, Any]:
203+
async def read_runtime_data(self) -> dict[str, Any]:
206204
response = await self._read_from_socket(self._READ_DEVICE_RUNNING_DATA)
207205
data = self._map_response(response, self.__sensors)
208206
return data
@@ -271,7 +269,7 @@ async def _write_setting(self, setting: Sensor, value: Any):
271269
else:
272270
await self._read_from_socket(Aa55WriteMultiCommand(setting.offset, raw_value))
273271

274-
async def read_settings_data(self) -> Dict[str, Any]:
272+
async def read_settings_data(self) -> dict[str, Any]:
275273
response = await self._read_from_socket(self._READ_DEVICE_SETTINGS_DATA)
276274
data = self._map_response(response, self.settings())
277275
return data
@@ -285,7 +283,7 @@ async def set_grid_export_limit(self, export_limit: int) -> None:
285283
Aa55ProtocolCommand("033502" + "{:04x}".format(export_limit), "03b5")
286284
)
287285

288-
async def get_operation_modes(self, include_emulated: bool) -> Tuple[OperationMode, ...]:
286+
async def get_operation_modes(self, include_emulated: bool) -> tuple[OperationMode, ...]:
289287
result = [e for e in OperationMode]
290288
result.remove(OperationMode.PEAK_SHAVING)
291289
result.remove(OperationMode.SELF_USE)
@@ -349,10 +347,10 @@ async def set_ongrid_battery_dod(self, dod: int) -> None:
349347
async def _reset_inverter(self) -> None:
350348
await self._read_from_socket(Aa55ProtocolCommand("031d00", "039d"))
351349

352-
def sensors(self) -> Tuple[Sensor, ...]:
350+
def sensors(self) -> tuple[Sensor, ...]:
353351
return self.__sensors
354352

355-
def settings(self) -> Tuple[Sensor, ...]:
353+
def settings(self) -> tuple[Sensor, ...]:
356354
return tuple(self._settings.values())
357355

358356
async def _set_general_mode(self) -> None:

0 commit comments

Comments
 (0)