Skip to content

Commit

Permalink
Updated to demonstrate class-based thermostats
Browse files Browse the repository at this point in the history
  • Loading branch information
andylockran committed Feb 4, 2024
1 parent 7de3539 commit cf6f960
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ jobs:
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=12 --max-line-length=127 --statistics
- name: Test with pytest
- name: Test with pytest and generate terminal coverage report
run: |
poetry run pytest --cov-report xml:coverage.xml --cov=heatmiserv3 tests/
poetry run pytest --cov-report term --cov=heatmiserv3 tests/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test.py
build/
dist/
.pytest_cache/v/cache/lastfailed
coverage.xml
57 changes: 13 additions & 44 deletions heatmiserv3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,29 @@ def __init__(self, ipaddress, port):
self.thermostats = {}
if ipaddress == "mock":
device = MockSerialThermostat
self._serport = serial.Serial(device.port)
self.serialport = serial.Serial(device.port)
else:
self._serport = serial.serial_for_url("socket://" + ipaddress + ":" + port)
self.serialport = serial.serial_for_url("socket://" + ipaddress + ":" + port)
# Ensures that the serial port has not
# been left hanging around by a previous process.
serport_response = self._serport.close()
logging.info("SerialPortResponse: %s", serport_response)
self._serport.baudrate = constants.COM_BAUD
self._serport.bytesize = constants.COM_SIZE
self._serport.parity = constants.COM_PARITY
self._serport.stopbits = constants.COM_STOP
self._serport.timeout = constants.COM_TIMEOUT
self.status = False
self.serialport.close()

### Serial Connection Settings
self.serialport.baudrate = constants.COM_BAUD
self.serialport.bytesize = constants.COM_SIZE
self.serialport.parity = constants.COM_PARITY
self.serialport.stopbits = constants.COM_STOP
self.serialport.timeout = constants.COM_TIMEOUT
self._open()

def _open(self):
if not self.status:
if not self.serialport.is_open:
logging.info("Opening serial port.")
self._serport.open()
self.serialport.open()
self.status = True
return True
else:
logging.info("Attempting to access already open port")
return False

def reopen(self):
if not self.status:
self._serport.open()
self.status = True
return self.status
else:
logging.error("Cannot open serial port")

def __del__(self):
logging.info("Closing serial port.")
self._serport.close()

def registerThermostat(self, thermostat):
"""Registers a thermostat with the UH1"""
try:
type(thermostat) == heatmiser.HeatmiserThermostat
if thermostat.address in self.thermostats.keys():
raise ValueError("Key already present")
else:
self.thermostats[thermostat.address] = thermostat
except ValueError:
pass
except Exception as e:
logging.info("You're not adding a HeatmiiserThermostat Object")
logging.info(e.message)
return self._serport

def listThermostats(self):
if self.thermostats:
return self.thermostats
else:
return None
self.serialport.close()
4 changes: 3 additions & 1 deletion heatmiserv3/heatmiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, address, model, uh1):
self.config = yaml.safe_load(config_yml)[model]
except yaml.YAMLError as exc:
logging.info("The YAML file is invalid: %s", exc)
self.conn = uh1.registerThermostat(self)
self.conn = uh1.serialport
self.dcb = ""
self.read_dcb()

Expand Down Expand Up @@ -251,6 +251,8 @@ def read_dcb(self):
self.dcb = self._hm_read_address()
return self.dcb


class HeatmiserThermostatPRT(HeatmiserThermostat):
def get_frost_temp(self):
"""
Returns the temperature
Expand Down
27 changes: 27 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from heatmiserv3 import connection

class TestConnection(unittest.TestCase):
"""Tests for the connection code"""
def test_init(self):
uh1 = connection.HeatmiserUH1("mock", 123)
assert uh1.status == True

def test_open(self):
uh1 = connection.HeatmiserUH1("mock", 123)
assert uh1.status == True
uh1._open()
assert uh1.status == True

def xtest_reopen(self):
"""Skipping this test as doesn't work with mock implementation."""
uh1 = connection.HeatmiserUH1("mock", 123)
uh1.serialport.close()
assert uh1.serialport.closed == True

def test_registration(self):
uh1 = connection.HeatmiserUH1("mock", 123)
try:
uh1.registerThermostat(1)
except Exception as e:
assert type(e) == AttributeError
6 changes: 3 additions & 3 deletions tests/test_heatmiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def setUp(self):
# @TODO - Setup the mock interface for serial to write the tests.
self.HeatmiserUH1 = connection.HeatmiserUH1("mock", "123")

self.thermo1 = heatmiser.HeatmiserThermostat(1,"prt", self.HeatmiserUH1)
self.thermo2 = heatmiser.HeatmiserThermostat(2,"prt", self.HeatmiserUH1)
self.thermo3 = heatmiser.HeatmiserThermostat(3,"prt", self.HeatmiserUH1)
self.thermo1 = heatmiser.HeatmiserThermostatPRT(1,"prt", self.HeatmiserUH1)
self.thermo2 = heatmiser.HeatmiserThermostatPRT(2,"prt", self.HeatmiserUH1)
self.thermo3 = heatmiser.HeatmiserThermostatPRT(3,"prt", self.HeatmiserUH1)


def test_thermo1_temperature(self):
Expand Down

0 comments on commit cf6f960

Please sign in to comment.