Skip to content

Commit d13a609

Browse files
committed
Add Missing Type Annotations
1 parent b6fcd49 commit d13a609

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

adafruit_ltr390.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
from adafruit_register.i2c_bits import RWBits
3737
from adafruit_register.i2c_bit import RWBit, ROBit
3838

39+
try:
40+
from typing import Optional, Tuple, Type, Union
41+
from typing_extensions import Literal
42+
from busio import I2C
43+
except ImportError:
44+
pass
45+
3946
__version__ = "0.0.0+auto.0"
4047
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LTR390.git"
4148

@@ -65,7 +72,11 @@ def __init__(self, register_address, struct_format, bitwidth, length):
6572
self._width = bitwidth
6673
self._num_bytes = length
6774

68-
def __get__(self, obj, objtype=None):
75+
def __get__(
76+
self,
77+
obj: Optional["UnalignedStruct"],
78+
objtype: Optional[Type["UnalignedStruct"]] = None,
79+
) -> int:
6980
# read bytes into buffer at correct alignment
7081
raw_value = unpack_from(self.format, self.buffer, offset=1)[0]
7182

@@ -81,7 +92,7 @@ def __get__(self, obj, objtype=None):
8192
raw_value = unpack_from(self.format, self.buffer, offset=1)[0]
8293
return raw_value >> 8
8394

84-
def __set__(self, obj, value):
95+
def __set__(self, obj: Optional["UnalignedStruct"], value: int) -> None:
8596
pack_into(self.format, self.buffer, 1, value)
8697
with obj.i2c_device as i2c:
8798
i2c.write(self.buffer)
@@ -91,7 +102,10 @@ class CV:
91102
"""struct helper"""
92103

93104
@classmethod
94-
def add_values(cls, value_tuples):
105+
def add_values(
106+
cls,
107+
value_tuples: Tuple[str, int, str, Union[int, None], int, Union[float, None]],
108+
) -> None:
95109
"""Add CV values to the class"""
96110
cls.string = {}
97111
cls.lsb = {}
@@ -107,7 +121,7 @@ def add_values(cls, value_tuples):
107121
cls.integration[value] = integration
108122

109123
@classmethod
110-
def is_valid(cls, value):
124+
def is_valid(cls, value: int) -> bool:
111125
"""Validate that a given value is a member"""
112126
return value in cls.string
113127

@@ -276,7 +290,7 @@ class LTR390: # pylint:disable=too-many-instance-attributes
276290
277291
Once read, this property will be False until it is updated in the next measurement cycle"""
278292

279-
def __init__(self, i2c, address=_DEFAULT_I2C_ADDR):
293+
def __init__(self, i2c: I2C, address: int = _DEFAULT_I2C_ADDR) -> None:
280294
self.i2c_device = i2c_device.I2CDevice(i2c, address)
281295
if self._id_reg != 0xB2:
282296

@@ -285,7 +299,7 @@ def __init__(self, i2c, address=_DEFAULT_I2C_ADDR):
285299
self._mode_cache = None
286300
self.initialize()
287301

288-
def initialize(self):
302+
def initialize(self) -> None:
289303
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
290304
defaults so it can be used"""
291305

@@ -303,7 +317,7 @@ def initialize(self):
303317
# self.high_threshold = 1000
304318
# ltr.configInterrupt(true, LTR390_MODE_UVS);
305319

306-
def _reset(self):
320+
def _reset(self) -> None:
307321
# The LTR390 software reset is ill behaved and can leave I2C bus in bad state.
308322
# Instead, just manually set register reset values per datasheet.
309323
with self.i2c_device as i2c:
@@ -316,11 +330,11 @@ def _reset(self):
316330
i2c.write(bytes((_THRESH_LOW, 0x00, 0x00, 0x00)))
317331

318332
@property
319-
def _mode(self):
333+
def _mode(self) -> Literal[0, 1]:
320334
return self._mode_bit
321335

322336
@_mode.setter
323-
def _mode(self, value):
337+
def _mode(self, value: Literal[0, 1]) -> None:
324338
if not value in [ALS, UV]:
325339
raise AttributeError("Mode must be ALS or UV")
326340
if self._mode_cache != value:
@@ -330,44 +344,46 @@ def _mode(self, value):
330344

331345
# something is wrong here; I had to add a sleep to the loop to get both to update correctly
332346
@property
333-
def uvs(self):
347+
def uvs(self) -> int:
334348
"""The calculated UV value"""
335349
self._mode = UV
336350
while not self.data_ready:
337351
sleep(0.010)
338352
return self._uvs_data_reg
339353

340354
@property
341-
def light(self):
355+
def light(self) -> int:
342356
"""The currently measured ambient light level"""
343357
self._mode = ALS
344358
while not self.data_ready:
345359
sleep(0.010)
346360
return self._als_data_reg
347361

348362
@property
349-
def gain(self):
363+
def gain(self) -> int:
350364
"""The amount of gain the raw measurements are multiplied by"""
351365
return self._gain_bits
352366

353367
@gain.setter
354-
def gain(self, value):
368+
def gain(self, value: int):
355369
if not Gain.is_valid(value):
356370
raise AttributeError("gain must be a Gain")
357371
self._gain_bits = value
358372

359373
@property
360-
def resolution(self):
374+
def resolution(self) -> int:
361375
"""Set the precision of the internal ADC used to read the light measurements"""
362376
return self._resolution_bits
363377

364378
@resolution.setter
365-
def resolution(self, value):
379+
def resolution(self, value: int):
366380
if not Resolution.is_valid(value):
367381
raise AttributeError("resolution must be a Resolution")
368382
self._resolution_bits = value
369383

370-
def enable_alerts(self, enable, source, persistance):
384+
def enable_alerts(
385+
self, enable: bool, source: Literal[0, 1], persistance: int
386+
) -> None:
371387
"""The configuration of alerts raised by the sensor
372388
373389
:param enable: Whether the interrupt output is enabled
@@ -386,19 +402,19 @@ def enable_alerts(self, enable, source, persistance):
386402
self._int_persistance_bits = persistance
387403

388404
@property
389-
def measurement_delay(self):
405+
def measurement_delay(self) -> int:
390406
"""The delay between measurements. This can be used to set the measurement rate which
391407
affects the sensor power usage."""
392408
return self._measurement_delay_bits
393409

394410
@measurement_delay.setter
395-
def measurement_delay(self, value):
411+
def measurement_delay(self, value: int) -> None:
396412
if not MeasurementDelay.is_valid(value):
397413
raise AttributeError("measurement_delay must be a MeasurementDelay")
398414
self._measurement_delay_bits = value
399415

400416
@property
401-
def uvi(self):
417+
def uvi(self) -> float:
402418
"""Read UV count and return calculated UV Index (UVI) value based upon the rated sensitivity
403419
of 1 UVI per 2300 counts at 18X gain factor and 20-bit resolution."""
404420
return (
@@ -413,22 +429,22 @@ def uvi(self):
413429
)
414430

415431
@property
416-
def lux(self):
432+
def lux(self) -> float:
417433
"""Read light level and return calculated Lux value."""
418434
return (
419435
(self.light * 0.6)
420436
/ (Gain.factor[self.gain] * Resolution.integration[self.resolution])
421437
) * self._window_factor
422438

423439
@property
424-
def window_factor(self):
440+
def window_factor(self) -> float:
425441
"""Window transmission factor (Wfac) for UVI and Lux calculations.
426442
A factor of 1 (default) represents no window or clear glass; > 1 for a tinted window.
427443
Factor of > 1 requires an empirical calibration with a reference light source."""
428444
return self._window_factor
429445

430446
@window_factor.setter
431-
def window_factor(self, factor=1):
447+
def window_factor(self, factor: float = 1):
432448
if factor < 1:
433449
raise ValueError(
434450
"window transmission factor must be a value of 1.0 or greater"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-register
77
adafruit-circuitpython-busdevice
8+
typing-extensions~=4.0

0 commit comments

Comments
 (0)