Skip to content

Commit 2fd9b48

Browse files
authored
Merge pull request #17 from tcfranks/main
Add Missing Type Annotations
2 parents 24397ef + ea02a1e commit 2fd9b48

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

adafruit_veml6075.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_bus_device.i2c_device import I2CDevice
3434
from micropython import const
3535

36+
try:
37+
import typing # pylint: disable=unused-import
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
3642
_VEML6075_ADDR = const(0x10)
3743

3844
_REG_CONF = const(0x00)
@@ -69,17 +75,17 @@ class VEML6075:
6975

7076
def __init__(
7177
self,
72-
i2c_bus,
78+
i2c_bus: I2C,
7379
*,
74-
integration_time=50,
75-
high_dynamic=True,
76-
uva_a_coef=2.22,
77-
uva_b_coef=1.33,
78-
uvb_c_coef=2.95,
79-
uvb_d_coef=1.74,
80-
uva_response=0.001461,
81-
uvb_response=0.002591
82-
):
80+
integration_time: int = 50,
81+
high_dynamic: bool = True,
82+
uva_a_coef: float = 2.22,
83+
uva_b_coef: float = 1.33,
84+
uvb_c_coef: float = 2.95,
85+
uvb_d_coef: float = 1.74,
86+
uva_response: float = 0.001461,
87+
uvb_response: float = 0.002591,
88+
) -> None:
8389
# Set coefficients
8490
self._a = uva_a_coef
8591
self._b = uva_b_coef
@@ -96,7 +102,7 @@ def __init__(
96102
# read ID!
97103
veml_id = self._read_register(_REV_ID)
98104
if veml_id != 0x26:
99-
raise RuntimeError("Incorrect VEML6075 ID 0x%02X" % veml_id)
105+
raise RuntimeError(f"Incorrect VEML6075 ID {hex(veml_id).upper()}")
100106

101107
# shut down
102108
self._write_register(_REG_CONF, 0x01)
@@ -111,7 +117,7 @@ def __init__(
111117
conf &= ~0x01 # Power on
112118
self._write_register(_REG_CONF, conf)
113119

114-
def _take_reading(self):
120+
def _take_reading(self) -> None:
115121
"""Perform a full reading and calculation of all UV calibrated values"""
116122
time.sleep(0.1)
117123
uva = self._read_register(_REG_UVA)
@@ -126,25 +132,25 @@ def _take_reading(self):
126132
# (uva, uvb, uvcomp1, uvcomp2, dark))
127133

128134
@property
129-
def uva(self):
135+
def uva(self) -> float:
130136
"""The calibrated UVA reading, in 'counts' over the sample period"""
131137
self._take_reading()
132138
return self._uvacalc
133139

134140
@property
135-
def uvb(self):
141+
def uvb(self) -> float:
136142
"""The calibrated UVB reading, in 'counts' over the sample period"""
137143
self._take_reading()
138144
return self._uvbcalc
139145

140146
@property
141-
def uv_index(self):
147+
def uv_index(self) -> float:
142148
"""The calculated UV Index"""
143149
self._take_reading()
144150
return ((self._uvacalc * self._uvaresp) + (self._uvbcalc * self._uvbresp)) / 2
145151

146152
@property
147-
def integration_time(self):
153+
def integration_time(self) -> int:
148154
"""The amount of time the VEML is sampling data for, in millis.
149155
Valid times are 50, 100, 200, 400 or 800ms"""
150156
key = (self._read_register(_REG_CONF) >> 4) & 0x7
@@ -154,7 +160,7 @@ def integration_time(self):
154160
raise RuntimeError("Invalid integration time")
155161

156162
@integration_time.setter
157-
def integration_time(self, val):
163+
def integration_time(self, val: int) -> None:
158164
if ( # pylint: disable=consider-iterating-dictionary
159165
not val in _VEML6075_UV_IT.keys()
160166
):
@@ -164,14 +170,14 @@ def integration_time(self, val):
164170
conf |= _VEML6075_UV_IT[val] << 4
165171
self._write_register(_REG_CONF, conf)
166172

167-
def _read_register(self, register):
173+
def _read_register(self, register: int) -> int:
168174
"""Read a 16-bit value from the `register` location"""
169175
self._buffer[0] = register
170176
with self._i2c as i2c:
171177
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1, in_end=2)
172178
return (self._buffer[1] << 8) | self._buffer[0]
173179

174-
def _write_register(self, register, value):
180+
def _write_register(self, register: int, value: int) -> None:
175181
"""Write a 16-bit value to the `register` location"""
176182
self._buffer[0] = register
177183
self._buffer[1] = value

0 commit comments

Comments
 (0)