Replaced fwupdate with new HA update entity #229

This commit is contained in:
Tomaae 2022-08-11 12:56:28 +02:00
parent 5b1c88fa6a
commit c639d65332
No known key found for this signature in database
GPG key ID: 8360BBD8A381D1C0
4 changed files with 145 additions and 14 deletions

View file

@ -111,20 +111,6 @@ class MikrotikBinarySensorEntityDescription(BinarySensorEntityDescription):
SENSOR_TYPES = { SENSOR_TYPES = {
"system_fwupdate": MikrotikBinarySensorEntityDescription(
key="system_fwupdate",
name="Firmware update",
icon_enabled="",
icon_disabled="",
device_class=BinarySensorDeviceClass.UPDATE,
entity_category=EntityCategory.DIAGNOSTIC,
ha_group="System",
data_path="fw-update",
data_name="",
data_uid="",
data_reference="",
data_attributes_list=DEVICE_ATTRIBUTES_FWUPDATE,
),
"system_ups": MikrotikBinarySensorEntityDescription( "system_ups": MikrotikBinarySensorEntityDescription(
key="system_ups", key="system_ups",
name="UPS", name="UPS",

View file

@ -7,6 +7,7 @@ PLATFORMS = [
Platform.DEVICE_TRACKER, Platform.DEVICE_TRACKER,
Platform.SWITCH, Platform.SWITCH,
Platform.BUTTON, Platform.BUTTON,
Platform.UPDATE,
] ]
DOMAIN = "mikrotik_router" DOMAIN = "mikrotik_router"

View file

@ -0,0 +1,108 @@
"""Support for the Mikrotik Router update service."""
import logging
from typing import Any
from requests import get as requests_get
from homeassistant.components.update import (
UpdateEntity,
UpdateDeviceClass,
UpdateEntityFeature,
)
from .model import model_async_setup_entry, MikrotikEntity
from .update_types import (
SENSOR_TYPES,
SENSOR_SERVICES,
)
_LOGGER = logging.getLogger(__name__)
DEVICE_UPDATE = "device_update"
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up entry for component"""
dispatcher = {
"MikrotikUpdate": MikrotikUpdate,
}
await model_async_setup_entry(
hass,
config_entry,
async_add_entities,
SENSOR_SERVICES,
SENSOR_TYPES,
dispatcher,
)
# ---------------------------
# MikrotikUpdate
# ---------------------------
class MikrotikUpdate(MikrotikEntity, UpdateEntity):
"""Define an Mikrotik Controller Update entity."""
TYPE = DEVICE_UPDATE
_attr_device_class = UpdateDeviceClass.FIRMWARE
def __init__(
self,
inst,
uid: "",
mikrotik_controller,
entity_description,
):
"""Set up device update entity."""
super().__init__(inst, uid, mikrotik_controller, entity_description)
self._attr_supported_features = UpdateEntityFeature.INSTALL
self._attr_supported_features |= UpdateEntityFeature.BACKUP
self._attr_supported_features |= UpdateEntityFeature.RELEASE_NOTES
@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:
self._ctrl.execute("/system/backup", "save", None, None)
self._ctrl.execute("/system/package/update", "install", None, None)
# await self.controller.api.devices.upgrade(self.device.mac)
async def async_release_notes(self) -> str:
"""Return the release notes."""
try:
response = await self._ctrl.hass.async_add_executor_job(
requests_get,
f"https://mikrotik.com/download/changelogs?ax=loadLog&val={self._data['latest-version']}",
)
if response.status_code == 200:
return response.text.replace(chr(10), "<br />")
except:
_LOGGER.warning("Mikrotik %s failed to download release notes", self._host)
return "Failed to download release notes"
@property
def release_url(self) -> str:
"""URL to the full release notes of the latest version available."""
return "https://mikrotik.com/download/changelogs"

View file

@ -0,0 +1,36 @@
"""Definitions for Mikrotik Router update entities."""
from dataclasses import dataclass, field
from typing import List
from homeassistant.components.update import UpdateEntityDescription
@dataclass
class MikrotikUpdateEntityDescription(UpdateEntityDescription):
"""Class describing mikrotik entities."""
ha_group: str = ""
ha_connection: str = ""
ha_connection_value: str = ""
data_path: str = ""
data_attribute: str = "available"
data_name: str = ""
data_name_comment: bool = False
data_uid: str = ""
data_reference: str = ""
data_attributes_list: List = field(default_factory=lambda: [])
func: str = "MikrotikUpdate"
SENSOR_TYPES = {
"system_fwupdate": MikrotikUpdateEntityDescription(
key="system_fwupdate",
name="Firmware update",
ha_group="System",
data_path="fw-update",
data_name="",
data_uid="",
data_reference="",
),
}
SENSOR_SERVICES = {}