converted update sensors

This commit is contained in:
Tomaae 2023-08-09 09:53:48 +02:00
parent eae9fd7e2d
commit 15bc156d74
No known key found for this signature in database
GPG key ID: 2F97770867DAA4E6
5 changed files with 45 additions and 36 deletions

View file

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

View file

@ -13,8 +13,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .entity import MikrotikEntity, async_add_entities
from .coordinator import MikrotikCoordinator
from .entity import MikrotikEntity, async_add_entities
from .helper import format_attribute
from .sensor_types import (
SENSOR_TYPES,

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass, field
from typing import List
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.entity import EntityCategory
from homeassistant.components.sensor import (

View file

@ -1,40 +1,46 @@
"""Support for the Mikrotik Router update service."""
from __future__ import annotations
import logging
from typing import Any
from logging import getLogger
from collections.abc import Mapping
from requests import get as requests_get
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.update import (
UpdateEntity,
UpdateDeviceClass,
UpdateEntityFeature,
)
from .entity import model_async_setup_entry, MikrotikEntity
from .coordinator import MikrotikCoordinator
from .entity import MikrotikEntity, async_add_entities
from .update_types import (
SENSOR_TYPES,
SENSOR_SERVICES,
)
_LOGGER = logging.getLogger(__name__)
_LOGGER = getLogger(__name__)
DEVICE_UPDATE = "device_update"
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
_async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry for component"""
dispatcher = {
"MikrotikRouterOSUpdate": MikrotikRouterOSUpdate,
"MikrotikRouterBoardFWUpdate": MikrotikRouterBoardFWUpdate,
}
await model_async_setup_entry(
hass,
config_entry,
async_add_entities,
SENSOR_SERVICES,
SENSOR_TYPES,
dispatcher,
)
await async_add_entities(hass, config_entry, dispatcher)
# ---------------------------
@ -45,13 +51,12 @@ class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
def __init__(
self,
inst,
uid: "",
mikrotik_controller,
coordinator: MikrotikCoordinator,
entity_description,
uid: str | None = None,
):
"""Set up device update entity."""
super().__init__(inst, uid, mikrotik_controller, entity_description)
super().__init__(coordinator, entity_description, uid)
self._attr_supported_features = UpdateEntityFeature.INSTALL
self._attr_supported_features |= UpdateEntityFeature.BACKUP
@ -79,14 +84,14 @@ class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
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.coordinator.execute("/system/backup", "save", None, None)
self._ctrl.execute("/system/package/update", "install", None, None)
self.coordinator.execute("/system/package/update", "install", None, None)
async def async_release_notes(self) -> str:
"""Return the release notes."""
try:
response = await self._ctrl.hass.async_add_executor_job(
response = await self.coordinator.hass.async_add_executor_job(
requests_get,
f"https://mikrotik.com/download/changelogs?ax=loadLog&val={self._data['latest-version']}",
)
@ -150,5 +155,5 @@ class MikrotikRouterBoardFWUpdate(MikrotikEntity, UpdateEntity):
async def async_install(self, version: str, backup: bool, **kwargs: Any) -> None:
"""Install an update."""
self._ctrl.execute("/system/routerboard", "upgrade", None, None)
self._ctrl.execute("/system", "reboot", None, None)
self.coordinator.execute("/system/routerboard", "upgrade", None, None)
self.coordinator.execute("/system", "reboot", None, None)

View file

@ -1,6 +1,9 @@
"""Definitions for Mikrotik Router update entities."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import List
from homeassistant.components.update import UpdateEntityDescription
@ -8,22 +11,22 @@ from homeassistant.components.update import UpdateEntityDescription
class MikrotikUpdateEntityDescription(UpdateEntityDescription):
"""Class describing mikrotik entities."""
ha_group: str = ""
ha_connection: str = ""
ha_connection_value: str = ""
title: str = ""
data_path: str = ""
ha_group: str | None = None
ha_connection: str | None = None
ha_connection_value: str | None = None
title: str | None = None
data_path: str | None = None
data_attribute: str = "available"
data_name: str = ""
data_name: str | None = None
data_name_comment: bool = False
data_uid: str = ""
data_reference: str = ""
data_uid: str | None = None
data_reference: str | None = None
data_attributes_list: List = field(default_factory=lambda: [])
func: str = "MikrotikRouterOSUpdate"
SENSOR_TYPES = {
"system_rosupdate": MikrotikUpdateEntityDescription(
SENSOR_TYPES: tuple[MikrotikUpdateEntityDescription, ...] = (
MikrotikUpdateEntityDescription(
key="system_rosupdate",
name="RouterOS update",
ha_group="System",
@ -34,7 +37,7 @@ SENSOR_TYPES = {
data_reference="",
func="MikrotikRouterOSUpdate",
),
"system_rbfwupdate": MikrotikUpdateEntityDescription(
MikrotikUpdateEntityDescription(
key="system_rbfwupdate",
name="RouterBOARD firmware update",
ha_group="System",
@ -46,7 +49,7 @@ SENSOR_TYPES = {
data_reference="",
func="MikrotikRouterBoardFWUpdate",
),
}
)
SENSOR_SERVICES = {}