Skip to content

Commit 8a520c2

Browse files
committed
Add Type Annotations
1 parent 333a949 commit 8a520c2

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

adafruit_max1704x.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_register.i2c_bit import RWBit, ROBit
3434
from adafruit_register.i2c_bits import RWBits
3535

36+
try:
37+
from typing import List, Tuple
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0-auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX1704x.git"
3844

@@ -100,7 +106,7 @@ class MAX17048:
100106
_reset_voltage = RWBits(7, _MAX1704X_VRESET_REG, 1)
101107
comparator_disabled = RWBit(_MAX1704X_VRESET_REG, 0)
102108

103-
def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
109+
def __init__(self, i2c_bus: I2C, address: int = MAX1704X_I2CADDR_DEFAULT) -> None:
104110
# pylint: disable=no-member
105111
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
106112

@@ -110,7 +116,7 @@ def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
110116
self.enable_sleep = False
111117
self.sleep = False
112118

113-
def reset(self):
119+
def reset(self) -> None:
114120
"""Perform a soft reset of the chip"""
115121
try:
116122
self._cmd = 0x5400
@@ -122,91 +128,91 @@ def reset(self):
122128
self.reset_alert = False # clean up RI alert
123129

124130
@property
125-
def cell_voltage(self):
131+
def cell_voltage(self) -> float:
126132
"""The state of charge of the battery, in volts"""
127133
return self._cell_voltage * 78.125 / 1_000_000
128134

129135
@property
130-
def cell_percent(self):
136+
def cell_percent(self) -> float:
131137
"""The state of charge of the battery, in percentage of 'fullness'"""
132138
return self._cell_SOC / 256.0
133139

134140
@property
135-
def charge_rate(self):
141+
def charge_rate(self) -> float:
136142
"""Charge or discharge rate of the battery in percent/hour"""
137143
return self._cell_crate * 0.208
138144

139145
@property
140-
def reset_voltage(self):
146+
def reset_voltage(self) -> float:
141147
"""The voltage that will determine whether the chip will consider it a reset/swap"""
142148
return self._reset_voltage * 0.04 # 40mV / LSB
143149

144150
@reset_voltage.setter
145-
def reset_voltage(self, reset_v):
151+
def reset_voltage(self, reset_v: float) -> None:
146152
if not 0 <= reset_v <= (127 * 0.04):
147153
raise ValueError("Reset voltage must be between 0 and 5.1 Volts")
148154
self._reset_voltage = int(reset_v / 0.04) # 40mV / LSB
149155

150156
@property
151-
def voltage_alert_min(self):
157+
def voltage_alert_min(self) -> float:
152158
"""The lower-limit voltage for the voltage alert"""
153159
return self._valrt_min * 0.02 # 20mV / LSB
154160

155161
@voltage_alert_min.setter
156-
def voltage_alert_min(self, minvoltage):
162+
def voltage_alert_min(self, minvoltage: float) -> None:
157163
if not 0 <= minvoltage <= (255 * 0.02):
158164
raise ValueError("Alert voltage must be between 0 and 5.1 Volts")
159165
self._valrt_min = int(minvoltage / 0.02) # 20mV / LSB
160166

161167
@property
162-
def voltage_alert_max(self):
168+
def voltage_alert_max(self) -> float:
163169
"""The upper-limit voltage for the voltage alert"""
164170
return self._valrt_max * 0.02 # 20mV / LSB
165171

166172
@voltage_alert_max.setter
167-
def voltage_alert_max(self, maxvoltage):
173+
def voltage_alert_max(self, maxvoltage: float) -> None:
168174
if not 0 <= maxvoltage <= (255 * 0.02):
169175
raise ValueError("Alert voltage must be between 0 and 5.1 Volts")
170176
self._valrt_max = int(maxvoltage / 0.02) # 20mV / LSB
171177

172178
@property
173-
def active_alert(self):
179+
def active_alert(self) -> bool:
174180
"""Whether there is an active alert to be checked"""
175181
return self._alert_status
176182

177183
@property
178-
def alert_reason(self):
184+
def alert_reason(self) -> bool:
179185
"""The 7 bits of alert-status that can be checked at once for flags"""
180186
return self._status & 0x3F
181187

182188
@property
183-
def activity_threshold(self):
189+
def activity_threshold(self) -> float:
184190
"""The absolute change in battery voltage that will trigger hibernation"""
185191
return self._hibrt_actthr * 0.00125 # 1.25mV per LSB
186192

187193
@activity_threshold.setter
188-
def activity_threshold(self, threshold_voltage):
194+
def activity_threshold(self, threshold_voltage: float) -> None:
189195
if not 0 <= threshold_voltage <= (255 * 0.00125):
190196
raise ValueError(
191197
"Activity voltage change must be between 0 and 0.31875 Volts"
192198
)
193199
self._hibrt_actthr = int(threshold_voltage / 0.00125) # 1.25mV per LSB
194200

195201
@property
196-
def hibernation_threshold(self):
202+
def hibernation_threshold(self) -> float:
197203
"""The absolute-value percent-per-hour change in charge rate
198204
that will trigger hibernation"""
199205
return self._hibrt_hibthr * 0.208 # 0.208% per hour
200206

201207
@hibernation_threshold.setter
202-
def hibernation_threshold(self, threshold_percent):
208+
def hibernation_threshold(self, threshold_percent: float) -> None:
203209
if not 0 <= threshold_percent <= (255 * 0.208):
204210
raise ValueError(
205211
"Activity percentage/hour change must be between 0 and 53%"
206212
)
207213
self._hibrt_hibthr = int(threshold_percent / 0.208) # 0.208% per hour
208214

209-
def hibernate(self):
215+
def hibernate(self) -> None:
210216
"""Setup thresholds for hibernation to go into hibernation mode immediately.
211217
212218
See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set
@@ -217,7 +223,7 @@ def hibernate(self):
217223
self._hibrt_hibthr = 0xFF
218224
self._hibrt_actthr = 0xFF
219225

220-
def wake(self):
226+
def wake(self) -> None:
221227
"""Setup thresholds for hibernation to leave hibernation mode immediately.
222228
223229
See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set

0 commit comments

Comments
 (0)