Skip to content

Commit c8a5eb2

Browse files
author
Alec Delaney
committed
Added type annotations
1 parent 017773c commit c8a5eb2

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

adafruit_ens160.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
from adafruit_register.i2c_bit import RWBit, ROBit
3535
from adafruit_register.i2c_bits import ROBits
3636

37+
try:
38+
from typing import Dict, Optional, Union, List
39+
from typing_extensions import Literal
40+
from busio import I2C
41+
except ImportError:
42+
pass
43+
3744
__version__ = "0.0.0+auto.0"
3845
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ENS160.git"
3946

@@ -70,8 +77,9 @@
7077

7178
class ENS160:
7279
"""Driver for the ENS160 air quality sensor
80+
7381
:param ~busio.I2C i2c_bus: The I2C bus the ENS160 is connected to.
74-
:param address: The I2C device address. Defaults to :const:`0x53`
82+
:param int address: The I2C device address. Defaults to :const:`0x53`
7583
"""
7684

7785
part_id = ROUnaryStruct(_ENS160_REG_PARTID, "<H")
@@ -96,7 +104,7 @@ class ENS160:
96104
interrupt_on_data = RWBit(_ENS160_REG_CONFIG, 1)
97105
interrupt_enable = RWBit(_ENS160_REG_CONFIG, 0)
98106

99-
def __init__(self, i2c_bus, address=ENS160_I2CADDR_DEFAULT):
107+
def __init__(self, i2c_bus: I2C, address: int = ENS160_I2CADDR_DEFAULT) -> None:
100108
# pylint: disable=no-member
101109
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
102110

@@ -119,25 +127,25 @@ def __init__(self, i2c_bus, address=ENS160_I2CADDR_DEFAULT):
119127
self.temperature_compensation = 25
120128
self.humidity_compensation = 50
121129

122-
def reset(self):
130+
def reset(self) -> None:
123131
"""Perform a soft reset command"""
124132
self.mode = MODE_RESET
125133
time.sleep(0.01)
126134

127-
def clear_command(self):
135+
def clear_command(self) -> None:
128136
"""Clears out custom data"""
129137
self.command = COMMAND_NOP
130138
self.command = COMMAND_CLRGPR
131139
time.sleep(0.01)
132140

133-
def _read_gpr(self):
141+
def _read_gpr(self) -> None:
134142
"""Read 8 bytes of general purpose registers into self._buf"""
135143
self._buf[0] = _ENS160_REG_GPRREAD
136144
with self.i2c_device as i2c:
137145
i2c.write_then_readinto(self._buf, self._buf, out_end=1)
138146

139147
@property
140-
def new_data_available(self):
148+
def new_data_available(self) -> bool:
141149
"""This function is wierd, it checks if there's new data or
142150
GPR (resistances) and if so immediately reads it into the
143151
internal buffer... otherwise the status is lost!"""
@@ -166,13 +174,13 @@ def new_data_available(self):
166174

167175
return newdat
168176

169-
def read_all_sensors(self):
177+
def read_all_sensors(self) -> Dict[str, Optional[Union[int, List[int]]]]:
170178
"""All of the currently buffered sensor information"""
171179
# return the currently buffered deets
172180
return self._bufferdict
173181

174182
@property
175-
def firmware_version(self):
183+
def firmware_version(self) -> str:
176184
"""Read the semver firmware version from the general registers"""
177185
curr_mode = self.mode
178186
self.mode = MODE_IDLE
@@ -184,34 +192,34 @@ def firmware_version(self):
184192
return "%d.%d.%d" % (self._buf[4], self._buf[5], self._buf[6])
185193

186194
@property
187-
def mode(self):
188-
"""Operational Mode, can be MODE_SLEEP, MODE_IDLE, or MODE_STANDARD"""
195+
def mode(self) -> Literal[0, 1, 2, 240]:
196+
"""Operational Mode, can be MODE_SLEEP, MODE_IDLE, MODE_STANDARD, or MODE_RESET"""
189197
return self._mode
190198

191199
@mode.setter
192-
def mode(self, newmode):
200+
def mode(self, newmode: Literal[0, 1, 2, 240]) -> None:
193201
if not newmode in _valid_modes:
194202
raise RuntimeError(
195-
"Invalid mode: must be MODE_SLEEP, MODE_IDLE, or MODE_STANDARD"
203+
"Invalid mode: must be MODE_SLEEP, MODE_IDLE, MODE_STANDARD, or MODE_RESET"
196204
)
197205
self._mode = newmode
198206

199207
@property
200-
def temperature_compensation(self):
208+
def temperature_compensation(self) -> float:
201209
"""Temperature compensation setting, set this to ambient temperature
202210
to get best gas sensor readings, floating point degrees C"""
203211
return (self._temp_in / 64.0) - 273.15
204212

205213
@temperature_compensation.setter
206-
def temperature_compensation(self, temp_c):
214+
def temperature_compensation(self, temp_c: float) -> None:
207215
self._temp_in = int((temp_c + 273.15) * 64.0 + 0.5)
208216

209217
@property
210-
def humidity_compensation(self):
218+
def humidity_compensation(self) -> float:
211219
"""Humidity compensation setting, set this to ambient relative
212220
humidity (percentage 0-100) to get best gas sensor readings"""
213221
return self._rh_in / 512.0
214222

215223
@humidity_compensation.setter
216-
def humidity_compensation(self, hum_perc):
224+
def humidity_compensation(self, hum_perc: float) -> None:
217225
self._rh_in = int(hum_perc * 512 + 0.5)

requirements.txt

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

0 commit comments

Comments
 (0)