Skip to content

Commit ba0e253

Browse files
authored
Merge pull request #17 from tcfranks/main
Add Missing Type Annotations
2 parents 797747d + 72220b4 commit ba0e253

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

adafruit_shtc3.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
import time
4242
from adafruit_bus_device import i2c_device
4343

44+
try:
45+
from typing import Tuple
46+
from busio import I2C
47+
except ImportError:
48+
pass
4449

4550
# include "Arduino.h"
4651
# include <Adafruit_I2CDevice.h>
@@ -113,7 +118,7 @@ class SHTC3:
113118
114119
"""
115120

116-
def __init__(self, i2c_bus):
121+
def __init__(self, i2c_bus: I2C) -> None:
117122
self.i2c_device = i2c_device.I2CDevice(i2c_bus, _SHTC3_DEFAULT_ADDR)
118123

119124
self._buffer = bytearray(6)
@@ -124,15 +129,15 @@ def __init__(self, i2c_bus):
124129
if self._chip_id != _SHTC3_CHIP_ID:
125130
raise RuntimeError("Failed to find an SHTC3 sensor - check your wiring!")
126131

127-
def _write_command(self, command):
132+
def _write_command(self, command: int) -> None:
128133
"""helper function to write a command to the i2c device"""
129134
self._buffer[0] = command >> 8
130135
self._buffer[1] = command & 0xFF
131136

132137
with self.i2c_device as i2c:
133138
i2c.write(self._buffer, start=0, end=2)
134139

135-
def _get_chip_id(self): # readCommand(SHTC3_READID, data, 3);
140+
def _get_chip_id(self) -> int: # readCommand(SHTC3_READID, data, 3);
136141
"""Determines the chip id of the sensor"""
137142
self._write_command(_SHTC3_READID)
138143
time.sleep(0.001)
@@ -141,7 +146,7 @@ def _get_chip_id(self): # readCommand(SHTC3_READID, data, 3);
141146

142147
return unpack_from(">H", self._buffer)[0] & 0x083F
143148

144-
def reset(self):
149+
def reset(self) -> None:
145150
"""Perform a soft reset of the sensor, resetting all settings to their power-on defaults"""
146151
self.sleeping = False
147152
try:
@@ -153,12 +158,12 @@ def reset(self):
153158
time.sleep(0.001)
154159

155160
@property
156-
def sleeping(self):
161+
def sleeping(self) -> bool:
157162
"""Determines the sleep state of the sensor"""
158163
return self._cached_sleep
159164

160165
@sleeping.setter
161-
def sleeping(self, sleep_enabled):
166+
def sleeping(self, sleep_enabled: bool) -> None:
162167
if sleep_enabled:
163168
self._write_command(_SHTC3_SLEEP)
164169
else:
@@ -169,26 +174,26 @@ def sleeping(self, sleep_enabled):
169174
# lowPowerMode(bool readmode) { _lpMode = readmode
170175

171176
@property
172-
def low_power(self):
177+
def low_power(self) -> bool:
173178
"""Enables the less accurate low power mode, trading accuracy for power consumption"""
174179
return self._low_power
175180

176181
@low_power.setter
177-
def low_power(self, low_power_enabled):
182+
def low_power(self, low_power_enabled: bool) -> None:
178183
self._low_power = low_power_enabled
179184

180185
@property
181-
def relative_humidity(self):
186+
def relative_humidity(self) -> float:
182187
"""The current relative humidity in % rH. This is a value from 0-100%."""
183188
return self.measurements[1]
184189

185190
@property
186-
def temperature(self):
191+
def temperature(self) -> float:
187192
"""The current temperature in degrees Celsius"""
188193
return self.measurements[0]
189194

190195
@property
191-
def measurements(self):
196+
def measurements(self) -> Tuple[float, float]:
192197
"""both `temperature` and `relative_humidity`, read simultaneously"""
193198

194199
self.sleeping = False
@@ -239,7 +244,7 @@ def measurements(self):
239244
# Test data [0xBE, 0xEF] should yield 0x92
240245

241246
@staticmethod
242-
def _crc8(buffer):
247+
def _crc8(buffer: bytearray) -> int:
243248
"""verify the crc8 checksum"""
244249
crc = 0xFF
245250
for byte in buffer:

0 commit comments

Comments
 (0)