2022-08-11 12:56:28 +02:00
|
|
|
"""Support for the Mikrotik Router update service."""
|
2024-04-12 14:39:36 +03:00
|
|
|
|
2023-08-09 09:53:48 +02:00
|
|
|
from __future__ import annotations
|
2022-08-11 12:56:28 +02:00
|
|
|
|
2023-08-09 09:53:48 +02:00
|
|
|
from logging import getLogger
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2024-04-12 16:13:54 +03:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2023-08-09 09:53:48 +02:00
|
|
|
|
2022-08-11 12:56:28 +02:00
|
|
|
from homeassistant.components.update import (
|
|
|
|
UpdateEntity,
|
|
|
|
UpdateDeviceClass,
|
|
|
|
UpdateEntityFeature,
|
|
|
|
)
|
2023-08-09 09:53:48 +02:00
|
|
|
|
|
|
|
from .coordinator import MikrotikCoordinator
|
|
|
|
from .entity import MikrotikEntity, async_add_entities
|
2022-08-11 12:56:28 +02:00
|
|
|
from .update_types import (
|
|
|
|
SENSOR_TYPES,
|
|
|
|
SENSOR_SERVICES,
|
|
|
|
)
|
|
|
|
|
2023-08-09 09:53:48 +02:00
|
|
|
_LOGGER = getLogger(__name__)
|
2022-08-11 12:56:28 +02:00
|
|
|
DEVICE_UPDATE = "device_update"
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
2023-08-09 09:53:48 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
_async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-08-11 12:56:28 +02:00
|
|
|
"""Set up entry for component"""
|
|
|
|
dispatcher = {
|
2022-08-11 14:58:30 +02:00
|
|
|
"MikrotikRouterOSUpdate": MikrotikRouterOSUpdate,
|
|
|
|
"MikrotikRouterBoardFWUpdate": MikrotikRouterBoardFWUpdate,
|
2022-08-11 12:56:28 +02:00
|
|
|
}
|
2023-08-09 09:53:48 +02:00
|
|
|
await async_add_entities(hass, config_entry, dispatcher)
|
2022-08-11 12:56:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
2022-08-11 14:58:30 +02:00
|
|
|
# MikrotikRouterOSUpdate
|
2022-08-11 12:56:28 +02:00
|
|
|
# ---------------------------
|
2022-08-11 14:58:30 +02:00
|
|
|
class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
|
2022-08-11 12:56:28 +02:00
|
|
|
"""Define an Mikrotik Controller Update entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-08-09 09:53:48 +02:00
|
|
|
coordinator: MikrotikCoordinator,
|
2022-08-11 12:56:28 +02:00
|
|
|
entity_description,
|
2023-08-09 09:53:48 +02:00
|
|
|
uid: str | None = None,
|
2022-08-11 12:56:28 +02:00
|
|
|
):
|
|
|
|
"""Set up device update entity."""
|
2023-08-09 09:53:48 +02:00
|
|
|
super().__init__(coordinator, entity_description, uid)
|
2022-08-11 12:56:28 +02:00
|
|
|
|
|
|
|
self._attr_supported_features = UpdateEntityFeature.INSTALL
|
|
|
|
self._attr_supported_features |= UpdateEntityFeature.BACKUP
|
|
|
|
self._attr_supported_features |= UpdateEntityFeature.RELEASE_NOTES
|
2022-08-17 16:23:41 +02:00
|
|
|
self._attr_title = self.entity_description.title
|
2022-08-11 12:56:28 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._data[self.entity_description.data_attribute]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def installed_version(self) -> str:
|
|
|
|
"""Version installed and in use."""
|
|
|
|
return self._data["installed-version"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def latest_version(self) -> str:
|
|
|
|
"""Latest version available for install."""
|
|
|
|
return self._data["latest-version"]
|
|
|
|
|
|
|
|
async def options_updated(self) -> None:
|
|
|
|
"""No action needed."""
|
|
|
|
|
|
|
|
async def async_install(self, version: str, backup: bool, **kwargs: Any) -> None:
|
|
|
|
"""Install an update."""
|
|
|
|
if backup:
|
2023-08-09 09:53:48 +02:00
|
|
|
self.coordinator.execute("/system/backup", "save", None, None)
|
2022-08-11 12:56:28 +02:00
|
|
|
|
2023-08-09 09:53:48 +02:00
|
|
|
self.coordinator.execute("/system/package/update", "install", None, None)
|
2022-08-11 12:56:28 +02:00
|
|
|
|
|
|
|
async def async_release_notes(self) -> str:
|
|
|
|
"""Return the release notes."""
|
|
|
|
try:
|
2024-04-12 16:13:54 +03:00
|
|
|
session = async_get_clientsession(self.hass)
|
|
|
|
async with session.get(
|
|
|
|
f"https://cdn.mikrotik.com/routeros/{self._data['latest-version']}/CHANGELOG"
|
|
|
|
) as response:
|
|
|
|
if response.status == 200:
|
|
|
|
text = await response.text()
|
|
|
|
return text.replace("*) ", "- ")
|
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Failed to fetch release notes due to a network error."
|
|
|
|
)
|
|
|
|
return "Failed to fetch release notes due to a network error."
|
2022-08-11 14:57:29 +02:00
|
|
|
except Exception as e:
|
|
|
|
_LOGGER.warning("Failed to download release notes (%s)", e)
|
2022-08-11 12:56:28 +02:00
|
|
|
|
2024-04-12 15:27:52 +03:00
|
|
|
return "Error fetching release notes."
|
2022-08-11 12:56:28 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def release_url(self) -> str:
|
|
|
|
"""URL to the full release notes of the latest version available."""
|
|
|
|
return "https://mikrotik.com/download/changelogs"
|
2022-08-11 14:58:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# MikrotikRouterBoardFWUpdate
|
|
|
|
# ---------------------------
|
|
|
|
class MikrotikRouterBoardFWUpdate(MikrotikEntity, UpdateEntity):
|
|
|
|
"""Define an Mikrotik Controller Update entity."""
|
|
|
|
|
2022-08-17 12:54:26 +02:00
|
|
|
TYPE = DEVICE_UPDATE
|
|
|
|
_attr_device_class = UpdateDeviceClass.FIRMWARE
|
2022-08-17 13:09:59 +02:00
|
|
|
|
2022-08-11 14:58:30 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-08-09 10:18:46 +02:00
|
|
|
coordinator: MikrotikCoordinator,
|
2022-08-11 14:58:30 +02:00
|
|
|
entity_description,
|
2023-08-09 10:18:46 +02:00
|
|
|
uid: str | None = None,
|
2022-08-11 14:58:30 +02:00
|
|
|
):
|
|
|
|
"""Set up device update entity."""
|
2023-08-09 10:18:46 +02:00
|
|
|
super().__init__(coordinator, entity_description, uid)
|
2022-08-11 14:58:30 +02:00
|
|
|
|
|
|
|
self._attr_supported_features = UpdateEntityFeature.INSTALL
|
2022-08-17 16:23:41 +02:00
|
|
|
self._attr_title = self.entity_description.title
|
2022-08-11 14:58:30 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return (
|
|
|
|
self.data["routerboard"]["current-firmware"]
|
|
|
|
!= self.data["routerboard"]["upgrade-firmware"]
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def installed_version(self) -> str:
|
|
|
|
"""Version installed and in use."""
|
|
|
|
return self._data["current-firmware"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def latest_version(self) -> str:
|
|
|
|
"""Latest version available for install."""
|
|
|
|
return self._data["upgrade-firmware"]
|
|
|
|
|
|
|
|
async def options_updated(self) -> None:
|
|
|
|
"""No action needed."""
|
|
|
|
|
|
|
|
async def async_install(self, version: str, backup: bool, **kwargs: Any) -> None:
|
|
|
|
"""Install an update."""
|
2023-08-09 09:53:48 +02:00
|
|
|
self.coordinator.execute("/system/routerboard", "upgrade", None, None)
|
|
|
|
self.coordinator.execute("/system", "reboot", None, None)
|