From 28e33dbfd9e45f7521c4fa9864a506b78cb7e8fa Mon Sep 17 00:00:00 2001 From: Michael Pham <61564344+Mikefly123@users.noreply.github.com> Date: Fri, 7 Feb 2025 14:10:46 -0800 Subject: [PATCH 1/4] Fixed the I2C Device Setup Bug --- .gitignore | 1 + rv3028/rv3028.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) mode change 100644 => 100755 rv3028/rv3028.py diff --git a/.gitignore b/.gitignore index f97c8f4..ca224f7 100644 --- a/.gitignore +++ b/.gitignore @@ -171,3 +171,4 @@ cython_debug/ # PyPI configuration file .pypirc +.DS_Store diff --git a/rv3028/rv3028.py b/rv3028/rv3028.py old mode 100644 new mode 100755 index 7b1d177..6421280 --- a/rv3028/rv3028.py +++ b/rv3028/rv3028.py @@ -21,9 +21,11 @@ try: from tests.stubs.i2c_device import I2CDevice + from busio import I2C except ImportError: from adafruit_bus_device.i2c_device import I2CDevice +_RV3028_DEFAULT_ADDRESS = 0x52 class WEEKDAY: SUNDAY = 0 @@ -36,8 +38,8 @@ class WEEKDAY: class RV3028: - def __init__(self, i2c_device: I2CDevice): - self.i2c_device = i2c_device + def __init__(self, i2c_bus: I2C, address: int = _RV3028_DEFAULT_ADDRESS): + self.i2c_device = I2CDevice(i2c_bus, address) def _read_register(self, register, length=1): with self.i2c_device as i2c: From 51bf8c61cbe868349e262ba632e099d843d5113a Mon Sep 17 00:00:00 2001 From: Michael Pham <61564344+Mikefly123@users.noreply.github.com> Date: Fri, 7 Feb 2025 14:16:48 -0800 Subject: [PATCH 2/4] Rant the Linter --- rv3028/rv3028.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rv3028/rv3028.py b/rv3028/rv3028.py index 6421280..b6b6a4c 100755 --- a/rv3028/rv3028.py +++ b/rv3028/rv3028.py @@ -20,13 +20,15 @@ ) try: - from tests.stubs.i2c_device import I2CDevice from busio import I2C + + from tests.stubs.i2c_device import I2CDevice except ImportError: from adafruit_bus_device.i2c_device import I2CDevice _RV3028_DEFAULT_ADDRESS = 0x52 + class WEEKDAY: SUNDAY = 0 MONDAY = 1 From 09d4fb0dacb0b8266d35ba491f7d22c3de7fed3e Mon Sep 17 00:00:00 2001 From: "davitb2013@gmail.com" Date: Fri, 7 Feb 2025 14:52:31 -0800 Subject: [PATCH 3/4] broken test fix --- rv3028/rv3028.py | 14 +++++++++----- tests/mocks/i2cMock.py | 7 +++++-- tests/stubs/i2c_device.py | 4 ++++ tests/test_RV3028.py | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/rv3028/rv3028.py b/rv3028/rv3028.py index b6b6a4c..04552c0 100755 --- a/rv3028/rv3028.py +++ b/rv3028/rv3028.py @@ -20,11 +20,10 @@ ) try: - from busio import I2C - - from tests.stubs.i2c_device import I2CDevice + from tests.stubs.i2c_device import I2C, I2CDevice except ImportError: from adafruit_bus_device.i2c_device import I2CDevice + from busio import I2C _RV3028_DEFAULT_ADDRESS = 0x52 @@ -40,8 +39,13 @@ class WEEKDAY: class RV3028: - def __init__(self, i2c_bus: I2C, address: int = _RV3028_DEFAULT_ADDRESS): - self.i2c_device = I2CDevice(i2c_bus, address) + def __init__(self, i2c, address: int = _RV3028_DEFAULT_ADDRESS): + if isinstance(i2c, I2C): + self.i2c_device = I2CDevice(i2c, address) + elif isinstance(i2c, I2CDevice): + self.i2c_device = i2c + else: + raise TypeError("i2c should be an i2c bus or device!") def _read_register(self, register, length=1): with self.i2c_device as i2c: diff --git a/tests/mocks/i2cMock.py b/tests/mocks/i2cMock.py index 5e78799..1ae75e4 100644 --- a/tests/mocks/i2cMock.py +++ b/tests/mocks/i2cMock.py @@ -1,9 +1,12 @@ -class MockI2C: +from stubs.i2c_device import I2C, I2CDevice + + +class MockI2C(I2C): def __init__(self): self.registers = [0x00] * 256 # 256 8 bit registers -class MockI2CDevice: +class MockI2CDevice(I2CDevice): def __init__(self, i2c: MockI2C, address): self.i2c = i2c self.address = address diff --git a/tests/stubs/i2c_device.py b/tests/stubs/i2c_device.py index c4d0af1..b6e8eb4 100644 --- a/tests/stubs/i2c_device.py +++ b/tests/stubs/i2c_device.py @@ -4,3 +4,7 @@ def __enter__(self): ... def __exit__(self, exc_type, exc_value, traceback): ... def write(self, data): ... def readinto(self, buffer): ... + + +class I2C: + def __init__(self): ... diff --git a/tests/test_RV3028.py b/tests/test_RV3028.py index a5dd7d0..21fc23c 100644 --- a/tests/test_RV3028.py +++ b/tests/test_RV3028.py @@ -19,7 +19,7 @@ def rtc(): i2c_bus = MockI2C() i2c_device = MockI2CDevice(i2c_bus, 0x52) - rtc = RV3028(i2c_device) + rtc = RV3028(i2c_device, 0x52) return rtc From 4c880c1696de913993b12ef123022c0c3f63f693 Mon Sep 17 00:00:00 2001 From: "davitb2013@gmail.com" Date: Fri, 7 Feb 2025 15:07:24 -0800 Subject: [PATCH 4/4] actual test fix --- tests/mocks/i2cMock.py | 2 +- tests/test_RV3028.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mocks/i2cMock.py b/tests/mocks/i2cMock.py index 1ae75e4..713b783 100644 --- a/tests/mocks/i2cMock.py +++ b/tests/mocks/i2cMock.py @@ -1,4 +1,4 @@ -from stubs.i2c_device import I2C, I2CDevice +from tests.stubs.i2c_device import I2C, I2CDevice class MockI2C(I2C): diff --git a/tests/test_RV3028.py b/tests/test_RV3028.py index 21fc23c..a5dd7d0 100644 --- a/tests/test_RV3028.py +++ b/tests/test_RV3028.py @@ -19,7 +19,7 @@ def rtc(): i2c_bus = MockI2C() i2c_device = MockI2CDevice(i2c_bus, 0x52) - rtc = RV3028(i2c_device, 0x52) + rtc = RV3028(i2c_device) return rtc