2023-08-08 00:50:09 +02:00
|
|
|
"""Mikrotik sensor platform."""
|
|
|
|
from __future__ import annotations
|
2019-12-03 03:09:30 +01:00
|
|
|
|
2023-08-08 00:50:09 +02:00
|
|
|
from logging import getLogger
|
2022-02-01 14:06:33 +01:00
|
|
|
from collections.abc import Mapping
|
2023-08-08 00:50:09 +02:00
|
|
|
from datetime import date, datetime
|
|
|
|
from decimal import Decimal
|
|
|
|
from typing import Any
|
|
|
|
|
2022-02-01 10:12:38 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2023-08-08 00:50:09 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
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
|
2022-02-02 22:13:39 +01:00
|
|
|
from .helper import format_attribute
|
2022-02-01 10:12:38 +01:00
|
|
|
from .sensor_types import (
|
|
|
|
SENSOR_TYPES,
|
2022-03-27 03:29:36 +02:00
|
|
|
SENSOR_SERVICES,
|
2022-02-04 22:55:20 +01:00
|
|
|
DEVICE_ATTRIBUTES_IFACE_ETHER,
|
|
|
|
DEVICE_ATTRIBUTES_IFACE_SFP,
|
2022-08-21 22:40:37 +02:00
|
|
|
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
|
2022-02-01 10:12:38 +01:00
|
|
|
)
|
2019-12-03 03:09:30 +01:00
|
|
|
|
2023-08-08 00:50:09 +02:00
|
|
|
_LOGGER = getLogger(__name__)
|
2019-12-03 03:09:30 +01:00
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
2023-08-08 00:50:09 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
_async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-03-27 03:29:36 +02:00
|
|
|
"""Set up entry for component"""
|
|
|
|
dispatcher = {
|
|
|
|
"MikrotikSensor": MikrotikSensor,
|
|
|
|
"MikrotikInterfaceTrafficSensor": MikrotikInterfaceTrafficSensor,
|
2023-08-09 09:25:33 +02:00
|
|
|
# "MikrotikClientTrafficSensor": MikrotikClientTrafficSensor,
|
2022-03-27 03:29:36 +02:00
|
|
|
}
|
2023-08-08 00:50:09 +02:00
|
|
|
await async_add_entities(hass, config_entry, dispatcher)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikSensor
|
2019-12-03 03:09:30 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikSensor(MikrotikEntity, SensorEntity):
|
2023-08-08 00:50:09 +02:00
|
|
|
"""Define an Mikrotik sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: MikrotikCoordinator,
|
|
|
|
entity_description,
|
|
|
|
uid: str | None = None,
|
|
|
|
):
|
|
|
|
super().__init__(coordinator, entity_description, uid)
|
|
|
|
self._attr_suggested_unit_of_measurement = (
|
|
|
|
self.entity_description.suggested_unit_of_measurement
|
|
|
|
)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
@property
|
2023-08-08 00:50:09 +02:00
|
|
|
def native_value(self) -> StateType | date | datetime | Decimal:
|
|
|
|
"""Return the value reported by the sensor."""
|
|
|
|
return self._data[self.entity_description.data_attribute]
|
2019-12-03 03:09:30 +01:00
|
|
|
|
2022-02-01 14:06:33 +01:00
|
|
|
@property
|
2023-08-08 00:50:09 +02:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2022-02-01 14:06:33 +01:00
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
if self.entity_description.native_unit_of_measurement:
|
|
|
|
if self.entity_description.native_unit_of_measurement.startswith("data__"):
|
|
|
|
uom = self.entity_description.native_unit_of_measurement[6:]
|
|
|
|
if uom in self._data:
|
2023-08-08 00:50:09 +02:00
|
|
|
return self._data[uom]
|
2022-02-01 14:06:33 +01:00
|
|
|
|
|
|
|
return self.entity_description.native_unit_of_measurement
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
|
2022-02-04 22:55:20 +01:00
|
|
|
# ---------------------------
|
|
|
|
# MikrotikInterfaceTrafficSensor
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikInterfaceTrafficSensor(MikrotikSensor):
|
2022-02-04 22:55:20 +01:00
|
|
|
"""Define an Mikrotik MikrotikInterfaceTrafficSensor sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attributes = super().extra_state_attributes
|
|
|
|
|
|
|
|
if self._data["type"] == "ether":
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_ETHER:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
|
|
|
if "sfp-shutdown-temperature" in self._data:
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_SFP:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
2022-08-21 22:40:37 +02:00
|
|
|
elif self._data["type"] == "wlan":
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_WIRELESS:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
2022-02-04 22:55:20 +01:00
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
2023-08-09 09:25:33 +02:00
|
|
|
# # ---------------------------
|
|
|
|
# # MikrotikClientTrafficSensor
|
|
|
|
# # ---------------------------
|
|
|
|
# class MikrotikClientTrafficSensor(MikrotikSensor):
|
|
|
|
# """Define an Mikrotik MikrotikClientTrafficSensor sensor."""
|
|
|
|
#
|
|
|
|
# @property
|
|
|
|
# def name(self) -> str:
|
|
|
|
# """Return the name."""
|
|
|
|
# return f"{self.entity_description.name}"
|
|
|
|
#
|
|
|
|
# # @property
|
|
|
|
# # def available(self) -> bool:
|
|
|
|
# # """Return if controller and accounting feature in Mikrotik is available.
|
|
|
|
# # Additional check for lan-tx/rx sensors
|
|
|
|
# # """
|
|
|
|
# # if self.entity_description.data_attribute in ["lan-tx", "lan-rx"]:
|
|
|
|
# # return (
|
|
|
|
# # self.coordinator.connected()
|
|
|
|
# # and self._data["available"]
|
|
|
|
# # and self._data["local_accounting"]
|
|
|
|
# # )
|
|
|
|
# # else:
|
|
|
|
# # return self.coordinator.connected() and self._data["available"]
|