Skip to content

Add type hints #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions adafruit_veml6070.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const

try:
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass


# Set I2C addresses:
_VEML6070_ADDR_ARA = const(0x18 >> 1)
Expand Down Expand Up @@ -125,7 +131,9 @@ class VEML6070:

"""

def __init__(self, i2c_bus, _veml6070_it="VEML6070_1_T", ack=False):
def __init__(
self, i2c_bus: I2C, _veml6070_it: str = "VEML6070_1_T", ack: bool = False
) -> None:
# Check if the IT is valid
if _veml6070_it not in _VEML6070_INTEGRATION_TIME:
raise ValueError(
Expand Down Expand Up @@ -162,7 +170,7 @@ def __init__(self, i2c_bus, _veml6070_it="VEML6070_1_T", ack=False):
i2c_cmd.write(self.buf)

@property
def uv_raw(self):
def uv_raw(self) -> int:
"""
Reads and returns the value of the UV intensity.
"""
Expand All @@ -176,7 +184,7 @@ def uv_raw(self):
return buffer[1] << 8 | buffer[0]

@property
def ack(self):
def ack(self) -> int:
"""
Turns on or off the ACKnowledge function of the sensor. The ACK function will send
a signal to the host when the value of the sensed UV light changes beyond the
Expand All @@ -185,9 +193,9 @@ def ack(self):
return self._ack

@ack.setter
def ack(self, new_ack):
def ack(self, new_ack: int) -> None:
if new_ack != bool(new_ack):
raise ValueError("ACK must be 'True' or 'False'.")
raise ValueError("ACK must be '1' (On) or '0' (Off).")
self._ack = int(new_ack)
self.buf[0] = (
self._ack << 5
Expand All @@ -199,7 +207,7 @@ def ack(self, new_ack):
i2c_cmd.write(self.buf)

@property
def ack_threshold(self):
def ack_threshold(self) -> int:
"""
The ACKnowledge Threshold, which alerts the host controller to value changes
greater than the threshold. Available settings are: :const:`0` = 102 steps;
Expand All @@ -208,7 +216,7 @@ def ack_threshold(self):
return self._ack_thd

@ack_threshold.setter
def ack_threshold(self, new_ack_thd):
def ack_threshold(self, new_ack_thd: int) -> None:
if new_ack_thd not in (0, 1):
raise ValueError("ACK Threshold must be '0' or '1'.")
self._ack_thd = int(new_ack_thd)
Expand All @@ -222,7 +230,7 @@ def ack_threshold(self, new_ack_thd):
i2c_cmd.write(self.buf)

@property
def integration_time(self):
def integration_time(self) -> str:
"""
The Integration Time of the sensor, which is the refresh interval of the
sensor. The higher the refresh interval, the more accurate the reading is (at
Expand All @@ -232,7 +240,7 @@ def integration_time(self):
return self._it

@integration_time.setter
def integration_time(self, new_it):
def integration_time(self, new_it: str) -> None:
if new_it not in _VEML6070_INTEGRATION_TIME:
raise ValueError(
"Integration Time invalid. Valid values are: ",
Expand All @@ -249,7 +257,7 @@ def integration_time(self, new_it):
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)

def sleep(self):
def sleep(self) -> None:
"""
Puts the VEML6070 into sleep ('shutdown') mode. Datasheet claims a current draw
of 1uA while in shutdown.
Expand All @@ -258,7 +266,7 @@ def sleep(self):
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)

def wake(self):
def wake(self) -> None:
"""
Wakes the VEML6070 from sleep. :class:`VEML6070.uv_raw` will also wake from sleep.
"""
Expand All @@ -271,7 +279,7 @@ def wake(self):
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)

def get_index(self, _raw):
def get_index(self, _raw: int) -> str:
"""
Calculates the UV Risk Level based on the captured UV reading. Requires the ``_raw``
argument (from :meth:`veml6070.uv_raw`). Risk level is available for Integration Times (IT)
Expand Down