2024-02-19 20:29:09 +00:00
|
|
|
"""Implementation of firmware module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-08 23:43:07 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2024-06-19 18:24:12 +00:00
|
|
|
from collections.abc import Coroutine
|
2024-05-01 13:59:35 +00:00
|
|
|
from datetime import date
|
2024-08-30 17:01:54 +00:00
|
|
|
from typing import TYPE_CHECKING, Callable, Optional
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-05-08 23:43:07 +00:00
|
|
|
# When support for cpython older than 3.11 is dropped
|
|
|
|
# async_timeout can be replaced with asyncio.timeout
|
|
|
|
from async_timeout import timeout as asyncio_timeout
|
2024-05-01 13:59:35 +00:00
|
|
|
from pydantic.v1 import BaseModel, Field, validator
|
|
|
|
|
2024-08-30 17:01:54 +00:00
|
|
|
from ...exceptions import KasaException
|
2024-04-24 16:38:52 +00:00
|
|
|
from ...feature import Feature
|
2024-07-11 15:21:59 +00:00
|
|
|
from ..smartmodule import SmartModule, allow_update_after
|
2024-02-19 20:29:09 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ..smartdevice import SmartDevice
|
|
|
|
|
|
|
|
|
2024-05-08 23:43:07 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class DownloadState(BaseModel):
|
|
|
|
"""Download state."""
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# {'status': 0, 'download_progress': 0, 'reboot_time': 5,
|
|
|
|
# 'upgrade_time': 5, 'auto_upgrade': False}
|
|
|
|
status: int
|
|
|
|
progress: int = Field(alias="download_progress")
|
|
|
|
reboot_time: int
|
|
|
|
upgrade_time: int
|
|
|
|
auto_upgrade: bool
|
|
|
|
|
|
|
|
|
2024-02-19 20:29:09 +00:00
|
|
|
class UpdateInfo(BaseModel):
|
|
|
|
"""Update info status object."""
|
|
|
|
|
|
|
|
status: int = Field(alias="type")
|
2024-05-08 23:43:07 +00:00
|
|
|
version: Optional[str] = Field(alias="fw_ver", default=None) # noqa: UP007
|
2024-04-17 13:39:24 +00:00
|
|
|
release_date: Optional[date] = None # noqa: UP007
|
|
|
|
release_notes: Optional[str] = Field(alias="release_note", default=None) # noqa: UP007
|
|
|
|
fw_size: Optional[int] = None # noqa: UP007
|
|
|
|
oem_id: Optional[str] = None # noqa: UP007
|
2024-02-19 20:29:09 +00:00
|
|
|
needs_upgrade: bool = Field(alias="need_to_upgrade")
|
|
|
|
|
|
|
|
@validator("release_date", pre=True)
|
2024-11-10 18:55:13 +00:00
|
|
|
def _release_date_optional(cls, v: str) -> str | None:
|
2024-02-19 20:29:09 +00:00
|
|
|
if not v:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
@property
|
2024-11-10 18:55:13 +00:00
|
|
|
def update_available(self) -> bool:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Return True if update available."""
|
|
|
|
if self.status != 0:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class Firmware(SmartModule):
|
|
|
|
"""Implementation of firmware module."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "firmware"
|
2024-07-11 15:21:59 +00:00
|
|
|
MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60 * 24
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def __init__(self, device: SmartDevice, module: str) -> None:
|
2024-02-19 20:29:09 +00:00
|
|
|
super().__init__(device, module)
|
2024-08-30 17:01:54 +00:00
|
|
|
self._firmware_update_info: UpdateInfo | None = None
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def _initialize_features(self) -> None:
|
2024-08-30 17:01:54 +00:00
|
|
|
"""Initialize features."""
|
|
|
|
device = self._device
|
2024-03-12 17:18:08 +00:00
|
|
|
if self.supported_version > 1:
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
2024-05-07 09:13:35 +00:00
|
|
|
id="auto_update_enabled",
|
|
|
|
name="Auto update enabled",
|
2024-03-12 17:18:08 +00:00
|
|
|
container=self,
|
|
|
|
attribute_getter="auto_update_enabled",
|
|
|
|
attribute_setter="set_auto_update_enabled",
|
2024-04-24 16:38:52 +00:00
|
|
|
type=Feature.Type.Switch,
|
2024-03-12 17:18:08 +00:00
|
|
|
)
|
2024-02-19 20:29:09 +00:00
|
|
|
)
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
2024-05-07 09:13:35 +00:00
|
|
|
id="update_available",
|
|
|
|
name="Update available",
|
2024-02-19 20:29:09 +00:00
|
|
|
container=self,
|
|
|
|
attribute_getter="update_available",
|
2024-04-24 16:38:52 +00:00
|
|
|
type=Feature.Type.BinarySensor,
|
2024-05-07 09:13:35 +00:00
|
|
|
category=Feature.Category.Info,
|
2024-02-19 20:29:09 +00:00
|
|
|
)
|
|
|
|
)
|
2024-05-08 23:43:07 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
|
|
|
id="current_firmware_version",
|
|
|
|
name="Current firmware version",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="current_firmware",
|
|
|
|
category=Feature.Category.Debug,
|
2024-06-25 16:30:36 +00:00
|
|
|
type=Feature.Type.Sensor,
|
2024-05-08 23:43:07 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
|
|
|
id="available_firmware_version",
|
|
|
|
name="Available firmware version",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="latest_firmware",
|
|
|
|
category=Feature.Category.Debug,
|
2024-06-25 16:30:36 +00:00
|
|
|
type=Feature.Type.Sensor,
|
2024-05-08 23:43:07 +00:00
|
|
|
)
|
|
|
|
)
|
2024-08-30 17:01:54 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
|
|
|
id="check_latest_firmware",
|
|
|
|
name="Check latest firmware",
|
|
|
|
container=self,
|
|
|
|
attribute_setter="check_latest_firmware",
|
|
|
|
category=Feature.Category.Info,
|
|
|
|
type=Feature.Type.Action,
|
|
|
|
)
|
|
|
|
)
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def query(self) -> dict:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Query to execute during the update cycle."""
|
2024-03-12 17:18:08 +00:00
|
|
|
if self.supported_version > 1:
|
2024-08-30 17:01:54 +00:00
|
|
|
return {"get_auto_update_info": None}
|
|
|
|
return {}
|
|
|
|
|
|
|
|
async def check_latest_firmware(self) -> UpdateInfo | None:
|
|
|
|
"""Check for the latest firmware for the device."""
|
|
|
|
try:
|
|
|
|
fw = await self.call("get_latest_fw")
|
|
|
|
self._firmware_update_info = UpdateInfo.parse_obj(fw["get_latest_fw"])
|
|
|
|
return self._firmware_update_info
|
|
|
|
except Exception:
|
|
|
|
_LOGGER.exception("Error getting latest firmware for %s:", self._device)
|
|
|
|
self._firmware_update_info = None
|
|
|
|
return None
|
2024-02-19 20:29:09 +00:00
|
|
|
|
|
|
|
@property
|
2024-05-08 23:43:07 +00:00
|
|
|
def current_firmware(self) -> str:
|
|
|
|
"""Return the current firmware version."""
|
|
|
|
return self._device.hw_info["sw_ver"]
|
|
|
|
|
|
|
|
@property
|
2024-08-30 17:01:54 +00:00
|
|
|
def latest_firmware(self) -> str | None:
|
2024-05-08 23:43:07 +00:00
|
|
|
"""Return the latest firmware version."""
|
2024-08-30 17:01:54 +00:00
|
|
|
if not self._firmware_update_info:
|
|
|
|
return None
|
|
|
|
return self._firmware_update_info.version
|
2024-05-08 23:43:07 +00:00
|
|
|
|
|
|
|
@property
|
2024-08-30 17:01:54 +00:00
|
|
|
def firmware_update_info(self) -> UpdateInfo | None:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Return latest firmware information."""
|
2024-08-30 17:01:54 +00:00
|
|
|
return self._firmware_update_info
|
2024-02-19 20:29:09 +00:00
|
|
|
|
|
|
|
@property
|
2024-04-23 11:56:32 +00:00
|
|
|
def update_available(self) -> bool | None:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Return True if update is available."""
|
2024-08-30 17:01:54 +00:00
|
|
|
if not self._device.is_cloud_connected or not self._firmware_update_info:
|
2024-04-23 11:56:32 +00:00
|
|
|
return None
|
2024-08-30 17:01:54 +00:00
|
|
|
return self._firmware_update_info.update_available
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-05-08 23:43:07 +00:00
|
|
|
async def get_update_state(self) -> DownloadState:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Return update state."""
|
2024-05-08 23:43:07 +00:00
|
|
|
resp = await self.call("get_fw_download_state")
|
|
|
|
state = resp["get_fw_download_state"]
|
|
|
|
return DownloadState(**state)
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-07-11 15:21:59 +00:00
|
|
|
@allow_update_after
|
2024-05-08 23:43:07 +00:00
|
|
|
async def update(
|
|
|
|
self, progress_cb: Callable[[DownloadState], Coroutine] | None = None
|
2024-11-10 18:55:13 +00:00
|
|
|
) -> dict:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Update the device firmware."""
|
2024-08-30 17:01:54 +00:00
|
|
|
if not self._firmware_update_info:
|
|
|
|
raise KasaException(
|
|
|
|
"You must call check_latest_firmware before calling update"
|
|
|
|
)
|
|
|
|
if not self.update_available:
|
|
|
|
raise KasaException("A new update must be available to call update")
|
2024-05-08 23:43:07 +00:00
|
|
|
current_fw = self.current_firmware
|
|
|
|
_LOGGER.info(
|
|
|
|
"Going to upgrade from %s to %s",
|
|
|
|
current_fw,
|
2024-08-30 17:01:54 +00:00
|
|
|
self._firmware_update_info.version,
|
2024-05-08 23:43:07 +00:00
|
|
|
)
|
|
|
|
await self.call("fw_download")
|
|
|
|
|
|
|
|
# TODO: read timeout from get_auto_update_info or from get_fw_download_state?
|
|
|
|
async with asyncio_timeout(60 * 5):
|
|
|
|
while True:
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
try:
|
|
|
|
state = await self.get_update_state()
|
|
|
|
except Exception as ex:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Got exception, maybe the device is rebooting? %s", ex
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2024-08-30 14:13:14 +00:00
|
|
|
_LOGGER.debug("Update state: %s", state)
|
2024-05-08 23:43:07 +00:00
|
|
|
if progress_cb is not None:
|
|
|
|
asyncio.create_task(progress_cb(state))
|
|
|
|
|
|
|
|
if state.status == 0:
|
|
|
|
_LOGGER.info(
|
|
|
|
"Update idle, hopefully updated to %s",
|
2024-08-30 17:01:54 +00:00
|
|
|
self._firmware_update_info.version,
|
2024-05-08 23:43:07 +00:00
|
|
|
)
|
|
|
|
break
|
|
|
|
elif state.status == 2:
|
|
|
|
_LOGGER.info("Downloading firmware, progress: %s", state.progress)
|
|
|
|
elif state.status == 3:
|
|
|
|
upgrade_sleep = state.upgrade_time
|
|
|
|
_LOGGER.info(
|
|
|
|
"Flashing firmware, sleeping for %s before checking status",
|
|
|
|
upgrade_sleep,
|
|
|
|
)
|
|
|
|
await asyncio.sleep(upgrade_sleep)
|
|
|
|
elif state.status < 0:
|
|
|
|
_LOGGER.error("Got error: %s", state.status)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("Unhandled state code: %s", state)
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
return state.dict()
|
|
|
|
|
2024-02-19 20:29:09 +00:00
|
|
|
@property
|
2024-08-30 17:01:54 +00:00
|
|
|
def auto_update_enabled(self) -> bool:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Return True if autoupdate is enabled."""
|
2024-08-30 17:01:54 +00:00
|
|
|
return "enable" in self.data and self.data["enable"]
|
2024-02-19 20:29:09 +00:00
|
|
|
|
2024-07-11 15:21:59 +00:00
|
|
|
@allow_update_after
|
2024-11-10 18:55:13 +00:00
|
|
|
async def set_auto_update_enabled(self, enabled: bool) -> dict:
|
2024-02-19 20:29:09 +00:00
|
|
|
"""Change autoupdate setting."""
|
2024-08-30 17:01:54 +00:00
|
|
|
data = {**self.data, "enable": enabled}
|
2024-11-10 18:55:13 +00:00
|
|
|
return await self.call("set_auto_update_info", data)
|