Skip to content
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

Fix set temperature from HA #38

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test Python Package

on:
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Run tests
run: python -m unittest
16 changes: 7 additions & 9 deletions pynobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,22 @@ class API:
DICT_NAME_TO_WEEK_PROFILE_STATUS = {NAME_ECO : WEEK_PROFILE_STATE_ECO, NAME_COMFORT : WEEK_PROFILE_STATE_COMFORT, NAME_AWAY : WEEK_PROFILE_STATE_AWAY, NAME_OFF : WEEK_PROFILE_STATE_OFF}

def is_valid_datetime(timestamp: str):
try:
datetime.datetime.strptime(timestamp, '%Y%m%d%H%M')
except ValueError:
if len(timestamp) != 12:
# Leading zero is optional for some of the fields below, but we require it.
return False
return True

def is_valid_time(time_of_day: str):
try:
datetime.datetime.strptime(time_of_day, '%H%M')
datetime.datetime.strptime(timestamp, '%Y%m%d%H%M')
except ValueError:
return False
return True

def time_is_quarter(minutes: str):
return int(minutes) % 15 == 0

def validate_temperature(temperature: str):
if not temperature.isdigit():
def validate_temperature(temperature: Union[int, str]):
if type(temperature) not in (int, str):
raise TypeError('Temperature must be integer or string')
if isinstance(temperature, str) and not temperature.isdigit():
raise ValueError(f'Temperature "{temperature}" must be digits')
temperature_int = int(temperature)
if temperature_int < 7:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# For a discussion on single-sourcing the version across setup.py and the
# project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.8.0',
version='1.8.1',
description='Nobø Hub / Nobø Energy Control TCP/IP Interface',

license='GPLv3+',
Expand Down
49 changes: 49 additions & 0 deletions test_pynobo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import unittest

from pynobo import nobo

class TestValidation(unittest.TestCase):

def test_is_valid_datetime(self):
self.assertTrue(nobo.API.is_valid_datetime("202404041800"))
self.assertFalse(nobo.API.is_valid_datetime("2024040418001"))
self.assertFalse(nobo.API.is_valid_datetime("20240404180"))
self.assertFalse(nobo.API.is_valid_datetime("invalid"))

def test_time_is_quarter(self):
self.assertTrue(nobo.API.time_is_quarter("00"))
self.assertTrue(nobo.API.time_is_quarter("15"))
self.assertTrue(nobo.API.time_is_quarter("30"))
self.assertTrue(nobo.API.time_is_quarter("45"))
self.assertFalse(nobo.API.time_is_quarter("01"))
self.assertFalse(nobo.API.time_is_quarter("59"))

def test_validate_temperature(self):
nobo.API.validate_temperature("20")
nobo.API.validate_temperature(20)
nobo.API.validate_temperature(7)
nobo.API.validate_temperature(30)
with self.assertRaises(TypeError):
nobo.API.validate_temperature(0.0)
with self.assertRaisesRegex(ValueError, "must be digits"):
nobo.API.validate_temperature("foo")
with self.assertRaisesRegex(ValueError, "Min temperature is 7"):
nobo.API.validate_temperature(6)
with self.assertRaisesRegex(ValueError, "Max temperature is 30"):
nobo.API.validate_temperature(31)

def test_validate_week_profile(self):
nobo.API.validate_week_profile(['00000','12001','16000','00000','12001','16000','00000','12001','16000','00000','12001','16000','00000','12001','16000','00000','12001','16000','00000','12001','16000'])
nobo.API.validate_week_profile(['00000','00000','00000','00000','00000','00000','00000'])
nobo.API.validate_week_profile(['00000','00001','00002','00004','00000','00000','00000'])
with self.assertRaisesRegex(ValueError, "must contain exactly 7 entries for midnight"):
nobo.API.validate_week_profile(['00000','00000','00000','00000','00000','00000'])
with self.assertRaisesRegex(ValueError, "must contain exactly 7 entries for midnight"):
nobo.API.validate_week_profile(['00000','00000','00000','00000','00000','00000','00000','00000'])
with self.assertRaisesRegex(ValueError, "invalid state"):
nobo.API.validate_week_profile(['00003','00000','00000','00000','00000','00000','00000'])
with self.assertRaisesRegex(ValueError, "not in whole quarters"):
nobo.API.validate_week_profile(['00000','01231','00000','00000','00000','00000','00000','00000'])

if __name__ == '__main__':
unittest.main()