mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-06-24 09:48:53 +02:00
converted update sensors
This commit is contained in:
parent
eae9fd7e2d
commit
15bc156d74
5 changed files with 45 additions and 36 deletions
|
@ -7,7 +7,7 @@ PLATFORMS = [
|
||||||
# Platform.DEVICE_TRACKER,
|
# Platform.DEVICE_TRACKER,
|
||||||
Platform.SWITCH,
|
Platform.SWITCH,
|
||||||
# Platform.BUTTON,
|
# Platform.BUTTON,
|
||||||
# Platform.UPDATE,
|
Platform.UPDATE,
|
||||||
]
|
]
|
||||||
|
|
||||||
DOMAIN = "mikrotik_router"
|
DOMAIN = "mikrotik_router"
|
||||||
|
|
|
@ -13,8 +13,8 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .entity import MikrotikEntity, async_add_entities
|
|
||||||
from .coordinator import MikrotikCoordinator
|
from .coordinator import MikrotikCoordinator
|
||||||
|
from .entity import MikrotikEntity, async_add_entities
|
||||||
from .helper import format_attribute
|
from .helper import format_attribute
|
||||||
from .sensor_types import (
|
from .sensor_types import (
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||||
from homeassistant.helpers.entity import EntityCategory
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
|
|
|
@ -1,40 +1,46 @@
|
||||||
"""Support for the Mikrotik Router update service."""
|
"""Support for the Mikrotik Router update service."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
from logging import getLogger
|
||||||
from typing import Any
|
from collections.abc import Mapping
|
||||||
from requests import get as requests_get
|
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 (
|
from homeassistant.components.update import (
|
||||||
UpdateEntity,
|
UpdateEntity,
|
||||||
UpdateDeviceClass,
|
UpdateDeviceClass,
|
||||||
UpdateEntityFeature,
|
UpdateEntityFeature,
|
||||||
)
|
)
|
||||||
from .entity import model_async_setup_entry, MikrotikEntity
|
|
||||||
|
from .coordinator import MikrotikCoordinator
|
||||||
|
from .entity import MikrotikEntity, async_add_entities
|
||||||
from .update_types import (
|
from .update_types import (
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
SENSOR_SERVICES,
|
SENSOR_SERVICES,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = getLogger(__name__)
|
||||||
DEVICE_UPDATE = "device_update"
|
DEVICE_UPDATE = "device_update"
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# async_setup_entry
|
# 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"""
|
"""Set up entry for component"""
|
||||||
dispatcher = {
|
dispatcher = {
|
||||||
"MikrotikRouterOSUpdate": MikrotikRouterOSUpdate,
|
"MikrotikRouterOSUpdate": MikrotikRouterOSUpdate,
|
||||||
"MikrotikRouterBoardFWUpdate": MikrotikRouterBoardFWUpdate,
|
"MikrotikRouterBoardFWUpdate": MikrotikRouterBoardFWUpdate,
|
||||||
}
|
}
|
||||||
await model_async_setup_entry(
|
await async_add_entities(hass, config_entry, dispatcher)
|
||||||
hass,
|
|
||||||
config_entry,
|
|
||||||
async_add_entities,
|
|
||||||
SENSOR_SERVICES,
|
|
||||||
SENSOR_TYPES,
|
|
||||||
dispatcher,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -45,13 +51,12 @@ class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
inst,
|
coordinator: MikrotikCoordinator,
|
||||||
uid: "",
|
|
||||||
mikrotik_controller,
|
|
||||||
entity_description,
|
entity_description,
|
||||||
|
uid: str | None = None,
|
||||||
):
|
):
|
||||||
"""Set up device update entity."""
|
"""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.INSTALL
|
||||||
self._attr_supported_features |= UpdateEntityFeature.BACKUP
|
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:
|
async def async_install(self, version: str, backup: bool, **kwargs: Any) -> None:
|
||||||
"""Install an update."""
|
"""Install an update."""
|
||||||
if backup:
|
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:
|
async def async_release_notes(self) -> str:
|
||||||
"""Return the release notes."""
|
"""Return the release notes."""
|
||||||
try:
|
try:
|
||||||
response = await self._ctrl.hass.async_add_executor_job(
|
response = await self.coordinator.hass.async_add_executor_job(
|
||||||
requests_get,
|
requests_get,
|
||||||
f"https://mikrotik.com/download/changelogs?ax=loadLog&val={self._data['latest-version']}",
|
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:
|
async def async_install(self, version: str, backup: bool, **kwargs: Any) -> None:
|
||||||
"""Install an update."""
|
"""Install an update."""
|
||||||
self._ctrl.execute("/system/routerboard", "upgrade", None, None)
|
self.coordinator.execute("/system/routerboard", "upgrade", None, None)
|
||||||
self._ctrl.execute("/system", "reboot", None, None)
|
self.coordinator.execute("/system", "reboot", None, None)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
"""Definitions for Mikrotik Router update entities."""
|
"""Definitions for Mikrotik Router update entities."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from homeassistant.components.update import UpdateEntityDescription
|
from homeassistant.components.update import UpdateEntityDescription
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,22 +11,22 @@ from homeassistant.components.update import UpdateEntityDescription
|
||||||
class MikrotikUpdateEntityDescription(UpdateEntityDescription):
|
class MikrotikUpdateEntityDescription(UpdateEntityDescription):
|
||||||
"""Class describing mikrotik entities."""
|
"""Class describing mikrotik entities."""
|
||||||
|
|
||||||
ha_group: str = ""
|
ha_group: str | None = None
|
||||||
ha_connection: str = ""
|
ha_connection: str | None = None
|
||||||
ha_connection_value: str = ""
|
ha_connection_value: str | None = None
|
||||||
title: str = ""
|
title: str | None = None
|
||||||
data_path: str = ""
|
data_path: str | None = None
|
||||||
data_attribute: str = "available"
|
data_attribute: str = "available"
|
||||||
data_name: str = ""
|
data_name: str | None = None
|
||||||
data_name_comment: bool = False
|
data_name_comment: bool = False
|
||||||
data_uid: str = ""
|
data_uid: str | None = None
|
||||||
data_reference: str = ""
|
data_reference: str | None = None
|
||||||
data_attributes_list: List = field(default_factory=lambda: [])
|
data_attributes_list: List = field(default_factory=lambda: [])
|
||||||
func: str = "MikrotikRouterOSUpdate"
|
func: str = "MikrotikRouterOSUpdate"
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[MikrotikUpdateEntityDescription, ...] = (
|
||||||
"system_rosupdate": MikrotikUpdateEntityDescription(
|
MikrotikUpdateEntityDescription(
|
||||||
key="system_rosupdate",
|
key="system_rosupdate",
|
||||||
name="RouterOS update",
|
name="RouterOS update",
|
||||||
ha_group="System",
|
ha_group="System",
|
||||||
|
@ -34,7 +37,7 @@ SENSOR_TYPES = {
|
||||||
data_reference="",
|
data_reference="",
|
||||||
func="MikrotikRouterOSUpdate",
|
func="MikrotikRouterOSUpdate",
|
||||||
),
|
),
|
||||||
"system_rbfwupdate": MikrotikUpdateEntityDescription(
|
MikrotikUpdateEntityDescription(
|
||||||
key="system_rbfwupdate",
|
key="system_rbfwupdate",
|
||||||
name="RouterBOARD firmware update",
|
name="RouterBOARD firmware update",
|
||||||
ha_group="System",
|
ha_group="System",
|
||||||
|
@ -46,7 +49,7 @@ SENSOR_TYPES = {
|
||||||
data_reference="",
|
data_reference="",
|
||||||
func="MikrotikRouterBoardFWUpdate",
|
func="MikrotikRouterBoardFWUpdate",
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
SENSOR_SERVICES = {}
|
SENSOR_SERVICES = {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue