From e109f31d0fa9f9f5a731b9b15b02932e43ed9d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Senart?= <> Date: Mon, 13 Jan 2025 00:05:04 +0100 Subject: [PATCH] [#27] In a Github workflow, run unit tests against a HA container --- .github/workflows/python-lint/action.yaml | 2 +- gazpar2haws/bridge.py | 2 +- gazpar2haws/gazpar.py | 8 ++++---- gazpar2haws/haws.py | 17 ++++++++++++++--- pyproject.toml | 3 ++- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.github/workflows/python-lint/action.yaml b/.github/workflows/python-lint/action.yaml index 7f7d185..27bb5ff 100644 --- a/.github/workflows/python-lint/action.yaml +++ b/.github/workflows/python-lint/action.yaml @@ -89,7 +89,7 @@ runs: # Run Mypy - name: Mypy shell: bash - run: poetry run mypy . --exclude .venv + run: poetry run mypy . --install-types --non-interactive --exclude .venv continue-on-error: true #---------------------------------------------- diff --git a/gazpar2haws/bridge.py b/gazpar2haws/bridge.py index 8c6fb36..cfe4d92 100644 --- a/gazpar2haws/bridge.py +++ b/gazpar2haws/bridge.py @@ -41,7 +41,7 @@ def __init__(self, config: config_utils.ConfigLoader): # ---------------------------------- # Graceful shutdown function - def handle_signal(self, signum): + def handle_signal(self, signum, _): print(f"Signal {signum} received. Shutting down gracefully...") Logger.info(f"Signal {signum} received. Shutting down gracefully...") self._running = False diff --git a/gazpar2haws/gazpar.py b/gazpar2haws/gazpar.py index 1cb52b2..acd47c4 100644 --- a/gazpar2haws/gazpar.py +++ b/gazpar2haws/gazpar.py @@ -26,8 +26,8 @@ def __init__(self, config: dict[str, Any], homeassistant: HomeAssistantWS): self._password = config.get("password") self._pce_identifier = str(config.get("pce_identifier")) self._tmp_dir = config.get("tmp_dir") - self._last_days = int(config.get("last_days")) - self._timezone = config.get("timezone") + self._last_days = int(str(config.get("last_days"))) + self._timezone = str(config.get("timezone")) self._reset = bool(config.get("reset")) # As of date: YYYY-MM-DD @@ -193,14 +193,14 @@ async def _find_last_date_days_value( if last_statistic: # Extract the end date of the last statistics from the unix timestamp last_date = datetime.fromtimestamp( - last_statistic.get("start") / 1000, tz=pytz.timezone(self._timezone) + int(str(last_statistic.get("start"))) / 1000, tz=pytz.timezone(self._timezone) ) # Compute the number of days since the last statistics last_days = (self._as_of_date - last_date).days # Get the last meter value - last_value = last_statistic.get("sum") + last_value = float(str(last_statistic.get("sum"))) Logger.debug( f"Last date: {last_date}, last days: {last_days}, last value: {last_value}" diff --git a/gazpar2haws/haws.py b/gazpar2haws/haws.py index 8f7c64b..1580d12 100644 --- a/gazpar2haws/haws.py +++ b/gazpar2haws/haws.py @@ -69,10 +69,13 @@ async def disconnect(self): Logger.debug("Disconnected from Home Assistant") # ---------------------------------- - async def send_message(self, message: dict) -> dict: + async def send_message(self, message: dict) -> dict | list[dict]: Logger.debug("Sending a message...") + if self._websocket is None: + raise HomeAssistantWSException("Not connected to Home Assistant") + message["id"] = self._message_id self._message_id += 1 @@ -96,7 +99,7 @@ async def send_message(self, message: dict) -> dict: return response_data.get("result") # ---------------------------------- - async def list_statistic_ids(self, statistic_type: str = None) -> list[str]: + async def list_statistic_ids(self, statistic_type: str | None = None) -> list[dict]: Logger.debug("Listing statistics IDs...") @@ -108,13 +111,17 @@ async def list_statistic_ids(self, statistic_type: str = None) -> list[str]: response = await self.send_message(list_statistic_ids_message) + # Check response instance type + if not isinstance(response, list): + raise HomeAssistantWSException(f"Invalid list_statistic_ids response type: got {type(response)} instead of list[dict]") + Logger.debug(f"Listed statistics IDs: {len(response)} ids") return response # ---------------------------------- async def exists_statistic_id( - self, entity_id: str, statistic_type: str = None + self, entity_id: str, statistic_type: str | None = None ) -> bool: Logger.debug(f"Checking if {entity_id} exists...") @@ -151,6 +158,10 @@ async def statistics_during_period( response = await self.send_message(statistics_message) + # Check response instance type + if not isinstance(response, dict): + raise HomeAssistantWSException(f"Invalid statistics_during_period response type: got {type(response)} instead of dict") + Logger.debug( f"Received {entity_ids} statistics during period from {start_time} to {end_time}" ) diff --git a/pyproject.toml b/pyproject.toml index 29b1221..7eda124 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,8 @@ classifiers = [ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13" ] dependencies = [ "pygazpar>=1.2.7",