Skip to content

Commit

Permalink
[#27] In a Github workflow, run unit tests against a HA container
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Senart committed Jan 12, 2025
1 parent 9919d28 commit e109f31
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-lint/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

#----------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gazpar2haws/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions gazpar2haws/gazpar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand Down
17 changes: 14 additions & 3 deletions gazpar2haws/haws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...")

Expand All @@ -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...")
Expand Down Expand Up @@ -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}"
)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e109f31

Please # to comment.