Skip to content

Commit

Permalink
QA: Code quality and fix pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Feb 15, 2024
1 parent a44cbf5 commit 3ffcef5
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 43 deletions.
4 changes: 2 additions & 2 deletions automationhat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion automationhat/pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions examples/hat-mini/analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions examples/hat-mini/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions examples/hat-mini/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions examples/hat/analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import automationhat

time.sleep(0.1) # short pause after ads1015 class creation recommended


print("""
Press CTRL+C to exit.
""")
Expand Down
1 change: 0 additions & 1 deletion examples/hat/lights.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import signal
import time

import automationhat

Expand Down
41 changes: 16 additions & 25 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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))
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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!")
Expand Down
5 changes: 0 additions & 5 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import sys

import mock


def test_setup(gpio, smbus, sn3218, ads1015, automationhat):
automationhat.setup()

Expand Down

0 comments on commit 3ffcef5

Please sign in to comment.