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

Add new functions to start firmware updates #317

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
38 changes: 35 additions & 3 deletions hatasmota/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from __future__ import annotations

import asyncio
import logging
import requests
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hatasmota library is mostly written with asyncio instead of threads for parallelism. Could you try to use aiohttp instead of requests for the firmware check?

from collections.abc import Callable, Coroutine
from dataclasses import dataclass
import logging
from typing import Any

from .const import COMMAND_BACKLOG
Expand All @@ -14,7 +15,6 @@

_LOGGER = logging.getLogger(__name__)


class Timer:
"""Simple timer."""

Expand Down Expand Up @@ -59,7 +59,7 @@ class ReceiveMessage:


class TasmotaMQTTClient:
"""Helper class to sue an external MQTT client."""
"""Helper class to use an external MQTT client."""

def __init__(
self,
Expand Down Expand Up @@ -115,6 +115,32 @@ async def unsubscribe(self, sub_state: dict | None) -> dict:
"""Unsubscribe from topics."""
return await self._unsubscribe(sub_state)

async def check_firmware_update(self, current_version: str) -> bool:
"""Check if a firmware update is available."""
latest_version = self.get_latest_firmware_version()
if latest_version and self.compare_versions(current_version, latest_version):
_LOGGER.info(f"Firmware update available: {latest_version}")
return True
_LOGGER.info("Firmware is up to date.")
return False

def get_latest_firmware_version(self) -> str | None:
"""Get the latest Tasmota firmware version from GitHub."""
url = "https://api.github.com/repos/arendst/Tasmota/releases/latest"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
return data["tag_name"].lstrip("v")
except Exception as err:
_LOGGER.error(f"Error retrieving latest firmware: {err}")
return None

def compare_versions(self, current_version: str, latest_version: str) -> bool:
"""Compare the current version with the latest version."""
current_version = current_version.split("(")[0] # Removes "release-tasmota"
return current_version < latest_version


async def send_commands(
mqtt_client: TasmotaMQTTClient,
Expand All @@ -125,3 +151,9 @@ async def send_commands(
backlog_topic = command_topic + COMMAND_BACKLOG
backlog = ";".join([f"NoDelay;{command[0]} {command[1]}" for command in commands])
await mqtt_client.publish(backlog_topic, backlog)


async def trigger_firmware_update(mqtt_client: TasmotaMQTTClient, command_topic: str) -> None:
"""Trigger a Tasmota firmware update via MQTT."""
_LOGGER.info(f"Triggering firmware update on {command_topic}")
await mqtt_client.publish(f"{command_topic}/cmnd/Upgrade", "1")