diff --git a/.github/workflows/automated-api-test.yaml b/.github/workflows/automated-api-test.yaml new file mode 100644 index 0000000..e7ccb16 --- /dev/null +++ b/.github/workflows/automated-api-test.yaml @@ -0,0 +1,31 @@ +name: Automated API test that runs against Thermia API and checks for breaking changes + +on: + schedule: + # run every six hours + - cron: '0 */6 * * *' + workflow_dispatch: + +jobs: + test_api: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4.2.2 + - name: Set up Python + uses: actions/setup-python@v5.3.0 + with: + python-version: "3.x" + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + pip install -U -r requirements.txt + pip install -U ThermiaOnlineAPI + - name: Create .env file + run: | + touch .env + echo "USERNAME=${{ secrets.THERMIA_USERNAME }}" >> .env + echo "PASSWORD=${{ secrets.THERMIA_PASSWORD }}" >> .env + - name: Create a debug log file + run: | + python scripts/test_api.py diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 248a2fe..48966aa 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -23,4 +23,4 @@ jobs: python -m pip install --upgrade pip pip install -U -r requirements.txt -r requirements_testing.txt - name: Run tests - run: pytest + run: pytest ThermiaOnlineAPI diff --git a/scripts/test_api.py b/scripts/test_api.py new file mode 100644 index 0000000..18011fb --- /dev/null +++ b/scripts/test_api.py @@ -0,0 +1,88 @@ +import os +from ThermiaOnlineAPI import Thermia + +TEST_EXCLUDED_LINE_STRINGS = [ + # self.__info related + "lastOnline", + "activeAlarms", + "activeCriticalAlarms", + "unreadErrors", + "unreadInfo", + "unreadWarnings", + # self.__status related + "dcmVersion", + "heatingEffect", + "hotWaterTemperature", + "indoorTemperature", + "isHotWaterActive", + "isOutdoorTempSensorFunctioning", + "outdoorTemperature", + "programVersion", + "reducedHeatingEffect", + # self.__device_data related + "firmwareVersion", + "lastOnline", + # Register group related + "registerValue", + "timeStamp", + "value", +] + + +def test_excluded_string_in_line(line: str) -> bool: + for excluded_string in TEST_EXCLUDED_LINE_STRINGS: + if excluded_string in line: + return True + return False + + +USERNAME = None +PASSWORD = None + +with open(".env", "r") as env_file: + for line in env_file: + if line.startswith("USERNAME="): + USERNAME = line.split("=")[1].strip() + elif line.startswith("PASSWORD="): + PASSWORD = line.split("=")[1].strip() + +if not USERNAME or not PASSWORD: + print("Username and password not present in .env file") + exit(1) + +thermia = Thermia(USERNAME, PASSWORD) + +print("Connected: " + str(thermia.connected)) + +heat_pump = thermia.heat_pumps[0] + +print("Fetching debug data") + +debug_data = heat_pump.debug() + +print("Comparing debug data to existing debug file") + +absolute_path = os.path.dirname(os.path.abspath(__file__)) +existing_data_filename = ( + f"{absolute_path}/../ThermiaOnlineAPI/tests/debug_files/diplomat_duo_921.txt" +) + +with open("debug.txt", "r") as f: + existing_debug_data = f.read() + +for [existing_line, new_line] in zip( + existing_debug_data.split("\n"), debug_data.split("\n") +): + if test_excluded_string_in_line(existing_line) and test_excluded_string_in_line( + new_line + ): + continue + + if existing_line != new_line: + print("Existing data does not match new data") + print("Existing line: " + existing_line) + print("New line: " + new_line) + print("\n") + exit(1) + +print("Debug data matches existing debug file")