diff --git a/automationhat/__init__.py b/automationhat/__init__.py index 2697d49..6070b5e 100644 --- a/automationhat/__init__.py +++ b/automationhat/__init__.py @@ -6,7 +6,7 @@ import RPi.GPIO as GPIO import sn3218 -from .pins import AsyncWorker, ObjectCollection, StoppableThread +from .pins import AsyncWorker, ObjectCollection, StoppableThread # noqa: F401 __version__ = '0.4.1' @@ -72,7 +72,7 @@ def write(self, value): if self.index is None: return - if type(value) is not int and type(value) is not float: + if not isinstance(value, (int, float)): raise TypeError("Value must be int or float") if value >= 0 and value <= 1.0: diff --git a/automationhat/pins.py b/automationhat/pins.py index 88e9a1c..18a29df 100644 --- a/automationhat/pins.py +++ b/automationhat/pins.py @@ -97,7 +97,7 @@ def handler(*args, **kwargs): return handler def __getitem__(self, key): - """Supprot accessing with [n]""" + """Support accessing with [n]""" if isinstance(key, int): return self._all[self._index[key]] else: diff --git a/examples/hat-mini/analog.py b/examples/hat-mini/analog.py index c33c2cf..828d558 100755 --- a/examples/hat-mini/analog.py +++ b/examples/hat-mini/analog.py @@ -5,8 +5,6 @@ import automationhat -time.sleep(0.1) # Short pause after ads1015 class creation recommended - try: from PIL import Image, ImageDraw, ImageFont except ImportError: diff --git a/examples/hat-mini/input.py b/examples/hat-mini/input.py index 94b491a..815243a 100755 --- a/examples/hat-mini/input.py +++ b/examples/hat-mini/input.py @@ -5,8 +5,6 @@ import automationhat -time.sleep(0.1) # Short pause after ads1015 class creation recommended - try: from PIL import Image, ImageDraw except ImportError: diff --git a/examples/hat-mini/output.py b/examples/hat-mini/output.py index a34d465..eb2c097 100755 --- a/examples/hat-mini/output.py +++ b/examples/hat-mini/output.py @@ -6,8 +6,6 @@ import automationhat -time.sleep(0.1) # Short pause after ads1015 class creation recommended - try: from PIL import Image, ImageDraw except ImportError: diff --git a/examples/hat/analog.py b/examples/hat/analog.py index 3cdabef..22aa242 100755 --- a/examples/hat/analog.py +++ b/examples/hat/analog.py @@ -4,9 +4,6 @@ import automationhat -time.sleep(0.1) # short pause after ads1015 class creation recommended - - print(""" Press CTRL+C to exit. """) diff --git a/examples/hat/lights.py b/examples/hat/lights.py index ef9b7cd..65c100f 100755 --- a/examples/hat/lights.py +++ b/examples/hat/lights.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import signal -import time import automationhat diff --git a/test.py b/test.py index d938cbf..0b14e6c 100755 --- a/test.py +++ b/test.py @@ -10,17 +10,15 @@ def rpi_gpio_output(pin, value): global pinstates pinstates[pin] = value - #debug("Setting {} to {}".format(pin, value)) + def rpi_gpio_input(pin): return pinstates[pin] -def sn3218_write_i2c_block_data(addr, reg, data): - debug("Writing {} to {}:{}".format(data, addr, reg)) rpi = mock.Mock() rpi.GPIO = mock.Mock() -rpi.GPIO.input = rpi_gpio_input # mock.Mock(return_value=0) +rpi.GPIO.input = rpi_gpio_input rpi.GPIO.output = rpi_gpio_output sys.modules['RPi'] = rpi @@ -29,13 +27,10 @@ def sn3218_write_i2c_block_data(addr, reg, data): sn3218 = mock.Mock() sn3218.i2c = mock.Mock() sn3218.i2c.read_i2c_block_data = mock.Mock(return_value=[0,0,0]) -#sn3218.i2c.write_i2c_block_data = sn3218_write_i2c_block_data sys.modules['sn3218'] = sn3218 -import RPi.GPIO - -import automationhat +import automationhat # noqa: E402 def test(verbose=False): @@ -50,7 +45,7 @@ def debug(msg): debug("testing: Lights") - assert str(automationhat.light).split(", ") == ["warn","comms","power"], "Lights missing one of [warn, comms, power]: {}".format(str(automationhat.light)) + assert sorted(str(automationhat.light).split(", ")) == ["comms", "power", "warn"], "Lights missing one of [warn, comms, power]: {}".format(str(automationhat.light)) for light in automationhat.light: debug(" light {}".format(light.name)) @@ -63,7 +58,7 @@ def debug(msg): debug("testing: Relays") - assert str(automationhat.relay).split(", ") == ["three","two","one"], "Relay missing one of [one, two, three]: {}".format(str(automationhat.relay)) + assert str(automationhat.relay).split(", ") == ["one","two","three"], "Relay missing one of [one, two, three]: {}".format(str(automationhat.relay)) # Test all relays have associated lights for relay in automationhat.relay: @@ -89,7 +84,7 @@ def debug(msg): debug("testing: Digital Outputs") - assert str(automationhat.output).split(", ") == ["three","two","one"], "Output missing one of [one, two, three]: {}".format(str(automationhat.output)) + assert str(automationhat.output).split(", ") == ["one","two","three"], "Output missing one of [one, two, three]: {}".format(str(automationhat.output)) # Test all outputs have associated lights for output in automationhat.output: @@ -116,46 +111,42 @@ def debug(msg): debug("testing: Analog Inputs") + automationhat._ads1015.get_voltage = mock.MagicMock() + for analog in automationhat.analog: debug(" analog {}".format(analog.name)) analog.auto_light(False) - assert analog._en_auto_lights == False, "Auto lights should be False/Disabled" + assert analog._en_auto_lights is False, "Auto lights should be False/Disabled" analog.auto_light(True) - assert analog._en_auto_lights == True, "Auto lights should be True/Enabled" + assert analog._en_auto_lights is True, "Auto lights should be True/Enabled" assert callable(analog.read), "analog.read() not callable!" # The full scale range of the ADC at 1:1 is +- 4.096v # so we adjust our mock value to obtain the max value at 3.3v - analog_raw = int(3300.0 * (2047.0 / 4096.0)) + # analog_raw = int(3300.0 * (2047.0 / 4096.0)) debug(" - max value") - - sn3218.i2c.read_i2c_block_data = mock.Mock(return_value=[ - (analog_raw >> 4) & 0xff, - (analog_raw << 4) & 0xff - ,0]) + + automationhat._ads1015.get_voltage.return_value = 3.3 time.sleep(0.01) assert analog.read() == analog.max_voltage, "analog.read() returning {}, should be {}!".format(analog.read(), analog.max_voltage) - analog_raw = int(1650.0 * (2047.0 / 4096.0)) + # analog_raw = int(1650.0 * (2047.0 / 4096.0)) debug(" - half value") - sn3218.i2c.read_i2c_block_data = mock.Mock(return_value=[ - (analog_raw >> 4) & 0xff, - (analog_raw << 4) & 0xff - ,0]) + automationhat._ads1015.get_voltage.return_value = 3.3 / 2.0001 # TODO: Fudge! time.sleep(0.01) voltage = round((analog.max_voltage / 2.0) - 0.005, 2) - assert analog.read() == voltage, "analog.read() returning {}, should be {}!".format(analog.read(), voltage) + assert round(analog.read(), 2) == voltage, "analog.read() returning {}, should be {}!".format(round(analog.read(), 2), voltage) debug("status: Ok!") diff --git a/tests/test_setup.py b/tests/test_setup.py index 14a52e5..c17eb0f 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -1,8 +1,3 @@ -import sys - -import mock - - def test_setup(gpio, smbus, sn3218, ads1015, automationhat): automationhat.setup()