Skip to content

[#37] Add type annotations #38

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 1 commit into from
Oct 31, 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
22 changes: 15 additions & 7 deletions adafruit_ds3231.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
from adafruit_register import i2c_bcd_alarm
from adafruit_register import i2c_bcd_datetime

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import I2C
from time import struct_time
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DS3231.git"

Expand Down Expand Up @@ -131,28 +139,28 @@ class DS3231:
_busy = i2c_bit.ROBit(0x0F, 2)
_conv = i2c_bit.RWBit(0x0E, 5)

def __init__(self, i2c):
def __init__(self, i2c: I2C) -> None:
self.i2c_device = I2CDevice(i2c, 0x68)

@property
def datetime(self):
def datetime(self) -> struct_time:
"""Gets the current date and time or sets the current date and time
then starts the clock."""
return self.datetime_register

@datetime.setter
def datetime(self, value):
def datetime(self, value: struct_time) -> None:
self.datetime_register = value
self.disable_oscillator = False
self.lost_power = False

@property
def temperature(self):
def temperature(self) -> float:
"""Returns the last temperature measurement. Temperature is updated
only every 64 seconds, or when a conversion is forced."""
return self._temperature / 4

def force_temperature_conversion(self):
def force_temperature_conversion(self) -> float:
"""Forces a conversion and returns the new temperature"""
while self._busy:
pass # Wait for any normal in-progress conversion to complete
Expand All @@ -162,7 +170,7 @@ def force_temperature_conversion(self):
return self.temperature

@property
def calibration(self):
def calibration(self) -> int:
"""Calibrate the frequency of the crystal oscillator by adding or
removing capacitance. The datasheet calls this the Aging Offset.
Calibration values range from -128 to 127; each step is approximately
Expand All @@ -172,6 +180,6 @@ def calibration(self):
return self._calibration

@calibration.setter
def calibration(self, value):
def calibration(self, value: int) -> None:
self._calibration = value
self.force_temperature_conversion()